-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Support .git-blame-ignore-revs file
#26395
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
Changes from 2 commits
1aeb8f2
b2d9961
e1a0ade
734048b
4125e33
c482da4
ac922ee
276e11b
61b079b
2da7857
d01b712
b4a1ab7
833c640
8d9e575
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,8 @@ import ( | |
| "io" | ||
| "os" | ||
| "regexp" | ||
|
|
||
| "code.gitea.io/gitea/modules/util" | ||
| ) | ||
|
|
||
| // BlamePart represents block of blame - continuous lines with one sha | ||
|
|
@@ -26,6 +28,11 @@ type BlameReader struct { | |
| bufferedReader *bufio.Reader | ||
| done chan error | ||
| lastSha *string | ||
| ignoreRevsFile *os.File | ||
| } | ||
|
|
||
| func (r *BlameReader) UsesIgnoreRevs() bool { | ||
| return r.ignoreRevsFile != nil | ||
| } | ||
|
|
||
| var shaLineRegex = regexp.MustCompile("^([a-z0-9]{40})") | ||
|
|
@@ -98,17 +105,32 @@ func (r *BlameReader) Close() error { | |
| r.bufferedReader = nil | ||
| _ = r.reader.Close() | ||
| _ = r.output.Close() | ||
| if r.ignoreRevsFile != nil { | ||
| _ = util.Remove(r.ignoreRevsFile.Name()) | ||
| } | ||
wxiaoguang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return err | ||
| } | ||
|
|
||
| // CreateBlameReader creates reader for given repository, commit and file | ||
| func CreateBlameReader(ctx context.Context, repoPath, commitID, file string) (*BlameReader, error) { | ||
| cmd := NewCommandContextNoGlobals(ctx, "blame", "--porcelain"). | ||
| AddDynamicArguments(commitID). | ||
| func CreateBlameReader(ctx context.Context, repoPath string, commit *Commit, file string, bypassBlameIgnore bool) (*BlameReader, error) { | ||
| cmd := NewCommandContextNoGlobals(ctx, "blame", "--porcelain") | ||
|
|
||
| var ignoreRevsFile *os.File | ||
KN4CK3R marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if !bypassBlameIgnore { | ||
| ignoreRevsFile = tryCreateBlameIgnoreRevsFile(commit) | ||
| if ignoreRevsFile != nil { | ||
| cmd.AddOptionValues("--ignore-revs-file", ignoreRevsFile.Name()) | ||
| } | ||
|
||
| } | ||
|
|
||
| cmd.AddDynamicArguments(commit.ID.String()). | ||
| AddDashesAndList(file). | ||
| SetDescription(fmt.Sprintf("GetBlame [repo_path: %s]", repoPath)) | ||
| reader, stdout, err := os.Pipe() | ||
| if err != nil { | ||
| if ignoreRevsFile != nil { | ||
| _ = util.Remove(ignoreRevsFile.Name()) | ||
| } | ||
| return nil, err | ||
| } | ||
|
|
||
|
|
@@ -134,5 +156,35 @@ func CreateBlameReader(ctx context.Context, repoPath, commitID, file string) (*B | |
| reader: reader, | ||
| bufferedReader: bufferedReader, | ||
| done: done, | ||
| ignoreRevsFile: ignoreRevsFile, | ||
| }, nil | ||
| } | ||
|
|
||
| func tryCreateBlameIgnoreRevsFile(commit *Commit) *os.File { | ||
| entry, err := commit.GetTreeEntryByPath(".git-blame-ignore-revs") | ||
| if err != nil { | ||
| return nil | ||
| } | ||
|
|
||
| r, err := entry.Blob().DataAsync() | ||
| if err != nil { | ||
| return nil | ||
| } | ||
| defer r.Close() | ||
|
|
||
| f, err := os.CreateTemp("", "gitea_git-blame-ignore-revs") | ||
delvh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return nil | ||
| } | ||
| defer f.Close() | ||
|
|
||
| _, err = io.Copy(f, r) | ||
| if err != nil { | ||
| defer func() { | ||
| _ = util.Remove(f.Name()) | ||
| }() | ||
| return nil | ||
| } | ||
KN4CK3R marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return f | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ref: refs/heads/master |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| [core] | ||
| repositoryformatversion = 0 | ||
| filemode = true | ||
| bare = true |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 544d8f7a3b15927cddf2299b4b562d6ebd71b6a7 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems strange to store a closed
Filehere. Maybe usingignoreRevsFileName stringis good enough?Otherwise it's not easy to understand when the file should be closed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used
os.Filehere to have an explicitnilcheck. Sure a!= ""check works too but my intent was to have a real file linked to the object. How about changing this member toos.FileInfo? That should be the best of both worlds.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the file has been closed, the object itself can't do other things except accessing the
f.Name(), which is simply{ return f.name }