2021-12-30 04:35:10 +02:00
|
|
|
package loaders
|
2019-02-19 00:18:30 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-12-30 04:44:41 +02:00
|
|
|
"io/ioutil"
|
2019-03-02 11:00:26 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2020-03-27 10:12:15 +02:00
|
|
|
"strconv"
|
2019-02-19 00:18:30 +02:00
|
|
|
"strings"
|
|
|
|
|
2020-09-29 12:28:39 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2020-09-29 11:10:57 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
2021-12-30 04:35:10 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
|
2021-12-29 05:33:38 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/common"
|
2021-07-27 15:00:37 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/style"
|
2019-02-19 00:18:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// context:
|
|
|
|
// here we get the commits from git log but format them to show whether they're
|
|
|
|
// unpushed/pushed/merged into the base branch or not, or if they're yet to
|
|
|
|
// be processed as part of a rebase (these won't appear in git log but we
|
|
|
|
// grab them from the rebase-related files in the .git directory to show them
|
|
|
|
|
2020-02-25 11:11:07 +02:00
|
|
|
const SEPARATION_CHAR = "|"
|
|
|
|
|
2021-12-30 04:35:10 +02:00
|
|
|
// CommitLoader returns a list of Commit objects for the current repo
|
|
|
|
type CommitLoader struct {
|
2021-12-29 05:33:38 +02:00
|
|
|
*common.Common
|
|
|
|
cmd oscommands.ICmdObjBuilder
|
|
|
|
|
|
|
|
getCurrentBranchName func() (string, string, error)
|
2021-12-30 04:35:10 +02:00
|
|
|
getRebaseMode func() (enums.RebaseMode, error)
|
2021-12-29 05:33:38 +02:00
|
|
|
readFile func(filename string) ([]byte, error)
|
2021-12-30 02:22:29 +02:00
|
|
|
walkFiles func(root string, fn filepath.WalkFunc) error
|
2021-12-29 05:33:38 +02:00
|
|
|
dotGitDir string
|
2019-02-19 00:18:30 +02:00
|
|
|
}
|
|
|
|
|
2021-12-30 04:44:41 +02:00
|
|
|
// making our dependencies explicit for the sake of easier testing
|
2021-12-30 04:35:10 +02:00
|
|
|
func NewCommitLoader(
|
2021-12-30 04:44:41 +02:00
|
|
|
cmn *common.Common,
|
2022-01-02 01:34:33 +02:00
|
|
|
cmd oscommands.ICmdObjBuilder,
|
|
|
|
dotGitDir string,
|
|
|
|
getCurrentBranchName func() (string, string, error),
|
|
|
|
getRebaseMode func() (enums.RebaseMode, error),
|
2021-12-30 04:35:10 +02:00
|
|
|
) *CommitLoader {
|
|
|
|
return &CommitLoader{
|
2021-12-30 04:44:41 +02:00
|
|
|
Common: cmn,
|
2022-01-02 01:34:33 +02:00
|
|
|
cmd: cmd,
|
|
|
|
getCurrentBranchName: getCurrentBranchName,
|
|
|
|
getRebaseMode: getRebaseMode,
|
2021-12-30 04:44:41 +02:00
|
|
|
readFile: ioutil.ReadFile,
|
|
|
|
walkFiles: filepath.Walk,
|
2022-01-02 01:34:33 +02:00
|
|
|
dotGitDir: dotGitDir,
|
2020-08-22 00:49:02 +02:00
|
|
|
}
|
2019-02-19 00:18:30 +02:00
|
|
|
}
|
|
|
|
|
2020-03-28 07:28:35 +02:00
|
|
|
type GetCommitsOptions struct {
|
2020-08-22 00:49:02 +02:00
|
|
|
Limit bool
|
|
|
|
FilterPath string
|
|
|
|
IncludeRebaseCommits bool
|
|
|
|
RefName string // e.g. "HEAD" or "my_branch"
|
2021-11-02 12:16:00 +02:00
|
|
|
// determines if we show the whole git graph i.e. pass the '--all' flag
|
|
|
|
All bool
|
2020-03-28 07:28:35 +02:00
|
|
|
}
|
|
|
|
|
2019-02-19 00:18:30 +02:00
|
|
|
// GetCommits obtains the commits of the current branch
|
2021-12-30 04:35:10 +02:00
|
|
|
func (self *CommitLoader) GetCommits(opts GetCommitsOptions) ([]*models.Commit, error) {
|
2020-09-29 10:36:54 +02:00
|
|
|
commits := []*models.Commit{}
|
|
|
|
var rebasingCommits []*models.Commit
|
2021-12-30 02:22:29 +02:00
|
|
|
rebaseMode, err := self.getRebaseMode()
|
2020-08-27 12:50:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-22 00:49:02 +02:00
|
|
|
|
2020-08-27 12:50:30 +02:00
|
|
|
if opts.IncludeRebaseCommits && opts.FilterPath == "" {
|
2020-08-22 00:49:02 +02:00
|
|
|
var err error
|
2021-12-30 02:22:29 +02:00
|
|
|
rebasingCommits, err = self.MergeRebasingCommits(commits)
|
2019-02-19 14:36:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-27 12:50:30 +02:00
|
|
|
commits = append(commits, rebasingCommits...)
|
2019-02-19 00:18:30 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 09:05:07 +02:00
|
|
|
passedFirstPushedCommit := false
|
2021-12-30 02:22:29 +02:00
|
|
|
firstPushedCommit, err := self.getFirstPushedCommit(opts.RefName)
|
2020-08-27 00:42:42 +02:00
|
|
|
if err != nil {
|
2020-08-27 09:05:07 +02:00
|
|
|
// must have no upstream branch so we'll consider everything as pushed
|
|
|
|
passedFirstPushedCommit = true
|
2020-08-27 00:42:42 +02:00
|
|
|
}
|
|
|
|
|
2021-12-30 02:22:29 +02:00
|
|
|
err = self.getLogCmd(opts).RunAndProcessLines(func(line string) (bool, error) {
|
2021-10-30 08:42:52 +02:00
|
|
|
if canExtractCommit(line) {
|
2021-12-30 02:22:29 +02:00
|
|
|
commit := self.extractCommitFromLine(line)
|
2020-08-27 00:42:42 +02:00
|
|
|
if commit.Sha == firstPushedCommit {
|
|
|
|
passedFirstPushedCommit = true
|
|
|
|
}
|
|
|
|
commit.Status = map[bool]string{true: "unpushed", false: "pushed"}[!passedFirstPushedCommit]
|
2020-04-06 19:26:12 +02:00
|
|
|
commits = append(commits, commit)
|
|
|
|
}
|
2020-03-26 12:11:21 +02:00
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-02-19 00:18:30 +02:00
|
|
|
}
|
2020-03-26 12:11:21 +02:00
|
|
|
|
2021-12-30 04:11:58 +02:00
|
|
|
if len(commits) == 0 {
|
|
|
|
return commits, nil
|
|
|
|
}
|
|
|
|
|
2021-12-30 04:35:10 +02:00
|
|
|
if rebaseMode != enums.REBASE_MODE_NONE {
|
2019-02-19 00:18:30 +02:00
|
|
|
currentCommit := commits[len(rebasingCommits)]
|
2021-12-30 02:22:29 +02:00
|
|
|
youAreHere := style.FgYellow.Sprintf("<-- %s ---", self.Tr.YouAreHere)
|
2019-02-19 00:18:30 +02:00
|
|
|
currentCommit.Name = fmt.Sprintf("%s %s", youAreHere, currentCommit.Name)
|
|
|
|
}
|
2019-02-24 04:51:52 +02:00
|
|
|
|
2021-12-30 02:22:29 +02:00
|
|
|
commits, err = self.setCommitMergedStatuses(opts.RefName, commits)
|
2019-02-24 04:51:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return commits, nil
|
2019-02-19 00:18:30 +02:00
|
|
|
}
|
|
|
|
|
2021-12-30 04:35:10 +02:00
|
|
|
func (self *CommitLoader) MergeRebasingCommits(commits []*models.Commit) ([]*models.Commit, error) {
|
2021-12-30 04:11:58 +02:00
|
|
|
// chances are we have as many commits as last time so we'll set the capacity to be the old length
|
|
|
|
result := make([]*models.Commit, 0, len(commits))
|
|
|
|
for i, commit := range commits {
|
|
|
|
if commit.Status != "rebasing" { // removing the existing rebase commits so we can add the refreshed ones
|
|
|
|
result = append(result, commits[i:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rebaseMode, err := self.getRebaseMode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-30 04:35:10 +02:00
|
|
|
if rebaseMode == enums.REBASE_MODE_NONE {
|
2021-12-30 04:11:58 +02:00
|
|
|
// not in rebase mode so return original commits
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rebasingCommits, err := self.getHydratedRebasingCommits(rebaseMode)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(rebasingCommits) > 0 {
|
|
|
|
result = append(rebasingCommits, result...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractCommitFromLine takes a line from a git log and extracts the sha, message, date, and tag if present
|
|
|
|
// then puts them into a commit object
|
|
|
|
// example input:
|
|
|
|
// 8ad01fe32fcc20f07bc6693f87aa4977c327f1e1|10 hours ago|Jesse Duffield| (HEAD -> master, tag: v0.15.2)|refresh commits when adding a tag
|
2021-12-30 04:35:10 +02:00
|
|
|
func (self *CommitLoader) extractCommitFromLine(line string) *models.Commit {
|
2021-12-30 04:11:58 +02:00
|
|
|
split := strings.Split(line, SEPARATION_CHAR)
|
|
|
|
|
|
|
|
sha := split[0]
|
|
|
|
unixTimestamp := split[1]
|
|
|
|
author := split[2]
|
|
|
|
extraInfo := strings.TrimSpace(split[3])
|
|
|
|
parentHashes := split[4]
|
|
|
|
|
|
|
|
message := strings.Join(split[5:], SEPARATION_CHAR)
|
|
|
|
tags := []string{}
|
|
|
|
|
|
|
|
if extraInfo != "" {
|
|
|
|
re := regexp.MustCompile(`tag: ([^,\)]+)`)
|
|
|
|
tagMatch := re.FindStringSubmatch(extraInfo)
|
|
|
|
if len(tagMatch) > 1 {
|
|
|
|
tags = append(tags, tagMatch[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unitTimestampInt, _ := strconv.Atoi(unixTimestamp)
|
|
|
|
|
|
|
|
return &models.Commit{
|
|
|
|
Sha: sha,
|
|
|
|
Name: message,
|
|
|
|
Tags: tags,
|
|
|
|
ExtraInfo: extraInfo,
|
|
|
|
UnixTimestamp: int64(unitTimestampInt),
|
|
|
|
Author: author,
|
|
|
|
Parents: strings.Split(parentHashes, " "),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-30 04:35:10 +02:00
|
|
|
func (self *CommitLoader) getHydratedRebasingCommits(rebaseMode enums.RebaseMode) ([]*models.Commit, error) {
|
2021-12-30 02:22:29 +02:00
|
|
|
commits, err := self.getRebasingCommits(rebaseMode)
|
2021-10-30 08:42:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-30 09:06:39 +02:00
|
|
|
if len(commits) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-10-30 08:42:52 +02:00
|
|
|
commitShas := make([]string, len(commits))
|
|
|
|
for i, commit := range commits {
|
|
|
|
commitShas[i] = commit.Sha
|
|
|
|
}
|
|
|
|
|
|
|
|
// note that we're not filtering these as we do non-rebasing commits just because
|
|
|
|
// I suspect that will cause some damage
|
2021-12-30 02:22:29 +02:00
|
|
|
cmdObj := self.cmd.New(
|
2021-10-30 08:42:52 +02:00
|
|
|
fmt.Sprintf(
|
|
|
|
"git show %s --no-patch --oneline %s --abbrev=%d",
|
|
|
|
strings.Join(commitShas, " "),
|
|
|
|
prettyFormat,
|
|
|
|
20,
|
|
|
|
),
|
2022-01-05 02:57:32 +02:00
|
|
|
).DontLog()
|
2021-10-30 08:42:52 +02:00
|
|
|
|
|
|
|
hydratedCommits := make([]*models.Commit, 0, len(commits))
|
|
|
|
i := 0
|
2021-12-30 02:22:29 +02:00
|
|
|
err = cmdObj.RunAndProcessLines(func(line string) (bool, error) {
|
2021-10-30 08:42:52 +02:00
|
|
|
if canExtractCommit(line) {
|
2021-12-30 02:22:29 +02:00
|
|
|
commit := self.extractCommitFromLine(line)
|
2021-10-30 09:06:39 +02:00
|
|
|
matchingCommit := commits[i]
|
|
|
|
commit.Action = matchingCommit.Action
|
|
|
|
commit.Status = matchingCommit.Status
|
2021-10-30 08:42:52 +02:00
|
|
|
hydratedCommits = append(hydratedCommits, commit)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return hydratedCommits, nil
|
|
|
|
}
|
|
|
|
|
2019-03-02 11:00:26 +02:00
|
|
|
// getRebasingCommits obtains the commits that we're in the process of rebasing
|
2021-12-30 04:35:10 +02:00
|
|
|
func (self *CommitLoader) getRebasingCommits(rebaseMode enums.RebaseMode) ([]*models.Commit, error) {
|
2019-03-02 11:00:26 +02:00
|
|
|
switch rebaseMode {
|
2021-12-30 04:35:10 +02:00
|
|
|
case enums.REBASE_MODE_MERGING:
|
2021-12-30 02:22:29 +02:00
|
|
|
return self.getNormalRebasingCommits()
|
2021-12-30 04:35:10 +02:00
|
|
|
case enums.REBASE_MODE_INTERACTIVE:
|
2021-12-30 02:22:29 +02:00
|
|
|
return self.getInteractiveRebasingCommits()
|
2019-03-02 11:00:26 +02:00
|
|
|
default:
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-30 04:35:10 +02:00
|
|
|
func (self *CommitLoader) getNormalRebasingCommits() ([]*models.Commit, error) {
|
2019-03-02 11:00:26 +02:00
|
|
|
rewrittenCount := 0
|
2021-12-30 02:22:29 +02:00
|
|
|
bytesContent, err := self.readFile(filepath.Join(self.dotGitDir, "rebase-apply/rewritten"))
|
2019-03-02 11:00:26 +02:00
|
|
|
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
|
2020-09-29 10:36:54 +02:00
|
|
|
commits := []*models.Commit{}
|
2021-12-30 02:22:29 +02:00
|
|
|
err = self.walkFiles(filepath.Join(self.dotGitDir, "rebase-apply"), func(path string, f os.FileInfo, err error) error {
|
2019-03-02 11:00:26 +02:00
|
|
|
if rewrittenCount > 0 {
|
|
|
|
rewrittenCount--
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
re := regexp.MustCompile(`^\d+$`)
|
|
|
|
if !re.MatchString(f.Name()) {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-30 02:22:29 +02:00
|
|
|
bytesContent, err := self.readFile(path)
|
2019-03-02 11:00:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
content := string(bytesContent)
|
2022-01-08 06:46:35 +02:00
|
|
|
commit := self.commitFromPatch(content)
|
2020-09-29 10:36:54 +02:00
|
|
|
commits = append([]*models.Commit{commit}, commits...)
|
2019-03-02 11:00:26 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return commits, nil
|
|
|
|
}
|
|
|
|
|
2019-02-19 00:18:30 +02:00
|
|
|
// git-rebase-todo example:
|
|
|
|
// pick ac446ae94ee560bdb8d1d057278657b251aaef17 ac446ae
|
|
|
|
// pick afb893148791a2fbd8091aeb81deba4930c73031 afb8931
|
|
|
|
|
|
|
|
// git-rebase-todo.backup example:
|
|
|
|
// pick 49cbba374296938ea86bbd4bf4fee2f6ba5cccf6 third commit on master
|
|
|
|
// pick ac446ae94ee560bdb8d1d057278657b251aaef17 blah commit on master
|
|
|
|
// pick afb893148791a2fbd8091aeb81deba4930c73031 fourth commit on master
|
|
|
|
|
2019-03-02 11:00:26 +02:00
|
|
|
// getInteractiveRebasingCommits takes our git-rebase-todo and our git-rebase-todo.backup files
|
2019-02-19 00:18:30 +02:00
|
|
|
// and extracts out the sha and names of commits that we still have to go
|
|
|
|
// in the rebase:
|
2021-12-30 04:35:10 +02:00
|
|
|
func (self *CommitLoader) getInteractiveRebasingCommits() ([]*models.Commit, error) {
|
2021-12-30 02:22:29 +02:00
|
|
|
bytesContent, err := self.readFile(filepath.Join(self.dotGitDir, "rebase-merge/git-rebase-todo"))
|
2019-02-19 14:36:29 +02:00
|
|
|
if err != nil {
|
2021-12-30 02:22:29 +02:00
|
|
|
self.Log.Error(fmt.Sprintf("error occurred reading git-rebase-todo: %s", err.Error()))
|
2019-02-19 14:36:29 +02:00
|
|
|
// we assume an error means the file doesn't exist so we just return
|
|
|
|
return nil, nil
|
2019-02-19 00:18:30 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 10:36:54 +02:00
|
|
|
commits := []*models.Commit{}
|
2019-02-19 14:36:29 +02:00
|
|
|
lines := strings.Split(string(bytesContent), "\n")
|
|
|
|
for _, line := range lines {
|
2019-02-24 04:51:52 +02:00
|
|
|
if line == "" || line == "noop" {
|
2019-02-19 14:36:29 +02:00
|
|
|
return commits, nil
|
2019-02-19 00:18:30 +02:00
|
|
|
}
|
2020-04-22 03:21:20 +02:00
|
|
|
if strings.HasPrefix(line, "#") {
|
2020-04-22 03:15:41 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-02-19 14:36:29 +02:00
|
|
|
splitLine := strings.Split(line, " ")
|
2020-09-29 10:36:54 +02:00
|
|
|
commits = append([]*models.Commit{{
|
2020-02-13 08:59:14 +02:00
|
|
|
Sha: splitLine[1],
|
2019-02-19 14:36:29 +02:00
|
|
|
Name: strings.Join(splitLine[2:], " "),
|
|
|
|
Status: "rebasing",
|
|
|
|
Action: splitLine[0],
|
|
|
|
}}, commits...)
|
2019-02-19 00:18:30 +02:00
|
|
|
}
|
|
|
|
|
2020-04-22 03:21:20 +02:00
|
|
|
return commits, nil
|
2019-02-19 00:18:30 +02:00
|
|
|
}
|
|
|
|
|
2019-03-02 11:00:26 +02:00
|
|
|
// 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
|
2022-01-08 06:46:35 +02:00
|
|
|
func (self *CommitLoader) commitFromPatch(content string) *models.Commit {
|
2019-03-02 11:00:26 +02:00
|
|
|
lines := strings.Split(content, "\n")
|
2020-02-13 08:59:14 +02:00
|
|
|
sha := strings.Split(lines[0], " ")[1]
|
2019-03-02 11:00:26 +02:00
|
|
|
name := strings.TrimPrefix(lines[3], "Subject: ")
|
2020-09-29 10:36:54 +02:00
|
|
|
return &models.Commit{
|
2019-03-02 11:00:26 +02:00
|
|
|
Sha: sha,
|
|
|
|
Name: name,
|
|
|
|
Status: "rebasing",
|
2022-01-08 06:46:35 +02:00
|
|
|
}
|
2019-03-02 11:00:26 +02:00
|
|
|
}
|
|
|
|
|
2021-12-30 04:35:10 +02:00
|
|
|
func (self *CommitLoader) setCommitMergedStatuses(refName string, commits []*models.Commit) ([]*models.Commit, error) {
|
2021-12-30 02:22:29 +02:00
|
|
|
ancestor, err := self.getMergeBase(refName)
|
2019-02-19 00:18:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ancestor == "" {
|
|
|
|
return commits, nil
|
|
|
|
}
|
|
|
|
passedAncestor := false
|
|
|
|
for i, commit := range commits {
|
|
|
|
if strings.HasPrefix(ancestor, commit.Sha) {
|
|
|
|
passedAncestor = true
|
|
|
|
}
|
|
|
|
if commit.Status != "pushed" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if passedAncestor {
|
|
|
|
commits[i].Status = "merged"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return commits, nil
|
|
|
|
}
|
|
|
|
|
2021-12-30 04:35:10 +02:00
|
|
|
func (self *CommitLoader) getMergeBase(refName string) (string, error) {
|
2021-12-30 02:22:29 +02:00
|
|
|
currentBranch, _, err := self.getCurrentBranchName()
|
2019-02-19 00:18:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
baseBranch := "master"
|
|
|
|
if strings.HasPrefix(currentBranch, "feature/") {
|
|
|
|
baseBranch = "develop"
|
|
|
|
}
|
|
|
|
|
2019-07-19 03:55:33 +02:00
|
|
|
// swallowing error because it's not a big deal; probably because there are no commits yet
|
2022-01-05 02:57:32 +02:00
|
|
|
output, _ := self.cmd.New(fmt.Sprintf("git merge-base %s %s", self.cmd.Quote(refName), self.cmd.Quote(baseBranch))).DontLog().RunWithOutput()
|
2020-09-02 12:39:24 +02:00
|
|
|
return ignoringWarnings(output), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ignoringWarnings(commandOutput string) string {
|
|
|
|
trimmedOutput := strings.TrimSpace(commandOutput)
|
|
|
|
split := strings.Split(trimmedOutput, "\n")
|
|
|
|
// need to get last line in case the first line is a warning about how the error is ambiguous.
|
|
|
|
// At some point we should find a way to make it unambiguous
|
|
|
|
lastLine := split[len(split)-1]
|
|
|
|
|
|
|
|
return lastLine
|
2019-02-19 00:18:30 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 00:42:42 +02:00
|
|
|
// getFirstPushedCommit returns the first commit SHA which has been pushed to the ref's upstream.
|
|
|
|
// all commits above this are deemed unpushed and marked as such.
|
2021-12-30 04:35:10 +02:00
|
|
|
func (self *CommitLoader) getFirstPushedCommit(refName string) (string, error) {
|
2021-12-30 02:22:29 +02:00
|
|
|
output, err := self.cmd.
|
|
|
|
New(
|
|
|
|
fmt.Sprintf("git merge-base %s %s@{u}", self.cmd.Quote(refName), self.cmd.Quote(refName)),
|
|
|
|
).
|
2022-01-05 02:57:32 +02:00
|
|
|
DontLog().
|
2021-12-30 02:22:29 +02:00
|
|
|
RunWithOutput()
|
2019-02-19 00:18:30 +02:00
|
|
|
if err != nil {
|
2020-08-27 00:42:42 +02:00
|
|
|
return "", err
|
2019-02-19 00:18:30 +02:00
|
|
|
}
|
2020-09-02 12:39:24 +02:00
|
|
|
|
|
|
|
return ignoringWarnings(output), nil
|
2019-02-19 00:18:30 +02:00
|
|
|
}
|
|
|
|
|
2020-01-11 09:23:35 +02:00
|
|
|
// getLog gets the git log.
|
2021-12-30 04:35:10 +02:00
|
|
|
func (self *CommitLoader) getLogCmd(opts GetCommitsOptions) oscommands.ICmdObj {
|
2020-01-11 09:23:35 +02:00
|
|
|
limitFlag := ""
|
2020-08-22 00:49:02 +02:00
|
|
|
if opts.Limit {
|
2021-12-30 04:11:58 +02:00
|
|
|
limitFlag = " -300"
|
2020-01-11 09:23:35 +02:00
|
|
|
}
|
|
|
|
|
2020-03-29 01:11:15 +02:00
|
|
|
filterFlag := ""
|
2020-08-22 00:49:02 +02:00
|
|
|
if opts.FilterPath != "" {
|
2021-12-30 02:22:29 +02:00
|
|
|
filterFlag = fmt.Sprintf(" --follow -- %s", self.cmd.Quote(opts.FilterPath))
|
2020-03-28 07:28:35 +02:00
|
|
|
}
|
|
|
|
|
2021-12-30 02:22:29 +02:00
|
|
|
config := self.UserConfig.Git.Log
|
2021-11-02 11:05:23 +02:00
|
|
|
|
|
|
|
orderFlag := "--" + config.Order
|
2021-11-02 12:16:00 +02:00
|
|
|
allFlag := ""
|
|
|
|
if opts.All {
|
|
|
|
allFlag = " --all"
|
|
|
|
}
|
2021-11-02 11:05:23 +02:00
|
|
|
|
2021-12-30 02:22:29 +02:00
|
|
|
return self.cmd.New(
|
2020-08-27 09:00:43 +02:00
|
|
|
fmt.Sprintf(
|
2021-12-30 04:11:58 +02:00
|
|
|
"git log %s %s %s --oneline %s%s --abbrev=%d%s",
|
2021-12-30 02:22:29 +02:00
|
|
|
self.cmd.Quote(opts.RefName),
|
2021-11-02 11:05:23 +02:00
|
|
|
orderFlag,
|
2021-11-02 12:16:00 +02:00
|
|
|
allFlag,
|
2021-10-30 08:42:52 +02:00
|
|
|
prettyFormat,
|
2020-08-27 09:00:43 +02:00
|
|
|
limitFlag,
|
|
|
|
20,
|
|
|
|
filterFlag,
|
|
|
|
),
|
2022-01-05 02:57:32 +02:00
|
|
|
).DontLog()
|
2019-02-19 00:18:30 +02:00
|
|
|
}
|
2021-10-30 08:42:52 +02:00
|
|
|
|
|
|
|
var prettyFormat = fmt.Sprintf(
|
|
|
|
"--pretty=format:\"%%H%s%%at%s%%aN%s%%d%s%%p%s%%s\"",
|
|
|
|
SEPARATION_CHAR,
|
|
|
|
SEPARATION_CHAR,
|
|
|
|
SEPARATION_CHAR,
|
|
|
|
SEPARATION_CHAR,
|
|
|
|
SEPARATION_CHAR,
|
|
|
|
)
|
|
|
|
|
|
|
|
func canExtractCommit(line string) bool {
|
2021-12-30 04:11:58 +02:00
|
|
|
return line != "" && strings.Split(line, " ")[0] != "gpg:"
|
2021-10-30 08:42:52 +02:00
|
|
|
}
|