Skip to content

Instantly share code, notes, and snippets.

@vbfox
Created November 10, 2010 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vbfox/670664 to your computer and use it in GitHub Desktop.
Save vbfox/670664 to your computer and use it in GitHub Desktop.
LINQPad program to remove 'svn:mime-type' properties with 'application/octet-stream' value from '.cs' files.
void Main()
{
var dir = "C:\\Code\\Repo\\";
var onlyChanged = true;
if (onlyChanged)
{
Console.WriteLine("Getting files from SVN... ");
var changed = GetSvnStatus(dir)
.Where(t => t.Item1 == 'A' || t.Item1 == 'M')
.Select(t => t.Item2)
.Select(p => { Console.WriteLine(" - {0}", p); return p; })
.Where(p => Path.GetExtension(p) == ".cs")
.Where(p => System.IO.File.Exists(p))
.ToList();
Console.WriteLine("Done!");
Console.WriteLine("{0} .cs files changed or added, checking content type...", changed.Count);
foreach(var changedPath in changed)
{
File(changedPath);
}
Console.WriteLine("Done!");
}
else
{
Dir(dir);
}
}
void Dir(string path)
{
foreach(var file in Directory.EnumerateFiles(path, "*.cs"))
{
File(file);
}
foreach(var dir in Directory.EnumerateDirectories(path))
{
Dir(dir);
}
}
void File(string path)
{
var prop = GetSvnProp("svn:mime-type", path);
if (prop == "application/octet-stream")
{
Console.WriteLine(" - Fixing {0}", path);
RemoveSvnProp("svn:mime-type", path);
}
}
string GetSvnProp(string prop, string path)
{
using (var process = SvnRun("propget", prop, path))
{
return process.StandardOutput.ReadToEnd().Trim();
}
}
IEnumerable<Tuple<char, string>> GetSvnStatus(string dir)
{
if (dir.EndsWith(Path.DirectorySeparatorChar.ToString())
|| dir.EndsWith(Path.AltDirectorySeparatorChar.ToString()))
{
dir = dir.Substring(0, dir.Length-1);
}
var regex = new Regex(@"^(?<change>.)(\+|\s)+(?<file>.*)$");
using (var process = SvnRun("status", dir))
{
string line;
while((line = process.StandardOutput.ReadLine()) != null)
{
//Console.WriteLine("RAW: {0}", line);
var m = regex.Match(line);
if (!m.Success)
{
line.Dump("Regex failed to parse this svn status line");
}
else
{
yield return Tuple.Create(m.Groups["change"].Value[0], m.Groups["file"].Value);
}
}
}
}
void RemoveSvnProp(string prop, string path)
{
SvnRun("propdel", prop, path).Dispose();
}
Process SvnRun(params string[] args)
{
return SvnRun((IEnumerable<string>)args);
}
Process SvnRun(IEnumerable<string> args)
{
args = args.Select(a => '"' + a + '"');
var argString = string.Join(" ", args);
var psi = new System.Diagnostics.ProcessStartInfo(@"svn", argString)
{
RedirectStandardOutput = true,
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false,
};
return Process.Start(psi);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment