使用SVN版本号检索文件列表



TortoiseSVN提供了一个COM接口来检索关于文件的信息。

使用VBA,我可以通过这样做获得关于SVN存储库中文件的信息:

Public Function getSvnURL(ByVal fullFilename As String)
    Dim oSvn As Object
    Set oSvn = CreateObject("SubWCRev.Object")
    oSvn.GetWCInfo fullFilename, 1, 1
    getSvnURL = oSvn.url
End Function

如果我有一个SVN版本号,但是,是否有一个API,我可以用它来获取文件,是提交的一部分?比如:

Public Function getFilesInRevision(revisionNumber As Integer) as Collection
    Dim oSvn As Object
    Set oSvn = CreateObject("SubWCRev.Object")
    oSvn.GetWCInfo revisionNumber
    getFilesInRevision= oSvn.fileList
End Function

我最终使用了以下方法:

Public Function getFilesForRevision(revisionNumber As Long, folder As String) As Collection
    Dim command As String
    command = "svn log -v -q -r " & revisionNumber & " " & folder
    Dim rawText As String
    rawText = ShellRun(command)
    Dim lines() As String
    lines = Split(rawText, vbLf)
    Set getFilesForRevision = New Collection
    Dim filenameRegex As Object
    Set filenameRegex = CreateObject("VBScript.RegExp")
    filenameRegex.Pattern = "s{3}.s(.*)"
    Dim line As Variant
    For Each line In lines
        If filenameRegex.test(line) Then
            getFilesForRevision.Add (filenameRegex.Execute(line).Item(0).submatches(0))
        End If
    Next line
End Function

它依赖于这个方法来运行命令并存储控制台输出:

'http://stackoverflow.com/questions/2784367/capture-output-value-from-a-shell-command-in-vba
Public Function ShellRun(sCmd As String) As String
    'Run a shell command, returning the output as a string'
    Dim oShell As Object
    Set oShell = CreateObject("WScript.Shell")
    'run command'
    Dim oExec As Object
    Dim oOutput As Object
    Set oExec = oShell.Exec(sCmd)
    Set oOutput = oExec.StdOut
    'handle the results as they are written to and read from the StdOut object'
    Dim s As String
    Dim sLine As String
    While Not oOutput.AtEndOfStream
        sLine = oOutput.ReadLine
        If sLine <> "" Then s = s & sLine & vbCrLf
    Wend
    ShellRun = s
End Function

可以这样调用:

Sub getFilesForRevisionTest()
    Dim files As Collection
    Set files = getFilesForRevision(111041, "C:SVN")
    Dim fullFilename As Variant
    For Each fullFilename In files
        Debug.Print fullFilename
    Next fullFilename
End Sub

最新更新