2011-03-02 20:41:09 -07:00
|
|
|
// Copyright 2011 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.
|
|
|
|
|
2010-09-12 18:46:17 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2010-09-30 22:14:18 -06:00
|
|
|
"fmt"
|
2010-09-12 18:46:17 -06:00
|
|
|
"os"
|
2011-03-02 20:41:09 -07:00
|
|
|
"regexp"
|
2010-09-12 18:46:17 -06:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Commit struct {
|
|
|
|
num int // mercurial revision number
|
|
|
|
node string // mercurial hash
|
|
|
|
parent string // hash of commit's parent
|
|
|
|
user string // author's Name <email>
|
|
|
|
date string // date of commit
|
|
|
|
desc string // description
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCommit returns details about the Commit specified by the revision hash
|
|
|
|
func getCommit(rev string) (c Commit, err os.Error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2010-09-30 22:14:18 -06:00
|
|
|
err = fmt.Errorf("getCommit: %s: %s", rev, err)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
parts, err := getCommitParts(rev)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
num, err := strconv.Atoi(parts[0])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
parent := ""
|
|
|
|
if num > 0 {
|
|
|
|
prev := strconv.Itoa(num - 1)
|
|
|
|
if pparts, err := getCommitParts(prev); err == nil {
|
|
|
|
parent = pparts[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user := strings.Replace(parts[2], "<", "<", -1)
|
|
|
|
user = strings.Replace(user, ">", ">", -1)
|
|
|
|
return Commit{num, parts[1], parent, user, parts[3], parts[4]}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCommitParts(rev string) (parts []string, err os.Error) {
|
|
|
|
const format = "{rev}>{node}>{author|escape}>{date}>{desc}"
|
2010-10-20 22:33:31 -06:00
|
|
|
s, _, err := runLog(nil, "", goroot,
|
2011-02-22 13:47:53 -07:00
|
|
|
"hg", "log",
|
|
|
|
"--encoding", "utf-8",
|
|
|
|
"--rev", rev,
|
|
|
|
"--limit", "1",
|
|
|
|
"--template", format,
|
|
|
|
)
|
2010-09-12 18:46:17 -06:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return strings.Split(s, ">", 5), nil
|
|
|
|
}
|
2011-03-02 20:41:09 -07:00
|
|
|
|
|
|
|
var revisionRe = regexp.MustCompile(`([0-9]+):[0-9a-f]+$`)
|
|
|
|
|
|
|
|
// getTag fetches a Commit by finding the first hg tag that matches re.
|
|
|
|
func getTag(re *regexp.Regexp) (c Commit, tag string, err os.Error) {
|
|
|
|
o, _, err := runLog(nil, "", goroot, "hg", "tags")
|
|
|
|
for _, l := range strings.Split(o, "\n", -1) {
|
|
|
|
tag = re.FindString(l)
|
|
|
|
if tag == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s := revisionRe.FindStringSubmatch(l)
|
|
|
|
if s == nil {
|
|
|
|
err = os.NewError("couldn't find revision number")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c, err = getCommit(s[1])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = os.NewError("no matching tag found")
|
|
|
|
return
|
|
|
|
}
|