2013-07-31 21:23:51 -06:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2013-08-27 22:52:18 -06:00
|
|
|
|
|
|
|
"code.google.com/p/go.tools/go/vcs"
|
2013-07-31 21:23:51 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Repo represents a mercurial repository.
|
|
|
|
type Repo struct {
|
2013-08-27 22:52:18 -06:00
|
|
|
Path string
|
|
|
|
Master *vcs.RepoRoot
|
2013-07-31 21:23:51 -06:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteRepo constructs a *Repo representing a remote repository.
|
2013-08-27 22:52:18 -06:00
|
|
|
func RemoteRepo(url, path string) (*Repo, error) {
|
|
|
|
rr, err := vcs.RepoRootForImportPath(url, *verbose)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-07-31 21:23:51 -06:00
|
|
|
}
|
2013-08-27 22:52:18 -06:00
|
|
|
return &Repo{
|
|
|
|
Path: path,
|
|
|
|
Master: rr,
|
|
|
|
}, nil
|
2013-07-31 21:23:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clone clones the current Repo to a new destination
|
|
|
|
// returning a new *Repo if successful.
|
|
|
|
func (r *Repo) Clone(path, rev string) (*Repo, error) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2013-08-27 22:52:18 -06:00
|
|
|
|
|
|
|
err := timeout(*cmdTimeout, func() error {
|
2013-10-07 10:06:32 -06:00
|
|
|
downloadPath := r.Path
|
|
|
|
if !r.Exists() {
|
|
|
|
downloadPath = r.Master.Repo
|
|
|
|
}
|
|
|
|
|
|
|
|
err := r.Master.VCS.CreateAtRev(path, downloadPath, rev)
|
2013-08-27 22:52:18 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return r.Master.VCS.TagSync(path, "")
|
|
|
|
})
|
|
|
|
if err != nil {
|
2013-07-31 21:23:51 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Repo{
|
2013-08-27 22:52:18 -06:00
|
|
|
Path: path,
|
|
|
|
Master: r.Master,
|
2013-07-31 21:23:51 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2014-02-13 18:50:29 -07:00
|
|
|
// Export exports the current Repo at revision rev to a new destination.
|
|
|
|
func (r *Repo) Export(path, rev string) error {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
downloadPath := r.Path
|
|
|
|
if !r.Exists() {
|
|
|
|
_, err := r.Clone(path, rev)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{r.Master.VCS.Cmd, "archive", "-t", "files", "-r", rev, path}
|
|
|
|
if err := run(*cmdTimeout, nil, downloadPath, args...); err != nil {
|
|
|
|
return fmt.Errorf("executing %s: %v", strings.Join(args, " "), err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-31 21:23:51 -06:00
|
|
|
// UpdateTo updates the working copy of this Repo to the
|
|
|
|
// supplied revision.
|
|
|
|
func (r *Repo) UpdateTo(hash string) error {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2013-08-27 22:52:18 -06:00
|
|
|
|
|
|
|
return timeout(*cmdTimeout, func() error {
|
|
|
|
return r.Master.VCS.TagSync(r.Path, hash)
|
|
|
|
})
|
2013-07-31 21:23:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exists reports whether this Repo represents a valid Mecurial repository.
|
|
|
|
func (r *Repo) Exists() bool {
|
2013-08-27 22:52:18 -06:00
|
|
|
fi, err := os.Stat(filepath.Join(r.Path, "."+r.Master.VCS.Cmd))
|
2013-07-31 21:23:51 -06:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return fi.IsDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull pulls changes from the default path, that is, the path
|
|
|
|
// this Repo was cloned from.
|
|
|
|
func (r *Repo) Pull() error {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2013-08-27 22:52:18 -06:00
|
|
|
|
|
|
|
return timeout(*cmdTimeout, func() error {
|
|
|
|
return r.Master.VCS.Download(r.Path)
|
|
|
|
})
|
2013-07-31 21:23:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Log returns the changelog for this repository.
|
|
|
|
func (r *Repo) Log() ([]HgLog, error) {
|
|
|
|
if err := r.Pull(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
var logStruct struct {
|
|
|
|
Log []HgLog
|
|
|
|
}
|
2013-08-27 22:52:18 -06:00
|
|
|
err := timeout(*cmdTimeout, func() error {
|
|
|
|
data, err := r.Master.VCS.Log(r.Path, xmlLogTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = xml.Unmarshal([]byte("<Top>"+string(data)+"</Top>"), &logStruct)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unmarshal %s log: %v", r.Master.VCS, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2013-07-31 21:23:51 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-13 01:00:11 -06:00
|
|
|
for i, log := range logStruct.Log {
|
|
|
|
// Let's pretend there can be only one parent.
|
|
|
|
if log.Parent != "" && strings.Contains(log.Parent, " ") {
|
|
|
|
logStruct.Log[i].Parent = strings.Split(log.Parent, " ")[0]
|
|
|
|
}
|
|
|
|
}
|
2013-07-31 21:23:51 -06:00
|
|
|
return logStruct.Log, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FullHash returns the full hash for the given Mercurial revision.
|
|
|
|
func (r *Repo) FullHash(rev string) (string, error) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2013-08-27 22:52:18 -06:00
|
|
|
|
|
|
|
var hash string
|
|
|
|
err := timeout(*cmdTimeout, func() error {
|
|
|
|
data, err := r.Master.VCS.LogAtRev(r.Path, rev, "{node}")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := strings.TrimSpace(string(data))
|
|
|
|
if s == "" {
|
|
|
|
return fmt.Errorf("cannot find revision")
|
|
|
|
}
|
|
|
|
if len(s) != 40 {
|
|
|
|
return fmt.Errorf("%s returned invalid hash: %s", r.Master.VCS, s)
|
|
|
|
}
|
|
|
|
hash = s
|
|
|
|
return nil
|
|
|
|
})
|
2013-07-31 21:23:51 -06:00
|
|
|
if err != nil {
|
2013-08-27 22:52:18 -06:00
|
|
|
return "", err
|
2013-07-31 21:23:51 -06:00
|
|
|
}
|
2013-08-27 22:52:18 -06:00
|
|
|
return hash, nil
|
2013-07-31 21:23:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// HgLog represents a single Mercurial revision.
|
|
|
|
type HgLog struct {
|
|
|
|
Hash string
|
|
|
|
Author string
|
|
|
|
Date string
|
|
|
|
Desc string
|
|
|
|
Parent string
|
2014-05-13 01:00:11 -06:00
|
|
|
Branch string
|
|
|
|
Files string
|
2013-07-31 21:23:51 -06:00
|
|
|
|
|
|
|
// Internal metadata
|
|
|
|
added bool
|
2014-05-13 01:00:11 -06:00
|
|
|
bench bool // needs to be benchmarked?
|
2013-07-31 21:23:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// xmlLogTemplate is a template to pass to Mercurial to make
|
|
|
|
// hg log print the log in valid XML for parsing with xml.Unmarshal.
|
2014-05-13 01:00:11 -06:00
|
|
|
// Can not escape branches and files, because it crashes python with:
|
|
|
|
// AttributeError: 'NoneType' object has no attribute 'replace'
|
2013-07-31 21:23:51 -06:00
|
|
|
const xmlLogTemplate = `
|
|
|
|
<Log>
|
|
|
|
<Hash>{node|escape}</Hash>
|
2014-05-13 01:00:11 -06:00
|
|
|
<Parent>{parents}</Parent>
|
2013-07-31 21:23:51 -06:00
|
|
|
<Author>{author|escape}</Author>
|
|
|
|
<Date>{date|rfc3339date}</Date>
|
|
|
|
<Desc>{desc|escape}</Desc>
|
2014-05-13 01:00:11 -06:00
|
|
|
<Branch>{branches}</Branch>
|
|
|
|
<Files>{files}</Files>
|
2013-07-31 21:23:51 -06:00
|
|
|
</Log>
|
|
|
|
`
|