2

I have a simple batch file:

cd <path to my git repository>
git pull

I try to execute it with the following C# function:

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 };
}

But I get the following error:

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

In other words I have tried to execute my batch file from C# without success for hours, however, my batch file works without any problem if I execute it in command line.

How can I get my C# function to work, what should I change in my batch file or C# function? Thanks

EDIT:

I have changed the batch file's content to the following:

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

But I receive the following error:

Access is denied.

EDIT2:

I've added access to the folder and now I get a new error:

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

EDIT3:

I have given access rights to the folder mentioned in EDIT2, but now I receive the following error:

"error: unable to create directory for C:/myFolder/.git/modules/myProject/ORIG_HEAD\nfatal: Cannot lock the ref 'ORIG_HEAD'.\n"
2
  • Here's a stupid (maybe not?) question for you: You're 100% sure the paths are the same? Commented Nov 15, 2012 at 22:28
  • Yes, I am sure, but it's never stupid to question the basics as the bug might be there. Commented Nov 15, 2012 at 22:32

3 Answers 3

1

You need to set processInfo.WorkingDirectory to the location of the git repo.

Sign up to request clarification or add additional context in comments.

9 Comments

"The directory name is invalid"
The batch file is at a different location from the git repository.
And therefore what? Why is it invalid?
If I knew I would be a wise and happy person. The paths are correct though, the batch file is executed incorrectly from C# and it executes without a problem from command line.
Please show me the path. Are you using a @"..." verbatim string literal.
|
1

It works now. To not have the same nerve-breaking issue I had, any future person who has to do the same should check the following: There is a user who runs the C# code, let's call him/her 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.)

Comments

1

if you are interested to integrate a whole library maybe it can help this one

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.