diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2019-03-02 20:00:26 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2019-03-02 20:00:26 +1100 |
commit | 0c886eddfb429f31352d0908fe993d415d7d3709 (patch) | |
tree | 00ded3a52ba73d1d200af9dd6cf5c9fdfd6ef3a2 /pkg/git/commit_list_builder.go | |
parent | 399346c2eef5d4e40e9c3b5252addd3dbb5b3598 (diff) |
Revert "remove old rebase code now that we're only ever interactively rebasing"
This reverts commit 1a19b1412d3da03992403cf62fddf06031de2927.
Diffstat (limited to 'pkg/git/commit_list_builder.go')
-rw-r--r-- | pkg/git/commit_list_builder.go | 84 |
1 files changed, 78 insertions, 6 deletions
diff --git a/pkg/git/commit_list_builder.go b/pkg/git/commit_list_builder.go index 08a10fafc..4f3268195 100644 --- a/pkg/git/commit_list_builder.go +++ b/pkg/git/commit_list_builder.go @@ -3,6 +3,9 @@ package git import ( "fmt" "io/ioutil" + "os" + "path/filepath" + "regexp" "strings" "github.com/fatih/color" @@ -45,13 +48,13 @@ func NewCommitListBuilder(log *logrus.Entry, gitCommand *commands.GitCommand, os func (c *CommitListBuilder) GetCommits() ([]*commands.Commit, error) { commits := []*commands.Commit{} var rebasingCommits []*commands.Commit - isRebasing, err := c.GitCommand.IsInRebasingState() + rebaseMode, err := c.GitCommand.RebaseMode() if err != nil { return nil, err } - if isRebasing { + if rebaseMode != "" { // here we want to also prepend the commits that we're in the process of rebasing - rebasingCommits, err = c.getRebasingCommits() + rebasingCommits, err = c.getRebasingCommits(rebaseMode) if err != nil { return nil, err } @@ -76,7 +79,7 @@ func (c *CommitListBuilder) GetCommits() ([]*commands.Commit, error) { DisplayString: strings.Join(splitLine, " "), }) } - if isRebasing { + if rebaseMode != "" { currentCommit := commits[len(rebasingCommits)] blue := color.New(color.FgYellow) youAreHere := blue.Sprintf("<-- %s ---", c.Tr.SLocalize("YouAreHere")) @@ -96,6 +99,59 @@ func (c *CommitListBuilder) GetCommits() ([]*commands.Commit, error) { return commits, nil } +// getRebasingCommits obtains the commits that we're in the process of rebasing +func (c *CommitListBuilder) getRebasingCommits(rebaseMode string) ([]*commands.Commit, error) { + switch rebaseMode { + case "normal": + return c.getNormalRebasingCommits() + case "interactive": + return c.getInteractiveRebasingCommits() + default: + return nil, nil + } +} + +func (c *CommitListBuilder) getNormalRebasingCommits() ([]*commands.Commit, error) { + rewrittenCount := 0 + bytesContent, err := ioutil.ReadFile(".git/rebase-apply/rewritten") + if err == nil { + content := string(bytesContent) + rewrittenCount = len(strings.Split(content, "\n")) + } + + // we know we're rebasing, so lets get all the files whose names have numbers + commits := []*commands.Commit{} + err = filepath.Walk(".git/rebase-apply", func(path string, f os.FileInfo, err error) error { + if rewrittenCount > 0 { + rewrittenCount-- + return nil + } + if err != nil { + return err + } + re := regexp.MustCompile(`^\d+$`) + if !re.MatchString(f.Name()) { + return nil + } + bytesContent, err := ioutil.ReadFile(path) + if err != nil { + return err + } + content := string(bytesContent) + commit, err := c.commitFromPatch(content) + if err != nil { + return err + } + commits = append([]*commands.Commit{commit}, commits...) + return nil + }) + if err != nil { + return nil, err + } + + return commits, nil +} + // git-rebase-todo example: // pick ac446ae94ee560bdb8d1d057278657b251aaef17 ac446ae // pick afb893148791a2fbd8091aeb81deba4930c73031 afb8931 @@ -105,10 +161,10 @@ func (c *CommitListBuilder) GetCommits() ([]*commands.Commit, error) { // pick ac446ae94ee560bdb8d1d057278657b251aaef17 blah commit on master // pick afb893148791a2fbd8091aeb81deba4930c73031 fourth commit on master -// getRebasingCommits takes our git-rebase-todo and our git-rebase-todo.backup files +// getInteractiveRebasingCommits takes our git-rebase-todo and our git-rebase-todo.backup files // and extracts out the sha and names of commits that we still have to go // in the rebase: -func (c *CommitListBuilder) getRebasingCommits() ([]*commands.Commit, error) { +func (c *CommitListBuilder) getInteractiveRebasingCommits() ([]*commands.Commit, error) { bytesContent, err := ioutil.ReadFile(".git/rebase-merge/git-rebase-todo") if err != nil { c.Log.Info(fmt.Sprintf("error occured reading git-rebase-todo: %s", err.Error())) @@ -134,6 +190,22 @@ func (c *CommitListBuilder) getRebasingCommits() ([]*commands.Commit, error) { return nil, nil } +// assuming the file starts like this: +// From e93d4193e6dd45ca9cf3a5a273d7ba6cd8b8fb20 Mon Sep 17 00:00:00 2001 +// From: Lazygit Tester <test@example.com> +// Date: Wed, 5 Dec 2018 21:03:23 +1100 +// Subject: second commit on master +func (c *CommitListBuilder) commitFromPatch(content string) (*commands.Commit, error) { + lines := strings.Split(content, "\n") + sha := strings.Split(lines[0], " ")[1][0:7] + name := strings.TrimPrefix(lines[3], "Subject: ") + return &commands.Commit{ + Sha: sha, + Name: name, + Status: "rebasing", + }, nil +} + func (c *CommitListBuilder) setCommitMergedStatuses(commits []*commands.Commit) ([]*commands.Commit, error) { ancestor, err := c.getMergeBase() if err != nil { |