Repository: jesseduffield/lazygit
File: pkg/commands/git_commands/commit_loader.go (Go)

Code before:
		}
		if passedAncestor {
			commits[i].Status = models.StatusMerged
		}
	}
	return commits
}

func (self *CommitLoader) getMergeBase(refName string) string {
	if self.quotedBaseBranches == nil {
		self.quotedBaseBranches = self.getExistingBaseBranches()
	}

	if *self.quotedBaseBranches == "" {
		return ""
	}

	// We pass all configured base branches to the merge-base call; git will
	// return the base commit for the closest one.
	output, err := self.cmd.New(fmt.Sprintf("git merge-base %s %s",
		self.cmd.Quote(refName), *self.quotedBaseBranches)).DontLog().RunWithOutput()
	if err != nil {
		// If there's an error, it must be because one of the base branches that
		// used to exist when we called getExistingBaseBranches() was deleted
		// meanwhile. To fix this for next time, throw away our cache.
		self.quotedBaseBranches = nil
	}
	return ignoringWarnings(output)
}

func (self *CommitLoader) getExistingBaseBranches() *string {
	quotedBaseBranches := strings.Join(
		lo.FilterMap(self.UserConfig.Git.BaseBranches,
			func(branchName string, _ int) (string, bool) {
				quotedRef := self.cmd.Quote("refs/heads/" + branchName)
				if err := self.cmd.New(fmt.Sprintf("git rev-parse --verify --quiet %s", quotedRef)).DontLog().Run(); err != nil {
					return "", false
				}
				return quotedRef, true
			}), " ")
	return &quotedBaseBranches
}

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

Review comment (bug):
I think this comment wasn't accurate; there can't be an error when there are no commits yet, because we don't call this function in that case. I think the only reason why this could fail (except for i/o errors or data corruption) is that the base branch doesn't exist. I now handle this error further down by throwing away the cache.

Code after:
		}
		if commit.Status != models.StatusPushed && commit.Status != models.StatusUnpushed {
			continue
		}
		if passedAncestor {
			commits[i].Status = models.StatusMerged
		}
	}
	return commits
}

func (self *CommitLoader) getMergeBase(refName string) string {
	if self.quotedMainBranches == nil {
		self.quotedMainBranches = lo.ToPtr(self.getExistingMainBranches())
	}

	if *self.quotedMainBranches == "" {
		return ""
	}

	// We pass all configured main branches to the merge-base call; git will
	// return the base commit for the closest one.
	output, err := self.cmd.New(fmt.Sprintf("git merge-base %s %s",
		self.cmd.Quote(refName), *self.quotedMainBranches)).DontLog().RunWithOutput()
	if err != nil {
		// If there's an error, it must be because one of the main branches that
		// used to exist when we called getExistingMainBranches() was deleted
		// meanwhile. To fix this for next time, throw away our cache.
		self.quotedMainBranches = nil
	}
	return ignoringWarnings(output)
}

func (self *CommitLoader) getExistingMainBranches() string {
	return strings.Join(
		lo.FilterMap(self.UserConfig.Git.MainBranches,
			func(branchName string, _ int) (string, bool) {
				quotedRef := self.cmd.Quote("refs/heads/" + branchName)
				if err := self.cmd.New(fmt.Sprintf("git rev-parse --verify --quiet %s", quotedRef)).DontLog().Run(); err != nil {
					return "", false
				}
				return quotedRef, true
			}), " ")
}

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]