Class DirCacheCheckout
- java.lang.Object
-
- org.eclipse.jgit.dircache.DirCacheCheckout
-
public class DirCacheCheckout extends java.lang.Object
This class handles checking out one or two trees merging with the index.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DirCacheCheckout.CheckoutMetadata
Metadata used in checkout process
-
Field Summary
Fields Modifier and Type Field Description private DirCacheBuilder
builder
private java.util.ArrayList<java.lang.String>
conflicts
private DirCache
dc
private boolean
failOnConflict
private boolean
force
private ObjectId
headCommitTree
private boolean
initialCheckout
private java.util.ArrayList<java.lang.String>
kept
private static org.slf4j.Logger
LOG
private static int
MAX_EXCEPTION_TEXT_SIZE
private ObjectId
mergeCommitTree
private ProgressMonitor
monitor
private boolean
performingCheckout
private java.util.ArrayList<java.lang.String>
removed
private Repository
repo
private java.util.ArrayList<java.lang.String>
toBeDeleted
private java.util.HashMap<java.lang.String,DirCacheCheckout.CheckoutMetadata>
updated
private NameConflictTreeWalk
walk
private WorkingTreeIterator
workingTree
-
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 void
addTree(TreeWalk tw, ObjectId id)
boolean
checkout()
Execute this checkout.static void
checkoutEntry(Repository repo, DirCacheEntry entry, ObjectReader or)
Deprecated.since 5.1, usecheckoutEntry(Repository, DirCacheEntry, ObjectReader, boolean, CheckoutMetadata)
insteadstatic void
checkoutEntry(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 void
checkoutGitlink(java.lang.String path, DirCacheEntry entry)
private static void
checkValidPath(CanonicalTreeParser t)
private static void
checkValidPathSegment(ObjectChecker chk, CanonicalTreeParser t)
private void
cleanUpConflicts()
This method implements how to handle conflicts whenfailOnConflict
is falseprivate void
conflict(java.lang.String path, DirCacheEntry e, AbstractTreeIterator h, AbstractTreeIterator m)
A conflict is detected - add the three different stages to the indexprivate boolean
doCheckout()
private boolean
equalIdAndMode(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 void
getContent(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 boolean
idEqual(AbstractTreeIterator a, AbstractTreeIterator b)
private boolean
isModified_IndexTree(java.lang.String path, ObjectId iId, FileMode iMode, ObjectId tId, FileMode tMode, ObjectId rootTree)
private boolean
isModifiedSubtree_IndexTree(java.lang.String path, ObjectId tree)
Checks whether the subtree starting at a given path differs between Index and some tree.private boolean
isModifiedSubtree_IndexWorkingtree(java.lang.String path)
Checks whether the subtree starting at a given path differs between Index and workingtree.private static boolean
isSamePrefix(java.lang.String a, java.lang.String b)
private void
keep(java.lang.String path, DirCacheEntry e, WorkingTreeIterator f)
void
prescanOneTree()
Scan index and merge tree (no HEAD).void
preScanTwoTrees()
Scan head, index and merge tree.(package private) void
processEntry(CanonicalTreeParser m, DirCacheBuildIterator i, WorkingTreeIterator f)
Processing an entry in the context ofprescanOneTree()
when only one tree is given(package private) void
processEntry(CanonicalTreeParser h, CanonicalTreeParser m, DirCacheBuildIterator i, WorkingTreeIterator f)
Here the main work is done.private void
remove(java.lang.String path)
private void
removeEmptyParents(java.io.File f)
private static void
runBuiltinFilterCommand(Repository repo, DirCacheCheckout.CheckoutMetadata checkoutMetadata, ObjectLoader ol, java.io.OutputStream channel)
private static void
runExternalFilterCommand(Repository repo, java.lang.String path, DirCacheCheckout.CheckoutMetadata checkoutMetadata, ObjectLoader ol, java.io.OutputStream channel)
void
setFailOnConflict(boolean failOnConflict)
Iftrue
, will scan first to see if it's possible to check out, otherwise throwCheckoutConflictException
.void
setForce(boolean force)
Iftrue
, dirty worktree files may be overridden.void
setProgressMonitor(ProgressMonitor monitor)
Set a progress monitor which can be passed to built-in filter commands, providing progress information for long running tasks.private void
update(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
-
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.IOException
Scan head, index and merge tree. Used during normal checkout or merge operations.- Throws:
CorruptObjectException
java.io.IOException
-
addTree
private void addTree(TreeWalk tw, ObjectId id) throws MissingObjectException, IncorrectObjectTypeException, java.io.IOException
- Throws:
MissingObjectException
IncorrectObjectTypeException
java.io.IOException
-
prescanOneTree
public void prescanOneTree() throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, java.io.IOException
Scan index and merge tree (no HEAD). Used e.g. for initial checkout when there is no head yet.- Throws:
MissingObjectException
IncorrectObjectTypeException
CorruptObjectException
java.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.IOException
Execute this checkout. AWorkingTreeModifiedEvent
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 casegetToBeDeleted()
lists the files which should be tried to be deleted outside of this method. Althoughfalse
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
-
doCheckout
private boolean doCheckout() throws CorruptObjectException, java.io.IOException, MissingObjectException, IncorrectObjectTypeException, CheckoutConflictException, IndexWriteException, CanceledException
-
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 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. Iffalse
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
-
cleanUpConflicts
private void cleanUpConflicts() throws CheckoutConflictException
This method implements how to handle conflicts whenfailOnConflict
is false- Throws:
CheckoutConflictException
-
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
-
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:
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
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
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 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:
MissingObjectException
java.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
-
-