git-submodule.sh: shorten submodule SHA-1s using rev-parse
authorSven van Haastregt <svenvh@gmail.com>
Sun, 3 Feb 2019 21:00:27 +0000 (21:00 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 4 Feb 2019 21:33:56 +0000 (13:33 -0800)
Until now, `git submodule summary` was always emitting 7-character
SHA-1s that have a higher chance of being ambiguous for larger
repositories. Use `git rev-parse --short` instead, which will
determine suitable short SHA-1 lengths.

When a submodule hasn't been initialized with "submodule init" or
not cloned, `git rev-parse` would not work in it yet; as a fallback,
use the original method of cutting at 7 hexdigits.

Signed-off-by: Sven van Haastregt <svenvh@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
git-submodule.sh
index 5e608f8bad305fea40e9063b854bf1be24ebd448..e26146e72115551ac69af22b2045905e490b3635 100755 (executable)
@@ -850,8 +850,11 @@ cmd_summary() {
                        ;;
                esac
 
-               sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
-               sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
+               sha1_abbr_src=$(GIT_DIR="$name/.git" git rev-parse --short $sha1_src 2>/dev/null ||
+                       echo $sha1_src | cut -c1-7)
+               sha1_abbr_dst=$(GIT_DIR="$name/.git" git rev-parse --short $sha1_dst 2>/dev/null ||
+                       echo $sha1_dst | cut -c1-7)
+
                if test $status = T
                then
                        blob="$(gettext "blob")"