gittuf/pkg/gitinterface/utils.go
lakshit verma a7e72c4c05
gitinterface: resolve worktrees for detached git dirs and worktrees
Worktree resolution chopped ".git" off the resolved GIT_DIR path, which
only works for the standard <repo>/.git layout. This breaks repositories
created with --separate-git-dir, misclassifies linked worktrees as bare
(so file restoration was silently skipped), and misclassifies bare
repositories named foo.git as non-bare. Linked worktrees could not even
be loaded because ensureNoCompatObjectFormat expects a config file that
only exists in the repository's common Git directory.

- add Repository.GetWorktree() resolving via $GIT_DIR/gitdir,
  core.worktree, load-time discovery, then the standard layout;
  validated so stale or malformed records fall through cleanly
- determine bareness via rev-parse --is-bare-repository instead of the
  GIT_DIR name, exposed through the ErrNoWorktree sentinel
- read configuration from the common directory via $GIT_DIR/commondir
- route Status(), RestoreWorktree(), and post-propagation restore
  through GetWorktree()

Fixes #1006

Signed-off-by: lakshit verma <vermalucky2004@gmail.com>
2026-08-24 16:16:00 +05:30

65 lines
1.9 KiB
Go

// Copyright The gittuf Authors
// SPDX-License-Identifier: Apache-2.0
package gitinterface
import (
"fmt"
"path"
"strings"
"testing"
)
// ResetDueToError reverses a change applied to a ref to the specified target
// ID. It is used to ensure a gittuf operation is atomic: if a gittuf operation
// fails, any changes made to the repository in refs/gittuf can be rolled back.
// Worktrees are not updated.
func (r *Repository) ResetDueToError(cause error, refName string, commitID Hash) error {
if err := r.SetReference(refName, commitID); err != nil {
return fmt.Errorf("unable to reset %s to %s, caused by following error: %w", refName, commitID.String(), cause)
}
return cause
}
func RemoteRef(refName, remoteName string) string {
var remotePath string
switch {
case strings.HasPrefix(refName, BranchRefPrefix):
// refs/heads/<path> -> refs/remotes/<remote>/<path>
rest := strings.TrimPrefix(refName, BranchRefPrefix)
remotePath = path.Join(RemoteRefPrefix, remoteName, rest)
case strings.HasPrefix(refName, TagRefPrefix):
// refs/tags/<path> -> refs/tags/<path>
remotePath = refName
default:
// refs/<path> -> refs/remotes/<remote>/<path>
rest := strings.TrimPrefix(refName, RefPrefix)
remotePath = path.Join(RemoteRefPrefix, remoteName, rest)
}
return remotePath
}
// RestoreWorktree is a test helper to fix the worktree in tests where we need
// to operate in a checked out copy of the repository. This is primarily needed
// for support with older Git versions.
func (r *Repository) RestoreWorktree(t *testing.T) {
t.Helper()
worktree, err := r.GetWorktree()
if err != nil {
t.Fatal(err)
}
if _, err := r.executor("restore", "--staged", ".").withDir(worktree).executeString(); err != nil {
t.Fatal(err)
}
if _, err := r.executor("restore", ".").withDir(worktree).executeString(); err != nil {
t.Fatal(err)
}
}
func testNameToRefName(testName string) string {
return BranchReferenceName(strings.ReplaceAll(testName, " ", "__"))
}