generated from fallion/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
feat!: use git directly #73
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
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -1,17 +1,83 @@ | ||
| package git | ||
|
|
||
| import ( | ||
| "github.com/go-git/go-git/v5/plumbing" | ||
| "github.com/go-git/go-git/v5/plumbing/object" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| // Commit find a commit based on commit hash and returns the Commit object | ||
| func (g *Git) Commit(hash plumbing.Hash) (*object.Commit, error) { | ||
| commitObject, err := g.repo.CommitObject(hash) | ||
| // Commit finds a commit based on commit hash and returns the Commit object | ||
| func (g *Git) Commit(hash Hash) (*Commit, error) { | ||
| hashStr := hash.String() | ||
|
|
||
| // Get commit message - use %B to get full body, then normalize | ||
| // go-git's Message field returns the raw commit message which always ends with \n | ||
| fullMessage, err := g.runGitCommand("log", "-1", "--format=%B", hashStr) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Normalize: remove trailing newlines and add exactly one | ||
| // This matches go-git's behavior where Message always ends with \n | ||
| message := strings.TrimRight(fullMessage, "\n") | ||
| if message != "" { | ||
| message += "\n" | ||
| } | ||
|
|
||
| // Get author info | ||
| authorName, err := g.runGitCommand("log", "-1", "--format=%an", hashStr) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| authorEmail, err := g.runGitCommand("log", "-1", "--format=%ae", hashStr) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| authorDateStr, err := g.runGitCommand("log", "-1", "--format=%at", hashStr) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| authorTimestamp, err := strconv.ParseInt(authorDateStr, 10, 64) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Get committer info | ||
| committerName, err := g.runGitCommand("log", "-1", "--format=%cn", hashStr) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| committerEmail, err := g.runGitCommand("log", "-1", "--format=%ce", hashStr) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| committerDateStr, err := g.runGitCommand("log", "-1", "--format=%ct", hashStr) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| committerTimestamp, err := strconv.ParseInt(committerDateStr, 10, 64) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return commitObject, nil | ||
| return &Commit{ | ||
| Hash: hash, | ||
| Message: message, | ||
| Author: Signature{ | ||
| Name: authorName, | ||
| Email: authorEmail, | ||
| When: time.Unix(authorTimestamp, 0), | ||
| }, | ||
| Committer: Signature{ | ||
| Name: committerName, | ||
| Email: committerEmail, | ||
| When: time.Unix(committerTimestamp, 0), | ||
| }, | ||
| }, 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 |
|---|---|---|
| @@ -1,20 +1,23 @@ | ||
| package git | ||
|
|
||
| import ( | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/go-git/go-git/v5/plumbing" | ||
| ) | ||
|
|
||
| // commitDate gets the commit at hash and returns the time of the commit | ||
| func (g *Git) commitDate(commit plumbing.Hash) (time.Time, error) { | ||
| commitObject, err := g.repo.CommitObject(commit) | ||
| func (g *Git) commitDate(commit Hash) (time.Time, error) { | ||
| hashStr := commit.String() | ||
|
|
||
| dateStr, err := g.runGitCommand("log", "-1", "--format=%ct", hashStr) | ||
| if err != nil { | ||
| return time.Now(), err | ||
| } | ||
|
|
||
| when := commitObject.Committer.When | ||
| timestamp, err := strconv.ParseInt(dateStr, 10, 64) | ||
| if err != nil { | ||
| return time.Now(), err | ||
| } | ||
|
|
||
| return when, nil | ||
| return time.Unix(timestamp, 0), 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 |
|---|---|---|
| @@ -1,21 +1,22 @@ | ||
| package git | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestCommit(t *testing.T) { | ||
| repo := setupRepo() | ||
| createTestHistory(repo) | ||
| tmpDir, testGit := setupRepo(t) | ||
| defer os.RemoveAll(tmpDir) | ||
|
|
||
| testGit := &Git{repo: repo} | ||
| createTestHistory(t, testGit) | ||
|
|
||
| head, _ := testGit.CurrentCommit() | ||
|
|
||
| commit, err := testGit.Commit(head.Hash) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, "third commit on new branch", commit.Message) | ||
| assert.Equal(t, "third commit on new branch\n", commit.Message) | ||
| assert.NotEmpty(t, commit.Hash) | ||
| } |
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 |
|---|---|---|
| @@ -1,40 +1,68 @@ | ||
| package git | ||
|
|
||
| import ( | ||
| "errors" | ||
|
|
||
| "github.com/go-git/go-git/v5" | ||
| "github.com/go-git/go-git/v5/plumbing" | ||
| "github.com/go-git/go-git/v5/plumbing/object" | ||
| ) | ||
|
|
||
| var ( | ||
| errReachedToCommit = errors.New("reached to commit") | ||
| "strings" | ||
| ) | ||
|
|
||
| // CommitsBetween returns a slice of commit hashes between two commits | ||
| func (g *Git) CommitsBetween(from plumbing.Hash, to plumbing.Hash) ([]plumbing.Hash, error) { | ||
| cIter, _ := g.repo.Log(&git.LogOptions{From: from}) | ||
| func (g *Git) CommitsBetween(from Hash, to Hash) ([]Hash, error) { | ||
| // If from and to are equal, return empty slice | ||
| if from == to { | ||
| return []Hash{}, nil | ||
| } | ||
|
|
||
| var commits []plumbing.Hash | ||
| fromStr := from.String() | ||
| toStr := to.String() | ||
|
|
||
| err := cIter.ForEach(func(c *object.Commit) error { | ||
| // If no previous tag is found then from and to are equal | ||
| if from == to { | ||
| return nil | ||
| // Check if 'to' is an empty hash (all zeros) | ||
| var emptyHash Hash | ||
| if to == emptyHash { | ||
| // If 'to' is empty, return all commits from 'from' | ||
| output, err := g.runGitCommand("log", "--format=%H", fromStr) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if c.Hash == to { | ||
| return errReachedToCommit | ||
| lines := strings.Split(output, "\n") | ||
| var commits []Hash | ||
| for _, line := range lines { | ||
| if line == "" { | ||
| continue | ||
| } | ||
| hash, err := NewHash(line) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| commits = append(commits, hash) | ||
| } | ||
| commits = append(commits, c.Hash) | ||
| return nil | ||
| }) | ||
|
|
||
| if err == errReachedToCommit { | ||
| return commits, nil | ||
| } | ||
|
|
||
| // Get commits from 'from' to 'to' (excluding 'to') | ||
| // Use ^to to exclude the 'to' commit | ||
| output, err := g.runGitCommand("log", "--format=%H", fromStr, "^"+toStr) | ||
| if err != nil { | ||
| return commits, err | ||
| // If the command fails, it might be because there are no commits between | ||
| // Try to check if 'to' is reachable from 'from' | ||
| _, err2 := g.runGitCommand("merge-base", "--is-ancestor", toStr, fromStr) | ||
| if err2 != nil { | ||
| return []Hash{}, nil | ||
| } | ||
| return nil, err | ||
| } | ||
|
|
||
| lines := strings.Split(output, "\n") | ||
| var commits []Hash | ||
|
|
||
| for _, line := range lines { | ||
| if line == "" { | ||
| continue | ||
| } | ||
| hash, err := NewHash(line) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| commits = append(commits, hash) | ||
| } | ||
|
|
||
| return commits, nil | ||
| } |
Oops, something went wrong.
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.
Multiple separate
git logcalls are used to retrieve the commit message, author, and committer details. Consider consolidating these calls into a single command to reduce overhead and improve performance.