subtree: clean up, improve UX
PATCH v3 3/30 subtree: t7900: use test-lib.sh's test_count
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
Use test-lib.sh's test_count, instead instead of having t7900-subtree.sh do its own book-keeping with subtree_test_count that has to be explicitly incremented by calling next_test.
--- contrib/subtree/t/t7900-subtree.sh | 636 ++++++++++++++--------------- 1 file changed, 300 insertions(+), 336 deletions(-)
contrib/subtree/t/t7900-subtree.sh +300 -336
@@ -86,90 +86,79 @@ last_commit_message() git log --pretty=format:%s -1 } -subtree_test_count=0 -next_test() { - subtree_test_count=$(($subtree_test_count+1)) -} - # # Tests for 'git subtree add' # -next_test test_expect_success 'no merge from non-existent subtree' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && test_must_fail git subtree merge --prefix="sub dir" FETCH_HEAD ) ' -next_test test_expect_success 'no pull from non-existent subtree' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" HEAD )' -next_test test_expect_success 'add subproj as subtree into sub dir/ with --prefix' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD && check_equal "$(last_commit_message)" "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''" ) ' -next_test test_expect_success 'add subproj as subtree into sub dir/ with --prefix and --message' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" --message="Added subproject" FETCH_HEAD && check_equal "$(last_commit_message)" "Added subproject" ) ' -next_test test_expect_success 'add subproj as subtree into sub dir/ with --prefix as -P and --message as -m' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add -P "sub dir" -m "Added subproject" FETCH_HEAD && check_equal "$(last_commit_message)" "Added subproject" ) ' -next_test test_expect_success 'add subproj as subtree into sub dir/ with --squash and --prefix and --message' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" --message="Added subproject with squash" --squash FETCH_HEAD && check_equal "$(last_commit_message)" "Added subproject with squash"
@@ -180,74 +169,70 @@ test_expect_success 'add subproj as subtree into sub dir/ with --squash and --pr # Tests for 'git subtree merge' # -next_test test_expect_success 'merge new subproj history into sub dir/ with --prefix' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count/sub proj" sub2 && + test_create_commit "$test_count/sub proj" sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD && check_equal "$(last_commit_message)" "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''" ) ' -next_test test_expect_success 'merge new subproj history into sub dir/ with --prefix and --message' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count/sub proj" sub2 && + test_create_commit "$test_count/sub proj" sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" --message="Merged changes from subproject" FETCH_HEAD && check_equal "$(last_commit_message)" "Merged changes from subproject" ) ' -next_test test_expect_success 'merge new subproj history into sub dir/ with --squash and --prefix and --message' ' - subtree_test_create_repo "$subtree_test_count/sub proj" && - subtree_test_create_repo "$subtree_test_count" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count/sub proj" && + subtree_test_create_repo "$test_count" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count/sub proj" sub2 && + test_create_commit "$test_count/sub proj" sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" --message="Merged changes from subproject using squash" --squash FETCH_HEAD && check_equal "$(last_commit_message)" "Merged changes from subproject using squash" ) ' -next_test test_expect_success 'merge the added subproj again, should do nothing' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD && # this shouldn not actually do anything, since FETCH_HEAD
@@ -257,7 +242,6 @@ test_expect_success 'merge the added subproj again, should do nothing' ' ) ' -next_test test_expect_success 'merge new subproj history into subdir/ with a slash appended to the argument of --prefix' ' test_create_repo "$test_count" && test_create_repo "$test_count/subproj" &&
@@ -281,14 +265,13 @@ test_expect_success 'merge new subproj history into subdir/ with a slash appende # Tests for 'git subtree split' # -next_test test_expect_success 'split requires option --prefix' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD && echo "You must provide the --prefix option." > expected &&
@@ -301,14 +284,13 @@ test_expect_success 'split requires option --prefix' ' ) ' -next_test test_expect_success 'split requires path given by option --prefix must exist' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD && echo "'\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" > expected &&
@@ -321,23 +303,22 @@ test_expect_success 'split requires path given by option --prefix must exist' ' ) ' -next_test test_expect_success 'split sub dir/ with --rejoin' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && - test_create_commit "$subtree_test_count" main2 && - test_create_commit "$subtree_test_count/sub proj" sub2 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && + test_create_commit "$test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" main2 && + test_create_commit "$test_count/sub proj" sub2 && + test_create_commit "$test_count" "sub dir"/main-sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD && split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
@@ -346,12 +327,11 @@ test_expect_success 'split sub dir/ with --rejoin' ' ) ' -next_test test_expect_success 'split sub dir/ with --rejoin from scratch' ' - subtree_test_create_repo "$subtree_test_count" && - test_create_commit "$subtree_test_count" main1 && + subtree_test_create_repo "$test_count" && + test_create_commit "$test_count" main1 && ( - cd "$subtree_test_count" && + cd "$test_count" && mkdir "sub dir" && echo file >"sub dir"/file && git add "sub dir/file" &&
@@ -362,23 +342,22 @@ test_expect_success 'split sub dir/ with --rejoin from scratch' ' ) ' -next_test test_expect_success 'split sub dir/ with --rejoin and --message' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && - test_create_commit "$subtree_test_count" main2 && - test_create_commit "$subtree_test_count/sub proj" sub2 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && + test_create_commit "$test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" main2 && + test_create_commit "$test_count/sub proj" sub2 && + test_create_commit "$test_count" "sub dir"/main-sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD && git subtree split --prefix="sub dir" --message="Split & rejoin" --annotate="*" --rejoin &&
@@ -386,23 +365,22 @@ test_expect_success 'split sub dir/ with --rejoin and --message' ' ) ' -next_test test_expect_success 'split "sub dir"/ with --branch' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && - test_create_commit "$subtree_test_count" main2 && - test_create_commit "$subtree_test_count/sub proj" sub2 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && + test_create_commit "$test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" main2 && + test_create_commit "$test_count/sub proj" sub2 && + test_create_commit "$test_count" "sub dir"/main-sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD && split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
@@ -411,23 +389,22 @@ test_expect_success 'split "sub dir"/ with --branch' ' ) ' -next_test test_expect_success 'check hash of split' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && - test_create_commit "$subtree_test_count" main2 && - test_create_commit "$subtree_test_count/sub proj" sub2 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && + test_create_commit "$test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" main2 && + test_create_commit "$test_count/sub proj" sub2 && + test_create_commit "$test_count" "sub dir"/main-sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD && split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
@@ -443,24 +420,23 @@ test_expect_success 'check hash of split' ' ) ' -next_test test_expect_success 'split "sub dir"/ with --branch for an existing branch' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git branch subproj-br FETCH_HEAD && git subtree add --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && - test_create_commit "$subtree_test_count" main2 && - test_create_commit "$subtree_test_count/sub proj" sub2 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && + test_create_commit "$test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" main2 && + test_create_commit "$test_count/sub proj" sub2 && + test_create_commit "$test_count" "sub dir"/main-sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD && split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
@@ -469,24 +445,23 @@ test_expect_success 'split "sub dir"/ with --branch for an existing branch' ' ) ' -next_test test_expect_success 'split "sub dir"/ with --branch for an incompatible branch' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git branch init HEAD && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && - test_create_commit "$subtree_test_count" main2 && - test_create_commit "$subtree_test_count/sub proj" sub2 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && + test_create_commit "$test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" main2 && + test_create_commit "$test_count/sub proj" sub2 && + test_create_commit "$test_count" "sub dir"/main-sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD && test_must_fail git subtree split --prefix="sub dir" --branch init
@@ -497,46 +472,45 @@ test_expect_success 'split "sub dir"/ with --branch for an incompatible branch' # Validity checking # -next_test test_expect_success 'make sure exactly the right set of files ends up in the subproj' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && - test_create_commit "$subtree_test_count" main2 && - test_create_commit "$subtree_test_count/sub proj" sub2 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && + test_create_commit "$test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" main2 && + test_create_commit "$test_count/sub proj" sub2 && + test_create_commit "$test_count" "sub dir"/main-sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin ) && - test_create_commit "$subtree_test_count/sub proj" sub3 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub3 && + test_create_commit "$test_count/sub proj" sub3 && + test_create_commit "$test_count" "sub dir"/main-sub3 && ( - cd "$subtree_test_count/sub proj" && + cd "$test_count/sub proj" && git fetch .. subproj-br && git merge FETCH_HEAD ) && - test_create_commit "$subtree_test_count/sub proj" sub4 && + test_create_commit "$test_count/sub proj" sub4 && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub4 && + test_create_commit "$test_count" "sub dir"/main-sub4 && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin ) && ( - cd "$subtree_test_count/sub proj" && + cd "$test_count/sub proj" && git fetch .. subproj-br && git merge FETCH_HEAD &&
@@ -547,46 +521,45 @@ test_expect_success 'make sure exactly the right set of files ends up in the sub ) ' -next_test test_expect_success 'make sure the subproj *only* contains commits that affect the "sub dir"' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && - test_create_commit "$subtree_test_count" main2 && - test_create_commit "$subtree_test_count/sub proj" sub2 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && + test_create_commit "$test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" main2 && + test_create_commit "$test_count/sub proj" sub2 && + test_create_commit "$test_count" "sub dir"/main-sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin ) && - test_create_commit "$subtree_test_count/sub proj" sub3 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub3 && + test_create_commit "$test_count/sub proj" sub3 && + test_create_commit "$test_count" "sub dir"/main-sub3 && ( - cd "$subtree_test_count/sub proj" && + cd "$test_count/sub proj" && git fetch .. subproj-br && git merge FETCH_HEAD ) && - test_create_commit "$subtree_test_count/sub proj" sub4 && + test_create_commit "$test_count/sub proj" sub4 && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub4 && + test_create_commit "$test_count" "sub dir"/main-sub4 && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin ) && ( - cd "$subtree_test_count/sub proj" && + cd "$test_count/sub proj" && git fetch .. subproj-br && git merge FETCH_HEAD &&
@@ -598,51 +571,50 @@ test_expect_success 'make sure the subproj *only* contains commits that affect t ) ' -next_test test_expect_success 'make sure exactly the right set of files ends up in the mainline' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && - test_create_commit "$subtree_test_count" main2 && - test_create_commit "$subtree_test_count/sub proj" sub2 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && + test_create_commit "$test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" main2 && + test_create_commit "$test_count/sub proj" sub2 && + test_create_commit "$test_count" "sub dir"/main-sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin ) && - test_create_commit "$subtree_test_count/sub proj" sub3 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub3 && + test_create_commit "$test_count/sub proj" sub3 && + test_create_commit "$test_count" "sub dir"/main-sub3 && ( - cd "$subtree_test_count/sub proj" && + cd "$test_count/sub proj" && git fetch .. subproj-br && git merge FETCH_HEAD ) && - test_create_commit "$subtree_test_count/sub proj" sub4 && + test_create_commit "$test_count/sub proj" sub4 && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub4 && + test_create_commit "$test_count" "sub dir"/main-sub4 && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin ) && ( - cd "$subtree_test_count/sub proj" && + cd "$test_count/sub proj" && git fetch .. subproj-br && git merge FETCH_HEAD ) && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree pull --prefix="sub dir" ./"sub proj" HEAD && test_write_lines main1 main2 >chkm &&
@@ -657,52 +629,51 @@ test_expect_success 'make sure exactly the right set of files ends up in the mai ) ' -next_test test_expect_success 'make sure each filename changed exactly once in the entire history' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git config log.date relative && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && - test_create_commit "$subtree_test_count" main2 && - test_create_commit "$subtree_test_count/sub proj" sub2 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && + test_create_commit "$test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" main2 && + test_create_commit "$test_count/sub proj" sub2 && + test_create_commit "$test_count" "sub dir"/main-sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin ) && - test_create_commit "$subtree_test_count/sub proj" sub3 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub3 && + test_create_commit "$test_count/sub proj" sub3 && + test_create_commit "$test_count" "sub dir"/main-sub3 && ( - cd "$subtree_test_count/sub proj" && + cd "$test_count/sub proj" && git fetch .. subproj-br && git merge FETCH_HEAD ) && - test_create_commit "$subtree_test_count/sub proj" sub4 && + test_create_commit "$test_count/sub proj" sub4 && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub4 && + test_create_commit "$test_count" "sub dir"/main-sub4 && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin ) && ( - cd "$subtree_test_count/sub proj" && + cd "$test_count/sub proj" && git fetch .. subproj-br && git merge FETCH_HEAD ) && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree pull --prefix="sub dir" ./"sub proj" HEAD && test_write_lines main1 main2 >chkm &&
@@ -723,101 +694,99 @@ test_expect_success 'make sure each filename changed exactly once in the entire ) ' -next_test test_expect_success 'make sure the --rejoin commits never make it into subproj' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && - test_create_commit "$subtree_test_count" main2 && - test_create_commit "$subtree_test_count/sub proj" sub2 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && + test_create_commit "$test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" main2 && + test_create_commit "$test_count/sub proj" sub2 && + test_create_commit "$test_count" "sub dir"/main-sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin ) && - test_create_commit "$subtree_test_count/sub proj" sub3 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub3 && + test_create_commit "$test_count/sub proj" sub3 && + test_create_commit "$test_count" "sub dir"/main-sub3 && ( - cd "$subtree_test_count/sub proj" && + cd "$test_count/sub proj" && git fetch .. subproj-br && git merge FETCH_HEAD ) && - test_create_commit "$subtree_test_count/sub proj" sub4 && + test_create_commit "$test_count/sub proj" sub4 && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub4 && + test_create_commit "$test_count" "sub dir"/main-sub4 && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin ) && ( - cd "$subtree_test_count/sub proj" && + cd "$test_count/sub proj" && git fetch .. subproj-br && git merge FETCH_HEAD ) && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree pull --prefix="sub dir" ./"sub proj" HEAD && check_equal "$(git log --pretty=format:"%s" HEAD^2 | grep -i split)" "" ) ' -next_test test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && - test_create_commit "$subtree_test_count" main2 && - test_create_commit "$subtree_test_count/sub proj" sub2 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && + test_create_commit "$test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" main2 && + test_create_commit "$test_count/sub proj" sub2 && + test_create_commit "$test_count" "sub dir"/main-sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin ) && - test_create_commit "$subtree_test_count/sub proj" sub3 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub3 && + test_create_commit "$test_count/sub proj" sub3 && + test_create_commit "$test_count" "sub dir"/main-sub3 && ( - cd "$subtree_test_count/sub proj" && + cd "$test_count/sub proj" && git fetch .. subproj-br && git merge FETCH_HEAD ) && - test_create_commit "$subtree_test_count/sub proj" sub4 && + test_create_commit "$test_count/sub proj" sub4 && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub4 && + test_create_commit "$test_count" "sub dir"/main-sub4 && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin ) && ( - cd "$subtree_test_count/sub proj" && + cd "$test_count/sub proj" && git fetch .. subproj-br && git merge FETCH_HEAD ) && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree pull --prefix="sub dir" ./"sub proj" HEAD && # They are meaningless to subproj since one side of the merge refers to the mainline
@@ -829,27 +798,26 @@ test_expect_success 'make sure no "git subtree" tagged commits make it into subp # A new set of tests # -next_test test_expect_success 'make sure "git subtree split" find the correct parent' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count/sub proj" sub2 && + test_create_commit "$test_count/sub proj" sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git branch subproj-ref FETCH_HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" "sub dir"/main-sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree split --prefix="sub dir" --branch subproj-br && # at this point, the new commit parent should be subproj-ref, if it is
@@ -860,32 +828,31 @@ test_expect_success 'make sure "git subtree split" find the correct parent' ' ) ' -next_test test_expect_success 'split a new subtree without --onto option' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count/sub proj" sub2 && + test_create_commit "$test_count/sub proj" sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" "sub dir"/main-sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree split --prefix="sub dir" --branch subproj-br ) && - mkdir "$subtree_test_count"/"sub dir2" && - test_create_commit "$subtree_test_count" "sub dir2"/main-sub2 && + mkdir "$test_count"/"sub dir2" && + test_create_commit "$test_count" "sub dir2"/main-sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && # also test that we still can split out an entirely new subtree # if the parent of the first commit in the tree is not empty,
@@ -895,33 +862,32 @@ test_expect_success 'split a new subtree without --onto option' ' ) ' -next_test test_expect_success 'verify one file change per commit' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git branch sub1 FETCH_HEAD && git subtree add --prefix="sub dir" sub1 ) && - test_create_commit "$subtree_test_count/sub proj" sub2 && + test_create_commit "$test_count/sub proj" sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" "sub dir"/main-sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree split --prefix="sub dir" --branch subproj-br ) && - mkdir "$subtree_test_count"/"sub dir2" && - test_create_commit "$subtree_test_count" "sub dir2"/main-sub2 && + mkdir "$test_count"/"sub dir2" && + test_create_commit "$test_count" "sub dir2"/main-sub2 && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree split --prefix="sub dir2" --branch subproj2-br && x= &&
@@ -939,31 +905,30 @@ test_expect_success 'verify one file change per commit' ' ) ' -next_test test_expect_success 'push split to subproj' ' - subtree_test_create_repo "$subtree_test_count" && - subtree_test_create_repo "$subtree_test_count/sub proj" && - test_create_commit "$subtree_test_count" main1 && - test_create_commit "$subtree_test_count/sub proj" sub1 && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && ( - cd "$subtree_test_count" && + cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && - test_create_commit "$subtree_test_count" main2 && - test_create_commit "$subtree_test_count/sub proj" sub2 && - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && + test_create_commit "$test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" main2 && + test_create_commit "$test_count/sub proj" sub2 && + test_create_commit "$test_count" "sub dir"/main-sub2 && ( - cd $subtree_test_count/"sub proj" && + cd $test_count/"sub proj" && git branch sub-branch-1 && cd .. && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD ) && - test_create_commit "$subtree_test_count" "sub dir"/main-sub3 && + test_create_commit "$test_count" "sub dir"/main-sub3 && ( - cd "$subtree_test_count" && + cd "$test_count" && git subtree push ./"sub proj" --prefix "sub dir" sub-branch-1 && cd ./"sub proj" && git checkout sub-branch-1 &&
@@ -991,38 +956,37 @@ test_expect_success 'push split to subproj' ' # set of commits. # -next_test test_expect_success 'subtree descendant check' ' - subtree_test_create_repo "$subtree_test_count" && - defaultBranch=$(sed "s,ref: refs/heads/,," "$subtree_test_count/.git/HEAD") && - test_create_commit "$subtree_test_count" folder_subtree/a && + subtree_test_create_repo "$test_count" && + defaultBranch=$(sed "s,ref: refs/heads/,," "$test_count/.git/HEAD") && + test_create_commit "$test_count" folder_subtree/a && ( - cd "$subtree_test_count" && + cd "$test_count" && git branch branch ) && - test_create_commit "$subtree_test_count" folder_subtree/0 && - test_create_commit "$subtree_test_count" folder_subtree/b && - cherry=$(cd "$subtree_test_count"; git rev-parse HEAD) && + test_create_commit "$test_count" folder_subtree/0 && + test_create_commit "$test_count" folder_subtree/b && + cherry=$(cd "$test_count"; git rev-parse HEAD) && ( - cd "$subtree_test_count" && + cd "$test_count" && git checkout branch ) && - test_create_commit "$subtree_test_count" commit_on_branch && + test_create_commit "$test_count" commit_on_branch && ( - cd "$subtree_test_count" && + cd "$test_count" && git cherry-pick $cherry && git checkout $defaultBranch && git merge -m "merge should be kept on subtree" branch && git branch no_subtree_work_branch ) && - test_create_commit "$subtree_test_count" folder_subtree/d && + test_create_commit "$test_count" folder_subtree/d && ( - cd "$subtree_test_count" && + cd "$test_count" && git checkout no_subtree_work_branch ) && - test_create_commit "$subtree_test_count" not_a_subtree_change && + test_create_commit "$test_count" not_a_subtree_change && ( - cd "$subtree_test_count" && + cd "$test_count" && git checkout $defaultBranch && git merge -m "merge should be skipped on subtree" no_subtree_work_branch &&
Signature · 1 line
2.31.1
Re: [PATCH v3 03/30] subtree: t7900: use test-lib.sh's test_count
Quoted 5 lines · Luke Shumaker
From: Luke Shumaker <[email protected]>
Use test-lib.sh's
test_count, instead instead of havingt7900-subtree.shdo its own book-keeping withsubtree_test_countthat has to be explicitly incremented by callingnext_test.
This just retains an existing pattern and all that, but I think this pattern of testing makes for hard to read & maintain tests. It looks like all of these could just be:
test_when_finished "rm -rf repo" &&
git init repo &&
[...]I.e. the only thing that's being done here is to piggy-back on the test count to create a unique repo.
Re: [PATCH v3 03/30] subtree: t7900: use test-lib.sh's test_count
Quoted 9 lines · Ævar Arnfjörð Bjarmason
Quoted 3 lines · Luke Shumaker
Use test-lib.sh's
test_count, instead instead of havingt7900-subtree.shdo its own book-keeping withsubtree_test_countthat has to be explicitly incremented by callingnext_test.This just retains an existing pattern and all that, but I think this pattern of testing makes for hard to read & maintain tests. It looks like all of these could just be:
Quoted 6 lines
test_when_finished "rm -rf repo" && git init repo && [...]I.e. the only thing that's being done here is to piggy-back on the test count to create a unique repo.
Thanks for the tip, I'll do that for any new tests I add, going forward.
Signature · 2 lines
Happy hacking, ~ Luke Shumaker
PATCH v3 4/30 subtree: t7900: use consistent formatting
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
The formatting in t7900-subtree.sh isn't even consistent throughout the file. Fix that; make it consistent throughout the file.
---
v2:
- Also normalize on test X instead of [ X ].
- Also normalize on >FILENAME with no spaces.
- Fix a few if statements with if and then on the same line that I missed in v1.
v3:
- Consistently put the do of a loop on its own line.
contrib/subtree/t/t7900-subtree.sh | 70 +++++++++++++++--------------- 1 file changed, 35 insertions(+), 35 deletions(-)
contrib/subtree/t/t7900-subtree.sh +35 -35
@@ -11,11 +11,9 @@ and split subcommands of git subtree. TEST_DIRECTORY=$(pwd)/../../../t export TEST_DIRECTORY +. "$TEST_DIRECTORY"/test-lib.sh -. ../../../t/test-lib.sh - -subtree_test_create_repo() -{ +subtree_test_create_repo () { test_create_repo "$1" && ( cd "$1" &&
@@ -23,26 +21,24 @@ subtree_test_create_repo() ) } -create() -{ +create () { echo "$1" >"$1" && git add "$1" } -check_equal() -{ +check_equal () { test_debug 'echo' test_debug "echo \"check a:\" \"{$1}\"" test_debug "echo \" b:\" \"{$2}\"" - if [ "$1" = "$2" ]; then + if test "$1" = "$2" + then return 0 else return 1 fi } -undo() -{ +undo () { git reset --hard HEAD~ }
@@ -50,15 +46,18 @@ undo() # The original set of commits changed only one file each. # A multi-file change would imply that we pruned commits # too aggressively. -join_commits() -{ +join_commits () { commit= all= - while read x y; do - if [ -z "$x" ]; then + while read x y + do + if test -z "$x" + then continue - elif [ "$x" = "commit:" ]; then - if [ -n "$commit" ]; then + elif test "$x" = "commit:" + then + if test -n "$commit" + then echo "$commit $all" all= fi
@@ -70,7 +69,7 @@ join_commits() echo "$commit $all" } -test_create_commit() ( +test_create_commit () ( repo=$1 && commit=$2 && cd "$repo" &&
@@ -81,8 +80,7 @@ test_create_commit() ( git commit -m "$commit" || error "Could not commit" ) -last_commit_message() -{ +last_commit_message () { git log --pretty=format:%s -1 }
@@ -111,7 +109,8 @@ test_expect_success 'no pull from non-existent subtree' ' cd "$test_count" && git fetch ./"sub proj" HEAD && test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" HEAD - )' + ) +' test_expect_success 'add subproj as subtree into sub dir/ with --prefix' ' subtree_test_create_repo "$test_count" &&
@@ -274,8 +273,8 @@ test_expect_success 'split requires option --prefix' ' cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD && - echo "You must provide the --prefix option." > expected && - test_must_fail git subtree split > actual 2>&1 && + echo "You must provide the --prefix option." >expected && + test_must_fail git subtree split >actual 2>&1 && test_debug "printf '"expected: "'" && test_debug "cat expected" && test_debug "printf '"actual: "'" &&
@@ -293,8 +292,8 @@ test_expect_success 'split requires path given by option --prefix must exist' ' cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD && - echo "'\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" > expected && - test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 && + echo "'\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" >expected && + test_must_fail git subtree split --prefix=non-existent-directory >actual 2>&1 && test_debug "printf '"expected: "'" && test_debug "cat expected" && test_debug "printf '"actual: "'" &&
@@ -325,7 +324,7 @@ test_expect_success 'split sub dir/ with --rejoin' ' git subtree split --prefix="sub dir" --annotate="*" --rejoin && check_equal "$(last_commit_message)" "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" ) - ' +' test_expect_success 'split sub dir/ with --rejoin from scratch' ' subtree_test_create_repo "$test_count" &&
@@ -340,7 +339,7 @@ test_expect_success 'split sub dir/ with --rejoin from scratch' ' git subtree split --prefix="sub dir" --rejoin && check_equal "$(last_commit_message)" "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" ) - ' +' test_expect_success 'split sub dir/ with --rejoin and --message' ' subtree_test_create_repo "$test_count" &&
@@ -893,7 +892,8 @@ test_expect_success 'verify one file change per commit' ' x= && git log --pretty=format:"commit: %H" | join_commits | ( - while read commit a b; do + while read commit a b + do test_debug "echo Verifying commit $commit" test_debug "echo a: $a" test_debug "echo b: $b"
@@ -921,18 +921,18 @@ test_expect_success 'push split to subproj' ' test_create_commit "$test_count" "sub dir"/main-sub2 && ( cd $test_count/"sub proj" && - git branch sub-branch-1 && - cd .. && + git branch sub-branch-1 && + cd .. && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD ) && test_create_commit "$test_count" "sub dir"/main-sub3 && - ( + ( cd "$test_count" && - git subtree push ./"sub proj" --prefix "sub dir" sub-branch-1 && - cd ./"sub proj" && - git checkout sub-branch-1 && - check_equal "$(last_commit_message)" "sub dir/main-sub3" + git subtree push ./"sub proj" --prefix "sub dir" sub-branch-1 && + cd ./"sub proj" && + git checkout sub-branch-1 && + check_equal "$(last_commit_message)" "sub dir/main-sub3" ) '
Signature · 1 line
2.31.1
PATCH v3 5/30 subtree: t7900: comment subtree_test_create_repo
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
It's unclear what the purpose of t7900-subtree.sh's subtree_test_create_repo helper function is. It wraps test-lib.sh's, test_create_repo but follows that up by setting log.date=relative. Why does it set log.date=relative?
My first guess was that at one point the tests required that, but no longer do, and that the function is now vestigial. I even wrote a patch to get rid of it and was moments away from git send-emailing it.
However, by chance when looking for something else in the history, I discovered the true reason, from e7aac44ed2 (contrib/subtree: ignore log.date configuration, 2015-07-21). It's testing that setting log.date=relative doesn't break git subtree, as at one point in the past that did break git subtree.
So, add a comment about this, to avoid future such confusion.
And while at it, go ahead and (1) touch up the function to avoid a pointless subshell and (2) update the one test that didn't use it.
---
v2:
- Also switch the few uses of vanilla test_create_repo over to subtree_test_create_repo. In v1 this was erroneously included in a different commit.
contrib/subtree/t/t7900-subtree.sh | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
contrib/subtree/t/t7900-subtree.sh +8 -6
@@ -13,12 +13,14 @@ TEST_DIRECTORY=$(pwd)/../../../t export TEST_DIRECTORY . "$TEST_DIRECTORY"/test-lib.sh +# Use our own wrapper around test-lib.sh's test_create_repo, in order +# to set log.date=relative. `git subtree` parses the output of `git +# log`, and so it must be careful to not be affected by settings that +# change the `git log` output. We test this by setting +# log.date=relative for every repo in the tests. subtree_test_create_repo () { test_create_repo "$1" && - ( - cd "$1" && - git config log.date relative - ) + git -C "$1" config log.date relative } create () {
@@ -242,8 +244,8 @@ test_expect_success 'merge the added subproj again, should do nothing' ' ' test_expect_success 'merge new subproj history into subdir/ with a slash appended to the argument of --prefix' ' - test_create_repo "$test_count" && - test_create_repo "$test_count/subproj" && + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/subproj" && test_create_commit "$test_count" main1 && test_create_commit "$test_count/subproj" sub1 && (
Signature · 1 line
2.31.1
Re: [PATCH v3 05/30] subtree: t7900: comment subtree_test_create_repo
Quoted 9 lines · Luke Shumaker
It's unclear what the purpose of t7900-subtree.sh's
subtree_test_create_repohelper function is. It wraps test-lib.sh's,test_create_repobut follows that up by setting log.date=relative. Why does it set log.date=relative?My first guess was that at one point the tests required that, but no longer do, and that the function is now vestigial. I even wrote a patch to get rid of it and was moments away from
git send-emailing it.
Quoted 5 lines
However, by chance when looking for something else in the history, I discovered the true reason, from
e7aac44ed2(contrib/subtree: ignore log.date configuration, 2015-07-21). It's testing that setting log.date=relative doesn't breakgit subtree, as at one point in the past that did breakgit subtree.
This commit message is too much about describing the journey IMO. We could just get straight to the point, e.g. something like:
Add a comment about why the git-subtree.sh tests set log.relative. It's to test that we don't have a regression of the bug fixed in e7aac44ed2 [...].
FWIW I think just having an isolated test for this would be better, but since you're just refactoring existing code I think this is fine.
I.e. we'd use "git init" in the rest, and just have a test that stresses this specifically. Or is the entry into git-subtree.sh so varied that we need to "fuzz" all the tests like this to fully test it? I haven't checked.
Re: [PATCH v3 05/30] subtree: t7900: comment subtree_test_create_repo
Quoted 16 lines · Ævar Arnfjörð Bjarmason
Quoted 9 lines · Luke Shumaker
It's unclear what the purpose of t7900-subtree.sh's
subtree_test_create_repohelper function is. It wraps test-lib.sh's,test_create_repobut follows that up by setting log.date=relative. Why does it set log.date=relative?My first guess was that at one point the tests required that, but no longer do, and that the function is now vestigial. I even wrote a patch to get rid of it and was moments away from
git send-emailing it.Quoted 5 lines
However, by chance when looking for something else in the history, I discovered the true reason, from
e7aac44ed2(contrib/subtree: ignore log.date configuration, 2015-07-21). It's testing that setting log.date=relative doesn't breakgit subtree, as at one point in the past that did breakgit subtree.
Quoted 6 lines
This commit message is too much about describing the journey IMO. We could just get straight to the point, e.g. something like:
Add a comment about why the
git-subtree.shtests set log.relative. It's to test that we don't have a regression of the bug fixed ine7aac44ed2[...].
OK, I'll reword it if I re-roll.
Quoted 7 lines
FWIW I think just having an isolated test for this would be better, but since you're just refactoring existing code I think this is fine.
I.e. we'd use "git init" in the rest, and just have a test that stresses this specifically. Or is the entry into
git-subtree.shso varied that we need to "fuzz" all the tests like this to fully test it? I haven't checked.
I'd have to examine it a bit more to be sure, but I think that you're right. That as long as the test does a 'split' that creates new commits, then just one test should be fine.
Signature · 2 lines
Happy hacking, ~ Luke Shumaker
PATCH v3 6/30 subtree: t7900: use 'test' for string equality
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
t7900-subtree.sh defines its own check_equal A B function, instead of just using test A = B like all of the other tests. Don't be special, get rid of check_equal in favor of test.
--- contrib/subtree/t/t7900-subtree.sh | 60 ++++++++++++------------------ 1 file changed, 24 insertions(+), 36 deletions(-)
contrib/subtree/t/t7900-subtree.sh +24 -36
@@ -28,18 +28,6 @@ create () { git add "$1" } -check_equal () { - test_debug 'echo' - test_debug "echo \"check a:\" \"{$1}\"" - test_debug "echo \" b:\" \"{$2}\"" - if test "$1" = "$2" - then - return 0 - else - return 1 - fi -} - undo () { git reset --hard HEAD~ }
@@ -123,7 +111,7 @@ test_expect_success 'add subproj as subtree into sub dir/ with --prefix' ' cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD && - check_equal "$(last_commit_message)" "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''" + test "$(last_commit_message)" = "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''" ) '
@@ -136,7 +124,7 @@ test_expect_success 'add subproj as subtree into sub dir/ with --prefix and --me cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" --message="Added subproject" FETCH_HEAD && - check_equal "$(last_commit_message)" "Added subproject" + test "$(last_commit_message)" = "Added subproject" ) '
@@ -149,7 +137,7 @@ test_expect_success 'add subproj as subtree into sub dir/ with --prefix as -P an cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add -P "sub dir" -m "Added subproject" FETCH_HEAD && - check_equal "$(last_commit_message)" "Added subproject" + test "$(last_commit_message)" = "Added subproject" ) '
@@ -162,7 +150,7 @@ test_expect_success 'add subproj as subtree into sub dir/ with --squash and --pr cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" --message="Added subproject with squash" --squash FETCH_HEAD && - check_equal "$(last_commit_message)" "Added subproject with squash" + test "$(last_commit_message)" = "Added subproject with squash" ) '
@@ -185,7 +173,7 @@ test_expect_success 'merge new subproj history into sub dir/ with --prefix' ' cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD && - check_equal "$(last_commit_message)" "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''" + test "$(last_commit_message)" = "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''" ) '
@@ -204,7 +192,7 @@ test_expect_success 'merge new subproj history into sub dir/ with --prefix and - cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" --message="Merged changes from subproject" FETCH_HEAD && - check_equal "$(last_commit_message)" "Merged changes from subproject" + test "$(last_commit_message)" = "Merged changes from subproject" ) '
@@ -223,7 +211,7 @@ test_expect_success 'merge new subproj history into sub dir/ with --squash and - cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" --message="Merged changes from subproject using squash" --squash FETCH_HEAD && - check_equal "$(last_commit_message)" "Merged changes from subproject using squash" + test "$(last_commit_message)" = "Merged changes from subproject using squash" ) '
@@ -239,7 +227,7 @@ test_expect_success 'merge the added subproj again, should do nothing' ' # this shouldn not actually do anything, since FETCH_HEAD # is already a parent result=$(git merge -s ours -m "merge -s -ours" FETCH_HEAD) && - check_equal "${result}" "Already up to date." + test "${result}" = "Already up to date." ) '
@@ -258,7 +246,7 @@ test_expect_success 'merge new subproj history into subdir/ with a slash appende cd "$test_count" && git fetch ./subproj HEAD && git subtree merge --prefix=subdir/ FETCH_HEAD && - check_equal "$(last_commit_message)" "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''" + test "$(last_commit_message)" = "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''" ) '
@@ -324,7 +312,7 @@ test_expect_success 'split sub dir/ with --rejoin' ' git subtree merge --prefix="sub dir" FETCH_HEAD && split_hash=$(git subtree split --prefix="sub dir" --annotate="*") && git subtree split --prefix="sub dir" --annotate="*" --rejoin && - check_equal "$(last_commit_message)" "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" + test "$(last_commit_message)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" ) '
@@ -339,7 +327,7 @@ test_expect_success 'split sub dir/ with --rejoin from scratch' ' git commit -m"sub dir file" && split_hash=$(git subtree split --prefix="sub dir" --rejoin) && git subtree split --prefix="sub dir" --rejoin && - check_equal "$(last_commit_message)" "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" + test "$(last_commit_message)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" ) '
@@ -362,7 +350,7 @@ test_expect_success 'split sub dir/ with --rejoin and --message' ' git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD && git subtree split --prefix="sub dir" --message="Split & rejoin" --annotate="*" --rejoin && - check_equal "$(last_commit_message)" "Split & rejoin" + test "$(last_commit_message)" = "Split & rejoin" ) '
@@ -386,7 +374,7 @@ test_expect_success 'split "sub dir"/ with --branch' ' git subtree merge --prefix="sub dir" FETCH_HEAD && split_hash=$(git subtree split --prefix="sub dir" --annotate="*") && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br && - check_equal "$(git rev-parse subproj-br)" "$split_hash" + test "$(git rev-parse subproj-br)" = "$split_hash" ) '
@@ -410,13 +398,13 @@ test_expect_success 'check hash of split' ' git subtree merge --prefix="sub dir" FETCH_HEAD && split_hash=$(git subtree split --prefix="sub dir" --annotate="*") && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br && - check_equal "$(git rev-parse subproj-br)" "$split_hash" && + test "$(git rev-parse subproj-br)" = "$split_hash" && # Check hash of split new_hash=$(git rev-parse subproj-br^2) && ( cd ./"sub proj" && subdir_hash=$(git rev-parse HEAD) && - check_equal ''"$new_hash"'' "$subdir_hash" + test ''"$new_hash"'' = "$subdir_hash" ) ) '
@@ -442,7 +430,7 @@ test_expect_success 'split "sub dir"/ with --branch for an existing branch' ' git subtree merge --prefix="sub dir" FETCH_HEAD && split_hash=$(git subtree split --prefix="sub dir" --annotate="*") && git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br && - check_equal "$(git rev-parse subproj-br)" "$split_hash" + test "$(git rev-parse subproj-br)" = "$split_hash" ) '
@@ -740,7 +728,7 @@ test_expect_success 'make sure the --rejoin commits never make it into subproj' ( cd "$test_count" && git subtree pull --prefix="sub dir" ./"sub proj" HEAD && - check_equal "$(git log --pretty=format:"%s" HEAD^2 | grep -i split)" "" + test "$(git log --pretty=format:"%s" HEAD^2 | grep -i split)" = "" ) '
@@ -791,7 +779,7 @@ test_expect_success 'make sure no "git subtree" tagged commits make it into subp git subtree pull --prefix="sub dir" ./"sub proj" HEAD && # They are meaningless to subproj since one side of the merge refers to the mainline - check_equal "$(git log --pretty=format:"%s%n%b" HEAD^2 | grep "git-subtree.*:")" "" + test "$(git log --pretty=format:"%s%n%b" HEAD^2 | grep "git-subtree.*:")" = "" ) '
@@ -825,7 +813,7 @@ test_expect_success 'make sure "git subtree split" find the correct parent' ' # not, something went wrong (the "newparent" of "HEAD~" commit should # have been sub2, but it was not, because its cache was not set to # itself) - check_equal "$(git log --pretty=format:%P -1 subproj-br)" "$(git rev-parse subproj-ref)" + test "$(git log --pretty=format:%P -1 subproj-br)" = "$(git rev-parse subproj-ref)" ) '
@@ -859,7 +847,7 @@ test_expect_success 'split a new subtree without --onto option' ' # if the parent of the first commit in the tree is not empty, # then the new subtree has accidentally been attached to something git subtree split --prefix="sub dir2" --branch subproj2-br && - check_equal "$(git log --pretty=format:%P -1 subproj2-br)" "" + test "$(git log --pretty=format:%P -1 subproj2-br)" = "" ) '
@@ -899,10 +887,10 @@ test_expect_success 'verify one file change per commit' ' test_debug "echo Verifying commit $commit" test_debug "echo a: $a" test_debug "echo b: $b" - check_equal "$b" "" + test "$b" = "" x=1 done - check_equal "$x" 1 + test "$x" = 1 ) ) '
@@ -934,7 +922,7 @@ test_expect_success 'push split to subproj' ' git subtree push ./"sub proj" --prefix "sub dir" sub-branch-1 && cd ./"sub proj" && git checkout sub-branch-1 && - check_equal "$(last_commit_message)" "sub dir/main-sub3" + test "$(last_commit_message)" = "sub dir/main-sub3" ) '
@@ -994,7 +982,7 @@ test_expect_success 'subtree descendant check' ' git subtree split --prefix folder_subtree/ --branch subtree_tip $defaultBranch && git subtree split --prefix folder_subtree/ --branch subtree_branch branch && - check_equal $(git rev-list --count subtree_tip..subtree_branch) 0 + test $(git rev-list --count subtree_tip..subtree_branch) = 0 ) '
Signature · 1 line
2.31.1
Re: [PATCH v3 06/30] subtree: t7900: use 'test' for string equality
Quoted 20 lines · Luke Shumaker
From: Luke Shumaker <[email protected]>
t7900-subtree.shdefines its owncheck_equal A Bfunction, instead of just usingtest A = Blike all of the other tests. Don't be special, get rid ofcheck_equalin favor oftest.Signed-off-by Luke Shumaker <[email protected]>--- contrib/subtree/t/t7900-subtree.sh | 60 ++++++++++++------------------ 1 file changed, 24 insertions(+), 36 deletions(-)contrib/subtree/t/t7900-subtree.sh +0
-1@@ -28,18 +28,6 @@ create () { git add "$1" } -check_equal () {
contrib/subtree/t/t7900-subtree.sh create () { · line 41
Quoted 10 lines
contrib/subtree/t/t7900-subtree.sh +0
-10- test_debug 'echo' - test_debug "echo \"check a:\" \"{$1}\"" - test_debug "echo \" b:\" \"{$2}\"" - if test "$1" = "$2" - then - return 0 - else - return 1 - fi -}
It looks like the reason this was used because when this fails just having the "test" makes for bad debugging. I.e. if the values don't match the $1 and $2 are not aligned, so it's hard to eyeball what went wrong.
These days this is more idiomatic:
echo "Add [...]" >expected
last_commit_message >actual &&
test_cmp expected actualSo I think in this case a better narrower improvement would be to keep the check_equal function. I wonder if we shouldn't just in general in t/test-lib.sh have a test_cmp_str for this use-case. I.e. a trivial wrapper that echos the two strings to a file for you, before running diff(1).
Re: [PATCH v3 06/30] subtree: t7900: use 'test' for string equality
Quoted 23 lines · Ævar Arnfjörð Bjarmason
Quoted 19 lines · Luke Shumaker
From: Luke Shumaker <[email protected]>
t7900-subtree.shdefines its owncheck_equal A Bfunction, instead of just usingtest A = Blike all of the other tests. Don't be special, get rid ofcheck_equalin favor oftest.Signed-off-by Luke Shumaker <[email protected]>--- contrib/subtree/t/t7900-subtree.sh | 60 ++++++++++++------------------ 1 file changed, 24 insertions(+), 36 deletions(-)contrib/subtree/t/t7900-subtree.sh +0
-0@@ -28,18 +28,6 @@ create () { git add "$1" }
Quoted 16 lines
contrib/subtree/t/t7900-subtree.sh create () { · line 41
Quoted 11 lines
contrib/subtree/t/t7900-subtree.sh +0
-11-check_equal () { - test_debug 'echo' - test_debug "echo \"check a:\" \"{$1}\"" - test_debug "echo \" b:\" \"{$2}\"" - if test "$1" = "$2" - then - return 0 - else - return 1 - fi -}It looks like the reason this was used because when this fails just having the "test" makes for bad debugging. I.e. if the values don't match the $1 and $2 are not aligned, so it's hard to eyeball what went wrong.
It's easy to make that assumption, but looking at the history it seems the "actual" reason it exists is that it's vestigial from before the subtree tests used test-lib.sh, and echoing it like that was the only way you'd get feedback.
Quoted 11 lines
These days this is more idiomatic:
echo "Add [...]" >expected last_commit_message >actual && test_cmp expected actualSo I think in this case a better narrower improvement would be to keep the
check_equalfunction. I wonder if we shouldn't just in general int/test-lib.shhave atest_cmp_strfor this use-case. I.e. a trivial wrapper that echos the two strings to a file for you, before running diff(1).
But it's been my experience that the tests are already impossible to debug without passing -x, so everything from check_equal ends up being just noise.
And also I figured if test is good enough for t9350-fast-export.sh, then it's good enough for t7900-subtree.sh... after working with subtree, I'd accidentally written a couple of the checks in one of my fast-export patchsets using check_equal. Being special makes things harder to hack on.
Signature · 2 lines
Happy hacking, ~ Luke Shumaker
PATCH v3 7/30 subtree: t7900: delete some dead code
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
--- contrib/subtree/t/t7900-subtree.sh | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-)
contrib/subtree/t/t7900-subtree.sh +1 -11
@@ -10,7 +10,6 @@ and split subcommands of git subtree. ' TEST_DIRECTORY=$(pwd)/../../../t -export TEST_DIRECTORY . "$TEST_DIRECTORY"/test-lib.sh # Use our own wrapper around test-lib.sh's test_create_repo, in order
@@ -23,15 +22,6 @@ subtree_test_create_repo () { git -C "$1" config log.date relative } -create () { - echo "$1" >"$1" && - git add "$1" -} - -undo () { - git reset --hard HEAD~ -} - # Make sure no patch changes more than one file. # The original set of commits changed only one file each. # A multi-file change would imply that we pruned commits
@@ -404,7 +394,7 @@ test_expect_success 'check hash of split' ' ( cd ./"sub proj" && subdir_hash=$(git rev-parse HEAD) && - test ''"$new_hash"'' = "$subdir_hash" + test "$new_hash" = "$subdir_hash" ) ) '
Signature · 1 line
2.31.1
PATCH v3 8/30 subtree: t7900: fix 'verify one file change per commit'
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
As far as I can tell, this test isn't actually testing anything, because someone forgot to tack on --name-only to git log. This seems to have been the case since the test was first written, back in fa16ab36ad ("test.sh: make sure no commit changes more than one file at a time.", 2009-04-26), unless git log used to do that by default and didn't need the flag back then?
Convincing myself that it's not actually testing anything was tricky, the code is a little hard to reason about. It can be made a lot simpler if instead of trying to parse all of the info from a single git log, we're OK calling git log from inside of a loop. And it's my opinion that tests are not the place for clever optimized code.
So, fix and simplify the test, so that it's actually testing something and is simpler to reason about.
--- contrib/subtree/t/t7900-subtree.sh | 46 ++++-------------------------- 1 file changed, 6 insertions(+), 40 deletions(-)
contrib/subtree/t/t7900-subtree.sh +6 -40
@@ -22,33 +22,6 @@ subtree_test_create_repo () { git -C "$1" config log.date relative } -# Make sure no patch changes more than one file. -# The original set of commits changed only one file each. -# A multi-file change would imply that we pruned commits -# too aggressively. -join_commits () { - commit= - all= - while read x y - do - if test -z "$x" - then - continue - elif test "$x" = "commit:" - then - if test -n "$commit" - then - echo "$commit $all" - all= - fi - commit="$y" - else - all="$all $y" - fi - done - echo "$commit $all" -} - test_create_commit () ( repo=$1 && commit=$2 &&
@@ -869,19 +842,12 @@ test_expect_success 'verify one file change per commit' ' cd "$test_count" && git subtree split --prefix="sub dir2" --branch subproj2-br && - x= && - git log --pretty=format:"commit: %H" | join_commits | - ( - while read commit a b - do - test_debug "echo Verifying commit $commit" - test_debug "echo a: $a" - test_debug "echo b: $b" - test "$b" = "" - x=1 - done - test "$x" = 1 - ) + git log --format="%H" >commit-list && + while read commit + do + git log -n1 --format="" --name-only "$commit" >file-list && + test_line_count -le 1 file-list || return 1 + done <commit-list ) '
Signature · 1 line
2.31.1
PATCH v3 9/30 subtree: t7900: rename last_commit_message to last_commit_subject
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
t7900-subtree.sh defines a helper function named last_commit_message. However, it only returns the subject line of the commit message, not the entire commit message. So rename it, to make the name less confusing.
--- contrib/subtree/t/t7900-subtree.sh | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-)
contrib/subtree/t/t7900-subtree.sh +13 -13
@@ -33,7 +33,7 @@ test_create_commit () ( git commit -m "$commit" || error "Could not commit" ) -last_commit_message () { +last_commit_subject () { git log --pretty=format:%s -1 }
@@ -74,7 +74,7 @@ test_expect_success 'add subproj as subtree into sub dir/ with --prefix' ' cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" FETCH_HEAD && - test "$(last_commit_message)" = "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''" + test "$(last_commit_subject)" = "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''" ) '
@@ -87,7 +87,7 @@ test_expect_success 'add subproj as subtree into sub dir/ with --prefix and --me cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" --message="Added subproject" FETCH_HEAD && - test "$(last_commit_message)" = "Added subproject" + test "$(last_commit_subject)" = "Added subproject" ) '
@@ -100,7 +100,7 @@ test_expect_success 'add subproj as subtree into sub dir/ with --prefix as -P an cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add -P "sub dir" -m "Added subproject" FETCH_HEAD && - test "$(last_commit_message)" = "Added subproject" + test "$(last_commit_subject)" = "Added subproject" ) '
@@ -113,7 +113,7 @@ test_expect_success 'add subproj as subtree into sub dir/ with --squash and --pr cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree add --prefix="sub dir" --message="Added subproject with squash" --squash FETCH_HEAD && - test "$(last_commit_message)" = "Added subproject with squash" + test "$(last_commit_subject)" = "Added subproject with squash" ) '
@@ -136,7 +136,7 @@ test_expect_success 'merge new subproj history into sub dir/ with --prefix' ' cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD && - test "$(last_commit_message)" = "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''" + test "$(last_commit_subject)" = "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''" ) '
@@ -155,7 +155,7 @@ test_expect_success 'merge new subproj history into sub dir/ with --prefix and - cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" --message="Merged changes from subproject" FETCH_HEAD && - test "$(last_commit_message)" = "Merged changes from subproject" + test "$(last_commit_subject)" = "Merged changes from subproject" ) '
@@ -174,7 +174,7 @@ test_expect_success 'merge new subproj history into sub dir/ with --squash and - cd "$test_count" && git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" --message="Merged changes from subproject using squash" --squash FETCH_HEAD && - test "$(last_commit_message)" = "Merged changes from subproject using squash" + test "$(last_commit_subject)" = "Merged changes from subproject using squash" ) '
@@ -209,7 +209,7 @@ test_expect_success 'merge new subproj history into subdir/ with a slash appende cd "$test_count" && git fetch ./subproj HEAD && git subtree merge --prefix=subdir/ FETCH_HEAD && - test "$(last_commit_message)" = "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''" + test "$(last_commit_subject)" = "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''" ) '
@@ -275,7 +275,7 @@ test_expect_success 'split sub dir/ with --rejoin' ' git subtree merge --prefix="sub dir" FETCH_HEAD && split_hash=$(git subtree split --prefix="sub dir" --annotate="*") && git subtree split --prefix="sub dir" --annotate="*" --rejoin && - test "$(last_commit_message)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" + test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" ) '
@@ -290,7 +290,7 @@ test_expect_success 'split sub dir/ with --rejoin from scratch' ' git commit -m"sub dir file" && split_hash=$(git subtree split --prefix="sub dir" --rejoin) && git subtree split --prefix="sub dir" --rejoin && - test "$(last_commit_message)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" + test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" ) '
@@ -313,7 +313,7 @@ test_expect_success 'split sub dir/ with --rejoin and --message' ' git fetch ./"sub proj" HEAD && git subtree merge --prefix="sub dir" FETCH_HEAD && git subtree split --prefix="sub dir" --message="Split & rejoin" --annotate="*" --rejoin && - test "$(last_commit_message)" = "Split & rejoin" + test "$(last_commit_subject)" = "Split & rejoin" ) '
@@ -878,7 +878,7 @@ test_expect_success 'push split to subproj' ' git subtree push ./"sub proj" --prefix "sub dir" sub-branch-1 && cd ./"sub proj" && git checkout sub-branch-1 && - test "$(last_commit_message)" = "sub dir/main-sub3" + test "$(last_commit_subject)" = "sub dir/main-sub3" ) '
Signature · 1 line
2.31.1
Re: [PATCH v3 09/30] subtree: t7900: rename last_commit_message to last_commit_subject
Quoted 5 lines · Luke Shumaker
From: Luke Shumaker <[email protected]>
t7900-subtree.shdefines a helper function namedlast_commit_message. However, it only returns the subject line of the commit message, not the entire commit message. So rename it, to make the name less confusing.
This and 06/30 (which I also had a comment on) would be less churn if it were arranged to e.g. use the helper / only touch all these lines once.
PATCH v3 10/30 subtree: t7900: add a test for the -h flag
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
It's a dumb test, but it's surprisingly easy to break.
--- contrib/subtree/t/t7900-subtree.sh | 7 +++++++ 1 file changed, 7 insertions(+)
contrib/subtree/t/t7900-subtree.sh +7 -0
@@ -37,6 +37,13 @@ last_commit_subject () { git log --pretty=format:%s -1 } +test_expect_success 'shows short help text for -h' ' + test_expect_code 129 git subtree -h >out 2>err && + test_must_be_empty err && + grep -e "^ *or: git subtree pull" out && + grep -e --annotate out +' + # # Tests for 'git subtree add' #
Signature · 1 line
2.31.1
Re: [PATCH v3 10/30] subtree: t7900: add a test for the -h flag
Quoted 14 lines · Luke Shumaker
From: Luke Shumaker <[email protected]>
It's a dumb test, but it's surprisingly easy to break.
Signed-off-by Luke Shumaker <[email protected]>--- contrib/subtree/t/t7900-subtree.sh | 7 +++++++ 1 file changed, 7 insertions(+)contrib/subtree/t/t7900-subtree.sh +0
-0
contrib/subtree/t/t7900-subtree.sh last_commit_subject () { · line 44
Quoted 8 lines
contrib/subtree/t/t7900-subtree.sh +5
-0@@ -37,6 +37,13 @@ last_commit_subject () { git log --pretty=format:%s -1 } +test_expect_success 'shows short help text for -h' ' + test_expect_code 129 git subtree -h >out 2>err && + test_must_be_empty err && + grep -e "^ *or: git subtree pull" out && + grep -e --annotate out
I wonder if "grep -e --could-be-an-option-if-not-for-e" is portable, probably....
Re: [PATCH v3 10/30] subtree: t7900: add a test for the -h flag
Quoted 21 lines · Ævar Arnfjörð Bjarmason
Quoted 17 lines · Luke Shumaker
From: Luke Shumaker <[email protected]>
It's a dumb test, but it's surprisingly easy to break.
Signed-off-by Luke Shumaker <[email protected]>--- contrib/subtree/t/t7900-subtree.sh | 7 +++++++ 1 file changed, 7 insertions(+)contrib/subtree/t/t7900-subtree.sh +0
-0@@ -37,6 +37,13 @@ last_commit_subject () { git log --pretty=format:%s -1 }
Quoted 8 lines
contrib/subtree/t/t7900-subtree.sh last_commit_subject () { · line 44
Quoted 5 lines
contrib/subtree/t/t7900-subtree.sh +5
-0+test_expect_success 'shows short help text for -h' ' + test_expect_code 129 git subtree -h >out 2>err && + test_must_be_empty err && + grep -e "^ *or: git subtree pull" out && + grep -e --annotate outI wonder if "grep -e --could-be-an-option-if-not-for-e" is portable, probably....
It's POSIX, and at least works on GNU, busybox, and macOS (based-on-FreeBSD) grep. If it turns out there's somewhere it doesn't work, I'd be happy to change it, but my baseline assumption is that it's portable.
Signature · 2 lines
Happy hacking, ~ Luke Shumaker
Re: [PATCH v3 10/30] subtree: t7900: add a test for the -h flag
In reply to Luke ShumakerQuoted 1 line · Luke Shumaker
It's a dumb test, but it's surprisingly easy to break.
Why did you say that? Very sensitive?
Signature · 1 line
An old man doll... just what I always wanted! - Clara
Re: [PATCH v3 10/30] subtree: t7900: add a test for the -h flag
Quoted 5 lines · Bagas Sanjaya
Quoted 1 line · Luke Shumaker
It's a dumb test, but it's surprisingly easy to break.
Why did you say that? Very sensitive?
Because I very nearly sent v1 of this patchset with a broken '-h' flag :)
I don't recall for certain, but I think the mistake I made was allowing the ordering of eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@")" and . git-sh-setup to get flipped. That certainly will break '-h', anyway.
Signature · 2 lines
Happy hacking, ~ Luke Shumaker
PATCH v3 11/30 subtree: t7900: add porcelain tests for 'pull' and 'push'
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
The 'pull' and 'push' subcommands deserve their own sections in the tests. Add some basic tests for them.
---
v2:
- Don't switch unrelated uses of vanilla test_create_repo over to subtree_test_create_repo; this has been moved to happen in an earlier commit.
- Fix whitespace.
contrib/subtree/t/t7900-subtree.sh | 127 +++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+)
contrib/subtree/t/t7900-subtree.sh +127 -0
@@ -427,6 +427,133 @@ test_expect_success 'split "sub dir"/ with --branch for an incompatible branch' ) ' +# +# Tests for 'git subtree pull' +# + +test_expect_success 'pull requires option --prefix' ' + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && + ( + cd "$test_count" && + git fetch ./"sub proj" HEAD && + git subtree add --prefix="sub dir" FETCH_HEAD + ) && + test_create_commit "$test_count/sub proj" sub2 && + ( + cd "$test_count" && + test_must_fail git subtree pull ./"sub proj" HEAD >out 2>err && + + echo "You must provide the --prefix option." >expected && + test_must_be_empty out && + test_cmp expected err + ) +' + +test_expect_success 'pull requires path given by option --prefix must exist' ' + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && + ( + test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" HEAD >out 2>err && + + echo "'\''sub dir'\'' does not exist; use '\''git subtree add'\''" >expected && + test_must_be_empty out && + test_cmp expected err + ) +' + +test_expect_success 'pull basic operation' ' + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && + ( + cd "$test_count" && + git fetch ./"sub proj" HEAD && + git subtree add --prefix="sub dir" FETCH_HEAD + ) && + test_create_commit "$test_count/sub proj" sub2 && + ( + cd "$test_count" && + exp=$(git -C "sub proj" rev-parse --verify HEAD:) && + git subtree pull --prefix="sub dir" ./"sub proj" HEAD && + act=$(git rev-parse --verify HEAD:"sub dir") && + test "$act" = "$exp" + ) +' + +# +# Tests for 'git subtree push' +# + +test_expect_success 'push requires option --prefix' ' + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && + ( + cd "$test_count" && + git fetch ./"sub proj" HEAD && + git subtree add --prefix="sub dir" FETCH_HEAD && + echo "You must provide the --prefix option." >expected && + test_must_fail git subtree push "./sub proj" from-mainline >actual 2>&1 && + test_debug "printf '"expected: "'" && + test_debug "cat expected" && + test_debug "printf '"actual: "'" && + test_debug "cat actual" && + test_cmp expected actual + ) +' + +test_expect_success 'push requires path given by option --prefix must exist' ' + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && + ( + cd "$test_count" && + git fetch ./"sub proj" HEAD && + git subtree add --prefix="sub dir" FETCH_HEAD && + echo "'\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" >expected && + test_must_fail git subtree push --prefix=non-existent-directory "./sub proj" from-mainline >actual 2>&1 && + test_debug "printf '"expected: "'" && + test_debug "cat expected" && + test_debug "printf '"actual: "'" && + test_debug "cat actual" && + test_cmp expected actual + ) +' + +test_expect_success 'push basic operation' ' + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && + ( + cd "$test_count" && + git fetch ./"sub proj" HEAD && + git subtree add --prefix="sub dir" FETCH_HEAD + ) && + test_create_commit "$test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" main2 && + test_create_commit "$test_count/sub proj" sub2 && + test_create_commit "$test_count" "sub dir"/main-sub2 && + ( + cd "$test_count" && + git fetch ./"sub proj" HEAD && + git subtree merge --prefix="sub dir" FETCH_HEAD && + before=$(git rev-parse --verify HEAD) && + split_hash=$(git subtree split --prefix="sub dir") && + git subtree push --prefix="sub dir" ./"sub proj" from-mainline && + test "$before" = "$(git rev-parse --verify HEAD)" && + test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)" + ) +' + # # Validity checking #
Signature · 1 line
2.31.1
PATCH v3 12/30 subtree: don't have loose code outside of a function
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
Shove all of the loose code inside of a main() function.
This comes down to personal preference more than anything else. A preference that I've developed over years of maintaining large Bash scripts, but still a mere personal preference.
In this specific case, it's also moving the set -- -h, the git rev-parse --parseopt, and the . git-sh-setup to be closer to all the rest of the argument parsing, which is a readability win on its own, IMO.
"Ignore space change" is probably helpful when viewing this diff.
---
v2:
- Include rationale in the the commit message.
contrib/subtree/git-subtree.sh | 245 +++++++++++++++++---------------- 1 file changed, 125 insertions(+), 120 deletions(-)
contrib/subtree/git-subtree.sh +125 -120
@@ -4,10 +4,7 @@ # # Copyright (C) 2009 Avery Pennarun <[email protected]> # -if test $# -eq 0 -then - set -- -h -fi + OPTS_SPEC="\ git subtree add --prefix=<prefix> <commit> git subtree add --prefix=<prefix> <repository> <ref>
@@ -30,12 +27,8 @@ rejoin merge the new branch back into HEAD options for 'add', 'merge', and 'pull' squash merge subtree changes as a single commit " -eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)" PATH=$PATH:$(git --exec-path) -. git-sh-setup - -require_work_tree quiet= branch=
@@ -84,126 +77,138 @@ ensure_single_rev () { fi } -while test $# -gt 0 -do - opt="$1" - shift +main () { + if test $# -eq 0 + then + set -- -h + fi + eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)" + . git-sh-setup + require_work_tree - case "$opt" in - -q) - quiet=1 - ;; - -d) - debug=1 - ;; - --annotate) - annotate="$1" - shift - ;; - --no-annotate) - annotate= - ;; - -b) - branch="$1" - shift - ;; - -P) - prefix="${1%/}" - shift - ;; - -m) - message="$1" - shift - ;; - --no-prefix) - prefix= - ;; - --onto) - onto="$1" + while test $# -gt 0 + do + opt="$1" shift + + case "$opt" in + -q) + quiet=1 + ;; + -d) + debug=1 + ;; + --annotate) + annotate="$1" + shift + ;; + --no-annotate) + annotate= + ;; + -b) + branch="$1" + shift + ;; + -P) + prefix="${1%/}" + shift + ;; + -m) + message="$1" + shift + ;; + --no-prefix) + prefix= + ;; + --onto) + onto="$1" + shift + ;; + --no-onto) + onto= + ;; + --rejoin) + rejoin=1 + ;; + --no-rejoin) + rejoin= + ;; + --ignore-joins) + ignore_joins=1 + ;; + --no-ignore-joins) + ignore_joins= + ;; + --squash) + squash=1 + ;; + --no-squash) + squash= + ;; + --) + break + ;; + *) + die "Unexpected option: $opt" + ;; + esac + done + + command="$1" + shift + + case "$command" in + add|merge|pull) + default= ;; - --no-onto) - onto= - ;; - --rejoin) - rejoin=1 - ;; - --no-rejoin) - rejoin= - ;; - --ignore-joins) - ignore_joins=1 - ;; - --no-ignore-joins) - ignore_joins= - ;; - --squash) - squash=1 + split|push) + default="--default HEAD" ;; - --no-squash) - squash= + *) + die "Unknown command '$command'" ;; - --) - break + esac + + if test -z "$prefix" + then + die "You must provide the --prefix option." + fi + + case "$command" in + add) + test -e "$prefix" && + die "prefix '$prefix' already exists." ;; *) - die "Unexpected option: $opt" + test -e "$prefix" || + die "'$prefix' does not exist; use 'git subtree add'" ;; esac -done - -command="$1" -shift - -case "$command" in -add|merge|pull) - default= - ;; -split|push) - default="--default HEAD" - ;; -*) - die "Unknown command '$command'" - ;; -esac - -if test -z "$prefix" -then - die "You must provide the --prefix option." -fi - -case "$command" in -add) - test -e "$prefix" && - die "prefix '$prefix' already exists." - ;; -*) - test -e "$prefix" || - die "'$prefix' does not exist; use 'git subtree add'" - ;; -esac - -dir="$(dirname "$prefix/.")" - -if test "$command" != "pull" && - test "$command" != "add" && - test "$command" != "push" -then - revs=$(git rev-parse $default --revs-only "$@") || exit $? - dirs=$(git rev-parse --no-revs --no-flags "$@") || exit $? - ensure_single_rev $revs - if test -n "$dirs" - then - die "Error: Use --prefix instead of bare filenames." - fi -fi - -debug "command: {$command}" -debug "quiet: {$quiet}" -debug "revs: {$revs}" -debug "dir: {$dir}" -debug "opts: {$*}" -debug + + dir="$(dirname "$prefix/.")" + + if test "$command" != "pull" && + test "$command" != "add" && + test "$command" != "push" + then + revs=$(git rev-parse $default --revs-only "$@") || exit $? + dirs=$(git rev-parse --no-revs --no-flags "$@") || exit $? + ensure_single_rev $revs + if test -n "$dirs" + then + die "Error: Use --prefix instead of bare filenames." + fi + fi + + debug "command: {$command}" + debug "quiet: {$quiet}" + debug "revs: {$revs}" + debug "dir: {$dir}" + debug "opts: {$*}" + debug + + "cmd_$command" "$@" +} cache_setup () { cachedir="$GIT_DIR/subtree-cache/$$"
@@ -898,4 +903,4 @@ cmd_push () { fi } -"cmd_$command" "$@" +main "$@"
Signature · 1 line
2.31.1
PATCH v3 13/30 subtree: more consistent error propagation
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
Ensure that every $(subshell) that calls a function (as opposed to an external executable) is followed by || exit $?. Similarly, ensure that every cmd | while read; do ... done loop is followed by || exit $?.
Both of those constructs mean that it can miss die calls, and keep running when it shouldn't.
--- contrib/subtree/git-subtree.sh | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)
contrib/subtree/git-subtree.sh +14 -14
@@ -243,7 +243,7 @@ cache_miss () { } check_parents () { - missed=$(cache_miss "$1") + missed=$(cache_miss "$1") || exit $? local indent=$(($2 + 1)) for miss in $missed do
@@ -345,7 +345,7 @@ find_latest_squash () { sub= ;; esac - done + done || exit $? } find_existing_splits () {
@@ -394,7 +394,7 @@ find_existing_splits () { sub= ;; esac - done + done || exit $? } copy_commit () {
@@ -508,7 +508,7 @@ subtree_for_commit () { test "$type" = "commit" && continue # ignore submodules echo $tree break - done + done || exit $? } tree_changed () {
@@ -518,7 +518,7 @@ tree_changed () { then return 0 # weird parents, consider it changed else - ptree=$(toptree_for_commit $1) + ptree=$(toptree_for_commit $1) || exit $? if test "$ptree" != "$tree" then return 0 # changed
@@ -652,7 +652,7 @@ process_split_commit () { progress "$revcount/$revmax ($createcount) [$extracount]" debug "Processing commit: $rev" - exists=$(cache_get "$rev") + exists=$(cache_get "$rev") || exit $? if test -n "$exists" then debug " prior: $exists"
@@ -661,10 +661,10 @@ process_split_commit () { createcount=$(($createcount + 1)) debug " parents: $parents" check_parents "$parents" "$indent" - newparents=$(cache_get $parents) + newparents=$(cache_get $parents) || exit $? debug " newparents: $newparents" - tree=$(subtree_for_commit "$rev" "$dir") + tree=$(subtree_for_commit "$rev" "$dir") || exit $? debug " tree is: $tree" # ugly. is there no better way to tell if this is a subtree
@@ -750,7 +750,7 @@ cmd_add_commit () { commit=$(add_squashed_msg "$rev" "$dir" | git commit-tree "$tree" $headp -p "$rev") || exit $? else - revp=$(peel_committish "$rev") && + revp=$(peel_committish "$rev") || exit $? commit=$(add_msg "$dir" $headrev "$rev" | git commit-tree "$tree" $headp -p "$revp") || exit $? fi
@@ -773,10 +773,10 @@ cmd_split () { # any parent we find there can be used verbatim debug " cache: $rev" cache_set "$rev" "$rev" - done + done || exit $? fi - unrevs="$(find_existing_splits "$dir" "$revs")" + unrevs="$(find_existing_splits "$dir" "$revs")" || exit $? # We can't restrict rev-list to only $dir here, because some of our # parents have the $dir contents the root, and those won't match.
@@ -792,7 +792,7 @@ cmd_split () { process_split_commit "$rev" "$parents" 0 done || exit $? - latest_new=$(cache_get latest_new) + latest_new=$(cache_get latest_new) || exit $? if test -z "$latest_new" then die "No new revisions were found"
@@ -801,7 +801,7 @@ cmd_split () { if test -n "$rejoin" then debug "Merging split branch into HEAD..." - latest_old=$(cache_get latest_old) + latest_old=$(cache_get latest_old) || exit $? git merge -s ours \ --allow-unrelated-histories \ -m "$(rejoin_msg "$dir" "$latest_old" "$latest_new")" \
@@ -834,7 +834,7 @@ cmd_merge () { if test -n "$squash" then - first_split="$(find_latest_squash "$dir")" + first_split="$(find_latest_squash "$dir")" || exit $? if test -z "$first_split" then die "Can't squash-merge: '$dir' was never added."
Signature · 1 line
2.31.1
PATCH v3 14/30 subtree: drop support for git < 1.7
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
Suport for Git versions older than 1.7.0 (older than February 2010) was nice to have when git-subtree lived out-of-tree. But now that it lives in git.git, it's not necessary to keep around. While it's technically in contrib, with the standard 'git' packages for common systems (including Arch Linux and macOS) including git-subtree, it seems vanishingly likely to me that people are separately installing git-subtree from git.git alongside an older 'git' install (although it also seems vanishingly likely that people are still using >11 year old git installs).
Not that there's much reason to remove it either, it's not much code, and none of my changes depend on a newer git (to my knowledge, anyway; I'm not actually testing against older git). I just figure it's an easy piece of fat to trim, in the journey to making the whole thing easier to hack on.
"Ignore space change" is probably helpful when viewing this diff.
---
v2:
- Include rationale in the the commit message.
contrib/subtree/git-subtree.sh | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-)
contrib/subtree/git-subtree.sh +4 -15
@@ -852,23 +852,12 @@ cmd_merge () { rev="$new" fi - version=$(git version) - if test "$version" \< "git version 1.7" + if test -n "$message" then - if test -n "$message" - then - git merge -s subtree --message="$message" "$rev" - else - git merge -s subtree "$rev" - fi + git merge -Xsubtree="$prefix" \ + --message="$message" "$rev" else - if test -n "$message" - then - git merge -Xsubtree="$prefix" \ - --message="$message" "$rev" - else - git merge -Xsubtree="$prefix" $rev - fi + git merge -Xsubtree="$prefix" $rev fi }
Signature · 1 line
2.31.1
PATCH v3 15/30 subtree: use `git merge-base --is-ancestor`
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
Instead of writing a slow rev_is_descendant_of_branch $a $b function in shell, just use the fast git merge-base --is-ancestor $b $a.
--- contrib/subtree/git-subtree.sh | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-)
contrib/subtree/git-subtree.sh +1 -15
@@ -280,20 +280,6 @@ rev_exists () { fi } -rev_is_descendant_of_branch () { - newrev="$1" - branch="$2" - branch_hash=$(git rev-parse "$branch") - match=$(git rev-list -1 "$branch_hash" "^$newrev") - - if test -z "$match" - then - return 0 - else - return 1 - fi -} - # if a commit doesn't have a parent, this might not work. But we only want # to remove the parent from the rev-list, and since it doesn't exist, it won't # be there anyway, so do nothing in that case.
@@ -811,7 +797,7 @@ cmd_split () { then if rev_exists "refs/heads/$branch" then - if ! rev_is_descendant_of_branch "$latest_new" "$branch" + if ! git merge-base --is-ancestor "$branch" "$latest_new" then die "Branch '$branch' is not an ancestor of commit '$latest_new'." fi
Signature · 1 line
2.31.1
PATCH v3 16/30 subtree: use git-sh-setup's `say`
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
subtree currently defines its own say implementation, rather than using git-sh-setups's implementation. Change that, don't re-invent the wheel.
--- contrib/subtree/git-subtree.sh | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-)
contrib/subtree/git-subtree.sh +7 -15
@@ -30,7 +30,6 @@ squash merge subtree changes as a single commit PATH=$PATH:$(git --exec-path) -quiet= branch= debug= command=
@@ -49,15 +48,8 @@ debug () { fi } -say () { - if test -z "$quiet" - then - printf "%s\n" "$*" >&2 - fi -} - progress () { - if test -z "$quiet" + if test -z "$GIT_QUIET" then printf "%s\r" "$*" >&2 fi
@@ -93,7 +85,7 @@ main () { case "$opt" in -q) - quiet=1 + GIT_QUIET=1 ;; -d) debug=1
@@ -201,7 +193,7 @@ main () { fi debug "command: {$command}" - debug "quiet: {$quiet}" + debug "quiet: {$GIT_QUIET}" debug "revs: {$revs}" debug "dir: {$dir}" debug "opts: {$*}"
@@ -698,7 +690,7 @@ cmd_add () { cmd_add_repository "$@" else - say "error: parameters were '$@'" + say >&2 "error: parameters were '$@'" die "Provide either a commit or a repository and commit." fi }
@@ -742,7 +734,7 @@ cmd_add_commit () { fi git reset "$commit" || exit $? - say "Added dir '$dir'" + say >&2 "Added dir '$dir'" } cmd_split () {
@@ -807,7 +799,7 @@ cmd_split () { fi git update-ref -m 'subtree split' \ "refs/heads/$branch" "$latest_new" || exit $? - say "$action branch '$branch'" + say >&2 "$action branch '$branch'" fi echo "$latest_new" exit 0
@@ -830,7 +822,7 @@ cmd_merge () { sub=$2 if test "$sub" = "$rev" then - say "Subtree is already at commit $rev." + say >&2 "Subtree is already at commit $rev." exit 0 fi new=$(new_squash_commit "$old" "$sub" "$rev") || exit $?
Signature · 1 line
2.31.1
PATCH v3 17/30 subtree: use more explicit variable names for cmdline args
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
Make it painfully obvious when reading the code which variables are direct parsings of command line arguments.
--- contrib/subtree/git-subtree.sh | 132 ++++++++++++++++----------------- 1 file changed, 66 insertions(+), 66 deletions(-)
contrib/subtree/git-subtree.sh +66 -66
@@ -30,19 +30,19 @@ squash merge subtree changes as a single commit PATH=$PATH:$(git --exec-path) -branch= -debug= -command= -onto= -rejoin= -ignore_joins= -annotate= -squash= -message= -prefix= +arg_debug= +arg_command= +arg_prefix= +arg_split_branch= +arg_split_onto= +arg_split_rejoin= +arg_split_ignore_joins= +arg_split_annotate= +arg_addmerge_squash= +arg_addmerge_message= debug () { - if test -n "$debug" + if test -n "$arg_debug" then printf "%s\n" "$*" >&2 fi
@@ -88,54 +88,54 @@ main () { GIT_QUIET=1 ;; -d) - debug=1 + arg_debug=1 ;; --annotate) - annotate="$1" + arg_split_annotate="$1" shift ;; --no-annotate) - annotate= + arg_split_annotate= ;; -b) - branch="$1" + arg_split_branch="$1" shift ;; -P) - prefix="${1%/}" + arg_prefix="${1%/}" shift ;; -m) - message="$1" + arg_addmerge_message="$1" shift ;; --no-prefix) - prefix= + arg_prefix= ;; --onto) - onto="$1" + arg_split_onto="$1" shift ;; --no-onto) - onto= + arg_split_onto= ;; --rejoin) - rejoin=1 + arg_split_rejoin=1 ;; --no-rejoin) - rejoin= + arg_split_rejoin= ;; --ignore-joins) - ignore_joins=1 + arg_split_ignore_joins=1 ;; --no-ignore-joins) - ignore_joins= + arg_split_ignore_joins= ;; --squash) - squash=1 + arg_addmerge_squash=1 ;; --no-squash) - squash= + arg_addmerge_squash= ;; --) break
@@ -146,10 +146,10 @@ main () { esac done - command="$1" + arg_command="$1" shift - case "$command" in + case "$arg_command" in add|merge|pull) default= ;;
@@ -157,31 +157,31 @@ main () { default="--default HEAD" ;; *) - die "Unknown command '$command'" + die "Unknown command '$arg_command'" ;; esac - if test -z "$prefix" + if test -z "$arg_prefix" then die "You must provide the --prefix option." fi - case "$command" in + case "$arg_command" in add) - test -e "$prefix" && - die "prefix '$prefix' already exists." + test -e "$arg_prefix" && + die "prefix '$arg_prefix' already exists." ;; *) - test -e "$prefix" || - die "'$prefix' does not exist; use 'git subtree add'" + test -e "$arg_prefix" || + die "'$arg_prefix' does not exist; use 'git subtree add'" ;; esac - dir="$(dirname "$prefix/.")" + dir="$(dirname "$arg_prefix/.")" - if test "$command" != "pull" && - test "$command" != "add" && - test "$command" != "push" + if test "$arg_command" != "pull" && + test "$arg_command" != "add" && + test "$arg_command" != "push" then revs=$(git rev-parse $default --revs-only "$@") || exit $? dirs=$(git rev-parse --no-revs --no-flags "$@") || exit $?
@@ -192,14 +192,14 @@ main () { fi fi - debug "command: {$command}" + debug "command: {$arg_command}" debug "quiet: {$GIT_QUIET}" debug "revs: {$revs}" debug "dir: {$dir}" debug "opts: {$*}" debug - "cmd_$command" "$@" + "cmd_$arg_command" "$@" } cache_setup () {
@@ -333,7 +333,7 @@ find_existing_splits () { main= sub= local grep_format="^git-subtree-dir: $dir/*\$" - if test -n "$ignore_joins" + if test -n "$arg_split_ignore_joins" then grep_format="^Add '$dir/' from commit '" fi
@@ -394,7 +394,7 @@ copy_commit () { GIT_COMMITTER_EMAIL \ GIT_COMMITTER_DATE ( - printf "%s" "$annotate" + printf "%s" "$arg_split_annotate" cat ) | git commit-tree "$2" $3 # reads the rest of stdin
@@ -405,9 +405,9 @@ add_msg () { dir="$1" latest_old="$2" latest_new="$3" - if test -n "$message" + if test -n "$arg_addmerge_message" then - commit_message="$message" + commit_message="$arg_addmerge_message" else commit_message="Add '$dir/' from commit '$latest_new'" fi
@@ -421,9 +421,9 @@ add_msg () { } add_squashed_msg () { - if test -n "$message" + if test -n "$arg_addmerge_message" then - echo "$message" + echo "$arg_addmerge_message" else echo "Merge commit '$1' as '$2'" fi
@@ -433,9 +433,9 @@ rejoin_msg () { dir="$1" latest_old="$2" latest_new="$3" - if test -n "$message" + if test -n "$arg_addmerge_message" then - commit_message="$message" + commit_message="$arg_addmerge_message" else commit_message="Split '$dir/' into commit '$latest_new'" fi
@@ -722,7 +722,7 @@ cmd_add_commit () { headp= fi - if test -n "$squash" + if test -n "$arg_addmerge_squash" then rev=$(new_squash_commit "" "" "$rev") || exit $? commit=$(add_squashed_msg "$rev" "$dir" |
@@ -741,10 +741,10 @@ cmd_split () { debug "Splitting $dir..." cache_setup || exit $? - if test -n "$onto" + if test -n "$arg_split_onto" then - debug "Reading history for --onto=$onto..." - git rev-list $onto | + debug "Reading history for --onto=$arg_split_onto..." + git rev-list $arg_split_onto | while read rev do # the 'onto' history is already just the subdir, so
@@ -776,7 +776,7 @@ cmd_split () { die "No new revisions were found" fi - if test -n "$rejoin" + if test -n "$arg_split_rejoin" then debug "Merging split branch into HEAD..." latest_old=$(cache_get latest_old) || exit $?
@@ -785,21 +785,21 @@ cmd_split () { -m "$(rejoin_msg "$dir" "$latest_old" "$latest_new")" \ "$latest_new" >&2 || exit $? fi - if test -n "$branch" + if test -n "$arg_split_branch" then - if rev_exists "refs/heads/$branch" + if rev_exists "refs/heads/$arg_split_branch" then - if ! git merge-base --is-ancestor "$branch" "$latest_new" + if ! git merge-base --is-ancestor "$arg_split_branch" "$latest_new" then - die "Branch '$branch' is not an ancestor of commit '$latest_new'." + die "Branch '$arg_split_branch' is not an ancestor of commit '$latest_new'." fi action='Updated' else action='Created' fi git update-ref -m 'subtree split' \ - "refs/heads/$branch" "$latest_new" || exit $? - say >&2 "$action branch '$branch'" + "refs/heads/$arg_split_branch" "$latest_new" || exit $? + say >&2 "$action branch '$arg_split_branch'" fi echo "$latest_new" exit 0
@@ -810,7 +810,7 @@ cmd_merge () { ensure_single_rev $rev ensure_clean - if test -n "$squash" + if test -n "$arg_addmerge_squash" then first_split="$(find_latest_squash "$dir")" || exit $? if test -z "$first_split"
@@ -830,12 +830,12 @@ cmd_merge () { rev="$new" fi - if test -n "$message" + if test -n "$arg_addmerge_message" then - git merge -Xsubtree="$prefix" \ - --message="$message" "$rev" + git merge -Xsubtree="$arg_prefix" \ + --message="$arg_addmerge_message" "$rev" else - git merge -Xsubtree="$prefix" $rev + git merge -Xsubtree="$arg_prefix" $rev fi }
@@ -863,7 +863,7 @@ cmd_push () { repository=$1 refspec=$2 echo "git push using: " "$repository" "$refspec" - localrev=$(git subtree split --prefix="$prefix") || die + localrev=$(git subtree split --prefix="$arg_prefix") || die git push "$repository" "$localrev":"refs/heads/$refspec" else die "'$dir' must already exist. Try 'git subtree add'."
Signature · 1 line
2.31.1
PATCH v3 18/30 subtree: use "$*" instead of "$@" as appropriate
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
"$*" is for when you want to concatenate the args together, whitespace-separated; and "$@" is for when you want them to be separate strings.
There are several places in subtree that erroneously use $@ when concatenating args together into an error message.
For instance, if the args are argv[1]="dead" and argv[2]="beef", then the line
die "You must provide exactly one revision. Got: '$@'"
surely intends to call 'die' with the argument
argv[1]="You must provide exactly one revision. Got: 'dead beef'"
however, because the line used $@ instead of $*, it will actually call 'die' with the arguments
argv[1]="You must provide exactly one revision. Got: 'dead" argv[2]="beef'"
This isn't a big deal, because 'die' concatenates its arguments together anyway (using "$*"). But that doesn't change the fact that it was a mistake to use $@ instead of $*, even though in the end $@ still ended up doing the right thing.
---
v2:
- Improve the commit message with quoting and clearer explanation.
contrib/subtree/git-subtree.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
contrib/subtree/git-subtree.sh +3 -3
@@ -58,14 +58,14 @@ progress () { assert () { if ! "$@" then - die "assertion failed: " "$@" + die "assertion failed: $*" fi } ensure_single_rev () { if test $# -ne 1 then - die "You must provide exactly one revision. Got: '$@'" + die "You must provide exactly one revision. Got: '$*'" fi }
@@ -690,7 +690,7 @@ cmd_add () { cmd_add_repository "$@" else - say >&2 "error: parameters were '$@'" + say >&2 "error: parameters were '$*'" die "Provide either a commit or a repository and commit." fi }
Signature · 1 line
2.31.1
PATCH v3 19/30 subtree: don't fuss with PATH
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
Scripts needing to fuss with with adding $(git --exec-prefix) PATH before loading git-sh-setup is a thing of the past. As far as I can tell, it's been a thing of the past since since Git v1.2.0 (2006-02-12), or more specifically, since 77cb17e940 (Exec git programs without using PATH, 2006-01-10). However, it stuck around in contrib scripts and in third-party scripts for long enough that it wasn't unusual to see.
Originally git subtree didn't fuss with PATH, but when people (including the original subtree author) had problems, because it was a common thing to see, it seemed that having subtree fuss with PATH was a reasonable solution.
Here is an abridged history of fussing with PATH in subtree:
2987e6add3 (Add explicit path of git installation by 'git --exec-path', Gianluca Pacchiella, 2009-08-20)
As pointed out by documentation, the correct use of 'git-sh-setup' is
using $(git --exec-path) to avoid problems with not standard
installations.
-. git-sh-setup
+. $(git --exec-path)/git-sh-setup 33aaa697a2 (Improve patch to use git --exec-path: add to PATH instead, Avery Pennarun, 2009-08-26)
If you (like me) are using a modified git straight out of its source directory (ie. without installing), then --exec-path isn't actually correct. Add it to the PATH instead, so if it is correct, it'll work, but if it's not, we fall back to the previous behaviour.
-. $(git --exec-path)/git-sh-setup
+PATH=$(git --exec-path):$PATH
+. git-sh-setup
9c632ea29c ((Hopefully) fix PATH setting for msysgit, Avery Pennarun, 2010-06-24)
Reported by Evan Shaw. The problem is that $(git --exec-path) includes a 'git' binary which is incompatible with the one in /usr/bin; if you run it, it gives you an error about libiconv2.dll.
+OPATH=$PATH
PATH=$(git --exec-path):$PATH
. git-sh-setup
+PATH=$OPATH # apparently needed for some versions of msysgit
df2302d774 (Another fix for PATH and msysgit, Avery Pennarun, 2010-06-24)
Evan Shaw tells me the previous fix didn't work. Let's use this one instead, which he says does work.
This fix is kind of wrong because it will run the "correct" git-sh-setup *after* the one in /usr/bin, if there is one, which could be weird if you have multiple versions of git installed. But it works on my Linux and his msysgit, so it's obviously better than what we had before.
-OPATH=$PATH
-PATH=$(git --exec-path):$PATH
+PATH=$PATH:$(git --exec-path)
. git-sh-setup
-PATH=$OPATH # apparently needed for some versions of msysgit
First of all, I disagree with Gianluca's reading of the documentation:
- I haven't gone back to read what the documentation said in 2009, but in my reading of the 2021 documentation is that it includes "$(git --exec-path)/" in the synopsis for illustrative purposes, not to say it's the proper way.
- After being executed by git, the git exec path should be the very first entry in PATH, so it shouldn't matter.
- None of the scripts that are part of git do it that way.
But secondly, the root reason for fussing with PATH seems to be that Avery didn't know that he needs to set GIT_EXEC_PATH if he's going to use git from the source directory without installing.
And finally, Evan's issue is clearly just a bug in msysgit. I assume that msysgit has since fixed the issue, and also msysgit has been deprecated for 6 years now, so let's drop the workaround for it.
So, remove the line fussing with PATH. However, since subtree *is* in 'contrib/' and it might get installed in funny ways by users after-the-fact, add a sanity check to the top of the script, checking that it is installed correctly.
---
v3:
- Fix capitalization in the commit message.
contrib/subtree/git-subtree.sh | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
contrib/subtree/git-subtree.sh +16 -2
@@ -5,6 +5,22 @@ # Copyright (C) 2009 Avery Pennarun <[email protected]> # +if test -z "$GIT_EXEC_PATH" || test "${PATH#"${GIT_EXEC_PATH}:"}" = "$PATH" || ! test -f "$GIT_EXEC_PATH/git-sh-setup" +then + echo >&2 'It looks like either your git installation or your' + echo >&2 'git-subtree installation is broken.' + echo >&2 + echo >&2 "Tips:" + echo >&2 " - If \`git --exec-path\` does not print the correct path to" + echo >&2 " your git install directory, then set the GIT_EXEC_PATH" + echo >&2 " environment variable to the correct directory." + echo >&2 " - Make sure that your \`${0##*/}\` file is either in your" + echo >&2 " PATH or in your git exec path (\`$(git --exec-path)\`)." + echo >&2 " - You should run git-subtree as \`git ${0##*/git-}\`," + echo >&2 " not as \`${0##*/}\`." >&2 + exit 126 +fi + OPTS_SPEC="\ git subtree add --prefix=<prefix> <commit> git subtree add --prefix=<prefix> <repository> <ref>
@@ -28,8 +44,6 @@ rejoin merge the new branch back into HEAD squash merge subtree changes as a single commit " -PATH=$PATH:$(git --exec-path) - arg_debug= arg_command= arg_prefix=
Signature · 1 line
2.31.1
PATCH v3 20/30 subtree: use "^{commit}" instead of "^0"
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
They are synonyms. Both are used in the file. ^{commit} is clearer, so "standardize" on that.
--- contrib/subtree/git-subtree.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
contrib/subtree/git-subtree.sh +2 -2
@@ -316,7 +316,7 @@ find_latest_squash () { main="$b" ;; git-subtree-split:) - sub="$(git rev-parse "$b^0")" || + sub="$(git rev-parse "$b^{commit}")" || die "could not rev-parse split hash $b from commit $sq" ;; END)
@@ -363,7 +363,7 @@ find_existing_splits () { main="$b" ;; git-subtree-split:) - sub="$(git rev-parse "$b^0")" || + sub="$(git rev-parse "$b^{commit}")" || die "could not rev-parse split hash $b from commit $sq" ;; END)
Signature · 1 line
2.31.1
PATCH v3 21/30 subtree: parse revs in individual cmd_ functions
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
The main argument parser goes ahead and tries to parse revs to make things simpler for the sub-command implementations. But, it includes enough special cases for different sub-commands. And it's difficult having having to think about "is this info coming from an argument, or a global variable?". So the main argument parser's effort to make things "simpler" ends up just making it more confusing and complicated.
Begone with the 'revs' global variable; parse 'rev=$(...)' as needed in individual 'cmd_*' functions.
Begone with the 'default' global variable. Its would-be value is knowable just from which function we're in.
Begone with the 'ensure_single_rev' function. Its functionality can be achieved by passing '--verify' to 'git rev-parse'.
--- contrib/subtree/git-subtree.sh | 62 +++++++++++++--------------------- 1 file changed, 24 insertions(+), 38 deletions(-)
contrib/subtree/git-subtree.sh +24 -38
@@ -76,13 +76,6 @@ assert () { fi } -ensure_single_rev () { - if test $# -ne 1 - then - die "You must provide exactly one revision. Got: '$*'" - fi -} - main () { if test $# -eq 0 then
@@ -164,11 +157,8 @@ main () { shift case "$arg_command" in - add|merge|pull) - default= - ;; - split|push) - default="--default HEAD" + add|merge|pull|split|push) + : ;; *) die "Unknown command '$arg_command'"
@@ -193,22 +183,8 @@ main () { dir="$(dirname "$arg_prefix/.")" - if test "$arg_command" != "pull" && - test "$arg_command" != "add" && - test "$arg_command" != "push" - then - revs=$(git rev-parse $default --revs-only "$@") || exit $? - dirs=$(git rev-parse --no-revs --no-flags "$@") || exit $? - ensure_single_rev $revs - if test -n "$dirs" - then - die "Error: Use --prefix instead of bare filenames." - fi - fi - debug "command: {$arg_command}" debug "quiet: {$GIT_QUIET}" - debug "revs: {$revs}" debug "dir: {$dir}" debug "opts: {$*}" debug
@@ -714,14 +690,13 @@ cmd_add_repository () { repository=$1 refspec=$2 git fetch "$@" || exit $? - revs=FETCH_HEAD - set -- $revs - cmd_add_commit "$@" + cmd_add_commit FETCH_HEAD } cmd_add_commit () { - rev=$(git rev-parse $default --revs-only "$@") || exit $? - ensure_single_rev $rev + # The rev has already been validated by cmd_add(), we just + # need to normalize it. + rev=$(git rev-parse --verify "$1^{commit}") || exit $? debug "Adding $dir as '$rev'..." git read-tree --prefix="$dir" $rev || exit $?
@@ -752,6 +727,17 @@ cmd_add_commit () { } cmd_split () { + if test $# -eq 0 + then + rev=$(git rev-parse HEAD) + elif test $# -eq 1 + then + rev=$(git rev-parse -q --verify "$1^{commit}") || + die "'$1' does not refer to a commit" + else + die "You must provide exactly one revision. Got: '$*'" + fi + debug "Splitting $dir..." cache_setup || exit $?
@@ -768,12 +754,12 @@ cmd_split () { done || exit $? fi - unrevs="$(find_existing_splits "$dir" "$revs")" || exit $? + unrevs="$(find_existing_splits "$dir" "$rev")" || exit $? # We can't restrict rev-list to only $dir here, because some of our # parents have the $dir contents the root, and those won't match. # (and rev-list --follow doesn't seem to solve this) - grl='git rev-list --topo-order --reverse --parents $revs $unrevs' + grl='git rev-list --topo-order --reverse --parents $rev $unrevs' revmax=$(eval "$grl" | wc -l) revcount=0 createcount=0
@@ -820,8 +806,10 @@ cmd_split () { } cmd_merge () { - rev=$(git rev-parse $default --revs-only "$@") || exit $? - ensure_single_rev $rev + test $# -eq 1 || + die "You must provide exactly one revision. Got: '$*'" + rev=$(git rev-parse -q --verify "$1^{commit}") || + die "'$1' does not refer to a commit" ensure_clean if test -n "$arg_addmerge_squash"
@@ -861,9 +849,7 @@ cmd_pull () { ensure_clean ensure_valid_ref_format "$2" git fetch "$@" || exit $? - revs=FETCH_HEAD - set -- $revs - cmd_merge "$@" + cmd_merge FETCH_HEAD } cmd_push () {
Signature · 1 line
2.31.1
PATCH v3 22/30 subtree: remove duplicate check
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
cmd_add starts with a check that the directory doesn't yet exist. However, the main function performs the exact same check before calling cmd_add. So remove the check from cmd_add.
--- contrib/subtree/git-subtree.sh | 4 ---- 1 file changed, 4 deletions(-)
contrib/subtree/git-subtree.sh +0 -4
@@ -655,10 +655,6 @@ process_split_commit () { } cmd_add () { - if test -e "$dir" - then - die "'$dir' already exists. Cannot add." - fi ensure_clean
Signature · 1 line
2.31.1
PATCH v3 23/30 subtree: add comments and sanity checks
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
For each function in subtree, add a usage comment saying what the arguments are, and add an assert checking the number of arguments.
In figuring out each thing's arguments in order to write those comments and assertions, it turns out that find_existing_splits is written as if it takes multiple 'revs', but it is in fact only ever passed a single 'rev':
unrevs="$(find_existing_splits "$dir" "$rev")" || exit $?
So go ahead and codify that by documenting and asserting that it takes exactly two arguments, one dir and one rev.
---
v2:
- Expand on the the commit message.
- Fix capitalization in one of the comments.
contrib/subtree/git-subtree.sh | 64 ++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 3 deletions(-)
contrib/subtree/git-subtree.sh +61 -3
@@ -55,6 +55,7 @@ arg_split_annotate= arg_addmerge_squash= arg_addmerge_message= +# Usage: debug [MSG...] debug () { if test -n "$arg_debug" then
@@ -62,6 +63,7 @@ debug () { fi } +# Usage: progress [MSG...] progress () { if test -z "$GIT_QUIET" then
@@ -69,6 +71,7 @@ progress () { fi } +# Usage: assert CMD... assert () { if ! "$@" then
@@ -192,7 +195,9 @@ main () { "cmd_$arg_command" "$@" } +# Usage: cache_setup cache_setup () { + assert test $# = 0 cachedir="$GIT_DIR/subtree-cache/$$" rm -rf "$cachedir" || die "Can't delete old cachedir: $cachedir"
@@ -203,6 +208,7 @@ cache_setup () { debug "Using cachedir: $cachedir" >&2 } +# Usage: cache_get [REVS...] cache_get () { for oldrev in "$@" do
@@ -214,6 +220,7 @@ cache_get () { done } +# Usage: cache_miss [REVS...] cache_miss () { for oldrev in "$@" do
@@ -224,7 +231,9 @@ cache_miss () { done } +# Usage: check_parents PARENTS_EXPR INDENT check_parents () { + assert test $# = 2 missed=$(cache_miss "$1") || exit $? local indent=$(($2 + 1)) for miss in $missed
@@ -237,11 +246,15 @@ check_parents () { done } +# Usage: set_notree REV set_notree () { + assert test $# = 1 echo "1" > "$cachedir/notree/$1" } +# Usage: cache_set OLDREV NEWREV cache_set () { + assert test $# = 2 oldrev="$1" newrev="$2" if test "$oldrev" != "latest_old" &&
@@ -253,7 +266,9 @@ cache_set () { echo "$newrev" >"$cachedir/$oldrev" } +# Usage: rev_exists REV rev_exists () { + assert test $# = 1 if git rev-parse "$1" >/dev/null 2>&1 then return 0
@@ -262,17 +277,22 @@ rev_exists () { fi } -# if a commit doesn't have a parent, this might not work. But we only want +# Usage: try_remove_previous REV +# +# If a commit doesn't have a parent, this might not work. But we only want # to remove the parent from the rev-list, and since it doesn't exist, it won't # be there anyway, so do nothing in that case. try_remove_previous () { + assert test $# = 1 if rev_exists "$1^" then echo "^$1^" fi } +# Usage: find_latest_squash DIR find_latest_squash () { + assert test $# = 1 debug "Looking for latest squash ($dir)..." dir="$1" sq=
@@ -316,10 +336,12 @@ find_latest_squash () { done || exit $? } +# Usage: find_existing_splits DIR REV find_existing_splits () { + assert test $# = 2 debug "Looking for prior splits..." dir="$1" - revs="$2" + rev="$2" main= sub= local grep_format="^git-subtree-dir: $dir/*\$"
@@ -328,7 +350,7 @@ find_existing_splits () { grep_format="^Add '$dir/' from commit '" fi git log --grep="$grep_format" \ - --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs | + --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' "$rev" | while read a b junk do case "$a" in
@@ -365,7 +387,9 @@ find_existing_splits () { done || exit $? } +# Usage: copy_commit REV TREE FLAGS_STR copy_commit () { + assert test $# = 3 # We're going to set some environment vars here, so # do it in a subshell to get rid of them safely later debug copy_commit "{$1}" "{$2}" "{$3}"
@@ -391,7 +415,9 @@ copy_commit () { ) || die "Can't copy commit $1" } +# Usage: add_msg DIR LATEST_OLD LATEST_NEW add_msg () { + assert test $# = 3 dir="$1" latest_old="$2" latest_new="$3"
@@ -410,7 +436,9 @@ add_msg () { EOF } +# Usage: add_squashed_msg REV DIR add_squashed_msg () { + assert test $# = 2 if test -n "$arg_addmerge_message" then echo "$arg_addmerge_message"
@@ -419,7 +447,9 @@ add_squashed_msg () { fi } +# Usage: rejoin_msg DIR LATEST_OLD LATEST_NEW rejoin_msg () { + assert test $# = 3 dir="$1" latest_old="$2" latest_new="$3"
@@ -438,7 +468,9 @@ rejoin_msg () { EOF } +# Usage: squash_msg DIR OLD_SUBTREE_COMMIT NEW_SUBTREE_COMMIT squash_msg () { + assert test $# = 3 dir="$1" oldsub="$2" newsub="$3"
@@ -460,12 +492,16 @@ squash_msg () { echo "git-subtree-split: $newsub" } +# Usage: toptree_for_commit COMMIT toptree_for_commit () { + assert test $# = 1 commit="$1" git rev-parse --verify "$commit^{tree}" || exit $? } +# Usage: subtree_for_commit COMMIT DIR subtree_for_commit () { + assert test $# = 2 commit="$1" dir="$2" git ls-tree "$commit" -- "$dir" |
@@ -479,7 +515,9 @@ subtree_for_commit () { done || exit $? } +# Usage: tree_changed TREE [PARENTS...] tree_changed () { + assert test $# -gt 0 tree=$1 shift if test $# -ne 1
@@ -496,7 +534,9 @@ tree_changed () { fi } +# Usage: new_squash_commit OLD_SQUASHED_COMMIT OLD_NONSQUASHED_COMMIT NEW_NONSQUASHED_COMMIT new_squash_commit () { + assert test $# = 3 old="$1" oldsub="$2" newsub="$3"
@@ -511,7 +551,9 @@ new_squash_commit () { fi } +# Usage: copy_or_skip REV TREE NEWPARENTS copy_or_skip () { + assert test $# = 3 rev="$1" tree="$2" newparents="$3"
@@ -586,7 +628,9 @@ copy_or_skip () { fi } +# Usage: ensure_clean ensure_clean () { + assert test $# = 0 if ! git diff-index HEAD --exit-code --quiet 2>&1 then die "Working tree has modifications. Cannot add."
@@ -597,12 +641,16 @@ ensure_clean () { fi } +# Usage: ensure_valid_ref_format REF ensure_valid_ref_format () { + assert test $# = 1 git check-ref-format "refs/heads/$1" || die "'$1' does not look like a ref" } +# Usage: process_split_commit REV PARENTS INDENT process_split_commit () { + assert test $# = 3 local rev="$1" local parents="$2" local indent=$3
@@ -654,6 +702,8 @@ process_split_commit () { cache_set latest_old "$rev" } +# Usage: cmd_add REV +# Or: cmd_add REPOSITORY REF cmd_add () { ensure_clean
@@ -681,7 +731,9 @@ cmd_add () { fi } +# Usage: cmd_add_repository REPOSITORY REFSPEC cmd_add_repository () { + assert test $# = 2 echo "git fetch" "$@" repository=$1 refspec=$2
@@ -689,9 +741,11 @@ cmd_add_repository () { cmd_add_commit FETCH_HEAD } +# Usage: cmd_add_commit REV cmd_add_commit () { # The rev has already been validated by cmd_add(), we just # need to normalize it. + assert test $# = 1 rev=$(git rev-parse --verify "$1^{commit}") || exit $? debug "Adding $dir as '$rev'..."
@@ -722,6 +776,7 @@ cmd_add_commit () { say >&2 "Added dir '$dir'" } +# Usage: cmd_split [REV] cmd_split () { if test $# -eq 0 then
@@ -801,6 +856,7 @@ cmd_split () { exit 0 } +# Usage: cmd_merge REV cmd_merge () { test $# -eq 1 || die "You must provide exactly one revision. Got: '$*'"
@@ -837,6 +893,7 @@ cmd_merge () { fi } +# Usage: cmd_pull REPOSITORY REMOTEREF cmd_pull () { if test $# -ne 2 then
@@ -848,6 +905,7 @@ cmd_pull () { cmd_merge FETCH_HEAD } +# Usage: cmd_push REPOSITORY REMOTEREF cmd_push () { if test $# -ne 2 then
Signature · 1 line
2.31.1
PATCH v3 24/30 subtree: don't let debug and progress output clash
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
Currently, debug output (triggered by passing '-d') and progress output stomp on each other. The debug output is just streamed as lines to stderr, and the progress output is sent to stderr as '%s\r'. When writing to a file, it is awkward to read and difficult to distinguish between the debug output and a progress line. When writing to a terminal the debug lines hide progress lines.
So, when '-d' has been passed, spit out progress as 'progress: %s\n', instead of as '%s\r', so that it can be detected, and so that the debug lines don't overwrite the progress when written to a terminal.
---
v2:
- Reword the commit message to be clearer.
- Add comments to the code.
- Flip the if and else cases around, so that the comments read better.
contrib/subtree/git-subtree.sh | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
contrib/subtree/git-subtree.sh +21 -1
@@ -67,7 +67,27 @@ debug () { progress () { if test -z "$GIT_QUIET" then - printf "%s\r" "$*" >&2 + if test -z "$arg_debug" + then + # Debug mode is off. + # + # Print one progress line that we keep updating (use + # "\r" to return to the beginning of the line, rather + # than "\n" to start a new line). This only really + # works when stderr is a terminal. + printf "%s\r" "$*" >&2 + else + # Debug mode is on. The `debug` function is regularly + # printing to stderr. + # + # Don't do the one-line-with-"\r" thing, because on a + # terminal the debug output would overwrite and hide the + # progress output. Add a "progress:" prefix to make the + # progress output and the debug output easy to + # distinguish. This ensures maximum readability whether + # stderr is a terminal or a file. + printf "progress: %s\n" "$*" >&2 + fi fi }
Signature · 1 line
2.31.1
PATCH v3 25/30 subtree: have $indent actually affect indentation
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
Currently, the $indent variable is just used to track how deeply we're nested, and the debug log is indented by things like
debug " foo"
That is: The indentation-level is hard-coded. It used to be that the code couldn't recurse, so the indentation level could be known statically, so it made sense to just hard-code it in the output. However, since 315a84f9aa ("subtree: use commits before rejoins for splits", 2018-09-28), it can now recurse, and the debug log is misleading.
So fix that. Indent according to $indent.
--- contrib/subtree/git-subtree.sh | 42 +++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 18 deletions(-)
contrib/subtree/git-subtree.sh +24 -18
@@ -55,11 +55,13 @@ arg_split_annotate= arg_addmerge_squash= arg_addmerge_message= +indent=0 + # Usage: debug [MSG...] debug () { if test -n "$arg_debug" then - printf "%s\n" "$*" >&2 + printf "%$(($indent * 2))s%s\n" '' "$*" >&2 fi }
@@ -251,17 +253,17 @@ cache_miss () { done } -# Usage: check_parents PARENTS_EXPR INDENT +# Usage: check_parents PARENTS_EXPR check_parents () { - assert test $# = 2 + assert test $# = 1 missed=$(cache_miss "$1") || exit $? - local indent=$(($2 + 1)) + local indent=$(($indent + 1)) for miss in $missed do if ! test -r "$cachedir/notree/$miss" then - debug " incorrect order: $miss" - process_split_commit "$miss" "" "$indent" + debug "incorrect order: $miss" + process_split_commit "$miss" "" fi done }
@@ -314,6 +316,8 @@ try_remove_previous () { find_latest_squash () { assert test $# = 1 debug "Looking for latest squash ($dir)..." + local indent=$(($indent + 1)) + dir="$1" sq= main=
@@ -360,6 +364,8 @@ find_latest_squash () { find_existing_splits () { assert test $# = 2 debug "Looking for prior splits..." + local indent=$(($indent + 1)) + dir="$1" rev="$2" main=
@@ -385,7 +391,7 @@ find_existing_splits () { die "could not rev-parse split hash $b from commit $sq" ;; END) - debug " Main is: '$main'" + debug "Main is: '$main'" if test -z "$main" -a -n "$sub" then # squash commits refer to a subtree
@@ -668,12 +674,11 @@ ensure_valid_ref_format () { die "'$1' does not look like a ref" } -# Usage: process_split_commit REV PARENTS INDENT +# Usage: process_split_commit REV PARENTS process_split_commit () { - assert test $# = 3 + assert test $# = 2 local rev="$1" local parents="$2" - local indent=$3 if test $indent -eq 0 then
@@ -688,20 +693,21 @@ process_split_commit () { progress "$revcount/$revmax ($createcount) [$extracount]" debug "Processing commit: $rev" + local indent=$(($indent + 1)) exists=$(cache_get "$rev") || exit $? if test -n "$exists" then - debug " prior: $exists" + debug "prior: $exists" return fi createcount=$(($createcount + 1)) - debug " parents: $parents" - check_parents "$parents" "$indent" + debug "parents: $parents" + check_parents "$parents" newparents=$(cache_get $parents) || exit $? - debug " newparents: $newparents" + debug "newparents: $newparents" tree=$(subtree_for_commit "$rev" "$dir") || exit $? - debug " tree is: $tree" + debug "tree is: $tree" # ugly. is there no better way to tell if this is a subtree # vs. a mainline commit? Does it matter?
@@ -716,7 +722,7 @@ process_split_commit () { fi newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $? - debug " newrev is: $newrev" + debug "newrev is: $newrev" cache_set "$rev" "$newrev" cache_set latest_new "$newrev" cache_set latest_old "$rev"
@@ -820,7 +826,7 @@ cmd_split () { do # the 'onto' history is already just the subdir, so # any parent we find there can be used verbatim - debug " cache: $rev" + debug "cache: $rev" cache_set "$rev" "$rev" done || exit $? fi
@@ -838,7 +844,7 @@ cmd_split () { eval "$grl" | while read rev parents do - process_split_commit "$rev" "$parents" 0 + process_split_commit "$rev" "$parents" done || exit $? latest_new=$(cache_get latest_new) || exit $?
Signature · 1 line
2.31.1
PATCH v3 26/30 subtree: give the docs a once-over
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
Just went through the docs looking for anything inaccurate or that can be improved.
In the '-h' text, in the man page synopsis, and in the man page
description: Normalize the ordering of the list of sub-commands: 'add', 'merge', 'split', 'pull', 'push'. This allows us to kinda separate the lower-level add/merge/split from the higher-level pull/push.
'-h' text:
- correction: Indicate that split's arg is optional.
- clarity: Emphasize that 'pull' takes the 'add'/'merge' flags.
man page:
- correction: State that all subcommands take options (it seemed to indicate that only 'split' takes any options other than '-P').
- correction: 'split' only guarantees that the results are identical if the flags are identical.
- correction: The flag is named '--ignore-joins', not '--ignore-join'.
- completeness: Clarify that 'push' always operates on HEAD, and that 'split' operates on HEAD if no local commit is given.
- clarity: In the description, when listing commands, repeat what their arguments are. This way the reader doesn't need to flip back and forth between the command description and the synopsis and the full description to understand what's being said.
- clarity: In the <variables> used to give command arguments, give slightly longer, descriptive names. Like <local-commit> instead of just <commit>.
- clarity: Emphasize that 'pull' takes the 'add'/'merge' flags.
- style: In the synopsis, list options before the subcommand. This makes things line up and be much more readable when shown non-monospace (such as in make html), and also more closely matches other man pages (like git-submodule.txt).
- style: Use the correct syntax for indicating the options ([<options>] instead of [OPTIONS]).
- style: In the synopsis, separate 'pull' and 'push' from the other lower-level commands. I think this helps readability.
- style: Code-quote things in prose that seem like they should be code-quoted, like '.gitmodules', flags, or full commands.
- style: Minor wording improvements, like more consistent mood (many of the command descriptions start in the imperative mood and switch to the indicative mode by the end). That sort of thing.
- style: Capitalize "ID".
- style: Remove the "This option is only valid for XXX command" remarks from each option, and instead rely on the section headings.
- style: Since that line is getting edited anyway, switch "behaviour" to American "behavior".
- style: Trim trailing whitespace.
todo:
- style: Trim trailing whitespace.
---
v2:
- behaviour -> behavior
v3:
- Trim trailing whitespace.
- Fix the man page saying "--ignore-join" instead of "--ignore-joins".
contrib/subtree/git-subtree.sh | 4 +- contrib/subtree/git-subtree.txt | 170 +++++++++++++++----------------- contrib/subtree/todo | 6 +- 3 files changed, 87 insertions(+), 93 deletions(-)
contrib/subtree/git-subtree.sh +2 -2
@@ -25,9 +25,9 @@ OPTS_SPEC="\ git subtree add --prefix=<prefix> <commit> git subtree add --prefix=<prefix> <repository> <ref> git subtree merge --prefix=<prefix> <commit> +git subtree split --prefix=<prefix> [<commit>] git subtree pull --prefix=<prefix> <repository> <ref> git subtree push --prefix=<prefix> <repository> <ref> -git subtree split --prefix=<prefix> <commit> -- h,help show the help q quiet
@@ -40,7 +40,7 @@ b,branch= create a new branch from the split subtree ignore-joins ignore prior --rejoin commits onto= try connecting new tree to an existing one rejoin merge the new branch back into HEAD - options for 'add', 'merge', and 'pull' + options for 'add' and 'merge' (also: 'pull') squash merge subtree changes as a single commit "
contrib/subtree/git-subtree.txt +82 -88
303 lines not on this page · raw message
contrib/subtree/todo +3 -3
@@ -23,9 +23,9 @@ "pull" and "merge" commands should fail if you've never merged that --prefix before - + docs should provide an example of "add" - + note that the initial split doesn't *have* to have a commitid specified... that's just an optimization
@@ -33,7 +33,7 @@ get a misleading "prefix must end with /" message from one of the other git tools that git-subtree calls. Should detect this situation and print the *real* problem. - + "pull --squash" should do fetch-synthesize-merge, but instead just does "pull" directly, which doesn't work at all.
Signature · 1 line
2.31.1
PATCH v3 27/30 subtree: allow --squash to be used with --rejoin
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
Besides being a genuinely useful thing to do, this also just makes sense and harmonizes which flags may be used when. git subtree split --rejoin amounts to "automatically go ahead and do a git subtree merge after doing the main git subtree split", so it's weird and arbitrary that you can't pass --squash to git subtree split --rejoin like you can git subtree merge. It's weird that git subtree split --rejoin inherits git subtree merge's --message but not --squash.
Reconcile the situation by just having split --rejoin actually just call merge internally (or call add instead, as appropriate), so it can get access to the full merge behavior, including --squash.
---
v2:
- In the added tests, use test_must_fail instead of !, as appropriate.
v3:
- Style: Put the then of an if/then on its own line.
- Fix a bug (and add a test) where the resulting commits confuse a subsequent git subtree merge --squash.
contrib/subtree/git-subtree.sh | 37 +++++++++++++++----- contrib/subtree/git-subtree.txt | 27 ++++++-------- contrib/subtree/t/t7900-subtree.sh | 56 ++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 24 deletions(-)
contrib/subtree/git-subtree.sh +29 -8
@@ -33,15 +33,15 @@ h,help show the help q quiet d show debug messages P,prefix= the name of the subdir to split out -m,message= use the given message as the commit message for the merge commit options for 'split' annotate= add a prefix to commit message of new commits b,branch= create a new branch from the split subtree ignore-joins ignore prior --rejoin commits onto= try connecting new tree to an existing one rejoin merge the new branch back into HEAD - options for 'add' and 'merge' (also: 'pull') + options for 'add' and 'merge' (also: 'pull' and 'split --rejoin') squash merge subtree changes as a single commit +m,message= use the given message as the commit message for the merge commit " arg_debug=
@@ -346,7 +346,8 @@ find_latest_squash () { then # a rejoin commit? # Pretend its sub was a squash. - sq="$sub" + sq=$(git rev-parse --verify "$sq^2") || + die fi debug "Squash found: $sq $sub" echo "$sq" "$sub"
@@ -453,6 +454,13 @@ add_msg () { else commit_message="Add '$dir/' from commit '$latest_new'" fi + if test -n "$arg_split_rejoin" + then + # If this is from a --rejoin, then rejoin_msg has + # already inserted the `git-subtree-xxx:` tags + echo "$commit_message" + return + fi cat <<-EOF $commit_message
@@ -775,7 +783,12 @@ cmd_add_commit () { rev=$(git rev-parse --verify "$1^{commit}") || exit $? debug "Adding $dir as '$rev'..." - git read-tree --prefix="$dir" $rev || exit $? + if test -z "$arg_split_rejoin" + then + # Only bother doing this if this is a genuine 'add', + # not a synthetic 'add' from '--rejoin'. + git read-tree --prefix="$dir" $rev || exit $? + fi git checkout -- "$dir" || exit $? tree=$(git write-tree) || exit $?
@@ -815,6 +828,11 @@ cmd_split () { die "You must provide exactly one revision. Got: '$*'" fi + if test -n "$arg_split_rejoin" + then + ensure_clean + fi + debug "Splitting $dir..." cache_setup || exit $?
@@ -857,10 +875,13 @@ cmd_split () { then debug "Merging split branch into HEAD..." latest_old=$(cache_get latest_old) || exit $? - git merge -s ours \ - --allow-unrelated-histories \ - -m "$(rejoin_msg "$dir" "$latest_old" "$latest_new")" \ - "$latest_new" >&2 || exit $? + arg_addmerge_message="$(rejoin_msg "$dir" "$latest_old" "$latest_new")" || exit $? + if test -z "$(find_latest_squash "$dir")" + then + cmd_add "$latest_new" >&2 || exit $? + else + cmd_merge "$latest_new" >&2 || exit $? + fi fi if test -n "$arg_split_branch" then
contrib/subtree/git-subtree.txt +11 -16
@@ -109,9 +109,6 @@ settings passed to 'split' (such as '--annotate') are the same. Because of this, if you add new commits and then re-split, the new commits will be attached as commits on top of the history you generated last time, so 'git merge' and friends will work as expected. -+ -Note that if you use '--squash' when you merge, you should usually not -just '--rejoin' when you split. pull <repository> <remote-ref>:: Exactly like 'merge', but parallels 'git pull' in that
@@ -124,8 +121,8 @@ push <repository> <remote-ref>:: <remote-ref>. This can be used to push your subtree to different branches of the remote repository. -OPTIONS -------- +OPTIONS FOR ALL COMMANDS +------------------------ -q:: --quiet:: Suppress unnecessary output messages on stderr.
@@ -140,15 +137,11 @@ OPTIONS want to manipulate. This option is mandatory for all commands. --m <message>:: ---message=<message>:: - This option is only valid for 'add', 'merge', 'pull', and 'split --rejoin'. - Specify <message> as the commit message for the merge commit. - -OPTIONS FOR 'add' AND 'merge' (ALSO: 'pull') --------------------------------------------- +OPTIONS FOR 'add' AND 'merge' (ALSO: 'pull' AND 'split --rejoin') +----------------------------------------------------------------- These options for 'add' and 'merge' may also be given to 'pull' (which -wraps 'merge'). +wraps 'merge') and 'split --rejoin' (which wraps either 'add' or +'merge' as appropriate). --squash:: Instead of merging the entire history from the subtree project, produce
@@ -176,6 +169,9 @@ Whether or not you use '--squash', changes made in your local repository remain intact and can be later split and send upstream to the subproject. +-m <message>:: +--message=<message>:: + Specify <message> as the commit message for the merge commit. OPTIONS FOR 'split' -------------------
@@ -229,9 +225,8 @@ Unfortunately, using this option results in 'git log' showing an extra copy of every new commit that was created (the original, and the synthetic one). + -If you do all your merges with '--squash', don't use '--rejoin' when you -split, because you don't want the subproject's history to be part of -your project anyway. +If you do all your merges with '--squash', make sure you also use +'--squash' when you 'split --rejoin'. EXAMPLE 1. 'add' command
contrib/subtree/t/t7900-subtree.sh +56 -0
62 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v3 28/30 subtree: allow 'split' flags to be passed to 'push'
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
'push' does a 'split' internally, but it doesn't pass flags through to the 'split'. This is silly, if you need to pass flags to 'split', then it means that you can't use 'push'!
So, have 'push' accept 'split' flags, and pass them through to 'split'.
Add tests for this by copying split's tests with minimal modification.
---
v2:
- In the added tests, use test_must_fail instead of !, as appropriate.
contrib/subtree/git-subtree.sh | 6 +- contrib/subtree/git-subtree.txt | 15 +- contrib/subtree/t/t7900-subtree.sh | 214 ++++++++++++++++++++++++++++- 3 files changed, 223 insertions(+), 12 deletions(-)
contrib/subtree/git-subtree.sh +3 -3
@@ -33,13 +33,13 @@ h,help show the help q quiet d show debug messages P,prefix= the name of the subdir to split out - options for 'split' + options for 'split' (also: 'push') annotate= add a prefix to commit message of new commits b,branch= create a new branch from the split subtree ignore-joins ignore prior --rejoin commits onto= try connecting new tree to an existing one rejoin merge the new branch back into HEAD - options for 'add' and 'merge' (also: 'pull' and 'split --rejoin') + options for 'add' and 'merge' (also: 'pull', 'split --rejoin', and 'push --rejoin') squash merge subtree changes as a single commit m,message= use the given message as the commit message for the merge commit "
@@ -964,7 +964,7 @@ cmd_push () { repository=$1 refspec=$2 echo "git push using: " "$repository" "$refspec" - localrev=$(git subtree split --prefix="$arg_prefix") || die + localrev=$(cmd_split) || die git push "$repository" "$localrev":"refs/heads/$refspec" else die "'$dir' must already exist. Try 'git subtree add'."
contrib/subtree/git-subtree.txt +8 -7
@@ -137,11 +137,11 @@ OPTIONS FOR ALL COMMANDS want to manipulate. This option is mandatory for all commands. -OPTIONS FOR 'add' AND 'merge' (ALSO: 'pull' AND 'split --rejoin') ------------------------------------------------------------------ +OPTIONS FOR 'add' AND 'merge' (ALSO: 'pull', 'split --rejoin', AND 'push --rejoin') +----------------------------------------------------------------------------------- These options for 'add' and 'merge' may also be given to 'pull' (which -wraps 'merge') and 'split --rejoin' (which wraps either 'add' or -'merge' as appropriate). +wraps 'merge'), 'split --rejoin' (which wraps either 'add' or 'merge' +as appropriate), and 'push --rejoin' (which wraps 'split --rejoin'). --squash:: Instead of merging the entire history from the subtree project, produce
@@ -173,9 +173,10 @@ subproject. --message=<message>:: Specify <message> as the commit message for the merge commit. -OPTIONS FOR 'split' -------------------- -These options are only valid for 'split'. +OPTIONS FOR 'split' (ALSO: 'push') +---------------------------------- +These options for 'split' may also be given to 'push' (which wraps +'split'). --annotate=<annotation>:: When generating synthetic history, add <annotation> as a prefix to each
contrib/subtree/t/t7900-subtree.sh +212 -2
226 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v3 29/30 subtree: push: allow specifying a local rev other than HEAD
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
'git subtree split' lets you specify a rev other than HEAD. 'git push' lets you specify a mapping between a local thing and a remot ref. So smash those together, and have 'git subtree push' let you specify which local thing to run split on and push the result of that split to the remote ref.
--- contrib/subtree/git-subtree.sh | 24 +++++++++++++++++------- contrib/subtree/git-subtree.txt | 14 ++++++++------ contrib/subtree/t/t7900-subtree.sh | 22 ++++++++++++++++++++++ 3 files changed, 47 insertions(+), 13 deletions(-)
contrib/subtree/git-subtree.sh +17 -7
@@ -27,7 +27,7 @@ git subtree add --prefix=<prefix> <repository> <ref> git subtree merge --prefix=<prefix> <commit> git subtree split --prefix=<prefix> [<commit>] git subtree pull --prefix=<prefix> <repository> <ref> -git subtree push --prefix=<prefix> <repository> <ref> +git subtree push --prefix=<prefix> <repository> <refspec> -- h,help show the help q quiet
@@ -952,20 +952,30 @@ cmd_pull () { cmd_merge FETCH_HEAD } -# Usage: cmd_push REPOSITORY REMOTEREF +# Usage: cmd_push REPOSITORY [+][LOCALREV:]REMOTEREF cmd_push () { if test $# -ne 2 then - die "You must provide <repository> <ref>" + die "You must provide <repository> <refspec>" fi - ensure_valid_ref_format "$2" if test -e "$dir" then repository=$1 - refspec=$2 + refspec=${2#+} + remoteref=${refspec#*:} + if test "$remoteref" = "$refspec" + then + localrevname_presplit=HEAD + else + localrevname_presplit=${refspec%%:*} + fi + ensure_valid_ref_format "$remoteref" + localrev_presplit=$(git rev-parse -q --verify "$localrevname_presplit^{commit}") || + die "'$localrevname_presplit' does not refer to a commit" + echo "git push using: " "$repository" "$refspec" - localrev=$(cmd_split) || die - git push "$repository" "$localrev":"refs/heads/$refspec" + localrev=$(cmd_split "$localrev_presplit") || die + git push "$repository" "$localrev":"refs/heads/$remoteref" else die "'$dir' must already exist. Try 'git subtree add'." fi
contrib/subtree/git-subtree.txt +8 -6
@@ -16,7 +16,7 @@ SYNOPSIS [verse] 'git subtree' [<options>] -P <prefix> pull <repository> <remote-ref> -'git subtree' [<options>] -P <prefix> push <repository> <remote-ref> +'git subtree' [<options>] -P <prefix> push <repository> <refspec> DESCRIPTION -----------
@@ -115,11 +115,13 @@ pull <repository> <remote-ref>:: it fetches the given ref from the specified remote repository. -push <repository> <remote-ref>:: - Does a 'split' using the <prefix> subtree of HEAD and then - does a 'git push' to push the result to the <repository> and - <remote-ref>. This can be used to push your subtree to - different branches of the remote repository. +push <repository> [+][<local-commit>:]<remote-ref>:: + Does a 'split' using the <prefix> subtree of <local-commit> + and then does a 'git push' to push the result to the + <repository> and <remote-ref>. This can be used to push your + subtree to different branches of the remote repository. Just + as with 'split', if no <local-commit> is given, then HEAD is + used. The optional leading '+' is ignored. OPTIONS FOR ALL COMMANDS ------------------------
contrib/subtree/t/t7900-subtree.sh +22 -0
@@ -820,6 +820,28 @@ test_expect_success 'push "sub dir"/ with --branch for an incompatible branch' ' ) ' +test_expect_success 'push "sub dir"/ with a local rev' ' + subtree_test_create_repo "$test_count" && + subtree_test_create_repo "$test_count/sub proj" && + test_create_commit "$test_count" main1 && + test_create_commit "$test_count/sub proj" sub1 && + ( + cd "$test_count" && + git fetch ./"sub proj" HEAD && + git subtree add --prefix="sub dir" FETCH_HEAD + ) && + test_create_commit "$test_count" "sub dir"/main-sub1 && + test_create_commit "$test_count" "sub dir"/main-sub2 && + ( + cd "$test_count" && + bad_tree=$(git rev-parse --verify HEAD:"sub dir") && + good_tree=$(git rev-parse --verify HEAD^:"sub dir") && + git subtree push --prefix="sub dir" --annotate="*" ./"sub proj" HEAD^:from-mainline && + split_tree=$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline:) && + test "$split_tree" = "$good_tree" + ) +' + # # Validity checking #
Signature · 1 line
2.31.1
PATCH v3 30/30 subtree: be stricter about validating flags
In reply to Luke ShumakerFrom: Luke Shumaker <[email protected]>
Don't silently ignore a flag that's invalid for a given subcommand. The user expected it to do something; we should tell the user that they are mistaken, instead of surprising the user.
It could be argued that this change might break existing users. I'd argue that those existing users are already broken, and they just don't know it. Let them know that they're broken.
--- contrib/subtree/git-subtree.sh | 89 ++++++++++++++++------- contrib/subtree/t/t7900-subtree.sh | 111 +++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+), 25 deletions(-)
contrib/subtree/git-subtree.sh +64 -25
@@ -44,17 +44,6 @@ squash merge subtree changes as a single commit m,message= use the given message as the commit message for the merge commit " -arg_debug= -arg_command= -arg_prefix= -arg_split_branch= -arg_split_onto= -arg_split_rejoin= -arg_split_ignore_joins= -arg_split_annotate= -arg_addmerge_squash= -arg_addmerge_message= - indent=0 # Usage: debug [MSG...]
@@ -106,10 +95,61 @@ main () { then set -- -h fi - eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)" + set_args="$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)" + eval "$set_args" . git-sh-setup require_work_tree + # First figure out the command and whether we use --rejoin, so + # that we can provide more helpful validation when we do the + # "real" flag parsing. + arg_split_rejoin= + allow_split= + allow_addmerge= + while test $# -gt 0 + do + opt="$1" + shift + case "$opt" in + --annotate|-b|-P|-m|--onto) + shift + ;; + --rejoin) + arg_split_rejoin=1 + ;; + --no-rejoin) + arg_split_rejoin= + ;; + --) + break + ;; + esac + done + arg_command=$1 + case "$arg_command" in + add|merge|pull) + allow_addmerge=1 + ;; + split|push) + allow_split=1 + allow_addmerge=$arg_split_rejoin + ;; + *) + die "Unknown command '$arg_command'" + ;; + esac + # Reset the arguments array for "real" flag parsing. + eval "$set_args" + + # Begin "real" flag parsing. + arg_debug= + arg_prefix= + arg_split_branch= + arg_split_onto= + arg_split_ignore_joins= + arg_split_annotate= + arg_addmerge_squash= + arg_addmerge_message= while test $# -gt 0 do opt="$1"
@@ -123,13 +163,16 @@ main () { arg_debug=1 ;; --annotate) + test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." arg_split_annotate="$1" shift ;; --no-annotate) + test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." arg_split_annotate= ;; -b) + test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." arg_split_branch="$1" shift ;;
@@ -138,6 +181,7 @@ main () { shift ;; -m) + test -n "$allow_addmerge" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." arg_addmerge_message="$1" shift ;;
@@ -145,28 +189,34 @@ main () { arg_prefix= ;; --onto) + test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." arg_split_onto="$1" shift ;; --no-onto) + test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." arg_split_onto= ;; --rejoin) - arg_split_rejoin=1 + test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." ;; --no-rejoin) - arg_split_rejoin= + test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." ;; --ignore-joins) + test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." arg_split_ignore_joins=1 ;; --no-ignore-joins) + test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." arg_split_ignore_joins= ;; --squash) + test -n "$allow_addmerge" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." arg_addmerge_squash=1 ;; --no-squash) + test -n "$allow_addmerge" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." arg_addmerge_squash= ;; --)
@@ -177,19 +227,8 @@ main () { ;; esac done - - arg_command="$1" shift - case "$arg_command" in - add|merge|pull|split|push) - : - ;; - *) - die "Unknown command '$arg_command'" - ;; - esac - if test -z "$arg_prefix" then die "You must provide the --prefix option."
contrib/subtree/t/t7900-subtree.sh +111 -0
147 lines not on this page · raw message
Signature · 1 line
2.31.1
A message that is not in the archive (sent off-list or not yet received): CAAgkN4cKUSPKpwuaLG-vR5Z7WFUZ81QXuRcsX-10obaRBAvwBA@mail.gmail.com
Re: [PATCH v3 30/30] subtree: be stricter about validating flags
Quoted 8 lines · Jakub Suder
Sorry for the contributing to the problem, but whom do I have to bribe to finally stop getting these emails that started filling my inbox last week?… I have a vague memory that I might have done something with git-subtree once a long time ago, but I'm not generally a contributor to git and I don't know what I'm doing here or how to get out…
JS
My apologies. I'll stop CC'ing you on my emails and further revisions of the patcheset. You may yet get a few straggling emails as others reply-all to emails that have already been sent (unless they manually remove you from the CC list).
As explanation (but not excuse): The usual etiquette is to CC recent or substantial contributors to the code area, and because subtree hasn't been touched in a while, I reached back further in to the history than usual when creating the CC list.
Again, I'm sorry for bothering you.
Signature · 2 lines
Happy hacking, ~ Luke Shumaker