| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package git |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 "regexp" |
| 10 "strconv" |
| 11 "time" |
| 12 ) |
| 13 |
| 14 // Types /////////////////////////////////////////////////////////////////////// |
| 15 |
| 16 // User represents an author/committer line in a Commit |
| 17 type User struct { |
| 18 Name string |
| 19 Email string |
| 20 Time time.Time |
| 21 } |
| 22 |
| 23 // Constructors //////////////////////////////////////////////////////////////// |
| 24 |
| 25 func MakeUserFromCommitLine(lineType, line string) (User, error) { |
| 26 fields := userRegex.FindStringSubmatch(line) |
| 27 if len(fields) == 0 { |
| 28 return User{}, fmt.Errorf("Incompatible user line: %#v", line) |
| 29 } |
| 30 if fields[1] != lineType { |
| 31 return User{}, fmt.Errorf("Expected to parse %s but got %s inste
ad", lineType, fields[1]) |
| 32 } |
| 33 t, err := time.Parse("-0700", fields[5]) |
| 34 if err != nil { |
| 35 return User{}, err |
| 36 } |
| 37 |
| 38 timecode, err := strconv.ParseInt(fields[4], 10, 64) |
| 39 if err != nil { |
| 40 return User{}, err |
| 41 } |
| 42 |
| 43 return User{ |
| 44 Name: fields[2], |
| 45 Email: fields[3], |
| 46 Time: time.Unix(timecode, 0).In(t.Location()), |
| 47 }, nil |
| 48 } |
| 49 |
| 50 // Member functions //////////////////////////////////////////////////////////// |
| 51 |
| 52 // RawString returns a `git hash-object` compatible string for this User |
| 53 func (u *User) RawString() string { |
| 54 return fmt.Sprintf("%s <%s> %d %s", u.Name, u.Email, u.Time.Unix(), |
| 55 u.Time.Format("-0700")) |
| 56 } |
| 57 |
| 58 // Private ///////////////////////////////////////////////////////////////////// |
| 59 |
| 60 // author|committer SP <user_name> SP '<' <user_email> '>' SP timecode +0000 |
| 61 // I would use named groups, but the interface to access them is terrible |
| 62 // in the regexp package... |
| 63 var userRegex = regexp.MustCompile( |
| 64 `^(author|committer) ` + |
| 65 `([^<]*) ` + // user_name |
| 66 `<([^>]*)> ` + // user_email |
| 67 `(\d*) ` + // timecode |
| 68 `([+-]\d{4})$`) //timezone |
| OLD | NEW |