Class DirCacheCheckout
- java.lang.Object
-
- org.eclipse.jgit.dircache.DirCacheCheckout
-
public class DirCacheCheckout extends java.lang.ObjectThis class handles checking out one or two trees merging with the index.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDirCacheCheckout.CheckoutMetadataMetadata used in checkout process
-
Field Summary
Fields Modifier and Type Field Description private DirCacheBuilderbuilderprivate Checkoutcheckoutprivate java.util.ArrayList<java.lang.String>conflictsprivate DirCachedcprivate booleanfailOnConflictprivate booleanforceprivate ObjectIdheadCommitTreeprivate booleaninitialCheckoutprivate java.util.ArrayList<java.lang.String>keptprivate static org.slf4j.LoggerLOGprivate static intMAX_EXCEPTION_TEXT_SIZEprivate ObjectIdmergeCommitTreeprivate ProgressMonitormonitorprivate booleanperformingCheckoutprivate java.util.ArrayList<java.lang.String>removedprivate Repositoryrepoprivate java.util.ArrayList<java.lang.String>toBeDeletedprivate java.util.HashMap<java.lang.String,DirCacheCheckout.CheckoutMetadata>updatedprivate NameConflictTreeWalkwalkprivate WorkingTreeIteratorworkingTree
-
Constructor Summary
Constructors Constructor Description DirCacheCheckout(Repository repo, DirCache dc, ObjectId mergeCommitTree)Constructs a DirCacheCeckout for checking out one tree, merging with the index.DirCacheCheckout(Repository repo, DirCache dc, ObjectId mergeCommitTree, WorkingTreeIterator workingTree)Constructs a DirCacheCeckout for checking out one tree, merging with the index.DirCacheCheckout(Repository repo, ObjectId headCommitTree, DirCache dc, ObjectId mergeCommitTree)Constructs a DirCacheCeckout for merging and checking out two trees (HEAD and mergeCommitTree) and the index.DirCacheCheckout(Repository repo, ObjectId headCommitTree, DirCache dc, ObjectId mergeCommitTree, WorkingTreeIterator workingTree)Constructs a DirCacheCeckout for merging and checking out two trees (HEAD and mergeCommitTree) and the index.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private voidaddTree(TreeWalk tw, ObjectId id)booleancheckout()Execute this checkout.static voidcheckoutEntry(Repository repo, DirCacheEntry entry, ObjectReader or)Deprecated.since 5.1, usecheckoutEntry(Repository, DirCacheEntry, ObjectReader, boolean, CheckoutMetadata)insteadstatic voidcheckoutEntry(Repository repo, DirCacheEntry entry, ObjectReader or, boolean deleteRecursive, DirCacheCheckout.CheckoutMetadata checkoutMetadata)Updates the file in the working tree with content and mode from an entry in the index.private static voidcheckValidPath(CanonicalTreeParser t)private static voidcheckValidPathSegment(ObjectChecker chk, CanonicalTreeParser t)private voidcleanUpConflicts()This method implements how to handle conflicts whenfailOnConflictis falseprivate voidconflict(java.lang.String path, DirCacheEntry e, AbstractTreeIterator h, AbstractTreeIterator m)A conflict is detected - add the three different stages to the indexprivate booleandoCheckout()private booleanequalIdAndMode(ObjectId id1, FileMode mode1, ObjectId id2, FileMode mode2)Compares whether two pairs of ObjectId and FileMode are equal.private static java.util.ArrayList<java.lang.String>filterOut(java.util.ArrayList<java.lang.String> strings, IntList indicesToRemove)java.util.List<java.lang.String>getConflicts()Get a list of conflicts created by this checkoutstatic voidgetContent(Repository repo, java.lang.String path, DirCacheCheckout.CheckoutMetadata checkoutMetadata, ObjectLoader ol, WorkingTreeOptions opt, java.io.OutputStream os)Return filtered content for a specific object (blob).java.util.List<java.lang.String>getRemoved()Get list of all files removed by this checkoutjava.util.List<java.lang.String>getToBeDeleted()Get list of paths of files which couldn't be deleted during last call tocheckout()java.util.Map<java.lang.String,DirCacheCheckout.CheckoutMetadata>getUpdated()Get list of updated paths and smudgeFilterCommandsprivate static booleanidEqual(AbstractTreeIterator a, AbstractTreeIterator b)private booleanisModified_IndexTree(java.lang.String path, ObjectId iId, FileMode iMode, ObjectId tId, FileMode tMode, ObjectId rootTree)private booleanisModifiedSubtree_IndexTree(java.lang.String path, ObjectId tree)Checks whether the subtree starting at a given path differs between Index and some tree.private booleanisModifiedSubtree_IndexWorkingtree(java.lang.String path)Checks whether the subtree starting at a given path differs between Index and workingtree.private static booleanisSamePrefix(java.lang.String a, java.lang.String b)private voidkeep(java.lang.String path, DirCacheEntry e, WorkingTreeIterator f)voidprescanOneTree()Scan index and merge tree (no HEAD).voidpreScanTwoTrees()Scan head, index and merge tree.(package private) voidprocessEntry(CanonicalTreeParser m, DirCacheBuildIterator i, WorkingTreeIterator f)Processing an entry in the context ofprescanOneTree()when only one tree is given(package private) voidprocessEntry(CanonicalTreeParser h, CanonicalTreeParser m, DirCacheBuildIterator i, WorkingTreeIterator f)Here the main work is done.private voidremove(java.lang.String path)private voidremoveEmptyParents(java.io.File f)private static voidrunBuiltinFilterCommand(Repository repo, DirCacheCheckout.CheckoutMetadata checkoutMetadata, ObjectLoader ol, java.io.OutputStream channel)private static voidrunExternalFilterCommand(Repository repo, java.lang.String path, DirCacheCheckout.CheckoutMetadata checkoutMetadata, ObjectLoader ol, java.io.OutputStream channel)voidsetFailOnConflict(boolean failOnConflict)Iftrue, will scan first to see if it's possible to check out, otherwise throwCheckoutConflictException.voidsetForce(boolean force)Iftrue, dirty worktree files may be overridden.voidsetProgressMonitor(ProgressMonitor monitor)Set a progress monitor which can be passed to built-in filter commands, providing progress information for long running tasks.private voidupdate(java.lang.String path, ObjectId mId, FileMode mode)
-
-
-
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
-
repo
private Repository repo
-
updated
private java.util.HashMap<java.lang.String,DirCacheCheckout.CheckoutMetadata> updated
-
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
-
dc
private DirCache dc
-
builder
private DirCacheBuilder builder
-
walk
private NameConflictTreeWalk walk
-
headCommitTree
private ObjectId headCommitTree
-
workingTree
private WorkingTreeIterator workingTree
-
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
-
checkout
private Checkout checkout
-
monitor
private ProgressMonitor monitor
-
-
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 checkoutheadCommitTree- the id of the tree of the head commitdc- the (already locked) Dircache for this repomergeCommitTree- the id of the tree we want to fast-forward toworkingTree- 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 standardFileTreeIterator- Parameters:
repo- the repository in which we do the checkoutheadCommitTree- the id of the tree of the head commitdc- the (already locked) Dircache for this repomergeCommitTree- 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 checkoutdc- the (already locked) Dircache for this repomergeCommitTree- the id of the tree we want to fast-forward toworkingTree- 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 standardFileTreeIterator- Parameters:
repo- the repository in which we do the checkoutdc- the (already locked) Dircache for this repomergeCommitTree- 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 tocheckout()- 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 ofDirCacheCheckout.
-
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- theProgressMonitor- Since:
- 4.11
-
preScanTwoTrees
public void preScanTwoTrees() throws CorruptObjectException, java.io.IOExceptionScan head, index and merge tree. Used during normal checkout or merge operations.- Throws:
CorruptObjectExceptionjava.io.IOException
-
addTree
private void addTree(TreeWalk tw, ObjectId id) throws MissingObjectException, IncorrectObjectTypeException, java.io.IOException
- Throws:
MissingObjectExceptionIncorrectObjectTypeExceptionjava.io.IOException
-
prescanOneTree
public void prescanOneTree() throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, java.io.IOExceptionScan index and merge tree (no HEAD). Used e.g. for initial checkout when there is no head yet.- Throws:
MissingObjectExceptionIncorrectObjectTypeExceptionCorruptObjectExceptionjava.io.IOException
-
processEntry
void processEntry(CanonicalTreeParser m, DirCacheBuildIterator i, WorkingTreeIterator f) throws java.io.IOException
Processing an entry in the context ofprescanOneTree()when only one tree is given- Parameters:
m- the tree to mergei- the indexf- the working tree- Throws:
java.io.IOException
-
checkout
public boolean checkout() throws java.io.IOExceptionExecute this checkout. AWorkingTreeModifiedEventis fired if the working tree was modified; even if the checkout fails.- Returns:
falseif this method could not delete all the files which should be deleted (e.g. because one of the files was locked). In this casegetToBeDeleted()lists the files which should be tried to be deleted outside of this method. Althoughfalseis returned the checkout was successful and the working tree was updated for all other files.trueis returned when no such problem occurred- Throws:
java.io.IOException
-
doCheckout
private boolean doCheckout() throws CorruptObjectException, java.io.IOException, MissingObjectException, IncorrectObjectTypeException, CheckoutConflictException, IndexWriteException, CanceledException
-
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:
trueif FileModes and ObjectIds are equal.falseotherwise
-
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 headm- the entry for the mergei- the entry for the indexf- the file in the working tree- Throws:
java.io.IOException
-
idEqual
private static boolean idEqual(AbstractTreeIterator a, AbstractTreeIterator b)
-
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 entrye- the previous index entryh- the first tree you want to merge (the HEAD)m- the second tree you want to merge
-
keep
private void keep(java.lang.String path, DirCacheEntry e, WorkingTreeIterator f) throws java.io.IOException- Throws:
java.io.IOException
-
remove
private void remove(java.lang.String path)
-
update
private void update(java.lang.String path, ObjectId mId, FileMode mode) throws java.io.IOException- Throws:
java.io.IOException
-
setFailOnConflict
public void setFailOnConflict(boolean failOnConflict)
Iftrue, will scan first to see if it's possible to check out, otherwise throwCheckoutConflictException. Iffalse, it will silently deal with the problem.- Parameters:
failOnConflict- a boolean.
-
setForce
public void setForce(boolean force)
Iftrue, dirty worktree files may be overridden. Iffalsedirty 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
-
cleanUpConflicts
private void cleanUpConflicts() throws CheckoutConflictExceptionThis method implements how to handle conflicts whenfailOnConflictis false- Throws:
CheckoutConflictException
-
isModifiedSubtree_IndexWorkingtree
private boolean isModifiedSubtree_IndexWorkingtree(java.lang.String path) throws CorruptObjectException, java.io.IOExceptionChecks whether the subtree starting at a given path differs between Index and workingtree.- Parameters:
path-- Returns:
- true if the subtrees differ
- Throws:
CorruptObjectExceptionjava.io.IOException
-
isModified_IndexTree
private boolean isModified_IndexTree(java.lang.String path, ObjectId iId, FileMode iMode, ObjectId tId, FileMode tMode, ObjectId rootTree) throws CorruptObjectException, java.io.IOException- Throws:
CorruptObjectExceptionjava.io.IOException
-
isModifiedSubtree_IndexTree
private boolean isModifiedSubtree_IndexTree(java.lang.String path, ObjectId tree) throws CorruptObjectException, java.io.IOExceptionChecks 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:
CorruptObjectExceptionjava.io.IOException
-
checkoutEntry
@Deprecated public static void checkoutEntry(Repository repo, DirCacheEntry entry, ObjectReader or) throws java.io.IOException
Deprecated.since 5.1, usecheckoutEntry(Repository, DirCacheEntry, ObjectReader, boolean, CheckoutMetadata)insteadUpdates 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
IOExceptionsince 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 contentor- 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 contentor- object reader to use for checkoutdeleteRecursive- true to recursively delete final path if it exists on the file systemcheckoutMetadata- 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 repositorypath- the path used to determine the correct filters for the objectcheckoutMetadata- 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 objectopt- the working tree options where only 'core.autocrlf' is used for EOL handling if 'checkoutMetadata.eolStreamType' is not validos- 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
-
runBuiltinFilterCommand
private static void runBuiltinFilterCommand(Repository repo, DirCacheCheckout.CheckoutMetadata checkoutMetadata, ObjectLoader ol, java.io.OutputStream channel) throws MissingObjectException, java.io.IOException
- Throws:
MissingObjectExceptionjava.io.IOException
-
checkValidPath
private static void checkValidPath(CanonicalTreeParser t) throws InvalidPathException
- Throws:
InvalidPathException
-
checkValidPathSegment
private static void checkValidPathSegment(ObjectChecker chk, CanonicalTreeParser t) throws InvalidPathException
- Throws:
InvalidPathException
-
-