2020-09-29 18:36:54 +10:00
|
|
|
package models
|
2018-09-17 21:02:30 +10:00
|
|
|
|
2022-01-19 18:32:27 +11:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-04-03 12:42:29 +02:00
|
|
|
"github.com/fsmiamoto/git-todo-parser/todo"
|
2022-01-19 18:32:27 +11:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
|
|
)
|
2020-08-16 22:01:14 +10:00
|
|
|
|
2022-03-26 22:03:32 +09:00
|
|
|
// Special commit hash for empty tree object
|
|
|
|
const EmptyTreeCommitHash = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
|
|
|
|
|
2023-04-03 12:40:29 +02:00
|
|
|
type CommitStatus int
|
|
|
|
|
|
|
|
const (
|
|
|
|
StatusNone CommitStatus = iota
|
|
|
|
StatusUnpushed
|
|
|
|
StatusPushed
|
|
|
|
StatusMerged
|
|
|
|
StatusRebasing
|
|
|
|
StatusSelected
|
|
|
|
StatusReflog
|
|
|
|
)
|
|
|
|
|
2023-04-03 12:42:29 +02:00
|
|
|
const (
|
|
|
|
// Conveniently for us, the todo package starts the enum at 1, and given
|
|
|
|
// that it doesn't have a "none" value, we're setting ours to 0
|
|
|
|
ActionNone todo.TodoCommand = 0
|
2023-02-26 11:51:02 +01:00
|
|
|
// "Comment" is the last one of the todo package's enum entries
|
|
|
|
ActionConflict = todo.Comment + 1
|
2023-04-03 12:42:29 +02:00
|
|
|
)
|
|
|
|
|
2018-09-17 21:02:30 +10:00
|
|
|
// Commit : A git commit
|
|
|
|
type Commit struct {
|
2020-03-27 19:12:15 +11:00
|
|
|
Sha string
|
|
|
|
Name string
|
2023-04-03 12:40:29 +02:00
|
|
|
Status CommitStatus
|
2023-04-03 12:42:29 +02:00
|
|
|
Action todo.TodoCommand
|
2020-03-27 19:12:15 +11:00
|
|
|
Tags []string
|
|
|
|
ExtraInfo string // something like 'HEAD -> master, tag: v0.15.2'
|
2022-05-08 14:23:29 +10:00
|
|
|
AuthorName string // something like 'Jesse Duffield'
|
|
|
|
AuthorEmail string // something like 'jessedduffield@gmail.com'
|
2020-03-27 19:12:15 +11:00
|
|
|
UnixTimestamp int64
|
2020-08-27 17:00:43 +10:00
|
|
|
|
2021-06-05 16:39:59 +10:00
|
|
|
// SHAs of parent commits (will be multiple if it's a merge commit)
|
|
|
|
Parents []string
|
2018-09-17 21:02:30 +10:00
|
|
|
}
|
2020-03-26 09:18:52 +11:00
|
|
|
|
|
|
|
func (c *Commit) ShortSha() string {
|
2022-01-19 18:32:27 +11:00
|
|
|
return utils.ShortSha(c.Sha)
|
2020-03-26 09:18:52 +11:00
|
|
|
}
|
2020-08-16 22:01:14 +10:00
|
|
|
|
2022-05-12 19:16:58 +09:00
|
|
|
func (c *Commit) FullRefName() string {
|
|
|
|
return c.Sha
|
|
|
|
}
|
|
|
|
|
2020-08-19 22:57:22 +10:00
|
|
|
func (c *Commit) RefName() string {
|
|
|
|
return c.Sha
|
|
|
|
}
|
2020-08-22 09:01:14 +10:00
|
|
|
|
2022-03-26 22:03:32 +09:00
|
|
|
func (c *Commit) ParentRefName() string {
|
|
|
|
if c.IsFirstCommit() {
|
|
|
|
return EmptyTreeCommitHash
|
|
|
|
}
|
|
|
|
return c.RefName() + "^"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commit) IsFirstCommit() bool {
|
|
|
|
return len(c.Parents) == 0
|
|
|
|
}
|
|
|
|
|
2020-08-22 09:01:14 +10:00
|
|
|
func (c *Commit) ID() string {
|
|
|
|
return c.RefName()
|
|
|
|
}
|
2020-08-22 10:14:53 +10:00
|
|
|
|
|
|
|
func (c *Commit) Description() string {
|
|
|
|
return fmt.Sprintf("%s %s", c.Sha[:7], c.Name)
|
|
|
|
}
|
2021-06-05 16:39:59 +10:00
|
|
|
|
|
|
|
func (c *Commit) IsMerge() bool {
|
|
|
|
return len(c.Parents) > 1
|
|
|
|
}
|
2022-01-22 12:56:57 +11:00
|
|
|
|
|
|
|
// returns true if this commit is not actually in the git log but instead
|
|
|
|
// is from a TODO file for an interactive rebase.
|
|
|
|
func (c *Commit) IsTODO() bool {
|
2023-04-03 12:42:29 +02:00
|
|
|
return c.Action != ActionNone
|
2022-01-22 12:56:57 +11:00
|
|
|
}
|
2023-03-03 19:53:15 +01:00
|
|
|
|
|
|
|
func IsHeadCommit(commits []*Commit, index int) bool {
|
|
|
|
return !commits[index].IsTODO() && (index == 0 || commits[index-1].IsTODO())
|
|
|
|
}
|