| Index: go/src/infra/libs/git/user.go
|
| diff --git a/go/src/infra/libs/git/user.go b/go/src/infra/libs/git/user.go
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..1e1d2dd74c2ce69f35e3ac3cef48b50574dcd024
|
| --- /dev/null
|
| +++ b/go/src/infra/libs/git/user.go
|
| @@ -0,0 +1,68 @@
|
| +// Copyright 2014 The Chromium 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 git
|
| +
|
| +import (
|
| + "fmt"
|
| + "regexp"
|
| + "strconv"
|
| + "time"
|
| +)
|
| +
|
| +// Types ///////////////////////////////////////////////////////////////////////
|
| +
|
| +// User represents an author/committer line in a Commit
|
| +type User struct {
|
| + Name string
|
| + Email string
|
| + Time time.Time
|
| +}
|
| +
|
| +// Constructors ////////////////////////////////////////////////////////////////
|
| +
|
| +func MakeUserFromCommitLine(lineType, line string) (User, error) {
|
| + fields := userRegex.FindStringSubmatch(line)
|
| + if len(fields) == 0 {
|
| + return User{}, fmt.Errorf("Incompatible user line: %#v", line)
|
| + }
|
| + if fields[1] != lineType {
|
| + return User{}, fmt.Errorf("Expected to parse %s but got %s instead", lineType, fields[1])
|
| + }
|
| + t, err := time.Parse("-0700", fields[5])
|
| + if err != nil {
|
| + return User{}, err
|
| + }
|
| +
|
| + timecode, err := strconv.ParseInt(fields[4], 10, 64)
|
| + if err != nil {
|
| + return User{}, err
|
| + }
|
| +
|
| + return User{
|
| + Name: fields[2],
|
| + Email: fields[3],
|
| + Time: time.Unix(timecode, 0).In(t.Location()),
|
| + }, nil
|
| +}
|
| +
|
| +// Member functions ////////////////////////////////////////////////////////////
|
| +
|
| +// RawString returns a `git hash-object` compatible string for this User
|
| +func (u *User) RawString() string {
|
| + return fmt.Sprintf("%s <%s> %d %s", u.Name, u.Email, u.Time.Unix(),
|
| + u.Time.Format("-0700"))
|
| +}
|
| +
|
| +// Private /////////////////////////////////////////////////////////////////////
|
| +
|
| +// author|committer SP <user_name> SP '<' <user_email> '>' SP timecode +0000
|
| +// I would use named groups, but the interface to access them is terrible
|
| +// in the regexp package...
|
| +var userRegex = regexp.MustCompile(
|
| + `^(author|committer) ` +
|
| + `([^<]*) ` + // user_name
|
| + `<([^>]*)> ` + // user_email
|
| + `(\d*) ` + // timecode
|
| + `([+-]\d{4})$`) //timezone
|
|
|