add support for merging

This commit is contained in:
Jesse Duffield 2018-06-09 19:06:33 +10:00
parent 4f6ae62e79
commit 7653ff9a24
10 changed files with 540 additions and 131 deletions

View file

@ -21,6 +21,7 @@ type GitFile struct {
HasUnstagedChanges bool
Tracked bool
Deleted bool
HasMergeConflicts bool
DisplayString string
}
@ -30,7 +31,6 @@ type Branch struct {
Type string
BaseBranch string
DisplayString string
DisplayColor color.Attribute
}
// Commit : A git commit
@ -133,8 +133,8 @@ func branchPropertiesFromName(name string) (string, string, color.Attribute) {
return "other", name, color.FgWhite
}
func coloredString(str string, colour color.Attribute) string {
return color.New(colour).SprintFunc()(fmt.Sprint(str))
func coloredString(str string, colour *color.Color) string {
return colour.SprintFunc()(fmt.Sprint(str))
}
func withPadding(str string, padding int) string {
@ -143,17 +143,17 @@ func withPadding(str string, padding int) string {
func branchFromLine(line string, index int) Branch {
recency, name := branchStringParts(line)
branchType, branchBase, colour := branchPropertiesFromName(name)
branchType, branchBase, colourAttr := branchPropertiesFromName(name)
if index == 0 {
recency = " *"
}
colour := color.New(colourAttr)
displayString := withPadding(recency, 4) + coloredString(name, colour)
return Branch{
Name: name,
Type: branchType,
BaseBranch: branchBase,
DisplayString: displayString,
DisplayColor: colour,
}
}
@ -206,10 +206,11 @@ func getGitStatusFiles() []GitFile {
gitFile := GitFile{
Name: filename,
DisplayString: statusString,
HasStagedChanges: tracked && stagedChange != " ",
HasStagedChanges: tracked && stagedChange != " " && stagedChange != "U",
HasUnstagedChanges: !tracked || unstagedChange != " ",
Tracked: tracked,
Deleted: unstagedChange == "D" || stagedChange == "D",
HasMergeConflicts: statusString[0:2] == "UU",
}
gitFiles = append(gitFiles, gitFile)
}
@ -328,14 +329,15 @@ func getDiff(file GitFile) string {
trackedArg = "--no-index /dev/null "
}
command := "git diff --color " + cachedArg + deletedArg + trackedArg + file.Name
s, err := runCommand(command)
if err != nil {
// for now we assume an error means the file was deleted
return s
}
// for now we assume an error means the file was deleted
s, _ := runCommand(command)
return s
}
func catFile(file string) (string, error) {
return runDirectCommand("cat " + file)
}
func stageFile(file string) error {
_, err := runCommand("git add " + file)
return err
@ -350,6 +352,14 @@ func getGitStatus() (string, error) {
return runCommand("git status --untracked-files=all --short")
}
func isInMergeState() (bool, error) {
output, err := runCommand("git status --untracked-files=all")
if err != nil {
return false, err
}
return strings.Contains(output, "conclude merge") || strings.Contains(output, "unmerged paths"), nil
}
func removeFile(file GitFile) error {
// if the file isn't tracked, we assume you want to delete it
if !file.Tracked {
@ -398,6 +408,14 @@ func gitListStash() (string, error) {
return runDirectCommand("git stash list")
}
func gitMerge(branchName string) (string, error) {
return runDirectCommand("git merge --no-edit " + branchName)
}
func gitAbortMerge() (string, error) {
return runDirectCommand("git merge --abort")
}
func gitUpstreamDifferenceCount() (string, string) {
pushableCount, err := runDirectCommand("git rev-list @{u}..head --count")
if err != nil {