shithub: furgit

Download patch

ref: 9d8e9f07083e3e3c85f27bc0583258bce0266509
parent: c4710f2f9b4f88d2e5c633dd96afea42ee732cec
author: Runxi Yu <me@runxiyu.org>
date: Sat Mar 7 09:46:42 EST 2026

refstore: Remove Shorten for now

--- a/refstore/chain/shorten.go
+++ /dev/null
@@ -1,33 +1,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
-}
--- a/refstore/refstore.go
+++ b/refstore/refstore.go
@@ -34,12 +34,6 @@
 	//
 	// 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
 }
--- a/refstore/shorten.go
+++ /dev/null
@@ -1,87 +1,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
-}
--- a/refstore/shorten_test.go
+++ /dev/null
@@ -1,77 +1,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")
-		}
-	})
-}
--