Code Stranger
Code Stranger

Reputation: 103

Write git logs to text file when called from a batch file

I have a handful of repositories (maybe 15 or 20), most with several hundred commits. I would like to be able to call a batch file that has git write a log of each to a text file so I can parse them (once I have the separate files, I believe I can do easily, and plan to use C# most likely).

I know I can use git-bash in separate commands to do something like:

cd "<path to my repo>"
git --no-pager log > log.txt

This gets me the files I want, but how can this be accomplished in a batch file?
Can I use git-bash in this way? Do I have to use git.exe instead?

If it's not a good idea to do this via a batch file, is it possible to do this in C# with Process.Start?

Some of the questions I've focused on so far:
How do I export a git log to a text file? (how I leaned to use git-bash to create a text log)
Run git commands from a C# function
Running Bash Commands from C#

But I'm still not sure how to call what I want on each of the individual repositories. Especially in a batch file, where to my limited knowledge, each line is a standalone command (eliminating git-bash.exe as a possibility?).

Update

I've tried using the below code:

public void RunGitForLogs()
{
    Process process = new Process();

    ProcessStartInfo processStartInfo = new ProcessStartInfo();
    processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    processStartInfo.FileName = @"C:\Program Files\Git\bin\git.exe";
    processStartInfo.WorkingDirectory = @"E:\git\myRepo";
    processStartInfo.Arguments = @"log --no-pager > C:\users\me\desktop\log.txt";
    processStartInfo.RedirectStandardOutput = true;
    processStartInfo.RedirectStandardError = true;
    processStartInfo.UseShellExecute = false;

    process.StartInfo = processStartInfo;
    process.Start();

    var outStd = new List<string>();
    var outErr = new List<string>();

    //https://stackoverflow.com/a/16276776/2957232
    while (process.StandardOutput.Peek() > -1)
        outStd .Add(process.StandardOutput.ReadLine());

    while (process.StandardError.Peek() > -1)
        outErr .Add(process.StandardError.ReadLine());

    process.WaitForExit();
}  

But in outErr I get the following lines:

fatal: ambiguous argument '>': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git [...] -- [...]'

Using "status" as an argument works as expected for this repo (Many lines in outStd, nothing in outErr).

Upvotes: 0

Views: 1991

Answers (1)

Alex Zhmerik
Alex Zhmerik

Reputation: 365

Probably not the most beautiful way of doing this, but this line called from a windows .cmd file works for me:

cmd.exe /c start /d"c:\my\git\repo\dir" /b git.exe --no-pager log > log.txt

Upvotes: 1

Related Questions