Code

FIXME help for --squash option
[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 merge --prefix=<prefix> <commit>
13 git subtree pull  --prefix=<prefix> <repository> <refspec...>
14 git subtree split --prefix=<prefix> <commit...>
15 --
16 h,help        show the help
17 q             quiet
18 d             show debug messages
19 prefix=       the name of the subdir to split out
20  options for 'split'
21 annotate=     add a prefix to commit message of new commits
22 b,branch=     create a new branch from the split subtree
23 ignore-joins  ignore prior --rejoin commits
24 onto=         try connecting new tree to an existing one
25 rejoin        merge the new branch back into HEAD
26  options for 'merge' and 'pull'
27 squash        merge subtree changes as a single commit
28 "
29 eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)
30 . git-sh-setup
31 require_work_tree
33 quiet=
34 branch=
35 debug=
36 command=
37 onto=
38 rejoin=
39 ignore_joins=
40 annotate=
41 squash=
43 debug()
44 {
45         if [ -n "$debug" ]; then
46                 echo "$@" >&2
47         fi
48 }
50 say()
51 {
52         if [ -z "$quiet" ]; then
53                 echo "$@" >&2
54         fi
55 }
57 assert()
58 {
59         if "$@"; then
60                 :
61         else
62                 die "assertion failed: " "$@"
63         fi
64 }
67 #echo "Options: $*"
69 while [ $# -gt 0 ]; do
70         opt="$1"
71         shift
72         case "$opt" in
73                 -q) quiet=1 ;;
74                 -d) debug=1 ;;
75                 --annotate) annotate="$1"; shift ;;
76                 --no-annotate) annotate= ;;
77                 -b) branch="$1"; shift ;;
78                 --prefix) prefix="$1"; shift ;;
79                 --no-prefix) prefix= ;;
80                 --onto) onto="$1"; shift ;;
81                 --no-onto) onto= ;;
82                 --rejoin) rejoin=1 ;;
83                 --no-rejoin) rejoin= ;;
84                 --ignore-joins) ignore_joins=1 ;;
85                 --no-ignore-joins) ignore_joins= ;;
86                 --squash) squash=1 ;;
87                 --no-squash) squash= ;;
88                 --) break ;;
89                 *) die "Unexpected option: $opt" ;;
90         esac
91 done
93 command="$1"
94 shift
95 case "$command" in
96         add|merge|pull) default= ;;
97         split) default="--default HEAD" ;;
98         *) die "Unknown command '$command'" ;;
99 esac
101 if [ -z "$prefix" ]; then
102         die "You must provide the --prefix option."
103 fi
104 dir="$prefix"
106 if [ "$command" != "pull" ]; then
107         revs=$(git rev-parse $default --revs-only "$@") || exit $?
108         dirs="$(git rev-parse --no-revs --no-flags "$@")" || exit $?
109         if [ -n "$dirs" ]; then
110                 die "Error: Use --prefix instead of bare filenames."
111         fi
112 fi
114 debug "command: {$command}"
115 debug "quiet: {$quiet}"
116 debug "revs: {$revs}"
117 debug "dir: {$dir}"
118 debug "opts: {$*}"
119 debug
121 cache_setup()
123         cachedir="$GIT_DIR/subtree-cache/$$"
124         rm -rf "$cachedir" || die "Can't delete old cachedir: $cachedir"
125         mkdir -p "$cachedir" || die "Can't create new cachedir: $cachedir"
126         debug "Using cachedir: $cachedir" >&2
129 cache_get()
131         for oldrev in $*; do
132                 if [ -r "$cachedir/$oldrev" ]; then
133                         read newrev <"$cachedir/$oldrev"
134                         echo $newrev
135                 fi
136         done
139 cache_set()
141         oldrev="$1"
142         newrev="$2"
143         if [ "$oldrev" != "latest_old" \
144              -a "$oldrev" != "latest_new" \
145              -a -e "$cachedir/$oldrev" ]; then
146                 die "cache for $oldrev already exists!"
147         fi
148         echo "$newrev" >"$cachedir/$oldrev"
151 rev_exists()
153         if git rev-parse "$1" >/dev/null 2>&1; then
154                 return 0
155         else
156                 return 1
157         fi
160 # if a commit doesn't have a parent, this might not work.  But we only want
161 # to remove the parent from the rev-list, and since it doesn't exist, it won't
162 # be there anyway, so do nothing in that case.
163 try_remove_previous()
165         if rev_exists "$1^"; then
166                 echo "^$1^"
167         fi
170 find_existing_splits()
172         debug "Looking for prior splits..."
173         dir="$1"
174         revs="$2"
175         git log --grep="^git-subtree-dir: $dir\$" \
176                 --pretty=format:'%s%n%n%b%nEND' $revs |
177         while read a b junk; do
178                 case "$a" in
179                         git-subtree-mainline:) main="$b" ;;
180                         git-subtree-split:) sub="$b" ;;
181                         *)
182                                 if [ -n "$main" -a -n "$sub" ]; then
183                                         debug "  Prior: $main -> $sub"
184                                         cache_set $main $sub
185                                         try_remove_previous "$main"
186                                         try_remove_previous "$sub"
187                                         main=
188                                         sub=
189                                 fi
190                                 ;;
191                 esac
192         done
195 copy_commit()
197         # We're going to set some environment vars here, so
198         # do it in a subshell to get rid of them safely later
199         debug copy_commit "{$1}" "{$2}" "{$3}"
200         git log -1 --pretty=format:'%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%s%n%n%b' "$1" |
201         (
202                 read GIT_AUTHOR_NAME
203                 read GIT_AUTHOR_EMAIL
204                 read GIT_AUTHOR_DATE
205                 read GIT_COMMITTER_NAME
206                 read GIT_COMMITTER_EMAIL
207                 read GIT_COMMITTER_DATE
208                 export  GIT_AUTHOR_NAME \
209                         GIT_AUTHOR_EMAIL \
210                         GIT_AUTHOR_DATE \
211                         GIT_COMMITTER_NAME \
212                         GIT_COMMITTER_EMAIL \
213                         GIT_COMMITTER_DATE
214                 (echo -n "$annotate"; cat ) |
215                 git commit-tree "$2" $3  # reads the rest of stdin
216         ) || die "Can't copy commit $1"
219 add_msg()
221         dir="$1"
222         latest_old="$2"
223         latest_new="$3"
224         cat <<-EOF
225                 Add '$dir/' from commit '$latest_new'
226                 
227                 git-subtree-dir: $dir
228                 git-subtree-mainline: $latest_old
229                 git-subtree-split: $latest_new
230         EOF
233 merge_msg()
235         dir="$1"
236         latest_old="$2"
237         latest_new="$3"
238         cat <<-EOF
239                 Split '$dir/' into commit '$latest_new'
240                 
241                 git-subtree-dir: $dir
242                 git-subtree-mainline: $latest_old
243                 git-subtree-split: $latest_new
244         EOF
247 toptree_for_commit()
249         commit="$1"
250         git log -1 --pretty=format:'%T' "$commit" -- || exit $?
253 subtree_for_commit()
255         commit="$1"
256         dir="$2"
257         git ls-tree "$commit" -- "$dir" |
258         while read mode type tree name; do
259                 assert [ "$name" = "$dir" ]
260                 echo $tree
261                 break
262         done
265 tree_changed()
267         tree=$1
268         shift
269         if [ $# -ne 1 ]; then
270                 return 0   # weird parents, consider it changed
271         else
272                 ptree=$(toptree_for_commit $1)
273                 if [ "$ptree" != "$tree" ]; then
274                         return 0   # changed
275                 else
276                         return 1   # not changed
277                 fi
278         fi
281 copy_or_skip()
283         rev="$1"
284         tree="$2"
285         newparents="$3"
286         assert [ -n "$tree" ]
288         identical=
289         nonidentical=
290         p=
291         gotparents=
292         for parent in $newparents; do
293                 ptree=$(toptree_for_commit $parent) || exit $?
294                 [ -z "$ptree" ] && continue
295                 if [ "$ptree" = "$tree" ]; then
296                         # an identical parent could be used in place of this rev.
297                         identical="$parent"
298                 else
299                         nonidentical="$parent"
300                 fi
301                 
302                 # sometimes both old parents map to the same newparent;
303                 # eliminate duplicates
304                 is_new=1
305                 for gp in $gotparents; do
306                         if [ "$gp" = "$parent" ]; then
307                                 is_new=
308                                 break
309                         fi
310                 done
311                 if [ -n "$is_new" ]; then
312                         gotparents="$gotparents $parent"
313                         p="$p -p $parent"
314                 fi
315         done
316         
317         if [ -n "$identical" ]; then
318                 echo $identical
319         else
320                 copy_commit $rev $tree "$p" || exit $?
321         fi
324 ensure_clean()
326         if ! git diff-index HEAD --exit-code --quiet; then
327                 die "Working tree has modifications.  Cannot add."
328         fi
329         if ! git diff-index --cached HEAD --exit-code --quiet; then
330                 die "Index has modifications.  Cannot add."
331         fi
334 cmd_add()
336         if [ -e "$dir" ]; then
337                 die "'$dir' already exists.  Cannot add."
338         fi
339         ensure_clean
340         
341         set -- $revs
342         if [ $# -ne 1 ]; then
343                 die "You must provide exactly one revision.  Got: '$revs'"
344         fi
345         rev="$1"
346         
347         debug "Adding $dir as '$rev'..."
348         git read-tree --prefix="$dir" $rev || exit $?
349         git checkout "$dir" || exit $?
350         tree=$(git write-tree) || exit $?
351         
352         headrev=$(git rev-parse HEAD) || exit $?
353         if [ -n "$headrev" -a "$headrev" != "$rev" ]; then
354                 headp="-p $headrev"
355         else
356                 headp=
357         fi
358         commit=$(add_msg "$dir" "$headrev" "$rev" |
359                  git commit-tree $tree $headp -p "$rev") || exit $?
360         git reset "$commit" || exit $?
363 cmd_split()
365         if [ -n "$branch" ] && rev_exists "refs/heads/$branch"; then
366                 die "Branch '$branch' already exists."
367         fi
369         debug "Splitting $dir..."
370         cache_setup || exit $?
371         
372         if [ -n "$onto" ]; then
373                 debug "Reading history for --onto=$onto..."
374                 git rev-list $onto |
375                 while read rev; do
376                         # the 'onto' history is already just the subdir, so
377                         # any parent we find there can be used verbatim
378                         debug "  cache: $rev"
379                         cache_set $rev $rev
380                 done
381         fi
382         
383         if [ -n "$ignore_joins" ]; then
384                 unrevs=
385         else
386                 unrevs="$(find_existing_splits "$dir" "$revs")"
387         fi
388         
389         # We can't restrict rev-list to only $dir here, because some of our
390         # parents have the $dir contents the root, and those won't match.
391         # (and rev-list --follow doesn't seem to solve this)
392         grl='git rev-list --reverse --parents $revs $unrevs'
393         revmax=$(eval "$grl" | wc -l)
394         revcount=0
395         createcount=0
396         eval "$grl" |
397         while read rev parents; do
398                 revcount=$(($revcount + 1))
399                 say -n "$revcount/$revmax ($createcount)\r"
400                 debug "Processing commit: $rev"
401                 exists=$(cache_get $rev)
402                 if [ -n "$exists" ]; then
403                         debug "  prior: $exists"
404                         continue
405                 fi
406                 createcount=$(($createcount + 1))
407                 debug "  parents: $parents"
408                 newparents=$(cache_get $parents)
409                 debug "  newparents: $newparents"
410                 
411                 tree=$(subtree_for_commit $rev "$dir")
412                 debug "  tree is: $tree"
413                 [ -z $tree ] && continue
415                 newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
416                 debug "  newrev is: $newrev"
417                 cache_set $rev $newrev
418                 cache_set latest_new $newrev
419                 cache_set latest_old $rev
420         done || exit $?
421         latest_new=$(cache_get latest_new)
422         if [ -z "$latest_new" ]; then
423                 die "No new revisions were found"
424         fi
425         
426         if [ -n "$rejoin" ]; then
427                 debug "Merging split branch into HEAD..."
428                 latest_old=$(cache_get latest_old)
429                 git merge -s ours \
430                         -m "$(merge_msg $dir $latest_old $latest_new)" \
431                         $latest_new >&2 || exit $?
432         fi
433         if [ -n "$branch" ]; then
434                 git update-ref -m 'subtree split' "refs/heads/$branch" \
435                         $latest_new "" || exit $?
436                 say "Created branch '$branch'"
437         fi
438         echo $latest_new
439         exit 0
442 cmd_merge()
444         ensure_clean
445         
446         set -- $revs
447         if [ $# -ne 1 ]; then
448                 die "You must provide exactly one revision.  Got: '$revs'"
449         fi
450         rev="$1"
451         
452         git merge -s subtree $rev
455 cmd_pull()
457         ensure_clean
458         set -x
459         git pull -s subtree "$@"
462 "cmd_$command" "$@"