Files
zzet--gortex/cmd/gortex/git.go
T
wehub-resource-sync a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

63 lines
1.8 KiB
Go

package main
import (
"bytes"
"os/exec"
"strings"
"github.com/zzet/gortex/internal/churn"
"github.com/zzet/gortex/internal/indexer"
)
// gitCommitHash returns the HEAD commit hash for the repository at dir,
// or an empty string if git is unavailable or the directory is not a repo.
func gitCommitHash(dir string) string {
cmd := exec.Command("git", "rev-parse", "HEAD")
cmd.Dir = dir
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = nil
if err := cmd.Run(); err != nil {
return ""
}
return strings.TrimSpace(out.String())
}
// gitBranch returns the current branch name for the repository at dir.
// It returns an empty string when git is unavailable, the directory is
// not a repo, or HEAD is detached — callers then key snapshots by
// commit hash instead of branch.
func gitBranch(dir string) string {
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
cmd.Dir = dir
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = nil
if err := cmd.Run(); err != nil {
return ""
}
branch := strings.TrimSpace(out.String())
if branch == "HEAD" {
return "" // detached HEAD — no branch to key on
}
return branch
}
// canonicalRepo resolves a git worktree to the main repository it
// shares a .git directory with, so every worktree of one repo keys its
// index cache under a shared base — the per-branch snapshot slot then
// gives each worktree its own entry. A non-worktree path is returned
// unchanged.
func canonicalRepo(dir string) string {
return indexer.ResolveWorktree(dir).MainRepoPath
}
// gitDefaultBranch returns the repository's default branch as a
// rev-parseable reference. Thin wrapper over churn.DefaultBranch so
// the CLI, daemon controller, and MCP tool resolve the same branch
// the same way.
func gitDefaultBranch(dir string) string {
return churn.DefaultBranch(dir)
}