@@ -712,56 +712,30 @@ func doInitRemoteAnnexRepository(t *testing.T, repoURL *url.URL) error {
712
712
}
713
713
714
714
/* 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.
728
716
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 })
740
722
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
742
724
}
743
725
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 ) )
749
731
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 })
757
733
if err != nil {
758
- return "" , err
734
+ return "" , fmt . Errorf ( "in %s: %s does not seem to be annexed: %w" , repoPath , file , err )
759
735
}
736
+ contentPath = strings .TrimSpace (contentPath )
760
737
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
765
739
}
766
740
767
741
/* like withKeyFile(), but automatically sets it the account given in ctx for use by git-annex */
0 commit comments