2020-11-16 18:09:55 +00:00
|
|
|
{- adjusted branch merging
|
|
|
|
-
|
2023-07-06 16:42:00 +00:00
|
|
|
- Copyright 2016-2023 Joey Hess <id@joeyh.name>
|
2020-11-16 18:09:55 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE BangPatterns, OverloadedStrings #-}
|
|
|
|
|
|
|
|
module Annex.AdjustedBranch.Merge (
|
2023-07-06 16:42:00 +00:00
|
|
|
canMergeToAdjustedBranch,
|
2020-11-16 18:09:55 +00:00
|
|
|
mergeToAdjustedBranch,
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Annex.Common
|
|
|
|
import Annex.AdjustedBranch
|
|
|
|
import qualified Annex
|
|
|
|
import Git
|
|
|
|
import Git.Types
|
|
|
|
import qualified Git.Branch
|
|
|
|
import qualified Git.Ref
|
|
|
|
import qualified Git.Command
|
|
|
|
import qualified Git.Merge
|
|
|
|
import Git.Sha
|
|
|
|
import Annex.CatFile
|
|
|
|
import Annex.AutoMerge
|
|
|
|
import Annex.Tmp
|
|
|
|
import Annex.GitOverlay
|
|
|
|
import Utility.Tmp.Dir
|
|
|
|
import Utility.CopyFile
|
|
|
|
import Utility.Directory.Create
|
|
|
|
|
|
|
|
import qualified Data.ByteString as S
|
|
|
|
import qualified System.FilePath.ByteString as P
|
|
|
|
|
2023-07-06 16:42:00 +00:00
|
|
|
canMergeToAdjustedBranch :: Branch -> (OrigBranch, Adjustment) -> Annex Bool
|
|
|
|
canMergeToAdjustedBranch tomerge (origbranch, adj) =
|
|
|
|
inRepo $ Git.Branch.changed currbranch tomerge
|
|
|
|
where
|
|
|
|
AdjBranch currbranch = originalToAdjusted origbranch adj
|
|
|
|
|
2020-11-16 18:09:55 +00:00
|
|
|
{- Update the currently checked out adjusted branch, merging the provided
|
|
|
|
- branch into it. Note that the provided branch should be a non-adjusted
|
|
|
|
- branch. -}
|
|
|
|
mergeToAdjustedBranch :: Branch -> (OrigBranch, Adjustment) -> [Git.Merge.MergeConfig] -> Bool -> Git.Branch.CommitMode -> Annex Bool
|
|
|
|
mergeToAdjustedBranch tomerge (origbranch, adj) mergeconfig canresolvemerge commitmode = catchBoolIO $
|
|
|
|
join $ preventCommits go
|
|
|
|
where
|
|
|
|
adjbranch@(AdjBranch currbranch) = originalToAdjusted origbranch adj
|
|
|
|
basis = basisBranch adjbranch
|
|
|
|
|
2023-07-06 16:42:00 +00:00
|
|
|
go commitsprevented = do
|
|
|
|
(updatedorig, _) <- propigateAdjustedCommits'
|
|
|
|
False origbranch adj commitsprevented
|
|
|
|
changestomerge updatedorig
|
2020-11-16 18:09:55 +00:00
|
|
|
|
|
|
|
{- Since the adjusted branch changes files, merging tomerge
|
2022-08-19 21:45:04 +00:00
|
|
|
- directly into it would likely result in unnecessary merge
|
2020-11-16 18:09:55 +00:00
|
|
|
- conflicts. To avoid those conflicts, instead merge tomerge into
|
|
|
|
- updatedorig. The result of the merge can the be
|
|
|
|
- adjusted to yield the final adjusted branch.
|
|
|
|
-
|
|
|
|
- In order to do a merge into a ref that is not checked out,
|
|
|
|
- set the work tree to a temp directory, and set GIT_DIR
|
|
|
|
- to another temp directory, in which HEAD contains the
|
|
|
|
- updatedorig sha. GIT_COMMON_DIR is set to point to the real
|
|
|
|
- git directory, and so git can read and write objects from there,
|
|
|
|
- but will use GIT_DIR for HEAD and index.
|
|
|
|
-
|
|
|
|
- (Doing the merge this way also lets it run even though the main
|
|
|
|
- index file is currently locked.)
|
|
|
|
-}
|
|
|
|
changestomerge (Just updatedorig) = withOtherTmp $ \othertmpdir -> do
|
|
|
|
git_dir <- fromRepo Git.localGitDir
|
|
|
|
let git_dir' = fromRawFilePath git_dir
|
|
|
|
tmpwt <- fromRepo gitAnnexMergeDir
|
|
|
|
withTmpDirIn (fromRawFilePath othertmpdir) "git" $ \tmpgit -> withWorkTreeRelated tmpgit $
|
|
|
|
withemptydir git_dir tmpwt $ withWorkTree tmpwt $ do
|
|
|
|
liftIO $ writeFile (tmpgit </> "HEAD") (fromRef updatedorig)
|
|
|
|
-- Copy in refs and packed-refs, to work
|
|
|
|
-- around bug in git 2.13.0, which
|
|
|
|
-- causes it not to look in GIT_DIR for refs.
|
2023-08-15 16:57:41 +00:00
|
|
|
refs <- liftIO $ emptyWhenDoesNotExist $
|
|
|
|
dirContentsRecursive $
|
|
|
|
git_dir' </> "refs"
|
2020-11-16 18:09:55 +00:00
|
|
|
let refs' = (git_dir' </> "packed-refs") : refs
|
2022-06-22 20:47:34 +00:00
|
|
|
liftIO $ forM_ refs' $ \src -> do
|
|
|
|
let src' = toRawFilePath src
|
2020-11-16 18:09:55 +00:00
|
|
|
whenM (doesFileExist src) $ do
|
2022-06-22 20:47:34 +00:00
|
|
|
dest <- relPathDirToFile git_dir src'
|
2020-11-16 18:09:55 +00:00
|
|
|
let dest' = toRawFilePath tmpgit P.</> dest
|
2022-08-12 16:45:46 +00:00
|
|
|
createDirectoryUnder [git_dir]
|
2020-11-16 18:09:55 +00:00
|
|
|
(P.takeDirectory dest')
|
2022-06-22 20:47:34 +00:00
|
|
|
void $ createLinkOrCopy src' dest'
|
2020-11-16 18:09:55 +00:00
|
|
|
-- This reset makes git merge not care
|
|
|
|
-- that the work tree is empty; otherwise
|
|
|
|
-- it will think that all the files have
|
|
|
|
-- been staged for deletion, and sometimes
|
|
|
|
-- the merge includes these deletions
|
|
|
|
-- (for an unknown reason).
|
|
|
|
-- http://thread.gmane.org/gmane.comp.version-control.git/297237
|
|
|
|
inRepo $ Git.Command.run [Param "reset", Param "HEAD", Param "--quiet"]
|
2023-07-06 16:42:00 +00:00
|
|
|
when (tomerge /= origbranch) $
|
|
|
|
showAction $ UnquotedString $ "Merging into " ++ fromRef (Git.Ref.base origbranch)
|
2020-11-16 18:09:55 +00:00
|
|
|
merged <- autoMergeFrom' tomerge Nothing mergeconfig commitmode True
|
|
|
|
(const $ resolveMerge (Just updatedorig) tomerge True)
|
|
|
|
if merged
|
|
|
|
then do
|
|
|
|
!mergecommit <- liftIO $ extractSha
|
|
|
|
<$> S.readFile (tmpgit </> "HEAD")
|
|
|
|
-- This is run after the commit lock is dropped.
|
|
|
|
return $ postmerge mergecommit
|
|
|
|
else return $ return False
|
|
|
|
changestomerge Nothing = return $ return False
|
|
|
|
|
|
|
|
withemptydir git_dir d a = bracketIO setup cleanup (const a)
|
|
|
|
where
|
|
|
|
setup = do
|
|
|
|
whenM (doesDirectoryExist d) $
|
|
|
|
removeDirectoryRecursive d
|
2022-08-12 16:45:46 +00:00
|
|
|
createDirectoryUnder [git_dir] (toRawFilePath d)
|
2020-11-16 18:09:55 +00:00
|
|
|
cleanup _ = removeDirectoryRecursive d
|
|
|
|
|
|
|
|
{- A merge commit has been made between the basisbranch and
|
|
|
|
- tomerge. Update the basisbranch and origbranch to point
|
|
|
|
- to that commit, adjust it to get the new adjusted branch,
|
|
|
|
- and check it out.
|
|
|
|
-
|
|
|
|
- But, there may be unstaged work tree changes that conflict,
|
|
|
|
- so the check out is done by making a normal merge of
|
|
|
|
- the new adjusted branch.
|
|
|
|
-}
|
|
|
|
postmerge (Just mergecommit) = do
|
|
|
|
setBasisBranch basis mergecommit
|
|
|
|
inRepo $ Git.Branch.update' origbranch mergecommit
|
|
|
|
adjtree <- adjustTree adj (BasisBranch mergecommit)
|
|
|
|
adjmergecommit <- commitAdjustedTree adjtree (BasisBranch mergecommit)
|
|
|
|
-- Make currbranch be the parent, so that merging
|
|
|
|
-- this commit will be a fast-forward.
|
|
|
|
adjmergecommitff <- commitAdjustedTree' adjtree (BasisBranch mergecommit) [currbranch]
|
|
|
|
showAction "Merging into adjusted branch"
|
|
|
|
ifM (autoMergeFrom adjmergecommitff (Just currbranch) mergeconfig commitmode canresolvemerge)
|
|
|
|
( reparent adjtree adjmergecommit =<< getcurrentcommit
|
|
|
|
, return False
|
|
|
|
)
|
|
|
|
postmerge Nothing = return False
|
|
|
|
|
|
|
|
-- Now that the merge into the adjusted branch is complete,
|
|
|
|
-- take the tree from that merge, and attach it on top of the
|
|
|
|
-- adjmergecommit, if it's different.
|
|
|
|
reparent adjtree adjmergecommit (Just currentcommit) = do
|
|
|
|
if (commitTree currentcommit /= adjtree)
|
|
|
|
then do
|
|
|
|
cmode <- annexCommitMode <$> Annex.getGitConfig
|
|
|
|
c <- inRepo $ Git.Branch.commitTree cmode
|
2024-04-09 16:56:47 +00:00
|
|
|
["Merged " ++ fromRef tomerge]
|
|
|
|
[adjmergecommit]
|
2020-11-16 18:09:55 +00:00
|
|
|
(commitTree currentcommit)
|
|
|
|
inRepo $ Git.Branch.update "updating adjusted branch" currbranch c
|
|
|
|
propigateAdjustedCommits origbranch adj
|
|
|
|
else inRepo $ Git.Branch.update "updating adjusted branch" currbranch adjmergecommit
|
|
|
|
return True
|
|
|
|
reparent _ _ Nothing = return False
|
|
|
|
|
|
|
|
getcurrentcommit = inRepo Git.Branch.currentUnsafe >>= \case
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Just c -> catCommit c
|