GetDefaultBranch read git default branch.
(nameVersion, repoDir string)
| 158 | |
| 159 | // GetDefaultBranch read git default branch. |
| 160 | func GetDefaultBranch(nameVersion, repoDir string) (string, error) { |
| 161 | title := fmt.Sprintf("[read default branch: %s]", nameVersion) |
| 162 | executor := cmd.NewExecutor(title, "git", "remote", "show", "origin") |
| 163 | executor.SetWorkDir(repoDir) |
| 164 | output, err := executor.ExecuteOutput() |
| 165 | if err != nil { |
| 166 | return "", fmt.Errorf("read git default branch: %w", err) |
| 167 | } |
| 168 | |
| 169 | lines := strings.SplitSeq(string(output), "\n") |
| 170 | for line := range lines { |
| 171 | if strings.Contains(line, "HEAD branch") { |
| 172 | parts := strings.Split(line, ":") |
| 173 | if len(parts) > 1 { |
| 174 | return strings.TrimSpace(parts[1]), nil |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return "", fmt.Errorf("default branch not found of %s", repoDir) |
| 180 | } |
| 181 | |
| 182 | // shortHash returns a concise git hash format: first 7 chars...last 7 chars |
| 183 | func shortHash(hash string) string { |
no test coverage detected