From 28f9af5d25ec04288d8c501058d83ac76c8be6f4 Mon Sep 17 00:00:00 2001 From: Ping Yin Date: Tue, 11 Mar 2008 21:52:15 +0800 Subject: [PATCH 1/6] git-submodule summary: code framework These patches teach git-submodule a new subcommand 'summary' to show commit summary of checked out submodules between a given super project commit (defaults to HEAD) and working tree (or index, when --cached is given). This patch just introduces the framework to find submodules which have summary to show. A submodule will have summary if it falls into these cases: - type 'M': modified and checked out (1) - type 'A': added and checked out (2) - type 'D': deleted - type 'T': typechanged (blob <-> submodule) Notes: 1. There may be modified but not checked out cases. In the case of a merge conflict, even if the submodule is not checked out, there may be still a diff between index and HEAD on the submodule entry (i.e. modified). The summary will not be show for such a submodule. 2. A similar explanation applies to the added but not checked out case. Signed-off-by: Ping Yin Signed-off-by: Junio C Hamano --- git-submodule.sh | 61 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/git-submodule.sh b/git-submodule.sh index 67d3224c8c..dbb866644a 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -4,7 +4,7 @@ # # Copyright (c) 2007 Lars Hjemli -USAGE='[--quiet] [--cached] [add [-b branch]|status|init|update] [--] [...]' +USAGE='[--quiet] [--cached] [add [-b branch]|status|init|update|summary []] [--] [...]' OPTIONS_SPEC= . git-sh-setup require_work_tree @@ -320,7 +320,58 @@ set_name_rev () { ) ) test -z "$revname" || revname=" ($revname)" } +# +# Show commit summary for submodules in index or working tree +# +# If '--cached' is given, show summary between index and given commit, +# or between working tree and given commit +# +# $@ = [commit (default 'HEAD'),] requested paths (default all) +# +cmd_summary() { + # parse $args after "submodule ... summary". + while test $# -ne 0 + do + case "$1" in + --cached) + cached="$1" + ;; + --) + shift + break + ;; + -*) + usage + ;; + *) + break + ;; + esac + shift + done + if rev=$(git rev-parse --verify "$1^0" 2>/dev/null) + then + head=$rev + shift + else + head=HEAD + fi + + cd_to_toplevel + # Get modified modules cared by user + modules=$(git diff-index $cached --raw $head -- "$@" | + grep -e '^:160000' -e '^:[0-7]* 160000' | + while read mod_src mod_dst sha1_src sha1_dst status name + do + # Always show modules deleted or type-changed (blob<->module) + test $status = D -o $status = T && echo "$name" && continue + # Also show added or modified modules which are checked out + GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 && + echo "$name" + done + ) +} # # List all submodules, prefixed with: # - submodule not initialized @@ -391,7 +442,7 @@ cmd_status() while test $# != 0 && test -z "$command" do case "$1" in - add | init | update | status) + add | init | update | status | summary) command=$1 ;; -q|--quiet) @@ -406,7 +457,7 @@ do branch="$2"; shift ;; --cached) - cached=1 + cached="$1" ;; --) break @@ -430,8 +481,8 @@ then usage fi -# "--cached" is accepted only by "status" -if test -n "$cached" && test "$command" != status +# "--cached" is accepted only by "status" and "summary" +if test -n "$cached" && test "$command" != status -a "$command" != summary then usage fi From 1cb639e6b02d8740cd2d4516a34822b43660babc Mon Sep 17 00:00:00 2001 From: Ping Yin Date: Tue, 11 Mar 2008 21:52:16 +0800 Subject: [PATCH 2/6] git-submodule summary: show commit summary This patch does the hard work to show submodule commit summary. For a modified submodule, a series of commits will be shown with the following command: git log --pretty='format:%m %s' \ --first-parent sha1_src...sha1_dst where the sha1_src is from the given super project commit and the sha1_dst is from the index or working tree (switched by --cached). For a deleted, added, or typechanged (blob<->submodule) submodule, only one single newest commit from the existing end (for example, src end for submodule deleted or type changed from submodule to blob) will be shown. If the src/dst sha1 for a submodule is missing in the submodule directory, a warning will be issued except in two cases where the submodule directory is deleted (type 'D') or typechanged to blob (one case of type 'T'). In the title line for a submodule, the src/dst sha1 and the number of commits (--first-parent) between the two commits will be shown. The following example demonstrates most cases. Example: commit summary for modified submodules sm1-sm5. -------------------------------------------- $ git submodule summary * sm1 354cd45...3f751e5 (4): < one line message for C < one line message for B > one line message for D > one line message for E * sm2 5c8bfb5...000000 (3): < one line message for F * sm3 354cd45...3f751e5: Warn: sm3 doesn't contain commit 354cd45 * sm4 354cd34(submodule)-> 235efa(blob) (1): < one line message for G * sm5 354cd34(blob)-> 235efa(submodule) (5): > one line message for H -------------------------------------------- Signed-off-by: Ping Yin Signed-off-by: Junio C Hamano --- git-submodule.sh | 102 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/git-submodule.sh b/git-submodule.sh index dbb866644a..01f26d341c 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -371,6 +371,108 @@ cmd_summary() { echo "$name" done ) + + test -n "$modules" && + git diff-index $cached --raw $head -- $modules | + grep -e '^:160000' -e '^:[0-7]* 160000' | + cut -c2- | + while read mod_src mod_dst sha1_src sha1_dst status name + do + if test -z "$cached" && + test $sha1_dst = 0000000000000000000000000000000000000000 + then + case "$mod_dst" in + 160000) + sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD) + ;; + 100644 | 100755 | 120000) + sha1_dst=$(git hash-object $name) + ;; + 000000) + ;; # removed + *) + # unexpected type + echo >&2 "unexpected mode $mod_dst" + continue ;; + esac + fi + missing_src= + missing_dst= + + test $mod_src = 160000 && + ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_src^0 >/dev/null 2>&1 && + missing_src=t + + test $mod_dst = 160000 && + ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_dst^0 >/dev/null 2>&1 && + missing_dst=t + + total_commits= + case "$missing_src,$missing_dst" in + t,) + errmsg=" Warn: $name doesn't contain commit $sha1_src" + ;; + ,t) + errmsg=" Warn: $name doesn't contain commit $sha1_dst" + ;; + t,t) + errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst" + ;; + *) + errmsg= + total_commits=$( + if test $mod_src = 160000 -a $mod_dst = 160000 + then + range="$sha1_src...$sha1_dst" + elif test $mod_src = 160000 + then + range=$sha1_src + else + range=$sha1_dst + fi + GIT_DIR="$name/.git" \ + git log --pretty=oneline --first-parent $range | wc -l + ) + total_commits=" ($total_commits)" + ;; + esac + + sha1_abbr_src=$(echo $sha1_src | cut -c1-7) + sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7) + if test $status = T + then + if test $mod_dst = 160000 + then + echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:" + else + echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:" + fi + else + echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:" + fi + if test -n "$errmsg" + then + # Don't give error msg for modification whose dst is not submodule + # i.e. deleted or changed to blob + test $mod_dst = 160000 && echo "$errmsg" + else + if test $mod_src = 160000 -a $mod_dst = 160000 + then + GIT_DIR="$name/.git" \ + git log --pretty='format: %m %s' \ + --first-parent $sha1_src...$sha1_dst + elif test $mod_dst = 160000 + then + GIT_DIR="$name/.git" \ + git log --pretty='format: > %s' -1 $sha1_dst + else + GIT_DIR="$name/.git" \ + git log --pretty='format: < %s' -1 $sha1_src + fi + echo + fi + echo + done } # # List all submodules, prefixed with: From f2dc06a344257df7b9307fc8453af7342409b610 Mon Sep 17 00:00:00 2001 From: Ping Yin Date: Tue, 11 Mar 2008 21:52:17 +0800 Subject: [PATCH 3/6] git-submodule summary: limit summary size This patch teaches git-submodule an option '--summary-limit|-n ' to limit number of commits in total for the summary of each submodule in the modified case (only a single commit is shown in other cases). Giving 0 will disable the summary; a negative number means unlimted, which is the default. Signed-off-by: Ping Yin Signed-off-by: Junio C Hamano --- git-submodule.sh | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/git-submodule.sh b/git-submodule.sh index 01f26d341c..4d28fc0988 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -4,7 +4,9 @@ # # Copyright (c) 2007 Lars Hjemli -USAGE='[--quiet] [--cached] [add [-b branch]|status|init|update|summary []] [--] [...]' +USAGE="[--quiet] [--cached] \ +[add [-b branch]|status|init|update|summary [-n|--summary-limit ] []] \ +[--] [...]" OPTIONS_SPEC= . git-sh-setup require_work_tree @@ -329,6 +331,8 @@ set_name_rev () { # $@ = [commit (default 'HEAD'),] requested paths (default all) # cmd_summary() { + summary_limit=-1 + # parse $args after "submodule ... summary". while test $# -ne 0 do @@ -336,6 +340,15 @@ cmd_summary() { --cached) cached="$1" ;; + -n|--summary-limit) + if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2" + then + : + else + usage + fi + shift + ;; --) shift break @@ -350,6 +363,8 @@ cmd_summary() { shift done + test $summary_limit = 0 && return + if rev=$(git rev-parse --verify "$1^0" 2>/dev/null) then head=$rev @@ -458,8 +473,10 @@ cmd_summary() { else if test $mod_src = 160000 -a $mod_dst = 160000 then + limit= + test $summary_limit -gt 0 && limit="-$summary_limit" GIT_DIR="$name/.git" \ - git log --pretty='format: %m %s' \ + git log $limit --pretty='format: %m %s' \ --first-parent $sha1_src...$sha1_dst elif test $mod_dst = 160000 then From 925e7f622d433e1cb2c7556acf8868bb30565a61 Mon Sep 17 00:00:00 2001 From: Ping Yin Date: Tue, 11 Mar 2008 21:52:18 +0800 Subject: [PATCH 4/6] git-submodule summary: documentation Signed-off-by: Ping Yin Signed-off-by: Junio C Hamano --- Documentation/git-submodule.txt | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Documentation/git-submodule.txt b/Documentation/git-submodule.txt index e818e6e789..e96bf36521 100644 --- a/Documentation/git-submodule.txt +++ b/Documentation/git-submodule.txt @@ -12,6 +12,7 @@ SYNOPSIS 'git-submodule' [--quiet] add [-b branch] [--] [] 'git-submodule' [--quiet] status [--cached] [--] [...] 'git-submodule' [--quiet] [init|update] [--] [...] +'git-submodule' [--quiet] summary [--summary-limit ] [commit] [--] [...] COMMANDS @@ -46,6 +47,11 @@ update:: checkout the commit specified in the index of the containing repository. This will make the submodules HEAD be detached. +summary:: + Show commit summary between the given commit (defaults to HEAD) and + working tree/index. For a submodule in question, a series of commits + in the submodule between the given super project commit and the + index or working tree (switched by --cached) are shown. OPTIONS ------- @@ -56,9 +62,16 @@ OPTIONS Branch of repository to add as submodule. --cached:: - Display the SHA-1 stored in the index, not the SHA-1 of the currently - checked out submodule commit. This option is only valid for the - status command. + This option is only valid for status and summary commands. These + commands typically use the commit found in the submodule HEAD, but + with this option, the commit stored in the index is used instead. + +-n, --summary-limit:: + This option is only valid for the summary command. + Limit the summary size (number of commits shown in total). + Giving 0 will disable the summary; a negative number means unlimted + (the default). This limit only applies to modified submodules. The + size is always limited to 1 for added/deleted/typechanged submodules. :: Path to submodule(s). When specified this will restrict the command From 2da2ddc664538f64c4d8a4ab61ef93b2b77a50d5 Mon Sep 17 00:00:00 2001 From: Ping Yin Date: Tue, 11 Mar 2008 21:52:19 +0800 Subject: [PATCH 5/6] git-submodule summary: test Signed-off-by: Ping Yin Signed-off-by: Junio C Hamano --- t/t7401-submodule-summary.sh | 195 +++++++++++++++++++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100755 t/t7401-submodule-summary.sh diff --git a/t/t7401-submodule-summary.sh b/t/t7401-submodule-summary.sh new file mode 100755 index 0000000000..0f3c42ab35 --- /dev/null +++ b/t/t7401-submodule-summary.sh @@ -0,0 +1,195 @@ +#!/bin/sh +# +# Copyright (c) 2008 Ping Yin +# + +test_description='Summary support for submodules + +This test tries to verify the sanity of summary subcommand of git-submodule. +' + +. ./test-lib.sh + +add_file () { + sm=$1 + shift + owd=$(pwd) + cd "$sm" + for name; do + echo "$name" > "$name" && + git add "$name" && + test_tick && + git commit -m "Add $name" + done >/dev/null + git rev-parse --verify HEAD | cut -c1-7 + cd "$owd" +} +commit_file () { + test_tick && + git commit "$@" -m "Commit $*" >/dev/null +} + +test_create_repo sm1 && +add_file . foo + +head1=$(add_file sm1 foo1 foo2) + +test_expect_success 'added submodule' " + git add sm1 && + git submodule summary >actual && + diff actual - <<-EOF +* sm1 0000000...$head1 (2): + > Add foo2 + +EOF +" + +commit_file sm1 && +head2=$(add_file sm1 foo3) + +test_expect_success 'modified submodule(forward)' " + git submodule summary >actual && + diff actual - <<-EOF +* sm1 $head1...$head2 (1): + > Add foo3 + +EOF +" + +commit_file sm1 && +cd sm1 && +git reset --hard HEAD~2 >/dev/null && +head3=$(git rev-parse --verify HEAD | cut -c1-7) && +cd .. + +test_expect_success 'modified submodule(backward)' " + git submodule summary >actual && + diff actual - <<-EOF +* sm1 $head2...$head3 (2): + < Add foo3 + < Add foo2 + +EOF +" + +head4=$(add_file sm1 foo4 foo5) && +head4_full=$(GIT_DIR=sm1/.git git rev-parse --verify HEAD) +test_expect_success 'modified submodule(backward and forward)' " + git submodule summary >actual && + diff actual - <<-EOF +* sm1 $head2...$head4 (4): + > Add foo5 + > Add foo4 + < Add foo3 + < Add foo2 + +EOF +" + +test_expect_success '--summary-limit' " + git submodule summary -n 3 >actual && + diff actual - <<-EOF +* sm1 $head2...$head4 (4): + > Add foo5 + > Add foo4 + < Add foo3 + +EOF +" + +commit_file sm1 && +mv sm1 sm1-bak && +echo sm1 >sm1 && +head5=$(git hash-object sm1 | cut -c1-7) && +git add sm1 && +rm -f sm1 && +mv sm1-bak sm1 + +test_expect_success 'typechanged submodule(submodule->blob), --cached' " + git submodule summary --cached >actual && + diff actual - <<-EOF +* sm1 $head4(submodule)->$head5(blob) (3): + < Add foo5 + +EOF +" + +rm -rf sm1 && +git checkout-index sm1 +test_expect_success 'typechanged submodule(submodule->blob)' " + git submodule summary >actual && + diff actual - <<-EOF +* sm1 $head4(submodule)->$head5(blob): + +EOF +" + +rm -f sm1 && +test_create_repo sm1 && +head6=$(add_file sm1 foo6 foo7) +test_expect_success 'nonexistent commit' " + git submodule summary >actual && + diff actual - <<-EOF +* sm1 $head4...$head6: + Warn: sm1 doesn't contain commit $head4_full + +EOF +" + +commit_file +test_expect_success 'typechanged submodule(blob->submodule)' " + git submodule summary >actual && + diff actual - <<-EOF +* sm1 $head5(blob)->$head6(submodule) (2): + > Add foo7 + +EOF +" + +commit_file sm1 && +rm -rf sm1 +test_expect_success 'deleted submodule' " + git submodule summary >actual && + diff actual - <<-EOF +* sm1 $head6...0000000: + +EOF +" + +test_create_repo sm2 && +head7=$(add_file sm2 foo8 foo9) && +git add sm2 + +test_expect_success 'multiple submodules' " + git submodule summary >actual && + diff actual - <<-EOF +* sm1 $head6...0000000: + +* sm2 0000000...$head7 (2): + > Add foo9 + +EOF +" + +test_expect_success 'path filter' " + git submodule summary sm2 >actual && + diff actual - <<-EOF +* sm2 0000000...$head7 (2): + > Add foo9 + +EOF +" + +commit_file sm2 +test_expect_success 'given commit' " + git submodule summary HEAD^ >actual && + diff actual - <<-EOF +* sm1 $head6...0000000: + +* sm2 0000000...$head7 (2): + > Add foo9 + +EOF +" + +test_done From eed35595759a65d51279547af0f2d017dd928fa5 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Wed, 12 Mar 2008 09:30:01 +0100 Subject: [PATCH 6/6] git-submodule summary: fix that some "wc" flavors produce leading spaces We print the number of commits in parentheses, but without this change we would get an oddly looking line like this: * sm1 4c8d358...41fbea9 ( 4): Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- git-submodule.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-submodule.sh b/git-submodule.sh index 4d28fc0988..558a5ca107 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -448,7 +448,7 @@ cmd_summary() { GIT_DIR="$name/.git" \ git log --pretty=oneline --first-parent $range | wc -l ) - total_commits=" ($total_commits)" + total_commits=" ($(($total_commits + 0)))" ;; esac