diff options
| author | İbrahim Serdar Açıkgöz <serdaracikgoz86@gmail.com> | 2019-01-04 10:47:22 +0300 |
|---|---|---|
| committer | İbrahim Serdar Açıkgöz <serdaracikgoz86@gmail.com> | 2019-01-04 10:47:22 +0300 |
| commit | ba246669df4f6c80fcba7445ae7e56fbc2d92541 (patch) | |
| tree | dcdbcaa1c86ac3bd580331774dcb721feab3b75d /core | |
| parent | refactor on var name (diff) | |
| download | gitbatch-ba246669df4f6c80fcba7445ae7e56fbc2d92541.tar.gz | |
add state to repository and some renaming
Diffstat (limited to 'core')
| -rw-r--r-- | core/command/add.go (renamed from core/command/cmd-add.go) | 0 | ||||
| -rw-r--r-- | core/command/commit.go (renamed from core/command/cmd-commit.go) | 0 | ||||
| -rw-r--r-- | core/command/config.go (renamed from core/command/cmd-config.go) | 0 | ||||
| -rw-r--r-- | core/command/diff.go (renamed from core/command/cmd-diff.go) | 2 | ||||
| -rw-r--r-- | core/command/fetch.go (renamed from core/command/cmd-fetch.go) | 12 | ||||
| -rw-r--r-- | core/command/merge.go (renamed from core/command/cmd-merge.go) | 2 | ||||
| -rw-r--r-- | core/command/pull.go (renamed from core/command/cmd-pull.go) | 6 | ||||
| -rw-r--r-- | core/command/reset.go (renamed from core/command/cmd-reset.go) | 0 | ||||
| -rw-r--r-- | core/command/status.go (renamed from core/command/cmd-status.go) | 0 | ||||
| -rw-r--r-- | core/git/branch.go | 16 | ||||
| -rw-r--r-- | core/git/commit.go | 15 | ||||
| -rw-r--r-- | core/git/remote.go | 10 | ||||
| -rw-r--r-- | core/git/repository.go | 75 | ||||
| -rw-r--r-- | core/job/job.go | 22 |
14 files changed, 81 insertions, 79 deletions
diff --git a/core/command/cmd-add.go b/core/command/add.go index 5ad8774..5ad8774 100644 --- a/core/command/cmd-add.go +++ b/core/command/add.go diff --git a/core/command/cmd-commit.go b/core/command/commit.go index bf81a25..bf81a25 100644 --- a/core/command/cmd-commit.go +++ b/core/command/commit.go diff --git a/core/command/cmd-config.go b/core/command/config.go index 35eff23..35eff23 100644 --- a/core/command/cmd-config.go +++ b/core/command/config.go diff --git a/core/command/cmd-diff.go b/core/command/diff.go index 89d17be..ef48336 100644 --- a/core/command/cmd-diff.go +++ b/core/command/diff.go @@ -48,7 +48,7 @@ func diffWithGoGit(r *git.Repository, hash string) (diff string, err error) { // maybe we dont need to log the repo again? commits, err := r.Repo.Log(&gogit.LogOptions{ - From: plumbing.NewHash(r.Commit.Hash), + From: plumbing.NewHash(r.State.Commit.Hash), Order: gogit.LogOrderCommitterTime, }) if err != nil { diff --git a/core/command/cmd-fetch.go b/core/command/fetch.go index 6415651..cc827bf 100644 --- a/core/command/cmd-fetch.go +++ b/core/command/fetch.go @@ -61,10 +61,10 @@ func Fetch(r *git.Repository, options FetchOptions) (err error) { // 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 r.Branch == nil { + if r.State.Branch == nil { refspec = "+refs/heads/*:refs/remotes/origin/*" } else { - refspec = "+" + "refs/heads/" + r.Branch.Name + ":" + "/refs/remotes/" + r.Remote.Branch.Name + refspec = "+" + "refs/heads/" + r.State.Branch.Name + ":" + "/refs/remotes/" + r.State.Remote.Branch.Name } err = fetchWithGoGit(r, options, refspec) return err @@ -94,7 +94,7 @@ func fetchWithGit(r *git.Repository, options FetchOptions) (err error) { if out, err := GenericGitCommandWithOutput(r.AbsPath, args); err != nil { return gerr.ParseGitError(out, err) } - r.SetState(git.Success) + r.SetWorkStatus(git.Success) // till this step everything should be ok return r.Refresh() } @@ -113,7 +113,7 @@ func fetchWithGoGit(r *git.Repository, options FetchOptions, refspec string) (er } // if any credential is given, let's add it to the git.FetchOptions if len(options.Credentials.User) > 0 { - protocol, err := git.AuthProtocol(r.Remote) + protocol, err := git.AuthProtocol(r.State.Remote) if err != nil { return err } @@ -137,7 +137,7 @@ func fetchWithGoGit(r *git.Repository, options FetchOptions, refspec string) (er // 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 := r.Remote.RefSpecs[0] + rp := r.State.Remote.RefSpecs[0] if fetchTryCount < fetchMaxTry { fetchTryCount++ fetchWithGoGit(r, options, rp) @@ -157,7 +157,7 @@ func fetchWithGoGit(r *git.Repository, options FetchOptions, refspec string) (er } } - r.SetState(git.Success) + r.SetWorkStatus(git.Success) // till this step everything should be ok return r.Refresh() } diff --git a/core/command/cmd-merge.go b/core/command/merge.go index 1bcc90f..0552403 100644 --- a/core/command/cmd-merge.go +++ b/core/command/merge.go @@ -34,6 +34,6 @@ func Merge(r *git.Repository, options MergeOptions) error { if out, err := GenericGitCommandWithOutput(r.AbsPath, args); err != nil { return gerr.ParseGitError(out, err) } - r.SetState(git.Success) + r.SetWorkStatus(git.Success) return r.Refresh() } diff --git a/core/command/cmd-pull.go b/core/command/pull.go index 7fbcecc..5644417 100644 --- a/core/command/cmd-pull.go +++ b/core/command/pull.go @@ -72,7 +72,7 @@ func pullWithGit(r *git.Repository, options PullOptions) (err error) { if out, err := GenericGitCommandWithOutput(r.AbsPath, args); err != nil { return gerr.ParseGitError(out, err) } - r.SetState(git.Success) + r.SetWorkStatus(git.Success) return r.Refresh() } @@ -88,7 +88,7 @@ func pullWithGoGit(r *git.Repository, 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 := git.AuthProtocol(r.Remote) + protocol, err := git.AuthProtocol(r.State.Remote) if err != nil { return err } @@ -136,6 +136,6 @@ func pullWithGoGit(r *git.Repository, options PullOptions) (err error) { } } - r.SetState(git.Success) + r.SetWorkStatus(git.Success) return r.Refresh() } diff --git a/core/command/cmd-reset.go b/core/command/reset.go index d97d82f..d97d82f 100644 --- a/core/command/cmd-reset.go +++ b/core/command/reset.go diff --git a/core/command/cmd-status.go b/core/command/status.go index f5947d6..f5947d6 100644 --- a/core/command/cmd-status.go +++ b/core/command/status.go diff --git a/core/git/branch.go b/core/git/branch.go index ead250a..e2ae793 100644 --- a/core/git/branch.go +++ b/core/git/branch.go @@ -54,7 +54,7 @@ func (r *Repository) loadLocalBranches() error { Ref2: "HEAD", }) if err != nil { - push = pushables[0] + push = "?" } else { push = strconv.Itoa(len(pushables)) } @@ -63,7 +63,7 @@ func (r *Repository) loadLocalBranches() error { Ref2: "@{u}", }) if err != nil { - pull = pullables[0] + pull = "?" } else { pull = strconv.Itoa(len(pullables)) } @@ -76,7 +76,7 @@ func (r *Repository) loadLocalBranches() error { Clean: clean, } if b.Name() == headRef.Name() { - r.Branch = branch + r.State.Branch = branch branchFound = true } lbs = append(lbs, branch) @@ -92,7 +92,7 @@ func (r *Repository) loadLocalBranches() error { Clean: r.isClean(), } lbs = append(lbs, branch) - r.Branch = branch + r.State.Branch = branch } r.Branches = lbs return err @@ -112,7 +112,7 @@ func (r *Repository) PreviousBranch() *Branch { func (r *Repository) currentBranchIndex() int { bix := 0 for i, lbs := range r.Branches { - if lbs.Name == r.Branch.Name { + if lbs.Name == r.State.Branch.Name { bix = i } } @@ -122,7 +122,7 @@ func (r *Repository) currentBranchIndex() int { // Checkout to given branch. If any errors occur, the method returns it instead // of returning nil func (r *Repository) Checkout(b *Branch) error { - if b.Name == r.Branch.Name { + if b.Name == r.State.Branch.Name { return nil } @@ -140,7 +140,7 @@ func (r *Repository) Checkout(b *Branch) error { // make this conditional on global scale // we don't care if this function returns an error - r.Remote.SyncBranches(b.Name) + r.State.Remote.SyncBranches(b.Name) return r.Refresh() } @@ -194,7 +194,7 @@ func RevList(r *Repository, options RevListOptions) ([]string, error) { cmd.Dir = r.AbsPath out, err := cmd.CombinedOutput() if err != nil { - return []string{"?"}, err + return nil, err } s := string(out) diff --git a/core/git/commit.go b/core/git/commit.go index 6c35778..71076b8 100644 --- a/core/git/commit.go +++ b/core/git/commit.go @@ -35,19 +35,19 @@ const ( // NextCommit iterates over next commit of a branch // TODO: the commits entites can tied to branch instead ot the repository func (r *Repository) NextCommit() { - r.Commit = r.Commits[(r.currentCommitIndex()+1)%len(r.Commits)] + r.State.Commit = r.Commits[(r.currentCommitIndex()+1)%len(r.Commits)] } // PreviousCommit iterates to opposite direction func (r *Repository) PreviousCommit() { - r.Commit = r.Commits[(len(r.Commits)+r.currentCommitIndex()-1)%len(r.Commits)] + r.State.Commit = r.Commits[(len(r.Commits)+r.currentCommitIndex()-1)%len(r.Commits)] } // returns the active commit index func (r *Repository) currentCommitIndex() int { cix := 0 for i, c := range r.Commits { - if c.Hash == r.Commit.Hash { + if c.Hash == r.State.Commit.Hash { cix = i } } @@ -116,6 +116,9 @@ func (r *Repository) pullDiffsToUpstream() ([]*Commit, error) { } else { re := regexp.MustCompile(`\r?\n`) for _, s := range pullables { + if len(s) < hashLength { + continue + } commit := &Commit{ Hash: s, Author: gitShowEmail(r.AbsPath, s), @@ -149,7 +152,7 @@ func gitShowEmail(repoPath, hash string) string { cmd.Dir = repoPath out, err := cmd.CombinedOutput() if err != nil { - return "?" + return "" } return string(out) } @@ -161,7 +164,7 @@ func gitShowBody(repoPath, hash string) string { cmd.Dir = repoPath out, err := cmd.CombinedOutput() if err != nil { - return "?" + return "" } return string(out) } @@ -173,7 +176,7 @@ func gitShowDate(repoPath, hash string) string { cmd.Dir = repoPath out, err := cmd.CombinedOutput() if err != nil { - return "?" + return "" } return string(out) } diff --git a/core/git/remote.go b/core/git/remote.go index a90bb85..4fb3070 100644 --- a/core/git/remote.go +++ b/core/git/remote.go @@ -17,15 +17,15 @@ type Remote struct { // NextRemote iterates over next branch of a remote func (r *Repository) NextRemote() error { - r.Remote = r.Remotes[(r.currentRemoteIndex()+1)%len(r.Remotes)] - r.Remote.SyncBranches(r.Branch.Name) + r.State.Remote = r.Remotes[(r.currentRemoteIndex()+1)%len(r.Remotes)] + r.State.Remote.SyncBranches(r.State.Branch.Name) return r.Publish(RepositoryUpdated, nil) } // PreviousRemote iterates over previous branch of a remote func (r *Repository) PreviousRemote() error { - r.Remote = r.Remotes[(len(r.Remotes)+r.currentRemoteIndex()-1)%len(r.Remotes)] - r.Remote.SyncBranches(r.Branch.Name) + r.State.Remote = r.Remotes[(len(r.Remotes)+r.currentRemoteIndex()-1)%len(r.Remotes)] + r.State.Remote.SyncBranches(r.State.Branch.Name) return r.Publish(RepositoryUpdated, nil) } @@ -33,7 +33,7 @@ func (r *Repository) PreviousRemote() error { func (r *Repository) currentRemoteIndex() int { cix := 0 for i, remote := range r.Remotes { - if remote.Name == r.Remote.Name { + if remote.Name == r.State.Remote.Name { cix = i } } diff --git a/core/git/repository.go b/core/git/repository.go index 8470f13..ba990fd 100644 --- a/core/git/repository.go +++ b/core/git/repository.go @@ -19,51 +19,54 @@ type Repository struct { AbsPath string ModTime time.Time Repo git.Repository - Branch *Branch Branches []*Branch - Remote *Remote Remotes []*Remote - Commit *Commit Commits []*Commit Stasheds []*StashedItem - state RepoState - - // TODO: move this into state - Message string + State *RepositoryState mutex *sync.RWMutex listeners map[string][]RepositoryListener } +// RepositoryState is the current pointers of a repository +type RepositoryState struct { + workStatus WorkStatus + Branch *Branch + Remote *Remote + Commit *Commit + Message string +} + // RepositoryListener is a type for listeners type RepositoryListener func(event *RepositoryEvent) error // RepositoryEvent is used to transfer event-related data. -// It is passed to listeners when Emit() is called +// It is passed to listeners when Publish() is called type RepositoryEvent struct { Name string Data interface{} } -// RepoState is the state of the repository for an operation -type RepoState struct { - State uint8 - Ready bool +// WorkStatus is the state of the repository for an operation +type WorkStatus struct { + Status uint8 + Ready bool } var ( // Available implies repo is ready for the operation - Available = RepoState{State: 0, Ready: true} + Available = WorkStatus{Status: 0, Ready: true} // Queued means repo is queued for a operation - Queued = RepoState{State: 1, Ready: false} + Queued = WorkStatus{Status: 1, Ready: false} // Working means an operation is just started for this repository - Working = RepoState{State: 2, Ready: false} + Working = WorkStatus{Status: 2, Ready: false} // Paused is expected when a user interaction is required - Paused = RepoState{State: 3, Ready: true} + Paused = WorkStatus{Status: 3, Ready: true} // Success is the expected outcome of the operation - Success = RepoState{State: 4, Ready: true} + Success = WorkStatus{Status: 4, Ready: true} // Fail is the unexpected outcome of the operation - Fail = RepoState{State: 5, Ready: false} + Fail = WorkStatus{Status: 5, Ready: false} ) const ( @@ -85,11 +88,14 @@ func FastInitializeRepo(dir string) (r *Repository, err error) { } // initialize Repository with minimum viable fields r = &Repository{RepoID: RandomString(8), - Name: fstat.Name(), - AbsPath: dir, - ModTime: fstat.ModTime(), - Repo: *rp, - state: Available, + Name: fstat.Name(), + AbsPath: dir, + ModTime: fstat.ModTime(), + Repo: *rp, + State: &RepositoryState{ + workStatus: Available, + Message: "", + }, mutex: &sync.RWMutex{}, listeners: make(map[string][]RepositoryListener), } @@ -126,14 +132,14 @@ func (r *Repository) loadComponents(reset bool) error { // set commit pointer for repository if len(r.Commits) > 0 { // select first commit - r.Commit = r.Commits[0] + r.State.Commit = r.Commits[0] } // set remote pointer for repository if len(r.Remotes) > 0 { // TODO: tend to take origin/master as default - r.Remote = r.Remotes[0] + r.State.Remote = r.Remotes[0] // if couldn't find, its ok. - r.Remote.SyncBranches(r.Branch.Name) + r.State.Remote.SyncBranches(r.State.Branch.Name) } else { // if there is no remote, this project is totally useless actually return errors.New("There is no remote for this repository") @@ -149,7 +155,7 @@ func (r *Repository) Refresh() error { // error can be ignored since the file already exists when app is loading // if the Repository is only fast initialized, no need to refresh because // it won't contain its belongings - if r.Branch == nil { + if r.State.Branch == nil { return nil } file, _ := os.Open(r.AbsPath) @@ -201,22 +207,15 @@ func (r *Repository) Publish(eventName string, data interface{}) error { } // State returns the state of the repository such as queued, failed etc. -func (r *Repository) State() RepoState { - return r.state +func (r *Repository) WorkStatus() WorkStatus { + return r.State.workStatus } // SetState sets the state of repository and sends repository updated event -func (r *Repository) SetState(state RepoState) { - r.state = state +func (r *Repository) SetWorkStatus(ws WorkStatus) { + r.State.workStatus = ws // we could send an event data but we don't need for this topic if err := r.Publish(RepositoryUpdated, nil); err != nil { log.Warnf("Cannot publish on %s topic.\n", RepositoryUpdated) } } - -// SetMessage sets the message of status, it is used if state is Fail -func (r *Repository) SetStateMessage(msg string) { - if r.State() == Fail { - r.Message = msg - } -} diff --git a/core/job/job.go b/core/job/job.go index 69953b7..9cfc0ed 100644 --- a/core/job/job.go +++ b/core/job/job.go @@ -31,7 +31,7 @@ const ( // starts the job func (j *Job) start() error { - j.Repository.SetState(git.Working) + j.Repository.SetWorkStatus(git.Working) // TODO: Handle errors? // TOOD: Better implementation required switch mode := j.JobType; mode { @@ -41,12 +41,12 @@ func (j *Job) start() error { opts = j.Options.(command.FetchOptions) } else { opts = command.FetchOptions{ - RemoteName: j.Repository.Remote.Name, + RemoteName: j.Repository.State.Remote.Name, } } if err := command.Fetch(j.Repository, opts); err != nil { - j.Repository.SetState(git.Fail) - j.Repository.SetStateMessage(err.Error()) + j.Repository.SetWorkStatus(git.Fail) + j.Repository.State.Message = err.Error() return err } case PullJob: @@ -55,24 +55,24 @@ func (j *Job) start() error { opts = j.Options.(command.PullOptions) } else { opts = command.PullOptions{ - RemoteName: j.Repository.Remote.Name, + RemoteName: j.Repository.State.Remote.Name, } } if err := command.Pull(j.Repository, opts); err != nil { - j.Repository.SetState(git.Fail) - j.Repository.SetStateMessage(err.Error()) + j.Repository.SetWorkStatus(git.Fail) + j.Repository.State.Message = err.Error() return err } case MergeJob: if err := command.Merge(j.Repository, command.MergeOptions{ - BranchName: j.Repository.Remote.Branch.Name, + BranchName: j.Repository.State.Remote.Branch.Name, }); err != nil { - j.Repository.SetState(git.Fail) - j.Repository.SetStateMessage(err.Error()) + j.Repository.SetWorkStatus(git.Fail) + j.Repository.State.Message = err.Error() return nil } default: - j.Repository.SetState(git.Available) + j.Repository.SetWorkStatus(git.Available) return nil } return nil |
