f7fe11117869460aad9f572cf0302b5f06ee0563
   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#
   7if [ $# -eq 0 ]; then
   8    set -- -h
   9fi
  10OPTS_SPEC="\
  11git subtree add   --prefix=<prefix> <commit>
  12git subtree merge --prefix=<prefix> <commit>
  13git subtree pull  --prefix=<prefix> <repository> <refspec...>
  14git subtree split --prefix=<prefix> <commit...>
  15--
  16h,help        show the help
  17q             quiet
  18d             show debug messages
  19prefix=       the name of the subdir to split out
  20 options for 'split'
  21annotate=     add a prefix to commit message of new commits
  22b,branch=     create a new branch from the split subtree
  23ignore-joins  ignore prior --rejoin commits
  24onto=         try connecting new tree to an existing one
  25rejoin        merge the new branch back into HEAD
  26 options for 'merge' and 'pull'
  27squash        merge subtree changes as a single commit
  28"
  29eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)
  30. git-sh-setup
  31require_work_tree
  32
  33quiet=
  34branch=
  35debug=
  36command=
  37onto=
  38rejoin=
  39ignore_joins=
  40annotate=
  41squash=
  42
  43debug()
  44{
  45        if [ -n "$debug" ]; then
  46                echo "$@" >&2
  47        fi
  48}
  49
  50say()
  51{
  52        if [ -z "$quiet" ]; then
  53                echo "$@" >&2
  54        fi
  55}
  56
  57assert()
  58{
  59        if "$@"; then
  60                :
  61        else
  62                die "assertion failed: " "$@"
  63        fi
  64}
  65
  66
  67#echo "Options: $*"
  68
  69while [ $# -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
  91done
  92
  93command="$1"
  94shift
  95case "$command" in
  96        add|merge|pull) default= ;;
  97        split) default="--default HEAD" ;;
  98        *) die "Unknown command '$command'" ;;
  99esac
 100
 101if [ -z "$prefix" ]; then
 102        die "You must provide the --prefix option."
 103fi
 104dir="$prefix"
 105
 106if [ "$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
 112fi
 113
 114debug "command: {$command}"
 115debug "quiet: {$quiet}"
 116debug "revs: {$revs}"
 117debug "dir: {$dir}"
 118debug "opts: {$*}"
 119debug
 120
 121cache_setup()
 122{
 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
 127}
 128
 129cache_get()
 130{
 131        for oldrev in $*; do
 132                if [ -r "$cachedir/$oldrev" ]; then
 133                        read newrev <"$cachedir/$oldrev"
 134                        echo $newrev
 135                fi
 136        done
 137}
 138
 139cache_set()
 140{
 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"
 149}
 150
 151rev_exists()
 152{
 153        if git rev-parse "$1" >/dev/null 2>&1; then
 154                return 0
 155        else
 156                return 1
 157        fi
 158}
 159
 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.
 163try_remove_previous()
 164{
 165        if rev_exists "$1^"; then
 166                echo "^$1^"
 167        fi
 168}
 169
 170find_latest_squash()
 171{
 172        debug "Looking for latest squash..."
 173        dir="$1"
 174        git log --grep="^git-subtree-dir: $dir\$" \
 175                --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
 176        while read a b junk; do
 177                case "$a" in
 178                        START) sq="$b" ;;
 179                        git-subtree-mainline:) main="$b" ;;
 180                        git-subtree-split:) sub="$b" ;;
 181                        END)
 182                                if [ -n "$sub" ]; then
 183                                        if [ -n "$main" ]; then
 184                                                # a rejoin commit?
 185                                                # Pretend its sub was a squash.
 186                                                sq="$sub"
 187                                        fi
 188                                        debug "Squash found: $sq $sub"
 189                                        echo "$sq" "$sub"
 190                                        break
 191                                fi
 192                                sq=
 193                                main=
 194                                sub=
 195                                ;;
 196                esac
 197        done
 198}
 199
 200find_existing_splits()
 201{
 202        debug "Looking for prior splits..."
 203        dir="$1"
 204        revs="$2"
 205        git log --grep="^git-subtree-dir: $dir\$" \
 206                --pretty=format:'%s%n%n%b%nEND%n' $revs |
 207        while read a b junk; do
 208                case "$a" in
 209                        START) main="$b" ;;
 210                        git-subtree-mainline:) main="$b" ;;
 211                        git-subtree-split:) sub="$b" ;;
 212                        END)
 213                                if [ -n "$main" -a -n "$sub" ]; then
 214                                        debug "  Prior: $main -> $sub"
 215                                        cache_set $main $sub
 216                                        try_remove_previous "$main"
 217                                        try_remove_previous "$sub"
 218                                fi
 219                                main=
 220                                sub=
 221                                ;;
 222                esac
 223        done
 224}
 225
 226copy_commit()
 227{
 228        # We're going to set some environment vars here, so
 229        # do it in a subshell to get rid of them safely later
 230        debug copy_commit "{$1}" "{$2}" "{$3}"
 231        git log -1 --pretty=format:'%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%s%n%n%b' "$1" |
 232        (
 233                read GIT_AUTHOR_NAME
 234                read GIT_AUTHOR_EMAIL
 235                read GIT_AUTHOR_DATE
 236                read GIT_COMMITTER_NAME
 237                read GIT_COMMITTER_EMAIL
 238                read GIT_COMMITTER_DATE
 239                export  GIT_AUTHOR_NAME \
 240                        GIT_AUTHOR_EMAIL \
 241                        GIT_AUTHOR_DATE \
 242                        GIT_COMMITTER_NAME \
 243                        GIT_COMMITTER_EMAIL \
 244                        GIT_COMMITTER_DATE
 245                (echo -n "$annotate"; cat ) |
 246                git commit-tree "$2" $3  # reads the rest of stdin
 247        ) || die "Can't copy commit $1"
 248}
 249
 250add_msg()
 251{
 252        dir="$1"
 253        latest_old="$2"
 254        latest_new="$3"
 255        cat <<-EOF
 256                Add '$dir/' from commit '$latest_new'
 257                
 258                git-subtree-dir: $dir
 259                git-subtree-mainline: $latest_old
 260                git-subtree-split: $latest_new
 261        EOF
 262}
 263
 264rejoin_msg()
 265{
 266        dir="$1"
 267        latest_old="$2"
 268        latest_new="$3"
 269        cat <<-EOF
 270                Split '$dir/' into commit '$latest_new'
 271                
 272                git-subtree-dir: $dir
 273                git-subtree-mainline: $latest_old
 274                git-subtree-split: $latest_new
 275        EOF
 276}
 277
 278squash_msg()
 279{
 280        dir="$1"
 281        oldsub="$2"
 282        newsub="$3"
 283        oldsub_short=$(git rev-parse --short "$oldsub")
 284        newsub_short=$(git rev-parse --short "$newsub")
 285        cat <<-EOF
 286                Squashed '$dir/' changes from $oldsub_short..$newsub_short
 287        
 288        EOF
 289        
 290        git log --pretty=tformat:'%h %s' "$oldsub..$newsub"
 291        git log --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
 292        
 293        cat <<-EOF
 294                
 295                git-subtree-dir: $dir
 296                git-subtree-split: $newsub
 297        EOF
 298}
 299
 300toptree_for_commit()
 301{
 302        commit="$1"
 303        git log -1 --pretty=format:'%T' "$commit" -- || exit $?
 304}
 305
 306subtree_for_commit()
 307{
 308        commit="$1"
 309        dir="$2"
 310        git ls-tree "$commit" -- "$dir" |
 311        while read mode type tree name; do
 312                assert [ "$name" = "$dir" ]
 313                echo $tree
 314                break
 315        done
 316}
 317
 318tree_changed()
 319{
 320        tree=$1
 321        shift
 322        if [ $# -ne 1 ]; then
 323                return 0   # weird parents, consider it changed
 324        else
 325                ptree=$(toptree_for_commit $1)
 326                if [ "$ptree" != "$tree" ]; then
 327                        return 0   # changed
 328                else
 329                        return 1   # not changed
 330                fi
 331        fi
 332}
 333
 334new_squash_commit()
 335{
 336        old="$1"
 337        oldsub="$2"
 338        newsub="$3"
 339        tree=$(toptree_for_commit $newsub) || exit $?
 340        squash_msg "$dir" "$oldsub" "$newsub" | 
 341                git commit-tree "$tree" -p "$old" || exit $?
 342}
 343
 344copy_or_skip()
 345{
 346        rev="$1"
 347        tree="$2"
 348        newparents="$3"
 349        assert [ -n "$tree" ]
 350
 351        identical=
 352        nonidentical=
 353        p=
 354        gotparents=
 355        for parent in $newparents; do
 356                ptree=$(toptree_for_commit $parent) || exit $?
 357                [ -z "$ptree" ] && continue
 358                if [ "$ptree" = "$tree" ]; then
 359                        # an identical parent could be used in place of this rev.
 360                        identical="$parent"
 361                else
 362                        nonidentical="$parent"
 363                fi
 364                
 365                # sometimes both old parents map to the same newparent;
 366                # eliminate duplicates
 367                is_new=1
 368                for gp in $gotparents; do
 369                        if [ "$gp" = "$parent" ]; then
 370                                is_new=
 371                                break
 372                        fi
 373                done
 374                if [ -n "$is_new" ]; then
 375                        gotparents="$gotparents $parent"
 376                        p="$p -p $parent"
 377                fi
 378        done
 379        
 380        if [ -n "$identical" ]; then
 381                echo $identical
 382        else
 383                copy_commit $rev $tree "$p" || exit $?
 384        fi
 385}
 386
 387ensure_clean()
 388{
 389        if ! git diff-index HEAD --exit-code --quiet; then
 390                die "Working tree has modifications.  Cannot add."
 391        fi
 392        if ! git diff-index --cached HEAD --exit-code --quiet; then
 393                die "Index has modifications.  Cannot add."
 394        fi
 395}
 396
 397cmd_add()
 398{
 399        if [ -e "$dir" ]; then
 400                die "'$dir' already exists.  Cannot add."
 401        fi
 402        ensure_clean
 403        
 404        set -- $revs
 405        if [ $# -ne 1 ]; then
 406                die "You must provide exactly one revision.  Got: '$revs'"
 407        fi
 408        rev="$1"
 409        
 410        debug "Adding $dir as '$rev'..."
 411        git read-tree --prefix="$dir" $rev || exit $?
 412        git checkout "$dir" || exit $?
 413        tree=$(git write-tree) || exit $?
 414        
 415        headrev=$(git rev-parse HEAD) || exit $?
 416        if [ -n "$headrev" -a "$headrev" != "$rev" ]; then
 417                headp="-p $headrev"
 418        else
 419                headp=
 420        fi
 421        commit=$(add_msg "$dir" "$headrev" "$rev" |
 422                 git commit-tree $tree $headp -p "$rev") || exit $?
 423        git reset "$commit" || exit $?
 424}
 425
 426cmd_split()
 427{
 428        if [ -n "$branch" ] && rev_exists "refs/heads/$branch"; then
 429                die "Branch '$branch' already exists."
 430        fi
 431
 432        debug "Splitting $dir..."
 433        cache_setup || exit $?
 434        
 435        if [ -n "$onto" ]; then
 436                debug "Reading history for --onto=$onto..."
 437                git rev-list $onto |
 438                while read rev; do
 439                        # the 'onto' history is already just the subdir, so
 440                        # any parent we find there can be used verbatim
 441                        debug "  cache: $rev"
 442                        cache_set $rev $rev
 443                done
 444        fi
 445        
 446        if [ -n "$ignore_joins" ]; then
 447                unrevs=
 448        else
 449                unrevs="$(find_existing_splits "$dir" "$revs")"
 450        fi
 451        
 452        # We can't restrict rev-list to only $dir here, because some of our
 453        # parents have the $dir contents the root, and those won't match.
 454        # (and rev-list --follow doesn't seem to solve this)
 455        grl='git rev-list --reverse --parents $revs $unrevs'
 456        revmax=$(eval "$grl" | wc -l)
 457        revcount=0
 458        createcount=0
 459        eval "$grl" |
 460        while read rev parents; do
 461                revcount=$(($revcount + 1))
 462                say -n "$revcount/$revmax ($createcount)
"
 463                debug "Processing commit: $rev"
 464                exists=$(cache_get $rev)
 465                if [ -n "$exists" ]; then
 466                        debug "  prior: $exists"
 467                        continue
 468                fi
 469                createcount=$(($createcount + 1))
 470                debug "  parents: $parents"
 471                newparents=$(cache_get $parents)
 472                debug "  newparents: $newparents"
 473                
 474                tree=$(subtree_for_commit $rev "$dir")
 475                debug "  tree is: $tree"
 476                
 477                # ugly.  is there no better way to tell if this is a subtree
 478                # vs. a mainline commit?  Does it matter?
 479                [ -z $tree ] && continue
 480
 481                newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
 482                debug "  newrev is: $newrev"
 483                cache_set $rev $newrev
 484                cache_set latest_new $newrev
 485                cache_set latest_old $rev
 486        done || exit $?
 487        latest_new=$(cache_get latest_new)
 488        if [ -z "$latest_new" ]; then
 489                die "No new revisions were found"
 490        fi
 491        
 492        if [ -n "$rejoin" ]; then
 493                debug "Merging split branch into HEAD..."
 494                latest_old=$(cache_get latest_old)
 495                git merge -s ours \
 496                        -m "$(rejoin_msg $dir $latest_old $latest_new)" \
 497                        $latest_new >&2 || exit $?
 498        fi
 499        if [ -n "$branch" ]; then
 500                git update-ref -m 'subtree split' "refs/heads/$branch" \
 501                        $latest_new "" || exit $?
 502                say "Created branch '$branch'"
 503        fi
 504        echo $latest_new
 505        exit 0
 506}
 507
 508cmd_merge()
 509{
 510        ensure_clean
 511        
 512        set -- $revs
 513        if [ $# -ne 1 ]; then
 514                die "You must provide exactly one revision.  Got: '$revs'"
 515        fi
 516        rev="$1"
 517        
 518        if [ -n "$squash" ]; then
 519                first_split="$(find_latest_squash "$dir")"
 520                if [ -z "$first_split" ]; then
 521                        die "Can't squash-merge: '$dir' was never added."
 522                fi
 523                set $first_split
 524                old=$1
 525                sub=$2
 526                if [ "$sub" = "$rev" ]; then
 527                        say "Subtree is already at commit $rev."
 528                        exit 0
 529                fi
 530                new=$(new_squash_commit "$old" "$sub" "$rev") || exit $?
 531                debug "New squash commit: $new"
 532                rev="$new"
 533        fi
 534        
 535        git merge -s subtree $rev
 536}
 537
 538cmd_pull()
 539{
 540        ensure_clean
 541        set -x
 542        git pull -s subtree "$@"
 543}
 544
 545"cmd_$command" "$@"