Odrai Odrai - 6 months ago 133
Git Question

Git diff - path is outside repository

I would like to execute a git diff command. The following command results in an 'fatal - path is outside repository' error. It is implemented in a C# application, using the Process class.


git diff HEAD -- "Folder\TestFile.cs" >
"C:\Users\Name\AppData\Local\Temp\tmpEA7C.diff"

fatal: C:\Users\Name\AppData\Local\Temp\tmpEA7C.diff:
C:\Users\Name\AppData\Local\Temp\tmpEA7C.diff is outside repository.


The command 'works', because I could successfuly execute it in a command prompt (cmd).

Could you explain what is wrong with the command and how to solve it in the C# application?

Answer Source

It looks like you're trying to redirect standard output to a file. Redirection is handled by the shell, and the Process class does not support it. Instead, it's passing the > C:\Users\Name\AppData\Local\Temp\tmpEA7C.diff to the git process. That's why git is complaining that the path C:\Users\Name\AppData\Local\Temp\tmpEA7C.diff is not in a repository.

If you're invoking a process and want to read its standard output, it's your responsibility to do that.

You can capture the output with the Process class. For example:

var proc = new Process {
    StartInfo = new ProcessStartInfo {
        FileName = "git.exe",
        Arguments = "diff HEAD -- "Folder\TestFile.cs"",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
};

proc.Start();

while (!proc.StandardOutput.EndOfStream) {
    var line = proc.StandardOutput.ReadLine();
}

Of course, you could also use LibGit2Sharp and not have to worry about process creation and screen scraping the output.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download