views:

435

answers:

4

i see that all I can set to is %repos% and %txn%

how can I use those to get to the commit message (in my case, so i can parse out the ticket number so i can see if it exists in the bug database before committing to it)

+5  A: 

I do not know SharpSVN, however if you create a hook script as you describe, you get as arguments %repos% and %txn%

With these data you can look into the transaction (%txn%) of the given repository. Usually you do this by using

svnlook -t %txn%  %repo%

Then you will get the log-message.

So you should look for an equivalent to svnlook in sharpSVN interface.

Peter Parker
yeah, i did already. I even asked a question regarding that syntax a while back: http://stackoverflow.com/questions/1258191/sharpsvn-svnlookclientunfortunately using SharpSVN's SVNLook didn't work. It failed to get the log message (how could it? is it even stored in SVN at the point where txn = 486-1?However, I didn't dig that deep into the problem, and wound up doing it exactly like you said, sending the data to a file: %SVNLOOK% log -t %TXN% %REPOS% > %LOG_FILE% %~dp0MyExeThatDoesOtherStuff.exe %LOG_FILE%
KevinDeus
Recent SharpSvn versions have a SvnLookClient that replicates the functionality of the svnlook command in .Net
Bert Huijben
+1  A: 

I've just gone through the process of building a hooks app myself and SharpSVN isn't required for looking at commit messages. Assuming you've built yourself a console app already, try this code which calls svnlook.exe directly:

string repos = args[0];
string txn = args[1];

var processStartInfo = new ProcessStartInfo
{
  FileName = "svnlook.exe",
  UseShellExecute = false,
  CreateNoWindow = true,
  RedirectStandardOutput = true,
  RedirectStandardError = true,
  Arguments = String.Format("log -t \"{0}\" \"{1}\"", txn, repos)
};

Process process = Process.Start(processStartInfo);
string message = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return message;

Make sure the location of svnlook.exe is added to the path environment variable of your machine so the above can be executed from any location.

Troy Hunt
+2  A: 

Some time ago I've written a C# wrapper for the svnlook.exe. I used this one to send commit messages to a bug tracker (if a ticket id was provided). Find it below, maybe it is useful for you.

/// <summary>
/// Encapsulates the SVNLook command in all of it's flavours
/// </summary>
public static class SvnLookCommand
{
    /// <summary>
    /// The string &quot;&quot; used as parameter for the svnlook.exe
    /// </summary>
    private static readonly string AUTHOR = "author";

    /// <summary>
    /// The string &quot;cat&quot; used as parameter for the svnlook.exe
    /// </summary>
    private static readonly string CAT = "cat";

    /// <summary>
    /// The string &quot;changed&quot; used as parameter for the svnlook.exe
    /// </summary>
    private static readonly string CHANGED = "changed";

    /// <summary>
    /// The string &quot;date&quot; used as parameter for the svnlook.exe
    /// </summary>
    private static readonly string DATE = "date";

    /// <summary>
    /// The string &quot;diff&quot; used as parameter for the svnlook.exe
    /// </summary>
    private static readonly string DIFF = "diff";

    /// <summary>
    /// The string &quot;dirs-changed&quot; used as parameter for the svnlook.exe
    /// </summary>
    private static readonly string DIRSCHANGED = "dirs-changed";

    /// <summary>
    /// The string &quot;history&quot; used as parameter for the svnlook.exe
    /// </summary>
    private static readonly string HISTORY = "history";

    /// <summary>
    /// The string &quot;info&quot; used as parameter for the svnlook.exe
    /// </summary>
    private static readonly string INFO = "info";

    /// <summary>
    /// The string &quot;lock&quot; used as parameter for the svnlook.exe
    /// </summary>
    private static readonly string LOCK = "lock";

    /// <summary>
    /// The string &quot;log&quot; used as parameter for the svnlook.exe
    /// </summary>
    private static readonly string LOG = "log";

    /// <summary>
    /// The string &quot;tree&quot; used as parameter for the svnlook.exe
    /// </summary>
    private static readonly string TREE = "tree";

    /// <summary>
    /// The string &quot;uuid&quot; used as parameter for the svnlook.exe
    /// </summary>
    private static readonly string UUID = "uuid";

    /// <summary>
    /// The string &quot;youngest&quot; used as parameter for the svnlook.exe
    /// </summary>
    private static readonly string YOUNGEST = "youngest";

    /// <summary>
    /// The full path of the svnlook.exe binary
    /// </summary>
    private static string commandPath = String.Empty;

    /// <summary>
    /// Initializes static members of the <see cref="SvnLookCommand"/> class.
    /// </summary>
    static SvnLookCommand()
    {
     commandPath = Settings.Default.SvnDirectoryPath;

     if (!Path.IsPathRooted(commandPath))
     {
      Assembly entryAssembly = Assembly.GetEntryAssembly();
      if (entryAssembly != null)
      {
       commandPath = new FileInfo(entryAssembly.Location).Directory.ToString() + Path.DirectorySeparatorChar + commandPath;
      }
     }

     if (!commandPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
     {
      commandPath = commandPath + Path.DirectorySeparatorChar;
     }

     commandPath += "svnlook.exe";
    }

    /// <summary>
    /// Gets the process info to start a svnlook.exe command with parameter &quot;author&quot;
    /// </summary>
    /// <param name="repository">The repository.</param>
    /// <param name="revision">The revision.</param>
    /// <returns>Gets the author of the revision in scope</returns>
    public static ProcessStartInfo GetAuthor(string repository, string revision)
    {
     ProcessStartInfo svnLookProcessStartInfo = new ProcessStartInfo(commandPath);
     svnLookProcessStartInfo.Arguments = String.Format("{0} {1} -r {2}", AUTHOR, repository, revision);
     return svnLookProcessStartInfo;
    }

    /// <summary>
    /// Gets the process info to start a svnlook.exe command with parameter &quot;log&quot;
    /// </summary>
    /// <param name="repository">The repository.</param>
    /// <param name="revision">The revision.</param>
    /// <returns>The svn log of the revision in scope</returns>
    public static ProcessStartInfo GetLog(string repository, string revision)
    {
     ProcessStartInfo svnLookProcessStartInfo = new ProcessStartInfo(commandPath);
     svnLookProcessStartInfo.Arguments = String.Format("{0} {1} -r {2}", LOG, repository, revision);
     return svnLookProcessStartInfo;
    }

    /// <summary>
    /// Gets the process info to start a svnlook.exe command with parameter &quot;changed&quot;
    /// </summary>
    /// <param name="repository">The repository.</param>
    /// <param name="revision">The revision.</param>
    /// <returns>The change log of the revision in scope</returns>
    public static ProcessStartInfo GetChangeLog(string repository, string revision)
    {
     ProcessStartInfo svnLookProcessStartInfo = new ProcessStartInfo(commandPath);
     svnLookProcessStartInfo.Arguments = String.Format("{0} {1} -r {2}", CHANGED, repository, revision);
     return svnLookProcessStartInfo;
    }

    /// <summary>
    /// Gets the process info to start a svnlook.exe command with parameter &quot;info&quot;
    /// </summary>
    /// <param name="repository">The repository.</param>
    /// <param name="revision">The revision.</param>
    /// <returns>The info of the revision in scope</returns>
    public static ProcessStartInfo GetInfo(string repository, string revision)
    {
     ProcessStartInfo svnLookProcessStartInfo = new ProcessStartInfo(commandPath);
     svnLookProcessStartInfo.Arguments = String.Format("{0} {1} -r {2}", INFO, repository, revision);
     return svnLookProcessStartInfo;
    }
}
Marc Wittke
+1 for sharing the code and work
balexandre
+1  A: 

Using a recent SharpSvn release you can use

SvnHookArguments ha; 
if (!SvnHookArguments.ParseHookArguments(args, SvnHookType.PreCommit, false, out ha))
{
    Console.Error.WriteLine("Invalid arguments");
    Environment.Exit(1);  
}

to parse the arguments of a pre-commit hook and then use

using (SvnLookClient cl = new SvnLookClient())
{
    SvnChangeInfoEventArgs ci;

    cl.GetChangeInfo(ha.LookOrigin, out ci);


    // ci contains information on the commit e.g.
    Console.WriteLine(ci.LogMessage); // Has log message

    foreach (SvnChangeItem i in ci.ChangedPaths)
    {

    }
}

to get to the log message, changed files, etc.

Bert Huijben