@matheusbsbs / matheus-bagliano / commits / 3046c7f69a
Fork 0

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>

Đoàn Trần Công Danh committed Aug 21, 2020 at 18:51 UTC 3046c7f69a6337a971b219d047b8110e3f9ea971
6 files changed +99 -5
Documentation/diff-options.txt
+5 -4
@@ -441,10 +441,11 @@ endif::git-format-patch[]
441 --abbrev[=<n>]::
442 Instead of showing the full 40-byte hexadecimal object
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>`.
449
450 -B[<n>][/<m>]::
451 --break-rewrites[=[<n>][/<m>]]::
diff.c
+4 -1
@@ -4319,7 +4319,10 @@ static void fill_metainfo(struct strbuf *msg,
4319 }
4320 if (one && two && !oideq(&one->oid, &two->oid)) {
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;
4326
4327 if (o->flags.binary) {
4328 mmfile_t mf;
t/t4013-diff-various.sh
+3
@@ -239,6 +239,9 @@ diff-tree --root -r --abbrev=4 initial
239 :noellipses diff-tree --root -r --abbrev=4 initial
240 diff-tree -p initial
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
245 diff-tree --patch-with-stat initial
246 diff-tree --root --patch-with-stat initial
247 diff-tree --patch-with-raw initial
t/t4013/diff.diff-tree_--root_-p_--abbrev=10_initial new
+29
@@ -0,0 +1,29 @@
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 +$
t/t4013/diff.diff-tree_--root_-p_--full-index_--abbrev=10_initial new
+29
@@ -0,0 +1,29 @@
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 +$
t/t4013/diff.diff-tree_--root_-p_--full-index_initial new
+29
@@ -0,0 +1,29 @@
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 +$