2012-11-15 88 views
2

我有一個簡單的批處理文件:的Git拉問題與C#函數執行批處理文件

cd <path to my git repository> 
git pull 

我嘗試用下面的C#函數來執行它:

private NuGetBO.ShellCommandReturn ExecuteCommand(string path) 
{ 
    ProcessStartInfo processInfo; 
    Process process; 

    processInfo = new ProcessStartInfo(path); 
    processInfo.CreateNoWindow = true; 
    processInfo.UseShellExecute = false; 

    // *** Redirect the output *** 
    processInfo.RedirectStandardError = true; 
    processInfo.RedirectStandardOutput = true; 


    process = Process.Start(processInfo); 
    process.WaitForExit(5000); 

    // *** Read the streams *** 
    string output = process.StandardOutput.ReadToEnd(); 
    string error = process.StandardError.ReadToEnd(); 

    int exitCode = process.ExitCode; 
    process.Close(); 
    return new NuGetBO.ShellCommandReturn { Error = error, ExitCode = exitCode, Output = output }; 
} 

但我得到以下錯誤:

"Access is denied.\r\nfatal: Not a git repository (or any of the parent directories): .git\n" 

換句話說,我試圖執行從C#我的批處理文件,沒有成功了幾個小時,但我的批處理文件WOR ks沒有任何問題,如果我在命令行中執行它。

如何讓我的C#函數工作,我應該在批處理文件或C#函數中更改哪些內容?由於

編輯:

我已經改變了批處理文件的內容如下:

cd <path to my git repository> && git pull 

但我收到以下錯誤:

Access is denied. 

EDIT2:

我已添加訪問該文件夾,現在我得到一個新的錯誤:

fatal: Not a git repository: "../.git/modules/myProject\n" 

EDIT3:

我已經給了訪問權限,在EDIT2提到的文件夾,但現在我收到以下錯誤:

"error: unable to create directory for C:/myFolder/.git/modules/myProject/ORIG_HEAD\nfatal: Cannot lock the ref 'ORIG_HEAD'.\n" 
+0

這是一個愚蠢的(也許不是?)問題要問你:你確定的路徑是相同的100%? –

+0

是的,我相信,但是從根本上來說,這個bug可能就在那裏,這絕不是愚蠢的做法。 –

回答

1

它現在有效。爲了不讓我有同樣的神經破壞問題,任何未來必須執行相同操作的人都應該檢查以下內容: 有一位用戶運行C#代碼,我們可以稱他/她爲User

1. User must have privileges to the folder where the git pull should be running. 
2. User must have privileges to the folder where the git repository is located (its path is described in the .git file located in the folder described at 1.) 
1

您需要設置processInfo.WorkingDirectory到GIT的位置回購。

+0

「目錄名稱無效」 –

+0

批處理文件位於與git存儲庫不同的位置。 –

+0

因此是什麼?爲什麼它是無效的? – SLaks

1

如果你有興趣到整個圖書館集成也許它可以幫助this one