Skip to content

Instantly share code, notes, and snippets.

@aeppert
Created November 28, 2016 19:00
Show Gist options
  • Select an option

  • Save aeppert/dc89b63e8d8fc5c5d07bf8484df32894 to your computer and use it in GitHub Desktop.

Select an option

Save aeppert/dc89b63e8d8fc5c5d07bf8484df32894 to your computer and use it in GitHub Desktop.
package main
import (
"errors"
"fmt"
"os"
"strings"
"os/exec"
)
func execmd(cmd string, env []string) (string, error) {
// splitting head => g++ parts => rest of the command
parts := strings.Fields(cmd)
head := parts[0]
parts = parts[1:len(parts)]
runcmd := exec.Command(head, parts...)
if len(env) > 0 {
runcmd.Env = env
}
out, err := runcmd.Output()
if err != nil {
return string(out[:]), err
}
return string(out[:]), nil
}
func setGitEnv() []string {
env := os.Environ()
env = append(env, fmt.Sprintf("GIT_SSH=%s", config.GITSSH))
return env
}
func genGitPathArgs(repo string) string {
pathArgs := []string{"--git-dir=" + repo + string(os.PathSeparator) + ".git",
"--work-tree=" + repo}
return strings.Join(pathArgs, " ")
}
func handleSubmodules(repo string) (string, error) {
var env []string
cwd, err := os.Getwd()
if err != nil {
return "", err
}
os.Chdir(repo)
defer os.Chdir(cwd)
out, err := execmd("git submodule sync", env)
if err != nil {
return out, err
}
out, err = execmd("git submodule init", env)
if err != nil {
return out, err
}
out, err = execmd("git submodule update", env)
if err != nil {
return out, err
}
out, err = execmd("git submodule foreach --recursive git checkout .", env)
if err != nil {
return out, err
}
out, err = execmd("git submodule update --recursive", env)
if err != nil {
return out, err
}
return "", nil
}
// RepoClone
func RepoClone(repo string, rev string, dest string, recursive bool, keyfile string) (string, error) {
var gitclone string
var gitoptions string
var env []string
if recursive {
gitoptions += "--recursive"
}
if len(keyfile) > 0 {
env = setGitEnv()
}
if len(rev) > 0 {
gitclone += strings.Join([]string{"git clone", gitoptions, repo, "-b", rev, dest}, " ")
} else {
gitclone += strings.Join([]string{"git clone", gitoptions, repo, dest}, " ")
}
// fmt.Println("git command =", gitclone)
out, err := execmd(gitclone, env)
if err != nil {
return out, err
}
return out, nil
}
// RepoPull
// Note:
// recursive option will update all submodules
//
func RepoPull(repo string, recursive bool) (string, error) {
var env []string
gitcmd := []string{"git", genGitPathArgs(repo), "pull"}
// fmt.Println("git command =", gitclone)
out, err := execmd(strings.Join(gitcmd, " "), env)
if err != nil {
return out, err
}
if recursive {
_, err := handleSubmodules(repo)
if err != nil {
return out, err
}
}
return "", nil
}
// RepoCheckout
// Note:
// recursive option will update all submodules
//
func RepoCheckout(repo string, hash string, recursive bool) (string, error) {
var gitcmd string
var env []string
if len(hash) == 0 {
return "", errors.New("Hash value is incorrect")
}
gitcmd += strings.Join([]string{"git checkout", hash}, " ")
// fmt.Println("git command =", gitclone)
out, err := execmd(gitcmd, env)
if err != nil {
return out, err
}
if recursive {
_, err := handleSubmodules(repo)
if err != nil {
return out, err
}
}
return "", nil
}
// RepoHash
func RepoHash(repo string) (string, error) {
gitargs := []string{"git", genGitPathArgs(repo),
"rev-parse",
"--verify", "HEAD"}
githash := strings.Join(gitargs, " ")
out, err := execmd(githash, nil)
if err != nil {
return "", err
}
return strings.TrimSpace(out), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment