diff options
| author | İbrahim Serdar Açıkgöz <serdaracikgoz86@gmail.com> | 2018-12-18 18:42:52 +0300 |
|---|---|---|
| committer | İbrahim Serdar Açıkgöz <serdaracikgoz86@gmail.com> | 2018-12-18 18:42:52 +0300 |
| commit | 6613adb7b499eca9e416393368099448ad35c9c8 (patch) | |
| tree | 4ccfaf6ab8983951a43e6702df12dd308eb46bc0 | |
| parent | repository construction revised (diff) | |
| download | gitbatch-6613adb7b499eca9e416393368099448ad35c9c8.tar.gz | |
go style refactoring on variable names and minor improvements
| -rw-r--r-- | pkg/git/authentication.go | 10 | ||||
| -rw-r--r-- | pkg/git/branch.go | 82 | ||||
| -rw-r--r-- | pkg/git/cmd-add.go | 18 | ||||
| -rw-r--r-- | pkg/git/cmd-commit.go | 22 | ||||
| -rw-r--r-- | pkg/git/cmd-config.go | 28 | ||||
| -rw-r--r-- | pkg/git/cmd-diff.go | 18 | ||||
| -rw-r--r-- | pkg/git/cmd-fetch.go | 32 | ||||
| -rw-r--r-- | pkg/git/cmd-merge.go | 9 | ||||
| -rw-r--r-- | pkg/git/cmd-pull.go | 24 | ||||
| -rw-r--r-- | pkg/git/cmd-reset.go | 22 | ||||
| -rw-r--r-- | pkg/git/cmd-rev-list.go | 4 | ||||
| -rw-r--r-- | pkg/git/cmd-stash.go | 20 | ||||
| -rw-r--r-- | pkg/git/cmd-status.go | 22 | ||||
| -rw-r--r-- | pkg/git/cmd.go | 4 | ||||
| -rw-r--r-- | pkg/git/commit.go | 80 | ||||
| -rw-r--r-- | pkg/git/file.go | 6 | ||||
| -rw-r--r-- | pkg/git/job-queue.go | 32 | ||||
| -rw-r--r-- | pkg/git/job.go | 34 | ||||
| -rw-r--r-- | pkg/git/remote.go | 54 | ||||
| -rw-r--r-- | pkg/git/remotebranch.go | 47 | ||||
| -rw-r--r-- | pkg/gui/keybindings.go | 12 | ||||
| -rw-r--r-- | pkg/gui/sideviews.go | 28 |
22 files changed, 267 insertions, 341 deletions
diff --git a/pkg/git/authentication.go b/pkg/git/authentication.go index def1f99..3e834ec 100644 --- a/pkg/git/authentication.go +++ b/pkg/git/authentication.go @@ -7,7 +7,9 @@ import ( // Credentials holds user credentials to authenticate and authorize while // communicating with remote if required type Credentials struct { - User string + // User is the user id for authentication + User string + // Password is the secret information required for authetntication Password string } @@ -17,8 +19,10 @@ var ( authProtocolSSH = "ssh" ) -func (entity *RepoEntity) authProtocol(remote *Remote) (p string, err error) { - u, err := url.Parse(remote.URL[0]) +// authentication protocol returns the type of protocol for given remote's URL +// various auth protocols require different kind of authentication +func authProtocol(r *Remote) (p string, err error) { + u, err := url.Parse(r.URL[0]) if err != nil { return p, err } diff --git a/pkg/git/branch.go b/pkg/git/branch.go index 7e78059..5be8e16 100644 --- a/pkg/git/branch.go +++ b/pkg/git/branch.go @@ -25,19 +25,19 @@ type Branch struct { // search for branches in go-git way. It is useful to do so that checkout and // checkout error handling can be handled by code rather than struggling with // git cammand and its output -func (entity *RepoEntity) loadLocalBranches() error { +func (e *RepoEntity) loadLocalBranches() error { lbs := make([]*Branch, 0) - branches, err := entity.Repository.Branches() + bs, err := e.Repository.Branches() if err != nil { log.Warn("Cannot load branches " + err.Error()) return err } - defer branches.Close() - headRef, _ := entity.Repository.Head() - branches.ForEach(func(b *plumbing.Reference) error { + defer bs.Close() + headRef, _ := e.Repository.Head() + bs.ForEach(func(b *plumbing.Reference) error { if b.Type() == plumbing.HashReference { var push, pull string - pushables, err := RevList(entity, RevListOptions{ + pushables, err := RevList(e, RevListOptions{ Ref1: "@{u}", Ref2: "HEAD", }) @@ -46,7 +46,7 @@ func (entity *RepoEntity) loadLocalBranches() error { } else { push = strconv.Itoa(len(pushables)) } - pullables, err := RevList(entity, RevListOptions{ + pullables, err := RevList(e, RevListOptions{ Ref1: "HEAD", Ref2: "@{u}", }) @@ -55,7 +55,7 @@ func (entity *RepoEntity) loadLocalBranches() error { } else { pull = strconv.Itoa(len(pullables)) } - clean := entity.isClean() + clean := e.isClean() branch := &Branch{ Name: b.Name().Short(), Reference: b, @@ -63,54 +63,45 @@ func (entity *RepoEntity) loadLocalBranches() error { Pullables: pull, Clean: clean, } - if b.Hash() == headRef.Hash() { - entity.Branch = branch + if b.Name() == headRef.Name() { + e.Branch = branch } lbs = append(lbs, branch) } return nil }) - entity.Branches = lbs + e.Branches = lbs return err } // NextBranch checkouts the next branch -func (entity *RepoEntity) NextBranch() *Branch { - currentBranchIndex := entity.findCurrentBranchIndex() - if currentBranchIndex == len(entity.Branches)-1 { - return entity.Branches[0] - } - return entity.Branches[currentBranchIndex+1] +func (e *RepoEntity) NextBranch() *Branch { + return e.Branches[(e.currentBranchIndex()+1)%len(e.Branches)] } // PreviousBranch checkouts the previous branch -func (entity *RepoEntity) PreviousBranch() *Branch { - currentBranchIndex := entity.findCurrentBranchIndex() - if currentBranchIndex == 0 { - return entity.Branches[len(entity.Branches)-1] - } - return entity.Branches[currentBranchIndex-1] +func (e *RepoEntity) PreviousBranch() *Branch { + return e.Branches[(len(e.Branches)+e.currentBranchIndex()-1)%len(e.Branches)] } // returns the active branch index -func (entity *RepoEntity) findCurrentBranchIndex() int { - currentBranch := entity.Branch - currentBranchIndex := 0 - for i, lbs := range entity.Branches { - if lbs.Name == currentBranch.Name { - currentBranchIndex = i +func (e *RepoEntity) currentBranchIndex() int { + bix := 0 + for i, lbs := range e.Branches { + if lbs.Name == e.Branch.Name { + bix = i } } - return currentBranchIndex + return bix } // Checkout to given branch. If any errors occur, the method returns it instead // of returning nil -func (entity *RepoEntity) Checkout(branch *Branch) error { - if branch.Name == entity.Branch.Name { +func (e *RepoEntity) Checkout(branch *Branch) error { + if branch.Name == e.Branch.Name { return nil } - w, err := entity.Repository.Worktree() + w, err := e.Repository.Worktree() if err != nil { log.Warn("Cannot get work tree " + err.Error()) return err @@ -122,29 +113,24 @@ func (entity *RepoEntity) Checkout(branch *Branch) error { return err } - // after checking out we need to refresh some values such as; - entity.loadCommits() - entity.Commit = entity.Commits[0] - entity.Branch = branch - // make this conditional on global scale - err = entity.Remote.SyncBranches(branch.Name) - return entity.Refresh() + err = e.Remote.SyncBranches(branch.Name) + return e.Refresh() } // checking the branch if it has any changes from its head revision. Initially // I implemented this with go-git but it was incredibly slow and there is also // an issue about it: https://github.com/src-d/go-git/issues/844 -func (entity *RepoEntity) isClean() bool { - status := entity.StatusWithGit() - status = helpers.TrimTrailingNewline(status) - if status != "?" { - verbose := strings.Split(status, "\n") - lastLine := verbose[len(verbose)-1] +func (e *RepoEntity) isClean() bool { + s := e.StatusWithGit() + s = helpers.TrimTrailingNewline(s) + if s != "?" { + vs := strings.Split(s, "\n") + line := vs[len(vs)-1] // earlier versions of git returns "working directory clean" instead of //"working tree clean" message - if strings.Contains(lastLine, "working tree clean") || - strings.Contains(lastLine, "working directory clean") { + if strings.Contains(line, "working tree clean") || + strings.Contains(line, "working directory clean") { return true } } diff --git a/pkg/git/cmd-add.go b/pkg/git/cmd-add.go index 649bad2..e7faff2 100644 --- a/pkg/git/cmd-add.go +++ b/pkg/git/cmd-add.go @@ -25,31 +25,31 @@ type AddOptions struct { } // Add is a wrapper function for "git add" command -func Add(entity *RepoEntity, file *File, option AddOptions) error { +func Add(e *RepoEntity, file *File, option AddOptions) error { addCmdMode = addCmdModeNative if option.Update || option.Force || option.DryRun { addCmdMode = addCmdModeLegacy } switch addCmdMode { case addCmdModeLegacy: - err := addWithGit(entity, file, option) + err := addWithGit(e, file, option) return err case addCmdModeNative: - err := addWithGoGit(entity, file) + err := addWithGoGit(e, file) return err } return errors.New("Unhandled add operation") } // AddAll function is the wrapper of "git add ." command -func AddAll(entity *RepoEntity, option AddOptions) error { +func AddAll(e *RepoEntity, option AddOptions) error { args := make([]string, 0) args = append(args, addCommand) if option.DryRun { args = append(args, "--dry-run") } args = append(args, ".") - out, err := GenericGitCommandWithOutput(entity.AbsPath, args) + out, err := GenericGitCommandWithOutput(e.AbsPath, args) if err != nil { log.Warn("Error while add command") return errors.New(out + "\n" + err.Error()) @@ -57,7 +57,7 @@ func AddAll(entity *RepoEntity, option AddOptions) error { return nil } -func addWithGit(entity *RepoEntity, file *File, option AddOptions) error { +func addWithGit(e *RepoEntity, file *File, option AddOptions) error { args := make([]string, 0) args = append(args, addCommand) args = append(args, file.Name) @@ -70,7 +70,7 @@ func addWithGit(entity *RepoEntity, file *File, option AddOptions) error { if option.DryRun { args = append(args, "--dry-run") } - out, err := GenericGitCommandWithOutput(entity.AbsPath, args) + out, err := GenericGitCommandWithOutput(e.AbsPath, args) if err != nil { log.Warn("Error while add command") return errors.New(out + "\n" + err.Error()) @@ -78,8 +78,8 @@ func addWithGit(entity *RepoEntity, file *File, option AddOptions) error { return nil } -func addWithGoGit(entity *RepoEntity, file *File) error { - w, err := entity.Repository.Worktree() +func addWithGoGit(e *RepoEntity, file *File) error { + w, err := e.Repository.Worktree() if err != nil { return err } diff --git a/pkg/git/cmd-commit.go b/pkg/git/cmd-commit.go index 8ea5bfe..b06b6fb 100644 --- a/pkg/git/cmd-commit.go +++ b/pkg/git/cmd-commit.go @@ -28,24 +28,24 @@ type CommitOptions struct { } // CommitCommand -func CommitCommand(entity *RepoEntity, options CommitOptions) (err error) { +func CommitCommand(e *RepoEntity, options CommitOptions) (err error) { // here we configure commit operation // default mode is go-git (this may be configured) commitCmdMode = commitCmdModeNative switch commitCmdMode { case commitCmdModeLegacy: - err = commitWithGit(entity, options) + err = commitWithGit(e, options) return err case commitCmdModeNative: - err = commitWithGoGit(entity, options) + err = commitWithGoGit(e, options) return err } return errors.New("Unhandled commit operation") } // commitWithGit is simply a bare git commit -m <msg> command which is flexible -func commitWithGit(entity *RepoEntity, options CommitOptions) (err error) { +func commitWithGit(e *RepoEntity, options CommitOptions) (err error) { args := make([]string, 0) args = append(args, commitCommand) args = append(args, "-m") @@ -53,18 +53,17 @@ func commitWithGit(entity *RepoEntity, options CommitOptions) (err error) { if len(options.CommitMsg) > 0 { args = append(args, options.CommitMsg) } - if err := GenericGitCommand(entity.AbsPath, args); err != nil { + if err := GenericGitCommand(e.AbsPath, args); err != nil { log.Warn("Error at git command (commit)") return err } // till this step everything should be ok - err = entity.Refresh() - return err + return e.Refresh() } // commitWithGoGit is the primary commit method -func commitWithGoGit(entity *RepoEntity, options CommitOptions) (err error) { - config, err := entity.Repository.Config() +func commitWithGoGit(e *RepoEntity, options CommitOptions) (err error) { + config, err := e.Repository.Config() if err != nil { return err } @@ -78,7 +77,7 @@ func commitWithGoGit(entity *RepoEntity, options CommitOptions) (err error) { }, } - w, err := entity.Repository.Worktree() + w, err := e.Repository.Worktree() if err != nil { return err } @@ -88,6 +87,5 @@ func commitWithGoGit(entity *RepoEntity, options CommitOptions) (err error) { return err } // till this step everything should be ok - err = entity.Refresh() - return err + return e.Refresh() } diff --git a/pkg/git/cmd-config.go b/pkg/git/cmd-config.go index dfbf661..06ba74b 100644 --- a/pkg/git/cmd-config.go +++ b/pkg/git/cmd-config.go @@ -34,24 +34,24 @@ const ( ) // Config -func Config(entity *RepoEntity, options ConfigOptions) (value string, err error) { +func Config(e *RepoEntity, options ConfigOptions) (value string, err error) { // here we configure config operation // default mode is go-git (this may be configured) configCmdMode = configCmdModeLegacy switch configCmdMode { case configCmdModeLegacy: - value, err = configWithGit(entity, options) + value, err = configWithGit(e, options) return value, err case configCmdModeNative: - value, err = configWithGoGit(entity, options) + value, err = configWithGoGit(e, options) return value, err } return value, errors.New("Unhandled config operation") } // configWithGit is simply a bare git commit -m <msg> command which is flexible -func configWithGit(entity *RepoEntity, options ConfigOptions) (value string, err error) { +func configWithGit(e *RepoEntity, options ConfigOptions) (value string, err error) { args := make([]string, 0) args = append(args, configCommand) if len(string(options.Site)) > 0 { @@ -60,7 +60,7 @@ func configWithGit(entity *RepoEntity, options ConfigOptions) (value string, err args = append(args, "--get") args = append(args, options.Section+"."+options.Option) // parse options to command line arguments - out, err := GenericGitCommandWithOutput(entity.AbsPath, args) + out, err := GenericGitCommandWithOutput(e.AbsPath, args) if err != nil { return out, err } @@ -69,25 +69,23 @@ func configWithGit(entity *RepoEntity, options ConfigOptions) (value string, err } // commitWithGoGit is the primary commit method -func configWithGoGit(entity *RepoEntity, options ConfigOptions) (value string, err error) { +func configWithGoGit(e *RepoEntity, options ConfigOptions) (value string, err error) { // TODO: add global search - config, err := entity.Repository.Config() + config, err := e.Repository.Config() if err != nil { return value, err } - value = config.Raw.Section(options.Section).Option(options.Option) - return value, nil + return config.Raw.Section(options.Section).Option(options.Option), nil } // AddConfig -func AddConfig(entity *RepoEntity, options ConfigOptions, value string) (err error) { - err = addConfigWithGit(entity, options, value) - return err +func AddConfig(e *RepoEntity, options ConfigOptions, value string) (err error) { + return addConfigWithGit(e, options, value) } // addConfigWithGit is simply a bare git config --add <option> command which is flexible -func addConfigWithGit(entity *RepoEntity, options ConfigOptions, value string) (err error) { +func addConfigWithGit(e *RepoEntity, options ConfigOptions, value string) (err error) { args := make([]string, 0) args = append(args, configCommand) if len(string(options.Site)) > 0 { @@ -98,10 +96,10 @@ func addConfigWithGit(entity *RepoEntity, options ConfigOptions, value string) ( if len(value) > 0 { args = append(args, value) } - if err := GenericGitCommand(entity.AbsPath, args); err != nil { + if err := GenericGitCommand(e.AbsPath, args); err != nil { log.Warn("Error at git command (config)") return err } // till this step everything should be ok - return entity.Refresh() + return e.Refresh() } diff --git a/pkg/git/cmd-diff.go b/pkg/git/cmd-diff.go index 3fe0bd6..f9fcb3f 100644 --- a/pkg/git/cmd-diff.go +++ b/pkg/git/cmd-diff.go @@ -18,36 +18,36 @@ var ( // Diff is a wrapper function for "git diff" command // Diff function returns the diff to previous commit detail of the given has // of a specific commit -func Diff(entity *RepoEntity, hash string) (diff string, err error) { +func Diff(e *RepoEntity, hash string) (diff string, err error) { diffCmdMode = diffCmdModeNative switch diffCmdMode { case diffCmdModeLegacy: - return diffWithGit(entity, hash) + return diffWithGit(e, hash) case diffCmdModeNative: - return diffWithGoGit(entity, hash) + return diffWithGoGit(e, hash) } return diff, errors.New("Unhandled diff operation") } -func diffWithGit(entity *RepoEntity, hash string) (diff string, err error) { +func diffWithGit(e *RepoEntity, hash string) (diff string, err error) { return diff, nil } -func diffWithGoGit(entity *RepoEntity, hash string) (diff string, err error) { +func diffWithGoGit(e *RepoEntity, hash string) (diff string, err error) { currentCommitIndex := 0 - for i, cs := range entity.Commits { + for i, cs := range e.Commits { if cs.Hash == hash { currentCommitIndex = i } } - if len(entity.Commits)-currentCommitIndex <= 1 { + if len(e.Commits)-currentCommitIndex <= 1 { return "there is no diff", nil } // maybe we dont need to log the repo again? - commits, err := entity.Repository.Log(&git.LogOptions{ - From: plumbing.NewHash(entity.Commit.Hash), + commits, err := e.Repository.Log(&git.LogOptions{ + From: plumbing.NewHash(e.Commit.Hash), Order: git.LogOrderCommitterTime, }) if err != nil { diff --git a/pkg/git/cmd-fetch.go b/pkg/git/cmd-fetch.go index 73c29d1..e7a60f7 100644 --- a/pkg/git/cmd-fetch.go +++ b/pkg/git/cmd-fetch.go @@ -39,7 +39,7 @@ type FetchOptions struct { // Fetch branches refs from one or more other repositories, along with the // objects necessary to complete their histories -func Fetch(entity *RepoEntity, options FetchOptions) (err error) { +func Fetch(e *RepoEntity, options FetchOptions) (err error) { // here we configure fetch operation // default mode is go-git (this may be configured) fetchCmdMode = fetchCmdModeNative @@ -50,18 +50,18 @@ func Fetch(entity *RepoEntity, options FetchOptions) (err error) { } switch fetchCmdMode { case fetchCmdModeLegacy: - err = fetchWithGit(entity, options) + err = fetchWithGit(e, options) return err case fetchCmdModeNative: // this should be the refspec as default, let's give it a try // TODO: Fix for quick mode, maybe better read config file var refspec string - if entity.Branch == nil { + if e.Branch == nil { refspec = "+refs/heads/*:refs/remotes/origin/*" } else { - refspec = "+" + "refs/heads/" + entity.Branch.Name + ":" + "/refs/remotes/" + entity.Remote.Branch.Name + refspec = "+" + "refs/heads/" + e.Branch.Name + ":" + "/refs/remotes/" + e.Remote.Branch.Name } - err = fetchWithGoGit(entity, options, refspec) + err = fetchWithGoGit(e, options, refspec) return err } return nil @@ -70,7 +70,7 @@ func Fetch(entity *RepoEntity, options FetchOptions) (err error) { // fetchWithGit is simply a bare git fetch <remote> command which is flexible // for complex operations, but on the other hand, it ties the app to another // tool. To avoid that, using native implementation is preferred. -func fetchWithGit(entity *RepoEntity, options FetchOptions) (err error) { +func fetchWithGit(e *RepoEntity, options FetchOptions) (err error) { args := make([]string, 0) args = append(args, fetchCommand) // parse options to command line arguments @@ -86,13 +86,13 @@ func fetchWithGit(entity *RepoEntity, options FetchOptions) (err error) { if options.DryRun { args = append(args, "--dry-run") } - if err := GenericGitCommand(entity.AbsPath, args); err != nil { + if err := GenericGitCommand(e.AbsPath, args); err != nil { log.Warn("Error at git command (fetch)") return err } - entity.SetState(Success) + e.SetState(Success) // till this step everything should be ok - return entity.Refresh() + return e.Refresh() } // fetchWithGoGit is the primary fetch method and refspec is the main feature. @@ -101,7 +101,7 @@ func fetchWithGit(entity *RepoEntity, options FetchOptions) (err error) { // pattern for references on the remote side and <dst> is where those references // will be written locally. The + tells Git to update the reference even if it // isn’t a fast-forward. -func fetchWithGoGit(entity *RepoEntity, options FetchOptions, refspec string) (err error) { +func fetchWithGoGit(e *RepoEntity, options FetchOptions, refspec string) (err error) { opt := &git.FetchOptions{ RemoteName: options.RemoteName, RefSpecs: []config.RefSpec{config.RefSpec(refspec)}, @@ -109,7 +109,7 @@ func fetchWithGoGit(entity *RepoEntity, options FetchOptions, refspec string) (e } // if any credential is given, let's add it to the git.FetchOptions if len(options.Credentials.User) > 0 { - protocol, err := entity.authProtocol(entity.Remote) + protocol, err := authProtocol(e.Remote) if err != nil { return err } @@ -123,7 +123,7 @@ func fetchWithGoGit(entity *RepoEntity, options FetchOptions, refspec string) (e } } - err = entity.Repository.Fetch(opt) + err = e.Repository.Fetch(opt) if err != nil { if err == git.NoErrAlreadyUpToDate { // Already up-to-date @@ -131,10 +131,10 @@ func fetchWithGoGit(entity *RepoEntity, options FetchOptions, refspec string) (e // TODO: submit a PR for this kind of error, this type of catch is lame } else if strings.Contains(err.Error(), "couldn't find remote ref") { // we dont have remote ref, so lets pull other things.. maybe it'd be useful - rp := entity.Remote.RefSpecs[0] + rp := e.Remote.RefSpecs[0] if fetchTryCount < fetchMaxTry { fetchTryCount++ - fetchWithGoGit(entity, options, rp) + fetchWithGoGit(e, options, rp) } else { return err } @@ -147,7 +147,7 @@ func fetchWithGoGit(entity *RepoEntity, options FetchOptions, refspec string) (e } } - entity.SetState(Success) + e.SetState(Success) // till this step everything should be ok - return entity.Refresh() + return e.Refresh() } diff --git a/pkg/git/cmd-merge.go b/pkg/git/cmd-merge.go index 69b21af..e89761d 100644 --- a/pkg/git/cmd-merge.go +++ b/pkg/git/cmd-merge.go @@ -18,7 +18,7 @@ type MergeOptions struct { // Merge incorporates changes from the named commits or branches into the // current branch -func Merge(entity *RepoEntity, options MergeOptions) error { +func Merge(e *RepoEntity, options MergeOptions) error { args := make([]string, 0) args = append(args, mergeCommand) if len(options.BranchName) > 0 { @@ -30,11 +30,10 @@ func Merge(entity *RepoEntity, options MergeOptions) error { if options.NoStat { args = append(args, "-n") } - if err := GenericGitCommand(entity.AbsPath, args); err != nil { + if err := GenericGitCommand(e.AbsPath, args); err != nil { log.Warn("Error while merging") return err } - entity.SetState(Success) - entity.Refresh() - return nil + e.SetState(Success) + return e.Refresh() } diff --git a/pkg/git/cmd-pull.go b/pkg/git/cmd-pull.go index 164d960..2c28d3a 100644 --- a/pkg/git/cmd-pull.go +++ b/pkg/git/cmd-pull.go @@ -34,7 +34,7 @@ type PullOptions struct { } // Pull ncorporates changes from a remote repository into the current branch. -func Pull(entity *RepoEntity, options PullOptions) (err error) { +func Pull(e *RepoEntity, options PullOptions) (err error) { // here we configure pull operation // default mode is go-git (this may be configured) pullCmdMode = pullCmdModeNative @@ -42,16 +42,16 @@ func Pull(entity *RepoEntity, options PullOptions) (err error) { switch pullCmdMode { case pullCmdModeLegacy: - err = pullWithGit(entity, options) + err = pullWithGit(e, options) return err case pullCmdModeNative: - err = pullWithGoGit(entity, options) + err = pullWithGoGit(e, options) return err } return nil } -func pullWithGit(entity *RepoEntity, options PullOptions) (err error) { +func pullWithGit(e *RepoEntity, options PullOptions) (err error) { args := make([]string, 0) args = append(args, pullCommand) // parse options to command line arguments @@ -61,15 +61,15 @@ func pullWithGit(entity *RepoEntity, options PullOptions) (err error) { if options.Force { args = append(args, "-f") } - if err := GenericGitCommand(entity.AbsPath, args); err != nil { + if err := GenericGitCommand(e.AbsPath, args); err != nil { log.Warn("Error at git command (pull)") return err } - entity.SetState(Success) - return entity.Refresh() + e.SetState(Success) + return e.Refresh() } -func pullWithGoGit(entity *RepoEntity, options PullOptions) (err error) { +func pullWithGoGit(e *RepoEntity, options PullOptions) (err error) { opt := &git.PullOptions{ RemoteName: options.RemoteName, SingleBranch: options.SingleBranch, @@ -81,7 +81,7 @@ func pullWithGoGit(entity *RepoEntity, options PullOptions) (err error) { } // if any credential is given, let's add it to the git.PullOptions if len(options.Credentials.User) > 0 { - protocol, err := entity.authProtocol(entity.Remote) + protocol, err := authProtocol(e.Remote) if err != nil { return err } @@ -94,7 +94,7 @@ func pullWithGoGit(entity *RepoEntity, options PullOptions) (err error) { return ErrInvalidAuthMethod } } - w, err := entity.Repository.Worktree() + w, err := e.Repository.Worktree() if err != nil { return err } @@ -111,6 +111,6 @@ func pullWithGoGit(entity *RepoEntity, options PullOptions) (err error) { return err } } - entity.SetState(Success) - return entity.Refresh() + e.SetState(Success) + return e.Refresh() } diff --git a/pkg/git/cmd-reset.go b/pkg/git/cmd-reset.go index b5e8a99..cc5f0f9 100644 --- a/pkg/git/cmd-reset.go +++ b/pkg/git/cmd-reset.go @@ -47,12 +47,12 @@ const ( ) // Reset is the wrapper of "git reset" command -func Reset(entity *RepoEntity, file *File, option ResetOptions) error { +func Reset(e *RepoEntity, file *File, option ResetOptions) error { resetCmdMode = resetCmdModeLegacy switch resetCmdMode { case resetCmdModeLegacy: - err := resetWithGit(entity, file, option) + err := resetWithGit(e, file, option) return err case resetCmdModeNative: @@ -60,7 +60,7 @@ func Reset(entity *RepoEntity, file *File, option ResetOptions) error { return errors.New("Unhandled reset operation") } -func resetWithGit(entity *RepoEntity, file *File, option ResetOptions) error { +func resetWithGit(e *RepoEntity, file *File, option ResetOptions) error { args := make([]string, 0) args = append(args, resetCommand) @@ -69,7 +69,7 @@ func resetWithGit(entity *RepoEntity, file *File, option ResetOptions) error { if len(option.Rtype) > 0 { args = append(args, "--"+string(option.Rtype)) } - out, err := GenericGitCommandWithOutput(entity.AbsPath, args) + out, err := GenericGitCommandWithOutput(e.AbsPath, args) if err != nil { log.Warn("Error while reset command") return errors.New(out + "\n" + err.Error()) @@ -78,27 +78,27 @@ func resetWithGit(entity *RepoEntity, file *File, option ResetOptions) error { } // ResetAll resets the changes in a repository, should be used wise -func ResetAll(entity *RepoEntity, option ResetOptions) error { +func ResetAll(e *RepoEntity, option ResetOptions) error { resetCmdMode = addCmdModeNative switch resetCmdMode { case resetCmdModeLegacy: - err := resetAllWithGit(entity, option) + err := resetAllWithGit(e, option) return err case resetCmdModeNative: - err := resetAllWithGoGit(entity, option) + err := resetAllWithGoGit(e, option) return err } return errors.New("Unhandled reset operation") } -func resetAllWithGit(entity *RepoEntity, option ResetOptions) error { +func resetAllWithGit(e *RepoEntity, option ResetOptions) error { args := make([]string, 0) args = append(args, resetCommand) if len(option.Rtype) > 0 { args = append(args, "--"+string(option.Rtype)) } - out, err := GenericGitCommandWithOutput(entity.AbsPath, args) + out, err := GenericGitCommandWithOutput(e.AbsPath, args) if err != nil { log.Warn("Error while add command") return errors.New(out + "\n" + err.Error()) @@ -106,8 +106,8 @@ func resetAllWithGit(entity *RepoEntity, option ResetOptions) error { return nil } -func resetAllWithGoGit(entity *RepoEntity, option ResetOptions) error { - w, err := entity.Repository.Worktree() +func resetAllWithGoGit(e *RepoEntity, option ResetOptions) error { + w, err := e.Repository.Worktree() if err != nil { return err } diff --git a/pkg/git/cmd-rev-list.go b/pkg/git/cmd-rev-list.go index 664d69f..e68e82a 100644 --- a/pkg/git/cmd-rev-list.go +++ b/pkg/git/cmd-rev-list.go @@ -19,14 +19,14 @@ type RevListOptions struct { // RevList returns the commit hashes that are links from the given commit(s). // The output is given in reverse chronological order by default. -func RevList(entity *RepoEntity, options RevListOptions) ([]string, error) { +func RevList(e *RepoEntity, options RevListOptions) ([]string, error) { args := make([]string, 0) args = append(args, revlistCommand) if len(options.Ref1) > 0 && len(options.Ref2) > 0 { arg1 := options.Ref1 + ".." + options.Ref2 args = append(args, arg1) } - out, err := GenericGitCommandWithOutput(entity.AbsPath, args) + out, err := GenericGitCommandWithOutput(e.AbsPath, args) if err != nil { log.Warn("Error while rev-list command") return []string{out}, err diff --git a/pkg/git/cmd-stash.go b/pkg/git/cmd-stash.go index 9d813a6..a786833 100644 --- a/pkg/git/cmd-stash.go +++ b/pkg/git/cmd-stash.go @@ -19,11 +19,11 @@ type StashedItem struct { EntityPath string } -func stashGet(entity *RepoEntity, option string) string { +func stashGet(e *RepoEntity, option string) string { args := make([]string, 0) args = append(args, stashCommand) args = append(args, option) - out, err := GenericGitCommandWithOutput(entity.AbsPath, args) + out, err := GenericGitCommandWithOutput(e.AbsPath, args) if err != nil { log.Warn("Error while stash command") return "?" @@ -31,9 +31,9 @@ func stashGet(entity *RepoEntity, option string) string { return out } -func (entity *RepoEntity) loadStashedItems() error { - entity.Stasheds = make([]*StashedItem, 0) - output := stashGet(entity, "list") +func (e *RepoEntity) loadStashedItems() error { + e.Stasheds = make([]*StashedItem, 0) + output := stashGet(e, "list") stashIDRegex := regexp.MustCompile(`stash@{[\d]+}:`) stashIDRegexInt := regexp.MustCompile(`[\d]+`) stashBranchRegex := regexp.MustCompile(`[\w]+: `) @@ -63,24 +63,24 @@ func (entity *RepoEntity) loadStashedItems() error { // trim hash desc := stashHashRegex.Split(trimmed, 2)[1][1:] - entity.Stasheds = append(entity.Stasheds, &StashedItem{ + e.Stasheds = append(e.Stasheds, &StashedItem{ StashID: i, BranchName: branchName, Hash: hash, Description: desc, - EntityPath: entity.AbsPath, + EntityPath: e.AbsPath, }) } return nil } // Stash is the wrapper of convetional "git stash" command -func (entity *RepoEntity) Stash() (output string, err error) { +func (e *RepoEntity) Stash() (output string, err error) { args := make([]string, 0) args = append(args, stashCommand) - output, err = GenericGitCommandWithErrorOutput(entity.AbsPath, args) - entity.Refresh() + output, err = GenericGitCommandWithErrorOutput(e.AbsPath, args) + e.Refresh() return output, err } diff --git a/pkg/git/cmd-status.go b/pkg/git/cmd-status.go index b7969e6..eec9b57 100644 --- a/pkg/git/cmd-status.go +++ b/pkg/git/cmd-status.go @@ -18,12 +18,12 @@ var ( statusCmdModeNative = "go-git" ) -func shortStatus(entity *RepoEntity, option string) string { +func shortStatus(e *RepoEntity, option string) string { args := make([]string, 0) args = append(args, statusCommand) args = append(args, option) args = append(args, "--short") - out, err := GenericGitCommandWithOutput(entity.AbsPath, args) + out, err := GenericGitCommandWithOutput(e.AbsPath, args) if err != nil { log.Warn("Error while status command") return "?" @@ -31,23 +31,23 @@ func shortStatus(entity *RepoEntity, option string) string { return out } -func Status(entity *RepoEntity) ([]*File, error) { +func Status(e *RepoEntity) ([]*File, error) { statusCmdMode = statusCmdModeNative switch statusCmdMode { case statusCmdModeLegacy: - return statusWithGit(entity) + return statusWithGit(e) case statusCmdModeNative: - return statusWithGoGit(entity) + return statusWithGoGit(e) } return nil, errors.New("Unhandled status operation") } // LoadFiles function simply commands a git status and collects output in a // structured way -func statusWithGit(entity *RepoEntity) ([]*File, error) { +func statusWithGit(e *RepoEntity) ([]*File, error) { files := make([]*File, 0) - output := shortStatus(entity, "--untracked-files=all") + output := shortStatus(e, "--untracked-files=all") if len(output) == 0 { return files, nil } @@ -60,7 +60,7 @@ func statusWithGit(entity *RepoEntity) ([]*File, error) { files = append(files, &File{ Name: path, - AbsPath: entity.AbsPath + string(os.PathSeparator) + path, + AbsPath: e.AbsPath + string(os.PathSeparator) + path, X: FileStatus(x), Y: FileStatus(y), }) @@ -69,9 +69,9 @@ func statusWithGit(entity *RepoEntity) ([]*File, error) { return files, nil } -func statusWithGoGit(entity *RepoEntity) ([]*File, error) { +func statusWithGoGit(e *RepoEntity) ([]*File, error) { files := make([]*File, 0) - w, err := entity.Repository.Worktree() + w, err := e.Repository.Worktree() if err != nil { return files, err } @@ -82,7 +82,7 @@ func statusWithGoGit(entity *RepoEntity) ([]*File, error) { for k, v := range s { files = append(files, &File{ Name: k, - AbsPath: entity.AbsPath + string(os.PathSeparator) + k, + AbsPath: e.AbsPath + string(os.PathSeparator) + k, X: FileStatus(v.Staging), Y: FileStatus(v.Worktree), }) diff --git a/pkg/git/cmd.go b/pkg/git/cmd.go index b78b501..a84f489 100644 --- a/pkg/git/cmd.go +++ b/pkg/git/cmd.go @@ -72,9 +72,9 @@ func GitShowDate(repoPath, hash string) string { } // StatusWithGit returns the plaintext short status of the repo -func (entity *RepoEntity) StatusWithGit() string { +func (e *RepoEntity) StatusWithGit() string { args := []string{"status"} - status, err := helpers.RunCommandWithOutput(entity.AbsPath, "git", args) + status, err := helpers.RunCommandWithOutput(e.AbsPath, "git", args) if err != nil { return "?" } diff --git a/pkg/git/commit.go b/pkg/git/commit.go index ef12890..716ccba 100644 --- a/pkg/git/commit.go +++ b/pkg/git/commit.go @@ -33,49 +33,37 @@ const ( // NextCommit iterates over next commit of a branch // TODO: the commits entites can tied to branch instead ot the repository -func (entity *RepoEntity) NextCommit() error { - currentCommitIndex := entity.findCurrentCommitIndex() - if currentCommitIndex == len(entity.Commits)-1 { - entity.Commit = entity.Commits[0] - return nil - } - entity.Commit = entity.Commits[currentCommitIndex+1] - return nil +func (e *RepoEntity) NextCommit() { + e.Commit = e.Commits[(e.currentCommitIndex()+1)%len(e.Commits)] } // PreviousCommit iterates to opposite direction -func (entity *RepoEntity) PreviousCommit() error { - currentCommitIndex := entity.findCurrentCommitIndex() - if currentCommitIndex == 0 { - entity.Commit = entity.Commits[len(entity.Commits)-1] - return nil - } - entity.Commit = entity.Commits[currentCommitIndex-1] - return nil +func (e *RepoEntity) PreviousCommit() { + e.Commit = e.Commits[(len(e.Commits)+e.currentCommitIndex()-1)%len(e.Commits)] } // returns the active commit index -func (entity *RepoEntity) findCurrentCommitIndex() int { - currentCommitIndex := 0 - for i, cs := range entity.Commits { - if cs.Hash == entity.Commit.Hash { - currentCommitIndex = i +func (e *RepoEntity) currentCommitIndex() int { + cix := 0 + for i, c := range e.Commits { + if c.Hash == e.Commit.Hash { + cix = i } } - return currentCommitIndex + return cix } // loads the local commits by simply using git log way. ALso, gets the upstream // diff commits -func (entity *RepoEntity) loadCommits() error { - r := entity.Repository - entity.Commits = make([]*Commit, 0) +func (e *RepoEntity) loadCommits() error { + r := e.Repository + e.Commits = make([]*Commit, 0) ref, err := r.Head() if err != nil { log.Trace("Cannot get HEAD " + err.Error()) return err } - + // git log first cIter, err := r.Log(&git.LogOptions{ From: ref.Hash(), Order: git.LogOrderCommitterTime, @@ -85,19 +73,13 @@ func (entity *RepoEntity) loadCommits() error { return err } defer cIter.Close() - rmcs, err := entity.pullDiffsToUpstream() - if err != nil { - log.Trace("git rev-list failed " + err.Error()) - return err - } - for _, rmc := range rmcs { - entity.Commits = append(entity.Commits, rmc) - } - lcs, err := entity.pushDiffsToUpstream() - if err != nil { - log.Trace("git rev-list failed " + err.Error()) - return err - } + // find commits that fetched from upstream but not merged commits + rmcs, _ := e.pullDiffsToUpstream() + e.Commits = append(e.Commits, rmcs...) + + // find commits that not pushed to upstream + lcs, _ := e.pushDiffsToUpstream() + // ... just iterates over the commits err = cIter.ForEach(func(c *object.Commit) error { re := regexp.MustCompile(`\r?\n`) @@ -114,22 +96,20 @@ func (entity *RepoEntity) loadCommits() error { Time: c.Author.When.String(), CommitType: cmType, } - entity.Commits = append(entity.Commits, commit) - + e.Commits = append(e.Commits, commit) return nil }) if err != nil { return err } - // entity.Commits = commits return nil } // this function creates the commit entities according to active branchs diffs // to *its* configured upstream -func (entity *RepoEntity) pullDiffsToUpstream() ([]*Commit, error) { +func (e *RepoEntity) pullDiffsToUpstream() ([]*Commit, error) { remoteCommits := make([]*Commit, 0) - pullables, err := RevList(entity, RevListOptions{ + pullables, err := RevList(e, RevListOptions{ Ref1: "HEAD", Ref2: "@{u}", }) @@ -140,9 +120,9 @@ func (entity *RepoEntity) pullDiffsToUpstream() ([]*Commit, error) { for _, s := range pullables { commit := &Commit{ Hash: s, - Author: GitShowEmail(entity.AbsPath, s), - Message: re.ReplaceAllString(GitShowBody(entity.AbsPath, s), " "), - Time: GitShowDate(entity.AbsPath, s), + Author: GitShowEmail(e.AbsPath, s), + Message: re.ReplaceAllString(GitShowBody(e.AbsPath, s), " "), + Time: GitShowDate(e.AbsPath, s), CommitType: RemoteCommit, } remoteCommits = append(remoteCommits, commit) @@ -151,8 +131,10 @@ func (entity *RepoEntity) pullDiffsToUpstream() ([]*Commit, error) { return remoteCommits, nil } -func (entity *RepoEntity) pushDiffsToUpstream() ([]string, error) { - pushables, err := RevList(entity, RevListOptions{ +// this function returns the hashes of the commits that are not pushed to the +// upstream of the specific branch +func (e *RepoEntity) pushDiffsToUpstream() ([]string, error) { + pushables, err := RevList(e, RevListOptions{ Ref1: "@{u}", Ref2: "HEAD", }) diff --git a/pkg/git/file.go b/pkg/git/file.go index b1e603a..1e086bc 100644 --- a/pkg/git/file.go +++ b/pkg/git/file.go @@ -39,12 +39,12 @@ var ( ) // Diff is a wrapper of "git diff" command for a file to compare with HEAD rev -func (file *File) Diff() (output string, err error) { +func (f *File) Diff() (output string, err error) { args := make([]string, 0) args = append(args, "diff") args = append(args, "HEAD") - args = append(args, file.Name) - output, err = GenericGitCommandWithErrorOutput(strings.TrimSuffix(file.AbsPath, file.Name), args) + args = append(args, f.Name) + output, err = GenericGitCommandWithErrorOutput(strings.TrimSuffix(f.AbsPath, f.Name), args) if err != nil { log.Warn(err) } diff --git a/pkg/git/job-queue.go b/pkg/git/job-queue.go index e95250a..3eee605 100644 --- a/pkg/git/job-queue.go +++ b/pkg/git/job-queue.go @@ -11,7 +11,7 @@ type JobQueue struct { // CreateJobQueue creates a jobqueue struct and initialize its slice then return // its pointer -func CreateJobQueue() (jobQueue *JobQueue) { +func CreateJobQueue() (jq *JobQueue) { s := make([]*Job, 0) return &JobQueue{ series: s, @@ -19,28 +19,28 @@ func CreateJobQueue() (jobQueue *JobQueue) { } // AddJob adds a job to the queue -func (jobQueue *JobQueue) AddJob(j *Job) error { - for _, job := range jobQueue.series { +func (jq *JobQueue) AddJob(j *Job) error { + for _, job := range jq.series { if job.Entity.RepoID == j.Entity.RepoID && job.JobType == j.JobType { return errors.New("Same job already is in the queue") } } - jobQueue.series = append(jobQueue.series, nil) - copy(jobQueue.series[1:], jobQueue.series[0:]) - jobQueue.series[0] = j + jq.series = append(jq.series, nil) + copy(jq.series[1:], jq.series[0:]) + jq.series[0] = j return nil } // StartNext starts the next job in the queue -func (jobQueue *JobQueue) StartNext() (j *Job, finished bool, err error) { +func (jq *JobQueue) StartNext() (j *Job, finished bool, err error) { finished = false - if len(jobQueue.series) < 1 { + if len(jq.series) < 1 { finished = true return nil, finished, nil } - i := len(jobQueue.series) - 1 - lastJob := jobQueue.series[i] - jobQueue.series = jobQueue.series[:i] + i := len(jq.series) - 1 + lastJob := jq.series[i] + jq.series = jq.series[:i] if err = lastJob.start(); err != nil { return lastJob, finished, err } @@ -49,11 +49,11 @@ func (jobQueue *JobQueue) StartNext() (j *Job, finished bool, err error) { // RemoveFromQueue deletes the given entity and its job from the queue // TODO: it is not safe if the job has been started -func (jobQueue *JobQueue) RemoveFromQueue(entity *RepoEntity) error { +func (jq *JobQueue) RemoveFromQueue(entity *RepoEntity) error { removed := false - for i, job := range jobQueue.series { + for i, job := range jq.series { if job.Entity.RepoID == entity.RepoID { - jobQueue.series = append(jobQueue.series[:i], jobQueue.series[i+1:]...) + jq.series = append(jq.series[:i], jq.series[i+1:]...) removed = true } } @@ -66,9 +66,9 @@ func (jobQueue *JobQueue) RemoveFromQueue(entity *RepoEntity) error { // IsInTheQueue function; since the job and entity is not tied with its own // struct, this function returns true if that entity is in the queue along with // the jobs type -func (jobQueue *JobQueue) IsInTheQueue(entity *RepoEntity) (inTheQueue bool, jt JobType) { +func (jq *JobQueue) IsInTheQueue(entity *RepoEntity) (inTheQueue bool, jt JobType) { inTheQueue = false - for _, job := range jobQueue.series { + for _, job := range jq.series { if job.Entity.RepoID == entity.RepoID { inTheQueue = true jt = job.JobType diff --git a/pkg/git/job.go b/pkg/git/job.go index 4889fc0..c64086e 100644 --- a/pkg/git/job.go +++ b/pkg/git/job.go @@ -25,46 +25,46 @@ const ( ) // starts the job -func (job *Job) start() error { - job.Entity.SetState(Working) +func (j *Job) start() error { + j.Entity.SetState(Working) // TODO: Handle errors? // TOOD: Better implementation required - switch mode := job.JobType; mode { + switch mode := j.JobType; mode { case FetchJob: var opts FetchOptions - if job.Options != nil { - opts = job.Options.(FetchOptions) + if j.Options != nil { + opts = j.Options.(FetchOptions) } else { opts = FetchOptions{ - RemoteName: job.Entity.Remote.Name, + RemoteName: j.Entity.Remote.Name, } } - if err := Fetch(job.Entity, opts); err != nil { - job.Entity.SetState(Fail) + if err := Fetch(j.Entity, opts); err != nil { + j.Entity.SetState(Fail) return err } case PullJob: var opts PullOptions - if job.Options != nil { - opts = job.Options.(PullOptions) + if j.Options != nil { + opts = j.Options.(PullOptions) } else { opts = PullOptions{ - RemoteName: job.Entity.Remote.Name, + RemoteName: j.Entity.Remote.Name, } } - if err := Pull(job.Entity, opts); err != nil { - job.Entity.SetState(Fail) + if err := Pull(j.Entity, opts); err != nil { + j.Entity.SetState(Fail) return err } case MergeJob: - if err := Merge(job.Entity, MergeOptions{ - BranchName: job.Entity.Remote.Branch.Name, + if err := Merge(j.Entity, MergeOptions{ + BranchName: j.Entity.Remote.Branch.Name, }); err != nil { - job.Entity.SetState(Fail) + j.Entity.SetState(Fail) return nil } default: - job.Entity.SetState(Available) + j.Entity.SetState(Available) return nil } return nil diff --git a/pkg/git/remote.go b/pkg/git/remote.go index 27f627b..c8442af 100644 --- a/pkg/git/remote.go +++ b/pkg/git/remote.go @@ -16,48 +16,36 @@ type Remote struct { } // NextRemote iterates over next branch of a remote -func (entity *RepoEntity) NextRemote() error { - currentRemoteIndex := entity.findCurrentRemoteIndex() - if currentRemoteIndex == len(entity.Remotes)-1 { - entity.Remote = entity.Remotes[0] - } else { - entity.Remote = entity.Remotes[currentRemoteIndex+1] - } - err := entity.Remote.SyncBranches(entity.Branch.Name) - return err +func (e *RepoEntity) NextRemote() error { + e.Remote = e.Remotes[(e.currentRemoteIndex()+1)%len(e.Remotes)] + return e.Remote.SyncBranches(e.Branch.Name) } // PreviousRemote iterates over previous branch of a remote -func (entity *RepoEntity) PreviousRemote() error { - currentRemoteIndex := entity.findCurrentRemoteIndex() - if currentRemoteIndex == 0 { - entity.Remote = entity.Remotes[len(entity.Remotes)-1] - } else { - entity.Remote = entity.Remotes[currentRemoteIndex-1] - } - err := entity.Remote.SyncBranches(entity.Branch.Name) - return err +func (e *RepoEntity) PreviousRemote() error { + e.Remote = e.Remotes[(len(e.Remotes)+e.currentRemoteIndex()-1)%len(e.Remotes)] + return e.Remote.SyncBranches(e.Branch.Name) } // returns the active remote index -func (entity *RepoEntity) findCurrentRemoteIndex() int { - currentRemoteIndex := 0 - for i, remote := range entity.Remotes { - if remote.Name == entity.Remote.Name { - currentRemoteIndex = i +func (e *RepoEntity) currentRemoteIndex() int { + cix := 0 + for i, remote := range e.Remotes { + if remote.Name == e.Remote.Name { + cix = i } } - return currentRemoteIndex + return cix } // search for remotes in go-git way. It is the short way to get remotes but it // does not give any insght about remote branches -func (entity *RepoEntity) loadRemotes() error { - r := entity.Repository - entity.Remotes = make([]*Remote, 0) +func (e *RepoEntity) loadRemotes() error { + r := e.Repository + e.Remotes = make([]*Remote, 0) - remotes, err := r.Remotes() - for _, rm := range remotes { + rms, err := r.Remotes() + for _, rm := range rms { rfs := make([]string, 0) for _, rf := range rm.Config().Fetch { rfs = append(rfs, string(rf)) @@ -67,11 +55,11 @@ func (entity *RepoEntity) loadRemotes() error { URL: rm.Config().URLs, RefSpecs: rfs, } - remote.loadRemoteBranches(entity) + remote.loadRemoteBranches(e) if len(remote.Branches) > 0 { remote.Branch = remote.Branches[0] } - entity.Remotes = append(entity.Remotes, remote) + e.Remotes = append(e.Remotes, remote) } if err != nil { @@ -82,8 +70,8 @@ func (entity *RepoEntity) loadRemotes() error { } // SyncBranches sets the remote branch according to repository's active branch -func (remote *Remote) SyncBranches(branchName string) error { - if err := remote.switchRemoteBranch(remote.Name + "/" + branchName); err != nil { +func (r *Remote) SyncBranches(branchName string) error { + if err := r.switchRemoteBranch(r.Name + "/" + branchName); err != nil { // probably couldn't find, but its ok. } return nil diff --git a/pkg/git/remotebranch.go b/pkg/git/remotebranch.go index 8ab737f..5fa1335 100644 --- a/pkg/git/remotebranch.go +++ b/pkg/git/remotebranch.go @@ -18,42 +18,32 @@ type RemoteBranch struct { } // NextRemoteBranch iterates to the next remote branch -func (remote *Remote) NextRemoteBranch() error { - currentRemoteIndex := remote.findCurrentRemoteBranchIndex() - if currentRemoteIndex == len(remote.Branches)-1 { - remote.Branch = remote.Branches[0] - } else { - remote.Branch = remote.Branches[currentRemoteIndex+1] - } +func (r *Remote) NextRemoteBranch() error { + r.Branch = r.Branches[(r.currentRemoteBranchIndex()+1)%len(r.Branches)] return nil } // PreviousRemoteBranch iterates to the previous remote branch -func (remote *Remote) PreviousRemoteBranch() error { - currentRemoteIndex := remote.findCurrentRemoteBranchIndex() - if currentRemoteIndex == 0 { - remote.Branch = remote.Branches[len(remote.Branches)-1] - } else { - remote.Branch = remote.Branches[currentRemoteIndex-1] - } +func (r *Remote) PreviousRemoteBranch() error { + r.Branch = r.Branches[(len(r.Branches)+r.currentRemoteBranchIndex()-1)%len(r.Branches)] return nil } // returns the active remote branch index -func (remote *Remote) findCurrentRemoteBranchIndex() int { - currentRemoteIndex := 0 - for i, rb := range remote.Branches { - if rb.Reference.Hash() == remote.Branch.Reference.Hash() { - currentRemoteIndex = i +func (r *Remote) currentRemoteBranchIndex() int { + cix := 0 + for i, rb := range r.Branches { + if rb.Reference.Hash() == r.Branch.Reference.Hash() { + cix = i } } - return currentRemoteIndex + return cix } // search for the remote branches of the remote. It takes the go-git's repo // pointer in order to get storer struct -func (remote *Remote) loadRemoteBranches(entity *RepoEntity) error { - remote.Branches = make([]*RemoteBranch, 0) +func (r *Remote) loadRemoteBranches(entity *RepoEntity) error { + r.Branches = make([]*RemoteBranch, 0) bs, err := remoteBranchesIter(entity.Repository.Storer) if err != nil { log.Warn("Cannot initiate iterator " + err.Error()) @@ -62,8 +52,8 @@ func (remote *Remote) loadRemoteBranches(entity *RepoEntity) error { defer bs.Close() err = bs.ForEach(func(b *plumbing.Reference) error { deleted := false - if strings.Split(b.Name().Short(), "/")[0] == remote.Name { - remote.Branches = append(remote.Branches, &RemoteBranch{ + if strings.Split(b.Name().Short(), "/")[0] == r.Name { + r.Branches = append(r.Branches, &RemoteBranch{ Name: b.Name().Short(), Reference: b, Deleted: deleted, @@ -71,9 +61,6 @@ func (remote *Remote) loadRemoteBranches(entity *RepoEntity) error { } return nil }) - if err != nil { - return err - } return err } @@ -95,10 +82,10 @@ func remoteBranchesIter(s storer.ReferenceStorer) (storer.ReferenceIter, error) } // switches to the given remote branch -func (remote *Remote) switchRemoteBranch(remoteBranchName string) error { - for _, rb := range remote.Branches { +func (r *Remote) switchRemoteBranch(remoteBranchName string) error { + for _, rb := range r.Branches { if rb.Name == remoteBranchName { - remote.Branch = rb + r.Branch = rb return nil } } diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index eff7952..3c5db5f 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -184,7 +184,7 @@ func (gui *Gui) generateKeybindings() error { Modifier: gocui.ModNone, Handler: gui.closeAuthenticationView, Display: "esc", - Description: "close/cancel", + Description: "Close/Cancel", Vital: true, }, { View: view.Name, @@ -214,7 +214,7 @@ func (gui *Gui) generateKeybindings() error { Modifier: gocui.ModNone, Handler: gui.closeCommitMessageView, Display: "esc", - Description: "close/cancel", + Description: "Close/Cancel", Vital: true, }, { View: view.Name, @@ -444,7 +444,7 @@ func (gui *Gui) generateKeybindings() error { Modifier: gocui.ModNone, Handler: gui.closeConfirmationView, Display: "esc", - Description: "close/cancel", + Description: "Close/Cancel", Vital: true, }, { View: confirmationViewFeature.Name, @@ -462,7 +462,7 @@ func (gui *Gui) generateKeybindings() error { Modifier: gocui.ModNone, Handler: gui.closeCommitDiffView, Display: "esc", - Description: "close/cancel", + Description: "Close/Cancel", Vital: true, }, { View: diffViewFeature.Name, @@ -504,7 +504,7 @@ func (gui *Gui) generateKeybindings() error { Modifier: gocui.ModNone, Handler: gui.closeCheatSheetView, Display: "esc", - Description: "close/cancel", + Description: "Close/Cancel", Vital: true, }, { View: cheatSheetViewFeature.Name, @@ -546,7 +546,7 @@ func (gui *Gui) generateKeybindings() error { Modifier: gocui.ModNone, Handler: gui.closeErrorView, Display: "esc", - Description: "close/cancel", + Description: "Close/Cancel", Vital: true, }, { View: errorViewFeature.Name, diff --git a/pkg/gui/sideviews.go b/pkg/gui/sideviews.go index b963391..21ce55e 100644 --- a/pkg/gui/sideviews.go +++ b/pkg/gui/sideviews.go @@ -167,11 +167,10 @@ func (gui *Gui) sideViewsNextItem(g *gocui.Gui, v *gocui.View) error { branchViewFeature.Name) return err } - err = gui.checkoutFollowUp(entity) + // err = gui.checkoutFollowUp(entity) case commitViewFeature.Name: - if err = entity.NextCommit(); err != nil { - return err - } + entity.NextCommit() + err = gui.updateCommits(entity) } return err @@ -198,11 +197,10 @@ func (gui *Gui) sideViewsPreviousItem(g *gocui.Gui, v *gocui.View) error { branchViewFeature.Name) return err } - err = gui.checkoutFollowUp(entity) + // err = gui.checkoutFollowUp(entity) case commitViewFeature.Name: - if err = entity.PreviousCommit(); err != nil { - return err - } + entity.PreviousCommit() + err = gui.updateCommits(entity) } return err @@ -283,17 +281,3 @@ func (gui *Gui) remoteChangeFollowUp(entity *git.RepoEntity) (err error) { err = gui.updateRemoteBranches(entity) return err } - -// after checkout a branch some refreshments needed -func (gui *Gui) checkoutFollowUp(entity *git.RepoEntity) (err error) { - if err = gui.updateBranch(entity); err != nil { - return err - } - if err = gui.updateCommits(entity); err != nil { - return err - } - if err = gui.updateRemoteBranches(entity); err != nil { - return err - } - return err -} |
