2019-03-01 18:44:22 +00:00
|
|
|
{- git-annex remote tracking branches
|
|
|
|
-
|
|
|
|
- Copyright 2019 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Annex.RemoteTrackingBranch
|
|
|
|
( RemoteTrackingBranch
|
|
|
|
, mkRemoteTrackingBranch
|
|
|
|
, fromRemoteTrackingBranch
|
|
|
|
, setRemoteTrackingBranch
|
2019-05-01 17:13:00 +00:00
|
|
|
, makeRemoteTrackingBranchMergeCommit
|
|
|
|
, makeRemoteTrackingBranchMergeCommit'
|
2019-05-01 18:20:26 +00:00
|
|
|
, getRemoteTrackingBranchImportHistory
|
2019-03-01 18:44:22 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Annex.Common
|
2019-05-06 17:56:39 +00:00
|
|
|
import Annex.CatFile
|
2019-03-01 18:44:22 +00:00
|
|
|
import Git.Types
|
|
|
|
import qualified Git.Ref
|
|
|
|
import qualified Git.Branch
|
2019-05-01 18:20:26 +00:00
|
|
|
import Git.History
|
2019-03-01 18:44:22 +00:00
|
|
|
import qualified Types.Remote as Remote
|
|
|
|
|
2019-05-01 18:20:26 +00:00
|
|
|
import qualified Data.Set as S
|
|
|
|
|
2019-03-01 18:44:22 +00:00
|
|
|
newtype RemoteTrackingBranch = RemoteTrackingBranch
|
|
|
|
{ fromRemoteTrackingBranch :: Ref }
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
|
|
{- Makes a remote tracking branch corresponding to a local branch.
|
|
|
|
- Note that the local branch does not need to exist yet. -}
|
|
|
|
mkRemoteTrackingBranch :: Remote -> Branch -> RemoteTrackingBranch
|
|
|
|
mkRemoteTrackingBranch remote ref = RemoteTrackingBranch $
|
|
|
|
Git.Ref.underBase ("refs/remotes/" ++ Remote.name remote) ref
|
|
|
|
|
|
|
|
{- Set remote tracking branch to point to a commit. -}
|
|
|
|
setRemoteTrackingBranch :: RemoteTrackingBranch -> Sha -> Annex ()
|
|
|
|
setRemoteTrackingBranch tb commit =
|
|
|
|
inRepo $ Git.Branch.update' (fromRemoteTrackingBranch tb) commit
|
2019-05-01 17:13:00 +00:00
|
|
|
|
|
|
|
{- Makes a merge commit that preserves the import history of the
|
|
|
|
- RemoteTrackingBranch, while grafting new git history into it.
|
|
|
|
-
|
|
|
|
- The second parent of the merge commit is the past history of the
|
|
|
|
- RemoteTrackingBranch as imported from a remote. When importing a
|
|
|
|
- history of trees from a remote, commits can be sythesized from
|
|
|
|
- them, but such commits won't have the same sha due to eg date differing.
|
|
|
|
- But since we know that the second parent consists entirely of such
|
|
|
|
- import commits, they can be reused when updating the
|
|
|
|
- RemoteTrackingBranch.
|
|
|
|
-}
|
2019-05-06 17:56:39 +00:00
|
|
|
makeRemoteTrackingBranchMergeCommit :: RemoteTrackingBranch -> Sha -> Annex Sha
|
|
|
|
makeRemoteTrackingBranchMergeCommit tb commitsha =
|
2019-05-01 17:13:00 +00:00
|
|
|
-- Check if the tracking branch exists.
|
|
|
|
inRepo (Git.Ref.sha (fromRemoteTrackingBranch tb)) >>= \case
|
|
|
|
Nothing -> return commitsha
|
2019-05-01 18:20:26 +00:00
|
|
|
Just _ -> inRepo (getHistoryToDepth 1 (fromRemoteTrackingBranch tb)) >>= \case
|
2019-05-01 17:13:00 +00:00
|
|
|
Nothing -> return commitsha
|
2019-05-01 18:20:26 +00:00
|
|
|
Just (History hc _) -> case historyCommitParents hc of
|
2019-05-06 18:38:02 +00:00
|
|
|
[_, importhistory] -> do
|
|
|
|
treesha <- maybe
|
|
|
|
(giveup $ "Unable to cat commit " ++ fromRef commitsha)
|
|
|
|
commitTree
|
|
|
|
<$> catCommit commitsha
|
|
|
|
makeRemoteTrackingBranchMergeCommit' commitsha importhistory treesha
|
2019-05-01 17:13:00 +00:00
|
|
|
-- Earlier versions of git-annex did not
|
|
|
|
-- make the merge commit, or perhaps
|
|
|
|
-- something else changed where the
|
|
|
|
-- tracking branch pointed.
|
|
|
|
_ -> return commitsha
|
|
|
|
|
2019-05-06 18:38:02 +00:00
|
|
|
makeRemoteTrackingBranchMergeCommit' :: Sha -> Sha -> Sha -> Annex Sha
|
|
|
|
makeRemoteTrackingBranchMergeCommit' commitsha importedhistory treesha =
|
2019-05-01 17:13:00 +00:00
|
|
|
inRepo $ Git.Branch.commitTree
|
|
|
|
Git.Branch.AutomaticCommit
|
|
|
|
"remote tracking branch"
|
|
|
|
[commitsha, importedhistory]
|
|
|
|
treesha
|
2019-05-01 18:20:26 +00:00
|
|
|
|
|
|
|
{- When makeRemoteTrackingBranchMergeCommit was used, this finds the
|
|
|
|
- import history, starting from the second parent of the merge commit.
|
|
|
|
-}
|
|
|
|
getRemoteTrackingBranchImportHistory :: History HistoryCommit -> Maybe (History HistoryCommit)
|
|
|
|
getRemoteTrackingBranchImportHistory (History hc s) =
|
|
|
|
case historyCommitParents hc of
|
|
|
|
[_, importhistory] -> go importhistory (S.toList s)
|
|
|
|
_ -> Nothing
|
|
|
|
where
|
|
|
|
go _ [] = Nothing
|
|
|
|
go i (h@(History hc' _):hs)
|
|
|
|
| historyCommit hc' == i = Just h
|
|
|
|
| otherwise = go i hs
|