|
4 | 4 |
|
5 | 5 | package git
|
6 | 6 |
|
7 |
| -import ( |
8 |
| - "bufio" |
9 |
| - "bytes" |
10 |
| - "strconv" |
11 |
| - "strings" |
12 |
| - "time" |
13 |
| -) |
14 |
| - |
15 |
| -type ( |
16 |
| - Blame struct { |
17 |
| - commits map[int]*Commit |
18 |
| - } |
19 |
| - BlameOptions struct { |
20 |
| - } |
21 |
| -) |
22 |
| - |
23 |
| -// BlameFile returns map of line number and the Commit changed that line. |
24 |
| -func (r *Repository) BlameFile(rev, file string, opts ...BlameOptions) (*Blame, error) { |
25 |
| - cmd := NewCommand("blame", "-p", rev, "--", file) |
26 |
| - stdout, err := cmd.RunInDir(r.path) |
27 |
| - if err != nil { |
28 |
| - return nil, err |
29 |
| - } |
30 |
| - return BlameContent(stdout) |
31 |
| -} |
32 |
| - |
33 |
| -// BlameContent parse content of `git blame` in porcelain format |
34 |
| -func BlameContent(content []byte) (*Blame, error) { |
35 |
| - var commits = make(map[[20]byte]*Commit) |
36 |
| - var commit = &Commit{} |
37 |
| - var details = make(map[string]string) |
38 |
| - var result = createBlame() |
39 |
| - scanner := bufio.NewScanner(bytes.NewReader(content)) |
40 |
| - for scanner.Scan() { |
41 |
| - line := scanner.Text() |
42 |
| - if string(line[0]) != "\t" { |
43 |
| - words := strings.Fields(line) |
44 |
| - sha, err := NewIDFromString(words[0]) |
45 |
| - if err == nil { |
46 |
| - // SHA and rows numbers line |
47 |
| - commit = getCommit(sha, commits) |
48 |
| - commit.fill(details) |
49 |
| - details = make(map[string]string) // empty all details |
50 |
| - i, err := strconv.Atoi(words[2]) |
51 |
| - if err != nil { |
52 |
| - return nil, err |
53 |
| - } |
54 |
| - result.commits[i] = commit |
55 |
| - } else { |
56 |
| - // commit details line |
57 |
| - switch words[0] { |
58 |
| - case "summary": |
59 |
| - commit.Message = line[len(words[0])+1:] |
60 |
| - case "previous": |
61 |
| - commit.parents = []*SHA1{MustIDFromString(words[1])} |
62 |
| - default: |
63 |
| - if len(words) > 1 { |
64 |
| - details[words[0]] = line[len(words[0])+1:] |
65 |
| - } |
66 |
| - } |
67 |
| - } |
68 |
| - } else { |
69 |
| - // needed for last line in blame |
70 |
| - commit.fill(details) |
71 |
| - } |
72 |
| - } |
73 |
| - |
74 |
| - return result, nil |
75 |
| -} |
76 |
| - |
77 |
| -func createBlame() *Blame { |
78 |
| - var blame = Blame{} |
79 |
| - blame.commits = make(map[int]*Commit) |
80 |
| - return &blame |
| 7 | +// Blame contains information of a Git file blame. |
| 8 | +type Blame struct { |
| 9 | + lines []*Commit |
81 | 10 | }
|
82 | 11 |
|
83 |
| -// Return commit from map or creates a new one |
84 |
| -func getCommit(sha *SHA1, commits map[[20]byte]*Commit) *Commit { |
85 |
| - commit, ok := commits[sha.bytes] |
86 |
| - if !ok { |
87 |
| - commit = &Commit{ |
88 |
| - ID: sha, |
89 |
| - } |
90 |
| - commits[sha.bytes] = commit |
91 |
| - } |
92 |
| - |
93 |
| - return commit |
94 |
| -} |
95 |
| - |
96 |
| -func (c *Commit) fill(data map[string]string) { |
97 |
| - author, ok := data["author"] |
98 |
| - if ok && c.Author == nil { |
99 |
| - t, err := parseBlameTime(data, "author") |
100 |
| - if err != nil { |
101 |
| - c.Author = &Signature{ |
102 |
| - Name: author, |
103 |
| - Email: data["author-mail"], |
104 |
| - } |
105 |
| - } else { |
106 |
| - c.Author = &Signature{ |
107 |
| - Name: author, |
108 |
| - Email: data["author-mail"], |
109 |
| - When: t, |
110 |
| - } |
111 |
| - } |
112 |
| - } |
113 |
| - committer, ok := data["committer"] |
114 |
| - if ok && c.Committer == nil { |
115 |
| - t, err := parseBlameTime(data, "committer") |
116 |
| - if err != nil { |
117 |
| - c.Committer = &Signature{ |
118 |
| - Name: committer, |
119 |
| - Email: data["committer-mail"], |
120 |
| - } |
121 |
| - } else { |
122 |
| - c.Committer = &Signature{ |
123 |
| - Name: committer, |
124 |
| - Email: data["committer-mail"], |
125 |
| - When: t, |
126 |
| - } |
127 |
| - } |
128 |
| - } |
129 |
| -} |
130 |
| - |
131 |
| -func parseBlameTime(data map[string]string, prefix string) (time.Time, error) { |
132 |
| - atoi, err := strconv.ParseInt(data[prefix+"-time"], 10, 64) |
133 |
| - if err != nil { |
134 |
| - return time.Time{}, err |
135 |
| - } |
136 |
| - t := time.Unix(atoi, 0) |
137 |
| - |
138 |
| - if len(data["author-tz"]) == 5 { |
139 |
| - hours, ok1 := strconv.ParseInt(data[prefix+"-tz"][:3], 10, 0) |
140 |
| - mins, ok2 := strconv.ParseInt(data[prefix+"-tz"][3:5], 10, 0) |
141 |
| - if ok1 == nil && ok2 == nil { |
142 |
| - t = t.In(time.FixedZone("Fixed", int((hours*60+mins)*60))) |
143 |
| - } |
| 12 | +// Line returns the commit by given line number (1-based). |
| 13 | +// It returns nil when no such line. |
| 14 | +func (b *Blame) Line(i int) *Commit { |
| 15 | + if i <= 0 || len(b.lines) < i { |
| 16 | + return nil |
144 | 17 | }
|
145 |
| - return t, nil |
| 18 | + return b.lines[i-1] |
146 | 19 | }
|
0 commit comments