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