reinject: Fix crash when reinjecting a file from outside the repository

Commit 4bf7940d6b introduced this
problem, but was otherwise doing a good thing. Problem being
that fileRef "/foo" used to return ":./foo", which was actually wrong,
but as long as there was no foo in the local repository, catKey
could operate on it without crashing. After that fix though, fileRef
would return eg "../../foo", resulting in fileRef returning
":./../../foo", which will make git cat-file crash since that's
not a valid path in the repo.

Fix is simply to make fileRef detect paths outside the repo and return
Nothing. Then catKey can be skipped. This needed several bugfixes to
dirContains as well, in previous commits.

In Command.Smudge, this led to needing to check for Nothing. That case
should actually never happen, because the fileoutsiderepo check will
detect it earlier.

Sponsored-by: Brock Spratlen on Patreon
This commit is contained in:
Joey Hess 2021-10-01 14:04:18 -04:00
parent b2efbd1cd3
commit 9012fa0187
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
7 changed files with 48 additions and 22 deletions

View file

@ -198,11 +198,12 @@ catSymLinkTarget sha = fromInternalGitPath . L.toStrict <$> get
catKeyFile :: RawFilePath -> Annex (Maybe Key)
catKeyFile f = ifM (Annex.getState Annex.daemon)
( catKeyFileHEAD f
, catKey =<< liftIO (Git.Ref.fileRef f)
, maybe (pure Nothing) catKey =<< inRepo (Git.Ref.fileRef f)
)
catKeyFileHEAD :: RawFilePath -> Annex (Maybe Key)
catKeyFileHEAD f = catKey =<< liftIO (Git.Ref.fileFromRef Git.Ref.headRef f)
catKeyFileHEAD f = maybe (pure Nothing) catKey
=<< inRepo (Git.Ref.fileFromRef Git.Ref.headRef f)
{- Look in the original branch from whence an adjusted branch is based
- to find the file. But only when the adjustment hides some files. -}
@ -215,5 +216,6 @@ catObjectMetaDataHidden = hiddenCat catObjectMetaData
hiddenCat :: (Ref -> Annex (Maybe a)) -> RawFilePath -> CurrBranch -> Annex (Maybe a)
hiddenCat a f (Just origbranch, Just adj)
| adjustmentHidesFiles adj =
a =<< liftIO (Git.Ref.fileFromRef origbranch f)
maybe (pure Nothing) a
=<< inRepo (Git.Ref.fileFromRef origbranch f)
hiddenCat _ _ _ = return Nothing

View file

@ -13,6 +13,8 @@ git-annex (8.20210904) UNRELEASED; urgency=medium
was interrupted.
* sync --content: Avoid a redundant checksum of a file that was
incrementally verified, when used on NTFS and perhaps other filesystems.
* reinject: Fix crash when reinjecting a file from outside the repository.
(Reversion in version 8.20210621)
-- Joey Hess <id@joeyh.name> Fri, 03 Sep 2021 12:02:55 -0400

View file

@ -105,13 +105,14 @@ clean file = do
addingExistingLink file k $ do
getMoveRaceRecovery k file
liftIO $ L.hPut stdout b
Nothing -> do
fileref <- liftIO $ Git.Ref.fileRef file
Nothing -> inRepo (Git.Ref.fileRef file) >>= \case
Just fileref -> do
indexmeta <- catObjectMetaData fileref
oldkey <- case indexmeta of
Just (_, sz, _) -> catKey' fileref sz
Nothing -> return Nothing
go' b indexmeta oldkey
Nothing -> liftIO $ L.hPut stdout b
go' b indexmeta oldkey = ifM (shouldAnnex file indexmeta oldkey)
( do
-- Before git 2.5, failing to consume all stdin here

2
Git.hs
View file

@ -71,7 +71,7 @@ repoLocation Repo { location = LocalUnknown dir } = fromRawFilePath dir
repoLocation Repo { location = Unknown } = error "unknown repoLocation"
{- Path to a repository. For non-bare, this is the worktree, for bare,
- it's the gitdit, and for URL repositories, is the path on the remote
- it's the gitdir, and for URL repositories, is the path on the remote
- host. -}
repoPath :: Repo -> RawFilePath
repoPath Repo { location = Url u } = toRawFilePath $ unEscapeString $ uriPath u

View file

@ -64,17 +64,22 @@ branchRef = underBase "refs/heads"
{- A Ref that can be used to refer to a file in the repository, as staged
- in the index.
-
- If the input file is located outside the repository, returns Nothing.
-}
fileRef :: RawFilePath -> IO Ref
fileRef f = do
fileRef :: RawFilePath -> Repo -> IO (Maybe Ref)
fileRef f repo = do
-- The filename could be absolute, or contain eg "../repo/file",
-- neither of which work in a ref, so convert it to a minimal
-- relative path.
f' <- relPathCwdToFile f
print ("f'", f', repoPath repo, repoPath repo `dirContains` f')
return $ if repoPath repo `dirContains` f'
-- Prefixing the file with ./ makes this work even when in a
-- subdirectory of a repo. Eg, ./foo in directory bar refers
-- to bar/foo, not to foo in the top of the repository.
return $ Ref $ ":./" <> toInternalGitPath f'
then Just $ Ref $ ":./" <> toInternalGitPath f'
else Nothing
{- A Ref that can be used to refer to a file in a particular branch. -}
branchFileRef :: Branch -> RawFilePath -> Ref
@ -85,11 +90,14 @@ dateRef :: Ref -> RefDate -> Ref
dateRef r (RefDate d) = Ref $ fromRef' r <> "@" <> encodeBS d
{- A Ref that can be used to refer to a file in the repository as it
- appears in a given Ref. -}
fileFromRef :: Ref -> RawFilePath -> IO Ref
fileFromRef r f = do
(Ref fr) <- fileRef f
return (Ref (fromRef' r <> fr))
- appears in a given Ref.
-
- If the file path is located outside the repository, returns Nothing.
-}
fileFromRef :: Ref -> RawFilePath -> Repo -> IO (Maybe Ref)
fileFromRef r f repo = fileRef f repo >>= return . \case
Just (Ref fr) -> Just (Ref (fromRef' r <> fr))
Nothing -> Nothing
{- Checks if a ref exists. Note that it must be fully qualified,
- eg refs/heads/master rather than master. -}

View file

@ -24,3 +24,5 @@ By means of bisection I have determined that commit 4bf7940d6b912fbf692b268f621e
git-annex: fd:15: Data.ByteString.hGetLine: end of file
--spwhitton
> [[fixed|done]] --[[Joey]]

View file

@ -0,0 +1,11 @@
[[!comment format=mdwn
username="joey"
subject="""comment 1"""
date="2021-10-01T16:42:36Z"
content="""
Also happens with a relative path to the file. And also
`git annex reinject ../bar bar` fails the same way.
Fixed. In case you want to cherry-pick the fix, it's the commit adding
this comment, as well as the 2 prior commits fixing bugs in dirContains.
"""]]