<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">

<article lang="en" id="gitdiffcore(7)">
<articleinfo>
    <title>gitdiffcore(7)</title>
        <indexterm>
                <primary>gitdiffcore(7)</primary>
        </indexterm>
</articleinfo>
<simplesect id="_name">
<title>NAME</title>
<simpara>gitdiffcore - Tweaking diff output (June 2005)</simpara>
</simplesect>
<simplesect id="_synopsis">
<title>SYNOPSIS</title>
<simpara><emphasis>git diff</emphasis> *</simpara>
</simplesect>
<simplesect id="_description">
<title>DESCRIPTION</title>
<simpara>The diff commands <emphasis>git-diff-index</emphasis>, <emphasis>git-diff-files</emphasis>, and <emphasis>git-diff-tree</emphasis>
can be told to manipulate differences they find in
unconventional ways before showing <emphasis>diff</emphasis> output.  The manipulation
is collectively called "diffcore transformation".  This short note
describes what they are and how to use them to produce <emphasis>diff</emphasis> output
that is easier to understand than the conventional kind.</simpara>
</simplesect>
<simplesect id="_the_chain_of_operation">
<title>The chain of operation</title>
<simpara>The <emphasis>git-diff-&#42;</emphasis> family works by first comparing two sets of
files:</simpara>
<itemizedlist>
<listitem>
<simpara>
<emphasis>git-diff-index</emphasis> compares contents of a "tree" object and the
   working directory (when <emphasis>--cached</emphasis> flag is not used) or a
   "tree" object and the index file (when <emphasis>--cached</emphasis> flag is
   used);
</simpara>
</listitem>
<listitem>
<simpara>
<emphasis>git-diff-files</emphasis> compares contents of the index file and the
   working directory;
</simpara>
</listitem>
<listitem>
<simpara>
<emphasis>git-diff-tree</emphasis> compares contents of two "tree" objects;
</simpara>
</listitem>
</itemizedlist>
<simpara>In all of these cases, the commands themselves first optionally limit
the two sets of files by any pathspecs given on their command-lines,
and compare corresponding paths in the two resulting sets of files.</simpara>
<simpara>The pathspecs are used to limit the world diff operates in.  They remove
the filepairs outside the specified sets of pathnames.  E.g. If the
input set of filepairs included:</simpara>
<literallayout>:100644 100644 bcd1234... 0123456... M junkfile</literallayout>
<simpara>but the command invocation was <literal>git diff-files myfile</literal>, then the
junkfile entry would be removed from the list because only "myfile"
is under consideration.</simpara>
<simpara>The result of comparison is passed from these commands to what is
internally called "diffcore", in a format similar to what is output
when the -p option is not used.  E.g.</simpara>
<literallayout>in-place edit  :100644 100644 bcd1234... 0123456... M file0
create         :000000 100644 0000000... 1234567... A file4
delete         :100644 000000 1234567... 0000000... D file5
unmerged       :000000 000000 0000000... 0000000... U file6</literallayout>
<simpara>The diffcore mechanism is fed a list of such comparison results
(each of which is called "filepair", although at this point each
of them talks about a single file), and transforms such a list
into another list.  There are currently 5 such transformations:</simpara>
<itemizedlist>
<listitem>
<simpara>
diffcore-break
</simpara>
</listitem>
<listitem>
<simpara>
diffcore-rename
</simpara>
</listitem>
<listitem>
<simpara>
diffcore-merge-broken
</simpara>
</listitem>
<listitem>
<simpara>
diffcore-pickaxe
</simpara>
</listitem>
<listitem>
<simpara>
diffcore-order
</simpara>
</listitem>
</itemizedlist>
<simpara>These are applied in sequence.  The set of filepairs <emphasis>git-diff-&#42;</emphasis>
commands find are used as the input to diffcore-break, and
the output from diffcore-break is used as the input to the
next transformation.  The final result is then passed to the
output routine and generates either diff-raw format (see Output
format sections of the manual for <emphasis>git-diff-&#42;</emphasis> commands) or
diff-patch format.</simpara>
</simplesect>
<simplesect id="_diffcore_break_for_splitting_up_complete_rewrites">
<title>diffcore-break: For Splitting Up "Complete Rewrites"</title>
<simpara>The second transformation in the chain is diffcore-break, and is
controlled by the -B option to the <emphasis>git-diff-&#42;</emphasis> commands.  This is
used to detect a filepair that represents "complete rewrite" and
break such filepair into two filepairs that represent delete and
create.  E.g.  If the input contained this filepair:</simpara>
<literallayout>:100644 100644 bcd1234... 0123456... M file0</literallayout>
<simpara>and if it detects that the file "file0" is completely rewritten,
it changes it to:</simpara>
<literallayout>:100644 000000 bcd1234... 0000000... D file0
:000000 100644 0000000... 0123456... A file0</literallayout>
<simpara>For the purpose of breaking a filepair, diffcore-break examines
the extent of changes between the contents of the files before
and after modification (i.e. the contents that have "bcd1234&#8230;"
and "0123456&#8230;" as their SHA1 content ID, in the above
example).  The amount of deletion of original contents and
insertion of new material are added together, and if it exceeds
the "break score", the filepair is broken into two.  The break
score defaults to 50% of the size of the smaller of the original
and the result (i.e. if the edit shrinks the file, the size of
the result is used; if the edit lengthens the file, the size of
the original is used), and can be customized by giving a number
after "-B" option (e.g. "-B75" to tell it to use 75%).</simpara>
</simplesect>
<simplesect id="_diffcore_rename_for_detection_renames_and_copies">
<title>diffcore-rename: For Detection Renames and Copies</title>
<simpara>This transformation is used to detect renames and copies, and is
controlled by the -M option (to detect renames) and the -C option
(to detect copies as well) to the <emphasis>git-diff-&#42;</emphasis> commands.  If the
input contained these filepairs:</simpara>
<literallayout>:100644 000000 0123456... 0000000... D fileX
:000000 100644 0000000... 0123456... A file0</literallayout>
<simpara>and the contents of the deleted file fileX is similar enough to
the contents of the created file file0, then rename detection
merges these filepairs and creates:</simpara>
<literallayout>:100644 100644 0123456... 0123456... R100 fileX file0</literallayout>
<simpara>When the "-C" option is used, the original contents of modified files,
and deleted files (and also unmodified files, if the
"--find-copies-harder" option is used) are considered as candidates
of the source files in rename/copy operation.  If the input were like
these filepairs, that talk about a modified file fileY and a newly
created file file0:</simpara>
<literallayout>:100644 100644 0123456... 1234567... M fileY
:000000 100644 0000000... bcd3456... A file0</literallayout>
<simpara>the original contents of fileY and the resulting contents of
file0 are compared, and if they are similar enough, they are
changed to:</simpara>
<literallayout>:100644 100644 0123456... 1234567... M fileY
:100644 100644 0123456... bcd3456... C100 fileY file0</literallayout>
<simpara>In both rename and copy detection, the same "extent of changes"
algorithm used in diffcore-break is used to determine if two
files are "similar enough", and can be customized to use
a similarity score different from the default of 50% by giving a
number after the "-M" or "-C" option (e.g. "-M8" to tell it to use
8/10 = 80%).</simpara>
<simpara>Note.  When the "-C" option is used with <literal>--find-copies-harder</literal>
option, <emphasis>git-diff-&#42;</emphasis> commands feed unmodified filepairs to
diffcore mechanism as well as modified ones.  This lets the copy
detector consider unmodified files as copy source candidates at
the expense of making it slower.  Without <literal>--find-copies-harder</literal>,
<emphasis>git-diff-&#42;</emphasis> commands can detect copies only if the file that was
copied happened to have been modified in the same changeset.</simpara>
</simplesect>
<simplesect id="_diffcore_merge_broken_for_putting_complete_rewrites_back_together">
<title>diffcore-merge-broken: For Putting "Complete Rewrites" Back Together</title>
<simpara>This transformation is used to merge filepairs broken by
diffcore-break, and not transformed into rename/copy by
diffcore-rename, back into a single modification.  This always
runs when diffcore-break is used.</simpara>
<simpara>For the purpose of merging broken filepairs back, it uses a
different "extent of changes" computation from the ones used by
diffcore-break and diffcore-rename.  It counts only the deletion
from the original, and does not count insertion.  If you removed
only 10 lines from a 100-line document, even if you added 910
new lines to make a new 1000-line document, you did not do a
complete rewrite.  diffcore-break breaks such a case in order to
help diffcore-rename to consider such filepairs as candidate of
rename/copy detection, but if filepairs broken that way were not
matched with other filepairs to create rename/copy, then this
transformation merges them back into the original
"modification".</simpara>
<simpara>The "extent of changes" parameter can be tweaked from the
default 80% (that is, unless more than 80% of the original
material is deleted, the broken pairs are merged back into a
single modification) by giving a second number to -B option,
like these:</simpara>
<itemizedlist>
<listitem>
<simpara>
-B50/60 (give 50% "break score" to diffcore-break, use 60%
  for diffcore-merge-broken).
</simpara>
</listitem>
<listitem>
<simpara>
-B/60 (the same as above, since diffcore-break defaults to 50%).
</simpara>
</listitem>
</itemizedlist>
<simpara>Note that earlier implementation left a broken pair as a separate
creation and deletion patches.  This was an unnecessary hack and
the latest implementation always merges all the broken pairs
back into modifications, but the resulting patch output is
formatted differently for easier review in case of such
a complete rewrite by showing the entire contents of old version
prefixed with <emphasis>-</emphasis>, followed by the entire contents of new
version prefixed with <emphasis>+</emphasis>.</simpara>
</simplesect>
<simplesect id="_diffcore_pickaxe_for_detecting_addition_deletion_of_specified_string">
<title>diffcore-pickaxe: For Detecting Addition/Deletion of Specified String</title>
<simpara>This transformation is used to find filepairs that represent
changes that touch a specified string, and is controlled by the
-S option and the <literal>--pickaxe-all</literal> option to the <emphasis>git-diff-&#42;</emphasis>
commands.</simpara>
<simpara>When diffcore-pickaxe is in use, it checks if there are
filepairs whose "original" side has the specified string and
whose "result" side does not.  Such a filepair represents "the
string appeared in this changeset".  It also checks for the
opposite case that loses the specified string.</simpara>
<simpara>When <literal>--pickaxe-all</literal> is not in effect, diffcore-pickaxe leaves
only such filepairs that touch the specified string in its
output.  When <literal>--pickaxe-all</literal> is used, diffcore-pickaxe leaves all
filepairs intact if there is such a filepair, or makes the
output empty otherwise.  The latter behaviour is designed to
make reviewing of the changes in the context of the whole
changeset easier.</simpara>
</simplesect>
<simplesect id="_diffcore_order_for_sorting_the_output_based_on_filenames">
<title>diffcore-order: For Sorting the Output Based on Filenames</title>
<simpara>This is used to reorder the filepairs according to the user&#8217;s
(or project&#8217;s) taste, and is controlled by the -O option to the
<emphasis>git-diff-&#42;</emphasis> commands.</simpara>
<simpara>This takes a text file each of whose lines is a shell glob
pattern.  Filepairs that match a glob pattern on an earlier line
in the file are output before ones that match a later line, and
filepairs that do not match any glob pattern are output last.</simpara>
<simpara>As an example, a typical orderfile for the core git probably
would look like this:</simpara>
<literallayout>README
Makefile
Documentation
*.h
*.c
t</literallayout>
</simplesect>
<simplesect id="_see_also">
<title>SEE ALSO</title>
<simpara><xref linkend="git-diff(1)"/>,
<xref linkend="git-diff-files(1)"/>,
<xref linkend="git-diff-index(1)"/>,
<xref linkend="git-diff-tree(1)"/>,
<xref linkend="git-format-patch(1)"/>,
<xref linkend="git-log(1)"/>,
<xref linkend="gitglossary(7)"/>,
<ulink url="user-manual.html">The Git User&#8217;s Manual</ulink></simpara>
</simplesect>
<simplesect id="_git">
<title>GIT</title>
<simpara>Part of the <xref linkend="git(1)"/> suite.</simpara>
</simplesect>
</article>
