add warning on add of annex link
authorJoey Hess <joeyh@joeyh.name>
Tue, 10 Nov 2020 16:10:51 +0000 (12:10 -0400)
committerJoey Hess <joeyh@joeyh.name>
Tue, 10 Nov 2020 16:10:51 +0000 (12:10 -0400)
Warn when adding a annex symlink or pointer file that uses a key that is
not known to the repository, to prevent confusion if the user has copied it
from some other repository.

This commit was sponsored by Jake Vosloo on Patreon.

Annex/Ingest.hs
CHANGELOG
Command/Add.hs
Command/Smudge.hs
doc/bugs/file_not_correctly_added.mdwn

index 3fa601347fff7eda5e69817af0e82dc116a8c6d2..0e5a7a2452f83a705e6f3d1f36cede619f37173d 100644 (file)
@@ -21,6 +21,7 @@ module Annex.Ingest (
        CheckGitIgnore(..),
        gitAddParams,
        addAnnexedFile,
+       addingExistingLink,
 ) where
 
 import Annex.Common
@@ -387,3 +388,19 @@ addAnnexedFile ci matcher file key mtmp = ifM (addUnlocked matcher mi)
                _ -> return ()
        
        writepointer mode = liftIO $ writePointerFile file key mode
+
+{- Use with actions that add an already existing annex symlink or pointer
+ - file. The warning avoids a confusing situation where the file got copied
+ - from another git-annex repo, probably by accident. -}
+addingExistingLink :: RawFilePath -> Key -> Annex a -> Annex a
+addingExistingLink f k a = do
+       unlessM (isKnownKey k <||> inAnnex k) $ do
+               islink <- isJust <$> isAnnexLink f
+               warning $ unwords
+                       [ fromRawFilePath f
+                       , "is a git-annex"
+                       , if islink then "symlink." else "pointer file."
+                       , "Its content is not available in this repository."
+                       , "(Maybe " ++ fromRawFilePath f ++ " was copied from another repository?)"
+                       ]
+       a
index 151940e525e7b6d0eb9123762e20f87c610b67ea..e12ce250fd638bae0bd92654d7479ec23f7df825 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -3,6 +3,9 @@ git-annex (8.20201104) UNRELEASED; urgency=medium
   * sync --content: Fix a bug where a file that was not preferred content
     could be transferred to a remote. This happened when the file got deleted
     after the sync started running.
+  * Warn when adding a annex symlink or pointer file that uses a key that
+    is not known to the repository, to prevent confusion if the user has
+    copied it from some other repository.
 
  -- Joey Hess <id@joeyh.name>  Mon, 09 Nov 2020 15:15:20 -0400
 
index 800b56bf87cf1ca740dc932997d92314b69fe482..614b406e7370db9e46df1d585fd30c7974d8c873 100644 (file)
@@ -27,8 +27,6 @@ import Utility.FileMode
 import Utility.OptParse
 import qualified Utility.RawFilePath as R
 
-import System.Log.Logger (debugM)
-
 cmd :: Command
 cmd = notBareRepo $ 
        withGlobalOptions opts $
@@ -171,16 +169,17 @@ start o si file addunlockedmatcher = do
                liftIO (catchMaybeIO $ R.getSymbolicLinkStatus file) >>= \case
                        Just s | isSymbolicLink s -> fixuplink key
                        _ -> add
-       fixuplink key = starting "add" (ActionItemWorkTreeFile file) si $ do
-               liftIO $ debugM "add" "adding existing annex symlink to git"
-               liftIO $ removeFile (fromRawFilePath file)
-               addLink (checkGitIgnoreOption o) file key Nothing
-               next $
-                       cleanup key =<< inAnnex key
-       fixuppointer key = starting "add" (ActionItemWorkTreeFile file) si $ do
-               liftIO $ debugM "add" "adding pointer file to git"
-               Database.Keys.addAssociatedFile key =<< inRepo (toTopFilePath file)
-               next $ addFile (checkGitIgnoreOption o) file
+       fixuplink key = 
+               starting "add" (ActionItemWorkTreeFile file) si $
+                       addingExistingLink file key $ do
+                               liftIO $ removeFile (fromRawFilePath file)
+                               addLink (checkGitIgnoreOption o) file key Nothing
+                               next $ cleanup key =<< inAnnex key
+       fixuppointer key =
+               starting "add" (ActionItemWorkTreeFile file) si $
+                       addingExistingLink file key $ do
+                               Database.Keys.addAssociatedFile key =<< inRepo (toTopFilePath file)
+                               next $ addFile (checkGitIgnoreOption o) file
 
 perform :: AddOptions -> RawFilePath -> AddUnlockedMatcher -> CommandPerform
 perform o file addunlockedmatcher = withOtherTmp $ \tmpdir -> do
index 007608c9b9b29bd2e9b0b134290d2226461517ea..ebf041018ae4d7a0ee16035137e1ed69f7fa72a5 100644 (file)
@@ -98,8 +98,9 @@ clean file = do
   where
        go b = case parseLinkTargetOrPointerLazy b of
                Just k -> do
-                       getMoveRaceRecovery k file
-                       liftIO $ L.hPut stdout b
+                       addingExistingLink file k $ do
+                               getMoveRaceRecovery k file
+                               liftIO $ L.hPut stdout b
                Nothing -> do
                        let fileref = Git.Ref.fileRef file
                        indexmeta <- catObjectMetaData fileref
index 6ef068f8c99c133fd1f145539f82e02e8507cd75..cb88fa9d8fa406dfb34d92cf511858d5d758cb5d 100644 (file)
@@ -219,3 +219,5 @@ $ tree -a
 
 ### Have you had any luck using git-annex before? (Sometimes we get tired of reading bug reports all day and a lil' positive end note does wonders)
 It used to work before. It seem to be broken since a system update. I have tried reinstalling `git-annex` as well as all dependencies, bit without any luck.
+
+> Added warning. [[done]] --[[Joey]]