| 1 | package gui |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/isacikgoz/gitbatch/internal/command" |
| 9 | "github.com/isacikgoz/gitbatch/internal/git" |
| 10 | "github.com/jroimartin/gocui" |
| 11 | ) |
| 12 | |
| 13 | // there is no AI, only too much if clauses |
| 14 | func (gui *Gui) initFocusStat(r *git.Repository) error { |
| 15 | v, err := gui.g.View(dynamicViewFeature.Name) |
| 16 | if err != nil { |
| 17 | return err |
| 18 | } |
| 19 | v.Clear() |
| 20 | v.Title = string(StatusMode) |
| 21 | if err := gui.updateDynamicKeybindings(); err != nil { |
| 22 | return err |
| 23 | } |
| 24 | fmt.Fprintln(v, "On branch "+cyan.Sprint(r.State.Branch.Name)) |
| 25 | ps, err := strconv.Atoi(r.State.Branch.Pushables) |
| 26 | pl, er2 := strconv.Atoi(r.State.Branch.Pullables) |
| 27 | // TODO: move to text-render |
| 28 | if err != nil || er2 != nil || r.State.Branch.Upstream == nil { |
| 29 | fmt.Fprintln(v, "Your branch is not tracking a remote branch.") |
| 30 | } else { |
| 31 | if ps == 0 && pl == 0 { |
| 32 | fmt.Fprintln(v, "Your branch is up to date with "+cyan.Sprint(r.State.Branch.Upstream.Name)) |
| 33 | } else { |
| 34 | if ps > 0 && pl > 0 { |
| 35 | fmt.Fprintln(v, "Your branch and "+cyan.Sprint(r.State.Branch.Upstream.Name)+" have diverged,") |
| 36 | fmt.Fprintln(v, "and have "+yellow.Sprint(r.State.Branch.Pushables)+" and "+yellow.Sprint(r.State.Branch.Pullables)+" different commits each, respectively.") |
| 37 | fmt.Fprintln(v, "(\"pull\" to merge the remote branch into yours)") |
| 38 | } else if pl > 0 && ps == 0 { |
| 39 | fmt.Fprintln(v, "Your branch is behind "+cyan.Sprint(r.State.Branch.Upstream.Name)+" by "+yellow.Sprint(r.State.Branch.Pullables)+" commit(s).") |
| 40 | fmt.Fprintln(v, "(\"pull\" to update your local branch)") |
| 41 | } else if ps > 0 && pl == 0 { |
| 42 | fmt.Fprintln(v, "Your branch is ahead of "+cyan.Sprint(r.State.Branch.Upstream.Name)+" by "+yellow.Sprint(r.State.Branch.Pushables)+" commit(s).") |
| 43 | fmt.Fprintln(v, "(\"push\" to publish your local commits)") |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | files, err := command.Status(r) |
| 48 | if err != nil { |
| 49 | return err |
| 50 | } |
| 51 | stagedFiles := make([]*git.File, 0) |
| 52 | unstagedFiles := make([]*git.File, 0) |
| 53 | for _, file := range files { |
| 54 | if file.X != git.StatusNotupdated && file.X != git.StatusUntracked && file.X != git.StatusIgnored && file.X != git.StatusUpdated { |
| 55 | stagedFiles = append(stagedFiles, file) |
| 56 | } |
| 57 | if file.Y != git.StatusNotupdated { |
| 58 | unstagedFiles = append(unstagedFiles, file) |
| 59 | } |
| 60 | } |
| 61 | if len(stagedFiles) == 0 && len(unstagedFiles) == 0 { |
| 62 | fmt.Fprintln(v, "\nNothing to commit, working tree clean") |
| 63 | } else { |
| 64 | if len(stagedFiles) > 0 { |
| 65 | fmt.Fprintln(v, "\nChanges to be committed:") |
| 66 | fmt.Fprintln(v, "") |
| 67 | for _, f := range stagedFiles { |
| 68 | fmt.Fprintln(v, " "+green.Sprint(string(f.X)+" "+f.Name)) |
| 69 | } |
| 70 | } |
| 71 | if len(unstagedFiles) > 0 { |
| 72 | fmt.Fprintln(v, "\nChanges not staged for commit:") |
| 73 | fmt.Fprintln(v, "") |
| 74 | for _, f := range unstagedFiles { |
| 75 | fmt.Fprintln(v, " "+red.Sprint(string(f.Y)+" "+f.Name)) |
| 76 | } |
| 77 | fmt.Fprintln(v, "\n"+strconv.Itoa(len(stagedFiles))+" change(s) added to commit (consider \"add\")") |
| 78 | } |
| 79 | _, cy := v.Cursor() |
| 80 | if cy == 0 { |
| 81 | _ = gui.statusCursorDown(gui.g, v) |
| 82 | } else { |
| 83 | _ = gui.statusCursorUp(gui.g, v) |
| 84 | } |
| 85 | } |
| 86 | return nil |
| 87 | } |
| 88 | |
| 89 | // moves the cursor downwards for the main view and if it goes to bottom it |
| 90 | // prevents from going further |
| 91 | func (gui *Gui) statusCursorDown(g *gocui.Gui, v *gocui.View) error { |
| 92 | if v != nil { |
| 93 | _, cy := v.Cursor() |
| 94 | ox, oy := v.Origin() |
| 95 | ly := v.BufferLines() |
| 96 | |
| 97 | ap := oy + cy |
| 98 | // if we are at the end we just return |
| 99 | if ap == len(ly)-2 { |
| 100 | return nil |
| 101 | } |
| 102 | v.EditDelete(true) |
| 103 | var next int |
| 104 | for i := ap + 1; i < len(ly); i++ { |
| 105 | if len(ly[i]) > 0 && ly[i][0] == ' ' { |
| 106 | next = i - ap |
| 107 | break |
| 108 | } |
| 109 | } |
| 110 | if err := v.SetCursor(0, cy+next); err != nil { |
| 111 | if err := v.SetOrigin(ox, oy+next); err != nil { |
| 112 | return err |
| 113 | } |
| 114 | } |
| 115 | v.EditWrite('→') |
| 116 | } |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | // moves the cursor upwards for the main view |
| 121 | func (gui *Gui) statusCursorUp(g *gocui.Gui, v *gocui.View) error { |
| 122 | if v != nil { |
| 123 | ox, oy := v.Origin() |
| 124 | _, cy := v.Cursor() |
| 125 | ly := v.BufferLines() |
| 126 | v.EditDelete(true) |
| 127 | ap := oy + cy |
| 128 | var prev int |
| 129 | for i := ap - 1; i >= 0; i-- { |
| 130 | if i < len(ly) && len(ly[i]) > 0 && ly[i][0] == ' ' { |
| 131 | prev = ap - i |
| 132 | break |
| 133 | } |
| 134 | } |
| 135 | if err := v.SetCursor(0, cy-prev); err != nil && oy > 0 { |
| 136 | if err := v.SetOrigin(ox, oy-prev); err != nil { |
| 137 | return err |
| 138 | } |
| 139 | } |
| 140 | v.EditWrite('→') |
| 141 | } |
| 142 | return nil |
| 143 | } |
| 144 | |
| 145 | // add or reset file |
| 146 | func (gui *Gui) statusAddReset(g *gocui.Gui, v *gocui.View) error { |
| 147 | _, cy := v.Cursor() |
| 148 | line, err := v.Line(cy) |
| 149 | if err != nil { |
| 150 | return err |
| 151 | } |
| 152 | r := gui.getSelectedRepository() |
| 153 | files, err := command.Status(r) |
| 154 | if err != nil { |
| 155 | return err |
| 156 | } |
| 157 | for _, f := range files { |
| 158 | if strings.Contains(line, f.Name) { |
| 159 | if f.X != git.StatusNotupdated && f.X != git.StatusUntracked && f.X != git.StatusIgnored && f.X != git.StatusUpdated { |
| 160 | if err := command.Reset(r, f, &command.ResetOptions{}); err != nil { |
| 161 | return err |
| 162 | } |
| 163 | } |
| 164 | if f.Y != git.StatusNotupdated { |
| 165 | if err := command.Add(r, f, &command.AddOptions{}); err != nil { |
| 166 | return err |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | return gui.initFocusStat(r) |
| 172 | } |
| 173 | |
| 174 | // show diff of the file |
| 175 | func (gui *Gui) statusDiff(g *gocui.Gui, v *gocui.View) error { |
| 176 | |
| 177 | _, cy := v.Cursor() |
| 178 | line, err := v.Line(cy) |
| 179 | if err != nil { |
| 180 | return err |
| 181 | } |
| 182 | r := gui.getSelectedRepository() |
| 183 | files, err := command.Status(r) |
| 184 | if err != nil { |
| 185 | return err |
| 186 | } |
| 187 | for _, f := range files { |
| 188 | if strings.Contains(line, f.Name) { |
| 189 | out, err := command.DiffFile(f) |
| 190 | if err != nil { |
| 191 | v.Clear() |
| 192 | v.Title = string(FileDiffMode) |
| 193 | if err := gui.updateDynamicKeybindings(); err != nil { |
| 194 | return err |
| 195 | } |
| 196 | fmt.Fprintln(v, "Can't get diff") |
| 197 | return nil |
| 198 | } |
| 199 | v.Clear() |
| 200 | v.Title = string(FileDiffMode) |
| 201 | if err := gui.updateDynamicKeybindings(); err != nil { |
| 202 | return err |
| 203 | } |
| 204 | fmt.Fprintln(v, strings.Join(colorizeDiff(out), "\n")) |
| 205 | } |
| 206 | } |
| 207 | return nil |
| 208 | } |
| 209 | |
| 210 | // stash uncommitted changes of the working directory |
| 211 | func (gui *Gui) stashChanges(g *gocui.Gui, v *gocui.View) error { |
| 212 | r := gui.getSelectedRepository() |
| 213 | output, err := r.Stash() |
| 214 | if err != nil { |
| 215 | if err = gui.openErrorView(g, output, |
| 216 | "You should manually resolve this issue", |
| 217 | stashViewFeature.Name); err != nil { |
| 218 | return err |
| 219 | } |
| 220 | } |
| 221 | if err := gui.focusToRepository(g, v); err != nil { |
| 222 | return err |
| 223 | } |
| 224 | if err := gui.initStashedView(r); err != nil { |
| 225 | return err |
| 226 | } |
| 227 | return nil |
| 228 | } |
| 229 | |
| 230 | // stats of current status, basically emulates "git status" |
| 231 | func (gui *Gui) statusStat(g *gocui.Gui, v *gocui.View) error { |
| 232 | |
| 233 | r := gui.getSelectedRepository() |
| 234 | if err := gui.initFocusStat(r); err != nil { |
| 235 | return err |
| 236 | } |
| 237 | return nil |
| 238 | } |
| 239 | |
| 240 | // add all items to index |
| 241 | func (gui *Gui) statusAddAll(g *gocui.Gui, v *gocui.View) error { |
| 242 | |
| 243 | r := gui.getSelectedRepository() |
| 244 | if err := command.AddAll(r, &command.AddOptions{}); err != nil { |
| 245 | return err |
| 246 | } |
| 247 | if err := gui.initFocusStat(r); err != nil { |
| 248 | return err |
| 249 | } |
| 250 | return nil |
| 251 | } |
| 252 | |
| 253 | // reset all indexed items |
| 254 | func (gui *Gui) statusResetAll(g *gocui.Gui, v *gocui.View) error { |
| 255 | |
| 256 | r := gui.getSelectedRepository() |
| 257 | ref, err := r.Repo.Head() |
| 258 | if err != nil { |
| 259 | return err |
| 260 | } |
| 261 | if err := command.ResetAll(r, &command.ResetOptions{ |
| 262 | Hash: ref.Hash().String(), |
| 263 | ResetType: command.ResetMixed, |
| 264 | CommandMode: command.ModeNative, |
| 265 | }); err != nil { |
| 266 | return err |
| 267 | } |
| 268 | if err := gui.initFocusStat(r); err != nil { |
| 269 | return err |
| 270 | } |
| 271 | return nil |
| 272 | } |