46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"io/ioutil"
|
||
|
"os"
|
||
|
"os/exec"
|
||
|
)
|
||
|
|
||
|
var gitStashName string
|
||
|
|
||
|
func gitStashPush() {
|
||
|
// create a temporary file that _ensures_ we have something to
|
||
|
// "git stash push". Without this, if all changes were staged, then
|
||
|
// "git stash push" would be a no-op, with exit code 0, and the
|
||
|
// final "git stash pop" would erroneously pop whatever the user had
|
||
|
// stashed themselves.
|
||
|
f, err := ioutil.TempFile(".", "git-pre-commit-hook-stash.")
|
||
|
if err != nil {
|
||
|
Err("failed to create temporary file: %v", err)
|
||
|
}
|
||
|
gitStashName = f.Name()
|
||
|
f.Close()
|
||
|
|
||
|
// perform git stash
|
||
|
cmd := exec.Command("git", "stash", "save",
|
||
|
"--keep-index", // don't stash staged changes
|
||
|
"--quiet",
|
||
|
"--include-untracked", // ignores new unstaged files
|
||
|
)
|
||
|
cmd.Stdout = os.Stdout
|
||
|
cmd.Stderr = os.Stderr
|
||
|
if err = cmd.Run(); err != nil {
|
||
|
Err("failed to run git stash: %v", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func gitStashPop() {
|
||
|
defer os.Remove(gitStashName)
|
||
|
cmd := exec.Command("git", "stash", "pop", "--quiet")
|
||
|
cmd.Stdout = os.Stdout
|
||
|
cmd.Stderr = os.Stderr
|
||
|
if err := cmd.Run(); err != nil {
|
||
|
Err("failed to run git stash pop: %v", err)
|
||
|
}
|
||
|
}
|