| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "sync" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/go-git/go-git/v5" |
| 10 | ) |
| 11 | |
| 12 | // Reference is the interface for commits, remotes and branches |
| 13 | type Reference interface { |
| 14 | Next() *Reference |
| 15 | Previous() *Reference |
| 16 | } |
| 17 | |
| 18 | // Repository is the main entity of the application. The repository name is |
| 19 | // actually the name of its folder in the host's filesystem. It holds the go-git |
| 20 | // repository entity along with critic entities such as remote/branches and commits |
| 21 | type Repository struct { |
| 22 | RepoID string |
| 23 | Name string |
| 24 | AbsPath string |
| 25 | ModTime time.Time |
| 26 | Repo git.Repository |
| 27 | Branches []*Branch |
| 28 | Remotes []*Remote |
| 29 | Stasheds []*StashedItem |
| 30 | State *RepositoryState |
| 31 | |
| 32 | mutex *sync.RWMutex |
| 33 | listeners map[string][]RepositoryListener |
| 34 | } |
| 35 | |
| 36 | // RepositoryState is the current pointers of a repository |
| 37 | type RepositoryState struct { |
| 38 | workStatus WorkStatus |
| 39 | Branch *Branch |
| 40 | Remote *Remote |
| 41 | Message string |
| 42 | } |
| 43 | |
| 44 | // RepositoryListener is a type for listeners |
| 45 | type RepositoryListener func(event *RepositoryEvent) error |
| 46 | |
| 47 | // RepositoryEvent is used to transfer event-related data. |
| 48 | // It is passed to listeners when Publish() is called |
| 49 | type RepositoryEvent struct { |
| 50 | Name string |
| 51 | Data interface{} |
| 52 | } |
| 53 | |
| 54 | // WorkStatus is the state of the repository for an operation |
| 55 | type WorkStatus struct { |
| 56 | Status uint8 |
| 57 | Ready bool |
| 58 | } |
| 59 | |
| 60 | var ( |
| 61 | // Available implies repo is ready for the operation |
| 62 | Available = WorkStatus{Status: 0, Ready: true} |
| 63 | // Queued means repo is queued for a operation |
| 64 | Queued = WorkStatus{Status: 1, Ready: false} |
| 65 | // Working means an operation is just started for this repository |
| 66 | Working = WorkStatus{Status: 2, Ready: false} |
| 67 | // Paused is expected when a user interaction is required |
| 68 | Paused = WorkStatus{Status: 3, Ready: true} |
| 69 | // Success is the expected outcome of the operation |
| 70 | Success = WorkStatus{Status: 4, Ready: true} |
| 71 | // Fail is the unexpected outcome of the operation |
| 72 | Fail = WorkStatus{Status: 5, Ready: false} |
| 73 | ) |
| 74 | |
| 75 | const ( |
| 76 | // RepositoryUpdated defines the topic for an updated repository. |
| 77 | RepositoryUpdated = "repository.updated" |
| 78 | // BranchUpdated defines the topic for an updated branch. |
| 79 | BranchUpdated = "branch.updated" |
| 80 | ) |
| 81 | |
| 82 | // FastInitializeRepo initializes a Repository struct without its belongings. |
| 83 | func FastInitializeRepo(dir string) (r *Repository, err error) { |
| 84 | f, err := os.Open(dir) |
| 85 | if err != nil { |
| 86 | return nil, err |
| 87 | } |
| 88 | defer f.Close() |
| 89 | // get status of the file |
| 90 | fstat, _ := f.Stat() |
| 91 | rp, err := git.PlainOpen(dir) |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | // initialize Repository with minimum viable fields |
| 96 | r = &Repository{RepoID: RandomString(8), |
| 97 | Name: fstat.Name(), |
| 98 | AbsPath: dir, |
| 99 | ModTime: fstat.ModTime(), |
| 100 | Repo: *rp, |
| 101 | State: &RepositoryState{ |
| 102 | workStatus: Available, |
| 103 | Message: "", |
| 104 | }, |
| 105 | mutex: &sync.RWMutex{}, |
| 106 | listeners: make(map[string][]RepositoryListener), |
| 107 | } |
| 108 | return r, nil |
| 109 | } |
| 110 | |
| 111 | // InitializeRepo initializes a Repository struct with its belongings. |
| 112 | func InitializeRepo(dir string) (r *Repository, err error) { |
| 113 | r, err = FastInitializeRepo(dir) |
| 114 | if err != nil { |
| 115 | return nil, err |
| 116 | } |
| 117 | // need nothing extra but loading additional components |
| 118 | return r, r.loadComponents(true) |
| 119 | } |
| 120 | |
| 121 | // loadComponents initializes the fields of a repository such as branches, |
| 122 | // remotes, commits etc. If reset, reload commit, remote pointers too |
| 123 | func (r *Repository) loadComponents(reset bool) error { |
| 124 | if err := r.initRemotes(); err != nil { |
| 125 | return err |
| 126 | } |
| 127 | |
| 128 | if err := r.initBranches(); err != nil { |
| 129 | return err |
| 130 | } |
| 131 | |
| 132 | if err := r.SyncRemoteAndBranch(r.State.Branch); err != nil { |
| 133 | return err |
| 134 | } |
| 135 | |
| 136 | return r.loadStashedItems() |
| 137 | } |
| 138 | |
| 139 | // Refresh the belongings of a repository, this function is called right after |
| 140 | // fetch/pull/merge operations |
| 141 | func (r *Repository) Refresh() error { |
| 142 | var err error |
| 143 | // error can be ignored since the file already exists when app is loading |
| 144 | // if the Repository is only fast initialized, no need to refresh because |
| 145 | // it won't contain its belongings |
| 146 | if r.State.Branch == nil { |
| 147 | return nil |
| 148 | } |
| 149 | file, _ := os.Open(r.AbsPath) |
| 150 | fstat, _ := file.Stat() |
| 151 | // re-initialize the go-git repository struct after supposed update |
| 152 | rp, err := git.PlainOpen(r.AbsPath) |
| 153 | if err != nil { |
| 154 | return err |
| 155 | } |
| 156 | r.Repo = *rp |
| 157 | // modification date may be changed |
| 158 | r.ModTime = fstat.ModTime() |
| 159 | if err := r.loadComponents(false); err != nil { |
| 160 | return err |
| 161 | } |
| 162 | // we could send an event data but we don't need for this topic |
| 163 | return r.Publish(RepositoryUpdated, nil) |
| 164 | } |
| 165 | |
| 166 | // On adds new listener. |
| 167 | // listener is a callback function that will be called when event emits |
| 168 | func (r *Repository) On(event string, listener RepositoryListener) { |
| 169 | r.mutex.Lock() |
| 170 | defer r.mutex.Unlock() |
| 171 | // add listener to the specific event topic |
| 172 | r.listeners[event] = append(r.listeners[event], listener) |
| 173 | } |
| 174 | |
| 175 | // Publish publishes the data to a certain event by its name. |
| 176 | func (r *Repository) Publish(eventName string, data interface{}) error { |
| 177 | r.mutex.RLock() |
| 178 | defer r.mutex.RUnlock() |
| 179 | // let's find listeners for this event topic |
| 180 | listeners, ok := r.listeners[eventName] |
| 181 | if !ok { |
| 182 | return nil |
| 183 | } |
| 184 | // now notify the listeners and channel the data |
| 185 | for i := range listeners { |
| 186 | event := &RepositoryEvent{ |
| 187 | Name: eventName, |
| 188 | Data: data, |
| 189 | } |
| 190 | if err := listeners[i](event); err != nil { |
| 191 | return err |
| 192 | } |
| 193 | } |
| 194 | return nil |
| 195 | } |
| 196 | |
| 197 | // WorkStatus returns the state of the repository such as queued, failed etc. |
| 198 | func (r *Repository) WorkStatus() WorkStatus { |
| 199 | return r.State.workStatus |
| 200 | } |
| 201 | |
| 202 | // SetWorkStatus sets the state of repository and sends repository updated event |
| 203 | func (r *Repository) SetWorkStatus(ws WorkStatus) { |
| 204 | r.State.workStatus = ws |
| 205 | // we could send an event data but we don't need for this topic |
| 206 | _ = r.Publish(RepositoryUpdated, nil) |
| 207 | } |
| 208 | |
| 209 | func (r *Repository) String() string { |
| 210 | return r.Name |
| 211 | } |
| 212 | |
| 213 | func Create(dir string) (*Repository, error) { |
| 214 | cmd := exec.Command("git", "init") |
| 215 | cmd.Dir = dir |
| 216 | _, err := cmd.CombinedOutput() |
| 217 | if err != nil { |
| 218 | return nil, err |
| 219 | } |
| 220 | return InitializeRepo(dir) |
| 221 | } |