Using Git with VB6

前端 未结 7 578
轻奢々
轻奢々 2020-12-05 08:00

Our company has a large codebase in VB6, and we currently use VSS which, for all that we hate about it, at least integrates into the VB6 IDE.

My own team, which is u

相关标签:
7条回答
  • 2020-12-05 08:46

    This really is an addition to the other excellent comments by KeithTheBiped, et al...

    But, it is possible to somewhat coerce the Immediate Window in VB6 to act like a modern terminal window, with some caveats. If we create a helper function to run the command, capture the output somehow, and then use Debug.Print to relay that back to the Immediate window SIMILAR to the terminal window, but without any interactive elements, of course.

    This works for most commands, but fails to capture some of the output in the git push phase. An acceptable compromise, for the convenience of no shell (imo).

    We can do just that using the the command prompt (cmd.exe /c) using the 1> and 2> pipes aimed at temporary files. I don't supply a couple underlying functions, but provide sources if you don't have them already.

    Consider the following function:

    Public Function RunCmdToOutput(ByVal cmd As String, Optional ByRef ErrStr As String = "") As String
    Const p_enSW_HIDE = 0
    
    On Error GoTo RunError
    
      Dim A As String, B As String
      A = TempFile
      B = TempFile
    
      ShellAndWait "cmd /c " & cmd & " 1> " & A & " 2> " & B, p_enSW_HIDE
    
      RunCmdToOutput = ReadEntireFileAndDelete(A)
      ErrStr = ReadEntireFileAndDelete(B)
    
      Exit Function
    
    RunError:
      RunCmdToOutput = ""
      ErrStr = "ShellOut.RunCmdToOutput: Command Execution Error - [" & Err.Number & "] " & Err.Description
    End Function
    

    You will need:

    • TempFile - Return a unique and non-existant file-name that your program has read/write access to. It should probably use the GetShortPathName API to shorten long path names.
    • ShellAndWait - The standard "start a process and wait for it to exit".
    • ReadEntireFileAndDelete - Just what it says... Grab the contents, delete the file, return the string.

    Once you have accomplished this and can successfully run any simple executing and output into the Immediate window like this:

    ?runcmdtooutput("ver")
    Microsoft Windows [Version 10.0.16299.309]
    

    From here, you can run Git, and display MOST things into the Immediate window, and could use it as simple as that, but we can do better.

    Assuming you have already installed a command-line Git and the path is updated so that it is available, you can create a few new functions (hopefully in a module):

    Private Function GitCmd(ByVal C As String, Optional ByVal NoOutput As Boolean = False) As String
      Dim ErrSt As String
      GitCmd = RunCmdToOutput(C, ErrSt)
      If Not NoOutput Then Debug.Print GitCmd ' Because it also returns the string
      If ErrSt <> "" Then Debug.Print "ERR: " & ErrSt
    End Function
    
    Public Function Git(ByVal C As String) As Boolean
      GitCmd "git " & C
      Git = True
    End Function
    

    From here, you can ALMOST run Git commands from the immediate window. Remember, Git() is a function, so you have to pass in the argument as a string... Just one necessary character. Of course, VB will auto-complete a string, so you don't NEED the trailing quite, but I'll include it. Use the syntax:

    Git "status"
    Git "add ."
    Git "commit -m ""Some Git Commit"""
    Git "pull -r"
    Git "push"
    

    It doesn't allow interactive git commands (git add -p), but, you could just brute-force it (git add . -f). But, the commands run and directly display their output into the Immediate window without much other effort.

    git "status"
    On branch master
    Your branch is up to date with 'origin/master'.
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    ...
    

    You get the idea.

    From there, you can also automate things. Create helper functions to batch commonly used Git commands, etc, or just eliminate some of the clunky syntax. With RunCmdToOutput, you can also re-work some of it to use the MsgBox, if preferred, but I thought the Immediate Window was intuitive enough.

    It might also be important at some point to restrict any functions to only run in the IDE, but that's optional.

    Public Function IsIDE() As Boolean
      On Error GoTo IDEInUse
      Debug.Print 1 \ 0 'division by zero error - Debug is ignored by compile
      IsIDE = False
      Exit Function
    IDEInUse:
      IsIDE = True
    End Function
    
    Public Function GitStatus()
      If Not IsIDE Then Exit Function
      GitCmd "git status"
      GitStatus = True
    End Function
    
    0 讨论(0)
提交回复
热议问题