Class DirCacheCheckout


  • public class DirCacheCheckout
    extends java.lang.Object
    This class handles checking out one or two trees merging with the index.
    • Field Detail

      • LOG

        private static final org.slf4j.Logger LOG
      • MAX_EXCEPTION_TEXT_SIZE

        private static final int MAX_EXCEPTION_TEXT_SIZE
        See Also:
        Constant Field Values
      • conflicts

        private java.util.ArrayList<java.lang.String> conflicts
      • removed

        private java.util.ArrayList<java.lang.String> removed
      • kept

        private java.util.ArrayList<java.lang.String> kept
      • mergeCommitTree

        private ObjectId mergeCommitTree
      • headCommitTree

        private ObjectId headCommitTree
      • failOnConflict

        private boolean failOnConflict
      • force

        private boolean force
      • toBeDeleted

        private java.util.ArrayList<java.lang.String> toBeDeleted
      • initialCheckout

        private boolean initialCheckout
      • performingCheckout

        private boolean performingCheckout
    • Constructor Detail

      • DirCacheCheckout

        public DirCacheCheckout​(Repository repo,
                                ObjectId headCommitTree,
                                DirCache dc,
                                ObjectId mergeCommitTree,
                                WorkingTreeIterator workingTree)
                         throws java.io.IOException
        Constructs a DirCacheCeckout for merging and checking out two trees (HEAD and mergeCommitTree) and the index.
        Parameters:
        repo - the repository in which we do the checkout
        headCommitTree - the id of the tree of the head commit
        dc - the (already locked) Dircache for this repo
        mergeCommitTree - the id of the tree we want to fast-forward to
        workingTree - an iterator over the repositories Working Tree
        Throws:
        java.io.IOException
      • DirCacheCheckout

        public DirCacheCheckout​(Repository repo,
                                ObjectId headCommitTree,
                                DirCache dc,
                                ObjectId mergeCommitTree)
                         throws java.io.IOException
        Constructs a DirCacheCeckout for merging and checking out two trees (HEAD and mergeCommitTree) and the index. As iterator over the working tree this constructor creates a standard FileTreeIterator
        Parameters:
        repo - the repository in which we do the checkout
        headCommitTree - the id of the tree of the head commit
        dc - the (already locked) Dircache for this repo
        mergeCommitTree - the id of the tree we want to fast-forward to
        Throws:
        java.io.IOException
      • DirCacheCheckout

        public DirCacheCheckout​(Repository repo,
                                DirCache dc,
                                ObjectId mergeCommitTree,
                                WorkingTreeIterator workingTree)
                         throws java.io.IOException
        Constructs a DirCacheCeckout for checking out one tree, merging with the index.
        Parameters:
        repo - the repository in which we do the checkout
        dc - the (already locked) Dircache for this repo
        mergeCommitTree - the id of the tree we want to fast-forward to
        workingTree - an iterator over the repositories Working Tree
        Throws:
        java.io.IOException
      • DirCacheCheckout

        public DirCacheCheckout​(Repository repo,
                                DirCache dc,
                                ObjectId mergeCommitTree)
                         throws java.io.IOException
        Constructs a DirCacheCeckout for checking out one tree, merging with the index. As iterator over the working tree this constructor creates a standard FileTreeIterator
        Parameters:
        repo - the repository in which we do the checkout
        dc - the (already locked) Dircache for this repo
        mergeCommitTree - the id of the tree of the
        Throws:
        java.io.IOException
    • Method Detail

      • getUpdated

        public java.util.Map<java.lang.String,​DirCacheCheckout.CheckoutMetadata> getUpdated()
        Get list of updated paths and smudgeFilterCommands
        Returns:
        a list of updated paths and smudgeFilterCommands
      • getConflicts

        public java.util.List<java.lang.String> getConflicts()
        Get a list of conflicts created by this checkout
        Returns:
        a list of conflicts created by this checkout
      • getToBeDeleted

        public java.util.List<java.lang.String> getToBeDeleted()
        Get list of paths of files which couldn't be deleted during last call to checkout()
        Returns:
        a list of paths (relative to the start of the working tree) of files which couldn't be deleted during last call to checkout() . checkout() detected that these files should be deleted but the deletion in the filesystem failed (e.g. because a file was locked). To have a consistent state of the working tree these files have to be deleted by the callers of DirCacheCheckout.
      • getRemoved

        public java.util.List<java.lang.String> getRemoved()
        Get list of all files removed by this checkout
        Returns:
        a list of all files removed by this checkout
      • setProgressMonitor

        public void setProgressMonitor​(ProgressMonitor monitor)
        Set a progress monitor which can be passed to built-in filter commands, providing progress information for long running tasks.
        Parameters:
        monitor - the ProgressMonitor
        Since:
        4.11
      • preScanTwoTrees

        public void preScanTwoTrees()
                             throws CorruptObjectException,
                                    java.io.IOException
        Scan head, index and merge tree. Used during normal checkout or merge operations.
        Throws:
        CorruptObjectException
        java.io.IOException
      • checkout

        public boolean checkout()
                         throws java.io.IOException
        Execute this checkout. A WorkingTreeModifiedEvent is fired if the working tree was modified; even if the checkout fails.
        Returns:
        false if this method could not delete all the files which should be deleted (e.g. because one of the files was locked). In this case getToBeDeleted() lists the files which should be tried to be deleted outside of this method. Although false is returned the checkout was successful and the working tree was updated for all other files. true is returned when no such problem occurred
        Throws:
        java.io.IOException
      • checkoutGitlink

        private void checkoutGitlink​(java.lang.String path,
                                     DirCacheEntry entry)
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • filterOut

        private static java.util.ArrayList<java.lang.String> filterOut​(java.util.ArrayList<java.lang.String> strings,
                                                                       IntList indicesToRemove)
      • isSamePrefix

        private static boolean isSamePrefix​(java.lang.String a,
                                            java.lang.String b)
      • removeEmptyParents

        private void removeEmptyParents​(java.io.File f)
      • equalIdAndMode

        private boolean equalIdAndMode​(ObjectId id1,
                                       FileMode mode1,
                                       ObjectId id2,
                                       FileMode mode2)
        Compares whether two pairs of ObjectId and FileMode are equal.
        Parameters:
        id1 -
        mode1 -
        id2 -
        mode2 -
        Returns:
        true if FileModes and ObjectIds are equal. false otherwise
      • processEntry

        void processEntry​(CanonicalTreeParser h,
                          CanonicalTreeParser m,
                          DirCacheBuildIterator i,
                          WorkingTreeIterator f)
                   throws java.io.IOException
        Here the main work is done. This method is called for each existing path in head, index and merge. This method decides what to do with the corresponding index entry: keep it, update it, remove it or mark a conflict.
        Parameters:
        h - the entry for the head
        m - the entry for the merge
        i - the entry for the index
        f - the file in the working tree
        Throws:
        java.io.IOException
      • conflict

        private void conflict​(java.lang.String path,
                              DirCacheEntry e,
                              AbstractTreeIterator h,
                              AbstractTreeIterator m)
        A conflict is detected - add the three different stages to the index
        Parameters:
        path - the path of the conflicting entry
        e - the previous index entry
        h - the first tree you want to merge (the HEAD)
        m - the second tree you want to merge
      • remove

        private void remove​(java.lang.String path)
      • update

        private void update​(CanonicalTreeParser tree)
                     throws java.io.IOException
        Throws:
        java.io.IOException
      • update

        private void update​(int index,
                            java.lang.String path,
                            ObjectId mId,
                            FileMode mode)
                     throws java.io.IOException
        Throws:
        java.io.IOException
      • setFailOnConflict

        public void setFailOnConflict​(boolean failOnConflict)
        If true, will scan first to see if it's possible to check out, otherwise throw CheckoutConflictException. If false, it will silently deal with the problem.
        Parameters:
        failOnConflict - a boolean.
      • setForce

        public void setForce​(boolean force)
        If true, dirty worktree files may be overridden. If false dirty worktree files will not be overridden in order not to delete unsaved content. This corresponds to native git's 'git checkout -f' option. By default this option is set to false.
        Parameters:
        force - a boolean.
        Since:
        5.3
      • isModifiedSubtree_IndexWorkingtree

        private boolean isModifiedSubtree_IndexWorkingtree​(java.lang.String path)
                                                    throws CorruptObjectException,
                                                           java.io.IOException
        Checks whether the subtree starting at a given path differs between Index and workingtree.
        Parameters:
        path -
        Returns:
        true if the subtrees differ
        Throws:
        CorruptObjectException
        java.io.IOException
      • isModifiedSubtree_IndexTree

        private boolean isModifiedSubtree_IndexTree​(java.lang.String path,
                                                    ObjectId tree)
                                             throws CorruptObjectException,
                                                    java.io.IOException
        Checks whether the subtree starting at a given path differs between Index and some tree.
        Parameters:
        path -
        tree - the tree to compare
        Returns:
        true if the subtrees differ
        Throws:
        CorruptObjectException
        java.io.IOException
      • checkoutEntry

        @Deprecated
        public static void checkoutEntry​(Repository repo,
                                         DirCacheEntry entry,
                                         ObjectReader or)
                                  throws java.io.IOException
        Updates the file in the working tree with content and mode from an entry in the index. The new content is first written to a new temporary file in the same directory as the real file. Then that new file is renamed to the final filename.

        Note: if the entry path on local file system exists as a non-empty directory, and the target entry type is a link or file, the checkout will fail with IOException since existing non-empty directory cannot be renamed to file or link without deleting it recursively.

        TODO: this method works directly on File IO, we may need another abstraction (like WorkingTreeIterator). This way we could tell e.g. Eclipse that Files in the workspace got changed

        Parameters:
        repo - repository managing the destination work tree.
        entry - the entry containing new mode and content
        or - object reader to use for checkout
        Throws:
        java.io.IOException
        Since:
        3.6
      • checkoutEntry

        public static void checkoutEntry​(Repository repo,
                                         DirCacheEntry entry,
                                         ObjectReader or,
                                         boolean deleteRecursive,
                                         DirCacheCheckout.CheckoutMetadata checkoutMetadata)
                                  throws java.io.IOException
        Updates the file in the working tree with content and mode from an entry in the index. The new content is first written to a new temporary file in the same directory as the real file. Then that new file is renamed to the final filename.

        Note: if the entry path on local file system exists as a file, it will be deleted and if it exists as a directory, it will be deleted recursively, independently if has any content.

        TODO: this method works directly on File IO, we may need another abstraction (like WorkingTreeIterator). This way we could tell e.g. Eclipse that Files in the workspace got changed

        Parameters:
        repo - repository managing the destination work tree.
        entry - the entry containing new mode and content
        or - object reader to use for checkout
        deleteRecursive - true to recursively delete final path if it exists on the file system
        checkoutMetadata - containing
        • smudgeFilterCommand to be run for smudging the entry to be checked out
        • eolStreamType used for stream conversion
        Throws:
        java.io.IOException
        Since:
        4.2
      • getContent

        public static void getContent​(Repository repo,
                                      java.lang.String path,
                                      DirCacheCheckout.CheckoutMetadata checkoutMetadata,
                                      ObjectLoader ol,
                                      WorkingTreeOptions opt,
                                      java.io.OutputStream os)
                               throws java.io.IOException
        Return filtered content for a specific object (blob). EOL handling and smudge-filter handling are applied in the same way as it would be done during a checkout.
        Parameters:
        repo - the repository
        path - the path used to determine the correct filters for the object
        checkoutMetadata - containing
        • smudgeFilterCommand to be run for smudging the object
        • eolStreamType used for stream conversion (can be null)
        ol - the object loader to read raw content of the object
        opt - the working tree options where only 'core.autocrlf' is used for EOL handling if 'checkoutMetadata.eolStreamType' is not valid
        os - the output stream the filtered content is written to. The caller is responsible to close the stream.
        Throws:
        java.io.IOException
        Since:
        5.7
      • runExternalFilterCommand

        private static void runExternalFilterCommand​(Repository repo,
                                                     java.lang.String path,
                                                     DirCacheCheckout.CheckoutMetadata checkoutMetadata,
                                                     ObjectLoader ol,
                                                     java.io.OutputStream channel)
                                              throws java.io.IOException
        Throws:
        java.io.IOException