grim/govcs

Change the package to be hosted on source hut
draft default tip
2021-11-18, Gary Kramlich
69c28523ced0
Change the package to be hosted on source hut
// govcs
// Copyright 2017-2019 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 interacts with Mercurial repositories.
package hg
import (
"os"
"strings"
"hg.sr.ht/~grim/govcs/exec"
"hg.sr.ht/~grim/govcs/vcs"
)
// Mercurial is an instance of vcs.VCS that will operate on a Mercurial
// repository.
type Mercurial struct {
wd string
}
// Detect will return a vcs.VCS instance if the given working directory
// is a Mercurial repository.
func Detect(wd string) vcs.VCS {
hg := &Mercurial{
wd: wd,
}
output := hg.runCmd([]string{"id", "-q"})
if output != "" {
return hg
}
return nil
}
// Name returns hg. This is used so that callers know what vcs they have.
func (m *Mercurial) Name() string {
return "hg"
}
// Remote will return the url for the given remote or 'default' if no remote
// is provided. If the repository does not have the given remote, empty string
// will be returned.
func (m *Mercurial) Remote(name string) string {
if name == "" {
name = "default"
}
remotes := m.Config("paths." + name)
if len(remotes) == 1 {
return remotes[0]
}
return ""
}
// Root will return the root of the repository.
func (m *Mercurial) Root() string {
return m.runCmd([]string{"root"})
}
// Commit will return the commit id of the repository.
func (m *Mercurial) Commit() string {
return m.runLog("{node}")
}
// ShortCommit returns the short commit id of the repository.
func (m *Mercurial) ShortCommit() string {
return m.runLog("{node|short}")
}
// Branch returns the name of the checked out branch.
func (m *Mercurial) Branch() string {
return m.runLog("{branch}")
}
// Bookmark returns the name of the active bookmark.
func (m *Mercurial) Bookmark() string {
return m.runLog("{activebookmark}")
}
// Config will look up the given key and return a string slice of the values.
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))
}