ResolveFirstParent resolves the first parent of a commit. Returns hasParent=false for root commits.
(repoPath, commitID string)
| 40 | // ResolveFirstParent resolves the first parent of a commit. |
| 41 | // Returns hasParent=false for root commits. |
| 42 | func ResolveFirstParent(repoPath, commitID string) (parent string, hasParent bool, err error) { |
| 43 | if err := validateCommit(repoPath, commitID); err != nil { |
| 44 | return "", false, err |
| 45 | } |
| 46 | |
| 47 | stdout, stderr, err := runGitCommand(repoPath, "rev-list", "--parents", "-n", "1", commitID) |
| 48 | if err != nil { |
| 49 | return "", false, gitCommandError(err, stderr) |
| 50 | } |
| 51 | |
| 52 | fields := strings.Fields(strings.TrimSpace(string(stdout))) |
| 53 | if len(fields) < 1 { |
| 54 | return "", false, fmt.Errorf("unexpected git rev-list output for commit %s", commitID) |
| 55 | } |
| 56 | if len(fields) == 1 { |
| 57 | return "", false, nil |
| 58 | } |
| 59 | |
| 60 | return fields[1], true, nil |
| 61 | } |
| 62 | |
| 63 | func ensureRepoRoot(repoPath string) (string, error) { |
| 64 | if _, err := os.Stat(repoPath); os.IsNotExist(err) { |
no test coverage detected