Git fork

Merge branch 'ls/subtree'

"git subtree" updates.

* ls/subtree: (30 commits)
subtree: be stricter about validating flags
subtree: push: allow specifying a local rev other than HEAD
subtree: allow 'split' flags to be passed to 'push'
subtree: allow --squash to be used with --rejoin
subtree: give the docs a once-over
subtree: have $indent actually affect indentation
subtree: don't let debug and progress output clash
subtree: add comments and sanity checks
subtree: remove duplicate check
subtree: parse revs in individual cmd_ functions
subtree: use "^{commit}" instead of "^0"
subtree: don't fuss with PATH
subtree: use "$*" instead of "$@" as appropriate
subtree: use more explicit variable names for cmdline args
subtree: use git-sh-setup's `say`
subtree: use `git merge-base --is-ancestor`
subtree: drop support for git < 1.7
subtree: more consistent error propagation
subtree: don't have loose code outside of a function
subtree: t7900: add porcelain tests for 'pull' and 'push'
...

+1421 -862
+1
.gitignore
··· 163 163 /git-stripspace 164 164 /git-submodule 165 165 /git-submodule--helper 166 + /git-subtree 166 167 /git-svn 167 168 /git-switch 168 169 /git-symbolic-ref
+380 -258
contrib/subtree/git-subtree.sh
··· 4 4 # 5 5 # Copyright (C) 2009 Avery Pennarun <apenwarr@gmail.com> 6 6 # 7 - if test $# -eq 0 7 + 8 + if test -z "$GIT_EXEC_PATH" || test "${PATH#"${GIT_EXEC_PATH}:"}" = "$PATH" || ! test -f "$GIT_EXEC_PATH/git-sh-setup" 8 9 then 9 - set -- -h 10 + echo >&2 'It looks like either your git installation or your' 11 + echo >&2 'git-subtree installation is broken.' 12 + echo >&2 13 + echo >&2 "Tips:" 14 + echo >&2 " - If \`git --exec-path\` does not print the correct path to" 15 + echo >&2 " your git install directory, then set the GIT_EXEC_PATH" 16 + echo >&2 " environment variable to the correct directory." 17 + echo >&2 " - Make sure that your \`${0##*/}\` file is either in your" 18 + echo >&2 " PATH or in your git exec path (\`$(git --exec-path)\`)." 19 + echo >&2 " - You should run git-subtree as \`git ${0##*/git-}\`," 20 + echo >&2 " not as \`${0##*/}\`." >&2 21 + exit 126 10 22 fi 23 + 11 24 OPTS_SPEC="\ 12 25 git subtree add --prefix=<prefix> <commit> 13 26 git subtree add --prefix=<prefix> <repository> <ref> 14 27 git subtree merge --prefix=<prefix> <commit> 28 + git subtree split --prefix=<prefix> [<commit>] 15 29 git subtree pull --prefix=<prefix> <repository> <ref> 16 - git subtree push --prefix=<prefix> <repository> <ref> 17 - git subtree split --prefix=<prefix> <commit> 30 + git subtree push --prefix=<prefix> <repository> <refspec> 18 31 -- 19 32 h,help show the help 20 33 q quiet 21 34 d show debug messages 22 35 P,prefix= the name of the subdir to split out 23 - m,message= use the given message as the commit message for the merge commit 24 - options for 'split' 36 + options for 'split' (also: 'push') 25 37 annotate= add a prefix to commit message of new commits 26 38 b,branch= create a new branch from the split subtree 27 39 ignore-joins ignore prior --rejoin commits 28 40 onto= try connecting new tree to an existing one 29 41 rejoin merge the new branch back into HEAD 30 - options for 'add', 'merge', and 'pull' 42 + options for 'add' and 'merge' (also: 'pull', 'split --rejoin', and 'push --rejoin') 31 43 squash merge subtree changes as a single commit 44 + m,message= use the given message as the commit message for the merge commit 32 45 " 33 - eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)" 34 46 35 - PATH=$PATH:$(git --exec-path) 36 - . git-sh-setup 37 - 38 - require_work_tree 39 - 40 - quiet= 41 - branch= 42 - debug= 43 - command= 44 - onto= 45 - rejoin= 46 - ignore_joins= 47 - annotate= 48 - squash= 49 - message= 50 - prefix= 47 + indent=0 51 48 49 + # Usage: debug [MSG...] 52 50 debug () { 53 - if test -n "$debug" 54 - then 55 - printf "%s\n" "$*" >&2 56 - fi 57 - } 58 - 59 - say () { 60 - if test -z "$quiet" 51 + if test -n "$arg_debug" 61 52 then 62 - printf "%s\n" "$*" >&2 53 + printf "%$(($indent * 2))s%s\n" '' "$*" >&2 63 54 fi 64 55 } 65 56 57 + # Usage: progress [MSG...] 66 58 progress () { 67 - if test -z "$quiet" 59 + if test -z "$GIT_QUIET" 68 60 then 69 - printf "%s\r" "$*" >&2 61 + if test -z "$arg_debug" 62 + then 63 + # Debug mode is off. 64 + # 65 + # Print one progress line that we keep updating (use 66 + # "\r" to return to the beginning of the line, rather 67 + # than "\n" to start a new line). This only really 68 + # works when stderr is a terminal. 69 + printf "%s\r" "$*" >&2 70 + else 71 + # Debug mode is on. The `debug` function is regularly 72 + # printing to stderr. 73 + # 74 + # Don't do the one-line-with-"\r" thing, because on a 75 + # terminal the debug output would overwrite and hide the 76 + # progress output. Add a "progress:" prefix to make the 77 + # progress output and the debug output easy to 78 + # distinguish. This ensures maximum readability whether 79 + # stderr is a terminal or a file. 80 + printf "progress: %s\n" "$*" >&2 81 + fi 70 82 fi 71 83 } 72 84 85 + # Usage: assert CMD... 73 86 assert () { 74 87 if ! "$@" 75 88 then 76 - die "assertion failed: " "$@" 89 + die "assertion failed: $*" 77 90 fi 78 91 } 79 92 80 - ensure_single_rev () { 81 - if test $# -ne 1 93 + main () { 94 + if test $# -eq 0 82 95 then 83 - die "You must provide exactly one revision. Got: '$@'" 96 + set -- -h 84 97 fi 85 - } 86 - 87 - while test $# -gt 0 88 - do 89 - opt="$1" 90 - shift 98 + set_args="$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)" 99 + eval "$set_args" 100 + . git-sh-setup 101 + require_work_tree 91 102 92 - case "$opt" in 93 - -q) 94 - quiet=1 95 - ;; 96 - -d) 97 - debug=1 98 - ;; 99 - --annotate) 100 - annotate="$1" 101 - shift 102 - ;; 103 - --no-annotate) 104 - annotate= 105 - ;; 106 - -b) 107 - branch="$1" 108 - shift 109 - ;; 110 - -P) 111 - prefix="${1%/}" 112 - shift 113 - ;; 114 - -m) 115 - message="$1" 103 + # First figure out the command and whether we use --rejoin, so 104 + # that we can provide more helpful validation when we do the 105 + # "real" flag parsing. 106 + arg_split_rejoin= 107 + allow_split= 108 + allow_addmerge= 109 + while test $# -gt 0 110 + do 111 + opt="$1" 116 112 shift 113 + case "$opt" in 114 + --annotate|-b|-P|-m|--onto) 115 + shift 116 + ;; 117 + --rejoin) 118 + arg_split_rejoin=1 119 + ;; 120 + --no-rejoin) 121 + arg_split_rejoin= 122 + ;; 123 + --) 124 + break 125 + ;; 126 + esac 127 + done 128 + arg_command=$1 129 + case "$arg_command" in 130 + add|merge|pull) 131 + allow_addmerge=1 117 132 ;; 118 - --no-prefix) 119 - prefix= 120 - ;; 121 - --onto) 122 - onto="$1" 123 - shift 124 - ;; 125 - --no-onto) 126 - onto= 127 - ;; 128 - --rejoin) 129 - rejoin=1 130 - ;; 131 - --no-rejoin) 132 - rejoin= 133 - ;; 134 - --ignore-joins) 135 - ignore_joins=1 136 - ;; 137 - --no-ignore-joins) 138 - ignore_joins= 139 - ;; 140 - --squash) 141 - squash=1 142 - ;; 143 - --no-squash) 144 - squash= 145 - ;; 146 - --) 147 - break 133 + split|push) 134 + allow_split=1 135 + allow_addmerge=$arg_split_rejoin 148 136 ;; 149 137 *) 150 - die "Unexpected option: $opt" 138 + die "Unknown command '$arg_command'" 151 139 ;; 152 140 esac 153 - done 141 + # Reset the arguments array for "real" flag parsing. 142 + eval "$set_args" 154 143 155 - command="$1" 156 - shift 144 + # Begin "real" flag parsing. 145 + arg_debug= 146 + arg_prefix= 147 + arg_split_branch= 148 + arg_split_onto= 149 + arg_split_ignore_joins= 150 + arg_split_annotate= 151 + arg_addmerge_squash= 152 + arg_addmerge_message= 153 + while test $# -gt 0 154 + do 155 + opt="$1" 156 + shift 157 157 158 - case "$command" in 159 - add|merge|pull) 160 - default= 161 - ;; 162 - split|push) 163 - default="--default HEAD" 164 - ;; 165 - *) 166 - die "Unknown command '$command'" 167 - ;; 168 - esac 158 + case "$opt" in 159 + -q) 160 + GIT_QUIET=1 161 + ;; 162 + -d) 163 + arg_debug=1 164 + ;; 165 + --annotate) 166 + test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." 167 + arg_split_annotate="$1" 168 + shift 169 + ;; 170 + --no-annotate) 171 + test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." 172 + arg_split_annotate= 173 + ;; 174 + -b) 175 + test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." 176 + arg_split_branch="$1" 177 + shift 178 + ;; 179 + -P) 180 + arg_prefix="${1%/}" 181 + shift 182 + ;; 183 + -m) 184 + test -n "$allow_addmerge" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." 185 + arg_addmerge_message="$1" 186 + shift 187 + ;; 188 + --no-prefix) 189 + arg_prefix= 190 + ;; 191 + --onto) 192 + test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." 193 + arg_split_onto="$1" 194 + shift 195 + ;; 196 + --no-onto) 197 + test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." 198 + arg_split_onto= 199 + ;; 200 + --rejoin) 201 + test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." 202 + ;; 203 + --no-rejoin) 204 + test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." 205 + ;; 206 + --ignore-joins) 207 + test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." 208 + arg_split_ignore_joins=1 209 + ;; 210 + --no-ignore-joins) 211 + test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." 212 + arg_split_ignore_joins= 213 + ;; 214 + --squash) 215 + test -n "$allow_addmerge" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." 216 + arg_addmerge_squash=1 217 + ;; 218 + --no-squash) 219 + test -n "$allow_addmerge" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'." 220 + arg_addmerge_squash= 221 + ;; 222 + --) 223 + break 224 + ;; 225 + *) 226 + die "Unexpected option: $opt" 227 + ;; 228 + esac 229 + done 230 + shift 169 231 170 - if test -z "$prefix" 171 - then 172 - die "You must provide the --prefix option." 173 - fi 232 + if test -z "$arg_prefix" 233 + then 234 + die "You must provide the --prefix option." 235 + fi 174 236 175 - case "$command" in 176 - add) 177 - test -e "$prefix" && 178 - die "prefix '$prefix' already exists." 179 - ;; 180 - *) 181 - test -e "$prefix" || 182 - die "'$prefix' does not exist; use 'git subtree add'" 183 - ;; 184 - esac 237 + case "$arg_command" in 238 + add) 239 + test -e "$arg_prefix" && 240 + die "prefix '$arg_prefix' already exists." 241 + ;; 242 + *) 243 + test -e "$arg_prefix" || 244 + die "'$arg_prefix' does not exist; use 'git subtree add'" 245 + ;; 246 + esac 185 247 186 - dir="$(dirname "$prefix/.")" 248 + dir="$(dirname "$arg_prefix/.")" 187 249 188 - if test "$command" != "pull" && 189 - test "$command" != "add" && 190 - test "$command" != "push" 191 - then 192 - revs=$(git rev-parse $default --revs-only "$@") || exit $? 193 - dirs=$(git rev-parse --no-revs --no-flags "$@") || exit $? 194 - ensure_single_rev $revs 195 - if test -n "$dirs" 196 - then 197 - die "Error: Use --prefix instead of bare filenames." 198 - fi 199 - fi 250 + debug "command: {$arg_command}" 251 + debug "quiet: {$GIT_QUIET}" 252 + debug "dir: {$dir}" 253 + debug "opts: {$*}" 254 + debug 200 255 201 - debug "command: {$command}" 202 - debug "quiet: {$quiet}" 203 - debug "revs: {$revs}" 204 - debug "dir: {$dir}" 205 - debug "opts: {$*}" 206 - debug 256 + "cmd_$arg_command" "$@" 257 + } 207 258 259 + # Usage: cache_setup 208 260 cache_setup () { 261 + assert test $# = 0 209 262 cachedir="$GIT_DIR/subtree-cache/$$" 210 263 rm -rf "$cachedir" || 211 264 die "Can't delete old cachedir: $cachedir" ··· 216 269 debug "Using cachedir: $cachedir" >&2 217 270 } 218 271 272 + # Usage: cache_get [REVS...] 219 273 cache_get () { 220 274 for oldrev in "$@" 221 275 do ··· 227 281 done 228 282 } 229 283 284 + # Usage: cache_miss [REVS...] 230 285 cache_miss () { 231 286 for oldrev in "$@" 232 287 do ··· 237 292 done 238 293 } 239 294 295 + # Usage: check_parents PARENTS_EXPR 240 296 check_parents () { 241 - missed=$(cache_miss "$1") 242 - local indent=$(($2 + 1)) 297 + assert test $# = 1 298 + missed=$(cache_miss "$1") || exit $? 299 + local indent=$(($indent + 1)) 243 300 for miss in $missed 244 301 do 245 302 if ! test -r "$cachedir/notree/$miss" 246 303 then 247 - debug " incorrect order: $miss" 248 - process_split_commit "$miss" "" "$indent" 304 + debug "incorrect order: $miss" 305 + process_split_commit "$miss" "" 249 306 fi 250 307 done 251 308 } 252 309 310 + # Usage: set_notree REV 253 311 set_notree () { 312 + assert test $# = 1 254 313 echo "1" > "$cachedir/notree/$1" 255 314 } 256 315 316 + # Usage: cache_set OLDREV NEWREV 257 317 cache_set () { 318 + assert test $# = 2 258 319 oldrev="$1" 259 320 newrev="$2" 260 321 if test "$oldrev" != "latest_old" && ··· 266 327 echo "$newrev" >"$cachedir/$oldrev" 267 328 } 268 329 330 + # Usage: rev_exists REV 269 331 rev_exists () { 332 + assert test $# = 1 270 333 if git rev-parse "$1" >/dev/null 2>&1 271 334 then 272 335 return 0 ··· 275 338 fi 276 339 } 277 340 278 - rev_is_descendant_of_branch () { 279 - newrev="$1" 280 - branch="$2" 281 - branch_hash=$(git rev-parse "$branch") 282 - match=$(git rev-list -1 "$branch_hash" "^$newrev") 283 - 284 - if test -z "$match" 285 - then 286 - return 0 287 - else 288 - return 1 289 - fi 290 - } 291 - 292 - # if a commit doesn't have a parent, this might not work. But we only want 341 + # Usage: try_remove_previous REV 342 + # 343 + # If a commit doesn't have a parent, this might not work. But we only want 293 344 # to remove the parent from the rev-list, and since it doesn't exist, it won't 294 345 # be there anyway, so do nothing in that case. 295 346 try_remove_previous () { 347 + assert test $# = 1 296 348 if rev_exists "$1^" 297 349 then 298 350 echo "^$1^" 299 351 fi 300 352 } 301 353 354 + # Usage: find_latest_squash DIR 302 355 find_latest_squash () { 356 + assert test $# = 1 303 357 debug "Looking for latest squash ($dir)..." 358 + local indent=$(($indent + 1)) 359 + 304 360 dir="$1" 305 361 sq= 306 362 main= ··· 319 375 main="$b" 320 376 ;; 321 377 git-subtree-split:) 322 - sub="$(git rev-parse "$b^0")" || 378 + sub="$(git rev-parse "$b^{commit}")" || 323 379 die "could not rev-parse split hash $b from commit $sq" 324 380 ;; 325 381 END) ··· 329 385 then 330 386 # a rejoin commit? 331 387 # Pretend its sub was a squash. 332 - sq="$sub" 388 + sq=$(git rev-parse --verify "$sq^2") || 389 + die 333 390 fi 334 391 debug "Squash found: $sq $sub" 335 392 echo "$sq" "$sub" ··· 340 397 sub= 341 398 ;; 342 399 esac 343 - done 400 + done || exit $? 344 401 } 345 402 403 + # Usage: find_existing_splits DIR REV 346 404 find_existing_splits () { 405 + assert test $# = 2 347 406 debug "Looking for prior splits..." 407 + local indent=$(($indent + 1)) 408 + 348 409 dir="$1" 349 - revs="$2" 410 + rev="$2" 350 411 main= 351 412 sub= 352 413 local grep_format="^git-subtree-dir: $dir/*\$" 353 - if test -n "$ignore_joins" 414 + if test -n "$arg_split_ignore_joins" 354 415 then 355 416 grep_format="^Add '$dir/' from commit '" 356 417 fi 357 418 git log --grep="$grep_format" \ 358 - --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs | 419 + --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' "$rev" | 359 420 while read a b junk 360 421 do 361 422 case "$a" in ··· 366 427 main="$b" 367 428 ;; 368 429 git-subtree-split:) 369 - sub="$(git rev-parse "$b^0")" || 430 + sub="$(git rev-parse "$b^{commit}")" || 370 431 die "could not rev-parse split hash $b from commit $sq" 371 432 ;; 372 433 END) 373 - debug " Main is: '$main'" 434 + debug "Main is: '$main'" 374 435 if test -z "$main" -a -n "$sub" 375 436 then 376 437 # squash commits refer to a subtree ··· 389 450 sub= 390 451 ;; 391 452 esac 392 - done 453 + done || exit $? 393 454 } 394 455 456 + # Usage: copy_commit REV TREE FLAGS_STR 395 457 copy_commit () { 458 + assert test $# = 3 396 459 # We're going to set some environment vars here, so 397 460 # do it in a subshell to get rid of them safely later 398 461 debug copy_commit "{$1}" "{$2}" "{$3}" ··· 411 474 GIT_COMMITTER_EMAIL \ 412 475 GIT_COMMITTER_DATE 413 476 ( 414 - printf "%s" "$annotate" 477 + printf "%s" "$arg_split_annotate" 415 478 cat 416 479 ) | 417 480 git commit-tree "$2" $3 # reads the rest of stdin 418 481 ) || die "Can't copy commit $1" 419 482 } 420 483 484 + # Usage: add_msg DIR LATEST_OLD LATEST_NEW 421 485 add_msg () { 486 + assert test $# = 3 422 487 dir="$1" 423 488 latest_old="$2" 424 489 latest_new="$3" 425 - if test -n "$message" 490 + if test -n "$arg_addmerge_message" 426 491 then 427 - commit_message="$message" 492 + commit_message="$arg_addmerge_message" 428 493 else 429 494 commit_message="Add '$dir/' from commit '$latest_new'" 430 495 fi 496 + if test -n "$arg_split_rejoin" 497 + then 498 + # If this is from a --rejoin, then rejoin_msg has 499 + # already inserted the `git-subtree-xxx:` tags 500 + echo "$commit_message" 501 + return 502 + fi 431 503 cat <<-EOF 432 504 $commit_message 433 505 ··· 437 509 EOF 438 510 } 439 511 512 + # Usage: add_squashed_msg REV DIR 440 513 add_squashed_msg () { 441 - if test -n "$message" 514 + assert test $# = 2 515 + if test -n "$arg_addmerge_message" 442 516 then 443 - echo "$message" 517 + echo "$arg_addmerge_message" 444 518 else 445 519 echo "Merge commit '$1' as '$2'" 446 520 fi 447 521 } 448 522 523 + # Usage: rejoin_msg DIR LATEST_OLD LATEST_NEW 449 524 rejoin_msg () { 525 + assert test $# = 3 450 526 dir="$1" 451 527 latest_old="$2" 452 528 latest_new="$3" 453 - if test -n "$message" 529 + if test -n "$arg_addmerge_message" 454 530 then 455 - commit_message="$message" 531 + commit_message="$arg_addmerge_message" 456 532 else 457 533 commit_message="Split '$dir/' into commit '$latest_new'" 458 534 fi ··· 465 541 EOF 466 542 } 467 543 544 + # Usage: squash_msg DIR OLD_SUBTREE_COMMIT NEW_SUBTREE_COMMIT 468 545 squash_msg () { 546 + assert test $# = 3 469 547 dir="$1" 470 548 oldsub="$2" 471 549 newsub="$3" ··· 487 565 echo "git-subtree-split: $newsub" 488 566 } 489 567 568 + # Usage: toptree_for_commit COMMIT 490 569 toptree_for_commit () { 570 + assert test $# = 1 491 571 commit="$1" 492 572 git rev-parse --verify "$commit^{tree}" || exit $? 493 573 } 494 574 575 + # Usage: subtree_for_commit COMMIT DIR 495 576 subtree_for_commit () { 577 + assert test $# = 2 496 578 commit="$1" 497 579 dir="$2" 498 580 git ls-tree "$commit" -- "$dir" | ··· 503 585 test "$type" = "commit" && continue # ignore submodules 504 586 echo $tree 505 587 break 506 - done 588 + done || exit $? 507 589 } 508 590 591 + # Usage: tree_changed TREE [PARENTS...] 509 592 tree_changed () { 593 + assert test $# -gt 0 510 594 tree=$1 511 595 shift 512 596 if test $# -ne 1 513 597 then 514 598 return 0 # weird parents, consider it changed 515 599 else 516 - ptree=$(toptree_for_commit $1) 600 + ptree=$(toptree_for_commit $1) || exit $? 517 601 if test "$ptree" != "$tree" 518 602 then 519 603 return 0 # changed ··· 523 607 fi 524 608 } 525 609 610 + # Usage: new_squash_commit OLD_SQUASHED_COMMIT OLD_NONSQUASHED_COMMIT NEW_NONSQUASHED_COMMIT 526 611 new_squash_commit () { 612 + assert test $# = 3 527 613 old="$1" 528 614 oldsub="$2" 529 615 newsub="$3" ··· 538 624 fi 539 625 } 540 626 627 + # Usage: copy_or_skip REV TREE NEWPARENTS 541 628 copy_or_skip () { 629 + assert test $# = 3 542 630 rev="$1" 543 631 tree="$2" 544 632 newparents="$3" ··· 613 701 fi 614 702 } 615 703 704 + # Usage: ensure_clean 616 705 ensure_clean () { 706 + assert test $# = 0 617 707 if ! git diff-index HEAD --exit-code --quiet 2>&1 618 708 then 619 709 die "Working tree has modifications. Cannot add." ··· 624 714 fi 625 715 } 626 716 717 + # Usage: ensure_valid_ref_format REF 627 718 ensure_valid_ref_format () { 719 + assert test $# = 1 628 720 git check-ref-format "refs/heads/$1" || 629 721 die "'$1' does not look like a ref" 630 722 } 631 723 724 + # Usage: process_split_commit REV PARENTS 632 725 process_split_commit () { 726 + assert test $# = 2 633 727 local rev="$1" 634 728 local parents="$2" 635 - local indent=$3 636 729 637 730 if test $indent -eq 0 638 731 then ··· 647 740 progress "$revcount/$revmax ($createcount) [$extracount]" 648 741 649 742 debug "Processing commit: $rev" 650 - exists=$(cache_get "$rev") 743 + local indent=$(($indent + 1)) 744 + exists=$(cache_get "$rev") || exit $? 651 745 if test -n "$exists" 652 746 then 653 - debug " prior: $exists" 747 + debug "prior: $exists" 654 748 return 655 749 fi 656 750 createcount=$(($createcount + 1)) 657 - debug " parents: $parents" 658 - check_parents "$parents" "$indent" 659 - newparents=$(cache_get $parents) 660 - debug " newparents: $newparents" 751 + debug "parents: $parents" 752 + check_parents "$parents" 753 + newparents=$(cache_get $parents) || exit $? 754 + debug "newparents: $newparents" 661 755 662 - tree=$(subtree_for_commit "$rev" "$dir") 663 - debug " tree is: $tree" 756 + tree=$(subtree_for_commit "$rev" "$dir") || exit $? 757 + debug "tree is: $tree" 664 758 665 759 # ugly. is there no better way to tell if this is a subtree 666 760 # vs. a mainline commit? Does it matter? ··· 675 769 fi 676 770 677 771 newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $? 678 - debug " newrev is: $newrev" 772 + debug "newrev is: $newrev" 679 773 cache_set "$rev" "$newrev" 680 774 cache_set latest_new "$newrev" 681 775 cache_set latest_old "$rev" 682 776 } 683 777 778 + # Usage: cmd_add REV 779 + # Or: cmd_add REPOSITORY REF 684 780 cmd_add () { 685 - if test -e "$dir" 686 - then 687 - die "'$dir' already exists. Cannot add." 688 - fi 689 781 690 782 ensure_clean 691 783 ··· 707 799 708 800 cmd_add_repository "$@" 709 801 else 710 - say "error: parameters were '$@'" 802 + say >&2 "error: parameters were '$*'" 711 803 die "Provide either a commit or a repository and commit." 712 804 fi 713 805 } 714 806 807 + # Usage: cmd_add_repository REPOSITORY REFSPEC 715 808 cmd_add_repository () { 809 + assert test $# = 2 716 810 echo "git fetch" "$@" 717 811 repository=$1 718 812 refspec=$2 719 813 git fetch "$@" || exit $? 720 - revs=FETCH_HEAD 721 - set -- $revs 722 - cmd_add_commit "$@" 814 + cmd_add_commit FETCH_HEAD 723 815 } 724 816 817 + # Usage: cmd_add_commit REV 725 818 cmd_add_commit () { 726 - rev=$(git rev-parse $default --revs-only "$@") || exit $? 727 - ensure_single_rev $rev 819 + # The rev has already been validated by cmd_add(), we just 820 + # need to normalize it. 821 + assert test $# = 1 822 + rev=$(git rev-parse --verify "$1^{commit}") || exit $? 728 823 729 824 debug "Adding $dir as '$rev'..." 730 - git read-tree --prefix="$dir" $rev || exit $? 825 + if test -z "$arg_split_rejoin" 826 + then 827 + # Only bother doing this if this is a genuine 'add', 828 + # not a synthetic 'add' from '--rejoin'. 829 + git read-tree --prefix="$dir" $rev || exit $? 830 + fi 731 831 git checkout -- "$dir" || exit $? 732 832 tree=$(git write-tree) || exit $? 733 833 ··· 739 839 headp= 740 840 fi 741 841 742 - if test -n "$squash" 842 + if test -n "$arg_addmerge_squash" 743 843 then 744 844 rev=$(new_squash_commit "" "" "$rev") || exit $? 745 845 commit=$(add_squashed_msg "$rev" "$dir" | 746 846 git commit-tree "$tree" $headp -p "$rev") || exit $? 747 847 else 748 - revp=$(peel_committish "$rev") && 848 + revp=$(peel_committish "$rev") || exit $? 749 849 commit=$(add_msg "$dir" $headrev "$rev" | 750 850 git commit-tree "$tree" $headp -p "$revp") || exit $? 751 851 fi 752 852 git reset "$commit" || exit $? 753 853 754 - say "Added dir '$dir'" 854 + say >&2 "Added dir '$dir'" 755 855 } 756 856 857 + # Usage: cmd_split [REV] 757 858 cmd_split () { 859 + if test $# -eq 0 860 + then 861 + rev=$(git rev-parse HEAD) 862 + elif test $# -eq 1 863 + then 864 + rev=$(git rev-parse -q --verify "$1^{commit}") || 865 + die "'$1' does not refer to a commit" 866 + else 867 + die "You must provide exactly one revision. Got: '$*'" 868 + fi 869 + 870 + if test -n "$arg_split_rejoin" 871 + then 872 + ensure_clean 873 + fi 874 + 758 875 debug "Splitting $dir..." 759 876 cache_setup || exit $? 760 877 761 - if test -n "$onto" 878 + if test -n "$arg_split_onto" 762 879 then 763 - debug "Reading history for --onto=$onto..." 764 - git rev-list $onto | 880 + debug "Reading history for --onto=$arg_split_onto..." 881 + git rev-list $arg_split_onto | 765 882 while read rev 766 883 do 767 884 # the 'onto' history is already just the subdir, so 768 885 # any parent we find there can be used verbatim 769 - debug " cache: $rev" 886 + debug "cache: $rev" 770 887 cache_set "$rev" "$rev" 771 - done 888 + done || exit $? 772 889 fi 773 890 774 - unrevs="$(find_existing_splits "$dir" "$revs")" 891 + unrevs="$(find_existing_splits "$dir" "$rev")" || exit $? 775 892 776 893 # We can't restrict rev-list to only $dir here, because some of our 777 894 # parents have the $dir contents the root, and those won't match. 778 895 # (and rev-list --follow doesn't seem to solve this) 779 - grl='git rev-list --topo-order --reverse --parents $revs $unrevs' 896 + grl='git rev-list --topo-order --reverse --parents $rev $unrevs' 780 897 revmax=$(eval "$grl" | wc -l) 781 898 revcount=0 782 899 createcount=0 ··· 784 901 eval "$grl" | 785 902 while read rev parents 786 903 do 787 - process_split_commit "$rev" "$parents" 0 904 + process_split_commit "$rev" "$parents" 788 905 done || exit $? 789 906 790 - latest_new=$(cache_get latest_new) 907 + latest_new=$(cache_get latest_new) || exit $? 791 908 if test -z "$latest_new" 792 909 then 793 910 die "No new revisions were found" 794 911 fi 795 912 796 - if test -n "$rejoin" 913 + if test -n "$arg_split_rejoin" 797 914 then 798 915 debug "Merging split branch into HEAD..." 799 - latest_old=$(cache_get latest_old) 800 - git merge -s ours \ 801 - --allow-unrelated-histories \ 802 - -m "$(rejoin_msg "$dir" "$latest_old" "$latest_new")" \ 803 - "$latest_new" >&2 || exit $? 916 + latest_old=$(cache_get latest_old) || exit $? 917 + arg_addmerge_message="$(rejoin_msg "$dir" "$latest_old" "$latest_new")" || exit $? 918 + if test -z "$(find_latest_squash "$dir")" 919 + then 920 + cmd_add "$latest_new" >&2 || exit $? 921 + else 922 + cmd_merge "$latest_new" >&2 || exit $? 923 + fi 804 924 fi 805 - if test -n "$branch" 925 + if test -n "$arg_split_branch" 806 926 then 807 - if rev_exists "refs/heads/$branch" 927 + if rev_exists "refs/heads/$arg_split_branch" 808 928 then 809 - if ! rev_is_descendant_of_branch "$latest_new" "$branch" 929 + if ! git merge-base --is-ancestor "$arg_split_branch" "$latest_new" 810 930 then 811 - die "Branch '$branch' is not an ancestor of commit '$latest_new'." 931 + die "Branch '$arg_split_branch' is not an ancestor of commit '$latest_new'." 812 932 fi 813 933 action='Updated' 814 934 else 815 935 action='Created' 816 936 fi 817 937 git update-ref -m 'subtree split' \ 818 - "refs/heads/$branch" "$latest_new" || exit $? 819 - say "$action branch '$branch'" 938 + "refs/heads/$arg_split_branch" "$latest_new" || exit $? 939 + say >&2 "$action branch '$arg_split_branch'" 820 940 fi 821 941 echo "$latest_new" 822 942 exit 0 823 943 } 824 944 945 + # Usage: cmd_merge REV 825 946 cmd_merge () { 826 - rev=$(git rev-parse $default --revs-only "$@") || exit $? 827 - ensure_single_rev $rev 947 + test $# -eq 1 || 948 + die "You must provide exactly one revision. Got: '$*'" 949 + rev=$(git rev-parse -q --verify "$1^{commit}") || 950 + die "'$1' does not refer to a commit" 828 951 ensure_clean 829 952 830 - if test -n "$squash" 953 + if test -n "$arg_addmerge_squash" 831 954 then 832 - first_split="$(find_latest_squash "$dir")" 955 + first_split="$(find_latest_squash "$dir")" || exit $? 833 956 if test -z "$first_split" 834 957 then 835 958 die "Can't squash-merge: '$dir' was never added." ··· 839 962 sub=$2 840 963 if test "$sub" = "$rev" 841 964 then 842 - say "Subtree is already at commit $rev." 965 + say >&2 "Subtree is already at commit $rev." 843 966 exit 0 844 967 fi 845 968 new=$(new_squash_commit "$old" "$sub" "$rev") || exit $? ··· 847 970 rev="$new" 848 971 fi 849 972 850 - version=$(git version) 851 - if test "$version" \< "git version 1.7" 973 + if test -n "$arg_addmerge_message" 852 974 then 853 - if test -n "$message" 854 - then 855 - git merge -s subtree --message="$message" "$rev" 856 - else 857 - git merge -s subtree "$rev" 858 - fi 975 + git merge -Xsubtree="$arg_prefix" \ 976 + --message="$arg_addmerge_message" "$rev" 859 977 else 860 - if test -n "$message" 861 - then 862 - git merge -Xsubtree="$prefix" \ 863 - --message="$message" "$rev" 864 - else 865 - git merge -Xsubtree="$prefix" $rev 866 - fi 978 + git merge -Xsubtree="$arg_prefix" $rev 867 979 fi 868 980 } 869 981 982 + # Usage: cmd_pull REPOSITORY REMOTEREF 870 983 cmd_pull () { 871 984 if test $# -ne 2 872 985 then ··· 875 988 ensure_clean 876 989 ensure_valid_ref_format "$2" 877 990 git fetch "$@" || exit $? 878 - revs=FETCH_HEAD 879 - set -- $revs 880 - cmd_merge "$@" 991 + cmd_merge FETCH_HEAD 881 992 } 882 993 994 + # Usage: cmd_push REPOSITORY [+][LOCALREV:]REMOTEREF 883 995 cmd_push () { 884 996 if test $# -ne 2 885 997 then 886 - die "You must provide <repository> <ref>" 998 + die "You must provide <repository> <refspec>" 887 999 fi 888 - ensure_valid_ref_format "$2" 889 1000 if test -e "$dir" 890 1001 then 891 1002 repository=$1 892 - refspec=$2 1003 + refspec=${2#+} 1004 + remoteref=${refspec#*:} 1005 + if test "$remoteref" = "$refspec" 1006 + then 1007 + localrevname_presplit=HEAD 1008 + else 1009 + localrevname_presplit=${refspec%%:*} 1010 + fi 1011 + ensure_valid_ref_format "$remoteref" 1012 + localrev_presplit=$(git rev-parse -q --verify "$localrevname_presplit^{commit}") || 1013 + die "'$localrevname_presplit' does not refer to a commit" 1014 + 893 1015 echo "git push using: " "$repository" "$refspec" 894 - localrev=$(git subtree split --prefix="$prefix") || die 895 - git push "$repository" "$localrev":"refs/heads/$refspec" 1016 + localrev=$(cmd_split "$localrev_presplit") || die 1017 + git push "$repository" "$localrev":"refs/heads/$remoteref" 896 1018 else 897 1019 die "'$dir' must already exist. Try 'git subtree add'." 898 1020 fi 899 1021 } 900 1022 901 - "cmd_$command" "$@" 1023 + main "$@"
+91 -99
contrib/subtree/git-subtree.txt
··· 9 9 SYNOPSIS 10 10 -------- 11 11 [verse] 12 - 'git subtree' add -P <prefix> <commit> 13 - 'git subtree' add -P <prefix> <repository> <ref> 14 - 'git subtree' pull -P <prefix> <repository> <ref> 15 - 'git subtree' push -P <prefix> <repository> <ref> 16 - 'git subtree' merge -P <prefix> <commit> 17 - 'git subtree' split -P <prefix> [OPTIONS] [<commit>] 12 + 'git subtree' [<options>] -P <prefix> add <local-commit> 13 + 'git subtree' [<options>] -P <prefix> add <repository> <remote-ref> 14 + 'git subtree' [<options>] -P <prefix> merge <local-commit> 15 + 'git subtree' [<options>] -P <prefix> split [<local-commit>] 18 16 17 + [verse] 18 + 'git subtree' [<options>] -P <prefix> pull <repository> <remote-ref> 19 + 'git subtree' [<options>] -P <prefix> push <repository> <refspec> 19 20 20 21 DESCRIPTION 21 22 ----------- ··· 28 29 29 30 Subtrees are not to be confused with submodules, which are meant for 30 31 the same task. Unlike submodules, subtrees do not need any special 31 - constructions (like .gitmodules files or gitlinks) be present in 32 + constructions (like '.gitmodules' files or gitlinks) be present in 32 33 your repository, and do not force end-users of your 33 34 repository to do anything special or to understand how subtrees 34 35 work. A subtree is just a subdirectory that can be ··· 59 60 affects both the library and the main application, commit it in 60 61 two pieces. That way, when you split the library commits out 61 62 later, their descriptions will still make sense. But if this 62 - isn't important to you, it's not *necessary*. git subtree will 63 + isn't important to you, it's not *necessary*. 'git subtree' will 63 64 simply leave out the non-library-related parts of the commit 64 65 when it splits it out into the subproject later. 65 66 66 67 67 68 COMMANDS 68 69 -------- 69 - add:: 70 + add <local-commit>:: 71 + add <repository> <remote-ref>:: 70 72 Create the <prefix> subtree by importing its contents 71 - from the given <commit> or <repository> and remote <ref>. 73 + from the given <local-commit> or <repository> and <remote-ref>. 72 74 A new commit is created automatically, joining the imported 73 - project's history with your own. With '--squash', imports 75 + project's history with your own. With '--squash', import 74 76 only a single commit from the subproject, rather than its 75 77 entire history. 76 78 77 - merge:: 78 - Merge recent changes up to <commit> into the <prefix> 79 + merge <local-commit>:: 80 + Merge recent changes up to <local-commit> into the <prefix> 79 81 subtree. As with normal 'git merge', this doesn't 80 82 remove your own local changes; it just merges those 81 - changes into the latest <commit>. With '--squash', 82 - creates only one commit that contains all the changes, 83 + changes into the latest <local-commit>. With '--squash', 84 + create only one commit that contains all the changes, 83 85 rather than merging in the entire history. 84 86 + 85 87 If you use '--squash', the merge direction doesn't always have to be 86 88 forward; you can use this command to go back in time from v2.5 to v2.4, 87 89 for example. If your merge introduces a conflict, you can resolve it in 88 90 the usual ways. 89 - 90 - pull:: 91 - Exactly like 'merge', but parallels 'git pull' in that 92 - it fetches the given ref from the specified remote 93 - repository. 94 - 95 - push:: 96 - Does a 'split' (see below) using the <prefix> supplied 97 - and then does a 'git push' to push the result to the 98 - repository and ref. This can be used to push your 99 - subtree to different branches of the remote repository. 100 91 101 - split:: 92 + split [<local-commit>]:: 102 93 Extract a new, synthetic project history from the 103 - history of the <prefix> subtree. The new history 94 + history of the <prefix> subtree of <local-commit>, or of 95 + HEAD if no <local-commit> is given. The new history 104 96 includes only the commits (including merges) that 105 97 affected <prefix>, and each of those commits now has the 106 98 contents of <prefix> at the root of the project instead 107 99 of in a subdirectory. Thus, the newly created history 108 100 is suitable for export as a separate git repository. 109 101 + 110 - After splitting successfully, a single commit id is printed to stdout. 102 + After splitting successfully, a single commit ID is printed to stdout. 111 103 This corresponds to the HEAD of the newly created tree, which you can 112 104 manipulate however you want. 113 105 + 114 106 Repeated splits of exactly the same history are guaranteed to be 115 - identical (i.e. to produce the same commit ids). Because of this, if 116 - you add new commits and then re-split, the new commits will be attached 117 - as commits on top of the history you generated last time, so 'git merge' 118 - and friends will work as expected. 119 - + 120 - Note that if you use '--squash' when you merge, you should usually not 121 - just '--rejoin' when you split. 107 + identical (i.e. to produce the same commit IDs) as long as the 108 + settings passed to 'split' (such as '--annotate') are the same. 109 + Because of this, if you add new commits and then re-split, the new 110 + commits will be attached as commits on top of the history you 111 + generated last time, so 'git merge' and friends will work as expected. 122 112 113 + pull <repository> <remote-ref>:: 114 + Exactly like 'merge', but parallels 'git pull' in that 115 + it fetches the given ref from the specified remote 116 + repository. 123 117 124 - OPTIONS 125 - ------- 118 + push <repository> [+][<local-commit>:]<remote-ref>:: 119 + Does a 'split' using the <prefix> subtree of <local-commit> 120 + and then does a 'git push' to push the result to the 121 + <repository> and <remote-ref>. This can be used to push your 122 + subtree to different branches of the remote repository. Just 123 + as with 'split', if no <local-commit> is given, then HEAD is 124 + used. The optional leading '+' is ignored. 125 + 126 + OPTIONS FOR ALL COMMANDS 127 + ------------------------ 126 128 -q:: 127 129 --quiet:: 128 130 Suppress unnecessary output messages on stderr. ··· 137 139 want to manipulate. This option is mandatory 138 140 for all commands. 139 141 140 - -m <message>:: 141 - --message=<message>:: 142 - This option is only valid for add, merge, pull, and split --rejoin. 143 - Specify <message> as the commit message for the merge commit. 142 + OPTIONS FOR 'add' AND 'merge' (ALSO: 'pull', 'split --rejoin', AND 'push --rejoin') 143 + ----------------------------------------------------------------------------------- 144 + These options for 'add' and 'merge' may also be given to 'pull' (which 145 + wraps 'merge'), 'split --rejoin' (which wraps either 'add' or 'merge' 146 + as appropriate), and 'push --rejoin' (which wraps 'split --rejoin'). 144 147 145 - 146 - OPTIONS FOR add, merge, and pull 147 - -------------------------------- 148 148 --squash:: 149 - This option is only valid for add, merge, and pull 150 - commands. 151 - + 152 - Instead of merging the entire history from the subtree project, produce 153 - only a single commit that contains all the differences you want to 154 - merge, and then merge that new commit into your project. 149 + Instead of merging the entire history from the subtree project, produce 150 + only a single commit that contains all the differences you want to 151 + merge, and then merge that new commit into your project. 155 152 + 156 153 Using this option helps to reduce log clutter. People rarely want to see 157 154 every change that happened between v1.0 and v1.1 of the library they're ··· 174 171 remain intact and can be later split and send upstream to the 175 172 subproject. 176 173 174 + -m <message>:: 175 + --message=<message>:: 176 + Specify <message> as the commit message for the merge commit. 177 177 178 - OPTIONS FOR split 179 - ----------------- 178 + OPTIONS FOR 'split' (ALSO: 'push') 179 + ---------------------------------- 180 + These options for 'split' may also be given to 'push' (which wraps 181 + 'split'). 182 + 180 183 --annotate=<annotation>:: 181 - This option is only valid for the split command. 182 - + 183 - When generating synthetic history, add <annotation> as a prefix to each 184 - commit message. Since we're creating new commits with the same commit 185 - message, but possibly different content, from the original commits, this 186 - can help to differentiate them and avoid confusion. 184 + When generating synthetic history, add <annotation> as a prefix to each 185 + commit message. Since we're creating new commits with the same commit 186 + message, but possibly different content, from the original commits, this 187 + can help to differentiate them and avoid confusion. 187 188 + 188 189 Whenever you split, you need to use the same <annotation>, or else you 189 190 don't have a guarantee that the new re-created history will be identical 190 191 to the old one. That will prevent merging from working correctly. git 191 - subtree tries to make it work anyway, particularly if you use --rejoin, 192 + subtree tries to make it work anyway, particularly if you use '--rejoin', 192 193 but it may not always be effective. 193 194 194 195 -b <branch>:: 195 196 --branch=<branch>:: 196 - This option is only valid for the split command. 197 - + 198 - After generating the synthetic history, create a new branch called 199 - <branch> that contains the new history. This is suitable for immediate 200 - pushing upstream. <branch> must not already exist. 197 + After generating the synthetic history, create a new branch called 198 + <branch> that contains the new history. This is suitable for immediate 199 + pushing upstream. <branch> must not already exist. 201 200 202 201 --ignore-joins:: 203 - This option is only valid for the split command. 204 - + 205 - If you use '--rejoin', git subtree attempts to optimize its history 206 - reconstruction to generate only the new commits since the last 207 - '--rejoin'. '--ignore-join' disables this behaviour, forcing it to 208 - regenerate the entire history. In a large project, this can take a long 209 - time. 202 + If you use '--rejoin', git subtree attempts to optimize its history 203 + reconstruction to generate only the new commits since the last 204 + '--rejoin'. '--ignore-joins' disables this behavior, forcing it to 205 + regenerate the entire history. In a large project, this can take a long 206 + time. 210 207 211 208 --onto=<onto>:: 212 - This option is only valid for the split command. 213 - + 214 - If your subtree was originally imported using something other than git 215 - subtree, its history may not match what git subtree is expecting. In 216 - that case, you can specify the commit id <onto> that corresponds to the 217 - first revision of the subproject's history that was imported into your 218 - project, and git subtree will attempt to build its history from there. 209 + If your subtree was originally imported using something other than git 210 + subtree, its history may not match what git subtree is expecting. In 211 + that case, you can specify the commit ID <onto> that corresponds to the 212 + first revision of the subproject's history that was imported into your 213 + project, and git subtree will attempt to build its history from there. 219 214 + 220 215 If you used 'git subtree add', you should never need this option. 221 216 222 217 --rejoin:: 223 - This option is only valid for the split command. 224 - + 225 - After splitting, merge the newly created synthetic history back into 226 - your main project. That way, future splits can search only the part of 227 - history that has been added since the most recent --rejoin. 218 + After splitting, merge the newly created synthetic history back into 219 + your main project. That way, future splits can search only the part of 220 + history that has been added since the most recent '--rejoin'. 228 221 + 229 222 If your split commits end up merged into the upstream subproject, and 230 223 then you want to get the latest upstream version, this will allow git's ··· 235 228 copy of every new commit that was created (the original, and the 236 229 synthetic one). 237 230 + 238 - If you do all your merges with '--squash', don't use '--rejoin' when you 239 - split, because you don't want the subproject's history to be part of 240 - your project anyway. 231 + If you do all your merges with '--squash', make sure you also use 232 + '--squash' when you 'split --rejoin'. 241 233 242 234 243 - EXAMPLE 1. Add command 244 - ---------------------- 235 + EXAMPLE 1. 'add' command 236 + ------------------------ 245 237 Let's assume that you have a local repository that you would like 246 238 to add an external vendor library to. In this case we will add the 247 239 git-subtree repository as a subdirectory of your already existing ··· 253 245 'master' needs to be a valid remote ref and can be a different branch 254 246 name 255 247 256 - You can omit the --squash flag, but doing so will increase the number 248 + You can omit the '--squash' flag, but doing so will increase the number 257 249 of commits that are included in your local repository. 258 250 259 251 We now have a ~/git-extensions/git-subtree directory containing code 260 252 from the master branch of git://github.com/apenwarr/git-subtree.git 261 253 in our git-extensions repository. 262 254 263 - EXAMPLE 2. Extract a subtree using commit, merge and pull 264 - --------------------------------------------------------- 255 + EXAMPLE 2. Extract a subtree using 'commit', 'merge' and 'pull' 256 + --------------------------------------------------------------- 265 257 Let's use the repository for the git source code as an example. 266 258 First, get your own copy of the git.git repository: 267 259 ··· 269 261 $ cd test-git 270 262 271 263 gitweb (commit 1130ef3) was merged into git as of commit 272 - 0a8f4f0, after which it was no longer maintained separately. 264 + 0a8f4f0, after which it was no longer maintained separately. 273 265 But imagine it had been maintained separately, and we wanted to 274 266 extract git's changes to gitweb since that time, to share with 275 267 the upstream. You could do this: ··· 279 271 --branch gitweb-latest 280 272 $ gitk gitweb-latest 281 273 $ git push git@github.com:whatever/gitweb.git gitweb-latest:master 282 - 274 + 283 275 (We use '0a8f4f0^..' because that means "all the changes from 284 276 0a8f4f0 to the current version, including 0a8f4f0 itself.") 285 277 286 278 If gitweb had originally been merged using 'git subtree add' (or 287 - a previous split had already been done with --rejoin specified) 279 + a previous split had already been done with '--rejoin' specified) 288 280 then you can do all your splits without having to remember any 289 - weird commit ids: 281 + weird commit IDs: 290 282 291 283 $ git subtree split --prefix=gitweb --annotate='(split) ' --rejoin \ 292 284 --branch gitweb-latest2 ··· 313 305 $ git subtree merge --prefix=gitweb --squash gitweb-latest 314 306 315 307 And notice that your change is still intact: 316 - 308 + 317 309 $ ls -l gitweb/myfile 318 310 319 311 And you can split it out and look at your changes versus ··· 321 313 322 314 git log gitweb-latest..$(git subtree split --prefix=gitweb) 323 315 324 - EXAMPLE 3. Extract a subtree using branch 325 - ----------------------------------------- 316 + EXAMPLE 3. Extract a subtree using a branch 317 + ------------------------------------------- 326 318 Suppose you have a source directory with many files and 327 319 subdirectories, and you want to extract the lib directory to its own 328 320 git project. Here's a short way to do it:
+946 -502
contrib/subtree/t/t7900-subtree.sh
··· 5 5 # 6 6 test_description='Basic porcelain support for subtrees 7 7 8 - This test verifies the basic operation of the add, pull, merge 9 - and split subcommands of git subtree. 8 + This test verifies the basic operation of the add, merge, split, pull, 9 + and push subcommands of git subtree. 10 10 ' 11 11 12 12 TEST_DIRECTORY=$(pwd)/../../../t 13 - export TEST_DIRECTORY 13 + . "$TEST_DIRECTORY"/test-lib.sh 14 14 15 - . ../../../t/test-lib.sh 16 - 17 - subtree_test_create_repo() 18 - { 15 + # Use our own wrapper around test-lib.sh's test_create_repo, in order 16 + # to set log.date=relative. `git subtree` parses the output of `git 17 + # log`, and so it must be careful to not be affected by settings that 18 + # change the `git log` output. We test this by setting 19 + # log.date=relative for every repo in the tests. 20 + subtree_test_create_repo () { 19 21 test_create_repo "$1" && 20 - ( 21 - cd "$1" && 22 - git config log.date relative 23 - ) 24 - } 25 - 26 - create() 27 - { 28 - echo "$1" >"$1" && 29 - git add "$1" 22 + git -C "$1" config log.date relative 30 23 } 31 24 32 - check_equal() 33 - { 34 - test_debug 'echo' 35 - test_debug "echo \"check a:\" \"{$1}\"" 36 - test_debug "echo \" b:\" \"{$2}\"" 37 - if [ "$1" = "$2" ]; then 38 - return 0 39 - else 40 - return 1 41 - fi 42 - } 43 - 44 - undo() 45 - { 46 - git reset --hard HEAD~ 47 - } 48 - 49 - # Make sure no patch changes more than one file. 50 - # The original set of commits changed only one file each. 51 - # A multi-file change would imply that we pruned commits 52 - # too aggressively. 53 - join_commits() 54 - { 55 - commit= 56 - all= 57 - while read x y; do 58 - if [ -z "$x" ]; then 59 - continue 60 - elif [ "$x" = "commit:" ]; then 61 - if [ -n "$commit" ]; then 62 - echo "$commit $all" 63 - all= 64 - fi 65 - commit="$y" 66 - else 67 - all="$all $y" 68 - fi 69 - done 70 - echo "$commit $all" 71 - } 72 - 73 - test_create_commit() ( 25 + test_create_commit () ( 74 26 repo=$1 && 75 27 commit=$2 && 76 28 cd "$repo" && ··· 81 33 git commit -m "$commit" || error "Could not commit" 82 34 ) 83 35 84 - last_commit_message() 85 - { 86 - git log --pretty=format:%s -1 36 + test_wrong_flag() { 37 + test_must_fail "$@" >out 2>err && 38 + test_must_be_empty out && 39 + grep "flag does not make sense with" err 87 40 } 88 41 89 - subtree_test_count=0 90 - next_test() { 91 - subtree_test_count=$(($subtree_test_count+1)) 42 + last_commit_subject () { 43 + git log --pretty=format:%s -1 92 44 } 93 45 46 + test_expect_success 'shows short help text for -h' ' 47 + test_expect_code 129 git subtree -h >out 2>err && 48 + test_must_be_empty err && 49 + grep -e "^ *or: git subtree pull" out && 50 + grep -e --annotate out 51 + ' 52 + 94 53 # 95 54 # Tests for 'git subtree add' 96 55 # 97 56 98 - next_test 99 57 test_expect_success 'no merge from non-existent subtree' ' 100 - subtree_test_create_repo "$subtree_test_count" && 101 - subtree_test_create_repo "$subtree_test_count/sub proj" && 102 - test_create_commit "$subtree_test_count" main1 && 103 - test_create_commit "$subtree_test_count/sub proj" sub1 && 58 + subtree_test_create_repo "$test_count" && 59 + subtree_test_create_repo "$test_count/sub proj" && 60 + test_create_commit "$test_count" main1 && 61 + test_create_commit "$test_count/sub proj" sub1 && 104 62 ( 105 - cd "$subtree_test_count" && 106 - git fetch ./"sub proj" master && 63 + cd "$test_count" && 64 + git fetch ./"sub proj" HEAD && 107 65 test_must_fail git subtree merge --prefix="sub dir" FETCH_HEAD 108 66 ) 109 67 ' 110 68 111 - next_test 112 69 test_expect_success 'no pull from non-existent subtree' ' 113 - subtree_test_create_repo "$subtree_test_count" && 114 - subtree_test_create_repo "$subtree_test_count/sub proj" && 115 - test_create_commit "$subtree_test_count" main1 && 116 - test_create_commit "$subtree_test_count/sub proj" sub1 && 70 + subtree_test_create_repo "$test_count" && 71 + subtree_test_create_repo "$test_count/sub proj" && 72 + test_create_commit "$test_count" main1 && 73 + test_create_commit "$test_count/sub proj" sub1 && 117 74 ( 118 - cd "$subtree_test_count" && 119 - git fetch ./"sub proj" master && 120 - test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" master 121 - )' 75 + cd "$test_count" && 76 + git fetch ./"sub proj" HEAD && 77 + test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" HEAD 78 + ) 79 + ' 122 80 123 - next_test 81 + test_expect_success 'add rejects flags for split' ' 82 + subtree_test_create_repo "$test_count" && 83 + subtree_test_create_repo "$test_count/sub proj" && 84 + test_create_commit "$test_count" main1 && 85 + test_create_commit "$test_count/sub proj" sub1 && 86 + ( 87 + cd "$test_count" && 88 + git fetch ./"sub proj" HEAD && 89 + test_wrong_flag git subtree add --prefix="sub dir" --annotate=foo FETCH_HEAD && 90 + test_wrong_flag git subtree add --prefix="sub dir" --branch=foo FETCH_HEAD && 91 + test_wrong_flag git subtree add --prefix="sub dir" --ignore-joins FETCH_HEAD && 92 + test_wrong_flag git subtree add --prefix="sub dir" --onto=foo FETCH_HEAD && 93 + test_wrong_flag git subtree add --prefix="sub dir" --rejoin FETCH_HEAD 94 + ) 95 + ' 96 + 124 97 test_expect_success 'add subproj as subtree into sub dir/ with --prefix' ' 125 - subtree_test_create_repo "$subtree_test_count" && 126 - subtree_test_create_repo "$subtree_test_count/sub proj" && 127 - test_create_commit "$subtree_test_count" main1 && 128 - test_create_commit "$subtree_test_count/sub proj" sub1 && 98 + subtree_test_create_repo "$test_count" && 99 + subtree_test_create_repo "$test_count/sub proj" && 100 + test_create_commit "$test_count" main1 && 101 + test_create_commit "$test_count/sub proj" sub1 && 129 102 ( 130 - cd "$subtree_test_count" && 131 - git fetch ./"sub proj" master && 103 + cd "$test_count" && 104 + git fetch ./"sub proj" HEAD && 132 105 git subtree add --prefix="sub dir" FETCH_HEAD && 133 - check_equal "$(last_commit_message)" "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''" 106 + test "$(last_commit_subject)" = "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''" 134 107 ) 135 108 ' 136 109 137 - next_test 138 110 test_expect_success 'add subproj as subtree into sub dir/ with --prefix and --message' ' 139 - subtree_test_create_repo "$subtree_test_count" && 140 - subtree_test_create_repo "$subtree_test_count/sub proj" && 141 - test_create_commit "$subtree_test_count" main1 && 142 - test_create_commit "$subtree_test_count/sub proj" sub1 && 111 + subtree_test_create_repo "$test_count" && 112 + subtree_test_create_repo "$test_count/sub proj" && 113 + test_create_commit "$test_count" main1 && 114 + test_create_commit "$test_count/sub proj" sub1 && 143 115 ( 144 - cd "$subtree_test_count" && 145 - git fetch ./"sub proj" master && 116 + cd "$test_count" && 117 + git fetch ./"sub proj" HEAD && 146 118 git subtree add --prefix="sub dir" --message="Added subproject" FETCH_HEAD && 147 - check_equal "$(last_commit_message)" "Added subproject" 119 + test "$(last_commit_subject)" = "Added subproject" 148 120 ) 149 121 ' 150 122 151 - next_test 152 123 test_expect_success 'add subproj as subtree into sub dir/ with --prefix as -P and --message as -m' ' 153 - subtree_test_create_repo "$subtree_test_count" && 154 - subtree_test_create_repo "$subtree_test_count/sub proj" && 155 - test_create_commit "$subtree_test_count" main1 && 156 - test_create_commit "$subtree_test_count/sub proj" sub1 && 124 + subtree_test_create_repo "$test_count" && 125 + subtree_test_create_repo "$test_count/sub proj" && 126 + test_create_commit "$test_count" main1 && 127 + test_create_commit "$test_count/sub proj" sub1 && 157 128 ( 158 - cd "$subtree_test_count" && 159 - git fetch ./"sub proj" master && 129 + cd "$test_count" && 130 + git fetch ./"sub proj" HEAD && 160 131 git subtree add -P "sub dir" -m "Added subproject" FETCH_HEAD && 161 - check_equal "$(last_commit_message)" "Added subproject" 132 + test "$(last_commit_subject)" = "Added subproject" 162 133 ) 163 134 ' 164 135 165 - next_test 166 136 test_expect_success 'add subproj as subtree into sub dir/ with --squash and --prefix and --message' ' 167 - subtree_test_create_repo "$subtree_test_count" && 168 - subtree_test_create_repo "$subtree_test_count/sub proj" && 169 - test_create_commit "$subtree_test_count" main1 && 170 - test_create_commit "$subtree_test_count/sub proj" sub1 && 137 + subtree_test_create_repo "$test_count" && 138 + subtree_test_create_repo "$test_count/sub proj" && 139 + test_create_commit "$test_count" main1 && 140 + test_create_commit "$test_count/sub proj" sub1 && 171 141 ( 172 - cd "$subtree_test_count" && 173 - git fetch ./"sub proj" master && 142 + cd "$test_count" && 143 + git fetch ./"sub proj" HEAD && 174 144 git subtree add --prefix="sub dir" --message="Added subproject with squash" --squash FETCH_HEAD && 175 - check_equal "$(last_commit_message)" "Added subproject with squash" 145 + test "$(last_commit_subject)" = "Added subproject with squash" 176 146 ) 177 147 ' 178 148 ··· 180 150 # Tests for 'git subtree merge' 181 151 # 182 152 183 - next_test 153 + test_expect_success 'merge rejects flags for split' ' 154 + subtree_test_create_repo "$test_count" && 155 + subtree_test_create_repo "$test_count/sub proj" && 156 + test_create_commit "$test_count" main1 && 157 + test_create_commit "$test_count/sub proj" sub1 && 158 + ( 159 + cd "$test_count" && 160 + git fetch ./"sub proj" HEAD && 161 + git subtree add --prefix="sub dir" FETCH_HEAD 162 + ) && 163 + test_create_commit "$test_count/sub proj" sub2 && 164 + ( 165 + cd "$test_count" && 166 + git fetch ./"sub proj" HEAD && 167 + test_wrong_flag git subtree merge --prefix="sub dir" --annotate=foo FETCH_HEAD && 168 + test_wrong_flag git subtree merge --prefix="sub dir" --branch=foo FETCH_HEAD && 169 + test_wrong_flag git subtree merge --prefix="sub dir" --ignore-joins FETCH_HEAD && 170 + test_wrong_flag git subtree merge --prefix="sub dir" --onto=foo FETCH_HEAD && 171 + test_wrong_flag git subtree merge --prefix="sub dir" --rejoin FETCH_HEAD 172 + ) 173 + ' 174 + 184 175 test_expect_success 'merge new subproj history into sub dir/ with --prefix' ' 185 - subtree_test_create_repo "$subtree_test_count" && 186 - subtree_test_create_repo "$subtree_test_count/sub proj" && 187 - test_create_commit "$subtree_test_count" main1 && 188 - test_create_commit "$subtree_test_count/sub proj" sub1 && 176 + subtree_test_create_repo "$test_count" && 177 + subtree_test_create_repo "$test_count/sub proj" && 178 + test_create_commit "$test_count" main1 && 179 + test_create_commit "$test_count/sub proj" sub1 && 189 180 ( 190 - cd "$subtree_test_count" && 191 - git fetch ./"sub proj" master && 181 + cd "$test_count" && 182 + git fetch ./"sub proj" HEAD && 192 183 git subtree add --prefix="sub dir" FETCH_HEAD 193 184 ) && 194 - test_create_commit "$subtree_test_count/sub proj" sub2 && 185 + test_create_commit "$test_count/sub proj" sub2 && 195 186 ( 196 - cd "$subtree_test_count" && 197 - git fetch ./"sub proj" master && 187 + cd "$test_count" && 188 + git fetch ./"sub proj" HEAD && 198 189 git subtree merge --prefix="sub dir" FETCH_HEAD && 199 - check_equal "$(last_commit_message)" "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''" 190 + test "$(last_commit_subject)" = "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''" 200 191 ) 201 192 ' 202 193 203 - next_test 204 194 test_expect_success 'merge new subproj history into sub dir/ with --prefix and --message' ' 205 - subtree_test_create_repo "$subtree_test_count" && 206 - subtree_test_create_repo "$subtree_test_count/sub proj" && 207 - test_create_commit "$subtree_test_count" main1 && 208 - test_create_commit "$subtree_test_count/sub proj" sub1 && 195 + subtree_test_create_repo "$test_count" && 196 + subtree_test_create_repo "$test_count/sub proj" && 197 + test_create_commit "$test_count" main1 && 198 + test_create_commit "$test_count/sub proj" sub1 && 209 199 ( 210 - cd "$subtree_test_count" && 211 - git fetch ./"sub proj" master && 200 + cd "$test_count" && 201 + git fetch ./"sub proj" HEAD && 212 202 git subtree add --prefix="sub dir" FETCH_HEAD 213 203 ) && 214 - test_create_commit "$subtree_test_count/sub proj" sub2 && 204 + test_create_commit "$test_count/sub proj" sub2 && 215 205 ( 216 - cd "$subtree_test_count" && 217 - git fetch ./"sub proj" master && 206 + cd "$test_count" && 207 + git fetch ./"sub proj" HEAD && 218 208 git subtree merge --prefix="sub dir" --message="Merged changes from subproject" FETCH_HEAD && 219 - check_equal "$(last_commit_message)" "Merged changes from subproject" 209 + test "$(last_commit_subject)" = "Merged changes from subproject" 220 210 ) 221 211 ' 222 212 223 - next_test 224 213 test_expect_success 'merge new subproj history into sub dir/ with --squash and --prefix and --message' ' 225 - subtree_test_create_repo "$subtree_test_count/sub proj" && 226 - subtree_test_create_repo "$subtree_test_count" && 227 - test_create_commit "$subtree_test_count" main1 && 228 - test_create_commit "$subtree_test_count/sub proj" sub1 && 214 + subtree_test_create_repo "$test_count/sub proj" && 215 + subtree_test_create_repo "$test_count" && 216 + test_create_commit "$test_count" main1 && 217 + test_create_commit "$test_count/sub proj" sub1 && 229 218 ( 230 - cd "$subtree_test_count" && 231 - git fetch ./"sub proj" master && 219 + cd "$test_count" && 220 + git fetch ./"sub proj" HEAD && 232 221 git subtree add --prefix="sub dir" FETCH_HEAD 233 222 ) && 234 - test_create_commit "$subtree_test_count/sub proj" sub2 && 223 + test_create_commit "$test_count/sub proj" sub2 && 235 224 ( 236 - cd "$subtree_test_count" && 237 - git fetch ./"sub proj" master && 225 + cd "$test_count" && 226 + git fetch ./"sub proj" HEAD && 238 227 git subtree merge --prefix="sub dir" --message="Merged changes from subproject using squash" --squash FETCH_HEAD && 239 - check_equal "$(last_commit_message)" "Merged changes from subproject using squash" 228 + test "$(last_commit_subject)" = "Merged changes from subproject using squash" 240 229 ) 241 230 ' 242 231 243 - next_test 244 232 test_expect_success 'merge the added subproj again, should do nothing' ' 245 - subtree_test_create_repo "$subtree_test_count" && 246 - subtree_test_create_repo "$subtree_test_count/sub proj" && 247 - test_create_commit "$subtree_test_count" main1 && 248 - test_create_commit "$subtree_test_count/sub proj" sub1 && 233 + subtree_test_create_repo "$test_count" && 234 + subtree_test_create_repo "$test_count/sub proj" && 235 + test_create_commit "$test_count" main1 && 236 + test_create_commit "$test_count/sub proj" sub1 && 249 237 ( 250 - cd "$subtree_test_count" && 251 - git fetch ./"sub proj" master && 238 + cd "$test_count" && 239 + git fetch ./"sub proj" HEAD && 252 240 git subtree add --prefix="sub dir" FETCH_HEAD && 253 241 # this shouldn not actually do anything, since FETCH_HEAD 254 242 # is already a parent 255 243 result=$(git merge -s ours -m "merge -s -ours" FETCH_HEAD) && 256 - check_equal "${result}" "Already up to date." 244 + test "${result}" = "Already up to date." 257 245 ) 258 246 ' 259 247 260 - next_test 261 248 test_expect_success 'merge new subproj history into subdir/ with a slash appended to the argument of --prefix' ' 262 - test_create_repo "$test_count" && 263 - test_create_repo "$test_count/subproj" && 249 + subtree_test_create_repo "$test_count" && 250 + subtree_test_create_repo "$test_count/subproj" && 264 251 test_create_commit "$test_count" main1 && 265 252 test_create_commit "$test_count/subproj" sub1 && 266 253 ( 267 254 cd "$test_count" && 268 - git fetch ./subproj master && 255 + git fetch ./subproj HEAD && 269 256 git subtree add --prefix=subdir/ FETCH_HEAD 270 257 ) && 271 258 test_create_commit "$test_count/subproj" sub2 && 272 259 ( 273 260 cd "$test_count" && 274 - git fetch ./subproj master && 261 + git fetch ./subproj HEAD && 275 262 git subtree merge --prefix=subdir/ FETCH_HEAD && 276 - check_equal "$(last_commit_message)" "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''" 263 + test "$(last_commit_subject)" = "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''" 277 264 ) 278 265 ' 279 266 ··· 281 268 # Tests for 'git subtree split' 282 269 # 283 270 284 - next_test 285 271 test_expect_success 'split requires option --prefix' ' 286 - subtree_test_create_repo "$subtree_test_count" && 287 - subtree_test_create_repo "$subtree_test_count/sub proj" && 288 - test_create_commit "$subtree_test_count" main1 && 289 - test_create_commit "$subtree_test_count/sub proj" sub1 && 272 + subtree_test_create_repo "$test_count" && 273 + subtree_test_create_repo "$test_count/sub proj" && 274 + test_create_commit "$test_count" main1 && 275 + test_create_commit "$test_count/sub proj" sub1 && 290 276 ( 291 - cd "$subtree_test_count" && 292 - git fetch ./"sub proj" master && 277 + cd "$test_count" && 278 + git fetch ./"sub proj" HEAD && 293 279 git subtree add --prefix="sub dir" FETCH_HEAD && 294 - echo "You must provide the --prefix option." > expected && 295 - test_must_fail git subtree split > actual 2>&1 && 280 + echo "You must provide the --prefix option." >expected && 281 + test_must_fail git subtree split >actual 2>&1 && 296 282 test_debug "printf '"expected: "'" && 297 283 test_debug "cat expected" && 298 284 test_debug "printf '"actual: "'" && ··· 301 287 ) 302 288 ' 303 289 304 - next_test 305 290 test_expect_success 'split requires path given by option --prefix must exist' ' 306 - subtree_test_create_repo "$subtree_test_count" && 307 - subtree_test_create_repo "$subtree_test_count/sub proj" && 308 - test_create_commit "$subtree_test_count" main1 && 309 - test_create_commit "$subtree_test_count/sub proj" sub1 && 291 + subtree_test_create_repo "$test_count" && 292 + subtree_test_create_repo "$test_count/sub proj" && 293 + test_create_commit "$test_count" main1 && 294 + test_create_commit "$test_count/sub proj" sub1 && 310 295 ( 311 - cd "$subtree_test_count" && 312 - git fetch ./"sub proj" master && 296 + cd "$test_count" && 297 + git fetch ./"sub proj" HEAD && 313 298 git subtree add --prefix="sub dir" FETCH_HEAD && 314 - echo "'\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" > expected && 315 - test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 && 299 + echo "'\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" >expected && 300 + test_must_fail git subtree split --prefix=non-existent-directory >actual 2>&1 && 316 301 test_debug "printf '"expected: "'" && 317 302 test_debug "cat expected" && 318 303 test_debug "printf '"actual: "'" && ··· 321 306 ) 322 307 ' 323 308 324 - next_test 309 + test_expect_success 'split rejects flags for add' ' 310 + subtree_test_create_repo "$test_count" && 311 + subtree_test_create_repo "$test_count/sub proj" && 312 + test_create_commit "$test_count" main1 && 313 + test_create_commit "$test_count/sub proj" sub1 && 314 + ( 315 + cd "$test_count" && 316 + git fetch ./"sub proj" HEAD && 317 + git subtree add --prefix="sub dir" FETCH_HEAD 318 + ) && 319 + test_create_commit "$test_count" "sub dir"/main-sub1 && 320 + test_create_commit "$test_count" main2 && 321 + test_create_commit "$test_count/sub proj" sub2 && 322 + test_create_commit "$test_count" "sub dir"/main-sub2 && 323 + ( 324 + cd "$test_count" && 325 + git fetch ./"sub proj" HEAD && 326 + git subtree merge --prefix="sub dir" FETCH_HEAD && 327 + split_hash=$(git subtree split --prefix="sub dir" --annotate="*") && 328 + test_wrong_flag git subtree split --prefix="sub dir" --squash && 329 + test_wrong_flag git subtree split --prefix="sub dir" --message=foo 330 + ) 331 + ' 332 + 325 333 test_expect_success 'split sub dir/ with --rejoin' ' 326 - subtree_test_create_repo "$subtree_test_count" && 327 - subtree_test_create_repo "$subtree_test_count/sub proj" && 328 - test_create_commit "$subtree_test_count" main1 && 329 - test_create_commit "$subtree_test_count/sub proj" sub1 && 334 + subtree_test_create_repo "$test_count" && 335 + subtree_test_create_repo "$test_count/sub proj" && 336 + test_create_commit "$test_count" main1 && 337 + test_create_commit "$test_count/sub proj" sub1 && 330 338 ( 331 - cd "$subtree_test_count" && 332 - git fetch ./"sub proj" master && 339 + cd "$test_count" && 340 + git fetch ./"sub proj" HEAD && 333 341 git subtree add --prefix="sub dir" FETCH_HEAD 334 342 ) && 335 - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && 336 - test_create_commit "$subtree_test_count" main2 && 337 - test_create_commit "$subtree_test_count/sub proj" sub2 && 338 - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && 343 + test_create_commit "$test_count" "sub dir"/main-sub1 && 344 + test_create_commit "$test_count" main2 && 345 + test_create_commit "$test_count/sub proj" sub2 && 346 + test_create_commit "$test_count" "sub dir"/main-sub2 && 339 347 ( 340 - cd "$subtree_test_count" && 341 - git fetch ./"sub proj" master && 348 + cd "$test_count" && 349 + git fetch ./"sub proj" HEAD && 342 350 git subtree merge --prefix="sub dir" FETCH_HEAD && 343 351 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") && 344 352 git subtree split --prefix="sub dir" --annotate="*" --rejoin && 345 - check_equal "$(last_commit_message)" "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" 353 + test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" 346 354 ) 347 - ' 355 + ' 348 356 349 - next_test 350 357 test_expect_success 'split sub dir/ with --rejoin from scratch' ' 351 - subtree_test_create_repo "$subtree_test_count" && 352 - test_create_commit "$subtree_test_count" main1 && 358 + subtree_test_create_repo "$test_count" && 359 + test_create_commit "$test_count" main1 && 353 360 ( 354 - cd "$subtree_test_count" && 361 + cd "$test_count" && 355 362 mkdir "sub dir" && 356 363 echo file >"sub dir"/file && 357 364 git add "sub dir/file" && 358 365 git commit -m"sub dir file" && 359 366 split_hash=$(git subtree split --prefix="sub dir" --rejoin) && 360 367 git subtree split --prefix="sub dir" --rejoin && 361 - check_equal "$(last_commit_message)" "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" 368 + test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" 362 369 ) 363 - ' 370 + ' 364 371 365 - next_test 366 372 test_expect_success 'split sub dir/ with --rejoin and --message' ' 367 - subtree_test_create_repo "$subtree_test_count" && 368 - subtree_test_create_repo "$subtree_test_count/sub proj" && 369 - test_create_commit "$subtree_test_count" main1 && 370 - test_create_commit "$subtree_test_count/sub proj" sub1 && 373 + subtree_test_create_repo "$test_count" && 374 + subtree_test_create_repo "$test_count/sub proj" && 375 + test_create_commit "$test_count" main1 && 376 + test_create_commit "$test_count/sub proj" sub1 && 371 377 ( 372 - cd "$subtree_test_count" && 373 - git fetch ./"sub proj" master && 378 + cd "$test_count" && 379 + git fetch ./"sub proj" HEAD && 374 380 git subtree add --prefix="sub dir" FETCH_HEAD 375 381 ) && 376 - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && 377 - test_create_commit "$subtree_test_count" main2 && 378 - test_create_commit "$subtree_test_count/sub proj" sub2 && 379 - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && 382 + test_create_commit "$test_count" "sub dir"/main-sub1 && 383 + test_create_commit "$test_count" main2 && 384 + test_create_commit "$test_count/sub proj" sub2 && 385 + test_create_commit "$test_count" "sub dir"/main-sub2 && 380 386 ( 381 - cd "$subtree_test_count" && 382 - git fetch ./"sub proj" master && 387 + cd "$test_count" && 388 + git fetch ./"sub proj" HEAD && 383 389 git subtree merge --prefix="sub dir" FETCH_HEAD && 384 390 git subtree split --prefix="sub dir" --message="Split & rejoin" --annotate="*" --rejoin && 385 - check_equal "$(last_commit_message)" "Split & rejoin" 391 + test "$(last_commit_subject)" = "Split & rejoin" 392 + ) 393 + ' 394 + 395 + test_expect_success 'split "sub dir"/ with --rejoin and --squash' ' 396 + subtree_test_create_repo "$test_count" && 397 + subtree_test_create_repo "$test_count/sub proj" && 398 + test_create_commit "$test_count" main1 && 399 + test_create_commit "$test_count/sub proj" sub1 && 400 + ( 401 + cd "$test_count" && 402 + git fetch ./"sub proj" HEAD && 403 + git subtree add --prefix="sub dir" --squash FETCH_HEAD 404 + ) && 405 + test_create_commit "$test_count" "sub dir"/main-sub1 && 406 + test_create_commit "$test_count" main2 && 407 + test_create_commit "$test_count/sub proj" sub2 && 408 + test_create_commit "$test_count" "sub dir"/main-sub2 && 409 + ( 410 + cd "$test_count" && 411 + git subtree pull --prefix="sub dir" --squash ./"sub proj" HEAD && 412 + MAIN=$(git rev-parse --verify HEAD) && 413 + SUB=$(git -C "sub proj" rev-parse --verify HEAD) && 414 + 415 + SPLIT=$(git subtree split --prefix="sub dir" --annotate="*" --rejoin --squash) && 416 + 417 + test_must_fail git merge-base --is-ancestor $SUB HEAD && 418 + test_must_fail git merge-base --is-ancestor $SPLIT HEAD && 419 + git rev-list HEAD ^$MAIN >commit-list && 420 + test_line_count = 2 commit-list && 421 + test "$(git rev-parse --verify HEAD:)" = "$(git rev-parse --verify $MAIN:)" && 422 + test "$(git rev-parse --verify HEAD:"sub dir")" = "$(git rev-parse --verify $SPLIT:)" && 423 + test "$(git rev-parse --verify HEAD^1)" = $MAIN && 424 + test "$(git rev-parse --verify HEAD^2)" != $SPLIT && 425 + test "$(git rev-parse --verify HEAD^2:)" = "$(git rev-parse --verify $SPLIT:)" && 426 + test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$SPLIT'\''" 386 427 ) 387 428 ' 388 429 389 - next_test 430 + test_expect_success 'split then pull "sub dir"/ with --rejoin and --squash' ' 431 + # 1. "add" 432 + subtree_test_create_repo "$test_count" && 433 + subtree_test_create_repo "$test_count/sub proj" && 434 + test_create_commit "$test_count" main1 && 435 + test_create_commit "$test_count/sub proj" sub1 && 436 + git -C "$test_count" subtree --prefix="sub dir" add --squash ./"sub proj" HEAD && 437 + 438 + # 2. commit from parent 439 + test_create_commit "$test_count" "sub dir"/main-sub1 && 440 + 441 + # 3. "split --rejoin --squash" 442 + git -C "$test_count" subtree --prefix="sub dir" split --rejoin --squash && 443 + 444 + # 4. "pull --squash" 445 + test_create_commit "$test_count/sub proj" sub2 && 446 + git -C "$test_count" subtree -d --prefix="sub dir" pull --squash ./"sub proj" HEAD && 447 + 448 + test_must_fail git merge-base HEAD FETCH_HEAD 449 + ' 450 + 390 451 test_expect_success 'split "sub dir"/ with --branch' ' 391 - subtree_test_create_repo "$subtree_test_count" && 392 - subtree_test_create_repo "$subtree_test_count/sub proj" && 393 - test_create_commit "$subtree_test_count" main1 && 394 - test_create_commit "$subtree_test_count/sub proj" sub1 && 452 + subtree_test_create_repo "$test_count" && 453 + subtree_test_create_repo "$test_count/sub proj" && 454 + test_create_commit "$test_count" main1 && 455 + test_create_commit "$test_count/sub proj" sub1 && 395 456 ( 396 - cd "$subtree_test_count" && 397 - git fetch ./"sub proj" master && 457 + cd "$test_count" && 458 + git fetch ./"sub proj" HEAD && 398 459 git subtree add --prefix="sub dir" FETCH_HEAD 399 460 ) && 400 - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && 401 - test_create_commit "$subtree_test_count" main2 && 402 - test_create_commit "$subtree_test_count/sub proj" sub2 && 403 - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && 461 + test_create_commit "$test_count" "sub dir"/main-sub1 && 462 + test_create_commit "$test_count" main2 && 463 + test_create_commit "$test_count/sub proj" sub2 && 464 + test_create_commit "$test_count" "sub dir"/main-sub2 && 404 465 ( 405 - cd "$subtree_test_count" && 406 - git fetch ./"sub proj" master && 466 + cd "$test_count" && 467 + git fetch ./"sub proj" HEAD && 407 468 git subtree merge --prefix="sub dir" FETCH_HEAD && 408 469 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") && 409 470 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br && 410 - check_equal "$(git rev-parse subproj-br)" "$split_hash" 471 + test "$(git rev-parse subproj-br)" = "$split_hash" 411 472 ) 412 473 ' 413 474 414 - next_test 415 475 test_expect_success 'check hash of split' ' 416 - subtree_test_create_repo "$subtree_test_count" && 417 - subtree_test_create_repo "$subtree_test_count/sub proj" && 418 - test_create_commit "$subtree_test_count" main1 && 419 - test_create_commit "$subtree_test_count/sub proj" sub1 && 476 + subtree_test_create_repo "$test_count" && 477 + subtree_test_create_repo "$test_count/sub proj" && 478 + test_create_commit "$test_count" main1 && 479 + test_create_commit "$test_count/sub proj" sub1 && 420 480 ( 421 - cd "$subtree_test_count" && 422 - git fetch ./"sub proj" master && 481 + cd "$test_count" && 482 + git fetch ./"sub proj" HEAD && 423 483 git subtree add --prefix="sub dir" FETCH_HEAD 424 484 ) && 425 - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && 426 - test_create_commit "$subtree_test_count" main2 && 427 - test_create_commit "$subtree_test_count/sub proj" sub2 && 428 - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && 485 + test_create_commit "$test_count" "sub dir"/main-sub1 && 486 + test_create_commit "$test_count" main2 && 487 + test_create_commit "$test_count/sub proj" sub2 && 488 + test_create_commit "$test_count" "sub dir"/main-sub2 && 429 489 ( 430 - cd "$subtree_test_count" && 431 - git fetch ./"sub proj" master && 490 + cd "$test_count" && 491 + git fetch ./"sub proj" HEAD && 432 492 git subtree merge --prefix="sub dir" FETCH_HEAD && 433 493 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") && 434 494 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br && 435 - check_equal "$(git rev-parse subproj-br)" "$split_hash" && 495 + test "$(git rev-parse subproj-br)" = "$split_hash" && 436 496 # Check hash of split 437 497 new_hash=$(git rev-parse subproj-br^2) && 438 498 ( 439 499 cd ./"sub proj" && 440 500 subdir_hash=$(git rev-parse HEAD) && 441 - check_equal ''"$new_hash"'' "$subdir_hash" 501 + test "$new_hash" = "$subdir_hash" 442 502 ) 443 503 ) 444 504 ' 445 505 446 - next_test 447 506 test_expect_success 'split "sub dir"/ with --branch for an existing branch' ' 448 - subtree_test_create_repo "$subtree_test_count" && 449 - subtree_test_create_repo "$subtree_test_count/sub proj" && 450 - test_create_commit "$subtree_test_count" main1 && 451 - test_create_commit "$subtree_test_count/sub proj" sub1 && 507 + subtree_test_create_repo "$test_count" && 508 + subtree_test_create_repo "$test_count/sub proj" && 509 + test_create_commit "$test_count" main1 && 510 + test_create_commit "$test_count/sub proj" sub1 && 452 511 ( 453 - cd "$subtree_test_count" && 454 - git fetch ./"sub proj" master && 512 + cd "$test_count" && 513 + git fetch ./"sub proj" HEAD && 455 514 git branch subproj-br FETCH_HEAD && 456 515 git subtree add --prefix="sub dir" FETCH_HEAD 457 516 ) && 458 - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && 459 - test_create_commit "$subtree_test_count" main2 && 460 - test_create_commit "$subtree_test_count/sub proj" sub2 && 461 - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && 517 + test_create_commit "$test_count" "sub dir"/main-sub1 && 518 + test_create_commit "$test_count" main2 && 519 + test_create_commit "$test_count/sub proj" sub2 && 520 + test_create_commit "$test_count" "sub dir"/main-sub2 && 462 521 ( 463 - cd "$subtree_test_count" && 464 - git fetch ./"sub proj" master && 522 + cd "$test_count" && 523 + git fetch ./"sub proj" HEAD && 465 524 git subtree merge --prefix="sub dir" FETCH_HEAD && 466 525 split_hash=$(git subtree split --prefix="sub dir" --annotate="*") && 467 526 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br && 468 - check_equal "$(git rev-parse subproj-br)" "$split_hash" 527 + test "$(git rev-parse subproj-br)" = "$split_hash" 469 528 ) 470 529 ' 471 530 472 - next_test 473 531 test_expect_success 'split "sub dir"/ with --branch for an incompatible branch' ' 474 - subtree_test_create_repo "$subtree_test_count" && 475 - subtree_test_create_repo "$subtree_test_count/sub proj" && 476 - test_create_commit "$subtree_test_count" main1 && 477 - test_create_commit "$subtree_test_count/sub proj" sub1 && 532 + subtree_test_create_repo "$test_count" && 533 + subtree_test_create_repo "$test_count/sub proj" && 534 + test_create_commit "$test_count" main1 && 535 + test_create_commit "$test_count/sub proj" sub1 && 478 536 ( 479 - cd "$subtree_test_count" && 537 + cd "$test_count" && 480 538 git branch init HEAD && 481 - git fetch ./"sub proj" master && 539 + git fetch ./"sub proj" HEAD && 482 540 git subtree add --prefix="sub dir" FETCH_HEAD 483 541 ) && 484 - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && 485 - test_create_commit "$subtree_test_count" main2 && 486 - test_create_commit "$subtree_test_count/sub proj" sub2 && 487 - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && 542 + test_create_commit "$test_count" "sub dir"/main-sub1 && 543 + test_create_commit "$test_count" main2 && 544 + test_create_commit "$test_count/sub proj" sub2 && 545 + test_create_commit "$test_count" "sub dir"/main-sub2 && 488 546 ( 489 - cd "$subtree_test_count" && 490 - git fetch ./"sub proj" master && 547 + cd "$test_count" && 548 + git fetch ./"sub proj" HEAD && 491 549 git subtree merge --prefix="sub dir" FETCH_HEAD && 492 550 test_must_fail git subtree split --prefix="sub dir" --branch init 493 551 ) 494 552 ' 495 553 496 554 # 555 + # Tests for 'git subtree pull' 556 + # 557 + 558 + test_expect_success 'pull requires option --prefix' ' 559 + subtree_test_create_repo "$test_count" && 560 + subtree_test_create_repo "$test_count/sub proj" && 561 + test_create_commit "$test_count" main1 && 562 + test_create_commit "$test_count/sub proj" sub1 && 563 + ( 564 + cd "$test_count" && 565 + git fetch ./"sub proj" HEAD && 566 + git subtree add --prefix="sub dir" FETCH_HEAD 567 + ) && 568 + test_create_commit "$test_count/sub proj" sub2 && 569 + ( 570 + cd "$test_count" && 571 + test_must_fail git subtree pull ./"sub proj" HEAD >out 2>err && 572 + 573 + echo "You must provide the --prefix option." >expected && 574 + test_must_be_empty out && 575 + test_cmp expected err 576 + ) 577 + ' 578 + 579 + test_expect_success 'pull requires path given by option --prefix must exist' ' 580 + subtree_test_create_repo "$test_count" && 581 + subtree_test_create_repo "$test_count/sub proj" && 582 + test_create_commit "$test_count" main1 && 583 + test_create_commit "$test_count/sub proj" sub1 && 584 + ( 585 + test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" HEAD >out 2>err && 586 + 587 + echo "'\''sub dir'\'' does not exist; use '\''git subtree add'\''" >expected && 588 + test_must_be_empty out && 589 + test_cmp expected err 590 + ) 591 + ' 592 + 593 + test_expect_success 'pull basic operation' ' 594 + subtree_test_create_repo "$test_count" && 595 + subtree_test_create_repo "$test_count/sub proj" && 596 + test_create_commit "$test_count" main1 && 597 + test_create_commit "$test_count/sub proj" sub1 && 598 + ( 599 + cd "$test_count" && 600 + git fetch ./"sub proj" HEAD && 601 + git subtree add --prefix="sub dir" FETCH_HEAD 602 + ) && 603 + test_create_commit "$test_count/sub proj" sub2 && 604 + ( 605 + cd "$test_count" && 606 + exp=$(git -C "sub proj" rev-parse --verify HEAD:) && 607 + git subtree pull --prefix="sub dir" ./"sub proj" HEAD && 608 + act=$(git rev-parse --verify HEAD:"sub dir") && 609 + test "$act" = "$exp" 610 + ) 611 + ' 612 + 613 + test_expect_success 'pull rejects flags for split' ' 614 + subtree_test_create_repo "$test_count" && 615 + subtree_test_create_repo "$test_count/sub proj" && 616 + test_create_commit "$test_count" main1 && 617 + test_create_commit "$test_count/sub proj" sub1 && 618 + ( 619 + cd "$test_count" && 620 + git fetch ./"sub proj" HEAD && 621 + git subtree add --prefix="sub dir" FETCH_HEAD 622 + ) && 623 + test_create_commit "$test_count/sub proj" sub2 && 624 + ( 625 + test_must_fail git subtree pull --prefix="sub dir" --annotate=foo ./"sub proj" HEAD && 626 + test_must_fail git subtree pull --prefix="sub dir" --branch=foo ./"sub proj" HEAD && 627 + test_must_fail git subtree pull --prefix="sub dir" --ignore-joins ./"sub proj" HEAD && 628 + test_must_fail git subtree pull --prefix="sub dir" --onto=foo ./"sub proj" HEAD && 629 + test_must_fail git subtree pull --prefix="sub dir" --rejoin ./"sub proj" HEAD 630 + ) 631 + ' 632 + 633 + # 634 + # Tests for 'git subtree push' 635 + # 636 + 637 + test_expect_success 'push requires option --prefix' ' 638 + subtree_test_create_repo "$test_count" && 639 + subtree_test_create_repo "$test_count/sub proj" && 640 + test_create_commit "$test_count" main1 && 641 + test_create_commit "$test_count/sub proj" sub1 && 642 + ( 643 + cd "$test_count" && 644 + git fetch ./"sub proj" HEAD && 645 + git subtree add --prefix="sub dir" FETCH_HEAD && 646 + echo "You must provide the --prefix option." >expected && 647 + test_must_fail git subtree push "./sub proj" from-mainline >actual 2>&1 && 648 + test_debug "printf '"expected: "'" && 649 + test_debug "cat expected" && 650 + test_debug "printf '"actual: "'" && 651 + test_debug "cat actual" && 652 + test_cmp expected actual 653 + ) 654 + ' 655 + 656 + test_expect_success 'push requires path given by option --prefix must exist' ' 657 + subtree_test_create_repo "$test_count" && 658 + subtree_test_create_repo "$test_count/sub proj" && 659 + test_create_commit "$test_count" main1 && 660 + test_create_commit "$test_count/sub proj" sub1 && 661 + ( 662 + cd "$test_count" && 663 + git fetch ./"sub proj" HEAD && 664 + git subtree add --prefix="sub dir" FETCH_HEAD && 665 + echo "'\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" >expected && 666 + test_must_fail git subtree push --prefix=non-existent-directory "./sub proj" from-mainline >actual 2>&1 && 667 + test_debug "printf '"expected: "'" && 668 + test_debug "cat expected" && 669 + test_debug "printf '"actual: "'" && 670 + test_debug "cat actual" && 671 + test_cmp expected actual 672 + ) 673 + ' 674 + 675 + test_expect_success 'push rejects flags for add' ' 676 + subtree_test_create_repo "$test_count" && 677 + subtree_test_create_repo "$test_count/sub proj" && 678 + test_create_commit "$test_count" main1 && 679 + test_create_commit "$test_count/sub proj" sub1 && 680 + ( 681 + cd "$test_count" && 682 + git fetch ./"sub proj" HEAD && 683 + git subtree add --prefix="sub dir" FETCH_HEAD 684 + ) && 685 + test_create_commit "$test_count" "sub dir"/main-sub1 && 686 + test_create_commit "$test_count" main2 && 687 + test_create_commit "$test_count/sub proj" sub2 && 688 + test_create_commit "$test_count" "sub dir"/main-sub2 && 689 + ( 690 + cd "$test_count" && 691 + git fetch ./"sub proj" HEAD && 692 + git subtree merge --prefix="sub dir" FETCH_HEAD && 693 + test_wrong_flag git subtree split --prefix="sub dir" --squash ./"sub proj" from-mainline && 694 + test_wrong_flag git subtree split --prefix="sub dir" --message=foo ./"sub proj" from-mainline 695 + ) 696 + ' 697 + 698 + test_expect_success 'push basic operation' ' 699 + subtree_test_create_repo "$test_count" && 700 + subtree_test_create_repo "$test_count/sub proj" && 701 + test_create_commit "$test_count" main1 && 702 + test_create_commit "$test_count/sub proj" sub1 && 703 + ( 704 + cd "$test_count" && 705 + git fetch ./"sub proj" HEAD && 706 + git subtree add --prefix="sub dir" FETCH_HEAD 707 + ) && 708 + test_create_commit "$test_count" "sub dir"/main-sub1 && 709 + test_create_commit "$test_count" main2 && 710 + test_create_commit "$test_count/sub proj" sub2 && 711 + test_create_commit "$test_count" "sub dir"/main-sub2 && 712 + ( 713 + cd "$test_count" && 714 + git fetch ./"sub proj" HEAD && 715 + git subtree merge --prefix="sub dir" FETCH_HEAD && 716 + before=$(git rev-parse --verify HEAD) && 717 + split_hash=$(git subtree split --prefix="sub dir") && 718 + git subtree push --prefix="sub dir" ./"sub proj" from-mainline && 719 + test "$before" = "$(git rev-parse --verify HEAD)" && 720 + test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)" 721 + ) 722 + ' 723 + 724 + test_expect_success 'push sub dir/ with --rejoin' ' 725 + subtree_test_create_repo "$test_count" && 726 + subtree_test_create_repo "$test_count/sub proj" && 727 + test_create_commit "$test_count" main1 && 728 + test_create_commit "$test_count/sub proj" sub1 && 729 + ( 730 + cd "$test_count" && 731 + git fetch ./"sub proj" HEAD && 732 + git subtree add --prefix="sub dir" FETCH_HEAD 733 + ) && 734 + test_create_commit "$test_count" "sub dir"/main-sub1 && 735 + test_create_commit "$test_count" main2 && 736 + test_create_commit "$test_count/sub proj" sub2 && 737 + test_create_commit "$test_count" "sub dir"/main-sub2 && 738 + ( 739 + cd "$test_count" && 740 + git fetch ./"sub proj" HEAD && 741 + git subtree merge --prefix="sub dir" FETCH_HEAD && 742 + split_hash=$(git subtree split --prefix="sub dir" --annotate="*") && 743 + git subtree push --prefix="sub dir" --annotate="*" --rejoin ./"sub proj" from-mainline && 744 + test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" && 745 + test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)" 746 + ) 747 + ' 748 + 749 + test_expect_success 'push sub dir/ with --rejoin from scratch' ' 750 + subtree_test_create_repo "$test_count" && 751 + test_create_commit "$test_count" main1 && 752 + ( 753 + cd "$test_count" && 754 + mkdir "sub dir" && 755 + echo file >"sub dir"/file && 756 + git add "sub dir/file" && 757 + git commit -m"sub dir file" && 758 + split_hash=$(git subtree split --prefix="sub dir" --rejoin) && 759 + git init --bare "sub proj.git" && 760 + git subtree push --prefix="sub dir" --rejoin ./"sub proj.git" from-mainline && 761 + test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$split_hash'\''" && 762 + test "$split_hash" = "$(git -C "sub proj.git" rev-parse --verify refs/heads/from-mainline)" 763 + ) 764 + ' 765 + 766 + test_expect_success 'push sub dir/ with --rejoin and --message' ' 767 + subtree_test_create_repo "$test_count" && 768 + subtree_test_create_repo "$test_count/sub proj" && 769 + test_create_commit "$test_count" main1 && 770 + test_create_commit "$test_count/sub proj" sub1 && 771 + ( 772 + cd "$test_count" && 773 + git fetch ./"sub proj" HEAD && 774 + git subtree add --prefix="sub dir" FETCH_HEAD 775 + ) && 776 + test_create_commit "$test_count" "sub dir"/main-sub1 && 777 + test_create_commit "$test_count" main2 && 778 + test_create_commit "$test_count/sub proj" sub2 && 779 + test_create_commit "$test_count" "sub dir"/main-sub2 && 780 + ( 781 + cd "$test_count" && 782 + git fetch ./"sub proj" HEAD && 783 + git subtree merge --prefix="sub dir" FETCH_HEAD && 784 + git subtree push --prefix="sub dir" --message="Split & rejoin" --annotate="*" --rejoin ./"sub proj" from-mainline && 785 + test "$(last_commit_subject)" = "Split & rejoin" && 786 + split_hash="$(git rev-parse --verify HEAD^2)" && 787 + test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)" 788 + ) 789 + ' 790 + 791 + test_expect_success 'push "sub dir"/ with --rejoin and --squash' ' 792 + subtree_test_create_repo "$test_count" && 793 + subtree_test_create_repo "$test_count/sub proj" && 794 + test_create_commit "$test_count" main1 && 795 + test_create_commit "$test_count/sub proj" sub1 && 796 + ( 797 + cd "$test_count" && 798 + git fetch ./"sub proj" HEAD && 799 + git subtree add --prefix="sub dir" --squash FETCH_HEAD 800 + ) && 801 + test_create_commit "$test_count" "sub dir"/main-sub1 && 802 + test_create_commit "$test_count" main2 && 803 + test_create_commit "$test_count/sub proj" sub2 && 804 + test_create_commit "$test_count" "sub dir"/main-sub2 && 805 + ( 806 + cd "$test_count" && 807 + git subtree pull --prefix="sub dir" --squash ./"sub proj" HEAD && 808 + MAIN=$(git rev-parse --verify HEAD) && 809 + SUB=$(git -C "sub proj" rev-parse --verify HEAD) && 810 + 811 + SPLIT=$(git subtree split --prefix="sub dir" --annotate="*") && 812 + git subtree push --prefix="sub dir" --annotate="*" --rejoin --squash ./"sub proj" from-mainline && 813 + 814 + test_must_fail git merge-base --is-ancestor $SUB HEAD && 815 + test_must_fail git merge-base --is-ancestor $SPLIT HEAD && 816 + git rev-list HEAD ^$MAIN >commit-list && 817 + test_line_count = 2 commit-list && 818 + test "$(git rev-parse --verify HEAD:)" = "$(git rev-parse --verify $MAIN:)" && 819 + test "$(git rev-parse --verify HEAD:"sub dir")" = "$(git rev-parse --verify $SPLIT:)" && 820 + test "$(git rev-parse --verify HEAD^1)" = $MAIN && 821 + test "$(git rev-parse --verify HEAD^2)" != $SPLIT && 822 + test "$(git rev-parse --verify HEAD^2:)" = "$(git rev-parse --verify $SPLIT:)" && 823 + test "$(last_commit_subject)" = "Split '\''sub dir/'\'' into commit '\''$SPLIT'\''" && 824 + test "$SPLIT" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)" 825 + ) 826 + ' 827 + 828 + test_expect_success 'push "sub dir"/ with --branch' ' 829 + subtree_test_create_repo "$test_count" && 830 + subtree_test_create_repo "$test_count/sub proj" && 831 + test_create_commit "$test_count" main1 && 832 + test_create_commit "$test_count/sub proj" sub1 && 833 + ( 834 + cd "$test_count" && 835 + git fetch ./"sub proj" HEAD && 836 + git subtree add --prefix="sub dir" FETCH_HEAD 837 + ) && 838 + test_create_commit "$test_count" "sub dir"/main-sub1 && 839 + test_create_commit "$test_count" main2 && 840 + test_create_commit "$test_count/sub proj" sub2 && 841 + test_create_commit "$test_count" "sub dir"/main-sub2 && 842 + ( 843 + cd "$test_count" && 844 + git fetch ./"sub proj" HEAD && 845 + git subtree merge --prefix="sub dir" FETCH_HEAD && 846 + split_hash=$(git subtree split --prefix="sub dir" --annotate="*") && 847 + git subtree push --prefix="sub dir" --annotate="*" --branch subproj-br ./"sub proj" from-mainline && 848 + test "$(git rev-parse subproj-br)" = "$split_hash" && 849 + test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)" 850 + ) 851 + ' 852 + 853 + test_expect_success 'check hash of push' ' 854 + subtree_test_create_repo "$test_count" && 855 + subtree_test_create_repo "$test_count/sub proj" && 856 + test_create_commit "$test_count" main1 && 857 + test_create_commit "$test_count/sub proj" sub1 && 858 + ( 859 + cd "$test_count" && 860 + git fetch ./"sub proj" HEAD && 861 + git subtree add --prefix="sub dir" FETCH_HEAD 862 + ) && 863 + test_create_commit "$test_count" "sub dir"/main-sub1 && 864 + test_create_commit "$test_count" main2 && 865 + test_create_commit "$test_count/sub proj" sub2 && 866 + test_create_commit "$test_count" "sub dir"/main-sub2 && 867 + ( 868 + cd "$test_count" && 869 + git fetch ./"sub proj" HEAD && 870 + git subtree merge --prefix="sub dir" FETCH_HEAD && 871 + split_hash=$(git subtree split --prefix="sub dir" --annotate="*") && 872 + git subtree push --prefix="sub dir" --annotate="*" --branch subproj-br ./"sub proj" from-mainline && 873 + test "$(git rev-parse subproj-br)" = "$split_hash" && 874 + # Check hash of split 875 + new_hash=$(git rev-parse subproj-br^2) && 876 + ( 877 + cd ./"sub proj" && 878 + subdir_hash=$(git rev-parse HEAD) && 879 + test "$new_hash" = "$subdir_hash" 880 + ) && 881 + test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)" 882 + ) 883 + ' 884 + 885 + test_expect_success 'push "sub dir"/ with --branch for an existing branch' ' 886 + subtree_test_create_repo "$test_count" && 887 + subtree_test_create_repo "$test_count/sub proj" && 888 + test_create_commit "$test_count" main1 && 889 + test_create_commit "$test_count/sub proj" sub1 && 890 + ( 891 + cd "$test_count" && 892 + git fetch ./"sub proj" HEAD && 893 + git branch subproj-br FETCH_HEAD && 894 + git subtree add --prefix="sub dir" FETCH_HEAD 895 + ) && 896 + test_create_commit "$test_count" "sub dir"/main-sub1 && 897 + test_create_commit "$test_count" main2 && 898 + test_create_commit "$test_count/sub proj" sub2 && 899 + test_create_commit "$test_count" "sub dir"/main-sub2 && 900 + ( 901 + cd "$test_count" && 902 + git fetch ./"sub proj" HEAD && 903 + git subtree merge --prefix="sub dir" FETCH_HEAD && 904 + split_hash=$(git subtree split --prefix="sub dir" --annotate="*") && 905 + git subtree push --prefix="sub dir" --annotate="*" --branch subproj-br ./"sub proj" from-mainline && 906 + test "$(git rev-parse subproj-br)" = "$split_hash" && 907 + test "$split_hash" = "$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline)" 908 + ) 909 + ' 910 + 911 + test_expect_success 'push "sub dir"/ with --branch for an incompatible branch' ' 912 + subtree_test_create_repo "$test_count" && 913 + subtree_test_create_repo "$test_count/sub proj" && 914 + test_create_commit "$test_count" main1 && 915 + test_create_commit "$test_count/sub proj" sub1 && 916 + ( 917 + cd "$test_count" && 918 + git branch init HEAD && 919 + git fetch ./"sub proj" HEAD && 920 + git subtree add --prefix="sub dir" FETCH_HEAD 921 + ) && 922 + test_create_commit "$test_count" "sub dir"/main-sub1 && 923 + test_create_commit "$test_count" main2 && 924 + test_create_commit "$test_count/sub proj" sub2 && 925 + test_create_commit "$test_count" "sub dir"/main-sub2 && 926 + ( 927 + cd "$test_count" && 928 + git fetch ./"sub proj" HEAD && 929 + git subtree merge --prefix="sub dir" FETCH_HEAD && 930 + test_must_fail git subtree push --prefix="sub dir" --branch init "./sub proj" from-mainline 931 + ) 932 + ' 933 + 934 + test_expect_success 'push "sub dir"/ with a local rev' ' 935 + subtree_test_create_repo "$test_count" && 936 + subtree_test_create_repo "$test_count/sub proj" && 937 + test_create_commit "$test_count" main1 && 938 + test_create_commit "$test_count/sub proj" sub1 && 939 + ( 940 + cd "$test_count" && 941 + git fetch ./"sub proj" HEAD && 942 + git subtree add --prefix="sub dir" FETCH_HEAD 943 + ) && 944 + test_create_commit "$test_count" "sub dir"/main-sub1 && 945 + test_create_commit "$test_count" "sub dir"/main-sub2 && 946 + ( 947 + cd "$test_count" && 948 + bad_tree=$(git rev-parse --verify HEAD:"sub dir") && 949 + good_tree=$(git rev-parse --verify HEAD^:"sub dir") && 950 + git subtree push --prefix="sub dir" --annotate="*" ./"sub proj" HEAD^:from-mainline && 951 + split_tree=$(git -C "sub proj" rev-parse --verify refs/heads/from-mainline:) && 952 + test "$split_tree" = "$good_tree" 953 + ) 954 + ' 955 + 956 + # 497 957 # Validity checking 498 958 # 499 959 500 - next_test 501 960 test_expect_success 'make sure exactly the right set of files ends up in the subproj' ' 502 - subtree_test_create_repo "$subtree_test_count" && 503 - subtree_test_create_repo "$subtree_test_count/sub proj" && 504 - test_create_commit "$subtree_test_count" main1 && 505 - test_create_commit "$subtree_test_count/sub proj" sub1 && 961 + subtree_test_create_repo "$test_count" && 962 + subtree_test_create_repo "$test_count/sub proj" && 963 + test_create_commit "$test_count" main1 && 964 + test_create_commit "$test_count/sub proj" sub1 && 506 965 ( 507 - cd "$subtree_test_count" && 508 - git fetch ./"sub proj" master && 966 + cd "$test_count" && 967 + git fetch ./"sub proj" HEAD && 509 968 git subtree add --prefix="sub dir" FETCH_HEAD 510 969 ) && 511 - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && 512 - test_create_commit "$subtree_test_count" main2 && 513 - test_create_commit "$subtree_test_count/sub proj" sub2 && 514 - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && 970 + test_create_commit "$test_count" "sub dir"/main-sub1 && 971 + test_create_commit "$test_count" main2 && 972 + test_create_commit "$test_count/sub proj" sub2 && 973 + test_create_commit "$test_count" "sub dir"/main-sub2 && 515 974 ( 516 - cd "$subtree_test_count" && 517 - git fetch ./"sub proj" master && 975 + cd "$test_count" && 976 + git fetch ./"sub proj" HEAD && 518 977 git subtree merge --prefix="sub dir" FETCH_HEAD && 519 978 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin 520 979 ) && 521 - test_create_commit "$subtree_test_count/sub proj" sub3 && 522 - test_create_commit "$subtree_test_count" "sub dir"/main-sub3 && 980 + test_create_commit "$test_count/sub proj" sub3 && 981 + test_create_commit "$test_count" "sub dir"/main-sub3 && 523 982 ( 524 - cd "$subtree_test_count/sub proj" && 983 + cd "$test_count/sub proj" && 525 984 git fetch .. subproj-br && 526 985 git merge FETCH_HEAD 527 986 ) && 528 - test_create_commit "$subtree_test_count/sub proj" sub4 && 987 + test_create_commit "$test_count/sub proj" sub4 && 529 988 ( 530 - cd "$subtree_test_count" && 989 + cd "$test_count" && 531 990 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin 532 991 ) && 533 - test_create_commit "$subtree_test_count" "sub dir"/main-sub4 && 992 + test_create_commit "$test_count" "sub dir"/main-sub4 && 534 993 ( 535 - cd "$subtree_test_count" && 994 + cd "$test_count" && 536 995 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin 537 996 ) && 538 997 ( 539 - cd "$subtree_test_count/sub proj" && 998 + cd "$test_count/sub proj" && 540 999 git fetch .. subproj-br && 541 1000 git merge FETCH_HEAD && 542 1001 ··· 547 1006 ) 548 1007 ' 549 1008 550 - next_test 551 1009 test_expect_success 'make sure the subproj *only* contains commits that affect the "sub dir"' ' 552 - subtree_test_create_repo "$subtree_test_count" && 553 - subtree_test_create_repo "$subtree_test_count/sub proj" && 554 - test_create_commit "$subtree_test_count" main1 && 555 - test_create_commit "$subtree_test_count/sub proj" sub1 && 1010 + subtree_test_create_repo "$test_count" && 1011 + subtree_test_create_repo "$test_count/sub proj" && 1012 + test_create_commit "$test_count" main1 && 1013 + test_create_commit "$test_count/sub proj" sub1 && 556 1014 ( 557 - cd "$subtree_test_count" && 558 - git fetch ./"sub proj" master && 1015 + cd "$test_count" && 1016 + git fetch ./"sub proj" HEAD && 559 1017 git subtree add --prefix="sub dir" FETCH_HEAD 560 1018 ) && 561 - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && 562 - test_create_commit "$subtree_test_count" main2 && 563 - test_create_commit "$subtree_test_count/sub proj" sub2 && 564 - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && 1019 + test_create_commit "$test_count" "sub dir"/main-sub1 && 1020 + test_create_commit "$test_count" main2 && 1021 + test_create_commit "$test_count/sub proj" sub2 && 1022 + test_create_commit "$test_count" "sub dir"/main-sub2 && 565 1023 ( 566 - cd "$subtree_test_count" && 567 - git fetch ./"sub proj" master && 1024 + cd "$test_count" && 1025 + git fetch ./"sub proj" HEAD && 568 1026 git subtree merge --prefix="sub dir" FETCH_HEAD && 569 1027 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin 570 1028 ) && 571 - test_create_commit "$subtree_test_count/sub proj" sub3 && 572 - test_create_commit "$subtree_test_count" "sub dir"/main-sub3 && 1029 + test_create_commit "$test_count/sub proj" sub3 && 1030 + test_create_commit "$test_count" "sub dir"/main-sub3 && 573 1031 ( 574 - cd "$subtree_test_count/sub proj" && 1032 + cd "$test_count/sub proj" && 575 1033 git fetch .. subproj-br && 576 1034 git merge FETCH_HEAD 577 1035 ) && 578 - test_create_commit "$subtree_test_count/sub proj" sub4 && 1036 + test_create_commit "$test_count/sub proj" sub4 && 579 1037 ( 580 - cd "$subtree_test_count" && 1038 + cd "$test_count" && 581 1039 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin 582 1040 ) && 583 - test_create_commit "$subtree_test_count" "sub dir"/main-sub4 && 1041 + test_create_commit "$test_count" "sub dir"/main-sub4 && 584 1042 ( 585 - cd "$subtree_test_count" && 1043 + cd "$test_count" && 586 1044 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin 587 1045 ) && 588 1046 ( 589 - cd "$subtree_test_count/sub proj" && 1047 + cd "$test_count/sub proj" && 590 1048 git fetch .. subproj-br && 591 1049 git merge FETCH_HEAD && 592 1050 ··· 598 1056 ) 599 1057 ' 600 1058 601 - next_test 602 1059 test_expect_success 'make sure exactly the right set of files ends up in the mainline' ' 603 - subtree_test_create_repo "$subtree_test_count" && 604 - subtree_test_create_repo "$subtree_test_count/sub proj" && 605 - test_create_commit "$subtree_test_count" main1 && 606 - test_create_commit "$subtree_test_count/sub proj" sub1 && 1060 + subtree_test_create_repo "$test_count" && 1061 + subtree_test_create_repo "$test_count/sub proj" && 1062 + test_create_commit "$test_count" main1 && 1063 + test_create_commit "$test_count/sub proj" sub1 && 607 1064 ( 608 - cd "$subtree_test_count" && 609 - git fetch ./"sub proj" master && 1065 + cd "$test_count" && 1066 + git fetch ./"sub proj" HEAD && 610 1067 git subtree add --prefix="sub dir" FETCH_HEAD 611 1068 ) && 612 - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && 613 - test_create_commit "$subtree_test_count" main2 && 614 - test_create_commit "$subtree_test_count/sub proj" sub2 && 615 - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && 1069 + test_create_commit "$test_count" "sub dir"/main-sub1 && 1070 + test_create_commit "$test_count" main2 && 1071 + test_create_commit "$test_count/sub proj" sub2 && 1072 + test_create_commit "$test_count" "sub dir"/main-sub2 && 616 1073 ( 617 - cd "$subtree_test_count" && 618 - git fetch ./"sub proj" master && 1074 + cd "$test_count" && 1075 + git fetch ./"sub proj" HEAD && 619 1076 git subtree merge --prefix="sub dir" FETCH_HEAD && 620 1077 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin 621 1078 ) && 622 - test_create_commit "$subtree_test_count/sub proj" sub3 && 623 - test_create_commit "$subtree_test_count" "sub dir"/main-sub3 && 1079 + test_create_commit "$test_count/sub proj" sub3 && 1080 + test_create_commit "$test_count" "sub dir"/main-sub3 && 624 1081 ( 625 - cd "$subtree_test_count/sub proj" && 1082 + cd "$test_count/sub proj" && 626 1083 git fetch .. subproj-br && 627 1084 git merge FETCH_HEAD 628 1085 ) && 629 - test_create_commit "$subtree_test_count/sub proj" sub4 && 1086 + test_create_commit "$test_count/sub proj" sub4 && 630 1087 ( 631 - cd "$subtree_test_count" && 1088 + cd "$test_count" && 632 1089 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin 633 1090 ) && 634 - test_create_commit "$subtree_test_count" "sub dir"/main-sub4 && 1091 + test_create_commit "$test_count" "sub dir"/main-sub4 && 635 1092 ( 636 - cd "$subtree_test_count" && 1093 + cd "$test_count" && 637 1094 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin 638 1095 ) && 639 1096 ( 640 - cd "$subtree_test_count/sub proj" && 1097 + cd "$test_count/sub proj" && 641 1098 git fetch .. subproj-br && 642 1099 git merge FETCH_HEAD 643 1100 ) && 644 1101 ( 645 - cd "$subtree_test_count" && 646 - git subtree pull --prefix="sub dir" ./"sub proj" master && 1102 + cd "$test_count" && 1103 + git subtree pull --prefix="sub dir" ./"sub proj" HEAD && 647 1104 648 1105 test_write_lines main1 main2 >chkm && 649 1106 test_write_lines main-sub1 main-sub2 main-sub3 main-sub4 >chkms && ··· 657 1114 ) 658 1115 ' 659 1116 660 - next_test 661 1117 test_expect_success 'make sure each filename changed exactly once in the entire history' ' 662 - subtree_test_create_repo "$subtree_test_count" && 663 - subtree_test_create_repo "$subtree_test_count/sub proj" && 664 - test_create_commit "$subtree_test_count" main1 && 665 - test_create_commit "$subtree_test_count/sub proj" sub1 && 1118 + subtree_test_create_repo "$test_count" && 1119 + subtree_test_create_repo "$test_count/sub proj" && 1120 + test_create_commit "$test_count" main1 && 1121 + test_create_commit "$test_count/sub proj" sub1 && 666 1122 ( 667 - cd "$subtree_test_count" && 1123 + cd "$test_count" && 668 1124 git config log.date relative && 669 - git fetch ./"sub proj" master && 1125 + git fetch ./"sub proj" HEAD && 670 1126 git subtree add --prefix="sub dir" FETCH_HEAD 671 1127 ) && 672 - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && 673 - test_create_commit "$subtree_test_count" main2 && 674 - test_create_commit "$subtree_test_count/sub proj" sub2 && 675 - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && 1128 + test_create_commit "$test_count" "sub dir"/main-sub1 && 1129 + test_create_commit "$test_count" main2 && 1130 + test_create_commit "$test_count/sub proj" sub2 && 1131 + test_create_commit "$test_count" "sub dir"/main-sub2 && 676 1132 ( 677 - cd "$subtree_test_count" && 678 - git fetch ./"sub proj" master && 1133 + cd "$test_count" && 1134 + git fetch ./"sub proj" HEAD && 679 1135 git subtree merge --prefix="sub dir" FETCH_HEAD && 680 1136 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin 681 1137 ) && 682 - test_create_commit "$subtree_test_count/sub proj" sub3 && 683 - test_create_commit "$subtree_test_count" "sub dir"/main-sub3 && 1138 + test_create_commit "$test_count/sub proj" sub3 && 1139 + test_create_commit "$test_count" "sub dir"/main-sub3 && 684 1140 ( 685 - cd "$subtree_test_count/sub proj" && 1141 + cd "$test_count/sub proj" && 686 1142 git fetch .. subproj-br && 687 1143 git merge FETCH_HEAD 688 1144 ) && 689 - test_create_commit "$subtree_test_count/sub proj" sub4 && 1145 + test_create_commit "$test_count/sub proj" sub4 && 690 1146 ( 691 - cd "$subtree_test_count" && 1147 + cd "$test_count" && 692 1148 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin 693 1149 ) && 694 - test_create_commit "$subtree_test_count" "sub dir"/main-sub4 && 1150 + test_create_commit "$test_count" "sub dir"/main-sub4 && 695 1151 ( 696 - cd "$subtree_test_count" && 1152 + cd "$test_count" && 697 1153 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin 698 1154 ) && 699 1155 ( 700 - cd "$subtree_test_count/sub proj" && 1156 + cd "$test_count/sub proj" && 701 1157 git fetch .. subproj-br && 702 1158 git merge FETCH_HEAD 703 1159 ) && 704 1160 ( 705 - cd "$subtree_test_count" && 706 - git subtree pull --prefix="sub dir" ./"sub proj" master && 1161 + cd "$test_count" && 1162 + git subtree pull --prefix="sub dir" ./"sub proj" HEAD && 707 1163 708 1164 test_write_lines main1 main2 >chkm && 709 1165 test_write_lines sub1 sub2 sub3 sub4 >chks && ··· 723 1179 ) 724 1180 ' 725 1181 726 - next_test 727 1182 test_expect_success 'make sure the --rejoin commits never make it into subproj' ' 728 - subtree_test_create_repo "$subtree_test_count" && 729 - subtree_test_create_repo "$subtree_test_count/sub proj" && 730 - test_create_commit "$subtree_test_count" main1 && 731 - test_create_commit "$subtree_test_count/sub proj" sub1 && 1183 + subtree_test_create_repo "$test_count" && 1184 + subtree_test_create_repo "$test_count/sub proj" && 1185 + test_create_commit "$test_count" main1 && 1186 + test_create_commit "$test_count/sub proj" sub1 && 732 1187 ( 733 - cd "$subtree_test_count" && 734 - git fetch ./"sub proj" master && 1188 + cd "$test_count" && 1189 + git fetch ./"sub proj" HEAD && 735 1190 git subtree add --prefix="sub dir" FETCH_HEAD 736 1191 ) && 737 - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && 738 - test_create_commit "$subtree_test_count" main2 && 739 - test_create_commit "$subtree_test_count/sub proj" sub2 && 740 - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && 1192 + test_create_commit "$test_count" "sub dir"/main-sub1 && 1193 + test_create_commit "$test_count" main2 && 1194 + test_create_commit "$test_count/sub proj" sub2 && 1195 + test_create_commit "$test_count" "sub dir"/main-sub2 && 741 1196 ( 742 - cd "$subtree_test_count" && 743 - git fetch ./"sub proj" master && 1197 + cd "$test_count" && 1198 + git fetch ./"sub proj" HEAD && 744 1199 git subtree merge --prefix="sub dir" FETCH_HEAD && 745 1200 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin 746 1201 ) && 747 - test_create_commit "$subtree_test_count/sub proj" sub3 && 748 - test_create_commit "$subtree_test_count" "sub dir"/main-sub3 && 1202 + test_create_commit "$test_count/sub proj" sub3 && 1203 + test_create_commit "$test_count" "sub dir"/main-sub3 && 749 1204 ( 750 - cd "$subtree_test_count/sub proj" && 1205 + cd "$test_count/sub proj" && 751 1206 git fetch .. subproj-br && 752 1207 git merge FETCH_HEAD 753 1208 ) && 754 - test_create_commit "$subtree_test_count/sub proj" sub4 && 1209 + test_create_commit "$test_count/sub proj" sub4 && 755 1210 ( 756 - cd "$subtree_test_count" && 1211 + cd "$test_count" && 757 1212 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin 758 1213 ) && 759 - test_create_commit "$subtree_test_count" "sub dir"/main-sub4 && 1214 + test_create_commit "$test_count" "sub dir"/main-sub4 && 760 1215 ( 761 - cd "$subtree_test_count" && 1216 + cd "$test_count" && 762 1217 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin 763 1218 ) && 764 1219 ( 765 - cd "$subtree_test_count/sub proj" && 1220 + cd "$test_count/sub proj" && 766 1221 git fetch .. subproj-br && 767 1222 git merge FETCH_HEAD 768 1223 ) && 769 1224 ( 770 - cd "$subtree_test_count" && 771 - git subtree pull --prefix="sub dir" ./"sub proj" master && 772 - check_equal "$(git log --pretty=format:"%s" HEAD^2 | grep -i split)" "" 1225 + cd "$test_count" && 1226 + git subtree pull --prefix="sub dir" ./"sub proj" HEAD && 1227 + test "$(git log --pretty=format:"%s" HEAD^2 | grep -i split)" = "" 773 1228 ) 774 1229 ' 775 1230 776 - next_test 777 1231 test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' ' 778 - subtree_test_create_repo "$subtree_test_count" && 779 - subtree_test_create_repo "$subtree_test_count/sub proj" && 780 - test_create_commit "$subtree_test_count" main1 && 781 - test_create_commit "$subtree_test_count/sub proj" sub1 && 1232 + subtree_test_create_repo "$test_count" && 1233 + subtree_test_create_repo "$test_count/sub proj" && 1234 + test_create_commit "$test_count" main1 && 1235 + test_create_commit "$test_count/sub proj" sub1 && 782 1236 ( 783 - cd "$subtree_test_count" && 784 - git fetch ./"sub proj" master && 1237 + cd "$test_count" && 1238 + git fetch ./"sub proj" HEAD && 785 1239 git subtree add --prefix="sub dir" FETCH_HEAD 786 1240 ) && 787 - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && 788 - test_create_commit "$subtree_test_count" main2 && 789 - test_create_commit "$subtree_test_count/sub proj" sub2 && 790 - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && 1241 + test_create_commit "$test_count" "sub dir"/main-sub1 && 1242 + test_create_commit "$test_count" main2 && 1243 + test_create_commit "$test_count/sub proj" sub2 && 1244 + test_create_commit "$test_count" "sub dir"/main-sub2 && 791 1245 ( 792 - cd "$subtree_test_count" && 793 - git fetch ./"sub proj" master && 1246 + cd "$test_count" && 1247 + git fetch ./"sub proj" HEAD && 794 1248 git subtree merge --prefix="sub dir" FETCH_HEAD && 795 1249 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin 796 1250 ) && 797 - test_create_commit "$subtree_test_count/sub proj" sub3 && 798 - test_create_commit "$subtree_test_count" "sub dir"/main-sub3 && 1251 + test_create_commit "$test_count/sub proj" sub3 && 1252 + test_create_commit "$test_count" "sub dir"/main-sub3 && 799 1253 ( 800 - cd "$subtree_test_count/sub proj" && 1254 + cd "$test_count/sub proj" && 801 1255 git fetch .. subproj-br && 802 1256 git merge FETCH_HEAD 803 1257 ) && 804 - test_create_commit "$subtree_test_count/sub proj" sub4 && 1258 + test_create_commit "$test_count/sub proj" sub4 && 805 1259 ( 806 - cd "$subtree_test_count" && 1260 + cd "$test_count" && 807 1261 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin 808 1262 ) && 809 - test_create_commit "$subtree_test_count" "sub dir"/main-sub4 && 1263 + test_create_commit "$test_count" "sub dir"/main-sub4 && 810 1264 ( 811 - cd "$subtree_test_count" && 1265 + cd "$test_count" && 812 1266 git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin 813 1267 ) && 814 1268 ( 815 - cd "$subtree_test_count/sub proj" && 1269 + cd "$test_count/sub proj" && 816 1270 git fetch .. subproj-br && 817 1271 git merge FETCH_HEAD 818 1272 ) && 819 1273 ( 820 - cd "$subtree_test_count" && 821 - git subtree pull --prefix="sub dir" ./"sub proj" master && 1274 + cd "$test_count" && 1275 + git subtree pull --prefix="sub dir" ./"sub proj" HEAD && 822 1276 823 1277 # They are meaningless to subproj since one side of the merge refers to the mainline 824 - check_equal "$(git log --pretty=format:"%s%n%b" HEAD^2 | grep "git-subtree.*:")" "" 1278 + test "$(git log --pretty=format:"%s%n%b" HEAD^2 | grep "git-subtree.*:")" = "" 825 1279 ) 826 1280 ' 827 1281 ··· 829 1283 # A new set of tests 830 1284 # 831 1285 832 - next_test 833 1286 test_expect_success 'make sure "git subtree split" find the correct parent' ' 834 - subtree_test_create_repo "$subtree_test_count" && 835 - subtree_test_create_repo "$subtree_test_count/sub proj" && 836 - test_create_commit "$subtree_test_count" main1 && 837 - test_create_commit "$subtree_test_count/sub proj" sub1 && 1287 + subtree_test_create_repo "$test_count" && 1288 + subtree_test_create_repo "$test_count/sub proj" && 1289 + test_create_commit "$test_count" main1 && 1290 + test_create_commit "$test_count/sub proj" sub1 && 838 1291 ( 839 - cd "$subtree_test_count" && 840 - git fetch ./"sub proj" master && 1292 + cd "$test_count" && 1293 + git fetch ./"sub proj" HEAD && 841 1294 git subtree add --prefix="sub dir" FETCH_HEAD 842 1295 ) && 843 - test_create_commit "$subtree_test_count/sub proj" sub2 && 1296 + test_create_commit "$test_count/sub proj" sub2 && 844 1297 ( 845 - cd "$subtree_test_count" && 846 - git fetch ./"sub proj" master && 1298 + cd "$test_count" && 1299 + git fetch ./"sub proj" HEAD && 847 1300 git branch subproj-ref FETCH_HEAD && 848 1301 git subtree merge --prefix="sub dir" FETCH_HEAD 849 1302 ) && 850 - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && 1303 + test_create_commit "$test_count" "sub dir"/main-sub1 && 851 1304 ( 852 - cd "$subtree_test_count" && 1305 + cd "$test_count" && 853 1306 git subtree split --prefix="sub dir" --branch subproj-br && 854 1307 855 1308 # at this point, the new commit parent should be subproj-ref, if it is 856 - # not, something went wrong (the "newparent" of "master~" commit should 1309 + # not, something went wrong (the "newparent" of "HEAD~" commit should 857 1310 # have been sub2, but it was not, because its cache was not set to 858 1311 # itself) 859 - check_equal "$(git log --pretty=format:%P -1 subproj-br)" "$(git rev-parse subproj-ref)" 1312 + test "$(git log --pretty=format:%P -1 subproj-br)" = "$(git rev-parse subproj-ref)" 860 1313 ) 861 1314 ' 862 1315 863 - next_test 864 1316 test_expect_success 'split a new subtree without --onto option' ' 865 - subtree_test_create_repo "$subtree_test_count" && 866 - subtree_test_create_repo "$subtree_test_count/sub proj" && 867 - test_create_commit "$subtree_test_count" main1 && 868 - test_create_commit "$subtree_test_count/sub proj" sub1 && 1317 + subtree_test_create_repo "$test_count" && 1318 + subtree_test_create_repo "$test_count/sub proj" && 1319 + test_create_commit "$test_count" main1 && 1320 + test_create_commit "$test_count/sub proj" sub1 && 869 1321 ( 870 - cd "$subtree_test_count" && 871 - git fetch ./"sub proj" master && 1322 + cd "$test_count" && 1323 + git fetch ./"sub proj" HEAD && 872 1324 git subtree add --prefix="sub dir" FETCH_HEAD 873 1325 ) && 874 - test_create_commit "$subtree_test_count/sub proj" sub2 && 1326 + test_create_commit "$test_count/sub proj" sub2 && 875 1327 ( 876 - cd "$subtree_test_count" && 877 - git fetch ./"sub proj" master && 1328 + cd "$test_count" && 1329 + git fetch ./"sub proj" HEAD && 878 1330 git subtree merge --prefix="sub dir" FETCH_HEAD 879 1331 ) && 880 - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && 1332 + test_create_commit "$test_count" "sub dir"/main-sub1 && 881 1333 ( 882 - cd "$subtree_test_count" && 1334 + cd "$test_count" && 883 1335 git subtree split --prefix="sub dir" --branch subproj-br 884 1336 ) && 885 - mkdir "$subtree_test_count"/"sub dir2" && 886 - test_create_commit "$subtree_test_count" "sub dir2"/main-sub2 && 1337 + mkdir "$test_count"/"sub dir2" && 1338 + test_create_commit "$test_count" "sub dir2"/main-sub2 && 887 1339 ( 888 - cd "$subtree_test_count" && 1340 + cd "$test_count" && 889 1341 890 1342 # also test that we still can split out an entirely new subtree 891 1343 # if the parent of the first commit in the tree is not empty, 892 1344 # then the new subtree has accidentally been attached to something 893 1345 git subtree split --prefix="sub dir2" --branch subproj2-br && 894 - check_equal "$(git log --pretty=format:%P -1 subproj2-br)" "" 1346 + test "$(git log --pretty=format:%P -1 subproj2-br)" = "" 895 1347 ) 896 1348 ' 897 1349 898 - next_test 899 1350 test_expect_success 'verify one file change per commit' ' 900 - subtree_test_create_repo "$subtree_test_count" && 901 - subtree_test_create_repo "$subtree_test_count/sub proj" && 902 - test_create_commit "$subtree_test_count" main1 && 903 - test_create_commit "$subtree_test_count/sub proj" sub1 && 1351 + subtree_test_create_repo "$test_count" && 1352 + subtree_test_create_repo "$test_count/sub proj" && 1353 + test_create_commit "$test_count" main1 && 1354 + test_create_commit "$test_count/sub proj" sub1 && 904 1355 ( 905 - cd "$subtree_test_count" && 906 - git fetch ./"sub proj" master && 1356 + cd "$test_count" && 1357 + git fetch ./"sub proj" HEAD && 907 1358 git branch sub1 FETCH_HEAD && 908 1359 git subtree add --prefix="sub dir" sub1 909 1360 ) && 910 - test_create_commit "$subtree_test_count/sub proj" sub2 && 1361 + test_create_commit "$test_count/sub proj" sub2 && 911 1362 ( 912 - cd "$subtree_test_count" && 913 - git fetch ./"sub proj" master && 1363 + cd "$test_count" && 1364 + git fetch ./"sub proj" HEAD && 914 1365 git subtree merge --prefix="sub dir" FETCH_HEAD 915 1366 ) && 916 - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && 1367 + test_create_commit "$test_count" "sub dir"/main-sub1 && 917 1368 ( 918 - cd "$subtree_test_count" && 1369 + cd "$test_count" && 919 1370 git subtree split --prefix="sub dir" --branch subproj-br 920 1371 ) && 921 - mkdir "$subtree_test_count"/"sub dir2" && 922 - test_create_commit "$subtree_test_count" "sub dir2"/main-sub2 && 1372 + mkdir "$test_count"/"sub dir2" && 1373 + test_create_commit "$test_count" "sub dir2"/main-sub2 && 923 1374 ( 924 - cd "$subtree_test_count" && 1375 + cd "$test_count" && 925 1376 git subtree split --prefix="sub dir2" --branch subproj2-br && 926 1377 927 - x= && 928 - git log --pretty=format:"commit: %H" | join_commits | 929 - ( 930 - while read commit a b; do 931 - test_debug "echo Verifying commit $commit" 932 - test_debug "echo a: $a" 933 - test_debug "echo b: $b" 934 - check_equal "$b" "" 935 - x=1 936 - done 937 - check_equal "$x" 1 938 - ) 1378 + git log --format="%H" >commit-list && 1379 + while read commit 1380 + do 1381 + git log -n1 --format="" --name-only "$commit" >file-list && 1382 + test_line_count -le 1 file-list || return 1 1383 + done <commit-list 939 1384 ) 940 1385 ' 941 1386 942 - next_test 943 1387 test_expect_success 'push split to subproj' ' 944 - subtree_test_create_repo "$subtree_test_count" && 945 - subtree_test_create_repo "$subtree_test_count/sub proj" && 946 - test_create_commit "$subtree_test_count" main1 && 947 - test_create_commit "$subtree_test_count/sub proj" sub1 && 1388 + subtree_test_create_repo "$test_count" && 1389 + subtree_test_create_repo "$test_count/sub proj" && 1390 + test_create_commit "$test_count" main1 && 1391 + test_create_commit "$test_count/sub proj" sub1 && 948 1392 ( 949 - cd "$subtree_test_count" && 950 - git fetch ./"sub proj" master && 1393 + cd "$test_count" && 1394 + git fetch ./"sub proj" HEAD && 951 1395 git subtree add --prefix="sub dir" FETCH_HEAD 952 1396 ) && 953 - test_create_commit "$subtree_test_count" "sub dir"/main-sub1 && 954 - test_create_commit "$subtree_test_count" main2 && 955 - test_create_commit "$subtree_test_count/sub proj" sub2 && 956 - test_create_commit "$subtree_test_count" "sub dir"/main-sub2 && 1397 + test_create_commit "$test_count" "sub dir"/main-sub1 && 1398 + test_create_commit "$test_count" main2 && 1399 + test_create_commit "$test_count/sub proj" sub2 && 1400 + test_create_commit "$test_count" "sub dir"/main-sub2 && 957 1401 ( 958 - cd $subtree_test_count/"sub proj" && 959 - git branch sub-branch-1 && 960 - cd .. && 961 - git fetch ./"sub proj" master && 1402 + cd $test_count/"sub proj" && 1403 + git branch sub-branch-1 && 1404 + cd .. && 1405 + git fetch ./"sub proj" HEAD && 962 1406 git subtree merge --prefix="sub dir" FETCH_HEAD 963 1407 ) && 964 - test_create_commit "$subtree_test_count" "sub dir"/main-sub3 && 965 - ( 966 - cd "$subtree_test_count" && 967 - git subtree push ./"sub proj" --prefix "sub dir" sub-branch-1 && 968 - cd ./"sub proj" && 969 - git checkout sub-branch-1 && 970 - check_equal "$(last_commit_message)" "sub dir/main-sub3" 1408 + test_create_commit "$test_count" "sub dir"/main-sub3 && 1409 + ( 1410 + cd "$test_count" && 1411 + git subtree push ./"sub proj" --prefix "sub dir" sub-branch-1 && 1412 + cd ./"sub proj" && 1413 + git checkout sub-branch-1 && 1414 + test "$(last_commit_subject)" = "sub dir/main-sub3" 971 1415 ) 972 1416 ' 973 1417 ··· 991 1435 # set of commits. 992 1436 # 993 1437 994 - next_test 995 1438 test_expect_success 'subtree descendant check' ' 996 - subtree_test_create_repo "$subtree_test_count" && 997 - test_create_commit "$subtree_test_count" folder_subtree/a && 1439 + subtree_test_create_repo "$test_count" && 1440 + defaultBranch=$(sed "s,ref: refs/heads/,," "$test_count/.git/HEAD") && 1441 + test_create_commit "$test_count" folder_subtree/a && 998 1442 ( 999 - cd "$subtree_test_count" && 1443 + cd "$test_count" && 1000 1444 git branch branch 1001 1445 ) && 1002 - test_create_commit "$subtree_test_count" folder_subtree/0 && 1003 - test_create_commit "$subtree_test_count" folder_subtree/b && 1004 - cherry=$(cd "$subtree_test_count"; git rev-parse HEAD) && 1446 + test_create_commit "$test_count" folder_subtree/0 && 1447 + test_create_commit "$test_count" folder_subtree/b && 1448 + cherry=$(cd "$test_count"; git rev-parse HEAD) && 1005 1449 ( 1006 - cd "$subtree_test_count" && 1450 + cd "$test_count" && 1007 1451 git checkout branch 1008 1452 ) && 1009 - test_create_commit "$subtree_test_count" commit_on_branch && 1453 + test_create_commit "$test_count" commit_on_branch && 1010 1454 ( 1011 - cd "$subtree_test_count" && 1455 + cd "$test_count" && 1012 1456 git cherry-pick $cherry && 1013 - git checkout master && 1457 + git checkout $defaultBranch && 1014 1458 git merge -m "merge should be kept on subtree" branch && 1015 1459 git branch no_subtree_work_branch 1016 1460 ) && 1017 - test_create_commit "$subtree_test_count" folder_subtree/d && 1461 + test_create_commit "$test_count" folder_subtree/d && 1018 1462 ( 1019 - cd "$subtree_test_count" && 1463 + cd "$test_count" && 1020 1464 git checkout no_subtree_work_branch 1021 1465 ) && 1022 - test_create_commit "$subtree_test_count" not_a_subtree_change && 1466 + test_create_commit "$test_count" not_a_subtree_change && 1023 1467 ( 1024 - cd "$subtree_test_count" && 1025 - git checkout master && 1468 + cd "$test_count" && 1469 + git checkout $defaultBranch && 1026 1470 git merge -m "merge should be skipped on subtree" no_subtree_work_branch && 1027 1471 1028 - git subtree split --prefix folder_subtree/ --branch subtree_tip master && 1472 + git subtree split --prefix folder_subtree/ --branch subtree_tip $defaultBranch && 1029 1473 git subtree split --prefix folder_subtree/ --branch subtree_branch branch && 1030 - check_equal $(git rev-list --count subtree_tip..subtree_branch) 0 1474 + test $(git rev-list --count subtree_tip..subtree_branch) = 0 1031 1475 ) 1032 1476 ' 1033 1477
+3 -3
contrib/subtree/todo
··· 23 23 24 24 "pull" and "merge" commands should fail if you've never merged 25 25 that --prefix before 26 - 26 + 27 27 docs should provide an example of "add" 28 - 28 + 29 29 note that the initial split doesn't *have* to have a commitid 30 30 specified... that's just an optimization 31 31 ··· 33 33 get a misleading "prefix must end with /" message from 34 34 one of the other git tools that git-subtree calls. Should 35 35 detect this situation and print the *real* problem. 36 - 36 + 37 37 "pull --squash" should do fetch-synthesize-merge, but instead just 38 38 does "pull" directly, which doesn't work at all. 39 39