1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-07 01:09:45 +02:00

add Commit.ParentRefName()

This commit is contained in:
Ryooooooga
2022-03-26 22:03:32 +09:00
committed by Jesse Duffield
parent 99ecc1cfdf
commit 30be50b641
4 changed files with 49 additions and 14 deletions

View File

@ -178,6 +178,11 @@ func (self *CommitLoader) extractCommitFromLine(line string) *models.Commit {
unitTimestampInt, _ := strconv.Atoi(unixTimestamp) unitTimestampInt, _ := strconv.Atoi(unixTimestamp)
parents := []string{}
if len(parentHashes) > 0 {
parents = strings.Split(parentHashes, " ")
}
return &models.Commit{ return &models.Commit{
Sha: sha, Sha: sha,
Name: message, Name: message,
@ -185,7 +190,7 @@ func (self *CommitLoader) extractCommitFromLine(line string) *models.Commit {
ExtraInfo: extraInfo, ExtraInfo: extraInfo,
UnixTimestamp: int64(unitTimestampInt), UnixTimestamp: int64(unitTimestampInt),
Author: author, Author: author,
Parents: strings.Split(parentHashes, " "), Parents: parents,
} }
} }

View File

@ -32,21 +32,28 @@ func (self *ReflogCommitLoader) GetReflogCommits(lastReflogCommit *models.Commit
filterPathArg = fmt.Sprintf(" --follow -- %s", self.cmd.Quote(filterPath)) filterPathArg = fmt.Sprintf(" --follow -- %s", self.cmd.Quote(filterPath))
} }
cmdObj := self.cmd.New(fmt.Sprintf(`git log -g --abbrev=40 --format="%s"%s`, "%h%x00%ct%x00%gs", filterPathArg)).DontLog() cmdObj := self.cmd.New(fmt.Sprintf(`git log -g --abbrev=40 --format="%s"%s`, "%h%x00%ct%x00%gs%x00%p", filterPathArg)).DontLog()
onlyObtainedNewReflogCommits := false onlyObtainedNewReflogCommits := false
err := cmdObj.RunAndProcessLines(func(line string) (bool, error) { err := cmdObj.RunAndProcessLines(func(line string) (bool, error) {
fields := strings.SplitN(line, "\x00", 3) fields := strings.SplitN(line, "\x00", 4)
if len(fields) <= 2 { if len(fields) <= 3 {
return false, nil return false, nil
} }
unixTimestamp, _ := strconv.Atoi(fields[1]) unixTimestamp, _ := strconv.Atoi(fields[1])
parentHashes := fields[3]
parents := []string{}
if len(parentHashes) > 0 {
parents = strings.Split(parentHashes, " ")
}
commit := &models.Commit{ commit := &models.Commit{
Sha: fields[0], Sha: fields[0],
Name: fields[2], Name: fields[2],
UnixTimestamp: int64(unixTimestamp), UnixTimestamp: int64(unixTimestamp),
Status: "reflog", Status: "reflog",
Parents: parents,
} }
// note that the unix timestamp here is the timestamp of the COMMIT, not the reflog entry itself, // note that the unix timestamp here is the timestamp of the COMMIT, not the reflog entry itself,

View File

@ -12,11 +12,11 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
var reflogOutput = strings.Replace(`c3c4b66b64c97ffeecde|1643150483|checkout: moving from A to B var reflogOutput = strings.Replace(`c3c4b66b64c97ffeecde|1643150483|checkout: moving from A to B|51baa8c1
c3c4b66b64c97ffeecde|1643150483|checkout: moving from B to A c3c4b66b64c97ffeecde|1643150483|checkout: moving from B to A|51baa8c1
c3c4b66b64c97ffeecde|1643150483|checkout: moving from A to B c3c4b66b64c97ffeecde|1643150483|checkout: moving from A to B|51baa8c1
c3c4b66b64c97ffeecde|1643150483|checkout: moving from master to A c3c4b66b64c97ffeecde|1643150483|checkout: moving from master to A|51baa8c1
f4ddf2f0d4be4ccc7efa|1643149435|checkout: moving from A to master f4ddf2f0d4be4ccc7efa|1643149435|checkout: moving from A to master|51baa8c1
`, "|", "\x00", -1) `, "|", "\x00", -1)
func TestGetReflogCommits(t *testing.T) { func TestGetReflogCommits(t *testing.T) {
@ -34,7 +34,7 @@ func TestGetReflogCommits(t *testing.T) {
{ {
testName: "no reflog entries", testName: "no reflog entries",
runner: oscommands.NewFakeRunner(t). runner: oscommands.NewFakeRunner(t).
Expect(`git log -g --abbrev=40 --format="%h%x00%ct%x00%gs"`, "", nil), Expect(`git log -g --abbrev=40 --format="%h%x00%ct%x00%gs%x00%p"`, "", nil),
lastReflogCommit: nil, lastReflogCommit: nil,
expectedCommits: []*models.Commit{}, expectedCommits: []*models.Commit{},
@ -44,7 +44,7 @@ func TestGetReflogCommits(t *testing.T) {
{ {
testName: "some reflog entries", testName: "some reflog entries",
runner: oscommands.NewFakeRunner(t). runner: oscommands.NewFakeRunner(t).
Expect(`git log -g --abbrev=40 --format="%h%x00%ct%x00%gs"`, reflogOutput, nil), Expect(`git log -g --abbrev=40 --format="%h%x00%ct%x00%gs%x00%p"`, reflogOutput, nil),
lastReflogCommit: nil, lastReflogCommit: nil,
expectedCommits: []*models.Commit{ expectedCommits: []*models.Commit{
@ -53,30 +53,35 @@ func TestGetReflogCommits(t *testing.T) {
Name: "checkout: moving from A to B", Name: "checkout: moving from A to B",
Status: "reflog", Status: "reflog",
UnixTimestamp: 1643150483, UnixTimestamp: 1643150483,
Parents: []string{"51baa8c1"},
}, },
{ {
Sha: "c3c4b66b64c97ffeecde", Sha: "c3c4b66b64c97ffeecde",
Name: "checkout: moving from B to A", Name: "checkout: moving from B to A",
Status: "reflog", Status: "reflog",
UnixTimestamp: 1643150483, UnixTimestamp: 1643150483,
Parents: []string{"51baa8c1"},
}, },
{ {
Sha: "c3c4b66b64c97ffeecde", Sha: "c3c4b66b64c97ffeecde",
Name: "checkout: moving from A to B", Name: "checkout: moving from A to B",
Status: "reflog", Status: "reflog",
UnixTimestamp: 1643150483, UnixTimestamp: 1643150483,
Parents: []string{"51baa8c1"},
}, },
{ {
Sha: "c3c4b66b64c97ffeecde", Sha: "c3c4b66b64c97ffeecde",
Name: "checkout: moving from master to A", Name: "checkout: moving from master to A",
Status: "reflog", Status: "reflog",
UnixTimestamp: 1643150483, UnixTimestamp: 1643150483,
Parents: []string{"51baa8c1"},
}, },
{ {
Sha: "f4ddf2f0d4be4ccc7efa", Sha: "f4ddf2f0d4be4ccc7efa",
Name: "checkout: moving from A to master", Name: "checkout: moving from A to master",
Status: "reflog", Status: "reflog",
UnixTimestamp: 1643149435, UnixTimestamp: 1643149435,
Parents: []string{"51baa8c1"},
}, },
}, },
expectedOnlyObtainedNew: false, expectedOnlyObtainedNew: false,
@ -85,13 +90,14 @@ func TestGetReflogCommits(t *testing.T) {
{ {
testName: "some reflog entries where last commit is given", testName: "some reflog entries where last commit is given",
runner: oscommands.NewFakeRunner(t). runner: oscommands.NewFakeRunner(t).
Expect(`git log -g --abbrev=40 --format="%h%x00%ct%x00%gs"`, reflogOutput, nil), Expect(`git log -g --abbrev=40 --format="%h%x00%ct%x00%gs%x00%p"`, reflogOutput, nil),
lastReflogCommit: &models.Commit{ lastReflogCommit: &models.Commit{
Sha: "c3c4b66b64c97ffeecde", Sha: "c3c4b66b64c97ffeecde",
Name: "checkout: moving from B to A", Name: "checkout: moving from B to A",
Status: "reflog", Status: "reflog",
UnixTimestamp: 1643150483, UnixTimestamp: 1643150483,
Parents: []string{"51baa8c1"},
}, },
expectedCommits: []*models.Commit{ expectedCommits: []*models.Commit{
{ {
@ -99,6 +105,7 @@ func TestGetReflogCommits(t *testing.T) {
Name: "checkout: moving from A to B", Name: "checkout: moving from A to B",
Status: "reflog", Status: "reflog",
UnixTimestamp: 1643150483, UnixTimestamp: 1643150483,
Parents: []string{"51baa8c1"},
}, },
}, },
expectedOnlyObtainedNew: true, expectedOnlyObtainedNew: true,
@ -107,13 +114,14 @@ func TestGetReflogCommits(t *testing.T) {
{ {
testName: "when passing filterPath", testName: "when passing filterPath",
runner: oscommands.NewFakeRunner(t). runner: oscommands.NewFakeRunner(t).
Expect(`git log -g --abbrev=40 --format="%h%x00%ct%x00%gs" --follow -- "path"`, reflogOutput, nil), Expect(`git log -g --abbrev=40 --format="%h%x00%ct%x00%gs%x00%p" --follow -- "path"`, reflogOutput, nil),
lastReflogCommit: &models.Commit{ lastReflogCommit: &models.Commit{
Sha: "c3c4b66b64c97ffeecde", Sha: "c3c4b66b64c97ffeecde",
Name: "checkout: moving from B to A", Name: "checkout: moving from B to A",
Status: "reflog", Status: "reflog",
UnixTimestamp: 1643150483, UnixTimestamp: 1643150483,
Parents: []string{"51baa8c1"},
}, },
filterPath: "path", filterPath: "path",
expectedCommits: []*models.Commit{ expectedCommits: []*models.Commit{
@ -122,6 +130,7 @@ func TestGetReflogCommits(t *testing.T) {
Name: "checkout: moving from A to B", Name: "checkout: moving from A to B",
Status: "reflog", Status: "reflog",
UnixTimestamp: 1643150483, UnixTimestamp: 1643150483,
Parents: []string{"51baa8c1"},
}, },
}, },
expectedOnlyObtainedNew: true, expectedOnlyObtainedNew: true,
@ -130,7 +139,7 @@ func TestGetReflogCommits(t *testing.T) {
{ {
testName: "when command returns error", testName: "when command returns error",
runner: oscommands.NewFakeRunner(t). runner: oscommands.NewFakeRunner(t).
Expect(`git log -g --abbrev=40 --format="%h%x00%ct%x00%gs"`, "", errors.New("haha")), Expect(`git log -g --abbrev=40 --format="%h%x00%ct%x00%gs%x00%p"`, "", errors.New("haha")),
lastReflogCommit: nil, lastReflogCommit: nil,
filterPath: "", filterPath: "",

View File

@ -6,6 +6,9 @@ import (
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
) )
// Special commit hash for empty tree object
const EmptyTreeCommitHash = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
// Commit : A git commit // Commit : A git commit
type Commit struct { type Commit struct {
Sha string Sha string
@ -29,6 +32,17 @@ func (c *Commit) RefName() string {
return c.Sha return c.Sha
} }
func (c *Commit) ParentRefName() string {
if c.IsFirstCommit() {
return EmptyTreeCommitHash
}
return c.RefName() + "^"
}
func (c *Commit) IsFirstCommit() bool {
return len(c.Parents) == 0
}
func (c *Commit) ID() string { func (c *Commit) ID() string {
return c.RefName() return c.RefName()
} }