Top Description Inners Constructors Methods
org.eclipse.jgit.diff

public abstract Class DiffAlgorithm

extends Object
Class Inheritance
Known Direct Subclasses
org.eclipse.jgit.diff.LowLevelDiffAlgorithm

Compares two org.eclipse.jgit.diff.Sequences to create an org.eclipse.jgit.diff.EditList of changes.

An algorithm's diff method must be callable from concurrent threads without data collisions. This permits some algorithms to use a singleton pattern, with concurrent invocations using the same singleton. Other algorithms may support parameterization, in which case the caller can create a unique instance per thread.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
DiffAlgorithm.SupportedAlgorithm

Supported diff algorithm

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static <S extends Sequence> Edit
coverEdit(S a, S b)

public <
type of sequence being compared.
S extends Sequence
>
EditList

Returns:

a modifiable edit list comparing the two sequences. If empty, the sequences are identical according to cmp's rules. The result list is never null.
diff
(SequenceComparator<? super S>
the comparator supplying the element equivalence function.
cmp
,
S
the first (also known as old or pre-image) sequence. Edits returned by this algorithm will reference indexes using the 'A' side: org.eclipse.jgit.diff.Edit#getBeginA(), org.eclipse.jgit.diff.Edit#getEndA().
a
,
S
the second (also known as new or post-image) sequence. Edits returned by this algorithm will reference indexes using the 'B' side: org.eclipse.jgit.diff.Edit#getBeginB(), org.eclipse.jgit.diff.Edit#getEndB().
b
)

Compare two sequences and identify a list of edits between them.

public abstract <
type of sequence being compared.
S extends Sequence
>
EditList

Returns:

a modifiable edit list comparing the two sequences.
diffNonCommon
(SequenceComparator<? super S>
the comparator supplying the element equivalence function.
cmp
,
S
the first (also known as old or pre-image) sequence. Edits returned by this algorithm will reference indexes using the 'A' side: org.eclipse.jgit.diff.Edit#getBeginA(), org.eclipse.jgit.diff.Edit#getEndA().
a
,
S
the second (also known as new or post-image) sequence. Edits returned by this algorithm will reference indexes using the 'B' side: org.eclipse.jgit.diff.Edit#getBeginB(), org.eclipse.jgit.diff.Edit#getEndB().
b
)

Compare two sequences and identify a list of edits between them.

public static DiffAlgorithm

Returns:

an implementation of the specified diff algorithm
getAlgorithm
(DiffAlgorithm.SupportedAlgorithm
the diff algorithm for which an implementation should be returned
alg
)

Get diff algorithm

private static <
type of sequence being compared.
S extends Sequence
>
EditList

Returns:

a modifiable edit list with edit regions shifted to their latest possible location. The result list is never null.
normalize
(SequenceComparator<? super S>
the comparator supplying the element equivalence function.
cmp
,
EditList
a modifiable edit list comparing the provided sequences.
e
,
S
the first (also known as old or pre-image) sequence.
a
,
S
the second (also known as new or post-image) sequence.
b
)

Reorganize an EditList for better diff consistency.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

DiffAlgorithmback to summary
public DiffAlgorithm()

Method Detail

coverEditback to summary
private static <S extends Sequence> Edit coverEdit(S a, S b)
diffback to summary
public <S extends Sequence> EditList diff(SequenceComparator<? super S> cmp, S a, S b)

Compare two sequences and identify a list of edits between them.

Parameters
<S>
type of sequence being compared.
cmp:SequenceComparator<? super S>

the comparator supplying the element equivalence function.

a:S

the first (also known as old or pre-image) sequence. Edits returned by this algorithm will reference indexes using the 'A' side: org.eclipse.jgit.diff.Edit#getBeginA(), org.eclipse.jgit.diff.Edit#getEndA().

b:S

the second (also known as new or post-image) sequence. Edits returned by this algorithm will reference indexes using the 'B' side: org.eclipse.jgit.diff.Edit#getBeginB(), org.eclipse.jgit.diff.Edit#getEndB().

Returns:EditList

a modifiable edit list comparing the two sequences. If empty, the sequences are identical according to cmp's rules. The result list is never null.

diffNonCommonback to summary
public abstract <S extends Sequence> EditList diffNonCommon(SequenceComparator<? super S> cmp, S a, S b)

Compare two sequences and identify a list of edits between them. This method should be invoked only after the two sequences have been proven to have no common starting or ending elements. The expected elimination of common starting and ending elements is automatically performed by the diff(SequenceComparator, Sequence, Sequence) method, which invokes this method using org.eclipse.jgit.diff.Subsequences.

Parameters
<S>
type of sequence being compared.
cmp:SequenceComparator<? super S>

the comparator supplying the element equivalence function.

a:S

the first (also known as old or pre-image) sequence. Edits returned by this algorithm will reference indexes using the 'A' side: org.eclipse.jgit.diff.Edit#getBeginA(), org.eclipse.jgit.diff.Edit#getEndA().

b:S

the second (also known as new or post-image) sequence. Edits returned by this algorithm will reference indexes using the 'B' side: org.eclipse.jgit.diff.Edit#getBeginB(), org.eclipse.jgit.diff.Edit#getEndB().

Returns:EditList

a modifiable edit list comparing the two sequences.

getAlgorithmback to summary
public static DiffAlgorithm getAlgorithm(DiffAlgorithm.SupportedAlgorithm alg)

Get diff algorithm

Parameters
alg:DiffAlgorithm.SupportedAlgorithm

the diff algorithm for which an implementation should be returned

Returns:DiffAlgorithm

an implementation of the specified diff algorithm

normalizeback to summary
private static <S extends Sequence> EditList normalize(SequenceComparator<? super S> cmp, EditList e, S a, S b)

Reorganize an EditList for better diff consistency.

DiffAlgorithms may return Edit.Type#INSERT or Edit.Type#DELETE edits that can be "shifted". For example, the deleted section

-a
-b
-c
 a
 b
 c
can be shifted down by 1, 2 or 3 locations.

To avoid later merge issues, we shift such edits to a consistent location. normalize uses a simple strategy of shifting such edits to their latest possible location.

This strategy may not always produce an aesthetically pleasing diff. For instance, it works well with

 function1 {
  ...
 }

+function2 {
+ ...
+}
+
function3 {
...
}
but less so for
 #
 # comment1
 #
 function1() {
 }

 #
+# comment3
+#
+function3() {
+}
+
+#
 # comment2
 #
 function2() {
 }
More sophisticated strategies are possible, say by calculating a suitable "aesthetic cost" for each possible position and using the lowest cost, but normalize just shifts edits to the end as much as possible.
Parameters
<S>
type of sequence being compared.
cmp:SequenceComparator<? super S>

the comparator supplying the element equivalence function.

e:EditList

a modifiable edit list comparing the provided sequences.

a:S

the first (also known as old or pre-image) sequence.

b:S

the second (also known as new or post-image) sequence.

Returns:EditList

a modifiable edit list with edit regions shifted to their latest possible location. The result list is never null.

Since
4.7
org.eclipse.jgit.diff back to summary

public final Enum DiffAlgorithm.SupportedAlgorithm

extends Enum<DiffAlgorithm.SupportedAlgorithm>
Class Inheritance

Supported diff algorithm

Field Summary

Modifier and TypeField and Description
public static final DiffAlgorithm.SupportedAlgorithm
HISTOGRAM

Histogram diff algorithm

public static final DiffAlgorithm.SupportedAlgorithm
MYERS

Myers diff algorithm

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static DiffAlgorithm.SupportedAlgorithm
public static DiffAlgorithm.SupportedAlgorithm[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

HISTOGRAMback to summary
public static final DiffAlgorithm.SupportedAlgorithm HISTOGRAM

Histogram diff algorithm

MYERSback to summary
public static final DiffAlgorithm.SupportedAlgorithm MYERS

Myers diff algorithm

Constructor Detail

SupportedAlgorithmback to summary
private SupportedAlgorithm()

Method Detail

valueOfback to summary
public static DiffAlgorithm.SupportedAlgorithm valueOf(String name)
valuesback to summary
public static DiffAlgorithm.SupportedAlgorithm[] values()