Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1001)

Unified Diff: go/src/infra/libs/git/user.go

Issue 662113003: Drover's back, baby! (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git/+/master
Patch Set: more tests and refactors Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « go/src/infra/libs/git/types.go ('k') | go/src/infra/libs/git/user_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « go/src/infra/libs/git/types.go ('k') | go/src/infra/libs/git/user_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698