Git fork

diff: index-line: respect --abbrev in object's name

A handful of Git's commands respect `--abbrev' for customizing length
of abbreviation of object names.

For diff-family, Git supports 2 different options for 2 different
purposes, `--full-index' for showing diff-patch object's name in full,
and `--abbrev' to customize the length of object names in diff-raw and
diff-tree header lines, without any options to customise the length of
object names in diff-patch format. When working with diff-patch format,
we only have two options, either full index, or default abbrev length.

Although, that behaviour is documented, it doesn't stop users from
trying to use `--abbrev' with the hope of customising diff-patch's
objects' name's abbreviation.

Let's allow the blob object names shown on the "index" line to be
abbreviated to arbitrary length given via the "--abbrev" option.

To preserve backward compatibility with old script that specify both
`--full-index' and `--abbrev', always show full object id
if `--full-index' is specified.

Signed-off-by: Đoàn Trần Công Danh <congdanhqx@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Đoàn Trần Công Danh and committed by
Junio C Hamano
3046c7f6 fc7e73d7

+99 -5
+5 -4
Documentation/diff-options.txt
··· 441 441 --abbrev[=<n>]:: 442 442 Instead of showing the full 40-byte hexadecimal object 443 443 name in diff-raw format output and diff-tree header 444 - lines, show only a partial prefix. This is 445 - independent of the `--full-index` option above, which controls 446 - the diff-patch output format. Non default number of 447 - digits can be specified with `--abbrev=<n>`. 444 + lines, show only a partial prefix. 445 + In diff-patch output format, `--full-index` takes higher 446 + precedence, i.e. if `--full-index` is specified, full blob 447 + names will be shown regardless of `--abbrev`. 448 + Non default number of digits can be specified with `--abbrev=<n>`. 448 449 449 450 -B[<n>][/<m>]:: 450 451 --break-rewrites[=[<n>][/<m>]]::
+4 -1
diff.c
··· 4319 4319 } 4320 4320 if (one && two && !oideq(&one->oid, &two->oid)) { 4321 4321 const unsigned hexsz = the_hash_algo->hexsz; 4322 - int abbrev = o->flags.full_index ? hexsz : DEFAULT_ABBREV; 4322 + int abbrev = o->abbrev ? o->abbrev : DEFAULT_ABBREV; 4323 + 4324 + if (o->flags.full_index) 4325 + abbrev = hexsz; 4323 4326 4324 4327 if (o->flags.binary) { 4325 4328 mmfile_t mf;
+3
t/t4013-diff-various.sh
··· 239 239 :noellipses diff-tree --root -r --abbrev=4 initial 240 240 diff-tree -p initial 241 241 diff-tree --root -p initial 242 + diff-tree --root -p --abbrev=10 initial 243 + diff-tree --root -p --full-index initial 244 + diff-tree --root -p --full-index --abbrev=10 initial 242 245 diff-tree --patch-with-stat initial 243 246 diff-tree --root --patch-with-stat initial 244 247 diff-tree --patch-with-raw initial
+29
t/t4013/diff.diff-tree_--root_-p_--abbrev=10_initial
··· 1 + $ git diff-tree --root -p --abbrev=10 initial 2 + 444ac553ac7612cc88969031b02b3767fb8a353a 3 + diff --git a/dir/sub b/dir/sub 4 + new file mode 100644 5 + index 0000000000..35d242ba79 6 + --- /dev/null 7 + +++ b/dir/sub 8 + @@ -0,0 +1,2 @@ 9 + +A 10 + +B 11 + diff --git a/file0 b/file0 12 + new file mode 100644 13 + index 0000000000..01e79c32a8 14 + --- /dev/null 15 + +++ b/file0 16 + @@ -0,0 +1,3 @@ 17 + +1 18 + +2 19 + +3 20 + diff --git a/file2 b/file2 21 + new file mode 100644 22 + index 0000000000..01e79c32a8 23 + --- /dev/null 24 + +++ b/file2 25 + @@ -0,0 +1,3 @@ 26 + +1 27 + +2 28 + +3 29 + $
+29
t/t4013/diff.diff-tree_--root_-p_--full-index_--abbrev=10_initial
··· 1 + $ git diff-tree --root -p --full-index --abbrev=10 initial 2 + 444ac553ac7612cc88969031b02b3767fb8a353a 3 + diff --git a/dir/sub b/dir/sub 4 + new file mode 100644 5 + index 0000000000000000000000000000000000000000..35d242ba79ae89ac695e26b3d4c27a8e6f028f9e 6 + --- /dev/null 7 + +++ b/dir/sub 8 + @@ -0,0 +1,2 @@ 9 + +A 10 + +B 11 + diff --git a/file0 b/file0 12 + new file mode 100644 13 + index 0000000000000000000000000000000000000000..01e79c32a8c99c557f0757da7cb6d65b3414466d 14 + --- /dev/null 15 + +++ b/file0 16 + @@ -0,0 +1,3 @@ 17 + +1 18 + +2 19 + +3 20 + diff --git a/file2 b/file2 21 + new file mode 100644 22 + index 0000000000000000000000000000000000000000..01e79c32a8c99c557f0757da7cb6d65b3414466d 23 + --- /dev/null 24 + +++ b/file2 25 + @@ -0,0 +1,3 @@ 26 + +1 27 + +2 28 + +3 29 + $
+29
t/t4013/diff.diff-tree_--root_-p_--full-index_initial
··· 1 + $ git diff-tree --root -p --full-index initial 2 + 444ac553ac7612cc88969031b02b3767fb8a353a 3 + diff --git a/dir/sub b/dir/sub 4 + new file mode 100644 5 + index 0000000000000000000000000000000000000000..35d242ba79ae89ac695e26b3d4c27a8e6f028f9e 6 + --- /dev/null 7 + +++ b/dir/sub 8 + @@ -0,0 +1,2 @@ 9 + +A 10 + +B 11 + diff --git a/file0 b/file0 12 + new file mode 100644 13 + index 0000000000000000000000000000000000000000..01e79c32a8c99c557f0757da7cb6d65b3414466d 14 + --- /dev/null 15 + +++ b/file0 16 + @@ -0,0 +1,3 @@ 17 + +1 18 + +2 19 + +3 20 + diff --git a/file2 b/file2 21 + new file mode 100644 22 + index 0000000000000000000000000000000000000000..01e79c32a8c99c557f0757da7cb6d65b3414466d 23 + --- /dev/null 24 + +++ b/file2 25 + @@ -0,0 +1,3 @@ 26 + +1 27 + +2 28 + +3 29 + $