diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2018-08-13 20:26:02 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2018-08-13 20:26:02 +1000 |
commit | 97cff656121270e9c790432e28622d92ab7b0f1a (patch) | |
tree | 7425013e94dc0a19699b48bde6bb20c6f5b86c8a /pkg/commands | |
parent | f9c39ad64bddd1577636c0ce5606eda44bc704ef (diff) |
progress on refactor
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/branch.go | 39 | ||||
-rw-r--r-- | pkg/commands/git.go | 92 | ||||
-rw-r--r-- | pkg/commands/git_structs.go | 12 | ||||
-rw-r--r-- | pkg/commands/os.go | 10 |
4 files changed, 92 insertions, 61 deletions
diff --git a/pkg/commands/branch.go b/pkg/commands/branch.go new file mode 100644 index 000000000..13c26e766 --- /dev/null +++ b/pkg/commands/branch.go @@ -0,0 +1,39 @@ +package commands + +import ( + "strings" + + "github.com/fatih/color" + "github.com/jesseduffield/lazygit/pkg/utils" +) + +// Branch : A git branch +// duplicating this for now +type Branch struct { + Name string + Recency string +} + +// GetDisplayString returns the dispaly string of branch +func (b *Branch) GetDisplayString() string { + return utils.WithPadding(b.Recency, 4) + utils.ColoredString(b.Name, b.GetColor()) +} + +// GetColor branch color +func (b *Branch) GetColor() color.Attribute { + switch b.getType() { + case "feature": + return color.FgGreen + case "bugfix": + return color.FgYellow + case "hotfix": + return color.FgRed + default: + return color.FgWhite + } +} + +// expected to return feature/bugfix/hotfix or blank string +func (b *Branch) getType() string { + return strings.Split(b.Name, "/")[0] +} diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 99ecec720..016a08fc6 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -38,13 +38,6 @@ func (c *GitCommand) SetupGit() { c.setupWorktree() } -// GitIgnore adds a file to the .gitignore of the repo -func (c *GitCommand) GitIgnore(filename string) { - if _, err := c.OSCommand.RunDirectCommand("echo '" + filename + "' >> .gitignore"); err != nil { - panic(err) - } -} - // GetStashEntries stash entryies func (c *GitCommand) GetStashEntries() []StashEntry { stashEntries := make([]StashEntry, 0) @@ -78,10 +71,10 @@ func includes(array []string, str string) bool { } // GetStatusFiles git status files -func (c *GitCommand) GetStatusFiles() []GitFile { +func (c *GitCommand) GetStatusFiles() []File { statusOutput, _ := c.GitStatus() statusStrings := utils.SplitLines(statusOutput) - gitFiles := make([]GitFile, 0) + files := make([]File, 0) for _, statusString := range statusStrings { change := statusString[0:2] @@ -89,7 +82,7 @@ func (c *GitCommand) GetStatusFiles() []GitFile { unstagedChange := statusString[1:2] filename := statusString[3:] tracked := !includes([]string{"??", "A "}, change) - gitFile := GitFile{ + file := File{ Name: filename, DisplayString: statusString, HasStagedChanges: !includes([]string{" ", "U", "?"}, stagedChange), @@ -98,10 +91,10 @@ func (c *GitCommand) GetStatusFiles() []GitFile { Deleted: unstagedChange == "D" || stagedChange == "D", HasMergeConflicts: change == "UU", } - gitFiles = append(gitFiles, gitFile) + files = append(files, file) } - c.Log.Info(gitFiles) // TODO: use a dumper-esque log here - return gitFiles + c.Log.Info(files) // TODO: use a dumper-esque log here + return files } // StashDo modify stash @@ -124,19 +117,19 @@ func (c *GitCommand) StashSave(message string) (string, error) { } // MergeStatusFiles merge status files -func (c *GitCommand) MergeStatusFiles(oldGitFiles, newGitFiles []GitFile) []GitFile { - if len(oldGitFiles) == 0 { - return newGitFiles +func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []File) []File { + if len(oldFiles) == 0 { + return newFiles } appendedIndexes := make([]int, 0) // retain position of files we already could see - result := make([]GitFile, 0) - for _, oldGitFile := range oldGitFiles { - for newIndex, newGitFile := range newGitFiles { - if oldGitFile.Name == newGitFile.Name { - result = append(result, newGitFile) + result := make([]File, 0) + for _, oldFile := range oldFiles { + for newIndex, newFile := range newFiles { + if oldFile.Name == newFile.Name { + result = append(result, newFile) appendedIndexes = append(appendedIndexes, newIndex) break } @@ -144,9 +137,9 @@ func (c *GitCommand) MergeStatusFiles(oldGitFiles, newGitFiles []GitFile) []GitF } // append any new files to the end - for index, newGitFile := range newGitFiles { + for index, newFile := range newFiles { if !includesInt(appendedIndexes, index) { - result = append(result, newGitFile) + result = append(result, newFile) } } @@ -217,17 +210,6 @@ func (c *GitCommand) GetCommitsToPush() []string { return utils.SplitLines(pushables) } -// BranchIncluded states whether a branch is included in a list of branches, -// with a case insensitive comparison on name -func (c *GitCommand) BranchIncluded(branchName string, branches []Branch) bool { - for _, existingBranch := range branches { - if strings.ToLower(existingBranch.Name) == strings.ToLower(branchName) { - return true - } - } - return false -} - // RenameCommit renames the topmost commit with the given name func (c *GitCommand) RenameCommit(name string) (string, error) { return c.OSCommand.RunDirectCommand("git commit --allow-empty --amend -m \"" + name + "\"") @@ -268,25 +250,26 @@ func (c *GitCommand) AbortMerge() (string, error) { return c.OSCommand.RunDirectCommand("git merge --abort") } -// GitCommit commit to git -func (c *GitCommand) GitCommit(g *gocui.Gui, message string) (string, error) { +// Commit commit to git +func (c *GitCommand) Commit(g *gocui.Gui, message string) (*exec.Cmd, error) { command := "git commit -m \"" + message + "\"" gpgsign, _ := gitconfig.Global("commit.gpgsign") if gpgsign != "" { - sub, err := c.OSCommand.RunSubProcess("git", "commit") - return "", nil + return c.OSCommand.PrepareSubProcess("git", "commit") } - return c.OSCommand.RunDirectCommand(command) + // TODO: make these runDirectCommand functions just return an error + _, err := c.OSCommand.RunDirectCommand(command) + return nil, err } -// GitPull pull from repo -func (c *GitCommand) GitPull() (string, error) { +// Pull pull from repo +func (c *GitCommand) Pull() (string, error) { return c.OSCommand.RunCommand("git pull --no-edit") } -// GitPush push to a branch -func (c *GitCommand) GitPush() (string, error) { - return c.OSCommand.RunDirectCommand("git push -u origin " + state.Branches[0].Name) +// Push push to a branch +func (c *GitCommand) Push(branchName string) (string, error) { + return c.OSCommand.RunDirectCommand("git push -u origin " + branchName) } // SquashPreviousTwoCommits squashes a commit down to the one below it @@ -364,7 +347,7 @@ func (c *GitCommand) IsInMergeState() (bool, error) { } // RemoveFile directly -func (c *GitCommand) RemoveFile(file GitFile) error { +func (c *GitCommand) RemoveFile(file File) error { // if the file isn't tracked, we assume you want to delete it if !file.Tracked { _, err := c.OSCommand.RunCommand("rm -rf ./" + file.Name) @@ -384,10 +367,15 @@ func (c *GitCommand) Checkout(branch string, force bool) (string, error) { return c.OSCommand.RunCommand("git checkout " + forceArg + branch) } -// AddPatch runs a subprocess for adding a patch by patch +// AddPatch prepares a subprocess for adding a patch by patch // this will eventually be swapped out for a better solution inside the Gui -func (c *GitCommand) AddPatch(g *gocui.Gui, filename string) (*exec.Cmd, error) { - return c.OSCommand.RunSubProcess("git", "add", "--patch", filename) +func (c *GitCommand) AddPatch(filename string) (*exec.Cmd, error) { + return c.OSCommand.PrepareSubProcess("git", "add", "--patch", filename) +} + +// PrepareCommitSubProcess prepares a subprocess for `git commit` +func (c *GitCommand) PrepareCommitSubProcess() (*exec.Cmd, error) { + return c.OSCommand.PrepareSubProcess("git", "commit") } // GetBranchGraph gets the color-formatted graph of the log for the given branch @@ -428,11 +416,11 @@ func includesInt(list []int, a int) bool { // GetCommits obtains the commits of the current branch func (c *GitCommand) GetCommits() []Commit { - pushables := gogit.GetCommitsToPush() - log := getLog() + pushables := c.GetCommitsToPush() + log := c.GetLog() commits := make([]Commit, 0) // now we can split it up and turn it into commits - lines := utils.RplitLines(log) + lines := utils.SplitLines(log) for _, line := range lines { splitLine := strings.Split(line, " ") sha := splitLine[0] @@ -477,7 +465,7 @@ func (c *GitCommand) Show(sha string) string { } // Diff returns the diff of a file -func (c *GitCommand) Diff(file GitFile) string { +func (c *GitCommand) Diff(file File) string { cachedArg := "" if file.HasStagedChanges && !file.HasUnstagedChanges { cachedArg = "--cached " diff --git a/pkg/commands/git_structs.go b/pkg/commands/git_structs.go index dd28d15fa..2f7255be1 100644 --- a/pkg/commands/git_structs.go +++ b/pkg/commands/git_structs.go @@ -2,7 +2,7 @@ package commands // File : A staged/unstaged file // TODO: decide whether to give all of these the Git prefix -type GitFile struct { +type File struct { Name string HasStagedChanges bool HasUnstagedChanges bool @@ -27,8 +27,10 @@ type StashEntry struct { DisplayString string } -// Branch : A git branch -type Branch struct { - Name string - Recency string +// Conflict : A git conflict with a start middle and end corresponding to line +// numbers in the file where the conflict bars appear +type Conflict struct { + start int + middle int + end int } diff --git a/pkg/commands/os.go b/pkg/commands/os.go index e7fe4515f..2313b5550 100644 --- a/pkg/commands/os.go +++ b/pkg/commands/os.go @@ -15,6 +15,8 @@ import ( var ( // ErrNoOpenCommand : When we don't know which command to use to open a file ErrNoOpenCommand = errors.New("Unsure what command to use to open this file") + // ErrNoEditorDefined : When we can't find an editor to edit a file + ErrNoEditorDefined = errors.New("No editor defined in $VISUAL, $EDITOR, or git config") ) // Platform stores the os state @@ -138,14 +140,14 @@ func (c *OSCommand) editFile(g *gocui.Gui, filename string) (string, error) { } } if editor == "" { - return "", createErrorPanel(g, "No editor defined in $VISUAL, $EDITOR, or git config.") + return "", ErrNoEditorDefined } - c.RunSubProcess(editor, filename) + c.PrepareSubProcess(editor, filename) return "", nil } -// RunSubProcess iniRunSubProcessrocess then tells the Gui to switch to it -func (c *OSCommand) RunSubProcess(cmdName string, commandArgs ...string) (*exec.Cmd, error) { +// PrepareSubProcess iniPrepareSubProcessrocess then tells the Gui to switch to it +func (c *OSCommand) PrepareSubProcess(cmdName string, commandArgs ...string) (*exec.Cmd, error) { subprocess := exec.Command(cmdName, commandArgs...) subprocess.Stdin = os.Stdin subprocess.Stdout = os.Stdout |