github.com/ivotron/vio@v0.1.1-0.20160328072646-778e014d4dee/vcs.go (about)

     1  package vio
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"strings"
     7  )
     8  
     9  func runCmd(path string, cmdstr string) (out string, err error) {
    10  	cwd, err := os.Getwd()
    11  	if err != nil {
    12  		return
    13  	}
    14  	err = os.Chdir(path)
    15  	if err != nil {
    16  		return
    17  	}
    18  	if len(strings.TrimSpace(cmdstr)) == 0 {
    19  		err = AnError{"Empty command"}
    20  		return
    21  	}
    22  	cmd_items := strings.Split(cmdstr, " ")
    23  	cmd := cmd_items[0]
    24  	args := []string{}
    25  	if len(cmd_items) > 1 {
    26  		args = cmd_items[1:]
    27  	}
    28  	o, err := exec.Command(cmd, args...).Output()
    29  	if err != nil {
    30  		return
    31  	}
    32  	out = string(o)
    33  	err = os.Chdir(cwd)
    34  	return
    35  }
    36  
    37  func HasUncommittedChanges(repoPath string) (has bool, err error) {
    38  	out, err := runCmd(repoPath, "git status -s -uno")
    39  	if err != nil {
    40  		return
    41  	}
    42  	has = strings.TrimSpace(out) != ""
    43  	return
    44  }
    45  
    46  func GetVersionedFiles(repoPath string) (versioned []string, err error) {
    47  	out, err := runCmd(repoPath, "git ls-files")
    48  	if err != nil {
    49  		return
    50  	}
    51  	versioned = strings.Split(strings.TrimSpace(out), "\n")
    52  	return
    53  }
    54  
    55  func GetCurrentCommitId(repoPath string) (id string, err error) {
    56  	out, err := runCmd(repoPath, "git rev-parse --verify --short HEAD")
    57  	if err != nil {
    58  		return
    59  	}
    60  	id = strings.TrimSpace(out)
    61  	return
    62  }