Merge branch 'da/subtree-modernize'

Style fixes for "git subtree" (in contrib/).

* da/subtree-modernize:
  subtree: adjust function definitions to match CodingGuidelines
  subtree: adjust style to match CodingGuidelines
This commit is contained in:
Junio C Hamano
2016-08-08 14:48:35 -07:00

View File

@ -4,7 +4,8 @@
# #
# Copyright (C) 2009 Avery Pennarun <apenwarr@gmail.com> # Copyright (C) 2009 Avery Pennarun <apenwarr@gmail.com>
# #
if [ $# -eq 0 ]; then if test $# -eq 0
then
set -- -h set -- -h
fi fi
OPTS_SPEC="\ OPTS_SPEC="\
@ -48,89 +49,144 @@ squash=
message= message=
prefix= prefix=
debug() debug () {
{ if test -n "$debug"
if [ -n "$debug" ]; then then
printf "%s\n" "$*" >&2 printf "%s\n" "$*" >&2
fi fi
} }
say() say () {
{ if test -z "$quiet"
if [ -z "$quiet" ]; then then
printf "%s\n" "$*" >&2 printf "%s\n" "$*" >&2
fi fi
} }
progress() progress () {
{ if test -z "$quiet"
if [ -z "$quiet" ]; then then
printf "%s\r" "$*" >&2 printf "%s\r" "$*" >&2
fi fi
} }
assert() assert () {
{ if ! "$@"
if "$@"; then then
:
else
die "assertion failed: " "$@" die "assertion failed: " "$@"
fi fi
} }
#echo "Options: $*" while test $# -gt 0
do
while [ $# -gt 0 ]; do
opt="$1" opt="$1"
shift shift
case "$opt" in case "$opt" in
-q) quiet=1 ;; -q)
-d) debug=1 ;; quiet=1
--annotate) annotate="$1"; shift ;; ;;
--no-annotate) annotate= ;; -d)
-b) branch="$1"; shift ;; debug=1
-P) prefix="${1%/}"; shift ;; ;;
-m) message="$1"; shift ;; --annotate)
--no-prefix) prefix= ;; annotate="$1"
--onto) onto="$1"; shift ;; shift
--no-onto) onto= ;; ;;
--rejoin) rejoin=1 ;; --no-annotate)
--no-rejoin) rejoin= ;; annotate=
--ignore-joins) ignore_joins=1 ;; ;;
--no-ignore-joins) ignore_joins= ;; -b)
--squash) squash=1 ;; branch="$1"
--no-squash) squash= ;; shift
--) break ;; ;;
*) die "Unexpected option: $opt" ;; -P)
prefix="${1%/}"
shift
;;
-m)
message="$1"
shift
;;
--no-prefix)
prefix=
;;
--onto)
onto="$1"
shift
;;
--no-onto)
onto=
;;
--rejoin)
rejoin=1
;;
--no-rejoin)
rejoin=
;;
--ignore-joins)
ignore_joins=1
;;
--no-ignore-joins)
ignore_joins=
;;
--squash)
squash=1
;;
--no-squash)
squash=
;;
--)
break
;;
*)
die "Unexpected option: $opt"
;;
esac esac
done done
command="$1" command="$1"
shift shift
case "$command" in case "$command" in
add|merge|pull) default= ;; add|merge|pull)
split|push) default="--default HEAD" ;; default=
*) die "Unknown command '$command'" ;; ;;
split|push)
default="--default HEAD"
;;
*)
die "Unknown command '$command'"
;;
esac esac
if [ -z "$prefix" ]; then if test -z "$prefix"
then
die "You must provide the --prefix option." die "You must provide the --prefix option."
fi fi
case "$command" in case "$command" in
add) [ -e "$prefix" ] && add)
die "prefix '$prefix' already exists." ;; test -e "$prefix" &&
*) [ -e "$prefix" ] || die "prefix '$prefix' already exists."
die "'$prefix' does not exist; use 'git subtree add'" ;; ;;
*)
test -e "$prefix" ||
die "'$prefix' does not exist; use 'git subtree add'"
;;
esac esac
dir="$(dirname "$prefix/.")" dir="$(dirname "$prefix/.")"
if [ "$command" != "pull" -a "$command" != "add" -a "$command" != "push" ]; then if test "$command" != "pull" &&
test "$command" != "add" &&
test "$command" != "push"
then
revs=$(git rev-parse $default --revs-only "$@") || exit $? revs=$(git rev-parse $default --revs-only "$@") || exit $?
dirs="$(git rev-parse --no-revs --no-flags "$@")" || exit $? dirs=$(git rev-parse --no-revs --no-flags "$@") || exit $?
if [ -n "$dirs" ]; then if test -n "$dirs"
then
die "Error: Use --prefix instead of bare filenames." die "Error: Use --prefix instead of bare filenames."
fi fi
fi fi
@ -142,78 +198,82 @@ debug "dir: {$dir}"
debug "opts: {$*}" debug "opts: {$*}"
debug debug
cache_setup() cache_setup () {
{
cachedir="$GIT_DIR/subtree-cache/$$" cachedir="$GIT_DIR/subtree-cache/$$"
rm -rf "$cachedir" || die "Can't delete old cachedir: $cachedir" rm -rf "$cachedir" ||
mkdir -p "$cachedir" || die "Can't create new cachedir: $cachedir" die "Can't delete old cachedir: $cachedir"
mkdir -p "$cachedir/notree" || die "Can't create new cachedir: $cachedir/notree" mkdir -p "$cachedir" ||
die "Can't create new cachedir: $cachedir"
mkdir -p "$cachedir/notree" ||
die "Can't create new cachedir: $cachedir/notree"
debug "Using cachedir: $cachedir" >&2 debug "Using cachedir: $cachedir" >&2
} }
cache_get() cache_get () {
{ for oldrev in "$@"
for oldrev in $*; do do
if [ -r "$cachedir/$oldrev" ]; then if test -r "$cachedir/$oldrev"
then
read newrev <"$cachedir/$oldrev" read newrev <"$cachedir/$oldrev"
echo $newrev echo $newrev
fi fi
done done
} }
cache_miss() cache_miss () {
{ for oldrev in "$@"
for oldrev in $*; do do
if [ ! -r "$cachedir/$oldrev" ]; then if ! test -r "$cachedir/$oldrev"
then
echo $oldrev echo $oldrev
fi fi
done done
} }
check_parents() check_parents () {
{ missed=$(cache_miss "$@")
missed=$(cache_miss $*) for miss in $missed
for miss in $missed; do do
if [ ! -r "$cachedir/notree/$miss" ]; then if ! test -r "$cachedir/notree/$miss"
then
debug " incorrect order: $miss" debug " incorrect order: $miss"
fi fi
done done
} }
set_notree() set_notree () {
{
echo "1" > "$cachedir/notree/$1" echo "1" > "$cachedir/notree/$1"
} }
cache_set() cache_set () {
{
oldrev="$1" oldrev="$1"
newrev="$2" newrev="$2"
if [ "$oldrev" != "latest_old" \ if test "$oldrev" != "latest_old" &&
-a "$oldrev" != "latest_new" \ test "$oldrev" != "latest_new" &&
-a -e "$cachedir/$oldrev" ]; then test -e "$cachedir/$oldrev"
then
die "cache for $oldrev already exists!" die "cache for $oldrev already exists!"
fi fi
echo "$newrev" >"$cachedir/$oldrev" echo "$newrev" >"$cachedir/$oldrev"
} }
rev_exists() rev_exists () {
{ if git rev-parse "$1" >/dev/null 2>&1
if git rev-parse "$1" >/dev/null 2>&1; then then
return 0 return 0
else else
return 1 return 1
fi fi
} }
rev_is_descendant_of_branch() rev_is_descendant_of_branch () {
{
newrev="$1" newrev="$1"
branch="$2" branch="$2"
branch_hash=$(git rev-parse $branch) branch_hash=$(git rev-parse "$branch")
match=$(git rev-list -1 $branch_hash ^$newrev) match=$(git rev-list -1 "$branch_hash" "^$newrev")
if [ -z "$match" ]; then if test -z "$match"
then
return 0 return 0
else else
return 1 return 1
@ -223,15 +283,14 @@ rev_is_descendant_of_branch()
# if a commit doesn't have a parent, this might not work. But we only want # if a commit doesn't have a parent, this might not work. But we only want
# to remove the parent from the rev-list, and since it doesn't exist, it won't # to remove the parent from the rev-list, and since it doesn't exist, it won't
# be there anyway, so do nothing in that case. # be there anyway, so do nothing in that case.
try_remove_previous() try_remove_previous () {
{ if rev_exists "$1^"
if rev_exists "$1^"; then then
echo "^$1^" echo "^$1^"
fi fi
} }
find_latest_squash() find_latest_squash () {
{
debug "Looking for latest squash ($dir)..." debug "Looking for latest squash ($dir)..."
dir="$1" dir="$1"
sq= sq=
@ -239,19 +298,26 @@ find_latest_squash()
sub= sub=
git log --grep="^git-subtree-dir: $dir/*\$" \ git log --grep="^git-subtree-dir: $dir/*\$" \
--pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD | --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
while read a b junk; do while read a b junk
do
debug "$a $b $junk" debug "$a $b $junk"
debug "{{$sq/$main/$sub}}" debug "{{$sq/$main/$sub}}"
case "$a" in case "$a" in
START) sq="$b" ;; START)
git-subtree-mainline:) main="$b" ;; sq="$b"
;;
git-subtree-mainline:)
main="$b"
;;
git-subtree-split:) git-subtree-split:)
sub="$(git rev-parse "$b^0")" || sub="$(git rev-parse "$b^0")" ||
die "could not rev-parse split hash $b from commit $sq" die "could not rev-parse split hash $b from commit $sq"
;; ;;
END) END)
if [ -n "$sub" ]; then if test -n "$sub"
if [ -n "$main" ]; then then
if test -n "$main"
then
# a rejoin commit? # a rejoin commit?
# Pretend its sub was a squash. # Pretend its sub was a squash.
sq="$sub" sq="$sub"
@ -268,8 +334,7 @@ find_latest_squash()
done done
} }
find_existing_splits() find_existing_splits () {
{
debug "Looking for prior splits..." debug "Looking for prior splits..."
dir="$1" dir="$1"
revs="$2" revs="$2"
@ -277,22 +342,29 @@ find_existing_splits()
sub= sub=
git log --grep="^git-subtree-dir: $dir/*\$" \ git log --grep="^git-subtree-dir: $dir/*\$" \
--pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs | --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs |
while read a b junk; do while read a b junk
do
case "$a" in case "$a" in
START) sq="$b" ;; START)
git-subtree-mainline:) main="$b" ;; sq="$b"
;;
git-subtree-mainline:)
main="$b"
;;
git-subtree-split:) git-subtree-split:)
sub="$(git rev-parse "$b^0")" || sub="$(git rev-parse "$b^0")" ||
die "could not rev-parse split hash $b from commit $sq" die "could not rev-parse split hash $b from commit $sq"
;; ;;
END) END)
debug " Main is: '$main'" debug " Main is: '$main'"
if [ -z "$main" -a -n "$sub" ]; then if test -z "$main" -a -n "$sub"
then
# squash commits refer to a subtree # squash commits refer to a subtree
debug " Squash: $sq from $sub" debug " Squash: $sq from $sub"
cache_set "$sq" "$sub" cache_set "$sq" "$sub"
fi fi
if [ -n "$main" -a -n "$sub" ]; then if test -n "$main" -a -n "$sub"
then
debug " Prior: $main -> $sub" debug " Prior: $main -> $sub"
cache_set $main $sub cache_set $main $sub
cache_set $sub $sub cache_set $sub $sub
@ -306,8 +378,7 @@ find_existing_splits()
done done
} }
copy_commit() copy_commit () {
{
# We're going to set some environment vars here, so # We're going to set some environment vars here, so
# do it in a subshell to get rid of them safely later # do it in a subshell to get rid of them safely later
debug copy_commit "{$1}" "{$2}" "{$3}" debug copy_commit "{$1}" "{$2}" "{$3}"
@ -325,17 +396,20 @@ copy_commit()
GIT_COMMITTER_NAME \ GIT_COMMITTER_NAME \
GIT_COMMITTER_EMAIL \ GIT_COMMITTER_EMAIL \
GIT_COMMITTER_DATE GIT_COMMITTER_DATE
(printf "%s" "$annotate"; cat ) | (
printf "%s" "$annotate"
cat
) |
git commit-tree "$2" $3 # reads the rest of stdin git commit-tree "$2" $3 # reads the rest of stdin
) || die "Can't copy commit $1" ) || die "Can't copy commit $1"
} }
add_msg() add_msg () {
{
dir="$1" dir="$1"
latest_old="$2" latest_old="$2"
latest_new="$3" latest_new="$3"
if [ -n "$message" ]; then if test -n "$message"
then
commit_message="$message" commit_message="$message"
else else
commit_message="Add '$dir/' from commit '$latest_new'" commit_message="Add '$dir/' from commit '$latest_new'"
@ -349,21 +423,21 @@ add_msg()
EOF EOF
} }
add_squashed_msg() add_squashed_msg () {
{ if test -n "$message"
if [ -n "$message" ]; then then
echo "$message" echo "$message"
else else
echo "Merge commit '$1' as '$2'" echo "Merge commit '$1' as '$2'"
fi fi
} }
rejoin_msg() rejoin_msg () {
{
dir="$1" dir="$1"
latest_old="$2" latest_old="$2"
latest_new="$3" latest_new="$3"
if [ -n "$message" ]; then if test -n "$message"
then
commit_message="$message" commit_message="$message"
else else
commit_message="Split '$dir/' into commit '$latest_new'" commit_message="Split '$dir/' into commit '$latest_new'"
@ -377,14 +451,14 @@ rejoin_msg()
EOF EOF
} }
squash_msg() squash_msg () {
{
dir="$1" dir="$1"
oldsub="$2" oldsub="$2"
newsub="$3" newsub="$3"
newsub_short=$(git rev-parse --short "$newsub") newsub_short=$(git rev-parse --short "$newsub")
if [ -n "$oldsub" ]; then if test -n "$oldsub"
then
oldsub_short=$(git rev-parse --short "$oldsub") oldsub_short=$(git rev-parse --short "$oldsub")
echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short" echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
echo echo
@ -399,35 +473,35 @@ squash_msg()
echo "git-subtree-split: $newsub" echo "git-subtree-split: $newsub"
} }
toptree_for_commit() toptree_for_commit () {
{
commit="$1" commit="$1"
git log -1 --pretty=format:'%T' "$commit" -- || exit $? git log -1 --pretty=format:'%T' "$commit" -- || exit $?
} }
subtree_for_commit() subtree_for_commit () {
{
commit="$1" commit="$1"
dir="$2" dir="$2"
git ls-tree "$commit" -- "$dir" | git ls-tree "$commit" -- "$dir" |
while read mode type tree name; do while read mode type tree name
assert [ "$name" = "$dir" ] do
assert [ "$type" = "tree" -o "$type" = "commit" ] assert test "$name" = "$dir"
[ "$type" = "commit" ] && continue # ignore submodules assert test "$type" = "tree" -o "$type" = "commit"
test "$type" = "commit" && continue # ignore submodules
echo $tree echo $tree
break break
done done
} }
tree_changed() tree_changed () {
{
tree=$1 tree=$1
shift shift
if [ $# -ne 1 ]; then if test $# -ne 1
then
return 0 # weird parents, consider it changed return 0 # weird parents, consider it changed
else else
ptree=$(toptree_for_commit $1) ptree=$(toptree_for_commit $1)
if [ "$ptree" != "$tree" ]; then if test "$ptree" != "$tree"
then
return 0 # changed return 0 # changed
else else
return 1 # not changed return 1 # not changed
@ -435,13 +509,13 @@ tree_changed()
fi fi
} }
new_squash_commit() new_squash_commit () {
{
old="$1" old="$1"
oldsub="$2" oldsub="$2"
newsub="$3" newsub="$3"
tree=$(toptree_for_commit $newsub) || exit $? tree=$(toptree_for_commit $newsub) || exit $?
if [ -n "$old" ]; then if test -n "$old"
then
squash_msg "$dir" "$oldsub" "$newsub" | squash_msg "$dir" "$oldsub" "$newsub" |
git commit-tree "$tree" -p "$old" || exit $? git commit-tree "$tree" -p "$old" || exit $?
else else
@ -450,21 +524,22 @@ new_squash_commit()
fi fi
} }
copy_or_skip() copy_or_skip () {
{
rev="$1" rev="$1"
tree="$2" tree="$2"
newparents="$3" newparents="$3"
assert [ -n "$tree" ] assert test -n "$tree"
identical= identical=
nonidentical= nonidentical=
p= p=
gotparents= gotparents=
for parent in $newparents; do for parent in $newparents
do
ptree=$(toptree_for_commit $parent) || exit $? ptree=$(toptree_for_commit $parent) || exit $?
[ -z "$ptree" ] && continue test -z "$ptree" && continue
if [ "$ptree" = "$tree" ]; then if test "$ptree" = "$tree"
then
# an identical parent could be used in place of this rev. # an identical parent could be used in place of this rev.
identical="$parent" identical="$parent"
else else
@ -474,63 +549,72 @@ copy_or_skip()
# sometimes both old parents map to the same newparent; # sometimes both old parents map to the same newparent;
# eliminate duplicates # eliminate duplicates
is_new=1 is_new=1
for gp in $gotparents; do for gp in $gotparents
if [ "$gp" = "$parent" ]; then do
if test "$gp" = "$parent"
then
is_new= is_new=
break break
fi fi
done done
if [ -n "$is_new" ]; then if test -n "$is_new"
then
gotparents="$gotparents $parent" gotparents="$gotparents $parent"
p="$p -p $parent" p="$p -p $parent"
fi fi
done done
copycommit= copycommit=
if [ -n "$identical" ] && [ -n "$nonidentical" ]; then if test -n "$identical" && test -n "$nonidentical"
then
extras=$(git rev-list --count $identical..$nonidentical) extras=$(git rev-list --count $identical..$nonidentical)
if [ "$extras" -ne 0 ]; then if test "$extras" -ne 0
then
# we need to preserve history along the other branch # we need to preserve history along the other branch
copycommit=1 copycommit=1
fi fi
fi fi
if [ -n "$identical" ] && [ -z "$copycommit" ]; then if test -n "$identical" && test -z "$copycommit"
then
echo $identical echo $identical
else else
copy_commit $rev $tree "$p" || exit $? copy_commit "$rev" "$tree" "$p" || exit $?
fi fi
} }
ensure_clean() ensure_clean () {
{ if ! git diff-index HEAD --exit-code --quiet 2>&1
if ! git diff-index HEAD --exit-code --quiet 2>&1; then then
die "Working tree has modifications. Cannot add." die "Working tree has modifications. Cannot add."
fi fi
if ! git diff-index --cached HEAD --exit-code --quiet 2>&1; then if ! git diff-index --cached HEAD --exit-code --quiet 2>&1
then
die "Index has modifications. Cannot add." die "Index has modifications. Cannot add."
fi fi
} }
ensure_valid_ref_format() ensure_valid_ref_format () {
{
git check-ref-format "refs/heads/$1" || git check-ref-format "refs/heads/$1" ||
die "'$1' does not look like a ref" die "'$1' does not look like a ref"
} }
cmd_add() cmd_add () {
{ if test -e "$dir"
if [ -e "$dir" ]; then then
die "'$dir' already exists. Cannot add." die "'$dir' already exists. Cannot add."
fi fi
ensure_clean ensure_clean
if [ $# -eq 1 ]; then if test $# -eq 1
then
git rev-parse -q --verify "$1^{commit}" >/dev/null || git rev-parse -q --verify "$1^{commit}" >/dev/null ||
die "'$1' does not refer to a commit" die "'$1' does not refer to a commit"
"cmd_add_commit" "$@" cmd_add_commit "$@"
elif [ $# -eq 2 ]; then
elif test $# -eq 2
then
# Technically we could accept a refspec here but we're # Technically we could accept a refspec here but we're
# just going to turn around and add FETCH_HEAD under the # just going to turn around and add FETCH_HEAD under the
# specified directory. Allowing a refspec might be # specified directory. Allowing a refspec might be
@ -538,15 +622,14 @@ cmd_add()
# branches fetched via the refspec. # branches fetched via the refspec.
ensure_valid_ref_format "$2" ensure_valid_ref_format "$2"
"cmd_add_repository" "$@" cmd_add_repository "$@"
else else
say "error: parameters were '$@'" say "error: parameters were '$@'"
die "Provide either a commit or a repository and commit." die "Provide either a commit or a repository and commit."
fi fi
} }
cmd_add_repository() cmd_add_repository () {
{
echo "git fetch" "$@" echo "git fetch" "$@"
repository=$1 repository=$1
refspec=$2 refspec=$2
@ -556,8 +639,7 @@ cmd_add_repository()
cmd_add_commit "$@" cmd_add_commit "$@"
} }
cmd_add_commit() cmd_add_commit () {
{
revs=$(git rev-parse $default --revs-only "$@") || exit $? revs=$(git rev-parse $default --revs-only "$@") || exit $?
set -- $revs set -- $revs
rev="$1" rev="$1"
@ -568,43 +650,47 @@ cmd_add_commit()
tree=$(git write-tree) || exit $? tree=$(git write-tree) || exit $?
headrev=$(git rev-parse HEAD) || exit $? headrev=$(git rev-parse HEAD) || exit $?
if [ -n "$headrev" -a "$headrev" != "$rev" ]; then if test -n "$headrev" && test "$headrev" != "$rev"
then
headp="-p $headrev" headp="-p $headrev"
else else
headp= headp=
fi fi
if [ -n "$squash" ]; then if test -n "$squash"
then
rev=$(new_squash_commit "" "" "$rev") || exit $? rev=$(new_squash_commit "" "" "$rev") || exit $?
commit=$(add_squashed_msg "$rev" "$dir" | commit=$(add_squashed_msg "$rev" "$dir" |
git commit-tree $tree $headp -p "$rev") || exit $? git commit-tree "$tree" $headp -p "$rev") || exit $?
else else
revp=$(peel_committish "$rev") && revp=$(peel_committish "$rev") &&
commit=$(add_msg "$dir" "$headrev" "$rev" | commit=$(add_msg "$dir" $headrev "$rev" |
git commit-tree $tree $headp -p "$revp") || exit $? git commit-tree "$tree" $headp -p "$revp") || exit $?
fi fi
git reset "$commit" || exit $? git reset "$commit" || exit $?
say "Added dir '$dir'" say "Added dir '$dir'"
} }
cmd_split() cmd_split () {
{
debug "Splitting $dir..." debug "Splitting $dir..."
cache_setup || exit $? cache_setup || exit $?
if [ -n "$onto" ]; then if test -n "$onto"
then
debug "Reading history for --onto=$onto..." debug "Reading history for --onto=$onto..."
git rev-list $onto | git rev-list $onto |
while read rev; do while read rev
do
# the 'onto' history is already just the subdir, so # the 'onto' history is already just the subdir, so
# any parent we find there can be used verbatim # any parent we find there can be used verbatim
debug " cache: $rev" debug " cache: $rev"
cache_set $rev $rev cache_set "$rev" "$rev"
done done
fi fi
if [ -n "$ignore_joins" ]; then if test -n "$ignore_joins"
then
unrevs= unrevs=
else else
unrevs="$(find_existing_splits "$dir" "$revs")" unrevs="$(find_existing_splits "$dir" "$revs")"
@ -618,12 +704,14 @@ cmd_split()
revcount=0 revcount=0
createcount=0 createcount=0
eval "$grl" | eval "$grl" |
while read rev parents; do while read rev parents
do
revcount=$(($revcount + 1)) revcount=$(($revcount + 1))
progress "$revcount/$revmax ($createcount)" progress "$revcount/$revmax ($createcount)"
debug "Processing commit: $rev" debug "Processing commit: $rev"
exists=$(cache_get $rev) exists=$(cache_get "$rev")
if [ -n "$exists" ]; then if test -n "$exists"
then
debug " prior: $exists" debug " prior: $exists"
continue continue
fi fi
@ -632,76 +720,88 @@ cmd_split()
newparents=$(cache_get $parents) newparents=$(cache_get $parents)
debug " newparents: $newparents" debug " newparents: $newparents"
tree=$(subtree_for_commit $rev "$dir") tree=$(subtree_for_commit "$rev" "$dir")
debug " tree is: $tree" debug " tree is: $tree"
check_parents $parents check_parents $parents
# ugly. is there no better way to tell if this is a subtree # ugly. is there no better way to tell if this is a subtree
# vs. a mainline commit? Does it matter? # vs. a mainline commit? Does it matter?
if [ -z $tree ]; then if test -z "$tree"
set_notree $rev then
if [ -n "$newparents" ]; then set_notree "$rev"
cache_set $rev $rev if test -n "$newparents"
then
cache_set "$rev" "$rev"
fi fi
continue continue
fi fi
newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $? newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
debug " newrev is: $newrev" debug " newrev is: $newrev"
cache_set $rev $newrev cache_set "$rev" "$newrev"
cache_set latest_new $newrev cache_set latest_new "$newrev"
cache_set latest_old $rev cache_set latest_old "$rev"
done || exit $? done || exit $?
latest_new=$(cache_get latest_new) latest_new=$(cache_get latest_new)
if [ -z "$latest_new" ]; then if test -z "$latest_new"
then
die "No new revisions were found" die "No new revisions were found"
fi fi
if [ -n "$rejoin" ]; then if test -n "$rejoin"
then
debug "Merging split branch into HEAD..." debug "Merging split branch into HEAD..."
latest_old=$(cache_get latest_old) latest_old=$(cache_get latest_old)
git merge -s ours \ git merge -s ours \
--allow-unrelated-histories \ --allow-unrelated-histories \
-m "$(rejoin_msg "$dir" $latest_old $latest_new)" \ -m "$(rejoin_msg "$dir" "$latest_old" "$latest_new")" \
$latest_new >&2 || exit $? "$latest_new" >&2 || exit $?
fi fi
if [ -n "$branch" ]; then if test -n "$branch"
if rev_exists "refs/heads/$branch"; then then
if ! rev_is_descendant_of_branch $latest_new $branch; then if rev_exists "refs/heads/$branch"
then
if ! rev_is_descendant_of_branch "$latest_new" "$branch"
then
die "Branch '$branch' is not an ancestor of commit '$latest_new'." die "Branch '$branch' is not an ancestor of commit '$latest_new'."
fi fi
action='Updated' action='Updated'
else else
action='Created' action='Created'
fi fi
git update-ref -m 'subtree split' "refs/heads/$branch" $latest_new || exit $? git update-ref -m 'subtree split' \
"refs/heads/$branch" "$latest_new" || exit $?
say "$action branch '$branch'" say "$action branch '$branch'"
fi fi
echo $latest_new echo "$latest_new"
exit 0 exit 0
} }
cmd_merge() cmd_merge () {
{
revs=$(git rev-parse $default --revs-only "$@") || exit $? revs=$(git rev-parse $default --revs-only "$@") || exit $?
ensure_clean ensure_clean
set -- $revs set -- $revs
if [ $# -ne 1 ]; then if test $# -ne 1
then
die "You must provide exactly one revision. Got: '$revs'" die "You must provide exactly one revision. Got: '$revs'"
fi fi
rev="$1" rev="$1"
if [ -n "$squash" ]; then if test -n "$squash"
then
first_split="$(find_latest_squash "$dir")" first_split="$(find_latest_squash "$dir")"
if [ -z "$first_split" ]; then if test -z "$first_split"
then
die "Can't squash-merge: '$dir' was never added." die "Can't squash-merge: '$dir' was never added."
fi fi
set $first_split set $first_split
old=$1 old=$1
sub=$2 sub=$2
if [ "$sub" = "$rev" ]; then if test "$sub" = "$rev"
then
say "Subtree is already at commit $rev." say "Subtree is already at commit $rev."
exit 0 exit 0
fi fi
@ -711,24 +811,28 @@ cmd_merge()
fi fi
version=$(git version) version=$(git version)
if [ "$version" \< "git version 1.7" ]; then if test "$version" \< "git version 1.7"
if [ -n "$message" ]; then then
git merge -s subtree --message="$message" $rev if test -n "$message"
then
git merge -s subtree --message="$message" "$rev"
else else
git merge -s subtree $rev git merge -s subtree "$rev"
fi fi
else else
if [ -n "$message" ]; then if test -n "$message"
git merge -Xsubtree="$prefix" --message="$message" $rev then
git merge -Xsubtree="$prefix" \
--message="$message" "$rev"
else else
git merge -Xsubtree="$prefix" $rev git merge -Xsubtree="$prefix" $rev
fi fi
fi fi
} }
cmd_pull() cmd_pull () {
{ if test $# -ne 2
if [ $# -ne 2 ]; then then
die "You must provide <repository> <ref>" die "You must provide <repository> <ref>"
fi fi
ensure_clean ensure_clean
@ -739,18 +843,19 @@ cmd_pull()
cmd_merge "$@" cmd_merge "$@"
} }
cmd_push() cmd_push () {
{ if test $# -ne 2
if [ $# -ne 2 ]; then then
die "You must provide <repository> <ref>" die "You must provide <repository> <ref>"
fi fi
ensure_valid_ref_format "$2" ensure_valid_ref_format "$2"
if [ -e "$dir" ]; then if test -e "$dir"
then
repository=$1 repository=$1
refspec=$2 refspec=$2
echo "git push using: " $repository $refspec echo "git push using: " "$repository" "$refspec"
localrev=$(git subtree split --prefix="$prefix") || die localrev=$(git subtree split --prefix="$prefix") || die
git push "$repository" $localrev:refs/heads/$refspec git push "$repository" "$localrev":"refs/heads/$refspec"
else else
die "'$dir' must already exist. Try 'git subtree add'." die "'$dir' must already exist. Try 'git subtree add'."
fi fi