简体   繁体   English

Git 钩子预提交错误:“找不到命令”

[英]Git hook pre-commit error: "command not found"

I have a simple Git hook that calls some other (VBScript) scripts.我有一个调用其他(VBScript)脚本的简单 Git 钩子。 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.这是在钩子中调用的 VBScript。

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.我读过它可能与PATH环境变量有关。

You probably want to prefix the calls to your .vbs scripts with:您可能希望在对.vbs脚本的调用添加前缀:

cscript yourscript.vbs

You're running a Bash/sh script from your Git hook.您正在从 Git 挂钩运行 Bash/sh 脚本。

Your Bash/sh implementation won't know what to do with Start-Sleep because that's a PowerShell command.您的 Bash/sh 实现不知道如何处理Start-Sleep因为这是一个 PowerShell 命令。 You'll need to find some Bash equivalent or shell out to PowerShell to execute that statement.您需要找到一些 Bash 等效项或 shell 到 PowerShell 来执行该语句。

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.然后,脚本尝试将RevisionDate.vbsMovePDF.vbs的内容作为 Bash/sh 语句执行,而不是启动通常在cmd.exe命令外壳下关联的 VBScript 解释器。 So you need to tell your shell script how to do this, ie, cscript yourscript.vbs .因此,您需要告诉您的 shell 脚本如何执行此操作,即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).大多数 Windows Bash/sh shell 实现不知道C:\\blah\\blah是什么意思( \\用作转义字符)。 Instead you need to use Unix-style paths.相反,您需要使用 Unix 风格的路径。 For example,例如,

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

would translate to:会翻译成:

/c/Users/Ian/desktop/Test2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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