grim/govcs

Add subversion support
draft
2017-12-27, Gary Kramlich
8f7139d219b9
Add subversion support
// 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 git interacts with Git repositories.
package svn
import (
"encoding/xml"
"fmt"
"strings"
"bitbucket.org/rw_grim/govcs/exec"
"bitbucket.org/rw_grim/govcs/vcs"
)
type svnCommit struct {
Revision string `xml:"revision,attr"`
}
type rawSubversion struct {
Remote string `xml:"entry>repository>root"`
Root string `xml:"entry>wc-info>wcroot-abspath"`
Commit svnCommit `xml:"entry>commit"`
Branch string `xml:"entry>relative-url"`
}
// SVN is an instance of vcs.VCS that will operate on a Subversion repository.
type Subversion struct {
wd string
raw rawSubversion
}
// Detect will return a vcs.VCS instance if the given working directory is a
// Git repository.
func Detect(wd string) vcs.VCS {
s := &Subversion{
wd: wd,
}
err := s.info()
if err != nil {
fmt.Printf("%#v\n", err)
return nil
}
return s
}
// Remote will return the url for repository. The given remote name is ignored
// as subversion does not support multiple remotes.
func (s *Subversion) Remote(name string) string {
return s.raw.Remote
}
// Root will return the root of the repository.
func (s *Subversion) Root() string {
return s.raw.Root
}
// Commit will return the commit id of the repository.
func (s *Subversion) Commit() string {
return s.raw.Commit.Revision
}
// ShortCommit returns the short commit id of the repository.
func (s *Subversion) ShortCommit() string {
return s.raw.Commit.Revision
}
// Branch returns the name of the checked out branch.
func (s *Subversion) Branch() string {
return s.raw.Branch
}
// Config will look up the given key and return a string slice of the values.
func (s *Subversion) Config(key string) []string {
return []string{}
}
// run `svn info $WD` and store the results in s.raw
func (s *Subversion) info() error {
cmd := exec.Command("svn", "info", "--config-dir", "/dev/null", s.wd)
output, err := cmd.Output()
if err != nil {
return err
}
var raw rawSubversion
err = xml.Unmarshal(output, &raw)
fmt.Printf("rawSubversion: %#v\n", raw)
if err != nil {
return err
}
s.raw = raw
// parse the branch now
if s.raw.Branch == "^/trunk" {
s.raw.Branch = "trunk"
} else if strings.HasPrefix(s.raw.Branch, "^/branches/") {
s.raw.Branch = s.raw.Branch[11:]
} else if strings.HasPrefix(s.raw.Branch, "^/tags/") {
s.raw.Branch = s.raw.Branch[7:]
}
return nil
}