PowerShell function to add to svn:ignore

After many years using (and enjoying) Git, I had to start using Subversion again on a daily basis.

Git treats ignores on a regular file (.gitignore, whose rules are applied both to the same folder and to all subfolders), while Subversion treats ignores as an svn:property, which makes it harder to add new filemasks to the ignore list.

Using TortoiseSvn you can just click on the “add to ignore list”, but on command line you don’t have that.

This PowerShell function helps you on that. It will extract the svn:ignore property on any folder, include a new pattern to the list of ignores, and update the property. Enjoy!

function Svn-Add-Ignore
{
   param(
     [Parameter(Position=0,mandatory=$true)]
     [string] $folder,
     [Parameter(Position=1,mandatory=$true)]
     [string] $filemask
   )

   if (( Test-Path $folder) -eq $False)
   {
       Write-Host "Path $folder not found" -for red
       Exit 1
   }
   
   $ignores = (svn propget svn:ignore $folder)
   if($ignores)
   {
      $ignores = ($ignores -join "`n" | Out-String).Trim()   # convert multiple lines to single multiline string
      $ignores = $ignores + "`n" + $filemask
   }
   else
   {
      $ignores = $filemask
   }
   svn propset svn:ignore $ignores $folder
}

Sample usage:

Svn-Add-Ignore .\ "bin"
Svn-Add-Ignore .\ "obj"
Svn-Add-Ignore .\ ".vs"
Svn-Add-Ignore .\ "*.user"

comments powered by Disqus