Friday, October 19, 2012

After recently importing a Subversion repository into Git, along with the associated .gitignore file, I noticed that git status was performing poorly. The solution was to split the monolithic .gitignore file in the root folder into multiple .gitignore files spread throughout the repository.

I wrote the following PowerShell script to do so:
$content = type .gitignore | % { $_.Trim() } | ? { $_.StartsWith('/') } | % { $_.SubString(1) }
$content | % {
    $folder = split-path -Parent $_    
    if($folder.length -gt 0) {
        if(-not (test-path $folder)) {
            md $folder | Out-Null
        }
        $pattern = split-path -Leaf $_
        $newPattern = '/' + $pattern
        $ignoreFile = "$folder\.gitignore"
        $exists = $false
        if((test-path $ignoreFile ) -and (type $ignore) -notcontains $newPattern) {
            $exists = $true
        }
        if(!$exists) {
            Add-Content -Force -Path $ignoreFile $newPattern
        }
    }
}

0 comments:

Post a Comment