2

我有一个简单的批处理文件:

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# 执行我的批处理文件几个小时都没有成功,但是,如果我在命令行中执行它,我的批处理文件可以正常工作。

我怎样才能让我的 C# 函数工作,我应该在我的批处理文件或 C# 函数中更改什么?谢谢

编辑:

我已将批处理文件的内容更改为以下内容:

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

但我收到以下错误:

Access is denied.

编辑2:

我已经添加了对该文件夹的访问权限,现在我收到了一个新错误:

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

编辑3:

我已授予 EDIT2 中提到的文件夹的访问权限,但现在我收到以下错误:

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

3 回答 3

1

您需要设置processInfo.WorkingDirectory到 git repo 的位置。

于 2012-11-15T22:19:12.610 回答
1

如果您有兴趣集成整个库,也许它可以帮助这个

于 2012-11-23T09:59:33.483 回答
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.)
于 2012-11-16T12:49:49.247 回答