2012-11-15 89 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