Top Description Inners Fields Constructors Methods
org.eclipse.jgit.patch

public Class PatchApplier

extends Object
Class Inheritance
Static Imports
org.eclipse.jgit.diff.DiffEntry.ChangeType.ADD, .DiffEntry.ChangeType.COPY, .DiffEntry.ChangeType.DELETE, .DiffEntry.ChangeType.MODIFY, .DiffEntry.ChangeType.RENAME, org.eclipse.jgit.lib.Constants.OBJ_BLOB

Applies a patch to files and the index.

After instantiating, applyPatch() should be called once.

Since
6.4

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
PatchApplier.ContentStreamLoader

Provide stream, along with the length of the object.

public static class
PatchApplier.Result

A wrapper for returning both the applied tree ID and the applied files list, as well as file specific errors.

private static class
PatchApplier.SHA1InputStream

An InputStream that updates a SHA1 on every byte read.

Field Summary

Modifier and TypeField and Description
private boolean
private final RevTree
beforeTree

The tree before applying the patch.

private final Charset
private static final int
private int
private final ObjectInserter
private static final byte[]
private final ObjectReader
private final Repository
private WorkingTreeOptions

Constructor Summary

AccessConstructor and Description
public
PatchApplier(Repository
repository to apply the patch in
repo
)

public
PatchApplier(Repository
repository to apply the patch in
repo
,
RevTree
ID of the tree to apply the patch in
beforeTree
,
ObjectInserter
to be used for modifying objects
oi
)

Method Summary

Modifier and TypeMethod and Description
public PatchApplier

Returns:

the PatchApplier to apply any patches
allowConflicts
()

Sets up the PatchApplier to apply patches even if they conflict.

private void
apply(String
The path to use for the pre-image. Also determines CRLF and smudge settings.
pathWithOriginalContent
,
DirCache
Dircache to read existing data from.
dirCache
,
DirCacheBuilder
Builder for Dircache to write new data to.
dirCacheBuilder
,
File
The file to update with new contents. Null for inCore usage.
f
,
FileHeader
The patch header.
fh
,
PatchApplier.Result
The patch application result.
result
)

Applies patch to a single file.

private PatchApplier.ContentStreamLoader

Returns:

a loader for the new content, or null if invalid.
applyBinary
(String
pathname of the file to write.
path
,
File
destination file
f
,
FileHeader
the patch to apply
fh
,
DirCacheCheckout.StreamSupplier
a supplier for the contents of the old file
inputSupplier
,
ObjectId
SHA1 for the old content
id
,
PatchApplier.Result
The patch application result
result
)

Applies a binary patch.

public PatchApplier.Result

Returns:

the result of the patch
applyPatch
(Patch
the patch to apply.
p
)

Applies the given patch

private PatchApplier.ContentStreamLoader
private ByteBuffer
private boolean
canApplyAt(List<ByteBuffer> hunkLines, List<ByteBuffer> newLines, int line)

private boolean
private boolean
private InputStream
filterClean(Repository repository, String path, InputStream fromFile, boolean convertCrLf, String filterCommand)

private File
private RawText

Returns:

the result raw text
getRawText
(File
to read from
file
,
DirCacheCheckout.StreamSupplier
if fromTreewalk, the stream of the file content
fileStreamSupplier
,
ObjectId
of the file
fileId
,
String
of the file
path
,
boolean
whether the file was loaded by a TreeWalk
fromTreeWalk
,
String
for reading the file content
filterCommand
,
boolean
whether a CR-LF conversion is needed
convertCrLf
)

Gets the raw text of the given file.

private TreeWalk
private static boolean
hasCrLf(FileHeader fileHeader)

private ObjectId
private boolean
private DirCacheEntry
insertToIndex(InputStream input, byte[] path, FileMode fileMode, Instant lastModified, long length, Attribute lfsAttribute)

private boolean
private boolean
private ByteBuffer
slice(ByteBuffer b, int off)

private static PatchApplier.ContentStreamLoader
private boolean
private boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

allowConflictsback to summary
private boolean allowConflicts
beforeTreeback to summary
private final RevTree beforeTree

The tree before applying the patch. Only non-null for inCore operation.

Annotations
@Nullable
charsetback to summary
private final Charset charset
FILE_TREE_INDEXback to summary
private static final int FILE_TREE_INDEX
inCoreSizeLimitback to summary
private int inCoreSizeLimit
inserterback to summary
private final ObjectInserter inserter
NO_EOLback to summary
private static final byte[] NO_EOL
readerback to summary
private final ObjectReader reader
repoback to summary
private final Repository repo
workingTreeOptionsback to summary
private WorkingTreeOptions workingTreeOptions

Constructor Detail

PatchApplierback to summary
public PatchApplier(Repository repo)
Parameters
repo:Repository

repository to apply the patch in

PatchApplierback to summary
public PatchApplier(Repository repo, RevTree beforeTree, ObjectInserter oi)
Parameters
repo:Repository

repository to apply the patch in

beforeTree:RevTree

ID of the tree to apply the patch in

oi:ObjectInserter

to be used for modifying objects

Method Detail

allowConflictsback to summary
public PatchApplier allowConflicts()

Sets up the PatchApplier to apply patches even if they conflict.

Returns:PatchApplier

the PatchApplier to apply any patches

Since
6.10
applyback to summary
private void apply(String pathWithOriginalContent, DirCache dirCache, DirCacheBuilder dirCacheBuilder, File f, FileHeader fh, PatchApplier.Result result) throws IOException

Applies patch to a single file.

Parameters
pathWithOriginalContent:String

The path to use for the pre-image. Also determines CRLF and smudge settings.

dirCache:DirCache

Dircache to read existing data from.

dirCacheBuilder:DirCacheBuilder

Builder for Dircache to write new data to.

f:File

The file to update with new contents. Null for inCore usage.

fh:FileHeader

The patch header.

result:PatchApplier.Result

The patch application result.

Exceptions
IOException:
if an IO error occurred
applyBinaryback to summary
private PatchApplier.ContentStreamLoader applyBinary(String path, File f, FileHeader fh, DirCacheCheckout.StreamSupplier inputSupplier, ObjectId id, PatchApplier.Result result) throws UnsupportedOperationException, IOException

Applies a binary patch.

Parameters
path:String

pathname of the file to write.

f:File

destination file

fh:FileHeader

the patch to apply

inputSupplier:DirCacheCheckout.StreamSupplier

a supplier for the contents of the old file

id:ObjectId

SHA1 for the old content

result:PatchApplier.Result

The patch application result

Returns:PatchApplier.ContentStreamLoader

a loader for the new content, or null if invalid.

Annotations
@Nullable
Exceptions
UnsupportedOperationException:
if an operation isn't supported
IOException:
if an IO error occurred
applyPatchback to summary
public PatchApplier.Result applyPatch(Patch p) throws IOException

Applies the given patch

Parameters
p:Patch

the patch to apply.

Returns:PatchApplier.Result

the result of the patch

Exceptions
IOException:
if an IO error occurred
Since
6.6
applyTextback to summary
private PatchApplier.ContentStreamLoader applyText(RawText rt, FileHeader fh, PatchApplier.Result result) throws IOException
Annotations
@SuppressWarnings:ByteBufferBackingArray
@Nullable
asBytesback to summary
private ByteBuffer asBytes(String str)
canApplyAtback to summary
private boolean canApplyAt(List<ByteBuffer> hunkLines, List<ByteBuffer> newLines, int line)
Annotations
@SuppressWarnings:ByteBufferBackingArray
checkOidback to summary
private boolean checkOid(ObjectId baseId, ObjectId id, DiffEntry.ChangeType type, File f, String path, PatchApplier.Result result) throws IOException
fileExistsback to summary
private boolean fileExists(String path, File f) throws IOException
filterCleanback to summary
private InputStream filterClean(Repository repository, String path, InputStream fromFile, boolean convertCrLf, String filterCommand) throws IOException
getFileback to summary
private File getFile(String path)
getRawTextback to summary
private RawText getRawText(File file, DirCacheCheckout.StreamSupplier fileStreamSupplier, ObjectId fileId, String path, boolean fromTreeWalk, String filterCommand, boolean convertCrLf) throws IOException

Gets the raw text of the given file.

Parameters
file:File

to read from

fileStreamSupplier:DirCacheCheckout.StreamSupplier

if fromTreewalk, the stream of the file content

fileId:ObjectId

of the file

path:String

of the file

fromTreeWalk:boolean

whether the file was loaded by a TreeWalk

filterCommand:String

for reading the file content

convertCrLf:boolean

whether a CR-LF conversion is needed

Returns:RawText

the result raw text

Exceptions
IOException:
in case of filtering issues
getTreeWalkForFileback to summary
private TreeWalk getTreeWalkForFile(String path, DirCache cache) throws IOException
Annotations
@Nullable
hasCrLfback to summary
private static boolean hasCrLf(FileHeader fileHeader)
hashback to summary
private ObjectId hash(File f) throws IOException
inCoreback to summary
private boolean inCore()
insertToIndexback to summary
private DirCacheEntry insertToIndex(InputStream input, byte[] path, FileMode fileMode, Instant lastModified, long length, Attribute lfsAttribute) throws IOException
isNoNewlineAtEndback to summary
private boolean isNoNewlineAtEnd(ByteBuffer hunkLine)
Annotations
@SuppressWarnings:ByteBufferBackingArray
needsCrLfConversionback to summary
private boolean needsCrLfConversion(File f, FileHeader fileHeader) throws IOException
sliceback to summary
private ByteBuffer slice(ByteBuffer b, int off)
Annotations
@SuppressWarnings:ByteBufferBackingArray
toContentStreamLoaderback to summary
private static PatchApplier.ContentStreamLoader toContentStreamLoader(List<ByteBuffer> newLines) throws IOException
validGitPathback to summary
private boolean validGitPath(String path)
verifyExistenceback to summary
private boolean verifyExistence(FileHeader fh, File src, File dest, PatchApplier.Result result) throws IOException
org.eclipse.jgit.patch back to summary

private Class PatchApplier.ContentStreamLoader

extends Object
Class Inheritance

Provide stream, along with the length of the object. We use this once to patch to the working tree, once to write the index. For on-disk operation, presumably we could stream to the destination file, and then read back the stream from disk. We don't because it is more complex.

Field Summary

Modifier and TypeField and Description
pack-priv long
pack-priv DirCacheCheckout.StreamSupplier

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

lengthback to summary
pack-priv long length
supplierback to summary
pack-priv DirCacheCheckout.StreamSupplier supplier

Constructor Detail

ContentStreamLoaderback to summary
pack-priv ContentStreamLoader(DirCacheCheckout.StreamSupplier supplier, long length)
org.eclipse.jgit.patch back to summary

public Class PatchApplier.Result

extends Object
Class Inheritance

A wrapper for returning both the applied tree ID and the applied files list, as well as file specific errors.
Since
6.3

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
PatchApplier.Result.Error

A wrapper for a patch applying error that affects a given file.

Field Summary

Modifier and TypeField and Description
private List<PatchApplier.Result.Error>
private List<String>
private ObjectId

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private void
addError(String msg, String oldFileName, HunkHeader hh)

private void
public List<PatchApplier.Result.Error>

Returns:

Errors occurred while applying the patch.
getErrors
()

Get errors

public List<String>

Returns:

List of modified paths.
getPaths
()

Get modified paths

public ObjectId

Returns:

The applied tree ID.
getTreeId
()

Get tree ID

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

errorsback to summary
private List<PatchApplier.Result.Error> errors
pathsback to summary
private List<String> paths
treeIdback to summary
private ObjectId treeId

Constructor Detail

Resultback to summary
public Result()

Method Detail

addErrorback to summary
private void addError(String msg, String oldFileName, HunkHeader hh)
addErrorWithGitConflictback to summary
private void addErrorWithGitConflict(String msg, String oldFileName, HunkHeader hh)
getErrorsback to summary
public List<PatchApplier.Result.Error> getErrors()

Get errors

Returns:List<PatchApplier.Result.Error>

Errors occurred while applying the patch.

Since
6.6
getPathsback to summary
public List<String> getPaths()

Get modified paths

Returns:List<String>

List of modified paths.

getTreeIdback to summary
public ObjectId getTreeId()

Get tree ID

Returns:ObjectId

The applied tree ID.

org.eclipse.jgit.patch back to summary

public Class PatchApplier.Result.Error

extends Object
Class Inheritance
Annotations
@SuppressWarnings:JavaLangClash

A wrapper for a patch applying error that affects a given file.
Since
6.6

Field Summary

Modifier and TypeField and Description
pack-priv final HunkHeader
pack-priv final boolean
pack-priv final String
pack-priv final String

Constructor Summary

AccessConstructor and Description
pack-priv
Error(String msg, String oldFileName, HunkHeader hh, boolean isGitConflict)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public boolean

Returns:

true if conflict markers were added for this error.
isGitConflict
()

Signals if as part of encountering this error, conflict markers were added to the file.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

hhback to summary
pack-priv final HunkHeader hh
Annotations
@Nullable
isGitConflictback to summary
pack-priv final boolean isGitConflict
msgback to summary
pack-priv final String msg
oldFileNameback to summary
pack-priv final String oldFileName

Constructor Detail

Errorback to summary
pack-priv Error(String msg, String oldFileName, HunkHeader hh, boolean isGitConflict)

Method Detail

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
o:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
isGitConflictback to summary
public boolean isGitConflict()

Signals if as part of encountering this error, conflict markers were added to the file.

Returns:boolean

true if conflict markers were added for this error.

Since
6.10
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
org.eclipse.jgit.patch back to summary

private Class PatchApplier.SHA1InputStream

extends InputStream
Class Inheritance

An InputStream that updates a SHA1 on every byte read.

Field Summary

Modifier and TypeField and Description
private final SHA1
private final InputStream

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Closes this input stream and releases any system resources associated with the stream.

public SHA1
public int
read()

Implements abstract java.io.InputStream.read.

Reads the next byte of data from the input stream.

public int
read(byte[]
the buffer into which the data is read.
b
,
int
the start offset in array b at which the data is written.
off
,
int
the maximum number of bytes to read.
len
)

Overrides java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes.

Inherited from java.io.InputStream:
availablemarkmarkSupportednullInputStreamreadreadAllBytesreadNBytesreadNBytesresetskipskipNBytestransferTo

Field Detail

hashback to summary
private final SHA1 hash
inback to summary
private final InputStream in

Constructor Detail

SHA1InputStreamback to summary
pack-priv SHA1InputStream(InputStream in, long size)

Method Detail

closeback to summary
public void close() throws IOException

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.InputStream.close.

Closes this input stream and releases any system resources associated with the stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
getHashback to summary
public SHA1 getHash()
readback to summary
public int read() throws IOException

Implements abstract java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Returns:int

the next byte of data, or -1 if the end of the stream is reached.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
readback to summary
public int read(byte[] b, int off, int len) throws IOException

Overrides java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.

This method blocks until input data is available, end of file is detected, or an exception is thrown.

If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

The first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, at most, equal to len. Let k be the number of bytes actually read; these bytes will be stored in elements b[off] through b[off+k-1], leaving elements b[off+k] through b[off+len-1] unaffected.

In every case, elements b[0] through b[off-1] and elements b[off+len] through b[b.length-1] are unaffected.

Parameters
b:byte[]

the buffer into which the data is read.

off:int

the start offset in array b at which the data is written.

len:int

the maximum number of bytes to read.

Returns:int

the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

Annotations
@Override
Exceptions
IOException:
If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs.