【问题标题】:How can i get access to the SVN pre-commit message using SharpSVN?如何使用 SharpSVN 访问 SVN 预提交消息?
【发布时间】:2010-11-25 17:25:01
【问题描述】:

我发现我只能设置 %repos% 和 %txn%

如何使用这些来获取提交消息(在我的情况下,我可以解析出票号,以便在提交之前查看它是否存在于错误数据库中)

【问题讨论】:

    标签: c# svn sharpsvn pre-commit-hook


    【解决方案1】:

    我不知道 SharpSVN,但是如果你按照描述创建一个钩子脚本,你会得到参数 %repos% 和 %txn%

    使用这些数据,您可以查看给定存储库的事务 (%txn%)。通常你使用

    svnlook -t %txn%  %repo%
    

    然后你会得到日志消息。

    所以你应该在sharpSVN界面中寻找与svnlook等价的东西。

    【讨论】:

    • 是的,我已经做到了。不久前我什至问了一个关于该语法的问题:stackoverflow.com/questions/1258191/sharpsvn-svnlookclient 不幸的是,使用 SharpSVN 的 SVNLook 不起作用。它无法获取日志消息(怎么可能?它甚至存储在 SVN 中 txn = 486-1 的位置吗?但是,我没有深入研究问题,并最终像你说的那样做,将数据发送到文件: %SVNLOOK% log -t %TXN% %REPOS% > %LOG_FILE% %~dp0MyExeThatDoesOtherStuff.exe %LOG_FILE%
    • 最近的 SharpSvn 版本有一个 SvnLookClient,它复制了 .Net 中 svnlook 命令的功能
    【解决方案2】:

    我自己刚刚完成了构建钩子应用程序的过程,并且查看提交消息不需要 SharpSVN。假设你已经自己构建了一个控制台应用程序,试试这个直接调用 svnlook.exe 的代码:

    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;
    

    确保将 svnlook.exe 的位置添加到您机器的路径环境变量中,以便可以从任何位置执行上述操作。

    【讨论】:

      【解决方案3】:

      前段时间,我为 svnlook.exe 编写了一个 C# 包装器。我使用这个将提交消息发送到错误跟踪器(如果提供了票证 ID)。在下面找到它,也许它对你有用。

      /// <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;
          }
      }
      

      【讨论】:

        【解决方案4】:

        使用最近的SharpSvn 版本,您可以使用

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

        解析预提交钩子的参数,然后使用

        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)
            {
        
            }
        }
        

        获取日志消息、更改的文件等。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-02-13
          • 1970-01-01
          • 2014-09-20
          • 1970-01-01
          • 2011-06-10
          • 2011-04-01
          • 2023-03-13
          • 1970-01-01
          相关资源
          最近更新 更多