Skip to content

Commit ace51f8

Browse files
kousuactions-user
authored andcommitted
git-annex tests: allow old-style annex symlinks
Previously annexObjectPath() only supported new-style git-annex-smudge files, and would reject old-style annex symlinks files as "does not appear to be annexed". This covers both cases. It also shortens the code by deferring to git-annex-contentlocation(1); there's no need to compute the hashdir nor the hashdirformat, all that is already handled.
1 parent ca00636 commit ace51f8

File tree

1 file changed

+16
-42
lines changed

1 file changed

+16
-42
lines changed

tests/integration/git_annex_test.go

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -712,56 +712,30 @@ func doInitRemoteAnnexRepository(t *testing.T, repoURL *url.URL) error {
712712
}
713713

714714
/* given a git repo and a path to an annexed file in it (assumed to be committed to its HEAD),
715-
find the path in .git/annex/objects/ that contains its actual contents. */
716-
func annexObjectPath(repoPath string, file string) (string, error) {
717-
718-
// find the path inside *.git/annex/objects of a given file
719-
// i.e. figure out its two-level hash prefix: https://git-annex.branchable.com/internals/hashing/
720-
// ASSUMES the target file is checked into HEAD
721-
722-
var bare bool // whether the repo is bare or not; this changes what the hashing algorithm is, due to backwards compatibility
723-
724-
bareStr, _, err := git.NewCommand(git.DefaultContext, "config", "core.bare").RunStdString(&git.RunOpts{Dir: repoPath})
725-
if err != nil {
726-
return "", err
727-
}
715+
find the path in .git/annex/objects/ that contains its actual contents.
728716
729-
if bareStr == "true\n" {
730-
bare = true
731-
} else if bareStr == "false\n" {
732-
bare = false
733-
} else {
734-
return "", errors.New(fmt.Sprintf("Could not determine if %s is a bare repo or not; git config core.bare = <%s>", repoPath, bareStr))
735-
}
736-
737-
// given a repo and a file in it
738-
// TODO: handle other branches, e.g. non-HEAD branches etc
739-
annexKey, _, err := git.NewCommand(git.DefaultContext, "show", "HEAD:"+file).RunStdString(&git.RunOpts{Dir: repoPath})
717+
TODO: pass a parameter to allow examining non-HEAD branches
718+
*/
719+
func annexObjectPath(repoPath string, file string) (string, error) {
720+
// NB: `git annex lookupkey` is more reliable, but doesn't work in bare repos.
721+
annexKey, _, err := git.NewCommandNoGlobals("show", "HEAD:"+file).RunStdString(&git.RunOpts{Dir: repoPath})
740722
if err != nil {
741-
return "", err
723+
return "", fmt.Errorf("in %s: %w", repoPath, err) // the error from git prints the filename but not repo
742724
}
743725

744-
annexKey = strings.TrimSpace(annexKey)
745-
if !strings.HasPrefix(annexKey, "/annex/objects/") {
746-
return "", errors.New(fmt.Sprintf("%s/%s does not appear to be annexed .", repoPath, file))
747-
}
748-
annexKey = strings.TrimPrefix(annexKey, "/annex/objects/")
726+
// There are two formats an annexed file pointer might be:
727+
// * a symlink to .git/annex/objects/$HASHDIR/$ANNEX_KEY/$ANNEX_KEY - used by files created with 'git annex add'
728+
// * a text file containing /annex/objects/$ANNEX_KEY - used by files for which 'git add' was configured to run git-annex-smudge
729+
// This recovers $ANNEX_KEY from either case:
730+
annexKey = path.Base(strings.TrimSpace(annexKey))
749731

750-
var keyformat string
751-
if bare {
752-
keyformat = "hashdirlower"
753-
} else {
754-
keyformat = "hashdirmixed"
755-
}
756-
keyHashPrefix, _, err := git.NewCommand(git.DefaultContext, "annex", "examinekey", "--format=${"+keyformat+"}", annexKey).RunStdString(&git.RunOpts{Dir: repoPath})
732+
contentPath, _, err := git.NewCommandNoGlobals("annex", "contentlocation", annexKey).RunStdString(&git.RunOpts{Dir: repoPath})
757733
if err != nil {
758-
return "", err
734+
return "", fmt.Errorf("in %s: %s does not seem to be annexed: %w", repoPath, file, err)
759735
}
736+
contentPath = strings.TrimSpace(contentPath)
760737

761-
if !bare {
762-
repoPath = path.Join(repoPath, ".git")
763-
}
764-
return path.Join(repoPath, "annex", "objects", keyHashPrefix, annexKey, annexKey), nil
738+
return path.Join(repoPath, contentPath), nil
765739
}
766740

767741
/* like withKeyFile(), but automatically sets it the account given in ctx for use by git-annex */

0 commit comments

Comments
 (0)