1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-03 13:21:56 +02:00
lazygit/gitcommands.go

280 lines
7.0 KiB
Go
Raw Normal View History

2018-05-19 11:16:34 +10:00
// Go has various value types including strings,
// integers, floats, booleans, etc. Here are a few
// basic examples.
2018-05-19 17:04:33 +10:00
package main
2018-05-19 11:16:34 +10:00
import (
2018-05-21 20:52:48 +10:00
2018-05-19 11:16:34 +10:00
// "log"
2018-05-21 20:52:48 +10:00
"fmt"
2018-05-26 13:23:39 +10:00
"os"
2018-05-19 11:16:34 +10:00
"os/exec"
"strings"
)
2018-05-21 20:52:48 +10:00
// GitFile : A staged/unstaged file
type GitFile struct {
Name string
DisplayString string
HasStagedChanges bool
HasUnstagedChanges bool
Tracked bool
Deleted bool
2018-05-19 11:16:34 +10:00
}
2018-05-21 20:52:48 +10:00
// Branch : A git branch
type Branch struct {
Name string
DisplayString string
Type string
BaseBranch string
2018-05-19 11:16:34 +10:00
}
2018-05-26 13:23:39 +10:00
func devLog(objects ...interface{}) {
localLog("/Users/jesseduffieldduffield/go/src/github.com/jesseduffield/gitgot/development.log", objects...)
}
func commandLog(objects ...interface{}) {
localLog("/Users/jesseduffieldduffield/go/src/github.com/jesseduffield/gitgot/commands.log", objects...)
localLog("/Users/jesseduffieldduffield/go/src/github.com/jesseduffield/gitgot/development.log", objects...)
}
func localLog(path string, objects ...interface{}) {
f, _ := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0644)
defer f.Close()
for _, object := range objects {
f.WriteString(fmt.Sprint(object) + "\n")
}
}
2018-05-21 20:52:48 +10:00
// Map (from https://gobyexample.com/collection-functions)
func Map(vs []string, f func(string) string) []string {
vsm := make([]string, len(vs))
for i, v := range vs {
vsm[i] = f(v)
}
return vsm
}
2018-05-19 11:16:34 +10:00
2018-05-26 13:23:39 +10:00
func includes(list []string, a string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
2018-05-21 20:52:48 +10:00
func mergeGitStatusFiles(oldGitFiles, newGitFiles []GitFile) []GitFile {
if len(oldGitFiles) == 0 {
return newGitFiles
}
2018-05-19 11:16:34 +10:00
2018-05-21 20:52:48 +10:00
result := make([]GitFile, 0)
for _, oldGitFile := range oldGitFiles {
for _, newGitFile := range newGitFiles {
if oldGitFile.Name == newGitFile.Name {
result = append(result, newGitFile)
break
}
}
2018-05-19 11:16:34 +10:00
}
2018-05-21 20:52:48 +10:00
return result
}
2018-05-19 11:16:34 +10:00
2018-05-21 22:34:02 +10:00
func runDirectCommand(command string) (string, error) {
2018-05-26 13:23:39 +10:00
commandLog(command)
2018-05-21 22:34:02 +10:00
cmdOut, err := exec.Command("bash", "-c", command).Output()
devLog(string(cmdOut))
2018-05-26 13:23:39 +10:00
devLog(err)
2018-05-21 20:52:48 +10:00
return string(cmdOut), err
}
2018-05-19 11:16:34 +10:00
2018-05-21 20:52:48 +10:00
func branchNameFromString(branchString string) string {
// because this has the recency at the beginning,
// we need to split and take the second part
splitBranchName := strings.Split(branchString, "\t")
return splitBranchName[len(splitBranchName)-1]
2018-05-19 11:16:34 +10:00
}
2018-05-21 20:52:48 +10:00
func getGitBranches() []Branch {
branches := make([]Branch, 0)
2018-05-21 22:34:02 +10:00
rawString, _ := runDirectCommand(getBranchesCommand)
2018-05-21 20:52:48 +10:00
branchLines := splitLines(rawString)
2018-05-26 13:23:39 +10:00
for i, line := range branchLines {
2018-05-21 20:52:48 +10:00
name := branchNameFromString(line)
var branchType string
var baseBranch string
if strings.Contains(line, "feature/") {
branchType = "feature"
baseBranch = "develop"
} else if strings.Contains(line, "bugfix/") {
branchType = "bugfix"
baseBranch = "develop"
} else if strings.Contains(line, "hotfix/") {
branchType = "hotfix"
baseBranch = "master"
} else {
branchType = "other"
baseBranch = name
}
2018-05-26 13:23:39 +10:00
if i == 0 {
line = line[:2] + "\t*" + line[2:]
}
2018-05-21 20:52:48 +10:00
branches = append(branches, Branch{name, line, branchType, baseBranch})
2018-05-19 11:16:34 +10:00
}
2018-05-26 13:23:39 +10:00
devLog(branches)
2018-05-21 20:52:48 +10:00
return branches
2018-05-19 11:16:34 +10:00
}
2018-05-21 20:52:48 +10:00
func getGitStatusFiles() []GitFile {
statusOutput, _ := getGitStatus()
statusStrings := splitLines(statusOutput)
2018-05-26 13:23:39 +10:00
devLog(statusStrings)
2018-05-21 20:52:48 +10:00
// a file can have both staged and unstaged changes
// I'll probably end up ignoring the unstaged flag for now but might revisit
// tracked, staged, unstaged
gitFiles := make([]GitFile, 0)
for _, statusString := range statusStrings {
stagedChange := statusString[0:1]
unstagedChange := statusString[1:2]
filename := statusString[3:]
tracked := statusString[0:2] != "??"
gitFile := GitFile{
Name: filename,
DisplayString: statusString,
HasStagedChanges: tracked && stagedChange != " ",
HasUnstagedChanges: !tracked || unstagedChange != " ",
Tracked: tracked,
Deleted: unstagedChange == "D" || stagedChange == "D",
}
gitFiles = append(gitFiles, gitFile)
}
return gitFiles
2018-05-19 11:16:34 +10:00
}
2018-05-21 20:52:48 +10:00
func gitCheckout(branch string, force bool) error {
forceArg := ""
if force {
forceArg = "--force "
}
_, err := runCommand("git checkout " + forceArg + branch)
return err
2018-05-19 11:16:34 +10:00
}
func runCommand(cmd string) (string, error) {
2018-05-26 13:23:39 +10:00
commandLog(cmd)
2018-05-19 11:16:34 +10:00
splitCmd := strings.Split(cmd, " ")
cmdOut, err := exec.Command(splitCmd[0], splitCmd[1:]...).Output()
2018-05-21 22:34:02 +10:00
devLog(string(cmdOut[:]))
2018-05-19 17:04:33 +10:00
return string(cmdOut), err
}
2018-05-19 11:16:34 +10:00
2018-05-26 13:23:39 +10:00
func openFile(filename string) (string, error) {
return runCommand("open " + filename)
}
func sublimeOpenFile(filename string) (string, error) {
return runCommand("subl " + filename)
}
2018-05-21 20:52:48 +10:00
func getBranchDiff(branch string, baseBranch string) (string, error) {
return runCommand("git diff --color " + baseBranch + "..." + branch)
}
2018-05-26 13:23:39 +10:00
func getLog() string {
result, err := runDirectCommand("git log --color --oneline")
if err != nil {
panic(err)
}
return result
}
2018-05-21 20:52:48 +10:00
func getDiff(file GitFile) string {
2018-05-19 17:04:33 +10:00
cachedArg := ""
2018-05-21 20:52:48 +10:00
if file.HasStagedChanges {
2018-05-19 17:04:33 +10:00
cachedArg = "--cached "
}
2018-05-21 20:52:48 +10:00
deletedArg := ""
2018-05-21 22:34:02 +10:00
if file.Deleted {
deletedArg = "-- "
}
trackedArg := ""
if !file.Tracked {
trackedArg = "--no-index /dev/null "
2018-05-21 20:52:48 +10:00
}
2018-05-21 22:34:02 +10:00
command := "git diff --color " + cachedArg + deletedArg + trackedArg + file.Name
2018-05-21 20:52:48 +10:00
s, err := runCommand(command)
2018-05-19 17:04:33 +10:00
if err != nil {
// for now we assume an error means the file was deleted
2018-05-21 20:52:48 +10:00
return s
2018-05-19 17:04:33 +10:00
}
return s
}
2018-05-19 11:16:34 +10:00
2018-05-19 17:04:33 +10:00
func stageFile(file string) error {
_, err := runCommand("git add " + file)
return err
}
func unStageFile(file string) error {
_, err := runCommand("git reset HEAD " + file)
return err
2018-05-19 11:16:34 +10:00
}
2018-05-21 20:52:48 +10:00
func getGitStatus() (string, error) {
return runCommand("git status --untracked-files=all --short")
}
2018-05-19 11:16:34 +10:00
2018-05-21 22:34:02 +10:00
func removeFile(file GitFile) error {
// if the file isn't tracked, we assume you want to delete it
if !file.Tracked {
_, err := runCommand("rm -rf ./" + file.Name)
return err
}
// if the file is tracked, we assume you want to just check it out
_, err := runCommand("git checkout " + file.Name)
return err
}
func gitCommit(message string) error {
_, err := runDirectCommand("git commit -m \"" + message + "\"")
return err
}
2018-05-26 15:44:44 +10:00
func gitPull() error {
_, err := runDirectCommand("git pull --no-edit")
return err
}
2018-05-21 20:52:48 +10:00
const getBranchesCommand = `set -e
git reflog -n100 --pretty='%cr|%gs' --grep-reflog='checkout: moving' HEAD | {
seen=":"
git_dir="$(git rev-parse --git-dir)"
while read line; do
date="${line%%|*}"
branch="${line##* }"
if ! [[ $seen == *:"${branch}":* ]]; then
seen="${seen}${branch}:"
if [ -f "${git_dir}/refs/heads/${branch}" ]; then
printf "%s\t%s\n" "$date" "$branch"
fi
fi
done | sed 's/ days /d /g' | sed 's/ weeks /w /g' | sed 's/ hours /h /g' | sed 's/ minutes /m /g' | sed 's/ago//g' | tr -d ' '
2018-05-19 11:16:34 +10:00
}
2018-05-21 20:52:48 +10:00
`
// func main() {
// getGitStatusFiles()
// }
2018-05-19 11:16:34 +10:00
2018-05-21 20:52:48 +10:00
// func devLog(s string) {
// f, _ := os.OpenFile("development.log", os.O_APPEND|os.O_WRONLY, 0644)
// defer f.Close()
2018-05-19 11:16:34 +10:00
2018-05-21 20:52:48 +10:00
// f.WriteString(s + "\n")
// }