|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + originNamesInLookupOrder = []string{"upstream", "github", "origin"} |
| 14 | + remotesRegexp = regexp.MustCompile(`(.+)\s+(.+)\s+\((push|fetch)\)`) |
| 15 | +) |
| 16 | + |
| 17 | +func BranchShortName(ref string) string { |
| 18 | + reg := regexp.MustCompile("^refs/(remotes/)?.+?/") |
| 19 | + return reg.ReplaceAllString(ref, "") |
| 20 | +} |
| 21 | + |
| 22 | +func ConfigAll(name string) ([]string, error) { |
| 23 | + mode := "--get-all" |
| 24 | + if strings.Contains(name, "*") { |
| 25 | + mode = "--get-regexp" |
| 26 | + } |
| 27 | + |
| 28 | + output, err := execGit("config", mode, name) |
| 29 | + if err != nil { |
| 30 | + return nil, fmt.Errorf("unknown config %s", name) |
| 31 | + } |
| 32 | + return splitLines(output), nil |
| 33 | +} |
| 34 | + |
| 35 | +func CurrentBranch() (string, error) { |
| 36 | + head, err := Head() |
| 37 | + if err != nil { |
| 38 | + return "", fmt.Errorf("aborted: not currently on any branch") |
| 39 | + } |
| 40 | + return head, nil |
| 41 | +} |
| 42 | + |
| 43 | +var cachedDir string |
| 44 | + |
| 45 | +func Dir() (string, error) { |
| 46 | + if cachedDir != "" { |
| 47 | + return cachedDir, nil |
| 48 | + } |
| 49 | + |
| 50 | + output, err := execGitQuiet("rev-parse", "-q", "--git-dir") |
| 51 | + if err != nil { |
| 52 | + return "", fmt.Errorf("not a git repository (or any of the parent directories): .git") |
| 53 | + } |
| 54 | + |
| 55 | + var chdir string |
| 56 | + // for i, flag := range GlobalFlags { |
| 57 | + // if flag == "-C" { |
| 58 | + // dir := GlobalFlags[i+1] |
| 59 | + // if filepath.IsAbs(dir) { |
| 60 | + // chdir = dir |
| 61 | + // } else { |
| 62 | + // chdir = filepath.Join(chdir, dir) |
| 63 | + // } |
| 64 | + // } |
| 65 | + // } |
| 66 | + |
| 67 | + gitDir := firstLine(output) |
| 68 | + |
| 69 | + if !filepath.IsAbs(gitDir) { |
| 70 | + if chdir != "" { |
| 71 | + gitDir = filepath.Join(chdir, gitDir) |
| 72 | + } |
| 73 | + |
| 74 | + gitDir, err = filepath.Abs(gitDir) |
| 75 | + if err != nil { |
| 76 | + return "", err |
| 77 | + } |
| 78 | + |
| 79 | + gitDir = filepath.Clean(gitDir) |
| 80 | + } |
| 81 | + |
| 82 | + cachedDir = gitDir |
| 83 | + return gitDir, nil |
| 84 | +} |
| 85 | + |
| 86 | +func DefaultBranch(remote string) string { |
| 87 | + if name, err := SymbolicRef(fmt.Sprintf("refs/remotes/%s/HEAD", remote)); err != nil { |
| 88 | + return name |
| 89 | + } |
| 90 | + return "refs/heads/main" |
| 91 | +} |
| 92 | + |
| 93 | +func HasFile(segments ...string) bool { |
| 94 | + // For Git >= 2.5.0 |
| 95 | + if output, err := execGitQuiet("rev-parse", "-q", "--git-path", filepath.Join(segments...)); err == nil { |
| 96 | + if lines := splitLines(output); len(lines) == 1 { |
| 97 | + if _, err := os.Stat(lines[0]); err == nil { |
| 98 | + return true |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return false |
| 104 | +} |
| 105 | + |
| 106 | +func Head() (string, error) { |
| 107 | + return SymbolicRef("HEAD") |
| 108 | +} |
| 109 | + |
| 110 | +func LocalBranches() ([]string, error) { |
| 111 | + output, err := execGit("branch", "--list") |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + branches := []string{} |
| 116 | + for _, branch := range splitLines(output) { |
| 117 | + branches = append(branches, branch[2:]) |
| 118 | + } |
| 119 | + return branches, nil |
| 120 | +} |
| 121 | + |
| 122 | +func MainRemote() (string, error) { |
| 123 | + remotes, err := Remotes() |
| 124 | + if err != nil || len(remotes) == 0 { |
| 125 | + return "", fmt.Errorf("aborted: no git remotes found") |
| 126 | + } |
| 127 | + return remotes[0], nil |
| 128 | +} |
| 129 | + |
| 130 | +func NewRange(a, b string) (*Range, error) { |
| 131 | + output, err := execGitQuiet("rev-parse", "-q", a, b) |
| 132 | + if err != nil { |
| 133 | + return nil, err |
| 134 | + } |
| 135 | + lines := splitLines(output) |
| 136 | + if len(lines) != 2 { |
| 137 | + return nil, fmt.Errorf("can't parse range %s..%s", a, b) |
| 138 | + } |
| 139 | + return &Range{lines[0], lines[1]}, nil |
| 140 | +} |
| 141 | + |
| 142 | +func Remotes() ([]string, error) { |
| 143 | + output, err := execGit("remote", "-v") |
| 144 | + if err != nil { |
| 145 | + return nil, fmt.Errorf("aborted: can't load git remotes") |
| 146 | + } |
| 147 | + |
| 148 | + remoteLines := splitLines(output) |
| 149 | + |
| 150 | + remotesMap := make(map[string]map[string]string) |
| 151 | + for _, r := range remoteLines { |
| 152 | + if remotesRegexp.MatchString(r) { |
| 153 | + match := remotesRegexp.FindStringSubmatch(r) |
| 154 | + name := strings.TrimSpace(match[1]) |
| 155 | + url := strings.TrimSpace(match[2]) |
| 156 | + urlType := strings.TrimSpace(match[3]) |
| 157 | + utm, ok := remotesMap[name] |
| 158 | + if !ok { |
| 159 | + utm = make(map[string]string) |
| 160 | + remotesMap[name] = utm |
| 161 | + } |
| 162 | + utm[urlType] = url |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + remotes := []string{} |
| 167 | + |
| 168 | + for _, name := range originNamesInLookupOrder { |
| 169 | + if _, ok := remotesMap[name]; ok { |
| 170 | + remotes = append(remotes, name) |
| 171 | + delete(remotesMap, name) |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + for name := range remotesMap { |
| 176 | + remotes = append(remotes, name) |
| 177 | + } |
| 178 | + |
| 179 | + return remotes, nil |
| 180 | +} |
| 181 | + |
| 182 | +func Spawn(args ...string) error { |
| 183 | + cmd := exec.Command("git", args...) |
| 184 | + cmd.Stdin = os.Stdin |
| 185 | + cmd.Stdout = os.Stdout |
| 186 | + cmd.Stderr = os.Stderr |
| 187 | + return cmd.Run() |
| 188 | +} |
| 189 | + |
| 190 | +func Quiet(args ...string) bool { |
| 191 | + fmt.Printf("%v\n", args) |
| 192 | + cmd := exec.Command("git", args...) |
| 193 | + cmd.Stderr = os.Stderr |
| 194 | + return cmd.Run() == nil |
| 195 | +} |
| 196 | + |
| 197 | +func SymbolicFullName(name string) (string, error) { |
| 198 | + output, err := execGitQuiet("rev-parse", "--symbolic-full-name", name) |
| 199 | + if err != nil { |
| 200 | + return "", fmt.Errorf("unknown revision or path not in the working tree: %s", name) |
| 201 | + } |
| 202 | + return firstLine(output), nil |
| 203 | +} |
| 204 | + |
| 205 | +func SymbolicRef(ref string) (string, error) { |
| 206 | + output, err := execGit("symbolic-ref", ref) |
| 207 | + if err != nil { |
| 208 | + return "", err |
| 209 | + } |
| 210 | + return firstLine(output), err |
| 211 | +} |
| 212 | + |
| 213 | +func execGit(args ...string) (string, error) { |
| 214 | + cmd := exec.Command("git", args...) |
| 215 | + cmd.Stderr = os.Stderr |
| 216 | + output, err := cmd.Output() |
| 217 | + return string(output), err |
| 218 | +} |
| 219 | + |
| 220 | +func execGitQuiet(args ...string) (string, error) { |
| 221 | + cmd := exec.Command("git", args...) |
| 222 | + output, err := cmd.Output() |
| 223 | + return string(output), err |
| 224 | +} |
| 225 | + |
| 226 | +func splitLines(output string) []string { |
| 227 | + output = strings.TrimSuffix(output, "\n") |
| 228 | + if output == "" { |
| 229 | + return []string{} |
| 230 | + } |
| 231 | + return strings.Split(output, "\n") |
| 232 | +} |
| 233 | + |
| 234 | +func firstLine(output string) string { |
| 235 | + if i := strings.Index(output, "\n"); i >= 0 { |
| 236 | + return output[0:i] |
| 237 | + } |
| 238 | + return output |
| 239 | +} |
0 commit comments