-
Notifications
You must be signed in to change notification settings - Fork 127
Add "git blame" parser #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package git | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// BlameFile return map of line number and Commit that change that line | ||
unknwon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func (r *Repository) BlameFile(file string) (map[int]*Commit, error) { | ||
unknwon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cmd := NewCommand("blame", "-p", file) | ||
stdout, err := cmd.Run() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return Blame(stdout) | ||
} | ||
|
||
// Blame parse content of `git blame` in porcelain format | ||
func Blame(content []byte) (map[int]*Commit, error) { | ||
var commits = make(map[[20]byte]*Commit) | ||
var commit = &Commit{} | ||
var details = make(map[string]string) | ||
var result = make(map[int]*Commit) | ||
scanner := bufio.NewScanner(bytes.NewReader(content)) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if string(line[0]) != "\t" { | ||
words := strings.Fields(line) | ||
sha, err := NewIDFromString(words[0]) | ||
if err == nil { | ||
// SHA and rows numbers line | ||
commit = getCommit(sha, commits) | ||
commit.fill(details) | ||
details = make(map[string]string) // empty all details | ||
i, err := strconv.Atoi(words[2]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result[i] = commit | ||
} else { | ||
// commit details line | ||
switch words[0] { | ||
case "summary": | ||
commit.Message = line[len(words[0])+1:] | ||
case "previous": | ||
commit.parents = []*SHA1{MustIDFromString(words[1])} | ||
default: | ||
if len(words) > 1 { | ||
details[words[0]] = line[len(words[0])+1:] | ||
} | ||
} | ||
} | ||
} else { | ||
// needed for last line in blame | ||
commit.fill(details) | ||
} | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
// Return commit from map or creates a new one | ||
func getCommit(sha *SHA1, commits map[[20]byte]*Commit) *Commit { | ||
commit, ok := commits[sha.bytes] | ||
if !ok { | ||
commit = &Commit{ | ||
ID: sha, | ||
} | ||
commits[sha.bytes] = commit | ||
} | ||
|
||
return commit | ||
} | ||
|
||
func (c *Commit) fill(data map[string]string) { | ||
author, ok := data["author"] | ||
if ok && c.Author == nil { | ||
t, err := parseBlameTime(data, "author") | ||
if err != nil { | ||
c.Author = &Signature{ | ||
Name: author, | ||
Email: data["author-mail"], | ||
} | ||
} else { | ||
c.Author = &Signature{ | ||
Name: author, | ||
Email: data["author-mail"], | ||
When: t, | ||
} | ||
} | ||
} | ||
committer, ok := data["committer"] | ||
if ok && c.Committer == nil { | ||
t, err := parseBlameTime(data, "committer") | ||
if err != nil { | ||
c.Committer = &Signature{ | ||
Name: committer, | ||
Email: data["committer-mail"], | ||
} | ||
} else { | ||
c.Committer = &Signature{ | ||
Name: committer, | ||
Email: data["committer-mail"], | ||
When: t, | ||
} | ||
} | ||
} | ||
} | ||
|
||
func parseBlameTime(data map[string]string, prefix string) (time.Time, error) { | ||
atoi, err := strconv.ParseInt(data[prefix+"-time"], 10, 64) | ||
if err != nil { | ||
return time.Time{}, err | ||
} | ||
t := time.Unix(atoi, 0) | ||
|
||
if len(data["author-tz"]) == 5 { | ||
hours, ok1 := strconv.ParseInt(data[prefix+"-tz"][:3], 10, 0) | ||
mins, ok2 := strconv.ParseInt(data[prefix+"-tz"][3:5], 10, 0) | ||
if ok1 == nil && ok2 == nil { | ||
t = t.In(time.FixedZone("Fixed", int((hours*60+mins)*60))) | ||
} | ||
} | ||
return t, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package git | ||
zhukovra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"time" | ||
unknwon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
var oneRowBlame = `2c49687c5b06776a44e2a8c0635428f647909472 3 3 4 | ||
author ᴜɴᴋɴᴡᴏɴ | ||
author-mail <[email protected]> | ||
author-time 1585383299 | ||
author-tz +0800 | ||
committer GitHub | ||
committer-mail <[email protected]> | ||
committer-time 1585383299 | ||
committer-tz +0800 | ||
summary ci: migrate from Travis to GitHub Actions (#50) | ||
previous 0d17b78404b7432905a58a235d875e9d28969ee3 README.md | ||
filename README.md | ||
[](https://github.com/gogs/git-module/actions?query=workflow%3AGo) | ||
` | ||
|
||
var twoRowsBlame = `f29bce1e3a666c02175d080892be185405dd3af4 1 1 2 | ||
author Unknwon | ||
author-mail <[email protected]> | ||
author-time 1573967409 | ||
author-tz -0800 | ||
committer Unknwon | ||
committer-mail <[email protected]> | ||
committer-time 1573967409 | ||
committer-tz -0800 | ||
summary README: update badges | ||
previous 065699e51f42559ab0c3ad22c1f2c789b2def8fb README.md | ||
filename README.md | ||
# Git Module | ||
f29bce1e3a666c02175d080892be185405dd3af4 2 2 | ||
|
||
` | ||
|
||
var commit1 = &Commit{ | ||
ID: MustIDFromString("f29bce1e3a666c02175d080892be185405dd3af4"), | ||
Message: "README: update badges", | ||
Author: &Signature{ | ||
Name: "Unknwon", | ||
Email: "<[email protected]>", | ||
When: time.Unix(1573967409, 0).In(time.FixedZone("Fixed", -8*60*60)), | ||
}, | ||
Committer: &Signature{ | ||
Name: "Unknwon", | ||
Email: "<[email protected]>", | ||
When: time.Unix(1573967409, 0).In(time.FixedZone("Fixed", -8*60*60)), | ||
}, | ||
parents: []*SHA1{MustIDFromString("065699e51f42559ab0c3ad22c1f2c789b2def8fb")}, | ||
} | ||
|
||
var commit2 = &Commit{ | ||
ID: MustIDFromString("2c49687c5b06776a44e2a8c0635428f647909472"), | ||
Message: "ci: migrate from Travis to GitHub Actions (#50)", | ||
Author: &Signature{ | ||
Name: "ᴜɴᴋɴᴡᴏɴ", | ||
Email: "<[email protected]>", | ||
When: time.Unix(1585383299, 0).In(time.FixedZone("Fixed", 8*60*60)), | ||
}, | ||
Committer: &Signature{ | ||
Name: "GitHub", | ||
Email: "<[email protected]>", | ||
When: time.Unix(1585383299, 0).In(time.FixedZone("Fixed", 8*60*60)), | ||
}, | ||
parents: []*SHA1{MustIDFromString("0d17b78404b7432905a58a235d875e9d28969ee3")}, | ||
} | ||
|
||
func TestOneRowBlame(t *testing.T) { | ||
blame, _ := Blame([]byte(oneRowBlame)) | ||
var expect = make(map[int]*Commit) | ||
|
||
expect[3] = commit2 | ||
|
||
assert.Equal(t, expect, blame) | ||
} | ||
|
||
func TestMultipleRowsBlame(t *testing.T) { | ||
blame, _ := Blame([]byte(twoRowsBlame + oneRowBlame)) | ||
var expect = make(map[int]*Commit) | ||
|
||
expect[1] = commit1 | ||
expect[2] = commit1 | ||
expect[3] = commit2 | ||
|
||
assert.Equal(t, expect, blame) | ||
} | ||
|
||
func TestRepository_BlameFile(t *testing.T) { | ||
blame, _ := testrepo.BlameFile("README.md") | ||
unknwon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert.Greater(t, len(blame), 0) | ||
} | ||
|
||
func TestRepository_BlameNotExistFile(t *testing.T) { | ||
_, err := testrepo.BlameFile("0") | ||
assert.Error(t, err) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.