I am trying to execute the code mentioned below. The code creates a branch, and then a worktree and commit is done from worktree directory
The code fails with below error:
error= exit status 1
If I execute the commit (Cmd3) directly from command line, it works fine:
sh-3.2# /usr/bin/git -C /Users/gitissue/folder1/Outer commit -m Commiting-from-folder1-Outer
On branch Outer
nothing to commit, working tree clean
Steps:
- Created folder /Users/gitissue
- cd /Users/gitissue
- git init
- touch t.txt
- git add .
- git commit -m "commit"
- mkdir -p /Users/gitissue/folder1
- execute go code mentioned below
Env details:
- MAC OS
- git version 2.37.0
- go version go1.18.1 darwin/amd64
Code:
package main
import (
"fmt"
"io"
exec "os/exec"
)
func main() {
Cmd := exec.Command("git", "-C", "/Users/gitissue", "branch", "Outer")
fmt.Print("Cmd1= " + Cmd.String())
err := execBashCmd(Cmd)
if err != nil {
fmt.Print("error1= " + err.Error())
}
Cmd = exec.Command("git", "-C", "/Users/gitissue/folder1", "worktree", "add", "Outer", "Outer")
fmt.Print("Cmd2= " + Cmd.String())
err = execBashCmd(Cmd)
if err != nil {
fmt.Print("error2= " + err.Error())
}
Cmd = exec.Command("git", "-C", "/Users/gitissue/folder1/Outer", "commit", "-m", "Commiting-from-folder1-Outer")
fmt.Print("Cmd3= " + Cmd.String())
err = execBashCmd(Cmd)
if err != nil {
fmt.Print("error3= " + err.Error())
}
}
func execBashCmd(cmd *exec.Cmd) error {
stderr, _ := cmd.StderrPipe()
if err := cmd.Start(); err != nil {
fmt.Print("error= " + err.Error())
}
slurp, _ := io.ReadAll(stderr)
fmt.Printf("%s\n", slurp)
if slurp != nil {
}
if err := cmd.Wait(); err != nil {
fmt.Print("error= " + err.Error())
return err
}
return nil
}
Output of above code:
Cmd1= /usr/bin/git -C /Users/gitissue branch Outer
Cmd2= /usr/bin/git -C /Users/gitissue/folder1 worktree add Outer OuterPreparing worktree (checking out 'Outer')
Cmd3= /usr/bin/git -C /Users/gitissue/folder1/Outer commit -m Commiting-from-folder1-Outer
error= exit status 1error3= exit status 1