Code

Only copy a commit if it has at least one nonidentical parent.
[git.git] / git-subtree.sh
1 #!/bin/bash
2 #
3 # git-subtree.sh: split/join git repositories in subdirectories of this one
4 #
5 # Copyright (C) 2009 Avery Pennarun <apenwarr@gmail.com>
6 #
7 if [ $# -eq 0 ]; then
8     set -- -h
9 fi
10 OPTS_SPEC="\
11 git subtree add --prefix=<prefix> <commit>
12 git subtree split [options...] --prefix=<prefix> <commit...>
13 git subtree merge --prefix=<prefix> <commit>
14 git subtree pull  --prefix=<prefix> <repository> <refspec...>
15 --
16 h,help        show the help
17 q             quiet
18 prefix=       the name of the subdir to split out
19  options for 'split'
20 annotate=     add a prefix to commit message of new commits
21 onto=         try connecting new tree to an existing one
22 rejoin        merge the new branch back into HEAD
23 ignore-joins  ignore prior --rejoin commits
24 "
25 eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)
26 . git-sh-setup
27 require_work_tree
29 quiet=
30 command=
31 onto=
32 rejoin=
33 ignore_joins=
34 annotate=
36 debug()
37 {
38         if [ -z "$quiet" ]; then
39                 echo "$@" >&2
40         fi
41 }
43 assert()
44 {
45         if "$@"; then
46                 :
47         else
48                 die "assertion failed: " "$@"
49         fi
50 }
53 #echo "Options: $*"
55 while [ $# -gt 0 ]; do
56         opt="$1"
57         shift
58         case "$opt" in
59                 -q) quiet=1 ;;
60                 --annotate) annotate="$1"; shift ;;
61                 --no-annotate) annotate= ;;
62                 --prefix) prefix="$1"; shift ;;
63                 --no-prefix) prefix= ;;
64                 --onto) onto="$1"; shift ;;
65                 --no-onto) onto= ;;
66                 --rejoin) rejoin=1 ;;
67                 --no-rejoin) rejoin= ;;
68                 --ignore-joins) ignore_joins=1 ;;
69                 --no-ignore-joins) ignore_joins= ;;
70                 --) break ;;
71         esac
72 done
74 command="$1"
75 shift
76 case "$command" in
77         add|merge|pull) default= ;;
78         split) default="--default HEAD" ;;
79         *) die "Unknown command '$command'" ;;
80 esac
82 if [ -z "$prefix" ]; then
83         die "You must provide the --prefix option."
84 fi
85 dir="$prefix"
87 if [ "$command" != "pull" ]; then
88         revs=$(git rev-parse $default --revs-only "$@") || exit $?
89         dirs="$(git rev-parse --no-revs --no-flags "$@")" || exit $?
90         if [ -n "$dirs" ]; then
91                 die "Error: Use --prefix instead of bare filenames."
92         fi
93 fi
95 debug "command: {$command}"
96 debug "quiet: {$quiet}"
97 debug "revs: {$revs}"
98 debug "dir: {$dir}"
99 debug "opts: {$*}"
100 debug
102 cache_setup()
104         cachedir="$GIT_DIR/subtree-cache/$$"
105         rm -rf "$cachedir" || die "Can't delete old cachedir: $cachedir"
106         mkdir -p "$cachedir" || die "Can't create new cachedir: $cachedir"
107         debug "Using cachedir: $cachedir" >&2
110 cache_get()
112         for oldrev in $*; do
113                 if [ -r "$cachedir/$oldrev" ]; then
114                         read newrev <"$cachedir/$oldrev"
115                         echo $newrev
116                 fi
117         done
120 cache_set()
122         oldrev="$1"
123         newrev="$2"
124         if [ "$oldrev" != "latest_old" \
125              -a "$oldrev" != "latest_new" \
126              -a -e "$cachedir/$oldrev" ]; then
127                 die "cache for $oldrev already exists!"
128         fi
129         echo "$newrev" >"$cachedir/$oldrev"
132 # if a commit doesn't have a parent, this might not work.  But we only want
133 # to remove the parent from the rev-list, and since it doesn't exist, it won't
134 # be there anyway, so do nothing in that case.
135 try_remove_previous()
137         if git rev-parse "$1^" >/dev/null 2>&1; then
138                 echo "^$1^"
139         fi
142 find_existing_splits()
144         debug "Looking for prior splits..."
145         dir="$1"
146         revs="$2"
147         git log --grep="^git-subtree-dir: $dir\$" \
148                 --pretty=format:'%s%n%n%b%nEND' $revs |
149         while read a b junk; do
150                 case "$a" in
151                         git-subtree-mainline:) main="$b" ;;
152                         git-subtree-split:) sub="$b" ;;
153                         *)
154                                 if [ -n "$main" -a -n "$sub" ]; then
155                                         debug "  Prior: $main -> $sub"
156                                         cache_set $main $sub
157                                         try_remove_previous "$main"
158                                         try_remove_previous "$sub"
159                                         main=
160                                         sub=
161                                 fi
162                                 ;;
163                 esac
164         done
167 copy_commit()
169         # We're doing to set some environment vars here, so
170         # do it in a subshell to get rid of them safely later
171         debug copy_commit "{$1}" "{$2}" "{$3}"
172         git log -1 --pretty=format:'%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%s%n%n%b' "$1" |
173         (
174                 read GIT_AUTHOR_NAME
175                 read GIT_AUTHOR_EMAIL
176                 read GIT_AUTHOR_DATE
177                 read GIT_COMMITTER_NAME
178                 read GIT_COMMITTER_EMAIL
179                 read GIT_COMMITTER_DATE
180                 export  GIT_AUTHOR_NAME \
181                         GIT_AUTHOR_EMAIL \
182                         GIT_AUTHOR_DATE \
183                         GIT_COMMITTER_NAME \
184                         GIT_COMMITTER_EMAIL \
185                         GIT_COMMITTER_DATE
186                 (echo -n "$annotate"; cat ) |
187                 git commit-tree "$2" $3  # reads the rest of stdin
188         ) || die "Can't copy commit $1"
191 add_msg()
193         dir="$1"
194         latest_old="$2"
195         latest_new="$3"
196         cat <<-EOF
197                 Add '$dir/' from commit '$latest_new'
198                 
199                 git-subtree-dir: $dir
200                 git-subtree-mainline: $latest_old
201                 git-subtree-split: $latest_new
202         EOF
205 merge_msg()
207         dir="$1"
208         latest_old="$2"
209         latest_new="$3"
210         cat <<-EOF
211                 Split '$dir/' into commit '$latest_new'
212                 
213                 git-subtree-dir: $dir
214                 git-subtree-mainline: $latest_old
215                 git-subtree-split: $latest_new
216         EOF
219 toptree_for_commit()
221         commit="$1"
222         git log -1 --pretty=format:'%T' "$commit" -- || exit $?
225 subtree_for_commit()
227         commit="$1"
228         dir="$2"
229         git ls-tree "$commit" -- "$dir" |
230         while read mode type tree name; do
231                 assert [ "$name" = "$dir" ]
232                 echo $tree
233                 break
234         done
237 tree_changed()
239         tree=$1
240         shift
241         if [ $# -ne 1 ]; then
242                 return 0   # weird parents, consider it changed
243         else
244                 ptree=$(toptree_for_commit $1)
245                 if [ "$ptree" != "$tree" ]; then
246                         return 0   # changed
247                 else
248                         return 1   # not changed
249                 fi
250         fi
253 copy_or_skip()
255         rev="$1"
256         tree="$2"
257         newparents="$3"
258         assert [ -n "$tree" ]
260         identical=
261         nonidentical=
262         p=
263         gotparents=
264         for parent in $newparents; do
265                 ptree=$(toptree_for_commit $parent) || exit $?
266                 [ -z "$ptree" ] && continue
267                 if [ "$ptree" = "$tree" ]; then
268                         # an identical parent could be used in place of this rev.
269                         identical="$parent"
270                 else
271                         nonidentical="$parent"
272                 fi
273                 
274                 # sometimes both old parents map to the same newparent;
275                 # eliminate duplicates
276                 is_new=1
277                 for gp in $gotparents; do
278                         if [ "$gp" = "$parent" ]; then
279                                 is_new=
280                                 break
281                         fi
282                 done
283                 if [ -n "$is_new" ]; then
284                         gotparents="$gotparents $parent"
285                         p="$p -p $parent"
286                 fi
287         done
288         
289         if [ -n "$identical" -a -z "$nonidentical" ]; then
290                 echo $identical
291         else
292                 copy_commit $rev $tree "$p" || exit $?
293         fi
296 ensure_clean()
298         if ! git diff-index HEAD --exit-code --quiet; then
299                 die "Working tree has modifications.  Cannot add."
300         fi
301         if ! git diff-index --cached HEAD --exit-code --quiet; then
302                 die "Index has modifications.  Cannot add."
303         fi
306 cmd_add()
308         if [ -e "$dir" ]; then
309                 die "'$dir' already exists.  Cannot add."
310         fi
311         ensure_clean
312         
313         set -- $revs
314         if [ $# -ne 1 ]; then
315                 die "You must provide exactly one revision.  Got: '$revs'"
316         fi
317         rev="$1"
318         
319         debug "Adding $dir as '$rev'..."
320         git read-tree --prefix="$dir" $rev || exit $?
321         git checkout "$dir" || exit $?
322         tree=$(git write-tree) || exit $?
323         
324         headrev=$(git rev-parse HEAD) || exit $?
325         if [ -n "$headrev" -a "$headrev" != "$rev" ]; then
326                 headp="-p $headrev"
327         else
328                 headp=
329         fi
330         commit=$(add_msg "$dir" "$headrev" "$rev" |
331                  git commit-tree $tree $headp -p "$rev") || exit $?
332         git reset "$commit" || exit $?
335 cmd_split()
337         debug "Splitting $dir..."
338         cache_setup || exit $?
339         
340         if [ -n "$onto" ]; then
341                 debug "Reading history for --onto=$onto..."
342                 git rev-list $onto |
343                 while read rev; do
344                         # the 'onto' history is already just the subdir, so
345                         # any parent we find there can be used verbatim
346                         debug "  cache: $rev"
347                         cache_set $rev $rev
348                 done
349         fi
350         
351         if [ -n "$ignore_joins" ]; then
352                 unrevs=
353         else
354                 unrevs="$(find_existing_splits "$dir" "$revs")"
355         fi
356         
357         # We can't restrict rev-list to only $dir here, because some of our
358         # parents have the $dir contents the root, and those won't match.
359         # (and rev-list --follow doesn't seem to solve this)
360         git rev-list --reverse --parents $revs $unrevs |
361         while read rev parents; do
362                 debug
363                 debug "Processing commit: $rev"
364                 exists=$(cache_get $rev)
365                 if [ -n "$exists" ]; then
366                         debug "  prior: $exists"
367                         continue
368                 fi
369                 debug "  parents: $parents"
370                 newparents=$(cache_get $parents)
371                 debug "  newparents: $newparents"
372                 
373                 tree=$(subtree_for_commit $rev "$dir")
374                 debug "  tree is: $tree"
375                 [ -z $tree ] && continue
377                 newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
378                 debug "  newrev is: $newrev"
379                 cache_set $rev $newrev
380                 cache_set latest_new $newrev
381                 cache_set latest_old $rev
382         done || exit $?
383         latest_new=$(cache_get latest_new)
384         if [ -z "$latest_new" ]; then
385                 die "No new revisions were found"
386         fi
387         
388         if [ -n "$rejoin" ]; then
389                 debug "Merging split branch into HEAD..."
390                 latest_old=$(cache_get latest_old)
391                 git merge -s ours \
392                         -m "$(merge_msg $dir $latest_old $latest_new)" \
393                         $latest_new >&2
394         fi
395         echo $latest_new
396         exit 0
399 cmd_merge()
401         ensure_clean
402         
403         set -- $revs
404         if [ $# -ne 1 ]; then
405                 die "You must provide exactly one revision.  Got: '$revs'"
406         fi
407         rev="$1"
408         
409         git merge -s subtree $rev
412 cmd_pull()
414         ensure_clean
415         set -x
416         git pull -s subtree "$@"
419 "cmd_$command" "$@"