| 1 | package gitscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/ |
| 2 | |
| 3 | import ( |
| 4 | "os/execscip-go gomod github.com/golang/go/src go1.16 `os/exec`/" |
| 5 | "regexpscip-go gomod github.com/golang/go/src go1.16 regexp/" |
| 6 | "strconvscip-go gomod github.com/golang/go/src go1.16 strconv/" |
| 7 | "stringsscip-go gomod github.com/golang/go/src go1.16 strings/" |
| 8 | ) |
| 9 | |
| 10 | // StashedItem holds the required fields for a stashed change |
| 11 | type StashedItemscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem# struct { |
| 12 | StashIDscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#StashID. int |
| 13 | BranchNamescip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#BranchName. string |
| 14 | Hashscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#Hash. string |
| 15 | Descriptionscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#Description. string |
| 16 | EntityPathscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#EntityPath. string |
| 17 | } |
| 18 | |
| 19 | func (r *Repositoryscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/Repository#) loadStashedItemsscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/Repository#loadStashedItems().() error { |
| 20 | r.Stashedsscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/Repository#Stasheds. = make([]*StashedItemscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#, 0) |
| 21 | output := stashGetscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/stashGet().(r, "list") |
| 22 | stashIDRegex := regexpscip-go gomod github.com/golang/go/src go1.16 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.16 regexp/MustCompile().(`stash@{[\d]+}:`) |
| 23 | stashIDRegexInt := regexpscip-go gomod github.com/golang/go/src go1.16 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.16 regexp/MustCompile().(`[\d]+`) |
| 24 | stashBranchRegex := regexpscip-go gomod github.com/golang/go/src go1.16 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.16 regexp/MustCompile().(`^(.*?): `) |
| 25 | stashMsgRegex := regexpscip-go gomod github.com/golang/go/src go1.16 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.16 regexp/MustCompile().(`WIP on \(?([^)]*)\)?`) |
| 26 | stashHashRegex := regexpscip-go gomod github.com/golang/go/src go1.16 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.16 regexp/MustCompile().(`[\w|\d]{7}\s`) |
| 27 | |
| 28 | stashlist := stringsscip-go gomod github.com/golang/go/src go1.16 strings/.Splitscip-go gomod github.com/golang/go/src go1.16 strings/Split().(output, "\n") |
| 29 | for _, stashitem := range stashlist { |
| 30 | // find id |
| 31 | id := stashIDRegexInt.FindStringscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#FindString().(stashIDRegex.FindStringscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#FindString().(stashitem)) |
| 32 | i, err := strconvscip-go gomod github.com/golang/go/src go1.16 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.16 strconv/Atoi().(id) |
| 33 | if err != nil { |
| 34 | // probably something isn't right let's continue over this iteration |
| 35 | continue |
| 36 | } |
| 37 | // trim id section |
| 38 | trimmed := stashIDRegex.Splitscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#Split().(stashitem, 2)[1] |
| 39 | |
| 40 | // let's ignore autostash |
| 41 | if trimmed[1:] == "autostash" { |
| 42 | continue |
| 43 | } |
| 44 | // find branch |
| 45 | stashBranchRegexMatch := stashBranchRegex.FindStringscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#FindString().(trimmed) |
| 46 | branchName := stashBranchRegexMatch[:len(stashBranchRegexMatch)-2] |
| 47 | |
| 48 | branchMatches := stashMsgRegex.FindStringSubmatchscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#FindStringSubmatch().(branchName) |
| 49 | if len(branchMatches) >= 2 { |
| 50 | branchName = stashBranchRegexMatch[:len(stashBranchRegexMatch)-2] |
| 51 | } |
| 52 | |
| 53 | // trim branch section |
| 54 | trimmed = stashBranchRegex.Splitscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#Split().(trimmed, 2)[1] |
| 55 | hash := "" |
| 56 | |
| 57 | var desc string |
| 58 | if stashHashRegex.MatchStringscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#MatchString().(trimmed) { |
| 59 | hash = stashHashRegex.FindStringscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#FindString().(trimmed)[:7] |
| 60 | desc = stashHashRegex.Splitscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#Split().(trimmed, 2)[1] |
| 61 | } else { |
| 62 | desc = trimmed |
| 63 | } |
| 64 | // trim hash |
| 65 | |
| 66 | r.Stashedsscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/Repository#Stasheds. = append(r.Stashedsscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/Repository#Stasheds., &StashedItemscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#{ |
| 67 | StashIDscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#StashID.: i, |
| 68 | BranchNamescip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#BranchName.: branchName, |
| 69 | Hashscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#Hash.: hash, |
| 70 | Descriptionscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#Description.: desc, |
| 71 | EntityPathscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#EntityPath.: r.AbsPathscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/Repository#AbsPath., |
| 72 | }) |
| 73 | } |
| 74 | return nil |
| 75 | } |
| 76 | |
| 77 | func stashGetscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/stashGet().(r *Repositoryscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/Repository#, option string) string { |
| 78 | args := make([]string, 0) |
| 79 | args = append(args, "stash") |
| 80 | args = append(args, option) |
| 81 | cmd := execscip-go gomod github.com/golang/go/src go1.16 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Command().("git", args...) |
| 82 | cmd.Dirscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#Dir. = r.AbsPathscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/Repository#AbsPath. |
| 83 | output, err := cmd.CombinedOutputscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#CombinedOutput().() |
| 84 | if err != nil { |
| 85 | return "?" |
| 86 | } |
| 87 | return string(output) |
| 88 | } |
| 89 | |
| 90 | // Pop is the wrapper of "git stash pop" command that used for a file |
| 91 | func (stashedItem *StashedItemscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#) Popscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#Pop().() (string, error) { |
| 92 | args := make([]string, 0) |
| 93 | args = append(args, "stash") |
| 94 | args = append(args, "pop") |
| 95 | args = append(args, "stash@{"+strconvscip-go gomod github.com/golang/go/src go1.16 strconv/.Itoascip-go gomod github.com/golang/go/src go1.16 strconv/Itoa().(stashedItem.StashIDscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#StashID.)+"}") |
| 96 | cmd := execscip-go gomod github.com/golang/go/src go1.16 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Command().("git", args...) |
| 97 | cmd.Dirscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#Dir. = stashedItem.EntityPathscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#EntityPath. |
| 98 | output, err := cmd.CombinedOutputscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#CombinedOutput().() |
| 99 | return string(output), err |
| 100 | } |
| 101 | |
| 102 | // Show is the wrapper of "git stash show -p " command |
| 103 | func (stashedItem *StashedItemscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#) Showscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#Show().() (string, error) { |
| 104 | args := make([]string, 0) |
| 105 | args = append(args, "stash") |
| 106 | args = append(args, "show") |
| 107 | args = append(args, "-p") |
| 108 | args = append(args, "stash@{"+strconvscip-go gomod github.com/golang/go/src go1.16 strconv/.Itoascip-go gomod github.com/golang/go/src go1.16 strconv/Itoa().(stashedItem.StashIDscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#StashID.)+"}") |
| 109 | cmd := execscip-go gomod github.com/golang/go/src go1.16 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Command().("git", args...) |
| 110 | cmd.Dirscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#Dir. = stashedItem.EntityPathscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/StashedItem#EntityPath. |
| 111 | output, err := cmd.CombinedOutputscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#CombinedOutput().() |
| 112 | |
| 113 | return string(output), err |
| 114 | } |
| 115 | |
| 116 | // Stash is the wrapper of conventional "git stash" command |
| 117 | func (r *Repositoryscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/Repository#) Stashscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/Repository#Stash().() (string, error) { |
| 118 | args := make([]string, 0) |
| 119 | args = append(args, "stash") |
| 120 | |
| 121 | cmd := execscip-go gomod github.com/golang/go/src go1.16 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Command().("git", args...) |
| 122 | cmd.Dirscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#Dir. = r.AbsPathscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/Repository#AbsPath. |
| 123 | output, err := cmd.CombinedOutputscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#CombinedOutput().() |
| 124 | _ = r.Refreshscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/git`/Repository#Refresh().() |
| 125 | return string(output), err |
| 126 | } |