mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-21 00:30:00 +02:00
.devcontainer
.github
.vscode
cmd
demo
docs
pkg
app
cheatsheet
commands
git_commands
bisect.go
bisect_info.go
blame.go
branch.go
branch_loader.go
branch_loader_test.go
branch_test.go
commit.go
commit_file_loader.go
commit_file_loader_test.go
commit_loader.go
commit_loader_test.go
commit_test.go
common.go
config.go
custom.go
deps_test.go
diff.go
file.go
file_loader.go
file_loader_test.go
file_test.go
flow.go
flow_test.go
git_command_builder.go
git_command_builder_test.go
main_branches.go
patch.go
rebase.go
rebase_test.go
reflog_commit_loader.go
reflog_commit_loader_test.go
remote.go
remote_loader.go
repo_paths.go
repo_paths_test.go
stash.go
stash_loader.go
stash_loader_test.go
stash_test.go
status.go
submodule.go
sync.go
sync_test.go
tag.go
tag_loader.go
tag_loader_test.go
version.go
version_test.go
working_tree.go
working_tree_test.go
worktree.go
worktree_loader.go
worktree_loader_test.go
git_config
hosting_service
models
oscommands
patch
testdata
types
git.go
git_cmd_obj_builder.go
git_cmd_obj_runner.go
common
config
constants
env
fakes
gui
i18n
integration
jsonschema
logs
snake
tasks
theme
updates
utils
schema
scripts
test
vendor
.codespellrc
.editorconfig
.gitattributes
.gitignore
.golangci.yml
.goreleaser.yml
CODE-OF-CONDUCT.md
CONTRIBUTING.md
Dockerfile
LICENSE
Makefile
README.md
go.mod
go.sum
main.go
93 lines
2.8 KiB
Go
93 lines
2.8 KiB
Go
package git_commands
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
|
"github.com/jesseduffield/lazygit/pkg/common"
|
|
)
|
|
|
|
type ReflogCommitLoader struct {
|
|
*common.Common
|
|
cmd oscommands.ICmdObjBuilder
|
|
}
|
|
|
|
func NewReflogCommitLoader(common *common.Common, cmd oscommands.ICmdObjBuilder) *ReflogCommitLoader {
|
|
return &ReflogCommitLoader{
|
|
Common: common,
|
|
cmd: cmd,
|
|
}
|
|
}
|
|
|
|
// 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 (self *ReflogCommitLoader) GetReflogCommits(lastReflogCommit *models.Commit, filterPath string, filterAuthor string) ([]*models.Commit, bool, error) {
|
|
commits := make([]*models.Commit, 0)
|
|
|
|
cmdArgs := NewGitCmd("log").
|
|
Config("log.showSignature=false").
|
|
Arg("-g").
|
|
Arg("--abbrev=40").
|
|
Arg("--format=%h%x00%ct%x00%gs%x00%p").
|
|
ArgIf(filterAuthor != "", "--author="+filterAuthor).
|
|
ArgIf(filterPath != "", "--follow", "--", filterPath).
|
|
ToArgv()
|
|
|
|
cmdObj := self.cmd.New(cmdArgs).DontLog()
|
|
|
|
onlyObtainedNewReflogCommits := false
|
|
err := cmdObj.RunAndProcessLines(func(line string) (bool, error) {
|
|
commit, ok := self.parseLine(line)
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
|
|
// note that the unix timestamp here is the timestamp of the COMMIT, not the reflog entry itself,
|
|
// so two consecutive reflog entries may have both the same hash and therefore same timestamp.
|
|
// We use the reflog message to disambiguate, and fingers crossed that we never see the same of those
|
|
// twice in a row. Reason being that it would mean we'd be erroneously exiting early.
|
|
if lastReflogCommit != nil && self.sameReflogCommit(commit, lastReflogCommit) {
|
|
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
|
|
}
|
|
|
|
func (self *ReflogCommitLoader) sameReflogCommit(a *models.Commit, b *models.Commit) bool {
|
|
return a.Hash == b.Hash && a.UnixTimestamp == b.UnixTimestamp && a.Name == b.Name
|
|
}
|
|
|
|
func (self *ReflogCommitLoader) parseLine(line string) (*models.Commit, bool) {
|
|
fields := strings.SplitN(line, "\x00", 4)
|
|
if len(fields) <= 3 {
|
|
return nil, false
|
|
}
|
|
|
|
unixTimestamp, _ := strconv.Atoi(fields[1])
|
|
|
|
parentHashes := fields[3]
|
|
parents := []string{}
|
|
if len(parentHashes) > 0 {
|
|
parents = strings.Split(parentHashes, " ")
|
|
}
|
|
|
|
return &models.Commit{
|
|
Hash: fields[0],
|
|
Name: fields[2],
|
|
UnixTimestamp: int64(unixTimestamp),
|
|
Status: models.StatusReflog,
|
|
Parents: parents,
|
|
}, true
|
|
}
|