From 9d8e9f07083e3e3c85f27bc0583258bce0266509 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sat, 7 Mar 2026 14:46:42 +0800 Subject: refstore: Remove Shorten for now --- refstore/chain/shorten.go | 33 ------------------ refstore/refstore.go | 6 ---- refstore/shorten.go | 87 ----------------------------------------------- refstore/shorten_test.go | 77 ----------------------------------------- 4 files changed, 203 deletions(-) delete mode 100644 refstore/chain/shorten.go delete mode 100644 refstore/shorten.go delete mode 100644 refstore/shorten_test.go (limited to 'refstore') diff --git a/refstore/chain/shorten.go b/refstore/chain/shorten.go deleted file mode 100644 index 9e755de4..00000000 --- a/refstore/chain/shorten.go +++ /dev/null @@ -1,33 +0,0 @@ -package chain - -import "codeberg.org/lindenii/furgit/refstore" - -// Shorten shortens a full reference name using the chain-visible namespace. -func (chain *Chain) Shorten(name string) (string, error) { - refs, err := chain.List("") - if err != nil { - return "", err - } - - names := make([]string, 0, len(refs)) - found := false - - for _, entry := range refs { - if entry == nil { - continue - } - - full := entry.Name() - - names = append(names, full) - if full == name { - found = true - } - } - - if !found { - return "", refstore.ErrReferenceNotFound - } - - return refstore.ShortenName(name, names), nil -} diff --git a/refstore/refstore.go b/refstore/refstore.go index 8d37b427..a952ae22 100644 --- a/refstore/refstore.go +++ b/refstore/refstore.go @@ -34,12 +34,6 @@ type ReadingStore interface { // // The exact pattern language is backend-defined. List(pattern string) ([]ref.Ref, error) - // Shorten returns the shortest unambiguous shorthand for a full - // reference name within this store's visible namespace. - // - // If name does not exist in this store, implementations should return - // ErrReferenceNotFound. - Shorten(name string) (string, error) // Close releases resources associated with the store. Close() error } diff --git a/refstore/shorten.go b/refstore/shorten.go deleted file mode 100644 index ae20a52e..00000000 --- a/refstore/shorten.go +++ /dev/null @@ -1,87 +0,0 @@ -package refstore - -import "strings" - -type shortenRule struct { - prefix string - suffix string -} - -//nolint:gochecknoglobals -var shortenRules = [...]shortenRule{ - {prefix: "", suffix: ""}, - {prefix: "refs/", suffix: ""}, - {prefix: "refs/tags/", suffix: ""}, - {prefix: "refs/heads/", suffix: ""}, - {prefix: "refs/remotes/", suffix: ""}, - {prefix: "refs/remotes/", suffix: "/HEAD"}, -} - -func (rule shortenRule) match(name string) (string, bool) { - if !strings.HasPrefix(name, rule.prefix) { - return "", false - } - - if !strings.HasSuffix(name, rule.suffix) { - return "", false - } - - short := strings.TrimPrefix(name, rule.prefix) - - short = strings.TrimSuffix(short, rule.suffix) - if short == "" { - return "", false - } - - if rule.prefix+short+rule.suffix != name { - return "", false - } - - return short, true -} - -func (rule shortenRule) render(short string) string { - return rule.prefix + short + rule.suffix -} - -// ShortenName returns the shortest unambiguous shorthand for name among all. -// -// all must contain full reference names visible to the shortening scope. -func ShortenName(name string, all []string) string { - names := make(map[string]struct{}, len(all)) - for _, full := range all { - if full == "" { - continue - } - - names[full] = struct{}{} - } - - for i := len(shortenRules) - 1; i > 0; i-- { - short, ok := shortenRules[i].match(name) - if !ok { - continue - } - - ambiguous := false - - for j := range shortenRules { - if j == i { - continue - } - - full := shortenRules[j].render(short) - if _, found := names[full]; found { - ambiguous = true - - break - } - } - - if !ambiguous { - return short - } - } - - return name -} diff --git a/refstore/shorten_test.go b/refstore/shorten_test.go deleted file mode 100644 index a4d91453..00000000 --- a/refstore/shorten_test.go +++ /dev/null @@ -1,77 +0,0 @@ -package refstore_test - -import ( - "testing" - - "codeberg.org/lindenii/furgit/refstore" -) - -func TestShortenName(t *testing.T) { - t.Parallel() - - t.Run("simple", func(t *testing.T) { - t.Parallel() - - got := refstore.ShortenName("refs/heads/main", []string{"refs/heads/main"}) - if got != "main" { - t.Fatalf("ShortenName simple = %q, want %q", got, "main") - } - }) - - t.Run("ambiguous with tags", func(t *testing.T) { - t.Parallel() - - got := refstore.ShortenName( - "refs/heads/main", - []string{ - "refs/heads/main", - "refs/tags/main", - }, - ) - if got != "heads/main" { - t.Fatalf("ShortenName tags ambiguity = %q, want %q", got, "heads/main") - } - }) - - t.Run("strict remote head ambiguity", func(t *testing.T) { - t.Parallel() - // In strict mode, refs/remotes/%s/HEAD blocks shortening to "%s". - got := refstore.ShortenName( - "refs/heads/main", - []string{ - "refs/heads/main", - "refs/remotes/main/HEAD", - }, - ) - if got != "heads/main" { - t.Fatalf("ShortenName strict ambiguity = %q, want %q", got, "heads/main") - } - }) - - t.Run("deep fallback still shortens", func(t *testing.T) { - t.Parallel() - // refs/remotes/origin/main conflicts with refs/heads/origin/main for - // "origin/main", so it should fall back to "remotes/origin/main". - got := refstore.ShortenName( - "refs/remotes/origin/main", - []string{ - "refs/remotes/origin/main", - "refs/heads/origin/main", - }, - ) - if got != "remotes/origin/main" { - t.Fatalf("ShortenName deep fallback = %q, want %q", got, "remotes/origin/main") - } - }) - - t.Run("refs-prefix fallback", func(t *testing.T) { - t.Parallel() - - name := "refs/notes/review/topic" - - got := refstore.ShortenName(name, []string{name}) - if got != "notes/review/topic" { - t.Fatalf("ShortenName refs-prefix fallback = %q, want %q", got, "notes/review/topic") - } - }) -} -- cgit v1.3.1-10-gc9f91