| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "os/exec" |
| 5 | "regexp" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // StashedItem holds the required fields for a stashed change |
| 11 | type StashedItem struct { |
| 12 | StashID int |
| 13 | BranchName string |
| 14 | Hash string |
| 15 | Description string |
| 16 | EntityPath string |
| 17 | } |
| 18 | |
| 19 | func (r *Repository) loadStashedItems() error { |
| 20 | r.Stasheds = make([]*StashedItem, 0) |
| 21 | output := stashGet(r, "list") |
| 22 | stashIDRegex := regexp.MustCompile(`stash@{[\d]+}:`) |
| 23 | stashIDRegexInt := regexp.MustCompile(`[\d]+`) |
| 24 | stashBranchRegex := regexp.MustCompile(`^(.*?): `) |
| 25 | stashMsgRegex := regexp.MustCompile(`WIP on \(?([^)]*)\)?`) |
| 26 | stashHashRegex := regexp.MustCompile(`[\w|\d]{7}\s`) |
| 27 | |
| 28 | stashlist := strings.Split(output, "\n") |
| 29 | for _, stashitem := range stashlist { |
| 30 | // find id |
| 31 | id := stashIDRegexInt.FindString(stashIDRegex.FindString(stashitem)) |
| 32 | i, err := 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.Split(stashitem, 2)[1] |
| 39 | |
| 40 | // let's ignore autostash |
| 41 | if trimmed[1:] == "autostash" { |
| 42 | continue |
| 43 | } |
| 44 | // find branch |
| 45 | stashBranchRegexMatch := stashBranchRegex.FindString(trimmed) |
| 46 | branchName := stashBranchRegexMatch[:len(stashBranchRegexMatch)-2] |
| 47 | |
| 48 | branchMatches := stashMsgRegex.FindStringSubmatch(branchName) |
| 49 | if len(branchMatches) >= 2 { |
| 50 | branchName = stashBranchRegexMatch[:len(stashBranchRegexMatch)-2] |
| 51 | } |
| 52 | |
| 53 | // trim branch section |
| 54 | trimmed = stashBranchRegex.Split(trimmed, 2)[1] |
| 55 | hash := "" |
| 56 | |
| 57 | var desc string |
| 58 | if stashHashRegex.MatchString(trimmed) { |
| 59 | hash = stashHashRegex.FindString(trimmed)[:7] |
| 60 | desc = stashHashRegex.Split(trimmed, 2)[1] |
| 61 | } else { |
| 62 | desc = trimmed |
| 63 | } |
| 64 | // trim hash |
| 65 | |
| 66 | r.Stasheds = append(r.Stasheds, &StashedItem{ |
| 67 | StashID: i, |
| 68 | BranchName: branchName, |
| 69 | Hash: hash, |
| 70 | Description: desc, |
| 71 | EntityPath: r.AbsPath, |
| 72 | }) |
| 73 | } |
| 74 | return nil |
| 75 | } |
| 76 | |
| 77 | func stashGet(r *Repository, option string) string { |
| 78 | args := make([]string, 0) |
| 79 | args = append(args, "stash") |
| 80 | args = append(args, option) |
| 81 | cmd := exec.Command("git", args...) |
| 82 | cmd.Dir = r.AbsPath |
| 83 | output, err := 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 *StashedItem) Pop() (string, error) { |
| 92 | args := make([]string, 0) |
| 93 | args = append(args, "stash") |
| 94 | args = append(args, "pop") |
| 95 | args = append(args, "stash@{"+strconv.Itoa(stashedItem.StashID)+"}") |
| 96 | cmd := exec.Command("git", args...) |
| 97 | cmd.Dir = stashedItem.EntityPath |
| 98 | output, err := cmd.CombinedOutput() |
| 99 | return string(output), err |
| 100 | } |
| 101 | |
| 102 | // Show is the wrapper of "git stash show -p " command |
| 103 | func (stashedItem *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@{"+strconv.Itoa(stashedItem.StashID)+"}") |
| 109 | cmd := exec.Command("git", args...) |
| 110 | cmd.Dir = stashedItem.EntityPath |
| 111 | output, err := cmd.CombinedOutput() |
| 112 | |
| 113 | return string(output), err |
| 114 | } |
| 115 | |
| 116 | // Stash is the wrapper of conventional "git stash" command |
| 117 | func (r *Repository) Stash() (string, error) { |
| 118 | args := make([]string, 0) |
| 119 | args = append(args, "stash") |
| 120 | |
| 121 | cmd := exec.Command("git", args...) |
| 122 | cmd.Dir = r.AbsPath |
| 123 | output, err := cmd.CombinedOutput() |
| 124 | _ = r.Refresh() |
| 125 | return string(output), err |
| 126 | } |