zoekt-archive-index: Add isGitOID
This improves the heuristic for deciding if a reference is a commit or a branch name.
Change-Id: I527e87c1a593ce552f9b4c0e74cfc344540b8364
diff --git a/cmd/zoekt-archive-index/main.go b/cmd/zoekt-archive-index/main.go
index ff20ced..629d202 100644
--- a/cmd/zoekt-archive-index/main.go
+++ b/cmd/zoekt-archive-index/main.go
@@ -36,6 +36,24 @@
return path
}
+// isGitOID checks if the revision is a git OID SHA string.
+//
+// Note: This doesn't mean the SHA exists in a repository, nor does it mean it
+// isn't a ref. Git allows 40-char hexadecimal strings to be references.
+func isGitOID(s string) bool {
+ if len(s) != 40 {
+ return false
+ }
+ for _, r := range s {
+ if !(('0' <= r && r <= '9') ||
+ ('a' <= r && r <= 'f') ||
+ ('A' <= r && r <= 'F')) {
+ return false
+ }
+ }
+ return true
+}
+
type Options struct {
Incremental bool
@@ -55,10 +73,10 @@
}
setRef := func(ref string) {
- if len(ref) == 40 && o.Commit == "" {
+ if isGitOID(ref) && o.Commit == "" {
o.Commit = ref
}
- if len(ref) != 40 && o.Branch == "" {
+ if !isGitOID(ref) && o.Branch == "" {
o.Branch = ref
}
}