diff options
| author | Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com> | 2018-12-30 20:49:13 +0300 |
|---|---|---|
| committer | Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com> | 2018-12-30 20:49:13 +0300 |
| commit | a8ba3a6383295b86f925fcbf8907943fda71e0dc (patch) | |
| tree | af2bb68fd650fd123aec89aa4fea47a25adccfc6 | |
| parent | Merge branch 'develop' of https://github.com/isacikgoz/gitbatch into develop (diff) | |
| parent | Minor checks and changes throughout files (#53) (diff) | |
| download | gitbatch-a8ba3a6383295b86f925fcbf8907943fda71e0dc.tar.gz | |
Merge branch 'master' into develop
| -rw-r--r-- | pkg/app/quick.go | 1 | ||||
| -rw-r--r-- | pkg/git/branch.go | 5 | ||||
| -rw-r--r-- | pkg/git/cmd-stash.go | 9 | ||||
| -rw-r--r-- | pkg/gui/authenticationview.go | 41 | ||||
| -rw-r--r-- | pkg/gui/commitview.go | 33 | ||||
| -rw-r--r-- | pkg/gui/gui.go | 16 | ||||
| -rw-r--r-- | pkg/gui/mainview.go | 12 | ||||
| -rw-r--r-- | pkg/gui/sideviews.go | 21 | ||||
| -rw-r--r-- | pkg/gui/stashview.go | 7 | ||||
| -rw-r--r-- | pkg/gui/statusview.go | 77 | ||||
| -rw-r--r-- | pkg/gui/unstagedview.go | 12 | ||||
| -rw-r--r-- | pkg/gui/util-common.go | 17 | ||||
| -rw-r--r-- | pkg/gui/util-textstyle.go | 11 | ||||
| -rw-r--r-- | pkg/helpers/utils.go | 4 | ||||
| -rw-r--r-- | pkg/helpers/utils_test.go | 11 |
15 files changed, 159 insertions, 118 deletions
diff --git a/pkg/app/quick.go b/pkg/app/quick.go index fba5d8f..488e1d6 100644 --- a/pkg/app/quick.go +++ b/pkg/app/quick.go @@ -9,7 +9,6 @@ import ( ) func quick(directories []string, depth int, mode string) { - var wg sync.WaitGroup start := time.Now() for _, dir := range directories { diff --git a/pkg/git/branch.go b/pkg/git/branch.go index e5facf8..d3b2425 100644 --- a/pkg/git/branch.go +++ b/pkg/git/branch.go @@ -117,6 +117,7 @@ func (e *RepoEntity) Checkout(branch *Branch) error { if branch.Name == e.Branch.Name { return nil } + w, err := e.Repository.Worktree() if err != nil { log.Warn("Cannot get work tree " + err.Error()) @@ -130,7 +131,9 @@ func (e *RepoEntity) Checkout(branch *Branch) error { } // make this conditional on global scale - err = e.Remote.SyncBranches(branch.Name) + // we don't care if this function returns an error + e.Remote.SyncBranches(branch.Name) + return e.Refresh() } diff --git a/pkg/git/cmd-stash.go b/pkg/git/cmd-stash.go index 463bd35..d2708ec 100644 --- a/pkg/git/cmd-stash.go +++ b/pkg/git/cmd-stash.go @@ -38,7 +38,7 @@ func (e *RepoEntity) loadStashedItems() error { stashIDRegexInt := regexp.MustCompile(`[\d]+`) stashBranchRegex := regexp.MustCompile(`^(.*?): `) stashMsgRegex := regexp.MustCompile(`WIP on \(?([^)]*)\)?`) - stashHashRegex := regexp.MustCompile(`[\w]{7}`) + stashHashRegex := regexp.MustCompile(`[\w|\d]{7}\s`) stashlist := strings.Split(output, "\n") for _, stashitem := range stashlist { @@ -64,11 +64,12 @@ func (e *RepoEntity) loadStashedItems() error { // trim branch section trimmed = stashBranchRegex.Split(trimmed, 2)[1] - hash := stashHashRegex.FindString(trimmed) + hash := "" var desc string - if stashHashRegex.MatchString(hash) && len(stashHashRegex.Split(trimmed, 2)) >= 2 { - desc = stashHashRegex.Split(trimmed, 2)[1][1:] + if stashHashRegex.MatchString(trimmed) { + hash = stashHashRegex.FindString(trimmed)[:7] + desc = stashHashRegex.Split(trimmed, 2)[1] } else { desc = trimmed } diff --git a/pkg/gui/authenticationview.go b/pkg/gui/authenticationview.go index b644b6b..a274425 100644 --- a/pkg/gui/authenticationview.go +++ b/pkg/gui/authenticationview.go @@ -12,10 +12,12 @@ import ( var ( // this is required so we can know where we can return authenticationReturnView string + // these views used as a label for git repository address and credential views authenticationViewFeature = viewFeature{Name: "authentication", Title: " Authentication "} authUserLabelFeature = viewFeature{Name: "authuserlabel", Title: " User: "} authPswdLabelViewFeature = viewFeature{Name: "authpasswdlabel", Title: " Password: "} + // these views used as a input for the credentials authUserFeature = viewFeature{Name: "authuser", Title: " User "} authPasswordViewFeature = viewFeature{Name: "authpasswd", Title: " Password "} @@ -77,13 +79,25 @@ func (gui *Gui) closeAuthenticationView(g *gocui.Gui, v *gocui.View) error { // close the opened auth views and submit the credentials func (gui *Gui) submitAuthenticationView(g *gocui.Gui, v *gocui.View) error { g.Cursor = false + // in order to read buffer of the views, first we need to find'em - v_user, err := g.View(authUserFeature.Name) - v_pswd, err := g.View(authPasswordViewFeature.Name) + vUser, err := g.View(authUserFeature.Name) + if err != nil { + log.Errorln("error while retrieving user from view:", err) + return err // should return?? + } + + vPswd, err := g.View(authPasswordViewFeature.Name) + if err != nil { + log.Errorln("error while retrieving password from view:", err) + return err // should return?? + } + // the return string of the views contain trailing new lines re := regexp.MustCompile(`\r?\n`) - creduser := re.ReplaceAllString(v_user.ViewBuffer(), "") - credpswd := re.ReplaceAllString(v_pswd.ViewBuffer(), "") + creduser := re.ReplaceAllString(vUser.ViewBuffer(), "") + credpswd := re.ReplaceAllString(vPswd.ViewBuffer(), "") + // since the git ops require different types of options we better switch switch mode := jobRequiresAuth.JobType; mode { case git.FetchJob: @@ -105,15 +119,22 @@ func (gui *Gui) submitAuthenticationView(g *gocui.Gui, v *gocui.View) error { } } jobRequiresAuth.Entity.SetState(git.Queued) + // add this job to the last of the queue - err = gui.State.Queue.AddJob(jobRequiresAuth) - if err != nil { + if err := gui.State.Queue.AddJob(jobRequiresAuth); err != nil { return err } - gui.closeAuthenticationView(g, v) - v_return, err := g.View(authenticationReturnView) - gui.startQueue(g, v_return) - return nil + + if err := gui.closeAuthenticationView(g, v); err != nil { + return err // should return?? + } + + vReturn, err := g.View(authenticationReturnView) + if err != nil { + return err // should return?? + } + + return gui.startQueue(g, vReturn) } // open an error view to inform user with a message and a useful note diff --git a/pkg/gui/commitview.go b/pkg/gui/commitview.go index 0810621..3359037 100644 --- a/pkg/gui/commitview.go +++ b/pkg/gui/commitview.go @@ -27,13 +27,13 @@ var ( func (gui *Gui) openCommitMessageView(g *gocui.Gui, v *gocui.View) error { maxX, maxY := g.Size() commitMesageReturnView = v.Name() - v_frame, err := g.SetView(commitFrameViewFeature.Name, maxX/2-30, maxY/2-4, maxX/2+30, maxY/2+3) + vFrame, err := g.SetView(commitFrameViewFeature.Name, maxX/2-30, maxY/2-4, maxX/2+30, maxY/2+3) if err != nil { if err != gocui.ErrUnknownView { return err } - v_frame.Frame = true - fmt.Fprintln(v_frame, " Enter your commit message:") + vFrame.Frame = true + fmt.Fprintln(vFrame, " Enter your commit message:") } v, err = g.SetView(commitMessageViewFeature.Name, maxX/2-29, maxY/2-3, maxX/2+29, maxY/2) if err != nil { @@ -124,19 +124,33 @@ func (gui *Gui) openCommitUserEmailView(g *gocui.Gui) error { // close the opened commite mesage view func (gui *Gui) submitCommitMessageView(g *gocui.Gui, v *gocui.View) error { e := gui.getSelectedRepository() + // in order to read buffer of the views, first we need to find'em - v_msg, err := g.View(commitMessageViewFeature.Name) - v_name, err := g.View(commitUserUserViewFeature.Name) - v_email, err := g.View(commitUserEmailViewFeature.Name) + vMsg, err := g.View(commitMessageViewFeature.Name) + if err != nil { + return err // should return?? + } + + vName, err := g.View(commitUserUserViewFeature.Name) + if err != nil { + return err // should return?? + } + + vEmail, err := g.View(commitUserEmailViewFeature.Name) + if err != nil { + return err // should return?? + } + // the return string of the views contain trailing new lines re := regexp.MustCompile(`\r?\n`) // TODO: maybe intentionally added new lines? - msg := re.ReplaceAllString(v_msg.ViewBuffer(), "") - name := re.ReplaceAllString(v_name.ViewBuffer(), "") - email := re.ReplaceAllString(v_email.ViewBuffer(), "") + msg := re.ReplaceAllString(vMsg.ViewBuffer(), "") + name := re.ReplaceAllString(vName.ViewBuffer(), "") + email := re.ReplaceAllString(vEmail.ViewBuffer(), "") if len(email) <= 0 { return errors.New("User email needs to be provided") } + err = git.CommitCommand(e, git.CommitOptions{ CommitMsg: msg, User: name, @@ -145,6 +159,7 @@ func (gui *Gui) submitCommitMessageView(g *gocui.Gui, v *gocui.View) error { if err != nil { return err } + return gui.closeCommitMessageView(g, v) } diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index c839860..0c050bd 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -100,6 +100,16 @@ func (gui *Gui) Run() error { if err != nil { return err } + defer g.Close() + + gui.g = g + g.Highlight = true + g.SelFgColor = gocui.ColorGreen + + // If InputEsc is true, when ESC sequence is in the buffer and it doesn't + // match any known sequence, ESC means KeyEsc. + g.InputEsc = true + g.SetManagerFunc(gui.layout) defer g.Close() gui.g = g @@ -213,14 +223,12 @@ func (gui *Gui) layout(g *gocui.Gui) error { // focus to next view func (gui *Gui) nextMainView(g *gocui.Gui, v *gocui.View) error { - err := gui.nextViewOfGroup(g, v, mainViews) - return err + return gui.nextViewOfGroup(g, v, mainViews) } // focus to previous view func (gui *Gui) previousMainView(g *gocui.Gui, v *gocui.View) error { - err := gui.previousViewOfGroup(g, v, mainViews) - return err + return gui.previousViewOfGroup(g, v, mainViews) } // quit from the gui and end its loop diff --git a/pkg/gui/mainview.go b/pkg/gui/mainview.go index 4661f43..d07a742 100644 --- a/pkg/gui/mainview.go +++ b/pkg/gui/mainview.go @@ -17,20 +17,18 @@ func (gui *Gui) fillMain(g *gocui.Gui) error { if err != nil { return err } + // if there is still a loading screen we better get rid of it - err = g.DeleteView(loadingViewFeature.Name) - if err != nil { + if err := g.DeleteView(loadingViewFeature.Name); err != nil { return err } - if _, err = gui.setCurrentViewOnTop(g, mainViewFeature.Name); err != nil { + if _, err := gui.setCurrentViewOnTop(g, mainViewFeature.Name); err != nil { return err } + // Sort by name is default behavior as expected, so it handles initial // rendering of the main view - if err = gui.sortByName(g, v); err != nil { - return err - } - return nil + return gui.sortByName(g, v) }) return nil } diff --git a/pkg/gui/sideviews.go b/pkg/gui/sideviews.go index a602b2e..fcd7f13 100644 --- a/pkg/gui/sideviews.go +++ b/pkg/gui/sideviews.go @@ -17,20 +17,20 @@ func (gui *Gui) renderSideViews(e *git.RepoEntity) error { if e == nil { return nil } - var err error - if err = gui.renderRemotes(e); err != nil { + + if err := gui.renderRemotes(e); err != nil { return err } - if err = gui.renderBranch(e); err != nil { + if err := gui.renderBranch(e); err != nil { return err } - if err = gui.renderRemoteBranches(e); err != nil { + if err := gui.renderRemoteBranches(e); err != nil { return err } - if err = gui.renderCommits(e); err != nil { + if err := gui.renderCommits(e); err != nil { return err } - return err + return nil } // updates the remotesview for given entity @@ -104,8 +104,8 @@ func (gui *Gui) renderBranch(e *git.RepoEntity) error { } fmt.Fprintln(out, tab+b.Name) } - err = gui.smartAnchorRelativeToLine(out, currentindex, totalbranches) - return err + + return gui.smartAnchorRelativeToLine(out, currentindex, totalbranches) } // updates the commitsview for given entity @@ -126,10 +126,7 @@ func (gui *Gui) renderCommits(e *git.RepoEntity) error { } fmt.Fprintln(out, tab+commitLabel(c)) } - if err = gui.smartAnchorRelativeToLine(out, currentindex, totalcommits); err != nil { - return err - } - return err + return gui.smartAnchorRelativeToLine(out, currentindex, totalcommits) } // cursor down variant for sideviews diff --git a/pkg/gui/stashview.go b/pkg/gui/stashview.go index 81de8f0..0afc983 100644 --- a/pkg/gui/stashview.go +++ b/pkg/gui/stashview.go @@ -56,10 +56,11 @@ func (gui *Gui) popStash(g *gocui.Gui, v *gocui.View) error { } } // since the pop is a func of stashed item, we need to refresh entity here - e.Refresh() + if err := e.Refresh(); err != nil { + return err + } - err = refreshAllStatusView(g, e, true) - return err + return refreshAllStatusView(g, e, true) } // refresh the main view and re-render the repository representations diff --git a/pkg/gui/statusview.go b/pkg/gui/statusview.go index cc6ef51..b66d49f 100644 --- a/pkg/gui/statusview.go +++ b/pkg/gui/statusview.go @@ -32,65 +32,61 @@ func (gui *Gui) openStatusView(g *gocui.Gui, v *gocui.View) error { return nil } -func reloadFiles(e *git.RepoEntity) (err error) { - stagedFiles, unstagedFiles, err = populateFileLists(e) +func reloadFiles(e *git.RepoEntity) error { + _, _, err := populateFileLists(e) return err } // focus to next view func (gui *Gui) nextStatusView(g *gocui.Gui, v *gocui.View) error { - err := gui.nextViewOfGroup(g, v, statusViews) - return err + return gui.nextViewOfGroup(g, v, statusViews) } // focus to previous view func (gui *Gui) previousStatusView(g *gocui.Gui, v *gocui.View) error { - err := gui.previousViewOfGroup(g, v, statusViews) - return err + return gui.previousViewOfGroup(g, v, statusViews) } // moves the cursor downwards for the main view and if it goes to bottom it // prevents from going further func (gui *Gui) statusCursorDown(g *gocui.Gui, v *gocui.View) error { - if v != nil { - cx, cy := v.Cursor() - ox, oy := v.Origin() - ly := len(v.BufferLines()) - 2 // why magic number? have no idea + if v == nil { + return nil + } - // if we are at the end we just return - if cy+oy == ly { - return nil - } - if err := v.SetCursor(cx, cy+1); err != nil { + cx, cy := v.Cursor() + ox, oy := v.Origin() + ly := len(v.BufferLines()) - 2 // why magic number? have no idea - if err := v.SetOrigin(ox, oy+1); err != nil { - return err - } - } - e := gui.getSelectedRepository() - if err := refreshStatusView(v.Name(), g, e, false); err != nil { + // if we are at the end we just return + if cy+oy == ly { + return nil + } + if err := v.SetCursor(cx, cy+1); err != nil { + + if err := v.SetOrigin(ox, oy+1); err != nil { return err } } - return nil + e := gui.getSelectedRepository() + return refreshStatusView(v.Name(), g, e, false) } // moves the cursor upwards for the main view func (gui *Gui) statusCursorUp(g *gocui.Gui, v *gocui.View) error { - if v != nil { - ox, oy := v.Origin() - cx, cy := v.Cursor() - if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 { - if err := v.SetOrigin(ox, oy-1); err != nil { - return err - } - } - e := gui.getSelectedRepository() - if err := refreshStatusView(v.Name(), g, e, false); err != nil { + if v == nil { + return nil + } + + ox, oy := v.Origin() + cx, cy := v.Cursor() + if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 { + if err := v.SetOrigin(ox, oy-1); err != nil { return err } } - return nil + e := gui.getSelectedRepository() + return refreshStatusView(v.Name(), g, e, false) } // header og the status layout @@ -146,21 +142,16 @@ func refreshStatusView(viewName string, g *gocui.Gui, e *git.RepoEntity, reload if reload { reloadFiles(e) } + var err error switch viewName { case stageViewFeature.Name: - if err := refreshStagedView(g); err != nil { - return err - } + err = refreshStagedView(g) case unstageViewFeature.Name: - if err := refreshUnstagedView(g); err != nil { - return err - } + err = refreshUnstagedView(g) case stashViewFeature.Name: - if err := refreshStashView(g, e); err != nil { - return err - } + err = refreshStashView(g, e) } - return nil + return err } func refreshAllStatusView(g *gocui.Gui, e *git.RepoEntity, reload bool) error { diff --git a/pkg/gui/unstagedview.go b/pkg/gui/unstagedview.go index dd2fe6d..8d152c9 100644 --- a/pkg/gui/unstagedview.go +++ b/pkg/gui/unstagedview.go @@ -18,8 +18,8 @@ func (gui *Gui) openUnStagedView(g *gocui.Gui) error { } v.Title = unstageViewFeature.Title } - err = refreshUnstagedView(g) - return err + + return refreshUnstagedView(g) } func (gui *Gui) addChanges(g *gocui.Gui, v *gocui.View) error { @@ -33,8 +33,8 @@ func (gui *Gui) addChanges(g *gocui.Gui, v *gocui.View) error { if err := git.Add(e, unstagedFiles[cy+oy], git.AddOptions{}); err != nil { return err } - err := refreshAllStatusView(g, e, true) - return err + + return refreshAllStatusView(g, e, true) } func (gui *Gui) addAllChanges(g *gocui.Gui, v *gocui.View) error { @@ -42,8 +42,8 @@ func (gui *Gui) addAllChanges(g *gocui.Gui, v *gocui.View) error { if err := git.AddAll(e, git.AddOptions{}); err != nil { return err } - err := refreshAllStatusView(g, e, true) - return err + + return refreshAllStatusView(g, e, true) } // refresh the main view and re-render the repository representations diff --git a/pkg/gui/util-common.go b/pkg/gui/util-common.go index 3f6be1a..92c49b9 100644 --- a/pkg/gui/util-common.go +++ b/pkg/gui/util-common.go @@ -28,8 +28,8 @@ func (gui *Gui) nextViewOfGroup(g *gocui.Gui, v *gocui.View, group []viewFeature }).Warn("View cannot be focused.") return nil } - gui.updateKeyBindingsView(g, focusedViewName) - return nil + + return gui.updateKeyBindingsView(g, focusedViewName) } // focus to previous view @@ -54,29 +54,26 @@ func (gui *Gui) previousViewOfGroup(g *gocui.Gui, v *gocui.View, group []viewFea }).Warn("View cannot be focused.") return nil } - gui.updateKeyBindingsView(g, focusedViewName) - return nil + + return gui.updateKeyBindingsView(g, focusedViewName) } // siwtch the app's mode to fetch func (gui *Gui) switchToFetchMode(g *gocui.Gui, v *gocui.View) error { gui.State.Mode = fetchMode - gui.updateKeyBindingsView(g, mainViewFeature.Name) - return nil + return gui.updateKeyBindingsView(g, mainViewFeature.Name) } // siwtch the app's mode to pull func (gui *Gui) switchToPullMode(g *gocui.Gui, v *gocui.View) error { gui.State.Mode = pullMode - gui.updateKeyBindingsView(g, mainViewFeature.Name) - return nil + return gui.updateKeyBindingsView(g, mainViewFeature.Name) } // siwtch the app's mode to merge func (gui *Gui) switchToMergeMode(g *gocui.Gui, v *gocui.View) error { gui.State.Mode = mergeMode - gui.updateKeyBindingsView(g, mainViewFeature.Name) - return nil + return gui.updateKeyBindingsView(g, mainViewFeature.Name) } // bring the view on the top by its name diff --git a/pkg/gui/util-textstyle.go b/pkg/gui/util-textstyle.go index fbe2b64..95c8de7 100644 --- a/pkg/gui/util-textstyle.go +++ b/pkg/gui/util-textstyle.go @@ -51,10 +51,8 @@ var ( // this function handles the render and representation of the repository // TODO: cleanup is required, right now it looks too complicated func (gui *Gui) repositoryLabel(e *git.RepoEntity) string { - suffix := "" - prefix := "" - repoName := "" + var prefix string if e.Branch.Pushables != "?" { prefix = prefix + pushable + ws + e.Branch.Pushables + ws + pullable + ws + e.Branch.Pullables @@ -63,6 +61,7 @@ func (gui *Gui) repositoryLabel(e *git.RepoEntity) string { ws + pullable + ws + yellow.Sprint(e.Branch.Pullables) } + var repoName string se := gui.getSelectedRepository() if se == e { prefix = prefix + selectionIndicator @@ -82,6 +81,7 @@ func (gui *Gui) repositoryLabel(e *git.RepoEntity) string { prefix = prefix + ws } + var suffix string // rendering the satus according to repository's state if e.State() == git.Queued { if inQueue, ty := gui.State.Queue.IsInTheQueue(e); inQueue { @@ -127,10 +127,9 @@ func commitLabel(c *git.Commit) string { } // limit the text length for visual concerns -func adjustTextLength(text string, maxLength int) (adjusted string) { +func adjustTextLength(text string, maxLength int) string { if len(text) > maxLength { - adjusted := text[:maxLength-2] + ".." - return adjusted + return text[:maxLength-2] + ".." } return text } diff --git a/pkg/helpers/utils.go b/pkg/helpers/utils.go index 1a3f411..35de2c3 100644 --- a/pkg/helpers/utils.go +++ b/pkg/helpers/utils.go @@ -7,7 +7,7 @@ import ( ) var characterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") -var src = rand.NewSource(time.Now().UnixNano()) +var r = rand.New(rand.NewSource(time.Now().UnixNano())) // TrimTrailingNewline removes the trailing new line form a string. this method // is used mostly on outputs of a command @@ -30,7 +30,7 @@ func Min(x, y int) int { func RandomString(n int) string { b := make([]rune, n) for i := range b { - b[i] = characterRunes[rand.Intn(len(characterRunes))] + b[i] = characterRunes[r.Intn(len(characterRunes))] } return string(b) } diff --git a/pkg/helpers/utils_test.go b/pkg/helpers/utils_test.go new file mode 100644 index 0000000..a6a56a4 --- /dev/null +++ b/pkg/helpers/utils_test.go @@ -0,0 +1,11 @@ +package helpers + +import "testing" + +func TestRandomString(t *testing.T) { + stringLength := 8 + randString := RandomString(stringLength) + if len(randString) != stringLength { + t.Errorf("The length of the string should be equal.") + } +} |
