grim/govcs

Add a simple readme
draft
2017-12-05, Gary Kramlich
9798f686fc4f
Add a simple readme
/*
* govcs
* Copyright 2017 Gary Kramlich <grim@reaperworld.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package hg
import (
"os"
"strings"
"bitbucket.org/rw_grim/govcs/exec"
"bitbucket.org/rw_grim/govcs/vcs"
)
type Mercurial struct {
wd string
}
func Detect(wd string) vcs.VCS {
hg := &Mercurial{
wd: wd,
}
output := hg.runCmd([]string{"id", "-q"})
if output != "" {
return hg
}
return nil
}
func (m *Mercurial) Remote(name string) string {
if name == "" {
name = "default"
}
remotes := m.Config("paths." + name)
if len(remotes) == 1 {
parts := strings.SplitN(remotes[0], "=", 2)
return parts[1]
}
return ""
}
func (m *Mercurial) Commit() string {
return m.runLog("{node}")
}
func (m *Mercurial) ShortCommit() string {
return m.runLog("{node|short}")
}
func (m *Mercurial) Branch() string {
return m.runLog("{branch}")
}
func (m *Mercurial) Bookmark() string {
return m.runLog("{activebookmark}")
}
func (m *Mercurial) Config(key string) []string {
output := m.runCmd([]string{"config", key})
return strings.Split(string(output), "\n")
}
// runLog will runs `hg log` with the given template
func (m *Mercurial) runLog(template string) string {
return m.runCmd([]string{"log", "-r", ".", "-T", template})
}
// runCmd will run the hg command without the users config, it will also set
// working directory. All you need to pass in is a string slice of the
// subcommand that you want to run.
func (m *Mercurial) runCmd(cmdv []string) string {
os.Setenv("HGRCPATH", "/dev/null")
args := append([]string{"--cwd", m.wd}, cmdv...)
cmd := exec.Command("hg", args...)
output, err := cmd.Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(output))
}