diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-09-29 19:22:26 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-09-29 20:48:49 +1000 |
commit | 1767f91047a35318f6b1e469199c8a7f547f2afc (patch) | |
tree | fbbd83d25289676d305eaabc1e354a8f68a834a4 /pkg/commands/loading_reflog_commits.go | |
parent | 1759ddf2470389d8de7ccedad24caf66c3cdb7d5 (diff) |
factor out code for loading models
Diffstat (limited to 'pkg/commands/loading_reflog_commits.go')
-rw-r--r-- | pkg/commands/loading_reflog_commits.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/pkg/commands/loading_reflog_commits.go b/pkg/commands/loading_reflog_commits.go new file mode 100644 index 000000000..5950349fc --- /dev/null +++ b/pkg/commands/loading_reflog_commits.go @@ -0,0 +1,54 @@ +package commands + +import ( + "fmt" + "regexp" + "strconv" + + "github.com/jesseduffield/lazygit/pkg/commands/oscommands" + "github.com/jesseduffield/lazygit/pkg/models" +) + +// 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) + re := regexp.MustCompile(`(\w+).*HEAD@\{([^\}]+)\}: (.*)`) + + filterPathArg := "" + if filterPath != "" { + filterPathArg = fmt.Sprintf(" --follow -- %s", c.OSCommand.Quote(filterPath)) + } + + cmd := c.OSCommand.ExecutableFromString(fmt.Sprintf("git reflog --abbrev=20 --date=unix %s", filterPathArg)) + onlyObtainedNewReflogCommits := false + err := oscommands.RunLineOutputCmd(cmd, func(line string) (bool, error) { + match := re.FindStringSubmatch(line) + if len(match) <= 1 { + return false, nil + } + + unixTimestamp, _ := strconv.Atoi(match[2]) + + commit := &models.Commit{ + Sha: match[1], + Name: match[3], + 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 +} |