From 0ca71b3737cbb26fbf037aa15b3f58735785e6e3 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 24 Apr 2009 14:13:34 -0400 Subject: [PATCH 001/103] basic options parsing and whatnot. --- .gitignore | 1 + git-subtree | 1 + git-subtree.sh | 123 +++++++++++++++++++++++++++++++++++++++++++++++++ shellopts.sh | 1 + 4 files changed, 126 insertions(+) create mode 100644 .gitignore create mode 120000 git-subtree create mode 100755 git-subtree.sh create mode 100644 shellopts.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..b25c15b81f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/git-subtree b/git-subtree new file mode 120000 index 0000000000..7d7539894e --- /dev/null +++ b/git-subtree @@ -0,0 +1 @@ +git-subtree.sh \ No newline at end of file diff --git a/git-subtree.sh b/git-subtree.sh new file mode 100755 index 0000000000..5e5b27f8ad --- /dev/null +++ b/git-subtree.sh @@ -0,0 +1,123 @@ +#!/bin/bash +# +# git-subtree.sh: split/join git repositories in subdirectories of this one +# +# Copyright (c) 2009 Avery Pennarun +# +OPTS_SPEC="\ +git subtree split -- +git subtree merge + +git subtree does foo and bar! +-- +h,help show the help +q quiet +v verbose +" +eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?) +. git-sh-setup +require_work_tree + +quiet= +command= + +debug() +{ + if [ -z "$quiet" ]; then + echo "$@" >&2 + fi +} + +#echo "Options: $*" + +while [ $# -gt 0 ]; do + opt="$1" + shift + case "$opt" in + -q) quiet=1 ;; + --) break ;; + esac +done + +command="$1" +shift +case "$command" in + split|merge) ;; + *) die "Unknown command '$command'" ;; +esac + +revs=$(git rev-parse --default HEAD --revs-only "$@") || exit $? +dirs="$(git rev-parse --sq --no-revs --no-flags "$@")" || exit $? + +#echo "dirs is {$dirs}" +eval $(echo set -- $dirs) +if [ "$#" -ne 1 ]; then + die "Must provide exactly one subtree dir (got $#)" +fi +dir="$1" + +debug "command: {$command}" +debug "quiet: {$quiet}" +debug "revs: {$revs}" +debug "dir: {$dir}" + +cache_setup() +{ + cachedir="$GIT_DIR/subtree-cache/$dir" + rm -rf "$cachedir" || die "Can't delete old cachedir: $cachedir" + mkdir -p "$cachedir" || die "Can't create new cachedir: $cachedir" + debug "Using cachedir: $cachedir" >&2 + echo "$cachedir" +} + +cache_get() +{ + for oldrev in $*; do + if [ -r "$cachedir/$oldrev" ]; then + read newrev <"$cachedir/$oldrev" + echo $newrev + fi + done +} + +cache_set() +{ + oldrev="$1" + newrev="$2" + if [ -e "$cachedir/$oldrev" ]; then + die "cache for $oldrev already exists!" + fi + echo "$newrev" >"$cachedir/$oldrev" +} + +cmd_split() +{ + debug "Splitting $dir..." + cache_setup || exit $? + + git rev-list --reverse --parents $revs -- "$dir" | + while read rev parents; do + newparents=$(cache_get $parents) + echo "rev: $rev / $newparents" + + git ls-tree $rev -- "$dir" | + while read mode type tree name; do + p="" + for parent in $newparents; do + p="$p -p $parent" + done + newrev=$(echo synthetic | git commit-tree $tree $p) \ + || die "Can't create new commit for $rev / $tree" + cache_set $rev $newrev + done + done + + exit 0 +} + +cmd_merge() +{ + die "merge command not implemented yet" +} + +"cmd_$command" diff --git a/shellopts.sh b/shellopts.sh new file mode 100644 index 0000000000..42644cd019 --- /dev/null +++ b/shellopts.sh @@ -0,0 +1 @@ +export PATH=$PWD:$PATH From 2573354e9b1619e92b8847d098555a0b6997c231 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 24 Apr 2009 14:24:38 -0400 Subject: [PATCH 002/103] 'git subtree split' now basically works. --- git-subtree.sh | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 5e5b27f8ad..c59759baa6 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -28,6 +28,16 @@ debug() fi } +assert() +{ + if "$@"; then + : + else + die "assertion failed: " "$@" + fi +} + + #echo "Options: $*" while [ $# -gt 0 ]; do @@ -63,7 +73,7 @@ debug "dir: {$dir}" cache_setup() { - cachedir="$GIT_DIR/subtree-cache/$dir" + cachedir="$GIT_DIR/subtree-cache/$$" rm -rf "$cachedir" || die "Can't delete old cachedir: $cachedir" mkdir -p "$cachedir" || die "Can't create new cachedir: $cachedir" debug "Using cachedir: $cachedir" >&2 @@ -98,19 +108,23 @@ cmd_split() git rev-list --reverse --parents $revs -- "$dir" | while read rev parents; do newparents=$(cache_get $parents) - echo "rev: $rev / $newparents" + debug + debug "Processing commit: $rev / $newparents" git ls-tree $rev -- "$dir" | while read mode type tree name; do + assert [ "$name" = "$dir" ] + debug " tree is: $tree" p="" for parent in $newparents; do p="$p -p $parent" done newrev=$(echo synthetic | git commit-tree $tree $p) \ || die "Can't create new commit for $rev / $tree" + echo " newrev is: $newrev" cache_set $rev $newrev - done - done + done || exit $? + done || exit $? exit 0 } From fd9500eef2eba7e8f66f984c924a11282daaa870 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 24 Apr 2009 14:45:02 -0400 Subject: [PATCH 003/103] We now copy the other stuff about a commit (changelog, author, etc). --- git-subtree.sh | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index c59759baa6..256946b0de 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -77,7 +77,6 @@ cache_setup() rm -rf "$cachedir" || die "Can't delete old cachedir: $cachedir" mkdir -p "$cachedir" || die "Can't create new cachedir: $cachedir" debug "Using cachedir: $cachedir" >&2 - echo "$cachedir" } cache_get() @@ -100,6 +99,24 @@ cache_set() echo "$newrev" >"$cachedir/$oldrev" } +copy_commit() +{ + # We're doing to set some environment vars here, so + # do it in a subshell to get rid of them safely later + git log -1 --pretty=format:'%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%s%n%n%b' "$1" | + ( + read GIT_AUTHOR_NAME + read GIT_AUTHOR_EMAIL + read GIT_AUTHOR_DATE + read GIT_COMMITTER_NAME + read GIT_COMMITTER_EMAIL + read GIT_COMMITTER_DATE + export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE + export GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE + git commit-tree "$2" $3 # reads the rest of stdin + ) || die "Can't copy commit $1" +} + cmd_split() { debug "Splitting $dir..." @@ -119,9 +136,9 @@ cmd_split() for parent in $newparents; do p="$p -p $parent" done - newrev=$(echo synthetic | git commit-tree $tree $p) \ - || die "Can't create new commit for $rev / $tree" - echo " newrev is: $newrev" + + newrev=$(copy_commit $rev $tree "$p") || exit $? + debug " newrev is: $newrev" cache_set $rev $newrev done || exit $? done || exit $? From e25a6bf837ba378b0a8264e580c61069951dce66 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 24 Apr 2009 14:52:27 -0400 Subject: [PATCH 004/103] Print out the newly created commitid at the end, for use in other scripts. --- git-subtree.sh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 256946b0de..5f8b0f6c59 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -93,7 +93,7 @@ cache_set() { oldrev="$1" newrev="$2" - if [ -e "$cachedir/$oldrev" ]; then + if [ "$oldrev" != "latest" -a -e "$cachedir/$oldrev" ]; then die "cache for $oldrev already exists!" fi echo "$newrev" >"$cachedir/$oldrev" @@ -140,9 +140,14 @@ cmd_split() newrev=$(copy_commit $rev $tree "$p") || exit $? debug " newrev is: $newrev" cache_set $rev $newrev + cache_set latest $newrev done || exit $? done || exit $? - + latest=$(cache_get latest) + if [ -z "$latest" ]; then + die "No new revisions were found" + fi + echo $latest exit 0 } From b77172f8140d830a9160306c8ee10ceed413ba23 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 24 Apr 2009 15:48:41 -0400 Subject: [PATCH 005/103] Add a new --rejoin option. The idea is to join the new split branch back into this one, so future splits can append themselves to the old split branch. We mark the split branch's history in our merge commit, so we can pull it back out later. --- git-subtree.sh | 53 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 5f8b0f6c59..a31b0b2f6a 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -2,10 +2,10 @@ # # git-subtree.sh: split/join git repositories in subdirectories of this one # -# Copyright (c) 2009 Avery Pennarun +# Copyright (C) 2009 Avery Pennarun # OPTS_SPEC="\ -git subtree split -- +git subtree split [--rejoin] [--onto rev] -- git subtree merge git subtree does foo and bar! @@ -13,6 +13,8 @@ git subtree does foo and bar! h,help show the help q quiet v verbose +onto= existing subtree revision to connect, if any +rejoin merge the new branch back into HEAD " eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?) . git-sh-setup @@ -20,6 +22,8 @@ require_work_tree quiet= command= +onto= +rejoin= debug() { @@ -42,9 +46,12 @@ assert() while [ $# -gt 0 ]; do opt="$1" + debug "option: $1" shift case "$opt" in -q) quiet=1 ;; + --onto) onto="$1"; shift ;; + --rejoin) rejoin=1 ;; --) break ;; esac done @@ -93,7 +100,9 @@ cache_set() { oldrev="$1" newrev="$2" - if [ "$oldrev" != "latest" -a -e "$cachedir/$oldrev" ]; then + if [ "$oldrev" != "latest_old" \ + -a "$oldrev" != "latest_new" \ + -a -e "$cachedir/$oldrev" ]; then die "cache for $oldrev already exists!" fi echo "$newrev" >"$cachedir/$oldrev" @@ -111,12 +120,29 @@ copy_commit() read GIT_COMMITTER_NAME read GIT_COMMITTER_EMAIL read GIT_COMMITTER_DATE - export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE - export GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE + export GIT_AUTHOR_NAME \ + GIT_AUTHOR_EMAIL \ + GIT_AUTHOR_DATE \ + GIT_COMMITTER_NAME \ + GIT_COMMITTER_EMAIL \ + GIT_COMMITTER_DATE git commit-tree "$2" $3 # reads the rest of stdin ) || die "Can't copy commit $1" } +merge_msg() +{ + dir="$1" + latest_old="$2" + latest_new="$3" + cat <<-EOF + Split changes from '$dir/' into commit '$latest_new' + + git-subtree-dir: $dir + git-subtree-includes: $latest_old + EOF +} + cmd_split() { debug "Splitting $dir..." @@ -140,14 +166,23 @@ cmd_split() newrev=$(copy_commit $rev $tree "$p") || exit $? debug " newrev is: $newrev" cache_set $rev $newrev - cache_set latest $newrev + cache_set latest_new $newrev + cache_set latest_old $rev done || exit $? done || exit $? - latest=$(cache_get latest) - if [ -z "$latest" ]; then + latest_new=$(cache_get latest_new) + if [ -z "$latest_new" ]; then die "No new revisions were found" fi - echo $latest + + if [ -n "$rejoin" ]; then + debug "Merging split branch into HEAD..." + latest_old=$(cache_get latest_old) + git merge -s ours \ + -m "$(merge_msg $dir $latest_old $latest_new)" \ + $latest_new + fi + echo $latest_new exit 0 } From 8b4a77f2a16d730f7261d66991107ab404b3b4ac Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 24 Apr 2009 16:48:08 -0400 Subject: [PATCH 006/103] Use information about prior splits to make sure merges work right. --- git-subtree.sh | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index a31b0b2f6a..af1d332e24 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -46,7 +46,6 @@ assert() while [ $# -gt 0 ]; do opt="$1" - debug "option: $1" shift case "$opt" in -q) quiet=1 ;; @@ -108,6 +107,30 @@ cache_set() echo "$newrev" >"$cachedir/$oldrev" } +find_existing_splits() +{ + debug "Looking for prior splits..." + dir="$1" + revs="$2" + git log --grep="^git-subtree-dir: $dir\$" \ + --pretty=format:'%s%n%n%b%nEND' "$revs" | + while read a b junk; do + case "$a" in + git-subtree-mainline:) main="$b" ;; + git-subtree-split:) sub="$b" ;; + *) + if [ -n "$main" -a -n "$sub" ]; then + debug " Prior: $main -> $sub" + cache_set $main $sub + echo "^$main^ ^$sub^" + main= + sub= + fi + ;; + esac + done +} + copy_commit() { # We're doing to set some environment vars here, so @@ -136,10 +159,11 @@ merge_msg() latest_old="$2" latest_new="$3" cat <<-EOF - Split changes from '$dir/' into commit '$latest_new' + Split '$dir/' into commit '$latest_new' git-subtree-dir: $dir - git-subtree-includes: $latest_old + git-subtree-mainline: $latest_old + git-subtree-split: $latest_new EOF } @@ -148,12 +172,20 @@ cmd_split() debug "Splitting $dir..." cache_setup || exit $? - git rev-list --reverse --parents $revs -- "$dir" | + unrevs="$(find_existing_splits "$dir" "$revs")" + + git rev-list --reverse --parents $revs $unrevs -- "$dir" | while read rev parents; do + exists=$(cache_get $rev) newparents=$(cache_get $parents) debug debug "Processing commit: $rev / $newparents" + if [ -n "$exists" ]; then + debug " prior: $exists" + continue + fi + git ls-tree $rev -- "$dir" | while read mode type tree name; do assert [ "$name" = "$dir" ] From 33ff583ad7a52edb5ed9d869109e04c846d6209d Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 24 Apr 2009 17:05:14 -0400 Subject: [PATCH 007/103] Added a --onto option, but it's so complicated I can't tell if it works. --- git-subtree.sh | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/git-subtree.sh b/git-subtree.sh index af1d332e24..7e1707ae2a 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -13,7 +13,7 @@ git subtree does foo and bar! h,help show the help q quiet v verbose -onto= existing subtree revision to connect, if any +onto= existing subtree revision to search for parent rejoin merge the new branch back into HEAD " eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?) @@ -172,6 +172,16 @@ cmd_split() debug "Splitting $dir..." cache_setup || exit $? + if [ -n "$onto" ]; then + echo "Reading history for $onto..." + git rev-list $onto | + while read rev; do + # the 'onto' history is already just the subdir, so + # any parent we find there can be used verbatim + cache_set $rev $rev + done + fi + unrevs="$(find_existing_splits "$dir" "$revs")" git rev-list --reverse --parents $revs $unrevs -- "$dir" | From 2c71b7c46d1ff887fb276a9d42346678a2948a00 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 24 Apr 2009 17:42:33 -0400 Subject: [PATCH 008/103] Hmm... can't actually filter rev-list on the subdir name. Otherwise we can't keep track of parent relationships. Argh. This change makes it "work", but we get a bunch of empty commits. --- git-subtree.sh | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 7e1707ae2a..1e1237f520 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -173,28 +173,31 @@ cmd_split() cache_setup || exit $? if [ -n "$onto" ]; then - echo "Reading history for $onto..." + echo "Reading history for --onto=$onto..." git rev-list $onto | while read rev; do # the 'onto' history is already just the subdir, so # any parent we find there can be used verbatim + debug " cache: $rev" cache_set $rev $rev done fi unrevs="$(find_existing_splits "$dir" "$revs")" - git rev-list --reverse --parents $revs $unrevs -- "$dir" | + debug "git rev-list --reverse $revs $unrevs" + git rev-list --reverse --parents $revs $unrevsx | while read rev parents; do - exists=$(cache_get $rev) - newparents=$(cache_get $parents) debug - debug "Processing commit: $rev / $newparents" - + debug "Processing commit: $rev" + exists=$(cache_get $rev) if [ -n "$exists" ]; then debug " prior: $exists" continue fi + debug " parents: $parents" + newparents=$(cache_get $parents) + debug " newparents: $newparents" git ls-tree $rev -- "$dir" | while read mode type tree name; do From 768d6d10051cbf2d0261b9cf671b1f969fb77a61 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 24 Apr 2009 17:53:10 -0400 Subject: [PATCH 009/103] Skip over empty commits. But we still need to get rid of unnecessary merge commits somehow... --- git-subtree.sh | 55 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 1e1237f520..7ae71886f4 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -167,6 +167,32 @@ merge_msg() EOF } +tree_for_commit() +{ + git ls-tree "$1" -- "$dir" | + while read mode type tree name; do + assert [ "$name" = "$dir" ] + echo $tree + break + done +} + +tree_changed() +{ + tree=$1 + shift + if [ $# -ne 1 ]; then + return 0 # weird parents, consider it changed + else + ptree=$(tree_for_commit $1) + if [ "$ptree" != "$tree" ]; then + return 0 # changed + else + return 1 # not changed + fi + fi +} + cmd_split() { debug "Splitting $dir..." @@ -199,21 +225,24 @@ cmd_split() newparents=$(cache_get $parents) debug " newparents: $newparents" - git ls-tree $rev -- "$dir" | - while read mode type tree name; do - assert [ "$name" = "$dir" ] - debug " tree is: $tree" - p="" - for parent in $newparents; do - p="$p -p $parent" - done + tree=$(tree_for_commit $rev) + debug " tree is: $tree" + [ -z $tree ] && continue + + p="" + for parent in $newparents; do + p="$p -p $parent" + done + if tree_changed $tree $parents; then newrev=$(copy_commit $rev $tree "$p") || exit $? - debug " newrev is: $newrev" - cache_set $rev $newrev - cache_set latest_new $newrev - cache_set latest_old $rev - done || exit $? + else + newrev="$newparents" + fi + debug " newrev is: $newrev" + cache_set $rev $newrev + cache_set latest_new $newrev + cache_set latest_old $rev done || exit $? latest_new=$(cache_get latest_new) if [ -z "$latest_new" ]; then From 847e868167eb0e8a270004cbccec4ae36db06626 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 24 Apr 2009 21:35:50 -0400 Subject: [PATCH 010/103] Quick test script for generating reasonably complex merge scenarios. --- git-subtree.sh | 5 +-- test.sh | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 2 deletions(-) create mode 100755 test.sh diff --git a/git-subtree.sh b/git-subtree.sh index 7ae71886f4..ffffb5ed88 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -149,6 +149,7 @@ copy_commit() GIT_COMMITTER_NAME \ GIT_COMMITTER_EMAIL \ GIT_COMMITTER_DATE + (echo -n '*'; cat ) | # FIXME git commit-tree "$2" $3 # reads the rest of stdin ) || die "Can't copy commit $1" } @@ -199,7 +200,7 @@ cmd_split() cache_setup || exit $? if [ -n "$onto" ]; then - echo "Reading history for --onto=$onto..." + debug "Reading history for --onto=$onto..." git rev-list $onto | while read rev; do # the 'onto' history is already just the subdir, so @@ -254,7 +255,7 @@ cmd_split() latest_old=$(cache_get latest_old) git merge -s ours \ -m "$(merge_msg $dir $latest_old $latest_new)" \ - $latest_new + $latest_new >&2 fi echo $latest_new exit 0 diff --git a/test.sh b/test.sh new file mode 100755 index 0000000000..8a9d92f703 --- /dev/null +++ b/test.sh @@ -0,0 +1,96 @@ +#!/bin/bash -x +. shellopts.sh +set -e + +rm -rf mainline subproj +mkdir mainline subproj + +cd subproj +git init + +touch sub1 +git add sub1 +git commit -m 'sub-1' +git branch sub1 +git branch -m master subproj + +touch sub2 +git add sub2 +git commit -m 'sub-2' +git branch sub2 + +touch sub3 +git add sub3 +git commit -m 'sub-3' +git branch sub3 + +cd ../mainline +git init +touch main1 +git add main1 +git commit -m 'main-1' +git branch -m master mainline + +git fetch ../subproj sub1 +git branch sub1 FETCH_HEAD +git read-tree --prefix=subdir FETCH_HEAD +git checkout subdir +git commit -m 'initial-subdir-merge' + +git merge -m 'merge -s -ours' -s ours FETCH_HEAD + +touch subdir/main-sub3 +git add subdir/main-sub3 +git commit -m 'main-sub3' + +touch main-2 +git add main-2 +git commit -m 'main-2 boring' + +touch subdir/main-sub4 +git add subdir/main-sub4 +git commit -m 'main-sub4' + +git fetch ../subproj sub2 +git branch sub2 FETCH_HEAD +git merge -s subtree FETCH_HEAD +git branch pre-split + +split1=$(git subtree split --onto FETCH_HEAD subdir --rejoin) +echo "split1={$split1}" +git branch split1 "$split1" + +touch subdir/main-sub5 +git add subdir/main-sub5 +git commit -m 'main-sub5' + +cd ../subproj +git fetch ../mainline split1 +git branch split1 FETCH_HEAD +git merge FETCH_HEAD + +touch sub6 +git add sub6 +git commit -m 'sub6' + +cd ../mainline +split2=$(git subtree split subdir --rejoin) +git branch split2 "$split2" + +touch subdir/main-sub7 +git add subdir/main-sub7 +git commit -m 'main-sub7' + +split3=$(git subtree split subdir --rejoin) +git branch split3 "$split3" + +cd ../subproj +git fetch ../mainline split3 +git branch split3 FETCH_HEAD +git merge FETCH_HEAD +git branch subproj-merge-split3 + +cd ../mainline +git fetch ../subproj subproj-merge-split3 +git branch subproj-merge-split3 FETCH_HEAD +git merge subproj-merge-split3 From 210d083904914dd4668e032870f92ff0d5d441cc Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 24 Apr 2009 21:49:19 -0400 Subject: [PATCH 011/103] Prune out some extra merge commits by comparing their parents correctly. --- git-subtree.sh | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index ffffb5ed88..e6d8ce8817 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -168,9 +168,17 @@ merge_msg() EOF } -tree_for_commit() +toptree_for_commit() { - git ls-tree "$1" -- "$dir" | + commit="$1" + git log -1 --pretty=format:'%T' "$commit" -- || exit $? +} + +subtree_for_commit() +{ + commit="$1" + dir="$2" + git ls-tree "$commit" -- "$dir" | while read mode type tree name; do assert [ "$name" = "$dir" ] echo $tree @@ -185,7 +193,7 @@ tree_changed() if [ $# -ne 1 ]; then return 0 # weird parents, consider it changed else - ptree=$(tree_for_commit $1) + ptree=$(toptree_for_commit $1) if [ "$ptree" != "$tree" ]; then return 0 # changed else @@ -226,7 +234,7 @@ cmd_split() newparents=$(cache_get $parents) debug " newparents: $newparents" - tree=$(tree_for_commit $rev) + tree=$(subtree_for_commit $rev "$dir") debug " tree is: $tree" [ -z $tree ] && continue @@ -235,7 +243,7 @@ cmd_split() p="$p -p $parent" done - if tree_changed $tree $parents; then + if tree_changed $tree $newparents; then newrev=$(copy_commit $rev $tree "$p") || exit $? else newrev="$newparents" From d691265880abea4428783beb858683be56f3b340 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 24 Apr 2009 22:05:30 -0400 Subject: [PATCH 012/103] Even more aggressive commit trimming. Now we cut out a commit if any of its parents had the same tree; just use that parent in its place. This makes the history look nice, but I don't think it's quite right... --- git-subtree.sh | 34 ++++++++++++++++++++++++---------- test.sh | 2 +- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index e6d8ce8817..03107e2251 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -202,6 +202,29 @@ tree_changed() fi } +copy_or_skip() +{ + rev="$1" + tree="$2" + newparents="$3" + assert [ -n "$tree" ] + + p="" + for parent in $newparents; do + ptree=$(toptree_for_commit $parent) || exit $? + if [ "$ptree" = "$tree" ]; then + # any identical parent means this commit is unnecessary + echo $parent + return 0 + elif [ -n "$ptree" ]; then + # an existing, non-identical parent is important + p="$p -p $parent" + fi + done + + copy_commit $rev $tree "$p" || exit $? +} + cmd_split() { debug "Splitting $dir..." @@ -238,16 +261,7 @@ cmd_split() debug " tree is: $tree" [ -z $tree ] && continue - p="" - for parent in $newparents; do - p="$p -p $parent" - done - - if tree_changed $tree $newparents; then - newrev=$(copy_commit $rev $tree "$p") || exit $? - else - newrev="$newparents" - fi + newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $? debug " newrev is: $newrev" cache_set $rev $newrev cache_set latest_new $newrev diff --git a/test.sh b/test.sh index 8a9d92f703..a8ed0dbc7d 100755 --- a/test.sh +++ b/test.sh @@ -93,4 +93,4 @@ git branch subproj-merge-split3 cd ../mainline git fetch ../subproj subproj-merge-split3 git branch subproj-merge-split3 FETCH_HEAD -git merge subproj-merge-split3 +git merge -s subtree subproj-merge-split3 From 96db2c0448c2f6040c098d73570a96413338c662 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 24 Apr 2009 22:36:06 -0400 Subject: [PATCH 013/103] Okay, that was a little too aggressive. Now we only prune out a commit if it has exactly one remaining parent and that parent's tree is identical to ours. But I also changed the test to create the initial "-s ours" merge in one step instead of two, and that merge can be eliminated since one of its parents doesn't affect the subdir at all, and is thus deleted. --- git-subtree.sh | 46 ++++++++++++++++++++++++++++++++-------------- test.sh | 5 ++++- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 03107e2251..d42cc1a164 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -4,17 +4,21 @@ # # Copyright (C) 2009 Avery Pennarun # +if [ $# -eq 0 ]; then + set -- -h +fi OPTS_SPEC="\ -git subtree split [--rejoin] [--onto rev] -- +git subtree split [options...] -- git subtree merge git subtree does foo and bar! -- -h,help show the help -q quiet -v verbose -onto= existing subtree revision to search for parent -rejoin merge the new branch back into HEAD +h,help show the help +q quiet +v verbose +onto= try connecting new tree to an existing one +rejoin merge the new branch back into HEAD +ignore-joins ignore prior --rejoin commits " eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?) . git-sh-setup @@ -24,6 +28,7 @@ quiet= command= onto= rejoin= +ignore_joins= debug() { @@ -50,7 +55,11 @@ while [ $# -gt 0 ]; do case "$opt" in -q) quiet=1 ;; --onto) onto="$1"; shift ;; + --no-onto) onto= ;; --rejoin) rejoin=1 ;; + --no-rejoin) rejoin= ;; + --ignore-joins) ignore_joins=1 ;; + --no-ignore-joins) ignore_joins= ;; --) break ;; esac done @@ -209,20 +218,25 @@ copy_or_skip() newparents="$3" assert [ -n "$tree" ] - p="" + identical= + p= for parent in $newparents; do ptree=$(toptree_for_commit $parent) || exit $? if [ "$ptree" = "$tree" ]; then - # any identical parent means this commit is unnecessary - echo $parent - return 0 - elif [ -n "$ptree" ]; then - # an existing, non-identical parent is important + # an identical parent could be used in place of this rev. + identical="$parent" + fi + if [ -n "$ptree" ]; then + parentmatch="$parentmatch$parent" p="$p -p $parent" fi done - copy_commit $rev $tree "$p" || exit $? + if [ -n "$identical" -a "$parentmatch" = "$identical" ]; then + echo $identical + else + copy_commit $rev $tree "$p" || exit $? + fi } cmd_split() @@ -241,7 +255,11 @@ cmd_split() done fi - unrevs="$(find_existing_splits "$dir" "$revs")" + if [ -n "$ignore_joins" ]; then + unrevs= + else + unrevs="$(find_existing_splits "$dir" "$revs")" + fi debug "git rev-list --reverse $revs $unrevs" git rev-list --reverse --parents $revs $unrevsx | diff --git a/test.sh b/test.sh index a8ed0dbc7d..39c4382f0d 100755 --- a/test.sh +++ b/test.sh @@ -35,7 +35,10 @@ git fetch ../subproj sub1 git branch sub1 FETCH_HEAD git read-tree --prefix=subdir FETCH_HEAD git checkout subdir -git commit -m 'initial-subdir-merge' +tree=$(git write-tree) +com=$(echo initial-subdir-merge | git commit-tree $tree -p HEAD -p FETCH_HEAD) +git reset $com +#git commit -m 'initial-subdir-merge' git merge -m 'merge -s -ours' -s ours FETCH_HEAD From 9a8821ff33d5abc9d2603dd91c04872016d40982 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 24 Apr 2009 22:57:14 -0400 Subject: [PATCH 014/103] Pass the path using the --prefix option instead of on the command line. I like this better. It's more like git-read-tree and some other commands. --- git-subtree.sh | 28 ++++++++++++++++------------ test.sh | 6 +++--- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index d42cc1a164..0c7b755cf1 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -8,14 +8,12 @@ if [ $# -eq 0 ]; then set -- -h fi OPTS_SPEC="\ -git subtree split [options...] -- +git subtree split [options...] <--prefix=prefix> -- git subtree merge - -git subtree does foo and bar! -- h,help show the help q quiet -v verbose +prefix= the name of the subdir to split out onto= try connecting new tree to an existing one rejoin merge the new branch back into HEAD ignore-joins ignore prior --rejoin commits @@ -54,6 +52,8 @@ while [ $# -gt 0 ]; do shift case "$opt" in -q) quiet=1 ;; + --prefix) prefix="$1"; shift ;; + --no-prefix) prefix= ;; --onto) onto="$1"; shift ;; --no-onto) onto= ;; --rejoin) rejoin=1 ;; @@ -72,14 +72,16 @@ case "$command" in esac revs=$(git rev-parse --default HEAD --revs-only "$@") || exit $? -dirs="$(git rev-parse --sq --no-revs --no-flags "$@")" || exit $? -#echo "dirs is {$dirs}" -eval $(echo set -- $dirs) -if [ "$#" -ne 1 ]; then - die "Must provide exactly one subtree dir (got $#)" +if [ -z "$prefix" ]; then + die "You must provide the --prefix option." +fi +dir="$prefix" + +dirs="$(git rev-parse --no-revs --no-flags "$@")" || exit $? +if [ -n "$dirs" ]; then + die "Error: Use --prefix instead of bare filenames." fi -dir="$1" debug "command: {$command}" debug "quiet: {$quiet}" @@ -261,8 +263,10 @@ cmd_split() unrevs="$(find_existing_splits "$dir" "$revs")" fi - debug "git rev-list --reverse $revs $unrevs" - git rev-list --reverse --parents $revs $unrevsx | + # We can't restrict rev-list to only "$dir" here, because that leaves out + # critical information about commit parents. + debug "git rev-list --reverse --parents $revs $unrevs" + git rev-list --reverse --parents $revs $unrevs | while read rev parents; do debug debug "Processing commit: $rev" diff --git a/test.sh b/test.sh index 39c4382f0d..dac9b3559a 100755 --- a/test.sh +++ b/test.sh @@ -59,7 +59,7 @@ git branch sub2 FETCH_HEAD git merge -s subtree FETCH_HEAD git branch pre-split -split1=$(git subtree split --onto FETCH_HEAD subdir --rejoin) +split1=$(git subtree split --prefix subdir --onto FETCH_HEAD --rejoin) echo "split1={$split1}" git branch split1 "$split1" @@ -77,14 +77,14 @@ git add sub6 git commit -m 'sub6' cd ../mainline -split2=$(git subtree split subdir --rejoin) +split2=$(git subtree split --prefix subdir --rejoin) git branch split2 "$split2" touch subdir/main-sub7 git add subdir/main-sub7 git commit -m 'main-sub7' -split3=$(git subtree split subdir --rejoin) +split3=$(git subtree split --prefix subdir --rejoin) git branch split3 "$split3" cd ../subproj From eb7b590c8c63994051545bf1fe1f650f5dc4aedb Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 24 Apr 2009 23:28:30 -0400 Subject: [PATCH 015/103] Add a new 'git subtree add' command for adding subtrees from a given rev. --- git-subtree.sh | 56 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 0c7b755cf1..2dc99e82cd 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -8,7 +8,8 @@ if [ $# -eq 0 ]; then set -- -h fi OPTS_SPEC="\ -git subtree split [options...] <--prefix=prefix> -- +git subtree add <--prefix=prefix +git subtree split [options...] <--prefix=prefix> git subtree merge -- h,help show the help @@ -67,11 +68,12 @@ done command="$1" shift case "$command" in - split|merge) ;; + add|merge) default= ;; + split) default="--default HEAD" ;; *) die "Unknown command '$command'" ;; esac -revs=$(git rev-parse --default HEAD --revs-only "$@") || exit $? +revs=$(git rev-parse $default --revs-only "$@") || exit $? if [ -z "$prefix" ]; then die "You must provide the --prefix option." @@ -87,6 +89,7 @@ debug "command: {$command}" debug "quiet: {$quiet}" debug "revs: {$revs}" debug "dir: {$dir}" +debug cache_setup() { @@ -165,6 +168,20 @@ copy_commit() ) || die "Can't copy commit $1" } +add_msg() +{ + dir="$1" + latest_old="$2" + latest_new="$3" + cat <<-EOF + Add '$dir/' from commit '$latest_new' + + git-subtree-dir: $dir + git-subtree-mainline: $latest_old + git-subtree-split: $latest_new + EOF +} + merge_msg() { dir="$1" @@ -241,6 +258,39 @@ copy_or_skip() fi } +cmd_add() +{ + if [ -e "$dir" ]; then + die "'$dir' already exists. Cannot add." + fi + if ! git diff-index HEAD --exit-code --quiet; then + die "Working tree has modifications. Cannot add." + fi + if ! git diff-index --cached HEAD --exit-code --quiet; then + die "Index has modifications. Cannot add." + fi + set -- $revs + if [ $# -ne 1 ]; then + die "You must provide exactly one revision. Got: '$revs'" + fi + rev="$1" + + debug "Adding $dir as '$rev'..." + git read-tree --prefix="$dir" $rev || exit $? + git checkout "$dir" || exit $? + tree=$(git write-tree) || exit $? + + headrev=$(git rev-parse HEAD) || exit $? + if [ -n "$headrev" -a "$headrev" != "$rev" ]; then + headp="-p $headrev" + else + headp= + fi + commit=$(add_msg "$dir" "$headrev" "$rev" | + git commit-tree $tree $headp -p "$rev") || exit $? + git reset "$commit" || exit $? +} + cmd_split() { debug "Splitting $dir..." From 13648af5eecd0880a64b6ea4b14f485f53daa2f1 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 24 Apr 2009 23:41:19 -0400 Subject: [PATCH 016/103] Add 'git subtree merge' and 'git subtree pull'. These are simple shortcuts for 'git merge -s subtree' and 'git pull -s subtree', but it makes sense to have it all in one command. --- git-subtree.sh | 55 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 2dc99e82cd..f2a1c6aae4 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -8,13 +8,15 @@ if [ $# -eq 0 ]; then set -- -h fi OPTS_SPEC="\ -git subtree add <--prefix=prefix -git subtree split [options...] <--prefix=prefix> -git subtree merge +git subtree add --prefix= +git subtree split [options...] --prefix= +git subtree merge --prefix= +git subtree pull --prefix= -- h,help show the help q quiet prefix= the name of the subdir to split out + options for 'split' onto= try connecting new tree to an existing one rejoin merge the new branch back into HEAD ignore-joins ignore prior --rejoin commits @@ -68,27 +70,29 @@ done command="$1" shift case "$command" in - add|merge) default= ;; + add|merge|pull) default= ;; split) default="--default HEAD" ;; *) die "Unknown command '$command'" ;; esac -revs=$(git rev-parse $default --revs-only "$@") || exit $? - if [ -z "$prefix" ]; then die "You must provide the --prefix option." fi dir="$prefix" -dirs="$(git rev-parse --no-revs --no-flags "$@")" || exit $? -if [ -n "$dirs" ]; then - die "Error: Use --prefix instead of bare filenames." +if [ "$command" != "pull" ]; then + revs=$(git rev-parse $default --revs-only "$@") || exit $? + dirs="$(git rev-parse --no-revs --no-flags "$@")" || exit $? + if [ -n "$dirs" ]; then + die "Error: Use --prefix instead of bare filenames." + fi fi debug "command: {$command}" debug "quiet: {$quiet}" debug "revs: {$revs}" debug "dir: {$dir}" +debug "opts: {$*}" debug cache_setup() @@ -258,17 +262,23 @@ copy_or_skip() fi } -cmd_add() +ensure_clean() { - if [ -e "$dir" ]; then - die "'$dir' already exists. Cannot add." - fi if ! git diff-index HEAD --exit-code --quiet; then die "Working tree has modifications. Cannot add." fi if ! git diff-index --cached HEAD --exit-code --quiet; then die "Index has modifications. Cannot add." fi +} + +cmd_add() +{ + if [ -e "$dir" ]; then + die "'$dir' already exists. Cannot add." + fi + ensure_clean + set -- $revs if [ $# -ne 1 ]; then die "You must provide exactly one revision. Got: '$revs'" @@ -357,7 +367,22 @@ cmd_split() cmd_merge() { - die "merge command not implemented yet" + ensure_clean + + set -- $revs + if [ $# -ne 1 ]; then + die "You must provide exactly one revision. Got: '$revs'" + fi + rev="$1" + + git merge -s subtree $rev } -"cmd_$command" +cmd_pull() +{ + ensure_clean + set -x + git pull -s subtree "$@" +} + +"cmd_$command" "$@" From b9de53532c3e7aa6b01aa188e0f0f17a266c099d Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sat, 25 Apr 2009 00:06:45 -0400 Subject: [PATCH 017/103] Handle it successfully if a given parent commit has no parents. --- git-subtree.sh | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/git-subtree.sh b/git-subtree.sh index f2a1c6aae4..aeafadac95 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -125,6 +125,16 @@ cache_set() echo "$newrev" >"$cachedir/$oldrev" } +# if a commit doesn't have a parent, this might not work. But we only want +# to remove the parent from the rev-list, and since it doesn't exist, it won't +# be there anyway, so do nothing in that case. +try_remove_previous() +{ + if git rev-parse "$1^" >/dev/null 2>&1; then + echo "^$1^" + fi +} + find_existing_splits() { debug "Looking for prior splits..." @@ -140,7 +150,8 @@ find_existing_splits() if [ -n "$main" -a -n "$sub" ]; then debug " Prior: $main -> $sub" cache_set $main $sub - echo "^$main^ ^$sub^" + try_remove_previous "$main" + try_remove_previous "$sub" main= sub= fi From a13a299996725a979f5a7d6bd878b0237de0f26d Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sat, 25 Apr 2009 00:07:04 -0400 Subject: [PATCH 018/103] Change test.sh to test the new add, merge, and pull commands. --- test.sh | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/test.sh b/test.sh index dac9b3559a..16fb8f217e 100755 --- a/test.sh +++ b/test.sh @@ -33,13 +33,9 @@ git branch -m master mainline git fetch ../subproj sub1 git branch sub1 FETCH_HEAD -git read-tree --prefix=subdir FETCH_HEAD -git checkout subdir -tree=$(git write-tree) -com=$(echo initial-subdir-merge | git commit-tree $tree -p HEAD -p FETCH_HEAD) -git reset $com -#git commit -m 'initial-subdir-merge' +git subtree add --prefix=subdir FETCH_HEAD +# this shouldn't actually do anything, since FETCH_HEAD is already a parent git merge -m 'merge -s -ours' -s ours FETCH_HEAD touch subdir/main-sub3 @@ -56,7 +52,7 @@ git commit -m 'main-sub4' git fetch ../subproj sub2 git branch sub2 FETCH_HEAD -git merge -s subtree FETCH_HEAD +git subtree merge --prefix=subdir FETCH_HEAD git branch pre-split split1=$(git subtree split --prefix subdir --onto FETCH_HEAD --rejoin) @@ -96,4 +92,4 @@ git branch subproj-merge-split3 cd ../mainline git fetch ../subproj subproj-merge-split3 git branch subproj-merge-split3 FETCH_HEAD -git merge -s subtree subproj-merge-split3 +git subtree pull --prefix=subdir ../subproj subproj-merge-split3 From 8c384754d8db80f74d3916100772171753bee4f9 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sun, 26 Apr 2009 08:53:14 -0400 Subject: [PATCH 019/103] todo list --- todo | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 todo diff --git a/todo b/todo new file mode 100644 index 0000000000..b5b8e257aa --- /dev/null +++ b/todo @@ -0,0 +1,8 @@ + + delete tempdir + --annotate-sometimes: only annotate if the patch also changes files + outside the subdir? + 'git subtree rejoin' option to do the same as --rejoin, eg. after a + rebase + "-s subtree" should be given an explicit subtree option? + \ No newline at end of file From d0eb1b1417b855b262b5ad31d025840ea6e52094 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sun, 26 Apr 2009 08:59:12 -0400 Subject: [PATCH 020/103] Add --annotate option, and create recognizable file content during tests. --- git-subtree.sh | 6 ++++- test.sh | 67 ++++++++++++++++++++++++++++---------------------- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index aeafadac95..e54651c336 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -17,6 +17,7 @@ h,help show the help q quiet prefix= the name of the subdir to split out options for 'split' +annotate= add a prefix to commit message of new commits onto= try connecting new tree to an existing one rejoin merge the new branch back into HEAD ignore-joins ignore prior --rejoin commits @@ -30,6 +31,7 @@ command= onto= rejoin= ignore_joins= +annotate= debug() { @@ -55,6 +57,8 @@ while [ $# -gt 0 ]; do shift case "$opt" in -q) quiet=1 ;; + --annotate) annotate="$1"; shift ;; + --no-annotate) annotate= ;; --prefix) prefix="$1"; shift ;; --no-prefix) prefix= ;; --onto) onto="$1"; shift ;; @@ -178,7 +182,7 @@ copy_commit() GIT_COMMITTER_NAME \ GIT_COMMITTER_EMAIL \ GIT_COMMITTER_DATE - (echo -n '*'; cat ) | # FIXME + (echo -n "$annotate"; cat ) | git commit-tree "$2" $3 # reads the rest of stdin ) || die "Can't copy commit $1" } diff --git a/test.sh b/test.sh index 16fb8f217e..85ed7ce549 100755 --- a/test.sh +++ b/test.sh @@ -1,4 +1,11 @@ #!/bin/bash -x +create() +{ + for d in 1 2 3 4 5 6 7 8 9 10; do + echo "$1" + done >"$1" +} + . shellopts.sh set -e @@ -8,27 +15,27 @@ mkdir mainline subproj cd subproj git init -touch sub1 +create sub1 git add sub1 -git commit -m 'sub-1' +git commit -m 'sub1' git branch sub1 git branch -m master subproj -touch sub2 +create sub2 git add sub2 -git commit -m 'sub-2' +git commit -m 'sub2' git branch sub2 -touch sub3 +create sub3 git add sub3 -git commit -m 'sub-3' +git commit -m 'sub3' git branch sub3 cd ../mainline git init -touch main1 -git add main1 -git commit -m 'main-1' +create main4 +git add main4 +git commit -m 'main4' git branch -m master mainline git fetch ../subproj sub1 @@ -38,49 +45,49 @@ git subtree add --prefix=subdir FETCH_HEAD # this shouldn't actually do anything, since FETCH_HEAD is already a parent git merge -m 'merge -s -ours' -s ours FETCH_HEAD -touch subdir/main-sub3 -git add subdir/main-sub3 -git commit -m 'main-sub3' +create subdir/main-sub5 +git add subdir/main-sub5 +git commit -m 'main-sub5' -touch main-2 -git add main-2 -git commit -m 'main-2 boring' +create main6 +git add main6 +git commit -m 'main6 boring' -touch subdir/main-sub4 -git add subdir/main-sub4 -git commit -m 'main-sub4' +create subdir/main-sub7 +git add subdir/main-sub7 +git commit -m 'main-sub7' git fetch ../subproj sub2 git branch sub2 FETCH_HEAD git subtree merge --prefix=subdir FETCH_HEAD git branch pre-split -split1=$(git subtree split --prefix subdir --onto FETCH_HEAD --rejoin) +split1=$(git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --rejoin) echo "split1={$split1}" git branch split1 "$split1" -touch subdir/main-sub5 -git add subdir/main-sub5 -git commit -m 'main-sub5' +create subdir/main-sub8 +git add subdir/main-sub8 +git commit -m 'main-sub8' cd ../subproj git fetch ../mainline split1 git branch split1 FETCH_HEAD git merge FETCH_HEAD -touch sub6 -git add sub6 -git commit -m 'sub6' +create sub9 +git add sub9 +git commit -m 'sub9' cd ../mainline -split2=$(git subtree split --prefix subdir --rejoin) +split2=$(git subtree split --annotate='*' --prefix subdir --rejoin) git branch split2 "$split2" -touch subdir/main-sub7 -git add subdir/main-sub7 -git commit -m 'main-sub7' +create subdir/main-sub10 +git add subdir/main-sub10 +git commit -m 'main-sub10' -split3=$(git subtree split --prefix subdir --rejoin) +split3=$(git subtree split --annotate='*' --prefix subdir --rejoin) git branch split3 "$split3" cd ../subproj From 86de04c6eb9ffcb4ed11f1f18bcc62f868cfb743 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sun, 26 Apr 2009 09:55:59 -0400 Subject: [PATCH 021/103] Typo when searching for existing splits. --- git-subtree.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-subtree.sh b/git-subtree.sh index e54651c336..8b797dfc23 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -145,7 +145,7 @@ find_existing_splits() dir="$1" revs="$2" git log --grep="^git-subtree-dir: $dir\$" \ - --pretty=format:'%s%n%n%b%nEND' "$revs" | + --pretty=format:'%s%n%n%b%nEND' $revs | while read a b junk; do case "$a" in git-subtree-mainline:) main="$b" ;; From 1f73862f3b63bbc9f0a8a8a12dd58e1a39a3355f Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sun, 26 Apr 2009 15:54:42 -0400 Subject: [PATCH 022/103] Clarify why we can't do 'git rev-list' with a path. --- git-subtree.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 8b797dfc23..19ac2ef1c1 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -338,9 +338,9 @@ cmd_split() unrevs="$(find_existing_splits "$dir" "$revs")" fi - # We can't restrict rev-list to only "$dir" here, because that leaves out - # critical information about commit parents. - debug "git rev-list --reverse --parents $revs $unrevs" + # We can't restrict rev-list to only $dir here, because some of our + # parents have the $dir contents the root, and those won't match. + # (and rev-list --follow doesn't seem to solve this) git rev-list --reverse --parents $revs $unrevs | while read rev parents; do debug From 0ad3dd8534215648e381663979ea99db855578b6 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sun, 26 Apr 2009 15:55:56 -0400 Subject: [PATCH 023/103] Add a 'create' helper function in test.sh. --- test.sh | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/test.sh b/test.sh index 85ed7ce549..276f40d1da 100755 --- a/test.sh +++ b/test.sh @@ -1,14 +1,14 @@ #!/bin/bash -x -create() -{ - for d in 1 2 3 4 5 6 7 8 9 10; do - echo "$1" - done >"$1" -} - . shellopts.sh set -e +create() +{ + echo "$1" >"$1" + git add "$1" +} + + rm -rf mainline subproj mkdir mainline subproj @@ -16,25 +16,21 @@ cd subproj git init create sub1 -git add sub1 git commit -m 'sub1' git branch sub1 git branch -m master subproj create sub2 -git add sub2 git commit -m 'sub2' git branch sub2 create sub3 -git add sub3 git commit -m 'sub3' git branch sub3 cd ../mainline git init create main4 -git add main4 git commit -m 'main4' git branch -m master mainline @@ -46,15 +42,12 @@ git subtree add --prefix=subdir FETCH_HEAD git merge -m 'merge -s -ours' -s ours FETCH_HEAD create subdir/main-sub5 -git add subdir/main-sub5 git commit -m 'main-sub5' create main6 -git add main6 git commit -m 'main6 boring' create subdir/main-sub7 -git add subdir/main-sub7 git commit -m 'main-sub7' git fetch ../subproj sub2 @@ -67,7 +60,6 @@ echo "split1={$split1}" git branch split1 "$split1" create subdir/main-sub8 -git add subdir/main-sub8 git commit -m 'main-sub8' cd ../subproj @@ -76,7 +68,6 @@ git branch split1 FETCH_HEAD git merge FETCH_HEAD create sub9 -git add sub9 git commit -m 'sub9' cd ../mainline @@ -84,7 +75,6 @@ split2=$(git subtree split --annotate='*' --prefix subdir --rejoin) git branch split2 "$split2" create subdir/main-sub10 -git add subdir/main-sub10 git commit -m 'main-sub10' split3=$(git subtree split --annotate='*' --prefix subdir --rejoin) From 1490e1546a380814fdd68dc3776023e58da60d48 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sun, 26 Apr 2009 16:28:56 -0400 Subject: [PATCH 024/103] Add some basic assertions to test.sh. --- test.sh | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/test.sh b/test.sh index 276f40d1da..4f2b674e5d 100755 --- a/test.sh +++ b/test.sh @@ -1,4 +1,4 @@ -#!/bin/bash -x +#!/bin/bash . shellopts.sh set -e @@ -8,6 +8,50 @@ create() git add "$1" } +check() +{ + echo + echo "check:" "$@" + if "$@"; then + echo ok + return 0 + else + echo FAILED + exit 1 + fi +} + +check_equal() +{ + echo + echo "check a:" "$1" + echo " b:" "$2" + if [ "$1" = "$2" ]; then + return 0 + else + echo FAILED + exit 1 + fi +} + +fixnl() +{ + t="" + while read x; do + t="$t$x " + done + echo $t +} + +multiline() +{ + while read x; do + set -- $x + for d in "$@"; do + echo "$d" + done + done +} rm -rf mainline subproj mkdir mainline subproj @@ -19,6 +63,7 @@ create sub1 git commit -m 'sub1' git branch sub1 git branch -m master subproj +check true create sub2 git commit -m 'sub2' @@ -86,7 +131,33 @@ git branch split3 FETCH_HEAD git merge FETCH_HEAD git branch subproj-merge-split3 +chkm="main4 main6" +chkms="main-sub10 main-sub5 main-sub7 main-sub8" +chkms_sub=$(echo $chkms | multiline | sed 's,^,subdir/,' | fixnl) +chks="sub1 sub2 sub3 sub9" +chks_sub=$(echo $chks | multiline | sed 's,^,subdir/,' | fixnl) + +# make sure exactly the right set of files ends up in the subproj +subfiles=$(git ls-files | fixnl) +check_equal "$subfiles" "$chkms $chks" + +# make sure the subproj history *only* contains commits that affect the subdir. +allchanges=$(git log --name-only --pretty=format:'' | sort | fixnl) +check_equal "$allchanges" "$chkms $chks" + cd ../mainline git fetch ../subproj subproj-merge-split3 git branch subproj-merge-split3 FETCH_HEAD git subtree pull --prefix=subdir ../subproj subproj-merge-split3 + +# make sure exactly the right set of files ends up in the mainline +mainfiles=$(git ls-files | fixnl) +check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub" + +# make sure each filename changed exactly once in the entire history. +# 'main-sub??' and '/subdir/main-sub??' both change, because those are the +# changes that were split into their own history. And 'subdir/sub??' never +# change, since they were *only* changed in the subtree branch. +allchanges=$(git log --name-only --pretty=format:'' | sort | fixnl) +check_equal "$allchanges" "$chkm $chkms $chks $chkms_sub" + From a046c7b124de17c4f8aa8f1c3ee7fa5745dde30e Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sun, 26 Apr 2009 16:33:38 -0400 Subject: [PATCH 025/103] test.sh tweak --- test.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test.sh b/test.sh index 4f2b674e5d..ef1c70e1e6 100755 --- a/test.sh +++ b/test.sh @@ -100,7 +100,8 @@ git branch sub2 FETCH_HEAD git subtree merge --prefix=subdir FETCH_HEAD git branch pre-split -split1=$(git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --rejoin) +split1=$(git subtree split --annotate='*' \ + --prefix subdir --onto FETCH_HEAD --rejoin) echo "split1={$split1}" git branch split1 "$split1" @@ -161,3 +162,5 @@ check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub" allchanges=$(git log --name-only --pretty=format:'' | sort | fixnl) check_equal "$allchanges" "$chkm $chkms $chks $chkms_sub" +echo +echo 'ok' From a64f3a7286c4e554b38cd3f7dc8c722aab97cf98 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sun, 26 Apr 2009 16:53:57 -0400 Subject: [PATCH 026/103] Trim some extra merge commits that don't need to go into the split tree. ...and update test.sh to test for this. --- git-subtree.sh | 19 ++++++++++++++++--- test.sh | 37 ++++++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 19ac2ef1c1..ffd3e0b865 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -168,6 +168,7 @@ copy_commit() { # We're doing to set some environment vars here, so # do it in a subshell to get rid of them safely later + debug copy_commit "{$1}" "{$2}" "{$3}" git log -1 --pretty=format:'%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%s%n%n%b' "$1" | ( read GIT_AUTHOR_NAME @@ -258,19 +259,31 @@ copy_or_skip() identical= p= + gotparents= for parent in $newparents; do ptree=$(toptree_for_commit $parent) || exit $? + [ -z "$ptree" ] && continue if [ "$ptree" = "$tree" ]; then # an identical parent could be used in place of this rev. identical="$parent" fi - if [ -n "$ptree" ]; then - parentmatch="$parentmatch$parent" + + # sometimes both old parents map to the same newparent; + # eliminate duplicates + is_new=1 + for gp in $gotparents; do + if [ "$gp" = "$parent" ]; then + is_new= + break + fi + done + if [ -n "$is_new" ]; then + gotparents="$gotparents $parent" p="$p -p $parent" fi done - if [ -n "$identical" -a "$parentmatch" = "$identical" ]; then + if [ -n "$identical" -a "$gotparents" = " $identical" ]; then echo $identical else copy_commit $rev $tree "$p" || exit $? diff --git a/test.sh b/test.sh index ef1c70e1e6..44d5da3f20 100755 --- a/test.sh +++ b/test.sh @@ -24,8 +24,8 @@ check() check_equal() { echo - echo "check a:" "$1" - echo " b:" "$2" + echo "check a:" "{$1}" + echo " b:" "{$2}" if [ "$1" = "$2" ]; then return 0 else @@ -100,17 +100,17 @@ git branch sub2 FETCH_HEAD git subtree merge --prefix=subdir FETCH_HEAD git branch pre-split -split1=$(git subtree split --annotate='*' \ +spl1=$(git subtree split --annotate='*' \ --prefix subdir --onto FETCH_HEAD --rejoin) -echo "split1={$split1}" -git branch split1 "$split1" +echo "spl1={$spl1}" +git branch spl1 "$spl1" create subdir/main-sub8 git commit -m 'main-sub8' cd ../subproj -git fetch ../mainline split1 -git branch split1 FETCH_HEAD +git fetch ../mainline spl1 +git branch spl1 FETCH_HEAD git merge FETCH_HEAD create sub9 @@ -123,14 +123,14 @@ git branch split2 "$split2" create subdir/main-sub10 git commit -m 'main-sub10' -split3=$(git subtree split --annotate='*' --prefix subdir --rejoin) -git branch split3 "$split3" +spl3=$(git subtree split --annotate='*' --prefix subdir --rejoin) +git branch spl3 "$spl3" cd ../subproj -git fetch ../mainline split3 -git branch split3 FETCH_HEAD +git fetch ../mainline spl3 +git branch spl3 FETCH_HEAD git merge FETCH_HEAD -git branch subproj-merge-split3 +git branch subproj-merge-spl3 chkm="main4 main6" chkms="main-sub10 main-sub5 main-sub7 main-sub8" @@ -147,9 +147,9 @@ allchanges=$(git log --name-only --pretty=format:'' | sort | fixnl) check_equal "$allchanges" "$chkms $chks" cd ../mainline -git fetch ../subproj subproj-merge-split3 -git branch subproj-merge-split3 FETCH_HEAD -git subtree pull --prefix=subdir ../subproj subproj-merge-split3 +git fetch ../subproj subproj-merge-spl3 +git branch subproj-merge-spl3 FETCH_HEAD +git subtree pull --prefix=subdir ../subproj subproj-merge-spl3 # make sure exactly the right set of files ends up in the mainline mainfiles=$(git ls-files | fixnl) @@ -162,5 +162,12 @@ check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub" allchanges=$(git log --name-only --pretty=format:'' | sort | fixnl) check_equal "$allchanges" "$chkm $chkms $chks $chkms_sub" +# make sure the --rejoin commits never make it into subproj +check_equal "$(git log --pretty=format:'%s' HEAD^2 | grep -i split)" "" + +# make sure no 'git subtree' tagged commits make it into subproj. (They're +# meaningless to subproj since one side of the merge refers to the mainline) +check_equal "$(git log --pretty=format:'%s%n%b' HEAD^2 | grep 'git-subtree.*:')" "" + echo echo 'ok' From 49cf82288aac5f0dcb152e2d75cd340e48d9e760 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sun, 26 Apr 2009 17:07:16 -0400 Subject: [PATCH 027/103] Only copy a commit if it has at least one nonidentical parent. This is a simplification of the previous logic. I don't *think* it'll break anything. Results in far fewer useless merge commmits when playing with gitweb in the git project: git subtree split --prefix=gitweb --annotate='(split) ' 0a8f4f0^^..f2e7330 --onto=1130ef3 ...and it doesn't *seem* to eliminate anything important. --- git-subtree.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/git-subtree.sh b/git-subtree.sh index ffd3e0b865..90e22ad8be 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -258,6 +258,7 @@ copy_or_skip() assert [ -n "$tree" ] identical= + nonidentical= p= gotparents= for parent in $newparents; do @@ -266,6 +267,8 @@ copy_or_skip() if [ "$ptree" = "$tree" ]; then # an identical parent could be used in place of this rev. identical="$parent" + else + nonidentical="$parent" fi # sometimes both old parents map to the same newparent; @@ -283,7 +286,7 @@ copy_or_skip() fi done - if [ -n "$identical" -a "$gotparents" = " $identical" ]; then + if [ -n "$identical" -a -z "$nonidentical" ]; then echo $identical else copy_commit $rev $tree "$p" || exit $? From fa16ab36ad014bcc03acc4313bb0918fb241b54d Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sun, 26 Apr 2009 17:43:53 -0400 Subject: [PATCH 028/103] test.sh: make sure no commit changes more than one file at a time. --- test.sh | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/test.sh b/test.sh index 44d5da3f20..4e00b536d7 100755 --- a/test.sh +++ b/test.sh @@ -169,5 +169,39 @@ check_equal "$(git log --pretty=format:'%s' HEAD^2 | grep -i split)" "" # meaningless to subproj since one side of the merge refers to the mainline) check_equal "$(git log --pretty=format:'%s%n%b' HEAD^2 | grep 'git-subtree.*:')" "" +# make sure no patch changes more than one file. The original set of commits +# changed only one file each. A multi-file change would imply that we pruned +# commits too aggressively. +joincommits() +{ + echo "hello world" + commit= + all= + while read x y; do + echo "{$x}" >&2 + if [ -z "$x" ]; then + continue + elif [ "$x" = "commit:" ]; then + if [ -n "$commit" ]; then + echo "$commit $all" + all= + fi + commit="$y" + else + all="$all $y" + fi + done + echo "$commit $all" +} +x=0 +git log --pretty=format:'commit: %H' | joincommits | +( while read commit a b; do + echo "Verifying commit $commit" + check_equal "$b" "" + x=$(($x + 1)) + done + check_equal $x 23 +) || exit 1 + echo echo 'ok' From 795e730e71e8a068ce0cb0790512dab0f1922369 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sun, 26 Apr 2009 17:44:18 -0400 Subject: [PATCH 029/103] Simplify merges even more aggressively. If any one of the parents is the same as the current one, then clearly the other parent branch isn't important, so throw it away entirely. Can't remember why I didn't do this before, but if I rediscover it, it definitely needs a unit test. --- git-subtree.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-subtree.sh b/git-subtree.sh index 90e22ad8be..e2e47f82ac 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -286,7 +286,7 @@ copy_or_skip() fi done - if [ -n "$identical" -a -z "$nonidentical" ]; then + if [ -n "$identical" ]; then echo $identical else copy_commit $rev $tree "$p" || exit $? From 34a82bda7766f000ef646130ed3f6af58ca23aa2 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sun, 26 Apr 2009 18:05:49 -0400 Subject: [PATCH 030/103] test.sh: oops, never intended to count the raw number of commits. Just needed to make sure the count was non-zero. --- test.sh | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test.sh b/test.sh index 4e00b536d7..38dff7a41a 100755 --- a/test.sh +++ b/test.sh @@ -174,7 +174,6 @@ check_equal "$(git log --pretty=format:'%s%n%b' HEAD^2 | grep 'git-subtree.*:')" # commits too aggressively. joincommits() { - echo "hello world" commit= all= while read x y; do @@ -193,14 +192,14 @@ joincommits() done echo "$commit $all" } -x=0 +x= git log --pretty=format:'commit: %H' | joincommits | ( while read commit a b; do echo "Verifying commit $commit" check_equal "$b" "" - x=$(($x + 1)) + x=1 done - check_equal $x 23 + check_equal "$x" 1 ) || exit 1 echo From 942dce5578c8eb03fdc7f9109c8418d499e931ff Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sun, 26 Apr 2009 18:06:08 -0400 Subject: [PATCH 031/103] debug messages are off by default; use -d to enable. Instead of debug messages, we print a progress counter to stderr. --- git-subtree.sh | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index e2e47f82ac..39c377c173 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -15,6 +15,7 @@ git subtree pull --prefix= -- h,help show the help q quiet +d show debug messages prefix= the name of the subdir to split out options for 'split' annotate= add a prefix to commit message of new commits @@ -27,6 +28,7 @@ eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?) require_work_tree quiet= +debug= command= onto= rejoin= @@ -34,6 +36,13 @@ ignore_joins= annotate= debug() +{ + if [ -n "$debug" ]; then + echo "$@" >&2 + fi +} + +say() { if [ -z "$quiet" ]; then echo "$@" >&2 @@ -57,6 +66,7 @@ while [ $# -gt 0 ]; do shift case "$opt" in -q) quiet=1 ;; + -d) debug=1 ;; --annotate) annotate="$1"; shift ;; --no-annotate) annotate= ;; --prefix) prefix="$1"; shift ;; @@ -357,15 +367,21 @@ cmd_split() # We can't restrict rev-list to only $dir here, because some of our # parents have the $dir contents the root, and those won't match. # (and rev-list --follow doesn't seem to solve this) - git rev-list --reverse --parents $revs $unrevs | + grl='git rev-list --reverse --parents $revs $unrevs' + revmax=$(eval "$grl" | wc -l) + revcount=0 + createcount=0 + eval "$grl" | while read rev parents; do - debug + revcount=$(($revcount + 1)) + say -n "$revcount/$revmax ($createcount) " debug "Processing commit: $rev" exists=$(cache_get $rev) if [ -n "$exists" ]; then debug " prior: $exists" continue fi + createcount=$(($createcount + 1)) debug " parents: $parents" newparents=$(cache_get $parents) debug " newparents: $newparents" From ea28d674423aa580bfdbe24991ffcf796f2dd3dc Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Thu, 30 Apr 2009 21:57:32 -0400 Subject: [PATCH 032/103] Abort if --rejoin fails. Thanks to Eduardo Kienetz for noticing this. --- git-subtree.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-subtree.sh b/git-subtree.sh index 39c377c173..692792b864 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -406,7 +406,7 @@ cmd_split() latest_old=$(cache_get latest_old) git merge -s ours \ -m "$(merge_msg $dir $latest_old $latest_new)" \ - $latest_new >&2 + $latest_new >&2 || exit $? fi echo $latest_new exit 0 From 7b7ba4bb3792572bd6c22c95082e064754de47be Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sun, 24 May 2009 15:28:54 -0400 Subject: [PATCH 033/103] More to-do items based on feedback --- todo | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/todo b/todo index b5b8e257aa..97142fa4e2 100644 --- a/todo +++ b/todo @@ -1,8 +1,20 @@ + + write proper docs (asciidoc format for git compatibility) delete tempdir + --annotate-sometimes: only annotate if the patch also changes files outside the subdir? + 'git subtree rejoin' option to do the same as --rejoin, eg. after a rebase + "-s subtree" should be given an explicit subtree option? - \ No newline at end of file + + --prefix doesn't force the subtree correctly in merge/pull + + add a 'push' subcommand to parallel 'pull' + + add a --squash option so we don't merge histories but can still split + + add to-submodule and from-submodule commands From f96bc79019083cdc41cf7f699979f9b5e250d160 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sat, 30 May 2009 00:47:59 -0400 Subject: [PATCH 034/103] typo in comment --- git-subtree.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-subtree.sh b/git-subtree.sh index 692792b864..825dd1dcad 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -176,7 +176,7 @@ find_existing_splits() copy_commit() { - # We're doing 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 debug copy_commit "{$1}" "{$2}" "{$3}" git log -1 --pretty=format:'%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%s%n%n%b' "$1" | From 43a3951243ebcfb8ef4c47259ecb2d0dfaadbce4 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sat, 30 May 2009 01:05:43 -0400 Subject: [PATCH 035/103] New --branch option to split command. This is just a handy way to create a new branch from the newly-split subtree. --- git-subtree.sh | 26 ++++++++++++++++++++++++-- todo | 10 +++++----- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 825dd1dcad..f6bdef3001 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -19,15 +19,17 @@ d show debug messages prefix= the name of the subdir to split out options for 'split' annotate= add a prefix to commit message of new commits +b,branch= create a new branch from the split subtree +ignore-joins ignore prior --rejoin commits onto= try connecting new tree to an existing one rejoin merge the new branch back into HEAD -ignore-joins ignore prior --rejoin commits " eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?) . git-sh-setup require_work_tree quiet= +branch= debug= command= onto= @@ -69,6 +71,7 @@ while [ $# -gt 0 ]; do -d) debug=1 ;; --annotate) annotate="$1"; shift ;; --no-annotate) annotate= ;; + -b) branch="$1"; shift ;; --prefix) prefix="$1"; shift ;; --no-prefix) prefix= ;; --onto) onto="$1"; shift ;; @@ -78,6 +81,7 @@ while [ $# -gt 0 ]; do --ignore-joins) ignore_joins=1 ;; --no-ignore-joins) ignore_joins= ;; --) break ;; + *) die "Unexpected option: $opt" ;; esac done @@ -139,12 +143,21 @@ cache_set() echo "$newrev" >"$cachedir/$oldrev" } +rev_exists() +{ + if git rev-parse "$1" >/dev/null 2>&1; then + return 0 + else + return 1 + fi +} + # if a commit doesn't have a parent, this might not work. But we only want # to remove the parent from the rev-list, and since it doesn't exist, it won't # be there anyway, so do nothing in that case. try_remove_previous() { - if git rev-parse "$1^" >/dev/null 2>&1; then + if rev_exists "$1^"; then echo "^$1^" fi } @@ -344,6 +357,10 @@ cmd_add() cmd_split() { + if [ -n "$branch" ] && rev_exists "refs/heads/$branch"; then + die "Branch '$branch' already exists." + fi + debug "Splitting $dir..." cache_setup || exit $? @@ -408,6 +425,11 @@ cmd_split() -m "$(merge_msg $dir $latest_old $latest_new)" \ $latest_new >&2 || exit $? fi + if [ -n "$branch" ]; then + git update-ref -m 'subtree split' "refs/heads/$branch" \ + $latest_new "" || exit $? + say "Created branch '$branch'" + fi echo $latest_new exit 0 } diff --git a/todo b/todo index 97142fa4e2..f23a6d4ff2 100644 --- a/todo +++ b/todo @@ -3,17 +3,17 @@ delete tempdir - --annotate-sometimes: only annotate if the patch also changes files - outside the subdir? - 'git subtree rejoin' option to do the same as --rejoin, eg. after a rebase + --prefix doesn't force the subtree correctly in merge/pull: "-s subtree" should be given an explicit subtree option? - - --prefix doesn't force the subtree correctly in merge/pull + There doesn't seem to be a way to do this. We'd have to + patch git-merge-subtree. Ugh. add a 'push' subcommand to parallel 'pull' + + add a 'log' subcommand to see what's new in a subtree? add a --squash option so we don't merge histories but can still split From f4f29557e7e02c318931395c1cd4fd1ba090fdfd Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sat, 30 May 2009 01:10:14 -0400 Subject: [PATCH 036/103] slightly rearrange help message for split. --- git-subtree.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index f6bdef3001..ea0294fb79 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -8,10 +8,10 @@ if [ $# -eq 0 ]; then set -- -h fi OPTS_SPEC="\ -git subtree add --prefix= -git subtree split [options...] --prefix= +git subtree add --prefix= git subtree merge --prefix= git subtree pull --prefix= +git subtree split --prefix= -- h,help show the help q quiet From 8e79043c47d69032a86e5587a69289304dd5ca23 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sat, 30 May 2009 00:48:07 -0400 Subject: [PATCH 037/103] FIXME help for --squash option --- git-subtree.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/git-subtree.sh b/git-subtree.sh index ea0294fb79..65b6348fe4 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -23,6 +23,8 @@ b,branch= create a new branch from the split subtree ignore-joins ignore prior --rejoin commits onto= try connecting new tree to an existing one rejoin merge the new branch back into HEAD + options for 'merge' and 'pull' +squash merge subtree changes as a single commit " eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?) . git-sh-setup @@ -36,6 +38,7 @@ onto= rejoin= ignore_joins= annotate= +squash= debug() { @@ -80,6 +83,8 @@ while [ $# -gt 0 ]; do --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 From 7ee9eef340170da6d13b9a0ab5a8d23950523b0d Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sat, 30 May 2009 01:28:20 -0400 Subject: [PATCH 038/103] merge_msg() is really more like rejoin_msg(). --- git-subtree.sh | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 65b6348fe4..d82e03e6fd 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -178,15 +178,15 @@ find_existing_splits() case "$a" in git-subtree-mainline:) main="$b" ;; git-subtree-split:) sub="$b" ;; - *) + END) if [ -n "$main" -a -n "$sub" ]; then debug " Prior: $main -> $sub" cache_set $main $sub try_remove_previous "$main" try_remove_previous "$sub" - main= - sub= fi + main= + sub= ;; esac done @@ -230,7 +230,7 @@ add_msg() EOF } -merge_msg() +rejoin_msg() { dir="$1" latest_old="$2" @@ -410,6 +410,9 @@ cmd_split() tree=$(subtree_for_commit $rev "$dir") debug " tree is: $tree" + + # ugly. is there no better way to tell if this is a subtree + # vs. a mainline commit? Does it matter? [ -z $tree ] && continue newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $? @@ -427,7 +430,7 @@ cmd_split() debug "Merging split branch into HEAD..." latest_old=$(cache_get latest_old) git merge -s ours \ - -m "$(merge_msg $dir $latest_old $latest_new)" \ + -m "$(rejoin_msg $dir $latest_old $latest_new)" \ $latest_new >&2 || exit $? fi if [ -n "$branch" ]; then From 1cc2cfff91c61bb56236914da7be7b15584951df Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sat, 30 May 2009 03:18:27 -0400 Subject: [PATCH 039/103] Basic "subtree merge --squash" support. Instead of merging in the history of the entire subproject, just squash it all into one commit, but try to at least track which commits we used so that we can do future merges correctly. Bonus feature: we can actually switch branches of the subproject this way, just by "squash merging" back and forth from one tag to another. --- git-subtree.sh | 78 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/git-subtree.sh b/git-subtree.sh index d82e03e6fd..863e28bb74 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -167,15 +167,46 @@ try_remove_previous() fi } +find_latest_squash() +{ + debug "Looking for latest squash..." + dir="$1" + git log --grep="^git-subtree-dir: $dir\$" \ + --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD | + while read a b junk; do + case "$a" in + START) sq="$b" ;; + git-subtree-mainline:) main="$b" ;; + git-subtree-split:) sub="$b" ;; + END) + if [ -n "$sub" ]; then + if [ -n "$main" ]; then + # a rejoin commit? + # Pretend its sub was a squash. + sq="$sub" + fi + debug "Squash found: $sq $sub" + echo "$sq" "$sub" + break + fi + sq= + main= + sub= + ;; + esac + done +} + find_existing_splits() { debug "Looking for prior splits..." dir="$1" revs="$2" git log --grep="^git-subtree-dir: $dir\$" \ - --pretty=format:'%s%n%n%b%nEND' $revs | + --pretty=format:'%s%n%n%b%nEND%n' $revs | while read a b junk; do case "$a" in + START) main="$b" ;; git-subtree-mainline:) main="$b" ;; git-subtree-split:) sub="$b" ;; END) @@ -244,6 +275,28 @@ rejoin_msg() EOF } +squash_msg() +{ + dir="$1" + oldsub="$2" + newsub="$3" + oldsub_short=$(git rev-parse --short "$oldsub") + newsub_short=$(git rev-parse --short "$newsub") + cat <<-EOF + Squashed '$dir/' changes from $oldsub_short..$newsub_short + + EOF + + git log --pretty=tformat:'%h %s' "$oldsub..$newsub" + git log --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub" + + cat <<-EOF + + git-subtree-dir: $dir + git-subtree-split: $newsub + EOF +} + toptree_for_commit() { commit="$1" @@ -278,6 +331,16 @@ tree_changed() fi } +new_squash_commit() +{ + old="$1" + oldsub="$2" + newsub="$3" + tree=$(toptree_for_commit $newsub) || exit $? + squash_msg "$dir" "$oldsub" "$newsub" | + git commit-tree "$tree" -p "$old" || exit $? +} + copy_or_skip() { rev="$1" @@ -452,6 +515,19 @@ cmd_merge() fi rev="$1" + if [ -n "$squash" ]; then + first_split="$(find_latest_squash "$dir")" + if [ -z "$first_split" ]; then + die "Can't squash-merge: '$dir' was never added." + fi + set $first_split + old=$1 + sub=$2 + new=$(new_squash_commit "$old" "$sub" "$rev") || exit $? + debug "New squash commit: $new" + rev="$new" + fi + git merge -s subtree $rev } From eb4fb91094cf5e20bf7570da3fadb50e444284bc Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sat, 30 May 2009 03:33:17 -0400 Subject: [PATCH 040/103] Don't squash-merge if the old and new commits are the same. --- git-subtree.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/git-subtree.sh b/git-subtree.sh index 863e28bb74..f7fe111178 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -523,6 +523,10 @@ cmd_merge() set $first_split old=$1 sub=$2 + if [ "$sub" = "$rev" ]; then + say "Subtree is already at commit $rev." + exit 0 + fi new=$(new_squash_commit "$old" "$sub" "$rev") || exit $? debug "New squash commit: $new" rev="$new" From 1a8c36dc5fdd8c439c65f18a91ad211050201fc8 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sat, 30 May 2009 03:33:39 -0400 Subject: [PATCH 041/103] Fix splitting after using a squash merge. --- git-subtree.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index f7fe111178..1fff10e854 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -203,13 +203,17 @@ find_existing_splits() dir="$1" revs="$2" git log --grep="^git-subtree-dir: $dir\$" \ - --pretty=format:'%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 case "$a" in - START) main="$b" ;; + START) main="$b"; sq="$b" ;; git-subtree-mainline:) main="$b" ;; git-subtree-split:) sub="$b" ;; END) + if [ -z "$main" -a -n "$sub" ]; then + # squash commits refer to a subtree + cache_set "$sq" "$sub" + fi if [ -n "$main" -a -n "$sub" ]; then debug " Prior: $main -> $sub" cache_set $main $sub From d713e2d87a5003da02d95a4ac5be28a1e9fdc3ce Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sat, 30 May 2009 04:11:43 -0400 Subject: [PATCH 042/103] Make --squash work with the 'add' command too. --- git-subtree.sh | 57 ++++++++++++++++++++++++++++++++++---------------- todo | 7 +++++-- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 1fff10e854..962d5ff509 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -23,7 +23,7 @@ b,branch= create a new branch from the split subtree ignore-joins ignore prior --rejoin commits onto= try connecting new tree to an existing one rejoin merge the new branch back into HEAD - options for 'merge' and 'pull' + options for 'add', 'merge', and 'pull' squash merge subtree changes as a single commit " eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?) @@ -169,11 +169,16 @@ try_remove_previous() find_latest_squash() { - debug "Looking for latest squash..." + debug "Looking for latest squash ($dir)..." dir="$1" + sq= + main= + sub= git log --grep="^git-subtree-dir: $dir\$" \ --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD | while read a b junk; do + debug "$a $b $junk" + debug "{{$sq/$main/$sub}}" case "$a" in START) sq="$b" ;; git-subtree-mainline:) main="$b" ;; @@ -202,6 +207,8 @@ find_existing_splits() debug "Looking for prior splits..." dir="$1" revs="$2" + main= + sub= git log --grep="^git-subtree-dir: $dir\$" \ --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs | while read a b junk; do @@ -284,21 +291,21 @@ squash_msg() dir="$1" oldsub="$2" newsub="$3" - oldsub_short=$(git rev-parse --short "$oldsub") newsub_short=$(git rev-parse --short "$newsub") - cat <<-EOF - Squashed '$dir/' changes from $oldsub_short..$newsub_short - EOF + if [ -n "$oldsub" ]; then + oldsub_short=$(git rev-parse --short "$oldsub") + echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short" + echo + git log --pretty=tformat:'%h %s' "$oldsub..$newsub" + git log --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub" + else + echo "Squashed '$dir/' content from commit $newsub_short" + fi - git log --pretty=tformat:'%h %s' "$oldsub..$newsub" - git log --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub" - - cat <<-EOF - - git-subtree-dir: $dir - git-subtree-split: $newsub - EOF + echo + echo "git-subtree-dir: $dir" + echo "git-subtree-split: $newsub" } toptree_for_commit() @@ -341,8 +348,13 @@ new_squash_commit() oldsub="$2" newsub="$3" tree=$(toptree_for_commit $newsub) || exit $? - squash_msg "$dir" "$oldsub" "$newsub" | - git commit-tree "$tree" -p "$old" || exit $? + if [ -n "$old" ]; then + squash_msg "$dir" "$oldsub" "$newsub" | + git commit-tree "$tree" -p "$old" || exit $? + else + squash_msg "$dir" "" "$newsub" | + git commit-tree "$tree" || exit $? + fi } copy_or_skip() @@ -422,9 +434,18 @@ cmd_add() else headp= fi - commit=$(add_msg "$dir" "$headrev" "$rev" | - git commit-tree $tree $headp -p "$rev") || exit $? + + if [ -n "$squash" ]; then + rev=$(new_squash_commit "" "" "$rev") || exit $? + commit=$(echo "Merge commit '$rev' as '$dir'" | + git commit-tree $tree $headp -p "$rev") || exit $? + else + commit=$(add_msg "$dir" "$headrev" "$rev" | + git commit-tree $tree $headp -p "$rev") || exit $? + fi git reset "$commit" || exit $? + + say "Added dir '$dir'" } cmd_split() diff --git a/todo b/todo index f23a6d4ff2..a15a378da5 100644 --- a/todo +++ b/todo @@ -10,11 +10,14 @@ "-s subtree" should be given an explicit subtree option? There doesn't seem to be a way to do this. We'd have to patch git-merge-subtree. Ugh. + (but we could avoid this problem by generating squashes with + exactly the right subtree structure, rather than using + subtree merge...) add a 'push' subcommand to parallel 'pull' add a 'log' subcommand to see what's new in a subtree? - add a --squash option so we don't merge histories but can still split - add to-submodule and from-submodule commands + + automated tests for --squash stuff From e75d1da38a7091c15ebd3c80539e4aab20faf5b7 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sat, 30 May 2009 14:05:33 -0400 Subject: [PATCH 043/103] Add basic git-subtree manpage in asciidoc format. --- .gitignore | 2 + Makefile | 18 ++++ asciidoc.conf | 91 ++++++++++++++++++ git-subtree.txt | 233 +++++++++++++++++++++++++++++++++++++++++++++ manpage-base.xsl | 35 +++++++ manpage-normal.xsl | 13 +++ 6 files changed, 392 insertions(+) create mode 100644 Makefile create mode 100644 asciidoc.conf create mode 100644 git-subtree.txt create mode 100644 manpage-base.xsl create mode 100644 manpage-normal.xsl diff --git a/.gitignore b/.gitignore index b25c15b81f..e358b18b78 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ *~ +git-subtree.xml +git-subtree.1 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000..bc163dd390 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +default: + @echo "git-subtree doesn't need to be built." + @echo + @echo "Try: make doc" + @false + +doc: git-subtree.1 + +%.1: %.xml + xmlto -m manpage-normal.xsl man $^ + +%.xml: %.txt + asciidoc -b docbook -d manpage -f asciidoc.conf \ + -agit_version=1.6.3 $^ + +clean: + rm -f *~ *.xml *.html *.1 + rm -rf subproj mainline diff --git a/asciidoc.conf b/asciidoc.conf new file mode 100644 index 0000000000..dc76e7f073 --- /dev/null +++ b/asciidoc.conf @@ -0,0 +1,91 @@ +## linkgit: macro +# +# Usage: linkgit:command[manpage-section] +# +# Note, {0} is the manpage section, while {target} is the command. +# +# Show GIT link as: (
); if section is defined, else just show +# the command. + +[macros] +(?su)[\\]?(?Plinkgit):(?P\S*?)\[(?P.*?)\]= + +[attributes] +asterisk=* +plus=+ +caret=^ +startsb=[ +endsb=] +tilde=~ + +ifdef::backend-docbook[] +[linkgit-inlinemacro] +{0%{target}} +{0#} +{0#{target}{0}} +{0#} +endif::backend-docbook[] + +ifdef::backend-docbook[] +ifndef::git-asciidoc-no-roff[] +# "unbreak" docbook-xsl v1.68 for manpages. v1.69 works with or without this. +# v1.72 breaks with this because it replaces dots not in roff requests. +[listingblock] +{title} + +ifdef::doctype-manpage[] + .ft C +endif::doctype-manpage[] +| +ifdef::doctype-manpage[] + .ft +endif::doctype-manpage[] + +{title#} +endif::git-asciidoc-no-roff[] + +ifdef::git-asciidoc-no-roff[] +ifdef::doctype-manpage[] +# The following two small workarounds insert a simple paragraph after screen +[listingblock] +{title} + +| + +{title#} + +[verseblock] +{title} +{title%} +{title#} +| + +{title#} +{title%} +endif::doctype-manpage[] +endif::git-asciidoc-no-roff[] +endif::backend-docbook[] + +ifdef::doctype-manpage[] +ifdef::backend-docbook[] +[header] +template::[header-declarations] + + +{mantitle} +{manvolnum} +Git +{git_version} +Git Manual + + + {manname} + {manpurpose} + +endif::backend-docbook[] +endif::doctype-manpage[] + +ifdef::backend-xhtml11[] +[linkgit-inlinemacro] +{target}{0?({0})} +endif::backend-xhtml11[] diff --git a/git-subtree.txt b/git-subtree.txt new file mode 100644 index 0000000000..d10630180d --- /dev/null +++ b/git-subtree.txt @@ -0,0 +1,233 @@ +git-subtree(1) +============== + +NAME +---- +git-subtree - add, merge, and split subprojects stored in subtrees + + +SYNOPSIS +-------- +[verse] +'git subtree' add --prefix= +'git subtree' merge --prefix= +'git subtree' pull --prefix= +'git subtree' split --prefix= + + +DESCRIPTION +----------- +git subtree allows you to include an subproject in your +own repository as a subdirectory, optionally including the +subproject's entire history. For example, you could +include the source code for a library as a subdirectory of your +application. + +You can also extract the entire history of a subdirectory from +your project and make it into a standalone project. For +example, if a library you made for one application ends up being +useful elsewhere, you can extract its entire history and publish +that as its own git repository, without accidentally +intermingling the history of your application project. + +Most importantly, you can alternate back and forth between these +two operations. If the standalone library gets updated, you can +automatically merge the changes into your project; if you +update the library inside your project, you can "split" the +changes back out again and merge them back into the library +project. + +Unlike the 'git submodule' command, git subtree doesn't produce +any special constructions (like .gitmodule files or gitlinks) in +your repository, and doesn't require end-users of your +repository to do anything special or to understand how subtrees +work. A subtree is just another subdirectory and can be +committed to, branched, and merged along with your project in +any way you want. + +In order to keep your commit messages clean, we recommend that +people split their commits between the subtrees and the main +project as much as possible. That is, if you make a change that +affects both the library and the main application, commit it in +two pieces. That way, when you split the library commits out +later, their descriptions will still make sense. But if this +isn't important to you, it's not *necessary*. git subtree will +simply leave out the non-library-related parts of the commit +when it splits it out into the subproject later. + + +COMMANDS +-------- +add:: + Create the subtree by importing its contents + from the given commit. A new commit is created + automatically, joining the imported project's history + with your own. With '--squash', imports only a single + commit from the subproject, rather than its entire + history. + +merge:: + Merge recent changes up to into the + subtree. As with normal 'git merge', this doesn't + remove your own local changes; it just merges those + changes into the latest . With '--squash', + creates only one commit that contains all the changes, + rather than merging in the entire history. + + If you use '--squash', the merge direction doesn't + always have to be forward; you can use this command to + go back in time from v2.5 to v2.4, for example. If your + merge introduces a conflict, you can resolve it in the + usual ways. + +pull:: + Exactly like 'merge', but parallels 'git pull' in that + it fetches the given commit from the specified remote + repository. + +split:: + Extract a new, synthetic project history from the + history of the subtree. The new history + includes only the commits (including merges) that + affected , and each of those commits now has the + contents of at the root of the project instead + of in a subdirectory. Thus, the newly created history + is suitable for export as a separate git repository. + + After splitting successfully, a single commit id is + printed to stdout. This corresponds to the HEAD of the + newly created tree, which you can manipulate however you + want. + + Repeated splits of exactly the same history are + guaranteed to be identical (ie. to produce the same + commit ids). Because of this, if you add new commits + and then re-split, the new commits will be attached as + commits on top of the history you generated last time, + so 'git merge' and friends will work as expected. + + Note that if you use '--squash' when you merge, you + should usually not just '--rejoin' when you split. + + +OPTIONS +------- +-q:: +--quiet:: + Suppress unnecessary output messages on stderr. + +-d:: +--debug:: + Produce even more unnecessary output messages on stderr. + +--prefix=:: + Specify the path in the repository to the subtree you + want to manipulate. This option is currently mandatory + for all commands. + + +OPTIONS FOR add, merge, AND pull +-------------------------------- +--squash:: + Instead of merging the entire history from the subtree + project, produce only a single commit that contains all + the differences you want to merge, and then merge that + new commit into your project. + + Using this option helps to reduce log clutter. People + rarely want to see every change that happened between + v1.0 and v1.1 of the library they're using, since none of the + interim versions were ever included in their application. + + Using '--squash' also helps avoid problems when the same + subproject is included multiple times in the same + project, or is removed and then re-added. In such a + case, it doesn't make sense to combine the histories + anyway, since it's unclear which part of the history + belongs to which subtree. + + Furthermore, with '--squash', you can switch back and + forth between different versions of a subtree, rather + than strictly forward. 'git subtree merge --squash' + always adjusts the subtree to match the exactly + specified commit, even if getting to that commit would + require undoing some changes that were added earlier. + + Whether or not you use '--squash', changes made in your + local repository remain intact and can be later split + and send upstream to the subproject. + + +OPTIONS FOR split +----------------- +--annotate=:: + When generating synthetic history, add as a + prefix to each commit message. Since we're creating new + commits with the same commit message, but possibly + different content, from the original commits, this can help + to differentiate them and avoid confusion. + + Whenever you split, you need to use the same + , or else you don't have a guarantee that + the new re-created history will be identical to the old + one. That will prevent merging from working correctly. + git subtree tries to make it work anyway, particularly + if you use --rejoin, but it may not always be effective. + +-b :: +--branch=:: + After generating the synthetic history, create a new + branch called that contains the new history. + This is suitable for immediate pushing upstream. + must not already exist. + +--ignore-joins:: + If you use '--rejoin', git subtree attempts to optimize + its history reconstruction to generate only the new + commits since the last '--rejoin'. '--ignore-join' + disables this behaviour, forcing it to regenerate the + entire history. In a large project, this can take a + long time. + +--onto=:: + If your subtree was originally imported using something + other than git subtree, its history may not match what + git subtree is expecting. In that case, you can specify + the commit id that corresponds to the first + revision of the subproject's history that was imported + into your project, and git subtree will attempt to build + its history from there. + + If you used 'git subtree add', you should never need + this option. + +--rejoin:: + After splitting, merge the newly created synthetic + history back into your main project. That way, future + splits can search only the part of history that has + been added since the most recent --rejoin. + + If your split commits end up merged into the upstream + subproject, and then you want to get the latest upstream + version, this will allow git's merge algorithm to more + intelligently avoid conflicts (since it knows these + synthetic commits are already part of the upstream + repository). + + Unfortunately, using this option results in 'git log' + showing an extra copy of every new commit that was + created (the original, and the synthetic one). + + If you do all your merges with '--squash', don't use + '--rejoin' when you split, because you don't want the + subproject's history to be part of your project anyway. + + +AUTHOR +------ +Written by Avery Pennarun + + +GIT +--- +Part of the linkgit:git[1] suite diff --git a/manpage-base.xsl b/manpage-base.xsl new file mode 100644 index 0000000000..a264fa6160 --- /dev/null +++ b/manpage-base.xsl @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + sp + + + + + + + + br + + + diff --git a/manpage-normal.xsl b/manpage-normal.xsl new file mode 100644 index 0000000000..a48f5b11f3 --- /dev/null +++ b/manpage-normal.xsl @@ -0,0 +1,13 @@ + + + + + + +\ +. + + From dd07906252a3983fdec396ea6c58de12548713e1 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sat, 30 May 2009 14:24:31 -0400 Subject: [PATCH 044/103] man page: add an EXAMPLES section. --- git-subtree.txt | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/git-subtree.txt b/git-subtree.txt index d10630180d..649cc30989 100644 --- a/git-subtree.txt +++ b/git-subtree.txt @@ -223,6 +223,68 @@ OPTIONS FOR split subproject's history to be part of your project anyway. +EXAMPLES +-------- +Let's use the repository for the git source code as an example. +First, get your own copy of the git.git repository: + + $ git clone git://git.kernel.org/pub/scm/git/git.git test-git + $ cd test-git + +gitweb (commit 1130ef3) was merged into git as of commit +0a8f4f0, after which it was no longer maintained separately. +But imagine it had been maintained separately, and we wanted to +extract git's changes to gitweb since that time, to share with +the upstream. You could do this: + + $ git subtree split --prefix=gitweb --annotate='(split) ' \ + 0a8f4f0^.. --onto=1130ef3 --rejoin \ + --branch gitweb-latest + $ gitk gitweb-latest + $ git push git@github.com:whatever/gitweb gitweb-latest:master + +(We use '0a8f4f0^..' because that means "all the changes from +0a8f4f0 to the current version, including 0a8f4f0 itself.") + +If gitweb had originally been merged using 'git subtree add' (or +a previous split had already been done with --rejoin specified) +then you can do all your splits without having to remember any +weird commit ids: + + $ git subtree split --prefix=gitweb --annotate='(split) ' --rejoin \ + --branch gitweb-latest2 + +And you can merge changes back in from the upstream project just +as easily: + + $ git subtree pull --prefix=gitweb \ + git@github.com:whatever/gitweb gitweb-latest:master + +Or, using '--squash', you can actually rewind to an earlier +version of gitweb: + + $ git subtree merge --prefix=gitweb --squash gitweb-latest~10 + +Then make some changes: + + $ date >gitweb/myfile + $ git add gitweb/myfile + $ git commit -m 'created myfile' + +And fast forward again: + + $ git subtree merge --prefix=gitweb --squash gitweb-latest + +And notice that your change is still intact: + + $ ls -l gitweb/myfile + +And you can split it out and look at your changes versus +the standard gitweb: + + git log gitweb-latest..$(git subtree split --prefix=gitweb) + + AUTHOR ------ Written by Avery Pennarun From c8a98d4f8730d85a6f693d5dbcd4c1309d97f7c8 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Mon, 15 Jun 2009 14:12:40 -0400 Subject: [PATCH 045/103] update todo --- todo | 3 +++ 1 file changed, 3 insertions(+) diff --git a/todo b/todo index a15a378da5..88a4359916 100644 --- a/todo +++ b/todo @@ -21,3 +21,6 @@ add to-submodule and from-submodule commands automated tests for --squash stuff + + test.sh fails in msysgit? + sort error - see Thell's email From 6aa76263eed68ffed7405687f407e66deec30b02 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Thu, 2 Jul 2009 12:39:48 -0400 Subject: [PATCH 046/103] Some todo items reported by pmccurdy --- todo | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/todo b/todo index 88a4359916..1a9d64408d 100644 --- a/todo +++ b/todo @@ -1,5 +1,3 @@ - - write proper docs (asciidoc format for git compatibility) delete tempdir @@ -24,3 +22,24 @@ test.sh fails in msysgit? sort error - see Thell's email + + "add" command non-obviously requires a commitid; would be easier if + it had a "pull" sort of mode instead + + "pull" and "merge" commands should fail if you've never merged + that --prefix before + + docs should provide an example of "add" + + note that the initial split doesn't *have* to have a commitid + specified... that's just an optimization + + if you try to add (or maybe merge?) with an invalid commitid, you + get a misleading "prefix must end with /" message from + one of the other git tools that git-subtree calls. Should + detect this situation and print the *real* problem. + + In fact, the prefix should *not* end with slash, and we + should detect (and fix) it if it does. Otherwise the + log message looks weird. + From 76a7356f56655dbcbbc9d472e0cdc85c6ed3759f Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Tue, 7 Jul 2009 17:41:38 -0400 Subject: [PATCH 047/103] todo --- todo | 2 ++ 1 file changed, 2 insertions(+) diff --git a/todo b/todo index 1a9d64408d..01d552979a 100644 --- a/todo +++ b/todo @@ -43,3 +43,5 @@ should detect (and fix) it if it does. Otherwise the log message looks weird. + totally weird behavior in 'git subtree add' if --prefix matches + a branch name From b64a7aa26c72fcae7b6e1decd88ed706c185cda7 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Wed, 8 Jul 2009 20:17:31 -0400 Subject: [PATCH 048/103] Docs: when pushing to github, the repo path needs to end in .git Reported by Thell Fowler. --- git-subtree.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/git-subtree.txt b/git-subtree.txt index 649cc30989..e7ce2d3654 100644 --- a/git-subtree.txt +++ b/git-subtree.txt @@ -241,7 +241,7 @@ the upstream. You could do this: 0a8f4f0^.. --onto=1130ef3 --rejoin \ --branch gitweb-latest $ gitk gitweb-latest - $ git push git@github.com:whatever/gitweb gitweb-latest:master + $ git push git@github.com:whatever/gitweb.git gitweb-latest:master (We use '0a8f4f0^..' because that means "all the changes from 0a8f4f0 to the current version, including 0a8f4f0 itself.") @@ -258,7 +258,7 @@ And you can merge changes back in from the upstream project just as easily: $ git subtree pull --prefix=gitweb \ - git@github.com:whatever/gitweb gitweb-latest:master + git@github.com:whatever/gitweb.git gitweb-latest:master Or, using '--squash', you can actually rewind to an earlier version of gitweb: From 5d1a5da411a8ec90cd7a7819bfc74440e3f2545c Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 10 Jul 2009 15:14:33 -0400 Subject: [PATCH 049/103] todo --- todo | 3 +++ 1 file changed, 3 insertions(+) diff --git a/todo b/todo index 01d552979a..e67f713ad4 100644 --- a/todo +++ b/todo @@ -45,3 +45,6 @@ totally weird behavior in 'git subtree add' if --prefix matches a branch name + + "pull --squash" should do fetch-synthesize-merge, but instead just + does "pull" directly, which doesn't work at all. From 344f58abe599cba53a056c0a707c9a99e2cd13a8 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Thu, 16 Jul 2009 14:31:50 -0400 Subject: [PATCH 050/103] todo^ --- todo | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/todo b/todo index e67f713ad4..155c4be155 100644 --- a/todo +++ b/todo @@ -48,3 +48,8 @@ "pull --squash" should do fetch-synthesize-merge, but instead just does "pull" directly, which doesn't work at all. + + make a 'force-update' that does what 'add' does even if the subtree + already exists. That way we can help people who imported + subtrees "incorrectly" (eg. by just copying in the files) in + the past. From 0af6aa46751aa6c1ec393155b938601ce0b5f7ae Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sat, 1 Aug 2009 01:19:48 -0400 Subject: [PATCH 051/103] todo --- todo | 2 ++ 1 file changed, 2 insertions(+) diff --git a/todo b/todo index 155c4be155..b59713064b 100644 --- a/todo +++ b/todo @@ -53,3 +53,5 @@ already exists. That way we can help people who imported subtrees "incorrectly" (eg. by just copying in the files) in the past. + + guess --prefix automatically if possible based on pwd From ef7596677c181c649436fe97356dd31c1cb4a13e Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sun, 2 Aug 2009 17:48:20 -0400 Subject: [PATCH 052/103] todo: idea for a 'git subtree grafts' command --- todo | 3 +++ 1 file changed, 3 insertions(+) diff --git a/todo b/todo index b59713064b..3040b9f171 100644 --- a/todo +++ b/todo @@ -55,3 +55,6 @@ the past. guess --prefix automatically if possible based on pwd + + make a 'git subtree grafts' that automatically expands --squash'd + commits so you can see the full history if you want it. From e1a5b9d3e708d6be1a4c5220dc492da9f2694411 Mon Sep 17 00:00:00 2001 From: Amiel Martin Date: Wed, 12 Aug 2009 15:24:50 -0700 Subject: [PATCH 053/103] fixed order of assertion in tests --- test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.sh b/test.sh index 38dff7a41a..4229f840ac 100755 --- a/test.sh +++ b/test.sh @@ -160,7 +160,7 @@ check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub" # changes that were split into their own history. And 'subdir/sub??' never # change, since they were *only* changed in the subtree branch. allchanges=$(git log --name-only --pretty=format:'' | sort | fixnl) -check_equal "$allchanges" "$chkm $chkms $chks $chkms_sub" +check_equal "$allchanges" "$chkms $chkm $chks $chkms_sub" # make sure the --rejoin commits never make it into subproj check_equal "$(git log --pretty=format:'%s' HEAD^2 | grep -i split)" "" From 558e7a57e20fe68aee05f74f8005b7a39795ac15 Mon Sep 17 00:00:00 2001 From: Amiel Martin Date: Wed, 12 Aug 2009 15:38:00 -0700 Subject: [PATCH 054/103] sort assertion to make it more generic --- test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.sh b/test.sh index 4229f840ac..8283fadaad 100755 --- a/test.sh +++ b/test.sh @@ -160,7 +160,7 @@ check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub" # changes that were split into their own history. And 'subdir/sub??' never # change, since they were *only* changed in the subtree branch. allchanges=$(git log --name-only --pretty=format:'' | sort | fixnl) -check_equal "$allchanges" "$chkms $chkm $chks $chkms_sub" +check_equal "$allchanges" "$(echo $chkms $chkm $chks $chkms_sub | multiline | sort | fixnl)" # make sure the --rejoin commits never make it into subproj check_equal "$(git log --pretty=format:'%s' HEAD^2 | grep -i split)" "" From 2987e6add32f3367be8cc196ecac9195a213e415 Mon Sep 17 00:00:00 2001 From: kTln2 Date: Thu, 20 Aug 2009 13:30:45 +0200 Subject: [PATCH 055/103] Add explicit path of git installation by 'git --exec-path'. As pointed out by documentation, the correct use of 'git-sh-setup' is using $(git --exec-path) to avoid problems with not standard installations. Signed-off-by: gianluca.pacchiella --- git-subtree.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-subtree.sh b/git-subtree.sh index 962d5ff509..c5c0201448 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -27,7 +27,7 @@ rejoin merge the new branch back into HEAD squash merge subtree changes as a single commit " eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?) -. git-sh-setup +. $(git --exec-path)/git-sh-setup require_work_tree quiet= From 33aaa697a2386a02002f9fb8439d11243f12e1c7 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Wed, 26 Aug 2009 10:41:03 -0400 Subject: [PATCH 056/103] Improve patch to use git --exec-path: add to PATH instead. If you (like me) are using a modified git straight out of its source directory (ie. without installing), then --exec-path isn't actually correct. Add it to the PATH instead, so if it is correct, it'll work, but if it's not, we fall back to the previous behaviour. --- git-subtree.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/git-subtree.sh b/git-subtree.sh index c5c0201448..f7d2fe408d 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -27,7 +27,8 @@ rejoin merge the new branch back into HEAD squash merge subtree changes as a single commit " eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?) -. $(git --exec-path)/git-sh-setup +PATH=$(git --exec-path):$PATH +. git-sh-setup require_work_tree quiet= From 227f78114752eee2e8ae3368089716d73d32dd8b Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Wed, 26 Aug 2009 10:43:43 -0400 Subject: [PATCH 057/103] Fix behaviour if you have a branch named the same as your --prefix We were trying to 'git checkout $prefix', which is ambiguous if $prefix names a directory *and* a branch. Do 'git checkout -- $prefix' instead. The main place this appeared was in 'git subtree add'. Reported by several people. --- git-subtree.sh | 2 +- test.sh | 1 + todo | 6 ------ 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index f7d2fe408d..b7c741cfd4 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -426,7 +426,7 @@ cmd_add() debug "Adding $dir as '$rev'..." git read-tree --prefix="$dir" $rev || exit $? - git checkout "$dir" || exit $? + git checkout -- "$dir" || exit $? tree=$(git write-tree) || exit $? headrev=$(git rev-parse HEAD) || exit $? diff --git a/test.sh b/test.sh index 8283fadaad..bed7f27906 100755 --- a/test.sh +++ b/test.sh @@ -78,6 +78,7 @@ git init create main4 git commit -m 'main4' git branch -m master mainline +git branch subdir git fetch ../subproj sub1 git branch sub1 FETCH_HEAD diff --git a/todo b/todo index 3040b9f171..5e72b2e510 100644 --- a/todo +++ b/todo @@ -20,9 +20,6 @@ automated tests for --squash stuff - test.sh fails in msysgit? - sort error - see Thell's email - "add" command non-obviously requires a commitid; would be easier if it had a "pull" sort of mode instead @@ -43,9 +40,6 @@ should detect (and fix) it if it does. Otherwise the log message looks weird. - totally weird behavior in 'git subtree add' if --prefix matches - a branch name - "pull --squash" should do fetch-synthesize-merge, but instead just does "pull" directly, which doesn't work at all. From 8ac5eca1eaa88bd5b998e91531937404bc6425c4 Mon Sep 17 00:00:00 2001 From: Pelle Wessman Date: Wed, 30 Sep 2009 14:29:42 +0200 Subject: [PATCH 058/103] Check that the type of the tree really is a tree and not a commit as it seems to sometimes become when eg. a submodule has existed in the same position previously. --- git-subtree.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/git-subtree.sh b/git-subtree.sh index b7c741cfd4..454ce7ef22 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -322,6 +322,7 @@ subtree_for_commit() git ls-tree "$commit" -- "$dir" | while read mode type tree name; do assert [ "$name" = "$dir" ] + assert [ "$type" = "tree" ] echo $tree break done From add00a3229ba4ada0cb47a447fdc483658df78e9 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 2 Oct 2009 11:51:25 -0400 Subject: [PATCH 059/103] Add a README that says to email me instead of using github mail. What's with this new generation who hates email so much? --- README | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 README diff --git a/README b/README new file mode 100644 index 0000000000..c686b4a69b --- /dev/null +++ b/README @@ -0,0 +1,8 @@ + +Please read git-subtree.txt for documentation. + +Please don't contact me using github mail; it's slow, ugly, and worst of +all, redundant. Email me instead at apenwarr@gmail.com and I'll be happy to +help. + +Avery From 6f2012cdc021f6b47ed19bc7fe64159ce9eeda8a Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 2 Oct 2009 15:22:15 -0400 Subject: [PATCH 060/103] If someone provides a --prefix that ends with slash, strip the slash. Prefixes that differ only in the trailing slash should be considered identical. Also update the test to check that this works. --- git-subtree.sh | 6 +++--- test.sh | 4 ++-- todo | 4 ---- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 454ce7ef22..0949fefe20 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -102,7 +102,7 @@ esac if [ -z "$prefix" ]; then die "You must provide the --prefix option." fi -dir="$prefix" +dir="$(dirname "$prefix/.")" if [ "$command" != "pull" ]; then revs=$(git rev-parse $default --revs-only "$@") || exit $? @@ -175,7 +175,7 @@ find_latest_squash() sq= main= 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 | while read a b junk; do debug "$a $b $junk" @@ -210,7 +210,7 @@ find_existing_splits() revs="$2" main= 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 | while read a b junk; do case "$a" in diff --git a/test.sh b/test.sh index bed7f27906..12b0456574 100755 --- a/test.sh +++ b/test.sh @@ -82,7 +82,7 @@ git branch subdir git fetch ../subproj sub1 git branch sub1 FETCH_HEAD -git subtree add --prefix=subdir FETCH_HEAD +git subtree add --prefix=subdir/ FETCH_HEAD # this shouldn't actually do anything, since FETCH_HEAD is already a parent git merge -m 'merge -s -ours' -s ours FETCH_HEAD @@ -118,7 +118,7 @@ create sub9 git commit -m 'sub9' cd ../mainline -split2=$(git subtree split --annotate='*' --prefix subdir --rejoin) +split2=$(git subtree split --annotate='*' --prefix subdir/ --rejoin) git branch split2 "$split2" create subdir/main-sub10 diff --git a/todo b/todo index 5e72b2e510..7e44b0024f 100644 --- a/todo +++ b/todo @@ -36,10 +36,6 @@ one of the other git tools that git-subtree calls. Should detect this situation and print the *real* problem. - In fact, the prefix should *not* end with slash, and we - should detect (and fix) it if it does. Otherwise the - log message looks weird. - "pull --squash" should do fetch-synthesize-merge, but instead just does "pull" directly, which doesn't work at all. From 2275f7077d5ea2bb9201599dec0dd8f2a5de2e40 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 2 Oct 2009 16:09:09 -0400 Subject: [PATCH 061/103] Fix a minor problem in identifying squashes vs. normal splits. This didn't seem to have any noticeable side effects other than suspicious-looking log messages when you used -d. --- git-subtree.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/git-subtree.sh b/git-subtree.sh index 0949fefe20..cccc3400fd 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -214,12 +214,14 @@ find_existing_splits() --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs | while read a b junk; do case "$a" in - START) main="$b"; sq="$b" ;; + START) sq="$b" ;; git-subtree-mainline:) main="$b" ;; git-subtree-split:) sub="$b" ;; END) + debug " Main is: '$main'" if [ -z "$main" -a -n "$sub" ]; then # squash commits refer to a subtree + debug " Squash: $sq from $sub" cache_set "$sq" "$sub" fi if [ -n "$main" -a -n "$sub" ]; then From e31d1e2f30c943473de7a23bbbcd2dcea698e312 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 2 Oct 2009 18:23:54 -0400 Subject: [PATCH 062/103] cmd_pull didn't support --squash correctly. We should implement it as git fetch ... git subtree merge ... But we were instead just calling git pull -s subtree ... because 'git subtree merge' used to be just an alias for 'git merge -s subtree', but it no longer is. --- git-subtree.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index cccc3400fd..8baa376fe5 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -567,8 +567,9 @@ cmd_merge() cmd_pull() { ensure_clean - set -x - git pull -s subtree "$@" + git fetch "$@" || exit $? + revs=FETCH_HEAD + cmd_merge } "cmd_$command" "$@" From c567d9e59fceb93d7334a1414d0a2a9d6f4913de Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Wed, 4 Nov 2009 14:50:33 -0500 Subject: [PATCH 063/103] Add some tips for how to install. --- INSTALL | 13 +++++++++++++ Makefile | 1 + 2 files changed, 14 insertions(+) create mode 100644 INSTALL diff --git a/INSTALL b/INSTALL new file mode 100644 index 0000000000..5966dde46c --- /dev/null +++ b/INSTALL @@ -0,0 +1,13 @@ + +HOW TO INSTALL git-subtree +========================== + +Copy the file 'git-subtree.sh' to /usr/local/bin/git-subtree. + +That will make a 'git subtree' (note: space instead of dash) command +available. See the file git-subtree.txt for more. + +You can also install the man page by doing: + + make doc + cp git-subtree.1 /usr/share/man/man1/ diff --git a/Makefile b/Makefile index bc163dd390..3e97c6246f 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ default: @echo "git-subtree doesn't need to be built." + @echo "Just copy it somewhere on your PATH, like /usr/local/bin." @echo @echo "Try: make doc" @false From d8b2c0da177ddfc2bc8d1e47278aa1c240d63034 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sun, 15 Nov 2009 12:13:08 -0500 Subject: [PATCH 064/103] Oops, forgot a COPYING file. It's GPLv2. Thanks to Ben Walton for pointing this out. --- COPYING | 339 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 339 insertions(+) create mode 100644 COPYING diff --git a/COPYING b/COPYING new file mode 100644 index 0000000000..d511905c16 --- /dev/null +++ b/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. From 0d31de303f9e8e28cc1649dbf41c1cc635bae2d8 Mon Sep 17 00:00:00 2001 From: Ben Walton Date: Fri, 13 Nov 2009 20:29:39 -0500 Subject: [PATCH 065/103] add installation support to Makefile Signed-off-by: Ben Walton --- Makefile | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Makefile b/Makefile index 3e97c6246f..faefffded5 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,13 @@ +prefix ?= /usr/local +mandir ?= $(prefix)/share/man +gitdir ?= $(shell git --exec-path) + +# this should be set to a 'standard' bsd-type install program +INSTALL ?= install +INSTALL_DATA = $(INSTALL) -c -m 0644 +INSTALL_EXE = $(INSTALL) -c -m 0755 +INSTALL_DIR = $(INSTALL) -c -d -m 0755 + default: @echo "git-subtree doesn't need to be built." @echo "Just copy it somewhere on your PATH, like /usr/local/bin." @@ -5,6 +15,16 @@ default: @echo "Try: make doc" @false +install: install-exe install-doc + +install-exe: git-subtree.sh + $(INSTALL_DIR) $(DESTDIR)/$(gitdir) + $(INSTALL_EXE) $< $(DESTDIR)/$(gitdir)/git-subtree + +install-doc: git-subtree.1 + $(INSTALL_DIR) $(DESTDIR)/$(mandir)/man1/ + $(INSTALL_DATA) $< $(DESTDIR)/$(mandir)/man1/ + doc: git-subtree.1 %.1: %.xml From d20ac24c2fe860dba99b40d76fe371a323e9918d Mon Sep 17 00:00:00 2001 From: Ben Walton Date: Fri, 13 Nov 2009 20:29:40 -0500 Subject: [PATCH 066/103] make git version dynamic when building documentation Signed-off-by: Ben Walton --- Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index faefffded5..9b204bdcae 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,8 @@ prefix ?= /usr/local mandir ?= $(prefix)/share/man gitdir ?= $(shell git --exec-path) +gitver ?= $(word 3,$(shell git --version)) + # this should be set to a 'standard' bsd-type install program INSTALL ?= install INSTALL_DATA = $(INSTALL) -c -m 0644 @@ -32,7 +34,7 @@ doc: git-subtree.1 %.xml: %.txt asciidoc -b docbook -d manpage -f asciidoc.conf \ - -agit_version=1.6.3 $^ + -agit_version=$(gitver) $^ clean: rm -f *~ *.xml *.html *.1 From d344532afd3d99c9cdd6d7cfc46bd92ff7def4e8 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Fri, 20 Nov 2009 19:43:47 -0500 Subject: [PATCH 067/103] Weird, I forgot to have 'make test' call test.sh. --- Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Makefile b/Makefile index 9b204bdcae..91e0cc08ea 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,7 @@ default: @echo "Just copy it somewhere on your PATH, like /usr/local/bin." @echo @echo "Try: make doc" + @echo " or: make test" @false install: install-exe install-doc @@ -35,6 +36,9 @@ doc: git-subtree.1 %.xml: %.txt asciidoc -b docbook -d manpage -f asciidoc.conf \ -agit_version=$(gitver) $^ + +test: + ./test.sh clean: rm -f *~ *.xml *.html *.1 From 6da401386ea37f1bda3f6717d7c77fb0e5c351b2 Mon Sep 17 00:00:00 2001 From: Jakub Suder Date: Wed, 6 Jan 2010 23:11:43 +0100 Subject: [PATCH 068/103] added -p alias for --prefix --- git-subtree.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 8baa376fe5..28fb8e81fb 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -16,7 +16,7 @@ git subtree split --prefix= h,help show the help q quiet d show debug messages -prefix= the name of the subdir to split out +p,prefix= the name of the subdir to split out options for 'split' annotate= add a prefix to commit message of new commits b,branch= create a new branch from the split subtree @@ -76,7 +76,7 @@ while [ $# -gt 0 ]; do --annotate) annotate="$1"; shift ;; --no-annotate) annotate= ;; -b) branch="$1"; shift ;; - --prefix) prefix="$1"; shift ;; + -p) prefix="$1"; shift ;; --no-prefix) prefix= ;; --onto) onto="$1"; shift ;; --no-onto) onto= ;; From 2da0969a794eac50401fd25ea072224d4378a5fe Mon Sep 17 00:00:00 2001 From: Jakub Suder Date: Sat, 9 Jan 2010 19:55:35 +0100 Subject: [PATCH 069/103] added -m/--message option for setting merge commit message --- git-subtree.sh | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 28fb8e81fb..96118735b2 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -17,6 +17,7 @@ h,help show the help q quiet d show debug messages p,prefix= the name of the subdir to split out +m,message= use the given message as the commit message for the merge commit options for 'split' annotate= add a prefix to commit message of new commits b,branch= create a new branch from the split subtree @@ -40,6 +41,7 @@ rejoin= ignore_joins= annotate= squash= +message= debug() { @@ -77,6 +79,7 @@ while [ $# -gt 0 ]; do --no-annotate) annotate= ;; -b) branch="$1"; shift ;; -p) prefix="$1"; shift ;; + -m) message="$1"; shift ;; --no-prefix) prefix= ;; --onto) onto="$1"; shift ;; --no-onto) onto= ;; @@ -266,8 +269,13 @@ add_msg() dir="$1" latest_old="$2" latest_new="$3" + if [ -n "$message" ]; then + commit_message="$message" + else + commit_message="Add '$dir/' from commit '$latest_new'" + fi cat <<-EOF - Add '$dir/' from commit '$latest_new' + $commit_message git-subtree-dir: $dir git-subtree-mainline: $latest_old @@ -275,13 +283,27 @@ add_msg() EOF } +add_squashed_msg() +{ + if [ -n "$message" ]; then + echo "$message" + else + echo "Merge commit '$1' as '$2'" + fi +} + rejoin_msg() { dir="$1" latest_old="$2" latest_new="$3" + if [ -n "$message" ]; then + commit_message="$message" + else + commit_message="Split '$dir/' into commit '$latest_new'" + fi cat <<-EOF - Split '$dir/' into commit '$latest_new' + $message git-subtree-dir: $dir git-subtree-mainline: $latest_old @@ -441,7 +463,7 @@ cmd_add() if [ -n "$squash" ]; then rev=$(new_squash_commit "" "" "$rev") || exit $? - commit=$(echo "Merge commit '$rev' as '$dir'" | + commit=$(add_squashed_msg "$rev" "$dir" | git commit-tree $tree $headp -p "$rev") || exit $? else commit=$(add_msg "$dir" "$headrev" "$rev" | @@ -561,7 +583,7 @@ cmd_merge() rev="$new" fi - git merge -s subtree $rev + git merge -s subtree --message="$message" $rev } cmd_pull() From 0a562948ae1fe7130f4e9a29ce4107311ab93b91 Mon Sep 17 00:00:00 2001 From: Jakub Suder Date: Sat, 9 Jan 2010 19:56:05 +0100 Subject: [PATCH 070/103] allow using --branch with existing branches if it makes sense --- git-subtree.sh | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 96118735b2..09992e39d5 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -161,6 +161,20 @@ rev_exists() fi } +rev_is_descendant_of_branch() +{ + newrev="$1" + branch="$2" + branch_hash=$(git rev-parse $branch) + match=$(git rev-list $newrev | grep $branch_hash) + + if [ -n "$match" ]; then + return 0 + else + return 1 + fi +} + # if a commit doesn't have a parent, this might not work. But we only want # to remove the parent from the rev-list, and since it doesn't exist, it won't # be there anyway, so do nothing in that case. @@ -476,10 +490,6 @@ cmd_add() cmd_split() { - if [ -n "$branch" ] && rev_exists "refs/heads/$branch"; then - die "Branch '$branch' already exists." - fi - debug "Splitting $dir..." cache_setup || exit $? @@ -510,7 +520,8 @@ cmd_split() eval "$grl" | while read rev parents; do revcount=$(($revcount + 1)) - say -n "$revcount/$revmax ($createcount) " + say -n "$revcount/$revmax ($createcount) +" debug "Processing commit: $rev" exists=$(cache_get $rev) if [ -n "$exists" ]; then @@ -548,9 +559,16 @@ cmd_split() $latest_new >&2 || exit $? fi if [ -n "$branch" ]; then - git update-ref -m 'subtree split' "refs/heads/$branch" \ - $latest_new "" || exit $? - say "Created branch '$branch'" + 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'." + fi + action='Updated' + else + action='Created' + fi + git update-ref -m 'subtree split' "refs/heads/$branch" $latest_new || exit $? + say "$action branch '$branch'" fi echo $latest_new exit 0 From da949cc554304bf9dc2b20ffcd470fb6b8a35576 Mon Sep 17 00:00:00 2001 From: Jakub Suder Date: Sat, 9 Jan 2010 23:01:39 +0100 Subject: [PATCH 071/103] fix for subtree split not finding proper base for new commits --- git-subtree.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/git-subtree.sh b/git-subtree.sh index 09992e39d5..cdf7b0992b 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -538,7 +538,10 @@ cmd_split() # ugly. is there no better way to tell if this is a subtree # vs. a mainline commit? Does it matter? - [ -z $tree ] && continue + if [ -z $tree ]; then + cache_set $rev $rev + continue + fi newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $? debug " newrev is: $newrev" From 6e25f79f353a1c1f454ae0c44aa45ff3ab44551c Mon Sep 17 00:00:00 2001 From: Jakub Suder Date: Tue, 12 Jan 2010 22:38:21 +0100 Subject: [PATCH 072/103] changed alias for --prefix from -p to -P --- git-subtree.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index cdf7b0992b..0a5cafa77f 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -16,7 +16,7 @@ git subtree split --prefix= h,help show the help q quiet d show debug messages -p,prefix= the name of the subdir to split out +P,prefix= the name of the subdir to split out m,message= use the given message as the commit message for the merge commit options for 'split' annotate= add a prefix to commit message of new commits @@ -78,7 +78,7 @@ while [ $# -gt 0 ]; do --annotate) annotate="$1"; shift ;; --no-annotate) annotate= ;; -b) branch="$1"; shift ;; - -p) prefix="$1"; shift ;; + -P) prefix="$1"; shift ;; -m) message="$1"; shift ;; --no-prefix) prefix= ;; --onto) onto="$1"; shift ;; From 12629161a89511847ec5703ca457ee373deb33cb Mon Sep 17 00:00:00 2001 From: Jakub Suder Date: Tue, 12 Jan 2010 22:38:34 +0100 Subject: [PATCH 073/103] fixed bug in commit message for split --- git-subtree.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-subtree.sh b/git-subtree.sh index 0a5cafa77f..48bc570390 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -317,7 +317,7 @@ rejoin_msg() commit_message="Split '$dir/' into commit '$latest_new'" fi cat <<-EOF - $message + $commit_message git-subtree-dir: $dir git-subtree-mainline: $latest_old From 13ea2b5e0786e70780f91637fb82ca18dd03a730 Mon Sep 17 00:00:00 2001 From: Jakub Suder Date: Tue, 12 Jan 2010 23:04:25 +0100 Subject: [PATCH 074/103] added tests for recent changes --- test.sh | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 2 deletions(-) diff --git a/test.sh b/test.sh index 12b0456574..cfe3a3c258 100755 --- a/test.sh +++ b/test.sh @@ -53,6 +53,16 @@ multiline() done } +undo() +{ + git reset --hard HEAD~ +} + +last_commit_message() +{ + git log --format=%s -1 +} + rm -rf mainline subproj mkdir mainline subproj @@ -82,7 +92,24 @@ git branch subdir git fetch ../subproj sub1 git branch sub1 FETCH_HEAD + +# check if --message works for add +git subtree add --prefix=subdir --message="Added subproject" sub1 +check_equal "$(last_commit_message)" "Added subproject" +undo + +# check if --message works as -m and --prefix as -P +git subtree add -P subdir -m "Added subproject using git subtree" sub1 +check_equal "$(last_commit_message)" "Added subproject using git subtree" +undo + +# check if --message works with squash too +git subtree add -P subdir -m "Added subproject with squash" --squash sub1 +check_equal "$(last_commit_message)" "Added subproject with squash" +undo + git subtree add --prefix=subdir/ FETCH_HEAD +check_equal "$(last_commit_message)" "Add 'subdir/' from commit '$(git rev-parse sub1)'" # this shouldn't actually do anything, since FETCH_HEAD is already a parent git merge -m 'merge -s -ours' -s ours FETCH_HEAD @@ -98,13 +125,44 @@ git commit -m 'main-sub7' git fetch ../subproj sub2 git branch sub2 FETCH_HEAD + +# check if --message works for merge +git subtree merge --prefix=subdir -m "Merged changes from subproject" sub2 +check_equal "$(last_commit_message)" "Merged changes from subproject" +undo + +# check if --message for merge works with squash too +git subtree merge --prefix subdir -m "Merged changes from subproject using squash" --squash sub2 +check_equal "$(last_commit_message)" "Merged changes from subproject using squash" +undo + git subtree merge --prefix=subdir FETCH_HEAD git branch pre-split +check_equal "$(last_commit_message)" "Merge commit '$(git rev-parse sub2)' into mainline" -spl1=$(git subtree split --annotate='*' \ - --prefix subdir --onto FETCH_HEAD --rejoin) +# check if --message works for split+rejoin +spl1=$(git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin) echo "spl1={$spl1}" git branch spl1 "$spl1" +check_equal "$(last_commit_message)" "Split & rejoin" +undo + +# check split with --branch +git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --branch splitbr1 +check_equal "$(git rev-parse splitbr1)" "$spl1" + +# check split with --branch for an existing branch +git branch splitbr2 sub1 +git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --branch splitbr2 +check_equal "$(git rev-parse splitbr2)" "$spl1" + +# check split with --branch for an incompatible branch +result=$(git subtree split --prefix subdir --onto FETCH_HEAD --branch subdir || echo "caught error") +check_equal "$result" "caught error" + + +git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --rejoin +check_equal "$(last_commit_message)" "Split 'subdir/' into commit '$spl1'" create subdir/main-sub8 git commit -m 'main-sub8' @@ -170,6 +228,50 @@ check_equal "$(git log --pretty=format:'%s' HEAD^2 | grep -i split)" "" # meaningless to subproj since one side of the merge refers to the mainline) check_equal "$(git log --pretty=format:'%s%n%b' HEAD^2 | grep 'git-subtree.*:')" "" + +# check if split can find proper base without --onto +# prepare second pair of repositories +mkdir test2 +cd test2 + +mkdir main +cd main +git init +create main1 +git commit -m "main1" + +cd .. +mkdir sub +cd sub +git init +create sub2 +git commit -m "sub2" + +cd ../main +git fetch ../sub master +git branch sub2 FETCH_HEAD +git subtree add --prefix subdir sub2 + +cd ../sub +create sub3 +git commit -m "sub3" + +cd ../main +git fetch ../sub master +git branch sub3 FETCH_HEAD +git subtree merge --prefix subdir sub3 + +create subdir/main-sub4 +git commit -m "main-sub4" +git subtree split --prefix subdir --branch mainsub4 + +# at this point, the new commit's parent should be sub3 +# if it's not, something went wrong (the "newparent" of "master~" commit should have been sub3, +# but it wasn't, because it's cache was not set to itself) +check_equal "$(git log --format=%P -1 mainsub4)" "$(git rev-parse sub3)" + + + # make sure no patch changes more than one file. The original set of commits # changed only one file each. A multi-file change would imply that we pruned # commits too aggressively. From 4a6ea5ce301c9844722bfdb9291bd9cb7797d9ed Mon Sep 17 00:00:00 2001 From: Jakub Suder Date: Tue, 12 Jan 2010 23:04:56 +0100 Subject: [PATCH 075/103] added temporary test dirs to gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index e358b18b78..7e77c9d022 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *~ git-subtree.xml git-subtree.1 +mainline +subproj From 6bd910a82155ae3def5cf38acb27d36a192c449e Mon Sep 17 00:00:00 2001 From: Jakub Suder Date: Tue, 12 Jan 2010 23:34:52 +0100 Subject: [PATCH 076/103] improved rev_is_descendant_of_branch() function --- git-subtree.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 48bc570390..66ce251eaa 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -166,9 +166,9 @@ rev_is_descendant_of_branch() newrev="$1" branch="$2" branch_hash=$(git rev-parse $branch) - match=$(git rev-list $newrev | grep $branch_hash) + match=$(git rev-list -1 $branch_hash ^$newrev) - if [ -n "$match" ]; then + if [ -z "$match" ]; then return 0 else return 1 From e1ce417d0cb4bfe719efa07417c690b1ce0326e9 Mon Sep 17 00:00:00 2001 From: Arlen Cuss Date: Tue, 19 Jan 2010 12:51:19 -0700 Subject: [PATCH 077/103] Fix refspecs in given example for git subtree pull. (Updated slightly by apenwarr) --- git-subtree.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-subtree.txt b/git-subtree.txt index e7ce2d3654..9b2d48e334 100644 --- a/git-subtree.txt +++ b/git-subtree.txt @@ -258,7 +258,7 @@ And you can merge changes back in from the upstream project just as easily: $ git subtree pull --prefix=gitweb \ - git@github.com:whatever/gitweb.git gitweb-latest:master + git@github.com:whatever/gitweb.git master Or, using '--squash', you can actually rewind to an earlier version of gitweb: From e2d0a4502f115ee63b8ff96661cfcc8aad075822 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Tue, 2 Feb 2010 10:30:11 -0500 Subject: [PATCH 078/103] Jakub's changes broke the progress message slightly. We really need that ^M (\r), not a ^J (\n) if we want the status message to overwrite itself nicely. --- git-subtree.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 66ce251eaa..11cda9ea82 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -520,8 +520,7 @@ cmd_split() eval "$grl" | while read rev parents; do revcount=$(($revcount + 1)) - say -n "$revcount/$revmax ($createcount) -" + say -n "$revcount/$revmax ($createcount) " debug "Processing commit: $rev" exists=$(cache_get $rev) if [ -n "$exists" ]; then From 37668a13edbc8bd8f8ac5ecbd5bf839a4171c09b Mon Sep 17 00:00:00 2001 From: Win Treese Date: Fri, 5 Feb 2010 19:48:11 -0500 Subject: [PATCH 079/103] git-subtree.txt: add another example. --- git-subtree.txt | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/git-subtree.txt b/git-subtree.txt index 9b2d48e334..2200aaeaf2 100644 --- a/git-subtree.txt +++ b/git-subtree.txt @@ -223,8 +223,8 @@ OPTIONS FOR split subproject's history to be part of your project anyway. -EXAMPLES --------- +EXAMPLE 1 +--------- Let's use the repository for the git source code as an example. First, get your own copy of the git.git repository: @@ -284,6 +284,23 @@ the standard gitweb: git log gitweb-latest..$(git subtree split --prefix=gitweb) +EXAMPLE 2 +--------- +Suppose you have a source directory with many files and +subdirectories, and you want to extract the lib directory to its own +git project. Here's a short way to do it: + +First, make the new repository wherever you want: + + git init --bare + +Back in your original directory: + git subtree split --prefix=lib --annotate="(split)" -b split + +Then push the new branch onto the new empty repository: + git push split:master + + AUTHOR ------ From 349a70d5cf127222c8a089f116070614ebd18732 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sat, 6 Feb 2010 15:05:17 -0500 Subject: [PATCH 080/103] Make tests pass with recent git (1.7.0 and up). It seems that in older versions, --message="" was interpreted as "use the default commit message" instead of "use an empty commit message", and git-subtree was depending on this behaviour. Now we don't, so tests pass again. --- git-subtree.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/git-subtree.sh b/git-subtree.sh index 11cda9ea82..009c0db9bc 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -603,7 +603,11 @@ cmd_merge() rev="$new" fi - git merge -s subtree --message="$message" $rev + if [ -n "$message" ]; then + git merge -s subtree --message="$message" $rev + else + git merge -s subtree $rev + fi } cmd_pull() From ec54f0d9adb50baa14eae44a1b8c410f794d32de Mon Sep 17 00:00:00 2001 From: Win Treese Date: Fri, 5 Feb 2010 22:02:43 -0500 Subject: [PATCH 081/103] Make sure that exists when splitting. And test cases for that check, as well as for an error if no prefix is specified at all. --- git-subtree.sh | 5 +++++ test.sh | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/git-subtree.sh b/git-subtree.sh index 009c0db9bc..52d4c0aeb1 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -105,6 +105,11 @@ esac if [ -z "$prefix" ]; then die "You must provide the --prefix option." fi + +if [ "$command" = "split" -a """"! -e "$prefix" ]; then + die "$prefix does not exist." +fi + dir="$(dirname "$prefix/.")" if [ "$command" != "pull" ]; then diff --git a/test.sh b/test.sh index cfe3a3c258..d0a2c86c24 100755 --- a/test.sh +++ b/test.sh @@ -140,6 +140,14 @@ git subtree merge --prefix=subdir FETCH_HEAD git branch pre-split check_equal "$(last_commit_message)" "Merge commit '$(git rev-parse sub2)' into mainline" +# Check that prefix argument is required for split (exits with warning and exit status = 1) +! result=$(git subtree split 2>&1) +check_equal "You must provide the --prefix option." "$result" + +# Check that the exists for a split. +! result=$(git subtree split --prefix=non-existent-directory 2>&1) +check_equal "non-existent-directory does not exist." "$result" + # check if --message works for split+rejoin spl1=$(git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin) echo "spl1={$spl1}" From 77ba30585213ee8e2be21841ba38786fd5bb26a5 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Mon, 8 Feb 2010 15:00:42 -0500 Subject: [PATCH 082/103] Improve checking for existence of the --prefix directory. For add, the prefix must *not* already exist. For all the other commands, it *must* already exist. --- git-subtree.sh | 9 ++++++--- test.sh | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 52d4c0aeb1..e76b45c2dd 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -106,9 +106,12 @@ if [ -z "$prefix" ]; then die "You must provide the --prefix option." fi -if [ "$command" = "split" -a """"! -e "$prefix" ]; then - die "$prefix does not exist." -fi +case "$command" in + add) [ -e "$prefix" ] && + die "prefix '$prefix' already exists." ;; + *) [ -e "$prefix" ] || + die "'$prefix' does not exist; use 'git subtree add'" ;; +esac dir="$(dirname "$prefix/.")" diff --git a/test.sh b/test.sh index d0a2c86c24..1446cfeed8 100755 --- a/test.sh +++ b/test.sh @@ -21,6 +21,19 @@ check() fi } +check_not() +{ + echo + echo "check: NOT " "$@" + if "$@"; then + echo FAILED + exit 1 + else + echo ok + return 0 + fi +} + check_equal() { echo @@ -94,6 +107,8 @@ git fetch ../subproj sub1 git branch sub1 FETCH_HEAD # check if --message works for add +check_not git subtree merge --prefix=subdir sub1 +check_not git subtree pull --prefix=subdir ../subproj sub1 git subtree add --prefix=subdir --message="Added subproject" sub1 check_equal "$(last_commit_message)" "Added subproject" undo From 00889c8ca7017d7b9d07d47843e3a3e245a1de26 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Mon, 8 Feb 2010 19:42:15 -0500 Subject: [PATCH 083/103] Oops. Apparently I didn't run 'make test' after most recent change. Thanks to Dan Sabath for pointing that out. --- test.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test.sh b/test.sh index 1446cfeed8..c6ecde20bd 100755 --- a/test.sh +++ b/test.sh @@ -161,7 +161,8 @@ check_equal "You must provide the --prefix option." "$result" # Check that the exists for a split. ! result=$(git subtree split --prefix=non-existent-directory 2>&1) -check_equal "non-existent-directory does not exist." "$result" +check_equal "'non-existent-directory' does not exist; use 'git subtree add'" \ + "$result" # check if --message works for split+rejoin spl1=$(git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin) From 6fe986307d983704d3c6b3540f91fa301ff48549 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Mon, 8 Feb 2010 19:44:41 -0500 Subject: [PATCH 084/103] Some recent tests accidentally depended on very new versions of git. The "--format" option is too new. Use "--pretty=format:" (which means the same thing) instead. Now it works again on git 1.6.0 (at least). --- test.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test.sh b/test.sh index c6ecde20bd..8c1f1ea6bd 100755 --- a/test.sh +++ b/test.sh @@ -73,7 +73,7 @@ undo() last_commit_message() { - git log --format=%s -1 + git log --pretty=format:%s -1 } rm -rf mainline subproj @@ -292,7 +292,7 @@ git subtree split --prefix subdir --branch mainsub4 # at this point, the new commit's parent should be sub3 # if it's not, something went wrong (the "newparent" of "master~" commit should have been sub3, # but it wasn't, because it's cache was not set to itself) -check_equal "$(git log --format=%P -1 mainsub4)" "$(git rev-parse sub3)" +check_equal "$(git log --pretty=format:%P -1 mainsub4)" "$(git rev-parse sub3)" From c6ca48d4dc5b93a7576d2afa66b057f8c8a20718 Mon Sep 17 00:00:00 2001 From: Dan Sabath Date: Mon, 8 Feb 2010 20:31:51 -0500 Subject: [PATCH 085/103] docs: add simple 'add' case to clarify setup. This patch adds a simple use case for adding a library to an existing repository. Signed-off-by: Dan Sabath --- git-subtree.txt | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/git-subtree.txt b/git-subtree.txt index 2200aaeaf2..cde5a7e73e 100644 --- a/git-subtree.txt +++ b/git-subtree.txt @@ -225,7 +225,31 @@ OPTIONS FOR split EXAMPLE 1 --------- -Let's use the repository for the git source code as an example. +Let's assume that you have a local repository that you would like +to add an external vendor library to. In this case we will add the +git-subtree repository as a subdirectory of your already existing +git-extensions repository in ~/git-extensions/. + +First we need to fetch the remote objects + $ cd ~/git-extensions + $ git fetch git://github.com/apenwarr/git-subtree.git master + +'master' needs to be a valid remote ref and can be a different branch +name + +Now we add the vendor library with + $ git subtree add --prefix=git-subtree --squash FETCH_HEAD + +You can omit the --squash flag, but doing so will increase the number +of commits that are incldued in your local repository. + +We now have ~/git-extensions/git-subtree directory with the git-subtree +subdirectory containing code from the master branch of +git://github.com/apenwarr/git-subtree.git + +EXAMPLE 2 +--------- +Let's use the repository for the git source code as an example. First, get your own copy of the git.git repository: $ git clone git://git.kernel.org/pub/scm/git/git.git test-git @@ -284,7 +308,7 @@ the standard gitweb: git log gitweb-latest..$(git subtree split --prefix=gitweb) -EXAMPLE 2 +EXAMPLE 3 --------- Suppose you have a source directory with many files and subdirectories, and you want to extract the lib directory to its own From ae3301876cfea3b7260fbe0840635d508968a047 Mon Sep 17 00:00:00 2001 From: Dan Sabath Date: Mon, 8 Feb 2010 17:43:09 -0800 Subject: [PATCH 086/103] Docs: cleaning up example textual redundancy Signed-off-by: Dan Sabath --- git-subtree.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/git-subtree.txt b/git-subtree.txt index cde5a7e73e..c455f6912b 100644 --- a/git-subtree.txt +++ b/git-subtree.txt @@ -243,9 +243,9 @@ Now we add the vendor library with You can omit the --squash flag, but doing so will increase the number of commits that are incldued in your local repository. -We now have ~/git-extensions/git-subtree directory with the git-subtree -subdirectory containing code from the master branch of -git://github.com/apenwarr/git-subtree.git +We now have a ~/git-extensions/git-subtree directory containing code +from the master branch of git://github.com/apenwarr/git-subtree.git +in our git-extensions repository. EXAMPLE 2 --------- From c00d1d11688dc02f066196ed18783effdb7767ab Mon Sep 17 00:00:00 2001 From: Wayne Walter Date: Sat, 13 Feb 2010 14:32:21 -0500 Subject: [PATCH 087/103] Added new 'push' command and 2-parameter form of 'add'. Now you can do: git subtree add --prefix=whatever git://wherever branchname to add a new branch, instead of rather weirdly having to do 'git fetch' first. You can also split and push in one step: git subtree push --prefix=whatever git://wherever newbranch (Somewhat cleaned up by apenwarr.) --- INSTALL | 11 +++++++++- git-subtree.sh | 58 +++++++++++++++++++++++++++++++++++++++++-------- git-subtree.txt | 24 +++++++++++++------- install.sh | 2 ++ 4 files changed, 77 insertions(+), 18 deletions(-) create mode 100644 install.sh diff --git a/INSTALL b/INSTALL index 5966dde46c..81ac702ad2 100644 --- a/INSTALL +++ b/INSTALL @@ -2,7 +2,16 @@ HOW TO INSTALL git-subtree ========================== -Copy the file 'git-subtree.sh' to /usr/local/bin/git-subtree. +You simply need to copy the file 'git-subtree.sh' to where +the rest of the git scripts are stored. + +From the Git bash window just run: + +install.sh + +Or if you have the full Cygwin installed, you can use make: + +make install That will make a 'git subtree' (note: space instead of dash) command available. See the file git-subtree.txt for more. diff --git a/git-subtree.sh b/git-subtree.sh index e76b45c2dd..501c6dc2f1 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -11,6 +11,7 @@ OPTS_SPEC="\ git subtree add --prefix= git subtree merge --prefix= git subtree pull --prefix= +git subtree push --prefix= git subtree split --prefix= -- h,help show the help @@ -24,7 +25,7 @@ b,branch= create a new branch from the split subtree ignore-joins ignore prior --rejoin commits onto= try connecting new tree to an existing one rejoin merge the new branch back into HEAD - options for 'add', 'merge', and 'pull' + options for 'add', 'merge', 'pull' and 'push' squash merge subtree changes as a single commit " eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?) @@ -98,7 +99,7 @@ command="$1" shift case "$command" in add|merge|pull) default= ;; - split) default="--default HEAD" ;; + split|push) default="--default HEAD" ;; *) die "Unknown command '$command'" ;; esac @@ -115,7 +116,7 @@ esac dir="$(dirname "$prefix/.")" -if [ "$command" != "pull" ]; then +if [ "$command" != "pull" -a "$command" != "add" -a "$command" != "push" ]; then revs=$(git rev-parse $default --revs-only "$@") || exit $? dirs="$(git rev-parse --no-revs --no-flags "$@")" || exit $? if [ -n "$dirs" ]; then @@ -450,10 +451,10 @@ copy_or_skip() ensure_clean() { - if ! git diff-index HEAD --exit-code --quiet; then + if ! git diff-index HEAD --exit-code --quiet 2>&1; then die "Working tree has modifications. Cannot add." fi - if ! git diff-index --cached HEAD --exit-code --quiet; then + if ! git diff-index --cached HEAD --exit-code --quiet 2>&1; then die "Index has modifications. Cannot add." fi } @@ -463,12 +464,34 @@ cmd_add() if [ -e "$dir" ]; then die "'$dir' already exists. Cannot add." fi + ensure_clean - set -- $revs - if [ $# -ne 1 ]; then - die "You must provide exactly one revision. Got: '$revs'" + if [ $# -eq 1 ]; then + "cmd_add_commit" "$@" + elif [ $# -eq 2 ]; then + "cmd_add_repository" "$@" + else + say "error: parameters were '$@'" + die "Provide either a refspec or a repository and refspec." fi +} + +cmd_add_repository() +{ + echo "git fetch" "$@" + repository=$1 + refspec=$2 + git fetch "$@" || exit $? + revs=FETCH_HEAD + set -- $revs + cmd_add_commit "$@" +} + +cmd_add_commit() +{ + revs=$(git rev-parse $default --revs-only "$@") || exit $? + set -- $revs rev="$1" debug "Adding $dir as '$rev'..." @@ -586,6 +609,7 @@ cmd_split() cmd_merge() { + revs=$(git rev-parse $default --revs-only "$@") || exit $? ensure_clean set -- $revs @@ -623,7 +647,23 @@ cmd_pull() ensure_clean git fetch "$@" || exit $? revs=FETCH_HEAD - cmd_merge + set -- $revs + cmd_merge "$@" +} + +cmd_push() +{ + if [ $# -ne 2 ]; then + die "You must provide " + fi + if [ -e "$dir" ]; then + repository=$1 + refspec=$2 + echo "git push using: " $repository $refspec + git push $repository $(git subtree split --prefix=$prefix):refs/heads/$refspec + else + die "'$dir' must already exist. Try 'git subtree add'." + fi } "cmd_$command" "$@" diff --git a/git-subtree.txt b/git-subtree.txt index c455f6912b..4f715c640b 100644 --- a/git-subtree.txt +++ b/git-subtree.txt @@ -9,10 +9,12 @@ git-subtree - add, merge, and split subprojects stored in subtrees SYNOPSIS -------- [verse] -'git subtree' add --prefix= -'git subtree' merge --prefix= +'git subtree' add --prefix= 'git subtree' pull --prefix= -'git subtree' split --prefix= +'git subtree' push --prefix= +'git subtree' add --prefix= +'git subtree' merge --prefix= +'git subtree' split --prefix= DESCRIPTION @@ -60,11 +62,11 @@ COMMANDS -------- add:: Create the subtree by importing its contents - from the given commit. A new commit is created - automatically, joining the imported project's history - with your own. With '--squash', imports only a single - commit from the subproject, rather than its entire - history. + from the given or and remote . + A new commit is created automatically, joining the imported + project's history with your own. With '--squash', imports + only a single commit from the subproject, rather than its + entire history. merge:: Merge recent changes up to into the @@ -84,6 +86,12 @@ pull:: Exactly like 'merge', but parallels 'git pull' in that it fetches the given commit from the specified remote repository. + +push:: + Does a 'split' (see above) using the supplied + and then does a 'git push' to push the result to the + repository and refspec. This can be used to push your + subtree to different branches of the remote repository. split:: Extract a new, synthetic project history from the diff --git a/install.sh b/install.sh new file mode 100644 index 0000000000..1f87a62434 --- /dev/null +++ b/install.sh @@ -0,0 +1,2 @@ +# copy Git to where the rest of the Git scripts are found. +cp git-subtree.sh "$(git --exec-path)"/git-subtree \ No newline at end of file From 448e71e2637e0d7546fb0a2b386e74bc7aa93be8 Mon Sep 17 00:00:00 2001 From: Pelle Wessman Date: Fri, 7 May 2010 21:21:25 +0200 Subject: [PATCH 088/103] Use 'git merge -Xsubtree' when git version >= 1.7.0. It's possible to specify the subdir of a subtree since Git 1.7.0 - adding support for that functionality to make the merge more stable. Also checking for git version - now only uses the new subtree subdir option when on at least 1.7. --- git-subtree.sh | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index 501c6dc2f1..b7c350107e 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -634,11 +634,20 @@ cmd_merge() debug "New squash commit: $new" rev="$new" fi - - if [ -n "$message" ]; then - git merge -s subtree --message="$message" $rev + + version=$(git version) + if [ "$version" \< "git version 1.7" ]; then + if [ -n "$message" ]; then + git merge -s subtree --message="$message" $rev + else + git merge -s subtree $rev + fi else - git merge -s subtree $rev + if [ -n "$message" ]; then + git merge -Xsubtree="$prefix" --message="$message" $rev + else + git merge -Xsubtree="$prefix" $rev + fi fi } From 39f5fff0d53bcc68f5c698150d2d3b35eececc27 Mon Sep 17 00:00:00 2001 From: Pelle Wessman Date: Thu, 20 May 2010 22:40:09 +0200 Subject: [PATCH 089/103] Fixed regression with splitting out new subtree A folder in a repository that wasn't initially imported as a subtree could no longer be splitted into an entirely new subtree with no parent. A fix and a new test to fix that regression is added here. --- git-subtree.sh | 5 ++++- test.sh | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/git-subtree.sh b/git-subtree.sh index b7c350107e..a86cfd8b9f 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -253,6 +253,7 @@ find_existing_splits() if [ -n "$main" -a -n "$sub" ]; then debug " Prior: $main -> $sub" cache_set $main $sub + cache_set $sub $sub try_remove_previous "$main" try_remove_previous "$sub" fi @@ -569,7 +570,9 @@ cmd_split() # ugly. is there no better way to tell if this is a subtree # vs. a mainline commit? Does it matter? if [ -z $tree ]; then - cache_set $rev $rev + if [ -n "$newparents" ]; then + cache_set $rev $rev + fi continue fi diff --git a/test.sh b/test.sh index 8c1f1ea6bd..45237c3374 100755 --- a/test.sh +++ b/test.sh @@ -294,6 +294,15 @@ git subtree split --prefix subdir --branch mainsub4 # but it wasn't, because it's cache was not set to itself) check_equal "$(git log --pretty=format:%P -1 mainsub4)" "$(git rev-parse sub3)" +mkdir subdir2 +create subdir2/main-sub5 +git commit -m "main-sub5" +git subtree split --prefix subdir2 --branch mainsub5 + +# also test that we still can split out an entirely new subtree +# if the parent of the first commit in the tree isn't empty, +# then the new subtree has accidently been attached to something +check_equal "$(git log --pretty=format:%P -1 mainsub5)" "" # make sure no patch changes more than one file. The original set of commits From 9c632ea29ccd58a9967690c2670edec31dc468cd Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Thu, 24 Jun 2010 01:53:05 -0400 Subject: [PATCH 090/103] (Hopefully) fix PATH setting for msysgit. Reported by Evan Shaw. The problem is that $(git --exec-path) includes a 'git' binary which is incompatible with the one in /usr/bin; if you run it, it gives you an error about libiconv2.dll. You might think we could just add $(git --exec-path) at the *end* of PATH, but then if there are multiple versions of git installed, we could end up with the wrong one; earlier versions used to put git-sh-setup in /usr/bin, so we'd pick up that one before the new one. So now we just set PATH back to its original value right after running git-sh-setup, and we should be okay. --- git-subtree.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/git-subtree.sh b/git-subtree.sh index 501c6dc2f1..935dfca7f3 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -29,8 +29,12 @@ rejoin merge the new branch back into HEAD squash merge subtree changes as a single commit " eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?) + +OPATH=$PATH PATH=$(git --exec-path):$PATH . git-sh-setup +PATH=$OPATH # apparently needed for some versions of msysgit + require_work_tree quiet= From df2302d77449832eb4167999d747aef57e4bd1fc Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Thu, 24 Jun 2010 16:57:58 -0400 Subject: [PATCH 091/103] Another fix for PATH and msysgit. Evan Shaw tells me the previous fix didn't work. Let's use this one instead, which he says does work. This fix is kind of wrong because it will run the "correct" git-sh-setup *after* the one in /usr/bin, if there is one, which could be weird if you have multiple versions of git installed. But it works on my Linux and his msysgit, so it's obviously better than what we had before. --- git-subtree.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/git-subtree.sh b/git-subtree.sh index a15d91ffb1..781eef3783 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -30,10 +30,8 @@ squash merge subtree changes as a single commit " eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?) -OPATH=$PATH -PATH=$(git --exec-path):$PATH +PATH=$PATH:$(git --exec-path) . git-sh-setup -PATH=$OPATH # apparently needed for some versions of msysgit require_work_tree From 242b20dc0ae51719c32776fb5996c5d6cb463928 Mon Sep 17 00:00:00 2001 From: Bryan Larsen Date: Wed, 21 Jul 2010 12:58:05 -0400 Subject: [PATCH 092/103] docs: simplify example 1 The documentation was written prior to Wayne Walter's 2-parameter add. Using 2-parameter add in example 1 makes the example much simpler. --- git-subtree.txt | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/git-subtree.txt b/git-subtree.txt index 4f715c640b..dbcba31346 100644 --- a/git-subtree.txt +++ b/git-subtree.txt @@ -236,18 +236,14 @@ EXAMPLE 1 Let's assume that you have a local repository that you would like to add an external vendor library to. In this case we will add the git-subtree repository as a subdirectory of your already existing -git-extensions repository in ~/git-extensions/. +git-extensions repository in ~/git-extensions/: -First we need to fetch the remote objects - $ cd ~/git-extensions - $ git fetch git://github.com/apenwarr/git-subtree.git master + $ git subtree add --prefix=git-subtree --squash \ + git://github.com/apenwarr/git-subtree.git master 'master' needs to be a valid remote ref and can be a different branch name -Now we add the vendor library with - $ git subtree add --prefix=git-subtree --squash FETCH_HEAD - You can omit the --squash flag, but doing so will increase the number of commits that are incldued in your local repository. From 7f74d65b12ecb5142442c276da805b42e3023a9d Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Thu, 12 Aug 2010 11:15:57 -0400 Subject: [PATCH 093/103] Fix typo: an -> a Thanks to Vanuan on github. --- git-subtree.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-subtree.txt b/git-subtree.txt index dbcba31346..a30293d607 100644 --- a/git-subtree.txt +++ b/git-subtree.txt @@ -19,7 +19,7 @@ SYNOPSIS DESCRIPTION ----------- -git subtree allows you to include an subproject in your +git subtree allows you to include a subproject in your own repository as a subdirectory, optionally including the subproject's entire history. For example, you could include the source code for a library as a subdirectory of your From 11f1511e7650a78709d0d7198bb150cd5392d9d1 Mon Sep 17 00:00:00 2001 From: Cole Stanfield Date: Mon, 13 Sep 2010 13:16:57 -0600 Subject: [PATCH 094/103] Fixing eval syntax error. --- git-subtree.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-subtree.sh b/git-subtree.sh index 781eef3783..ce94d363dc 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -28,7 +28,7 @@ rejoin merge the new branch back into HEAD options for 'add', 'merge', 'pull' and 'push' squash merge subtree changes as a single commit " -eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?) +eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)" PATH=$PATH:$(git --exec-path) . git-sh-setup From 7f86ff0fe292a5061757f3ceaffdd992c5feaa9f Mon Sep 17 00:00:00 2001 From: John Yani Date: Thu, 12 Aug 2010 19:54:55 +0300 Subject: [PATCH 095/103] docs: Description, synopsys, options and examples changes. Description: Made the difference from submodules and the subtree merge strategy clearer. Synopsys and options: Synchronize with 'git subtree -h' output. I hope, properly. Examples: Added example descriptions in captions. Small fixes. Signed-off-by: John Yani --- git-subtree.txt | 110 ++++++++++++++++++++++++++++++------------------ 1 file changed, 68 insertions(+), 42 deletions(-) diff --git a/git-subtree.txt b/git-subtree.txt index a30293d607..18a9af501f 100644 --- a/git-subtree.txt +++ b/git-subtree.txt @@ -3,50 +3,55 @@ git-subtree(1) NAME ---- -git-subtree - add, merge, and split subprojects stored in subtrees +git-subtree - Merge subtrees together and split repository into subtrees SYNOPSIS -------- [verse] -'git subtree' add --prefix= -'git subtree' pull --prefix= -'git subtree' push --prefix= -'git subtree' add --prefix= -'git subtree' merge --prefix= -'git subtree' split --prefix= - +'git subtree' add -P |--prefix= +'git subtree' pull -P |--prefix= +'git subtree' push -P |--prefix= +'git subtree' merge -P |--prefix= +'git subtree' split -P |--prefix= [OPTIONS] [] + DESCRIPTION ----------- -git subtree allows you to include a subproject in your -own repository as a subdirectory, optionally including the -subproject's entire history. For example, you could -include the source code for a library as a subdirectory of your -application. +Subtrees allow subprojects to be included within a subdirectory +of the main project, optionally including the subproject's +entire history. -You can also extract the entire history of a subdirectory from -your project and make it into a standalone project. For -example, if a library you made for one application ends up being -useful elsewhere, you can extract its entire history and publish -that as its own git repository, without accidentally -intermingling the history of your application project. +For example, you could include the source code for a library +as a subdirectory of your application. -Most importantly, you can alternate back and forth between these -two operations. If the standalone library gets updated, you can +Subtrees are not to be confused with submodules, which are meant for +the same task. Unlike submodules, subtrees do not need any special +constructions (like .gitmodule files or gitlinks) be present in +your repository, and do not force end-users of your +repository to do anything special or to understand how subtrees +work. A subtree is just a subdirectory that can be +committed to, branched, and merged along with your project in +any way you want. + +They are neither not to be confused with using the subtree merge +strategy. The main difference is that, besides merging +of the other project as a subdirectory, you can also extract the +entire history of a subdirectory from your project and make it +into a standalone project. Unlike the subtree merge strategy +you can alternate back and forth between these +two operations. If the standalone library gets updated, you can automatically merge the changes into your project; if you update the library inside your project, you can "split" the changes back out again and merge them back into the library project. -Unlike the 'git submodule' command, git subtree doesn't produce -any special constructions (like .gitmodule files or gitlinks) in -your repository, and doesn't require end-users of your -repository to do anything special or to understand how subtrees -work. A subtree is just another subdirectory and can be -committed to, branched, and merged along with your project in -any way you want. +For example, if a library you made for one application ends up being +useful elsewhere, you can extract its entire history and publish +that as its own git repository, without accidentally +intermingling the history of your application project. +[TIP] In order to keep your commit messages clean, we recommend that people split their commits between the subtrees and the main project as much as possible. That is, if you make a change that @@ -128,20 +133,29 @@ OPTIONS --debug:: Produce even more unnecessary output messages on stderr. +-P :: --prefix=:: Specify the path in the repository to the subtree you - want to manipulate. This option is currently mandatory + want to manipulate. This option is mandatory for all commands. +-m :: +--message=:: + This option is only valid for add, merge and pull (unsure). + Specify as the commit message for the merge commit. -OPTIONS FOR add, merge, AND pull --------------------------------- + +OPTIONS FOR add, merge, push, pull +---------------------------------- --squash:: + This option is only valid for add, merge, push and pull + commands. + Instead of merging the entire history from the subtree project, produce only a single commit that contains all the differences you want to merge, and then merge that new commit into your project. - + Using this option helps to reduce log clutter. People rarely want to see every change that happened between v1.0 and v1.1 of the library they're using, since none of the @@ -169,6 +183,8 @@ OPTIONS FOR add, merge, AND pull OPTIONS FOR split ----------------- --annotate=:: + This option is only valid for the split command. + When generating synthetic history, add as a prefix to each commit message. Since we're creating new commits with the same commit message, but possibly @@ -184,12 +200,16 @@ OPTIONS FOR split -b :: --branch=:: + This option is only valid for the split command. + After generating the synthetic history, create a new branch called that contains the new history. This is suitable for immediate pushing upstream. must not already exist. --ignore-joins:: + This option is only valid for the split command. + If you use '--rejoin', git subtree attempts to optimize its history reconstruction to generate only the new commits since the last '--rejoin'. '--ignore-join' @@ -198,6 +218,8 @@ OPTIONS FOR split long time. --onto=:: + This option is only valid for the split command. + If your subtree was originally imported using something other than git subtree, its history may not match what git subtree is expecting. In that case, you can specify @@ -210,6 +232,8 @@ OPTIONS FOR split this option. --rejoin:: + This option is only valid for the split command. + After splitting, merge the newly created synthetic history back into your main project. That way, future splits can search only the part of history that has @@ -231,8 +255,8 @@ OPTIONS FOR split subproject's history to be part of your project anyway. -EXAMPLE 1 ---------- +EXAMPLE 1. Add command +---------------------- Let's assume that you have a local repository that you would like to add an external vendor library to. In this case we will add the git-subtree repository as a subdirectory of your already existing @@ -251,8 +275,8 @@ We now have a ~/git-extensions/git-subtree directory containing code from the master branch of git://github.com/apenwarr/git-subtree.git in our git-extensions repository. -EXAMPLE 2 ---------- +EXAMPLE 2. Extract a subtree using commit, merge and pull +--------------------------------------------------------- Let's use the repository for the git source code as an example. First, get your own copy of the git.git repository: @@ -312,22 +336,24 @@ the standard gitweb: git log gitweb-latest..$(git subtree split --prefix=gitweb) -EXAMPLE 3 ---------- +EXAMPLE 3. Extract a subtree using branch +----------------------------------------- Suppose you have a source directory with many files and subdirectories, and you want to extract the lib directory to its own git project. Here's a short way to do it: First, make the new repository wherever you want: - - git init --bare + + $ + $ git init --bare Back in your original directory: - git subtree split --prefix=lib --annotate="(split)" -b split + + $ git subtree split --prefix=lib --annotate="(split)" -b split Then push the new branch onto the new empty repository: - git push split:master + $ git push split:master AUTHOR From 9a40fcc2014cc4a85eca71f81ff4c86d49b7a9e2 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Thu, 21 Oct 2010 12:28:18 -0700 Subject: [PATCH 096/103] Fix a few typos/grammar-o's in the preceding commit. --- git-subtree.txt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/git-subtree.txt b/git-subtree.txt index 18a9af501f..0c44fda011 100644 --- a/git-subtree.txt +++ b/git-subtree.txt @@ -9,11 +9,11 @@ git-subtree - Merge subtrees together and split repository into subtrees SYNOPSIS -------- [verse] -'git subtree' add -P |--prefix= -'git subtree' pull -P |--prefix= -'git subtree' push -P |--prefix= -'git subtree' merge -P |--prefix= -'git subtree' split -P |--prefix= [OPTIONS] [] +'git subtree' add -P +'git subtree' pull -P +'git subtree' push -P +'git subtree' merge -P +'git subtree' split -P [OPTIONS] [] DESCRIPTION @@ -34,9 +34,9 @@ work. A subtree is just a subdirectory that can be committed to, branched, and merged along with your project in any way you want. -They are neither not to be confused with using the subtree merge +They are also not to be confused with using the subtree merge strategy. The main difference is that, besides merging -of the other project as a subdirectory, you can also extract the +the other project as a subdirectory, you can also extract the entire history of a subdirectory from your project and make it into a standalone project. Unlike the subtree merge strategy you can alternate back and forth between these From 6f4f84fa2a03f441826f323c291ca6566acd0d3a Mon Sep 17 00:00:00 2001 From: Jesse Greenwald Date: Tue, 9 Nov 2010 08:34:49 -0600 Subject: [PATCH 097/103] Split cmd now processes commits in topo order. Added the "--topo-order" option to git rev-list. Without this, it seems that the revision list is coming back in reverse order but it is sorted chronologically. This does not gurantee that parent commits are handled before child commits. --- git-subtree.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-subtree.sh b/git-subtree.sh index ce94d363dc..390c0fc574 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -547,7 +547,7 @@ cmd_split() # We can't restrict rev-list to only $dir here, because some of our # parents have the $dir contents the root, and those won't match. # (and rev-list --follow doesn't seem to solve this) - grl='git rev-list --reverse --parents $revs $unrevs' + grl='git rev-list --topo-order --reverse --parents $revs $unrevs' revmax=$(eval "$grl" | wc -l) revcount=0 createcount=0 From 915b9894abe94169e60a14e4dc671f6bd15131f3 Mon Sep 17 00:00:00 2001 From: Jesse Greenwald Date: Tue, 9 Nov 2010 22:18:36 -0600 Subject: [PATCH 098/103] Added check to order of processed commits. With debug messages enabled, "incorrect order" will be output whenever a commit is processed before its parents have been processed. This can be determined by checking to see if a parent isn't mapped to a new commit, but it has been processed. --- git-subtree.sh | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/git-subtree.sh b/git-subtree.sh index 390c0fc574..cf50de150c 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -138,6 +138,7 @@ cache_setup() cachedir="$GIT_DIR/subtree-cache/$$" rm -rf "$cachedir" || die "Can't delete old cachedir: $cachedir" 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 } @@ -151,6 +152,30 @@ cache_get() done } +cache_miss() +{ + for oldrev in $*; do + if [ ! -r "$cachedir/$oldrev" ]; then + echo $oldrev + fi + done +} + +check_parents() +{ + missed=$(cache_miss $*) + for miss in $missed; do + if [ ! -r "$cachedir/notree/$miss" ]; then + debug " incorrect order: $miss" + fi + done +} + +set_notree() +{ + echo "1" > "$cachedir/notree/$1" +} + cache_set() { oldrev="$1" @@ -568,10 +593,13 @@ cmd_split() tree=$(subtree_for_commit $rev "$dir") debug " tree is: $tree" + + check_parents $parents # ugly. is there no better way to tell if this is a subtree # vs. a mainline commit? Does it matter? if [ -z $tree ]; then + set_notree $rev if [ -n "$newparents" ]; then cache_set $rev $rev fi From 856bea1f71db30339539b0c917d6d7dc32729d0f Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Mon, 28 Feb 2011 16:49:42 -0800 Subject: [PATCH 099/103] It's also okay if an expected tree object is actually a commit. ...that happens with submodules sometimes, so don't panic. Reported by Sum-Wai Low. --- git-subtree.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-subtree.sh b/git-subtree.sh index cf50de150c..fa4e3e3661 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -397,7 +397,7 @@ subtree_for_commit() git ls-tree "$commit" -- "$dir" | while read mode type tree name; do assert [ "$name" = "$dir" ] - assert [ "$type" = "tree" ] + assert [ "$type" = "tree" -o "$type" = "commit" ] echo $tree break done From 2793ee6ba6da57d97e9c313741041f7eb2e88974 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Mon, 28 Feb 2011 20:48:50 -0800 Subject: [PATCH 100/103] Skip commit objects that should be trees, rather than copying them. An improvement on the previous patch, based on more reports from Sum-Wai Low. --- git-subtree.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/git-subtree.sh b/git-subtree.sh index fa4e3e3661..920c664bb7 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -398,6 +398,7 @@ subtree_for_commit() while read mode type tree name; do assert [ "$name" = "$dir" ] assert [ "$type" = "tree" -o "$type" = "commit" ] + [ "$type" = "commit" ] && continue # ignore submodules echo $tree break done From 9153f19f6d340b97c7d581d44884b6318e627155 Mon Sep 17 00:00:00 2001 From: "David A. Greene" Date: Sun, 29 Jan 2012 15:57:36 -0600 Subject: [PATCH 101/103] Move Tests Into Subdirectory Move the git-subtree tests into a "t" subdir to reflect how testing works at the top level. Signed-off-by: David A. Greene --- test.sh => t/test.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test.sh => t/test.sh (100%) diff --git a/test.sh b/t/test.sh similarity index 100% rename from test.sh rename to t/test.sh From 9e2a55a276df9647d0f79c0695e23e5164835c0d Mon Sep 17 00:00:00 2001 From: "David A. Greene" Date: Sun, 29 Jan 2012 16:09:15 -0600 Subject: [PATCH 102/103] Rename Test Rename the subtree test file to conform with git project conventions. Signed-off-by: David A. Greene --- t/{test.sh => t7900-subtree.sh} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename t/{test.sh => t7900-subtree.sh} (100%) diff --git a/t/test.sh b/t/t7900-subtree.sh similarity index 100% rename from t/test.sh rename to t/t7900-subtree.sh From d3a04e06c77d57978bb5230361c64946232cc346 Mon Sep 17 00:00:00 2001 From: "David A. Greene" Date: Sun, 29 Jan 2012 16:09:58 -0600 Subject: [PATCH 103/103] Use Test Harness Clean up the git-subtree tests to conform the git project conventions and use the existing test harness. Signed-off-by: David A. Greene --- t/t7900-subtree.sh | 572 +++++++++++++++++++++++++++++---------------- 1 file changed, 368 insertions(+), 204 deletions(-) diff --git a/t/t7900-subtree.sh b/t/t7900-subtree.sh index 45237c3374..585f8d5751 100755 --- a/t/t7900-subtree.sh +++ b/t/t7900-subtree.sh @@ -1,6 +1,14 @@ -#!/bin/bash -. shellopts.sh -set -e +#!/bin/sh +# +# Copyright (c) 2012 Avery Pennaraum +# +test_description='Basic porcelain support for subtrees + +This test verifies the basic operation of the merge, pull, add +and split subcommands of git subtree. +' + +. ./test-lib.sh create() { @@ -8,42 +16,16 @@ create() git add "$1" } -check() -{ - echo - echo "check:" "$@" - if "$@"; then - echo ok - return 0 - else - echo FAILED - exit 1 - fi -} - -check_not() -{ - echo - echo "check: NOT " "$@" - if "$@"; then - echo FAILED - exit 1 - else - echo ok - return 0 - fi -} check_equal() { - echo - echo "check a:" "{$1}" - echo " b:" "{$2}" + test_debug 'echo' + test_debug "echo \"check a:\" \"{$1}\"" + test_debug "echo \" b:\" \"{$2}\"" if [ "$1" = "$2" ]; then return 0 else - echo FAILED - exit 1 + return 1 fi } @@ -76,144 +58,257 @@ last_commit_message() git log --pretty=format:%s -1 } -rm -rf mainline subproj -mkdir mainline subproj +# 1 +test_expect_success 'init subproj' ' + test_create_repo subproj +' +# To the subproject! cd subproj -git init -create sub1 -git commit -m 'sub1' -git branch sub1 -git branch -m master subproj -check true +# 2 +test_expect_success 'add sub1' ' + create sub1 && + git commit -m "sub1" && + git branch sub1 && + git branch -m master subproj +' -create sub2 -git commit -m 'sub2' -git branch sub2 +# 3 +test_expect_success 'add sub2' ' + create sub2 && + git commit -m "sub2" && + git branch sub2 +' -create sub3 -git commit -m 'sub3' -git branch sub3 +# 4 +test_expect_success 'add sub3' ' + create sub3 && + git commit -m "sub3" && + git branch sub3 +' -cd ../mainline -git init -create main4 -git commit -m 'main4' -git branch -m master mainline -git branch subdir +# Back to mainline +cd .. -git fetch ../subproj sub1 -git branch sub1 FETCH_HEAD +# 5 +test_expect_success 'add main4' ' + create main4 && + git commit -m "main4" && + git branch -m master mainline && + git branch subdir +' -# check if --message works for add -check_not git subtree merge --prefix=subdir sub1 -check_not git subtree pull --prefix=subdir ../subproj sub1 -git subtree add --prefix=subdir --message="Added subproject" sub1 -check_equal "$(last_commit_message)" "Added subproject" -undo +# 6 +test_expect_success 'fetch subproj history' ' + git fetch ./subproj sub1 && + git branch sub1 FETCH_HEAD +' -# check if --message works as -m and --prefix as -P -git subtree add -P subdir -m "Added subproject using git subtree" sub1 -check_equal "$(last_commit_message)" "Added subproject using git subtree" -undo +# 7 +test_expect_success 'no subtree exists in main tree' ' + test_must_fail git subtree merge --prefix=subdir sub1 +' -# check if --message works with squash too -git subtree add -P subdir -m "Added subproject with squash" --squash sub1 -check_equal "$(last_commit_message)" "Added subproject with squash" -undo +# 8 +test_expect_success 'no pull from non-existant subtree' ' + test_must_fail git subtree pull --prefix=subdir ./subproj sub1 +' -git subtree add --prefix=subdir/ FETCH_HEAD -check_equal "$(last_commit_message)" "Add 'subdir/' from commit '$(git rev-parse sub1)'" +# 9 +test_expect_success 'check if --message works for add' ' + git subtree add --prefix=subdir --message="Added subproject" sub1 && + check_equal ''"$(last_commit_message)"'' "Added subproject" && + undo +' +# 10 +test_expect_success 'check if --message works as -m and --prefix as -P' ' + git subtree add -P subdir -m "Added subproject using git subtree" sub1 && + check_equal ''"$(last_commit_message)"'' "Added subproject using git subtree" && + undo +' + +# 11 +test_expect_success 'check if --message works with squash too' ' + git subtree add -P subdir -m "Added subproject with squash" --squash sub1 && + check_equal ''"$(last_commit_message)"'' "Added subproject with squash" && + undo +' + +# 12 +test_expect_success 'add subproj to mainline' ' + git subtree add --prefix=subdir/ FETCH_HEAD && + check_equal ''"$(last_commit_message)"'' "Add '"'subdir/'"' from commit '"'"'''"$(git rev-parse sub1)"'''"'"'" +' + +# 13 # this shouldn't actually do anything, since FETCH_HEAD is already a parent -git merge -m 'merge -s -ours' -s ours FETCH_HEAD +test_expect_success 'merge fetched subproj' ' + git merge -m "merge -s -ours" -s ours FETCH_HEAD +' -create subdir/main-sub5 -git commit -m 'main-sub5' +# 14 +test_expect_success 'add main-sub5' ' + create subdir/main-sub5 && + git commit -m "main-sub5" +' -create main6 -git commit -m 'main6 boring' +# 15 +test_expect_success 'add main6' ' + create main6 && + git commit -m "main6 boring" +' -create subdir/main-sub7 -git commit -m 'main-sub7' +# 16 +test_expect_success 'add main-sub7' ' + create subdir/main-sub7 && + git commit -m "main-sub7" +' -git fetch ../subproj sub2 -git branch sub2 FETCH_HEAD +# 17 +test_expect_success 'fetch new subproj history' ' + git fetch ./subproj sub2 && + git branch sub2 FETCH_HEAD +' -# check if --message works for merge -git subtree merge --prefix=subdir -m "Merged changes from subproject" sub2 -check_equal "$(last_commit_message)" "Merged changes from subproject" -undo +# 18 +test_expect_success 'check if --message works for merge' ' + git subtree merge --prefix=subdir -m "Merged changes from subproject" sub2 && + check_equal ''"$(last_commit_message)"'' "Merged changes from subproject" && + undo +' -# check if --message for merge works with squash too -git subtree merge --prefix subdir -m "Merged changes from subproject using squash" --squash sub2 -check_equal "$(last_commit_message)" "Merged changes from subproject using squash" -undo +# 19 +test_expect_success 'check if --message for merge works with squash too' ' + git subtree merge --prefix subdir -m "Merged changes from subproject using squash" --squash sub2 && + check_equal ''"$(last_commit_message)"'' "Merged changes from subproject using squash" && + undo +' -git subtree merge --prefix=subdir FETCH_HEAD -git branch pre-split -check_equal "$(last_commit_message)" "Merge commit '$(git rev-parse sub2)' into mainline" +# 20 +test_expect_success 'merge new subproj history into subdir' ' + git subtree merge --prefix=subdir FETCH_HEAD && + git branch pre-split && + check_equal ''"$(last_commit_message)"'' "Merge commit '"'"'"$(git rev-parse sub2)"'"'"' into mainline" +' -# Check that prefix argument is required for split (exits with warning and exit status = 1) -! result=$(git subtree split 2>&1) -check_equal "You must provide the --prefix option." "$result" +# 21 +test_expect_success 'Check that prefix argument is required for split' ' + echo "You must provide the --prefix option." > expected && + test_must_fail git subtree split > actual 2>&1 && + test_debug "echo -n expected: " && + test_debug "cat expected" && + test_debug "echo -n actual: " && + test_debug "cat actual" && + test_cmp expected actual && + rm -f expected actual +' -# Check that the exists for a split. -! result=$(git subtree split --prefix=non-existent-directory 2>&1) -check_equal "'non-existent-directory' does not exist; use 'git subtree add'" \ - "$result" +# 22 +test_expect_success 'Check that the exists for a split' ' + echo "'"'"'non-existent-directory'"'"'" does not exist\; use "'"'"'git subtree add'"'"'" > expected && + test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 && + test_debug "echo -n expected: " && + test_debug "cat expected" && + test_debug "echo -n actual: " && + test_debug "cat actual" && + test_cmp expected actual +# rm -f expected actual +' -# check if --message works for split+rejoin -spl1=$(git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin) -echo "spl1={$spl1}" -git branch spl1 "$spl1" -check_equal "$(last_commit_message)" "Split & rejoin" -undo +# 23 +test_expect_success 'check if --message works for split+rejoin' ' + spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' && + git branch spl1 "$spl1" && + check_equal ''"$(last_commit_message)"'' "Split & rejoin" && + undo +' -# check split with --branch -git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --branch splitbr1 -check_equal "$(git rev-parse splitbr1)" "$spl1" +# 24 +test_expect_success 'check split with --branch' ' + spl1=$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin) && + undo && + git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr1 && + check_equal ''"$(git rev-parse splitbr1)"'' "$spl1" +' -# check split with --branch for an existing branch -git branch splitbr2 sub1 -git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --branch splitbr2 -check_equal "$(git rev-parse splitbr2)" "$spl1" +# 25 +test_expect_success 'check split with --branch for an existing branch' ' + spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' && + undo && + git branch splitbr2 sub1 && + git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr2 && + check_equal ''"$(git rev-parse splitbr2)"'' "$spl1" +' -# check split with --branch for an incompatible branch -result=$(git subtree split --prefix subdir --onto FETCH_HEAD --branch subdir || echo "caught error") -check_equal "$result" "caught error" +# 26 +test_expect_success 'check split with --branch for an incompatible branch' ' + test_must_fail git subtree split --prefix subdir --onto FETCH_HEAD --branch subdir +' -git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --rejoin -check_equal "$(last_commit_message)" "Split 'subdir/' into commit '$spl1'" +# 27 +test_expect_success 'check split+rejoin' ' + spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' && + undo && + git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --rejoin && + check_equal ''"$(last_commit_message)"'' "Split '"'"'subdir/'"'"' into commit '"'"'"$spl1"'"'"'" +' -create subdir/main-sub8 -git commit -m 'main-sub8' +# 28 +test_expect_success 'add main-sub8' ' + create subdir/main-sub8 && + git commit -m "main-sub8" +' -cd ../subproj -git fetch ../mainline spl1 -git branch spl1 FETCH_HEAD -git merge FETCH_HEAD +# To the subproject! +cd ./subproj -create sub9 -git commit -m 'sub9' +# 29 +test_expect_success 'merge split into subproj' ' + git fetch .. spl1 && + git branch spl1 FETCH_HEAD && + git merge FETCH_HEAD +' -cd ../mainline -split2=$(git subtree split --annotate='*' --prefix subdir/ --rejoin) -git branch split2 "$split2" +# 30 +test_expect_success 'add sub9' ' + create sub9 && + git commit -m "sub9" +' -create subdir/main-sub10 -git commit -m 'main-sub10' +# Back to mainline +cd .. -spl3=$(git subtree split --annotate='*' --prefix subdir --rejoin) -git branch spl3 "$spl3" +# 31 +test_expect_success 'split for sub8' ' + split2=''"$(git subtree split --annotate='"'*'"' --prefix subdir/ --rejoin)"'' + git branch split2 "$split2" +' -cd ../subproj -git fetch ../mainline spl3 -git branch spl3 FETCH_HEAD -git merge FETCH_HEAD -git branch subproj-merge-spl3 +# 32 +test_expect_success 'add main-sub10' ' + create subdir/main-sub10 && + git commit -m "main-sub10" +' + +# 33 +test_expect_success 'split for sub10' ' + spl3=''"$(git subtree split --annotate='"'*'"' --prefix subdir --rejoin)"'' && + git branch spl3 "$spl3" +' + +# To the subproject! +cd ./subproj + +# 34 +test_expect_success 'merge split into subproj' ' + git fetch .. spl3 && + git branch spl3 FETCH_HEAD && + git merge FETCH_HEAD && + git branch subproj-merge-spl3 +' chkm="main4 main6" chkms="main-sub10 main-sub5 main-sub7 main-sub8" @@ -221,89 +316,150 @@ chkms_sub=$(echo $chkms | multiline | sed 's,^,subdir/,' | fixnl) chks="sub1 sub2 sub3 sub9" chks_sub=$(echo $chks | multiline | sed 's,^,subdir/,' | fixnl) -# make sure exactly the right set of files ends up in the subproj -subfiles=$(git ls-files | fixnl) -check_equal "$subfiles" "$chkms $chks" +# 35 +test_expect_success 'make sure exactly the right set of files ends up in the subproj' ' + subfiles=''"$(git ls-files | fixnl)"'' && + check_equal "$subfiles" "$chkms $chks" +' -# make sure the subproj history *only* contains commits that affect the subdir. -allchanges=$(git log --name-only --pretty=format:'' | sort | fixnl) -check_equal "$allchanges" "$chkms $chks" +# 36 +test_expect_success 'make sure the subproj history *only* contains commits that affect the subdir' ' + allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' && + check_equal "$allchanges" "$chkms $chks" +' -cd ../mainline -git fetch ../subproj subproj-merge-spl3 -git branch subproj-merge-spl3 FETCH_HEAD -git subtree pull --prefix=subdir ../subproj subproj-merge-spl3 +# Back to mainline +cd .. -# make sure exactly the right set of files ends up in the mainline -mainfiles=$(git ls-files | fixnl) -check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub" +# 37 +test_expect_success 'pull from subproj' ' + git fetch ./subproj subproj-merge-spl3 && + git branch subproj-merge-spl3 FETCH_HEAD && + git subtree pull --prefix=subdir ./subproj subproj-merge-spl3 +' -# make sure each filename changed exactly once in the entire history. -# 'main-sub??' and '/subdir/main-sub??' both change, because those are the -# changes that were split into their own history. And 'subdir/sub??' never -# change, since they were *only* changed in the subtree branch. -allchanges=$(git log --name-only --pretty=format:'' | sort | fixnl) -check_equal "$allchanges" "$(echo $chkms $chkm $chks $chkms_sub | multiline | sort | fixnl)" +# 38 +test_expect_success 'make sure exactly the right set of files ends up in the mainline' ' + mainfiles=''"$(git ls-files | fixnl)"'' && + check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub" +' -# make sure the --rejoin commits never make it into subproj -check_equal "$(git log --pretty=format:'%s' HEAD^2 | grep -i split)" "" +# 39 +test_expect_success 'make sure each filename changed exactly once in the entire history' ' + # main-sub?? and /subdir/main-sub?? both change, because those are the + # changes that were split into their own history. And subdir/sub?? never + # change, since they were *only* changed in the subtree branch. + allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' && + check_equal "$allchanges" ''"$(echo $chkms $chkm $chks $chkms_sub | multiline | sort | fixnl)"'' +' -# make sure no 'git subtree' tagged commits make it into subproj. (They're -# meaningless to subproj since one side of the merge refers to the mainline) -check_equal "$(git log --pretty=format:'%s%n%b' HEAD^2 | grep 'git-subtree.*:')" "" +# 40 +test_expect_success 'make sure the --rejoin commits never make it into subproj' ' + check_equal ''"$(git log --pretty=format:'"'%s'"' HEAD^2 | grep -i split)"'' "" +' +# 41 +test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' ' + # They are meaningless to subproj since one side of the merge refers to the mainline + check_equal ''"$(git log --pretty=format:'"'%s%n%b'"' HEAD^2 | grep "git-subtree.*:")"'' "" +' -# check if split can find proper base without --onto # prepare second pair of repositories mkdir test2 cd test2 -mkdir main +# 42 +test_expect_success 'init main' ' + test_create_repo main +' + cd main -git init -create main1 -git commit -m "main1" + +# 43 +test_expect_success 'add main1' ' + create main1 && + git commit -m "main1" +' cd .. -mkdir sub + +# 44 +test_expect_success 'init sub' ' + test_create_repo sub +' + cd sub -git init -create sub2 -git commit -m "sub2" + +# 45 +test_expect_success 'add sub2' ' + create sub2 && + git commit -m "sub2" +' cd ../main -git fetch ../sub master -git branch sub2 FETCH_HEAD -git subtree add --prefix subdir sub2 + +# check if split can find proper base without --onto + +# 46 +test_expect_success 'add sub as subdir in main' ' + git fetch ../sub master && + git branch sub2 FETCH_HEAD && + git subtree add --prefix subdir sub2 +' cd ../sub -create sub3 -git commit -m "sub3" + +# 47 +test_expect_success 'add sub3' ' + create sub3 && + git commit -m "sub3" +' cd ../main -git fetch ../sub master -git branch sub3 FETCH_HEAD -git subtree merge --prefix subdir sub3 -create subdir/main-sub4 -git commit -m "main-sub4" -git subtree split --prefix subdir --branch mainsub4 +# 48 +test_expect_success 'merge from sub' ' + git fetch ../sub master && + git branch sub3 FETCH_HEAD && + git subtree merge --prefix subdir sub3 +' -# at this point, the new commit's parent should be sub3 -# if it's not, something went wrong (the "newparent" of "master~" commit should have been sub3, -# but it wasn't, because it's cache was not set to itself) -check_equal "$(git log --pretty=format:%P -1 mainsub4)" "$(git rev-parse sub3)" +# 49 +test_expect_success 'add main-sub4' ' + create subdir/main-sub4 && + git commit -m "main-sub4" +' -mkdir subdir2 -create subdir2/main-sub5 -git commit -m "main-sub5" -git subtree split --prefix subdir2 --branch mainsub5 +# 50 +test_expect_success 'split for main-sub4 without --onto' ' + git subtree split --prefix subdir --branch mainsub4 +' -# also test that we still can split out an entirely new subtree -# if the parent of the first commit in the tree isn't empty, -# then the new subtree has accidently been attached to something -check_equal "$(git log --pretty=format:%P -1 mainsub5)" "" +# at this point, the new commit parent should be sub3 if it is not, +# something went wrong (the "newparent" of "master~" commit should +# have been sub3, but it was not, because its cache was not set to +# itself) +# 51 +test_expect_success 'check that the commit parent is sub3' ' + check_equal ''"$(git log --pretty=format:%P -1 mainsub4)"'' ''"$(git rev-parse sub3)"'' +' + +# 52 +test_expect_success 'add main-sub5' ' + mkdir subdir2 && + create subdir2/main-sub5 && + git commit -m "main-sub5" +' + +# 53 +test_expect_success 'split for main-sub5 without --onto' ' + # also test that we still can split out an entirely new subtree + # if the parent of the first commit in the tree is not empty, + # then the new subtree has accidently been attached to something + git subtree split --prefix subdir2 --branch mainsub5 && + check_equal ''"$(git log --pretty=format:%P -1 mainsub5)"'' "" +' # make sure no patch changes more than one file. The original set of commits # changed only one file each. A multi-file change would imply that we pruned @@ -313,7 +469,7 @@ joincommits() commit= all= while read x y; do - echo "{$x}" >&2 + #echo "{$x}" >&2 if [ -z "$x" ]; then continue elif [ "$x" = "commit:" ]; then @@ -328,15 +484,23 @@ joincommits() done echo "$commit $all" } -x= -git log --pretty=format:'commit: %H' | joincommits | -( while read commit a b; do - echo "Verifying commit $commit" - check_equal "$b" "" - x=1 - done - check_equal "$x" 1 -) || exit 1 -echo -echo 'ok' +# 54 +test_expect_success 'verify one file change per commit' ' + x= && + list=''"$(git log --pretty=format:'"'commit: %H'"' | joincommits)"'' && +# test_debug "echo HERE" && +# test_debug "echo ''"$list"''" && + (git log --pretty=format:'"'commit: %H'"' | joincommits | + ( while read commit a b; do + test_debug "echo Verifying commit "''"$commit"'' + test_debug "echo a: "''"$a"'' + test_debug "echo b: "''"$b"'' + check_equal "$b" "" + x=1 + done + check_equal "$x" 1 + )) +' + +test_done