2020-09-29 11:22:26 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2021-10-18 15:44:01 +02:00
|
|
|
"strings"
|
2020-09-29 11:22:26 +02:00
|
|
|
|
2020-09-29 12:28:39 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2020-09-29 11:22:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetReflogCommits only returns the new reflog commits since the given lastReflogCommit
|
|
|
|
// if none is passed (i.e. it's value is nil) then we get all the reflog commits
|
|
|
|
func (c *GitCommand) GetReflogCommits(lastReflogCommit *models.Commit, filterPath string) ([]*models.Commit, bool, error) {
|
|
|
|
commits := make([]*models.Commit, 0)
|
|
|
|
|
|
|
|
filterPathArg := ""
|
|
|
|
if filterPath != "" {
|
|
|
|
filterPathArg = fmt.Sprintf(" --follow -- %s", c.OSCommand.Quote(filterPath))
|
|
|
|
}
|
|
|
|
|
2021-12-07 12:59:36 +02:00
|
|
|
cmdObj := c.OSCommand.NewCmdObj(fmt.Sprintf(`git log -g --abbrev=20 --format="%%h %%ct %%gs" %s`, filterPathArg))
|
2020-09-29 11:22:26 +02:00
|
|
|
onlyObtainedNewReflogCommits := false
|
2021-12-07 12:59:36 +02:00
|
|
|
err := c.OSCommand.RunLineOutputCmd(cmdObj, func(line string) (bool, error) {
|
2021-10-18 15:44:01 +02:00
|
|
|
fields := strings.SplitN(line, " ", 3)
|
|
|
|
if len(fields) <= 2 {
|
2020-09-29 11:22:26 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-10-18 15:44:01 +02:00
|
|
|
unixTimestamp, _ := strconv.Atoi(fields[1])
|
2020-09-29 11:22:26 +02:00
|
|
|
|
|
|
|
commit := &models.Commit{
|
2021-10-18 15:44:01 +02:00
|
|
|
Sha: fields[0],
|
|
|
|
Name: fields[2],
|
2020-09-29 11:22:26 +02:00
|
|
|
UnixTimestamp: int64(unixTimestamp),
|
|
|
|
Status: "reflog",
|
|
|
|
}
|
|
|
|
|
|
|
|
if lastReflogCommit != nil && commit.Sha == lastReflogCommit.Sha && commit.UnixTimestamp == lastReflogCommit.UnixTimestamp {
|
|
|
|
onlyObtainedNewReflogCommits = true
|
|
|
|
// after this point we already have these reflogs loaded so we'll simply return the new ones
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
commits = append(commits, commit)
|
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return commits, onlyObtainedNewReflogCommits, nil
|
|
|
|
}
|