Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with invoking `git shortlog` from Go 'exec()'?

Tags:

git

go

exec

I'm trying to invoke git shortlog from Go to grab the output, but I'm running into a wall.

Here's a working example of how I'm able to do this with git log:

package main

import (
    "fmt"
    "os"
    "os/exec"
)

func main() {
    runBasicExample()
}

func runBasicExample() {
    cmdOut, err := exec.Command("git", "log").Output()
    if err != nil {
        fmt.Fprintln(os.Stderr, "There was an error running the git command: ", err)
        os.Exit(1)
    }
    output := string(cmdOut)
    fmt.Printf("Output: \n%s\n", output)
}

Which gives expected output:

$>  go run show-commits.go 
Output: 
commit 4abb96396c69fa4e604c9739abe338e03705f9d4
Author: TheAndruu
Date:   Tue Aug 21 21:55:07 2018 -0400

    Updating readme

But I really want to do this with git shortlog.

For some reason... I just can't get it to work with shortlog. Here's the program again, with the only change being the git command line:

package main

import (
    "fmt"
    "os"
    "os/exec"
)

func main() {
    runBasicExample()
}

func runBasicExample() {
    cmdOut, err := exec.Command("git", "shortlog").Output()
    if err != nil {
        fmt.Fprintln(os.Stderr, "There was an error running the git command: ", err)
        os.Exit(1)
    }
    output := string(cmdOut)
    fmt.Printf("Output: \n%s\n", output)
}

With empty output:

$>  go run show-commits.go 
Output: 

I can run git shortlog directly from the command line and it seems to work fine. Checking the docs, I'm led to believe the 'shortlog' command is part of git itself.

Can anyone help point out what I could do different?

Thanks

like image 715
Cuga Avatar asked Sep 05 '25 13:09

Cuga


1 Answers

Turns out, I was able to find the answer by re-reading the git docs

The answer was in this line:

If no revisions are passed on the command line and either standard input is not a terminal or there is no current branch, git shortlog will output a summary of the log read from standard input, without reference to the current repository.

Despite the fact that I can run git shortlog from the terminal and see expected output, when running via the exec() command, I need to specify the branch.

So in the above examples, I added 'master' to the command arguments like so:

cmdOut, err := exec.Command("git", "shortlog", "master").Output()

And everything works as expected.

like image 104
Cuga Avatar answered Sep 08 '25 02:09

Cuga