简体   繁体   中英

Git hook pre-commit error: "command not found"

I have a simple Git hook that calls some other (VBScript) scripts. The script runs correctly when I call it from the command line. However, when the hook is executed, it gives me the following error(s):

./RevisionDate.vbs: line 1: syntax error near unexpected token `('
./RevisionDate.vbs: line 1: `Set copyFSO = CreateObject ("Scripting.FileSystemObject")'
.git/hooks/pre-commit: line 8: Start-Sleep: command not found
test
./MovePDF.vbs: line 1: unexpected EOF while looking for matching `''
./MovePDF.vbs: line 6: syntax error: unexpected end of file

Here is the VBScript that gets called in the hook.

Set copyFSO = CreateObject ("Scripting.FileSystemObject")
copyFSO.copyFile "C:\Users\Ian\Desktop\Test\*.pdf", "C:\Users\Ian\Desktop\Test2"
copyFSO.moveFile "C:\Users\Ian\Desktop\Test\*.pdf", "C:\Users\Ian\Desktop\Temp"

'------ Microsoft Excel -------

inputPrefix = cint(inputbox("Please enter two digit prefix for file you would like updated.", "File Update"))
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
sFolder = "C:\Users\Ian\Desktop\Test"
Set oFSO = CreateObject("Scripting.FileSystemObject")

For Each oFile In oFSO.GetFolder(sFolder).Files
    fileName = oFile
    Set objWorkbook = nothing
    Set objSelection = nothing
    Set objWorksheet = nothing

    If UCase(oFSO.GetExtensionName(oFile.Name)) = "XLSX" Then
        Prefix = left(oFile.Name, 2)
        filePrefix = cint(Prefix)
        Set objWorkbook = objExcel.Workbooks.Open(fileName)
        Set objSelection = objExcel.Selection
        Set objWorksheet = objWorkbook.Worksheets(1)
        objExcel.DisplayAlerts = False
        myYear = Year(Now())
        myMonth = Month(Now())
        myDay = Day(Now())
        myDate = myYear & "/" & myMonth & "/" & myDay
        myDateFile = myYear & "-" & myMonth & "-" & myDay

        If (filePrefix = inputPrefix) then
            objWorksheet.PageSetup.RightFooter = "Revision Date: " & myDate & " C"
            objWorkbook.Save
        End If

        fileName = Replace(oFile.Name, ".xlsx", "")
        saveAndCloseXlsx objWorkbook
    End if
Next

Function saveAndCloseXlsx(objWorkbook)
    objWorkbook.ExportAsFixedFormat xiTypePDF, "C:\Users\Ian\Desktop\Test\" & fileName
    objWorkbook.Close
end Function


'------ Microsoft Word -------

Set objWord = CreateObject("Word.Application")
objWord.Visible = False
sFolder = "C:\Users\Ian\Desktop\Test"
Set oFSO = CreateObject("Scripting.FileSystemObject")

For Each oFile In oFSO.GetFolder(sFolder).Files

    If UCase(oFSO.GetExtensionName(oFile.Name)) = "DOCX" Then
        fileName = oFile
        Set objDoc = objWord.Documents.Open(fileName)
        Set objSelection = objWord.Selection

        If objDoc.Bookmarks.Exists("RevisionDate") = True Then
            Set objRange = objDoc.Bookmarks("RevisionDate").Range
            myYear = Year(Now())
            myMonth = Month(Now())
            myDay = Day(Now())
            myDate = myYear & "/" & myMonth & "/" & myDay
            myDateFile = myYear & "-" & myMonth & "-" & myDay
            Prefix = left(oFile.Name, 2)
            filePrefix = cint(Prefix)

            If (inputPrefix = filePrefix) then
                objRange.text = "Revision Date: " & myDate & " C"
                objDoc.Bookmarks.Add "RevisionDate", objRange
            End If

            wdFormatPDF = 17
            SaveAndCloseDocx objDoc
        End If
    End if
Next

set oFSO = Nothing
objWord.Quit

Function SaveAndCloseDocx(objDoc)
    fileName = Replace(oFile.Name, ".docx", "")
    objDoc.SaveAs "C:\Users\Ian\Desktop\Test\" & fileName & ".pdf", wdFormatPDF
    objDoc.Close
End Function

And finally the hook itself:

#!/bin/sh
#
#
echo "Script Running"
cd 'C:\Users\Ian\desktop\QMS_Manual'
"./RevisionDate.vbs"
Start-Sleep -s 30
"./MovePDF.vbs"
cd 'C:\Users\Ian\desktop\Test2'
pdftk *.pdf cat output ECMWC.pdf
cd 'C:\Users\Ian\desktop\QMS_Manual'
DeleteAllButFinal.vbs

Why does this happen? I've read it may have something to do with the PATH environment variable.

You probably want to prefix the calls to your .vbs scripts with:

cscript yourscript.vbs

You're running a Bash/sh script from your Git hook.

Your Bash/sh implementation won't know what to do with Start-Sleep because that's a PowerShell command. You'll need to find some Bash equivalent or shell out to PowerShell to execute that statement.

The script then tries to execute the contents of RevisionDate.vbs and MovePDF.vbs as Bash/sh statements rather than launch the VBScript interpreter that would normally be associated under a cmd.exe command shell. So you need to tell your shell script how to do this, ie, cscript yourscript.vbs .

Paths:

Also watch those paths. Most Windows Bash/sh shell implementations won't know what C:\\blah\\blah means (the \\ is used as an escape character). Instead you need to use Unix-style paths. For example,

'C:\Users\Ian\desktop\Test2'

would translate to:

/c/Users/Ian/desktop/Test2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM