git-subtree.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. #!/bin/sh
  2. #
  3. # git-subtree.sh: split/join git repositories in subdirectories of this one
  4. #
  5. # Copyright (C) 2009 Avery Pennarun <apenwarr@gmail.com>
  6. #
  7. if test $# -eq 0
  8. then
  9. set -- -h
  10. fi
  11. OPTS_SPEC="\
  12. git subtree add --prefix=<prefix> <commit>
  13. git subtree add --prefix=<prefix> <repository> <ref>
  14. git subtree merge --prefix=<prefix> <commit>
  15. git subtree pull --prefix=<prefix> <repository> <ref>
  16. git subtree push --prefix=<prefix> <repository> <ref>
  17. git subtree split --prefix=<prefix> <commit>
  18. --
  19. h,help show the help
  20. q quiet
  21. d show debug messages
  22. 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'
  25. annotate= add a prefix to commit message of new commits
  26. b,branch= create a new branch from the split subtree
  27. ignore-joins ignore prior --rejoin commits
  28. onto= try connecting new tree to an existing one
  29. rejoin merge the new branch back into HEAD
  30. options for 'add', 'merge', and 'pull'
  31. squash merge subtree changes as a single commit
  32. "
  33. eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
  34. PATH=$PATH:$(git --exec-path)
  35. . git-sh-setup
  36. require_work_tree
  37. quiet=
  38. branch=
  39. debug=
  40. command=
  41. onto=
  42. rejoin=
  43. ignore_joins=
  44. annotate=
  45. squash=
  46. message=
  47. prefix=
  48. debug () {
  49. if test -n "$debug"
  50. then
  51. printf "%s\n" "$*" >&2
  52. fi
  53. }
  54. say () {
  55. if test -z "$quiet"
  56. then
  57. printf "%s\n" "$*" >&2
  58. fi
  59. }
  60. progress () {
  61. if test -z "$quiet"
  62. then
  63. printf "%s\r" "$*" >&2
  64. fi
  65. }
  66. assert () {
  67. if ! "$@"
  68. then
  69. die "assertion failed: " "$@"
  70. fi
  71. }
  72. ensure_single_rev () {
  73. if test $# -ne 1
  74. then
  75. die "You must provide exactly one revision. Got: '$@'"
  76. fi
  77. }
  78. while test $# -gt 0
  79. do
  80. opt="$1"
  81. shift
  82. case "$opt" in
  83. -q)
  84. quiet=1
  85. ;;
  86. -d)
  87. debug=1
  88. ;;
  89. --annotate)
  90. annotate="$1"
  91. shift
  92. ;;
  93. --no-annotate)
  94. annotate=
  95. ;;
  96. -b)
  97. branch="$1"
  98. shift
  99. ;;
  100. -P)
  101. prefix="${1%/}"
  102. shift
  103. ;;
  104. -m)
  105. message="$1"
  106. shift
  107. ;;
  108. --no-prefix)
  109. prefix=
  110. ;;
  111. --onto)
  112. onto="$1"
  113. shift
  114. ;;
  115. --no-onto)
  116. onto=
  117. ;;
  118. --rejoin)
  119. rejoin=1
  120. ;;
  121. --no-rejoin)
  122. rejoin=
  123. ;;
  124. --ignore-joins)
  125. ignore_joins=1
  126. ;;
  127. --no-ignore-joins)
  128. ignore_joins=
  129. ;;
  130. --squash)
  131. squash=1
  132. ;;
  133. --no-squash)
  134. squash=
  135. ;;
  136. --)
  137. break
  138. ;;
  139. *)
  140. die "Unexpected option: $opt"
  141. ;;
  142. esac
  143. done
  144. command="$1"
  145. shift
  146. case "$command" in
  147. add|merge|pull)
  148. default=
  149. ;;
  150. split|push)
  151. default="--default HEAD"
  152. ;;
  153. *)
  154. die "Unknown command '$command'"
  155. ;;
  156. esac
  157. if test -z "$prefix"
  158. then
  159. die "You must provide the --prefix option."
  160. fi
  161. case "$command" in
  162. add)
  163. test -e "$prefix" &&
  164. die "prefix '$prefix' already exists."
  165. ;;
  166. *)
  167. test -e "$prefix" ||
  168. die "'$prefix' does not exist; use 'git subtree add'"
  169. ;;
  170. esac
  171. dir="$(dirname "$prefix/.")"
  172. if test "$command" != "pull" &&
  173. test "$command" != "add" &&
  174. test "$command" != "push"
  175. then
  176. revs=$(git rev-parse $default --revs-only "$@") || exit $?
  177. dirs=$(git rev-parse --no-revs --no-flags "$@") || exit $?
  178. ensure_single_rev $revs
  179. if test -n "$dirs"
  180. then
  181. die "Error: Use --prefix instead of bare filenames."
  182. fi
  183. fi
  184. debug "command: {$command}"
  185. debug "quiet: {$quiet}"
  186. debug "revs: {$revs}"
  187. debug "dir: {$dir}"
  188. debug "opts: {$*}"
  189. debug
  190. cache_setup () {
  191. cachedir="$GIT_DIR/subtree-cache/$$"
  192. rm -rf "$cachedir" ||
  193. die "Can't delete old cachedir: $cachedir"
  194. mkdir -p "$cachedir" ||
  195. die "Can't create new cachedir: $cachedir"
  196. mkdir -p "$cachedir/notree" ||
  197. die "Can't create new cachedir: $cachedir/notree"
  198. debug "Using cachedir: $cachedir" >&2
  199. }
  200. cache_get () {
  201. for oldrev in "$@"
  202. do
  203. if test -r "$cachedir/$oldrev"
  204. then
  205. read newrev <"$cachedir/$oldrev"
  206. echo $newrev
  207. fi
  208. done
  209. }
  210. cache_miss () {
  211. for oldrev in "$@"
  212. do
  213. if ! test -r "$cachedir/$oldrev"
  214. then
  215. echo $oldrev
  216. fi
  217. done
  218. }
  219. check_parents () {
  220. missed=$(cache_miss "$1")
  221. local indent=$(($2 + 1))
  222. for miss in $missed
  223. do
  224. if ! test -r "$cachedir/notree/$miss"
  225. then
  226. debug " incorrect order: $miss"
  227. process_split_commit "$miss" "" "$indent"
  228. fi
  229. done
  230. }
  231. set_notree () {
  232. echo "1" > "$cachedir/notree/$1"
  233. }
  234. cache_set () {
  235. oldrev="$1"
  236. newrev="$2"
  237. if test "$oldrev" != "latest_old" &&
  238. test "$oldrev" != "latest_new" &&
  239. test -e "$cachedir/$oldrev"
  240. then
  241. die "cache for $oldrev already exists!"
  242. fi
  243. echo "$newrev" >"$cachedir/$oldrev"
  244. }
  245. rev_exists () {
  246. if git rev-parse "$1" >/dev/null 2>&1
  247. then
  248. return 0
  249. else
  250. return 1
  251. fi
  252. }
  253. rev_is_descendant_of_branch () {
  254. newrev="$1"
  255. branch="$2"
  256. branch_hash=$(git rev-parse "$branch")
  257. match=$(git rev-list -1 "$branch_hash" "^$newrev")
  258. if test -z "$match"
  259. then
  260. return 0
  261. else
  262. return 1
  263. fi
  264. }
  265. # if a commit doesn't have a parent, this might not work. But we only want
  266. # to remove the parent from the rev-list, and since it doesn't exist, it won't
  267. # be there anyway, so do nothing in that case.
  268. try_remove_previous () {
  269. if rev_exists "$1^"
  270. then
  271. echo "^$1^"
  272. fi
  273. }
  274. find_latest_squash () {
  275. debug "Looking for latest squash ($dir)..."
  276. dir="$1"
  277. sq=
  278. main=
  279. sub=
  280. git log --grep="^git-subtree-dir: $dir/*\$" \
  281. --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
  282. while read a b junk
  283. do
  284. debug "$a $b $junk"
  285. debug "{{$sq/$main/$sub}}"
  286. case "$a" in
  287. START)
  288. sq="$b"
  289. ;;
  290. git-subtree-mainline:)
  291. main="$b"
  292. ;;
  293. git-subtree-split:)
  294. sub="$(git rev-parse "$b^0")" ||
  295. die "could not rev-parse split hash $b from commit $sq"
  296. ;;
  297. END)
  298. if test -n "$sub"
  299. then
  300. if test -n "$main"
  301. then
  302. # a rejoin commit?
  303. # Pretend its sub was a squash.
  304. sq="$sub"
  305. fi
  306. debug "Squash found: $sq $sub"
  307. echo "$sq" "$sub"
  308. break
  309. fi
  310. sq=
  311. main=
  312. sub=
  313. ;;
  314. esac
  315. done
  316. }
  317. find_existing_splits () {
  318. debug "Looking for prior splits..."
  319. dir="$1"
  320. revs="$2"
  321. main=
  322. sub=
  323. local grep_format="^git-subtree-dir: $dir/*\$"
  324. if test -n "$ignore_joins"
  325. then
  326. grep_format="^Add '$dir/' from commit '"
  327. fi
  328. git log --grep="$grep_format" \
  329. --no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs |
  330. while read a b junk
  331. do
  332. case "$a" in
  333. START)
  334. sq="$b"
  335. ;;
  336. git-subtree-mainline:)
  337. main="$b"
  338. ;;
  339. git-subtree-split:)
  340. sub="$(git rev-parse "$b^0")" ||
  341. die "could not rev-parse split hash $b from commit $sq"
  342. ;;
  343. END)
  344. debug " Main is: '$main'"
  345. if test -z "$main" -a -n "$sub"
  346. then
  347. # squash commits refer to a subtree
  348. debug " Squash: $sq from $sub"
  349. cache_set "$sq" "$sub"
  350. fi
  351. if test -n "$main" -a -n "$sub"
  352. then
  353. debug " Prior: $main -> $sub"
  354. cache_set $main $sub
  355. cache_set $sub $sub
  356. try_remove_previous "$main"
  357. try_remove_previous "$sub"
  358. fi
  359. main=
  360. sub=
  361. ;;
  362. esac
  363. done
  364. }
  365. copy_commit () {
  366. # We're going to set some environment vars here, so
  367. # do it in a subshell to get rid of them safely later
  368. debug copy_commit "{$1}" "{$2}" "{$3}"
  369. git log -1 --no-show-signature --pretty=format:'%an%n%ae%n%aD%n%cn%n%ce%n%cD%n%B' "$1" |
  370. (
  371. read GIT_AUTHOR_NAME
  372. read GIT_AUTHOR_EMAIL
  373. read GIT_AUTHOR_DATE
  374. read GIT_COMMITTER_NAME
  375. read GIT_COMMITTER_EMAIL
  376. read GIT_COMMITTER_DATE
  377. export GIT_AUTHOR_NAME \
  378. GIT_AUTHOR_EMAIL \
  379. GIT_AUTHOR_DATE \
  380. GIT_COMMITTER_NAME \
  381. GIT_COMMITTER_EMAIL \
  382. GIT_COMMITTER_DATE
  383. (
  384. printf "%s" "$annotate"
  385. cat
  386. ) |
  387. git commit-tree "$2" $3 # reads the rest of stdin
  388. ) || die "Can't copy commit $1"
  389. }
  390. add_msg () {
  391. dir="$1"
  392. latest_old="$2"
  393. latest_new="$3"
  394. if test -n "$message"
  395. then
  396. commit_message="$message"
  397. else
  398. commit_message="Add '$dir/' from commit '$latest_new'"
  399. fi
  400. cat <<-EOF
  401. $commit_message
  402. git-subtree-dir: $dir
  403. git-subtree-mainline: $latest_old
  404. git-subtree-split: $latest_new
  405. EOF
  406. }
  407. add_squashed_msg () {
  408. if test -n "$message"
  409. then
  410. echo "$message"
  411. else
  412. echo "Merge commit '$1' as '$2'"
  413. fi
  414. }
  415. rejoin_msg () {
  416. dir="$1"
  417. latest_old="$2"
  418. latest_new="$3"
  419. if test -n "$message"
  420. then
  421. commit_message="$message"
  422. else
  423. commit_message="Split '$dir/' into commit '$latest_new'"
  424. fi
  425. cat <<-EOF
  426. $commit_message
  427. git-subtree-dir: $dir
  428. git-subtree-mainline: $latest_old
  429. git-subtree-split: $latest_new
  430. EOF
  431. }
  432. squash_msg () {
  433. dir="$1"
  434. oldsub="$2"
  435. newsub="$3"
  436. newsub_short=$(git rev-parse --short "$newsub")
  437. if test -n "$oldsub"
  438. then
  439. oldsub_short=$(git rev-parse --short "$oldsub")
  440. echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
  441. echo
  442. git log --no-show-signature --pretty=tformat:'%h %s' "$oldsub..$newsub"
  443. git log --no-show-signature --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
  444. else
  445. echo "Squashed '$dir/' content from commit $newsub_short"
  446. fi
  447. echo
  448. echo "git-subtree-dir: $dir"
  449. echo "git-subtree-split: $newsub"
  450. }
  451. toptree_for_commit () {
  452. commit="$1"
  453. git rev-parse --verify "$commit^{tree}" || exit $?
  454. }
  455. subtree_for_commit () {
  456. commit="$1"
  457. dir="$2"
  458. git ls-tree "$commit" -- "$dir" |
  459. while read mode type tree name
  460. do
  461. assert test "$name" = "$dir"
  462. assert test "$type" = "tree" -o "$type" = "commit"
  463. test "$type" = "commit" && continue # ignore submodules
  464. echo $tree
  465. break
  466. done
  467. }
  468. tree_changed () {
  469. tree=$1
  470. shift
  471. if test $# -ne 1
  472. then
  473. return 0 # weird parents, consider it changed
  474. else
  475. ptree=$(toptree_for_commit $1)
  476. if test "$ptree" != "$tree"
  477. then
  478. return 0 # changed
  479. else
  480. return 1 # not changed
  481. fi
  482. fi
  483. }
  484. new_squash_commit () {
  485. old="$1"
  486. oldsub="$2"
  487. newsub="$3"
  488. tree=$(toptree_for_commit $newsub) || exit $?
  489. if test -n "$old"
  490. then
  491. squash_msg "$dir" "$oldsub" "$newsub" |
  492. git commit-tree "$tree" -p "$old" || exit $?
  493. else
  494. squash_msg "$dir" "" "$newsub" |
  495. git commit-tree "$tree" || exit $?
  496. fi
  497. }
  498. copy_or_skip () {
  499. rev="$1"
  500. tree="$2"
  501. newparents="$3"
  502. assert test -n "$tree"
  503. identical=
  504. nonidentical=
  505. p=
  506. gotparents=
  507. copycommit=
  508. for parent in $newparents
  509. do
  510. ptree=$(toptree_for_commit $parent) || exit $?
  511. test -z "$ptree" && continue
  512. if test "$ptree" = "$tree"
  513. then
  514. # an identical parent could be used in place of this rev.
  515. if test -n "$identical"
  516. then
  517. # if a previous identical parent was found, check whether
  518. # one is already an ancestor of the other
  519. mergebase=$(git merge-base $identical $parent)
  520. if test "$identical" = "$mergebase"
  521. then
  522. # current identical commit is an ancestor of parent
  523. identical="$parent"
  524. elif test "$parent" != "$mergebase"
  525. then
  526. # no common history; commit must be copied
  527. copycommit=1
  528. fi
  529. else
  530. # first identical parent detected
  531. identical="$parent"
  532. fi
  533. else
  534. nonidentical="$parent"
  535. fi
  536. # sometimes both old parents map to the same newparent;
  537. # eliminate duplicates
  538. is_new=1
  539. for gp in $gotparents
  540. do
  541. if test "$gp" = "$parent"
  542. then
  543. is_new=
  544. break
  545. fi
  546. done
  547. if test -n "$is_new"
  548. then
  549. gotparents="$gotparents $parent"
  550. p="$p -p $parent"
  551. fi
  552. done
  553. if test -n "$identical" && test -n "$nonidentical"
  554. then
  555. extras=$(git rev-list --count $identical..$nonidentical)
  556. if test "$extras" -ne 0
  557. then
  558. # we need to preserve history along the other branch
  559. copycommit=1
  560. fi
  561. fi
  562. if test -n "$identical" && test -z "$copycommit"
  563. then
  564. echo $identical
  565. else
  566. copy_commit "$rev" "$tree" "$p" || exit $?
  567. fi
  568. }
  569. ensure_clean () {
  570. if ! git diff-index HEAD --exit-code --quiet 2>&1
  571. then
  572. die "Working tree has modifications. Cannot add."
  573. fi
  574. if ! git diff-index --cached HEAD --exit-code --quiet 2>&1
  575. then
  576. die "Index has modifications. Cannot add."
  577. fi
  578. }
  579. ensure_valid_ref_format () {
  580. git check-ref-format "refs/heads/$1" ||
  581. die "'$1' does not look like a ref"
  582. }
  583. process_split_commit () {
  584. local rev="$1"
  585. local parents="$2"
  586. local indent=$3
  587. if test $indent -eq 0
  588. then
  589. revcount=$(($revcount + 1))
  590. else
  591. # processing commit without normal parent information;
  592. # fetch from repo
  593. parents=$(git rev-parse "$rev^@")
  594. extracount=$(($extracount + 1))
  595. fi
  596. progress "$revcount/$revmax ($createcount) [$extracount]"
  597. debug "Processing commit: $rev"
  598. exists=$(cache_get "$rev")
  599. if test -n "$exists"
  600. then
  601. debug " prior: $exists"
  602. return
  603. fi
  604. createcount=$(($createcount + 1))
  605. debug " parents: $parents"
  606. check_parents "$parents" "$indent"
  607. newparents=$(cache_get $parents)
  608. debug " newparents: $newparents"
  609. tree=$(subtree_for_commit "$rev" "$dir")
  610. debug " tree is: $tree"
  611. # ugly. is there no better way to tell if this is a subtree
  612. # vs. a mainline commit? Does it matter?
  613. if test -z "$tree"
  614. then
  615. set_notree "$rev"
  616. if test -n "$newparents"
  617. then
  618. cache_set "$rev" "$rev"
  619. fi
  620. return
  621. fi
  622. newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
  623. debug " newrev is: $newrev"
  624. cache_set "$rev" "$newrev"
  625. cache_set latest_new "$newrev"
  626. cache_set latest_old "$rev"
  627. }
  628. cmd_add () {
  629. if test -e "$dir"
  630. then
  631. die "'$dir' already exists. Cannot add."
  632. fi
  633. ensure_clean
  634. if test $# -eq 1
  635. then
  636. git rev-parse -q --verify "$1^{commit}" >/dev/null ||
  637. die "'$1' does not refer to a commit"
  638. cmd_add_commit "$@"
  639. elif test $# -eq 2
  640. then
  641. # Technically we could accept a refspec here but we're
  642. # just going to turn around and add FETCH_HEAD under the
  643. # specified directory. Allowing a refspec might be
  644. # misleading because we won't do anything with any other
  645. # branches fetched via the refspec.
  646. ensure_valid_ref_format "$2"
  647. cmd_add_repository "$@"
  648. else
  649. say "error: parameters were '$@'"
  650. die "Provide either a commit or a repository and commit."
  651. fi
  652. }
  653. cmd_add_repository () {
  654. echo "git fetch" "$@"
  655. repository=$1
  656. refspec=$2
  657. git fetch "$@" || exit $?
  658. revs=FETCH_HEAD
  659. set -- $revs
  660. cmd_add_commit "$@"
  661. }
  662. cmd_add_commit () {
  663. rev=$(git rev-parse $default --revs-only "$@") || exit $?
  664. ensure_single_rev $rev
  665. debug "Adding $dir as '$rev'..."
  666. git read-tree --prefix="$dir" $rev || exit $?
  667. git checkout -- "$dir" || exit $?
  668. tree=$(git write-tree) || exit $?
  669. headrev=$(git rev-parse HEAD) || exit $?
  670. if test -n "$headrev" && test "$headrev" != "$rev"
  671. then
  672. headp="-p $headrev"
  673. else
  674. headp=
  675. fi
  676. if test -n "$squash"
  677. then
  678. rev=$(new_squash_commit "" "" "$rev") || exit $?
  679. commit=$(add_squashed_msg "$rev" "$dir" |
  680. git commit-tree "$tree" $headp -p "$rev") || exit $?
  681. else
  682. revp=$(peel_committish "$rev") &&
  683. commit=$(add_msg "$dir" $headrev "$rev" |
  684. git commit-tree "$tree" $headp -p "$revp") || exit $?
  685. fi
  686. git reset "$commit" || exit $?
  687. say "Added dir '$dir'"
  688. }
  689. cmd_split () {
  690. debug "Splitting $dir..."
  691. cache_setup || exit $?
  692. if test -n "$onto"
  693. then
  694. debug "Reading history for --onto=$onto..."
  695. git rev-list $onto |
  696. while read rev
  697. do
  698. # the 'onto' history is already just the subdir, so
  699. # any parent we find there can be used verbatim
  700. debug " cache: $rev"
  701. cache_set "$rev" "$rev"
  702. done
  703. fi
  704. unrevs="$(find_existing_splits "$dir" "$revs")"
  705. # We can't restrict rev-list to only $dir here, because some of our
  706. # parents have the $dir contents the root, and those won't match.
  707. # (and rev-list --follow doesn't seem to solve this)
  708. grl='git rev-list --topo-order --reverse --parents $revs $unrevs'
  709. revmax=$(eval "$grl" | wc -l)
  710. revcount=0
  711. createcount=0
  712. extracount=0
  713. eval "$grl" |
  714. while read rev parents
  715. do
  716. process_split_commit "$rev" "$parents" 0
  717. done || exit $?
  718. latest_new=$(cache_get latest_new)
  719. if test -z "$latest_new"
  720. then
  721. die "No new revisions were found"
  722. fi
  723. if test -n "$rejoin"
  724. then
  725. debug "Merging split branch into HEAD..."
  726. latest_old=$(cache_get latest_old)
  727. git merge -s ours \
  728. --allow-unrelated-histories \
  729. -m "$(rejoin_msg "$dir" "$latest_old" "$latest_new")" \
  730. "$latest_new" >&2 || exit $?
  731. fi
  732. if test -n "$branch"
  733. then
  734. if rev_exists "refs/heads/$branch"
  735. then
  736. if ! rev_is_descendant_of_branch "$latest_new" "$branch"
  737. then
  738. die "Branch '$branch' is not an ancestor of commit '$latest_new'."
  739. fi
  740. action='Updated'
  741. else
  742. action='Created'
  743. fi
  744. git update-ref -m 'subtree split' \
  745. "refs/heads/$branch" "$latest_new" || exit $?
  746. say "$action branch '$branch'"
  747. fi
  748. echo "$latest_new"
  749. exit 0
  750. }
  751. cmd_merge () {
  752. rev=$(git rev-parse $default --revs-only "$@") || exit $?
  753. ensure_single_rev $rev
  754. ensure_clean
  755. if test -n "$squash"
  756. then
  757. first_split="$(find_latest_squash "$dir")"
  758. if test -z "$first_split"
  759. then
  760. die "Can't squash-merge: '$dir' was never added."
  761. fi
  762. set $first_split
  763. old=$1
  764. sub=$2
  765. if test "$sub" = "$rev"
  766. then
  767. say "Subtree is already at commit $rev."
  768. exit 0
  769. fi
  770. new=$(new_squash_commit "$old" "$sub" "$rev") || exit $?
  771. debug "New squash commit: $new"
  772. rev="$new"
  773. fi
  774. version=$(git version)
  775. if test "$version" \< "git version 1.7"
  776. then
  777. if test -n "$message"
  778. then
  779. git merge -s subtree --message="$message" "$rev"
  780. else
  781. git merge -s subtree "$rev"
  782. fi
  783. else
  784. if test -n "$message"
  785. then
  786. git merge -Xsubtree="$prefix" \
  787. --message="$message" "$rev"
  788. else
  789. git merge -Xsubtree="$prefix" $rev
  790. fi
  791. fi
  792. }
  793. cmd_pull () {
  794. if test $# -ne 2
  795. then
  796. die "You must provide <repository> <ref>"
  797. fi
  798. ensure_clean
  799. ensure_valid_ref_format "$2"
  800. git fetch "$@" || exit $?
  801. revs=FETCH_HEAD
  802. set -- $revs
  803. cmd_merge "$@"
  804. }
  805. cmd_push () {
  806. if test $# -ne 2
  807. then
  808. die "You must provide <repository> <ref>"
  809. fi
  810. ensure_valid_ref_format "$2"
  811. if test -e "$dir"
  812. then
  813. repository=$1
  814. refspec=$2
  815. echo "git push using: " "$repository" "$refspec"
  816. localrev=$(git subtree split --prefix="$prefix") || die
  817. git push "$repository" "$localrev":"refs/heads/$refspec"
  818. else
  819. die "'$dir' must already exist. Try 'git subtree add'."
  820. fi
  821. }
  822. "cmd_$command" "$@"