From c350cdba4337fa8961e5fbe16e70d73fae7ccc20 Mon Sep 17 00:00:00 2001 From: skanehira Date: Sat, 23 Mar 2019 22:46:08 +0900 Subject: add feature of display diff between specific commits #397 --- docs/keybindings/Keybindings_en.md | 32 +++++++++--------- docs/keybindings/Keybindings_nl.md | 6 ++-- docs/keybindings/Keybindings_pl.md | 6 ++-- pkg/commands/commit.go | 10 +++++- pkg/commands/git.go | 6 ++++ pkg/gui/commits_panel.go | 66 ++++++++++++++++++++++++++++++++++++++ pkg/gui/gui.go | 5 ++- pkg/gui/keybindings.go | 6 ++++ pkg/i18n/dutch.go | 6 ++++ pkg/i18n/english.go | 6 ++++ pkg/i18n/polish.go | 6 ++++ 11 files changed, 131 insertions(+), 24 deletions(-) diff --git a/docs/keybindings/Keybindings_en.md b/docs/keybindings/Keybindings_en.md index 9d9d1471e..3f393610d 100644 --- a/docs/keybindings/Keybindings_en.md +++ b/docs/keybindings/Keybindings_en.md @@ -25,16 +25,15 @@ A: amend last commit C: commit changes using git editor space: toggle staged - d: delete if untracked / checkout if tracked + d: view 'discard changes' options e: edit file o: open file i: add to .gitignore r: refresh files S: stash files - s: soft reset to last commit a: stage/unstage all t: add patch - D: reset hard and remove untracked files + D: view reset options enter: stage individual hunks/lines f: fetch @@ -72,6 +71,7 @@ C: copy commit range (cherry-pick) v: paste commits (cherry-pick) enter: view commit's files + space: diff specific commits ## Stash @@ -91,19 +91,6 @@ o: open file -## Main (Merging) - -
-  esc: return to files panel
-  space: pick hunk
-  b: pick both hunks
-  : select previous conflict
-  : select next conflict
-  : select top hunk
-  : select bottom hunk
-  z: undo
-
- ## Main (Normal)
@@ -122,3 +109,16 @@
   space: stage line
   a: stage hunk
 
+ +## Main (Merging) + +
+  esc: return to files panel
+  space: pick hunk
+  b: pick both hunks
+  : select previous conflict
+  : select next conflict
+  : select top hunk
+  : select bottom hunk
+  z: undo
+
diff --git a/docs/keybindings/Keybindings_nl.md b/docs/keybindings/Keybindings_nl.md index dfa45df2c..3b5f250ec 100644 --- a/docs/keybindings/Keybindings_nl.md +++ b/docs/keybindings/Keybindings_nl.md @@ -25,16 +25,15 @@ A: wijzig laatste commit C: commit veranderingen met de git editor space: toggle staged - d: Verwijder als untracked / uitchecken wordt gevolgd (ga weg) + d: view 'discard changes' options e: verander bestand o: open bestand i: voeg toe aan .gitignore r: refresh bestanden S: stash-bestanden - s: soft reset to last commit a: toggle staged alle t: bewerkingen toevoegen - D: harde reset and verwijderen ongevolgde bestanden + D: view reset options enter: stage individuele hunks/lijnen f: fetch @@ -72,6 +71,7 @@ C: copy commit range (cherry-pick) v: paste commits (cherry-pick) enter: view commit's files + space: diff specific commits ## Stash diff --git a/docs/keybindings/Keybindings_pl.md b/docs/keybindings/Keybindings_pl.md index 9e60e9657..f4394af58 100644 --- a/docs/keybindings/Keybindings_pl.md +++ b/docs/keybindings/Keybindings_pl.md @@ -25,16 +25,15 @@ A: zmień ostatnie zatwierdzenie C: commituj zmiany używając edytora z gita space: przełącz zatwierdzenie - d: usuń jeśli nie śledzony / przełącz jeśli śledzony + d: view 'discard changes' options e: edytuj plik o: otwórz plik i: dodaj do .gitignore r: odśwież pliki S: przechowaj pliki - s: soft reset to last commit a: przełącz wszystkie zatwierdzenia t: dodaj łatkę - D: zresetuj twardo i usuń niepotwierdzone pliki + D: view reset options enter: zatwierdź pojedyncze linie f: fetch @@ -72,6 +71,7 @@ C: copy commit range (cherry-pick) v: paste commits (cherry-pick) enter: view commit's files + space: diff specific commits ## Schowek diff --git a/pkg/commands/commit.go b/pkg/commands/commit.go index 4253a5495..8f23ca001 100644 --- a/pkg/commands/commit.go +++ b/pkg/commands/commit.go @@ -13,6 +13,7 @@ type Commit struct { DisplayString string Action string // one of "", "pick", "edit", "squash", "reword", "drop", "fixup" Copied bool // to know if this commit is ready to be cherry-picked somewhere + Selected bool } // GetDisplayStrings is a function. @@ -52,5 +53,12 @@ func (c *Commit) GetDisplayStrings(isFocused bool) []string { actionString = cyan.Sprint(utils.WithPadding(c.Action, 7)) + " " } - return []string{shaColor.Sprint(c.Sha), actionString + white.Sprint(c.Name)} + name := "" + if c.Selected { + name = color.New(color.FgMagenta).Sprint(c.Name) + } else { + name = white.Sprint(c.Name) + } + + return []string{shaColor.Sprint(c.Sha), actionString + name} } diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 1b798990c..afd18dee7 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -910,3 +910,9 @@ func (c *GitCommand) ResetHardHead() error { func (c *GitCommand) ResetSoftHead() error { return c.OSCommand.RunCommand("git reset --soft HEAD") } + +// DiffCommits show diff between commits +func (c *GitCommand) DiffCommits(sha1, sha2 string) (string, error) { + cmd := fmt.Sprintf("git diff --color %s %s", sha1, sha2) + return c.OSCommand.RunCommandWithOutput(cmd) +} diff --git a/pkg/gui/commits_panel.go b/pkg/gui/commits_panel.go index 1bd8be4a1..0d3dd6e84 100644 --- a/pkg/gui/commits_panel.go +++ b/pkg/gui/commits_panel.go @@ -39,6 +39,12 @@ func (gui *Gui) handleCommitSelect(g *gocui.Gui, v *gocui.View) error { if err := gui.focusPoint(0, gui.State.Panels.Commits.SelectedLine, len(gui.State.Commits), v); err != nil { return err } + + // if specific diff mode is on, don't show diff + if gui.State.Panels.Commits.SpecificDiffMode { + return nil + } + commitText, err := gui.GitCommand.Show(commit.Sha) if err != nil { return err @@ -56,6 +62,13 @@ func (gui *Gui) refreshCommits(g *gocui.Gui) error { if err != nil { return err } + + for _, commit := range commits { + if _, has := gui.State.DiffEntries[commit.Sha]; has { + commit.Selected = true + } + } + gui.State.Commits = commits gui.refreshSelectedLine(&gui.State.Panels.Commits.SelectedLine, len(gui.State.Commits)) @@ -448,3 +461,56 @@ func (gui *Gui) handleSwitchToCommitFilesPanel(g *gocui.Gui, v *gocui.View) erro return gui.switchFocus(g, v, gui.getCommitFilesView()) } + +func (gui *Gui) handleToggleDiffCommit(g *gocui.Gui, v *gocui.View) error { + selectLimit := 2 + + // get selected commit + commit := gui.getSelectedCommit(g) + if commit == nil { + return gui.renderString(g, "main", gui.Tr.SLocalize("NoCommitsThisBranch")) + } + + // if already selected commit delete + if _, has := gui.State.DiffEntries[commit.Sha]; has { + delete(gui.State.DiffEntries, commit.Sha) + gui.setDiffMode() + } else { + if len(gui.State.DiffEntries) == selectLimit { + return gui.createErrorPanel(gui.g, "you have already selected two commit, please deselect one of two") + } + gui.State.DiffEntries[commit.Sha] = commit + gui.setDiffMode() + } + + // if selected tow commits, display diff between + if len(gui.State.DiffEntries) == selectLimit { + var entries []string + for _, entry := range gui.State.DiffEntries { + entries = append(entries, entry.Sha) + } + + commitText, err := gui.GitCommand.DiffCommits(entries[0], entries[1]) + + if err != nil { + return gui.createErrorPanel(gui.g, err.Error()) + } + + return gui.renderString(g, "main", commitText) + } + + return nil +} + +func (gui *Gui) setDiffMode() { + v := gui.getCommitsView() + if len(gui.State.DiffEntries) != 0 { + gui.State.Panels.Commits.SpecificDiffMode = true + v.Title = gui.Tr.SLocalize("CommitsDiffTitle") + } else { + gui.State.Panels.Commits.SpecificDiffMode = false + v.Title = gui.Tr.SLocalize("CommitsTitle") + } + + gui.refreshCommits(gui.g) +} diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 07d51e2bc..f6cdd093d 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -103,7 +103,8 @@ type branchPanelState struct { } type commitPanelState struct { - SelectedLine int + SelectedLine int + SpecificDiffMode bool } type stashPanelState struct { @@ -135,6 +136,7 @@ type guiState struct { Commits []*commands.Commit StashEntries []*commands.StashEntry CommitFiles []*commands.CommitFile + DiffEntries map[string]*commands.Commit MenuItemCount int // can't store the actual list because it's of interface{} type PreviousView string Platform commands.Platform @@ -154,6 +156,7 @@ func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *comma Commits: make([]*commands.Commit, 0), CherryPickedCommits: make([]*commands.Commit, 0), StashEntries: make([]*commands.StashEntry, 0), + DiffEntries: make(map[string]*commands.Commit), Platform: *oSCommand.Platform, Panels: &panelStates{ Files: &filePanelState{SelectedLine: -1}, diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 518575550..28a60d473 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -388,6 +388,12 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { Modifier: gocui.ModNone, Handler: gui.handleSwitchToCommitFilesPanel, Description: gui.Tr.SLocalize("viewCommitFiles"), + }, { + ViewName: "commits", + Key: gocui.KeySpace, + Modifier: gocui.ModNone, + Handler: gui.handleToggleDiffCommit, + Description: gui.Tr.SLocalize("CommitsDiff"), }, { ViewName: "stash", Key: gocui.KeySpace, diff --git a/pkg/i18n/dutch.go b/pkg/i18n/dutch.go index 8e0f6ce3b..c87f93c9b 100644 --- a/pkg/i18n/dutch.go +++ b/pkg/i18n/dutch.go @@ -28,6 +28,12 @@ func addDutch(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "CommitsTitle", Other: "Commits", + }, &i18n.Message{ + ID: "CommitsDiffTitle", + Other: "Commits(specific diff mode)", + }, &i18n.Message{ + ID: "CommitsDiff", + Other: "diff specific commits", }, &i18n.Message{ ID: "StashTitle", Other: "Stash", diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 397c262a2..c5fd61924 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -36,6 +36,12 @@ func addEnglish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "CommitsTitle", Other: "Commits", + }, &i18n.Message{ + ID: "CommitsDiffTitle", + Other: "Commits(specific diff mode)", + }, &i18n.Message{ + ID: "CommitsDiff", + Other: "diff specific commits", }, &i18n.Message{ ID: "StashTitle", Other: "Stash", diff --git a/pkg/i18n/polish.go b/pkg/i18n/polish.go index e6d09eafb..cf9d7abf5 100644 --- a/pkg/i18n/polish.go +++ b/pkg/i18n/polish.go @@ -26,6 +26,12 @@ func addPolish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "CommitsTitle", Other: "Commity", + }, &i18n.Message{ + ID: "CommitsDiffTitle", + Other: "Commits(specific diff mode)", + }, &i18n.Message{ + ID: "CommitsDiff", + Other: "diff specific commits", }, &i18n.Message{ ID: "StashTitle", Other: "Schowek", -- cgit v1.2.3