2014-11-24 20:14:01 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2023-06-28 19:26:46 +00:00
|
|
|
- Copyright 2014-2023 Joey Hess <id@joeyh.name>
|
2014-11-24 20:14:01 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2014-11-24 20:14:01 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.DiffDriver where
|
|
|
|
|
|
|
|
import Command
|
|
|
|
import Annex.Content
|
|
|
|
import Annex.Link
|
|
|
|
import Git.Types
|
2023-08-22 15:58:13 +00:00
|
|
|
import qualified Command.Get
|
2014-11-24 20:14:01 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2023-08-22 15:58:13 +00:00
|
|
|
cmd = command "diffdriver" SectionPlumbing
|
|
|
|
"git diff driver"
|
|
|
|
("-- cmd --") (seek <$$> optParser)
|
2014-11-24 20:14:01 +00:00
|
|
|
|
2023-06-28 19:26:46 +00:00
|
|
|
data Options = Options
|
|
|
|
{ textDiff :: Bool
|
2023-08-22 15:58:13 +00:00
|
|
|
, getOption :: Bool
|
2023-06-28 19:26:46 +00:00
|
|
|
, restOptions :: CmdParams
|
|
|
|
}
|
2014-11-24 20:14:01 +00:00
|
|
|
|
2023-06-28 19:26:46 +00:00
|
|
|
optParser :: CmdParamsDesc -> Parser Options
|
|
|
|
optParser desc = Options
|
|
|
|
<$> switch
|
|
|
|
( long "text"
|
|
|
|
<> help "diff text files with diff(1)"
|
|
|
|
)
|
2023-08-22 15:58:13 +00:00
|
|
|
<*> switch
|
|
|
|
( long "get"
|
|
|
|
<> help "get file contents from remotes"
|
|
|
|
)
|
2023-06-28 19:26:46 +00:00
|
|
|
<*> cmdParams desc
|
|
|
|
|
|
|
|
seek :: Options -> CommandSeek
|
2023-08-22 15:58:13 +00:00
|
|
|
seek opts = do
|
2014-11-24 20:14:01 +00:00
|
|
|
let (req, differ) = parseReq opts
|
2023-08-22 15:58:13 +00:00
|
|
|
void $ liftIO . exitBool =<< liftIO . differ =<< fixupReq req opts
|
2014-11-24 20:14:01 +00:00
|
|
|
|
|
|
|
data Req
|
|
|
|
= Req
|
|
|
|
{ rPath :: FilePath
|
|
|
|
, rOldFile :: FilePath
|
|
|
|
, rOldHex :: String
|
|
|
|
, rOldMode :: String
|
|
|
|
, rNewFile :: FilePath
|
|
|
|
, rNewHex :: String
|
|
|
|
, rNewMode ::String
|
|
|
|
}
|
|
|
|
| UnmergedReq
|
|
|
|
{ rPath :: FilePath
|
|
|
|
}
|
|
|
|
|
|
|
|
type Differ = Req -> IO Bool
|
|
|
|
|
|
|
|
serializeReq :: Req -> [CommandParam]
|
|
|
|
serializeReq req@(UnmergedReq {}) = [Param $ rPath req]
|
|
|
|
serializeReq req@(Req {}) = map Param
|
|
|
|
[ rPath req
|
|
|
|
, rOldFile req
|
|
|
|
, rOldHex req
|
|
|
|
, rOldMode req
|
|
|
|
, rNewFile req
|
|
|
|
, rNewHex req
|
|
|
|
, rNewMode req
|
|
|
|
]
|
|
|
|
|
2023-06-28 19:26:46 +00:00
|
|
|
parseReq :: Options -> (Req, Differ)
|
|
|
|
parseReq opts
|
2023-07-05 19:43:29 +00:00
|
|
|
| textDiff opts = case separate (== "--") (restOptions opts) of
|
|
|
|
(_,[]) -> (mk (restOptions opts), textDiffer [])
|
|
|
|
(ps,rest) -> (mk rest, textDiffer ps)
|
2023-06-28 19:26:46 +00:00
|
|
|
| otherwise = case separate (== "--") (restOptions opts) of
|
|
|
|
(c:ps, l) -> (mk l, externalDiffer c ps)
|
|
|
|
([],_) -> badopts
|
2014-11-24 20:14:01 +00:00
|
|
|
where
|
|
|
|
mk (path:old_file:old_hex:old_mode:new_file:new_hex:new_mode:[]) =
|
|
|
|
Req
|
|
|
|
{ rPath = path
|
|
|
|
, rOldFile = old_file
|
|
|
|
, rOldHex = old_hex
|
|
|
|
, rOldMode = old_mode
|
|
|
|
, rNewFile = new_file
|
|
|
|
, rNewHex = new_hex
|
|
|
|
, rNewMode = new_mode
|
|
|
|
}
|
|
|
|
mk (unmergedpath:[]) = UnmergedReq { rPath = unmergedpath }
|
|
|
|
mk _ = badopts
|
|
|
|
|
2023-06-28 19:26:46 +00:00
|
|
|
badopts = giveup $ "Unexpected input: " ++ unwords (restOptions opts)
|
2014-11-24 20:14:01 +00:00
|
|
|
|
|
|
|
{- Check if either file is a symlink to a git-annex object,
|
|
|
|
- which git-diff will leave as a normal file containing the link text.
|
2021-04-08 18:32:09 +00:00
|
|
|
-
|
|
|
|
- Also check if either file is a pointer file, as used for unlocked files.
|
|
|
|
-
|
|
|
|
- In either case, adjust the Req to instead point to the actual
|
|
|
|
- location of the annexed object (which may or may not be present).
|
2023-08-22 15:58:13 +00:00
|
|
|
-
|
|
|
|
- This also gets objects from remotes when the getOption is set.
|
2021-04-08 18:32:09 +00:00
|
|
|
-}
|
2023-08-22 15:58:13 +00:00
|
|
|
fixupReq :: Req -> Options -> Annex Req
|
|
|
|
fixupReq req@(UnmergedReq {}) _ = return req
|
|
|
|
fixupReq req@(Req {}) opts =
|
2023-09-21 17:38:36 +00:00
|
|
|
check rOldFile rOldMode setoldfile req
|
|
|
|
>>= check rNewFile rNewMode setnewfile
|
2014-11-24 20:14:01 +00:00
|
|
|
where
|
2023-09-21 17:38:36 +00:00
|
|
|
setoldfile r@(Req {}) f = r { rOldFile = f }
|
|
|
|
setoldfile r@(UnmergedReq {}) _ = r
|
|
|
|
setnewfile r@(Req {}) f = r { rNewFile = f }
|
|
|
|
setnewfile r@(UnmergedReq {}) _ = r
|
2021-08-11 00:45:02 +00:00
|
|
|
check getfile getmode setfile r = case readTreeItemType (encodeBS (getmode r)) of
|
2018-05-14 18:22:44 +00:00
|
|
|
Just TreeSymlink -> do
|
2021-04-08 18:32:09 +00:00
|
|
|
v <- getAnnexLinkTarget' f False
|
2023-08-22 15:58:13 +00:00
|
|
|
maybe (return r) go (parseLinkTargetOrPointer =<< v)
|
|
|
|
_ -> maybe (return r) go =<< liftIO (isPointerFile f)
|
2021-04-08 18:32:09 +00:00
|
|
|
where
|
2023-08-22 15:58:13 +00:00
|
|
|
f = toRawFilePath (getfile r)
|
|
|
|
go k = do
|
|
|
|
when (getOption opts) $
|
|
|
|
unlessM (inAnnex k) $
|
|
|
|
commandAction $
|
|
|
|
starting "get" ai si $
|
|
|
|
Command.Get.perform k af
|
|
|
|
repoint k
|
|
|
|
where
|
|
|
|
ai = OnlyActionOn k (ActionItemKey k)
|
|
|
|
si = SeekInput []
|
|
|
|
af = AssociatedFile (Just f)
|
2021-04-08 18:32:09 +00:00
|
|
|
repoint k = withObjectLoc k $
|
|
|
|
pure . setfile r . fromRawFilePath
|
2014-11-24 20:14:01 +00:00
|
|
|
|
|
|
|
externalDiffer :: String -> [String] -> Differ
|
|
|
|
externalDiffer c ps = \req -> boolSystem c (map Param ps ++ serializeReq req )
|
2023-06-28 19:26:46 +00:00
|
|
|
|
2023-07-05 19:43:29 +00:00
|
|
|
textDiffer :: [String] -> Differ
|
|
|
|
textDiffer diffopts req = do
|
2023-06-28 19:26:46 +00:00
|
|
|
putStrLn ("diff a/" ++ rPath req ++ " b/" ++ rPath req)
|
|
|
|
-- diff exits nonzero on difference, so ignore exit status
|
2023-07-05 19:43:29 +00:00
|
|
|
void $ boolSystem "diff" $
|
2023-06-28 19:26:46 +00:00
|
|
|
[ Param "-u"
|
|
|
|
, Param (rOldFile req)
|
|
|
|
, Param (rNewFile req)
|
2023-07-05 19:43:29 +00:00
|
|
|
] ++ map Param diffopts
|
2023-06-28 19:26:46 +00:00
|
|
|
return True
|