mirror of
https://github.com/vee1e/gittuf.git
synced 2026-09-01 18:27:06 +00:00
LoadRepository did os.Chdir(repositoryPath) to run 'git rev-parse --git-dir', and Status/RestoreWorktree/tree-restore did the same to run worktree-relative commands. os.Chdir is process-global; concurrent LoadRepository calls for different paths could each resolve the wrong gitDirPath. Adds executor.withDir(dir) which sets cmd.Dir. LoadRepository now runs 'git rev-parse --absolute-git-dir' with cmd.Dir set to the repository path (and EvalSymlinks the result to match prior behaviour). The worktree commands use withDir(worktree). TestLoadRepositoryConcurrent runs 20 pairs of LoadRepository for two distinct repos in parallel under -race and asserts each gets its own gitDirPath. Signed-off-by: Andrew Nesbitt <andrewnez@gmail.com>
65 lines
2 KiB
Go
65 lines
2 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 := r.gitDirPath
|
|
if !r.IsBare() {
|
|
worktree = strings.TrimSuffix(worktree, ".git") // TODO: this doesn't support detached git dir
|
|
}
|
|
|
|
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, " ", "__"))
|
|
}
|