-
-
Save anonymous/2d52c96233abf771212b to your computer and use it in GitHub Desktop.
C# console program that mimics part of the functionality from SubWCRev in a Plastic workspace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.IO; | |
namespace PlasticWcRev | |
{ | |
// NOTE: This program mimics parts of the SubWcRev tool from TortoiseSVN. | |
// Written by goran.wallgren @ surgical-science.com | |
// TODO: exitcode/errorlevel, $WCNOW$ | |
class PlasticWcRev | |
{ | |
////////////////////////////////////////////////////////////////////////// | |
static void Main(string[] args) | |
{ | |
string procName = Process.GetCurrentProcess().ProcessName; | |
if (args.Length % 2 != 1) | |
{ | |
Console.WriteLine("Usage: " + procName + " WorkspacePath [SrcFile DstFile] [SrcFile DstFile] ..."); | |
Console.WriteLine(); | |
Console.WriteLine("The following keywords will be replaced in copying from SrcFile to DstFile:"); | |
Console.WriteLine("$WCURL$ $WCREV$ $WCDATE$ $WCMODS$ $WCUNVER$"); | |
return; | |
} | |
try | |
{ | |
string workspacePath = args[0]; | |
//-------------------------------------------------------------------- | |
// Get info... | |
string statusLine; | |
bool modified, unversioned; | |
GetStatus(workspacePath, out statusLine, out modified, out unversioned); | |
string changeset, url; | |
UnpackStatusLine(statusLine, out changeset, out url); | |
string dateStr = GetLogDate(changeset); | |
//-------------------------------------------------------------------- | |
// Print info... | |
DirectoryInfo dirInfo = new DirectoryInfo(workspacePath); | |
Console.WriteLine(procName + " : " + dirInfo.FullName); | |
Console.WriteLine(url); | |
Console.Write("At changeset " + changeset); | |
if (modified) Console.Write(" (* modified)"); | |
if (unversioned) Console.Write(" (+ unversioned)"); | |
Console.WriteLine(); | |
Console.WriteLine("Changeset date: " + dateStr); | |
//-------------------------------------------------------------------- | |
// Replace keywords during copying of files, if any... | |
for (int n = 1; n+1 < args.Length; n += 2) | |
PerformFileReplacement(args[n], args[n+1], url, changeset, modified, unversioned, dateStr); | |
//-------------------------------------------------------------------- | |
if (modified) | |
Environment.Exit(1); | |
if (unversioned) | |
Environment.Exit(2); | |
} | |
catch (System.Exception ex) | |
{ | |
Console.WriteLine(ex); | |
Environment.Exit(10); | |
} | |
} | |
////////////////////////////////////////////////////////////////////////// | |
private static void PerformFileReplacement(string srcFile, string dstFile, | |
string url, string changeset, bool modified, bool unversioned, string dateStr) | |
{ | |
string txt = File.ReadAllText(srcFile); | |
txt = txt.Replace("$WCURL$", url); | |
txt = txt.Replace("$WCREV$", changeset); | |
txt = txt.Replace("$WCDATE$", dateStr); | |
txt = txt.Replace("$WCMODS$", (modified ? "true" : "false")); | |
txt = txt.Replace("$WCUNVER$", (unversioned ? "true" : "false")); | |
File.WriteAllText(dstFile, txt); | |
} | |
////////////////////////////////////////////////////////////////////////// | |
private static void UnpackStatusLine(string statusLine, out string changeset, out string url) | |
{ | |
changeset = "<none>"; | |
url = ""; | |
var tokens = statusLine.Split('@'); | |
string repo = ""; | |
string server = ""; | |
if (tokens.Length > 0) changeset = tokens[0]; | |
if (tokens.Length > 1) repo = tokens[1]; | |
if (tokens.Length > 2) server = tokens[2]; | |
if (changeset.StartsWith("cs:")) | |
changeset = changeset.Remove(0, "cs:".Length); | |
if (repo.StartsWith("rep:")) | |
repo = repo.Remove(0, "rep:".Length); | |
if (server.StartsWith("repserver:")) | |
server = server.Remove(0, "repserver:".Length); | |
url = repo + "@" + server; | |
} | |
////////////////////////////////////////////////////////////////////////// | |
private static void GetStatus(string workspacePath, | |
out string statusLine, out bool modified, out bool unversioned) | |
{ | |
statusLine = "cs:@rep:@repserver:"; | |
modified = false; | |
unversioned = false; | |
string options = " --all --noheaders"; | |
//options += " --nochanges"; | |
//options += " --nostatus"; | |
Process proc = new Process | |
{ | |
StartInfo = new ProcessStartInfo | |
{ | |
FileName = "cm", | |
Arguments = "status " + workspacePath + options, | |
UseShellExecute = false, | |
RedirectStandardOutput = true, | |
CreateNoWindow = true | |
} | |
}; | |
proc.Start(); | |
while (!proc.StandardOutput.EndOfStream) | |
{ | |
string line = proc.StandardOutput.ReadLine(); | |
if (!String.IsNullOrWhiteSpace(line)) | |
{ | |
if (line.StartsWith("cs:")) | |
statusLine = line; | |
else if (line.StartsWith(" PR ")) | |
unversioned = true; | |
else | |
modified = true; | |
} | |
} | |
} | |
////////////////////////////////////////////////////////////////////////// | |
private static string GetLogDate(string changeset) | |
{ | |
string dateStr = ""; | |
string options = " cs:" + changeset + " --csFormat={date}"; | |
Process proc = new Process | |
{ | |
StartInfo = new ProcessStartInfo | |
{ | |
FileName = "cm", | |
Arguments = "log " + options, | |
UseShellExecute = false, | |
RedirectStandardOutput = true, | |
CreateNoWindow = true | |
} | |
}; | |
proc.Start(); | |
while (!proc.StandardOutput.EndOfStream) | |
{ | |
string line = proc.StandardOutput.ReadLine(); | |
if (!String.IsNullOrWhiteSpace(line)) | |
dateStr = line; | |
} | |
return dateStr; | |
} | |
////////////////////////////////////////////////////////////////////////// | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment