This repository was archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 534
plumbing: object, Add support for Log with filenames. Fixes #826 #979
Merged
Merged
Changes from all 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
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,115 @@ | ||
package object | ||
|
||
import ( | ||
"gopkg.in/src-d/go-git.v4/plumbing/storer" | ||
"io" | ||
) | ||
|
||
type commitFileIter struct { | ||
fileName string | ||
sourceIter CommitIter | ||
currentCommit *Commit | ||
} | ||
|
||
// NewCommitFileIterFromIter returns a commit iterator which performs diffTree between | ||
// successive trees returned from the commit iterator from the argument. The purpose of this is | ||
// to find the commits that explain how the files that match the path came to be. | ||
func NewCommitFileIterFromIter(fileName string, commitIter CommitIter) CommitIter { | ||
iterator := new(commitFileIter) | ||
iterator.sourceIter = commitIter | ||
iterator.fileName = fileName | ||
return iterator | ||
} | ||
|
||
func (c *commitFileIter) Next() (*Commit, error) { | ||
if c.currentCommit == nil { | ||
var err error | ||
c.currentCommit, err = c.sourceIter.Next() | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
commit, commitErr := c.getNextFileCommit() | ||
|
||
// Setting current-commit to nil to prevent unwanted states when errors are raised | ||
if commitErr != nil { | ||
c.currentCommit = nil | ||
} | ||
return commit, commitErr | ||
} | ||
|
||
func (c *commitFileIter) getNextFileCommit() (*Commit, error) { | ||
for { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you move this logic to a function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
// Parent-commit can be nil if the current-commit is the initial commit | ||
parentCommit, parentCommitErr := c.sourceIter.Next() | ||
if parentCommitErr != nil { | ||
// If the parent-commit is beyond the initial commit, keep it nil | ||
if parentCommitErr != io.EOF { | ||
return nil, parentCommitErr | ||
} | ||
parentCommit = nil | ||
} | ||
|
||
// Fetch the trees of the current and parent commits | ||
currentTree, currTreeErr := c.currentCommit.Tree() | ||
if currTreeErr != nil { | ||
return nil, currTreeErr | ||
} | ||
|
||
var parentTree *Tree | ||
if parentCommit != nil { | ||
var parentTreeErr error | ||
parentTree, parentTreeErr = parentCommit.Tree() | ||
if parentTreeErr != nil { | ||
return nil, parentTreeErr | ||
} | ||
} | ||
|
||
// Find diff between current and parent trees | ||
changes, diffErr := DiffTree(currentTree, parentTree) | ||
if diffErr != nil { | ||
return nil, diffErr | ||
} | ||
|
||
foundChangeForFile := false | ||
for _, change := range changes { | ||
if change.name() == c.fileName { | ||
foundChangeForFile = true | ||
break | ||
} | ||
} | ||
|
||
// Storing the current-commit in-case a change is found, and | ||
// Updating the current-commit for the next-iteration | ||
prevCommit := c.currentCommit | ||
c.currentCommit = parentCommit | ||
|
||
if foundChangeForFile == true { | ||
return prevCommit, nil | ||
} | ||
|
||
// If not matches found and if parent-commit is beyond the initial commit, then return with EOF | ||
if parentCommit == nil { | ||
return nil, io.EOF | ||
} | ||
} | ||
} | ||
|
||
func (c *commitFileIter) ForEach(cb func(*Commit) error) error { | ||
for { | ||
commit, nextErr := c.Next() | ||
if nextErr != nil { | ||
return nextErr | ||
} | ||
err := cb(commit) | ||
if err == storer.ErrStop { | ||
return nil | ||
} else if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
func (c *commitFileIter) Close() { | ||
c.sourceIter.Close() | ||
} |
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
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
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.
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.
Can you document this?
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.
Done