git-subtree.shon commit Fix splitting after using a squash merge. (1a8c36d)
   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:'START %H%n%s%n%n%b%nEND%n' $revs |
 207        while read a b junk; do
 208                case "$a" in
 209                        START) main="$b"; sq="$b" ;;
 210                        git-subtree-mainline:) main="$b" ;;
 211                        git-subtree-split:) sub="$b" ;;
 212                        END)
 213                                if [ -z "$main" -a -n "$sub" ]; then
 214                                        # squash commits refer to a subtree
 215                                        cache_set "$sq" "$sub"
 216                                fi
 217                                if [ -n "$main" -a -n "$sub" ]; then
 218                                        debug "  Prior: $main -> $sub"
 219                                        cache_set $main $sub
 220                                        try_remove_previous "$main"
 221                                        try_remove_previous "$sub"
 222                                fi
 223                                main=
 224                                sub=
 225                                ;;
 226                esac
 227        done
 228}
 229
 230copy_commit()
 231{
 232        # We're going to set some environment vars here, so
 233        # do it in a subshell to get rid of them safely later
 234        debug copy_commit "{$1}" "{$2}" "{$3}"
 235        git log -1 --pretty=format:'%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%s%n%n%b' "$1" |
 236        (
 237                read GIT_AUTHOR_NAME
 238                read GIT_AUTHOR_EMAIL
 239                read GIT_AUTHOR_DATE
 240                read GIT_COMMITTER_NAME
 241                read GIT_COMMITTER_EMAIL
 242                read GIT_COMMITTER_DATE
 243                export  GIT_AUTHOR_NAME \
 244                        GIT_AUTHOR_EMAIL \
 245                        GIT_AUTHOR_DATE \
 246                        GIT_COMMITTER_NAME \
 247                        GIT_COMMITTER_EMAIL \
 248                        GIT_COMMITTER_DATE
 249                (echo -n "$annotate"; cat ) |
 250                git commit-tree "$2" $3  # reads the rest of stdin
 251        ) || die "Can't copy commit $1"
 252}
 253
 254add_msg()
 255{
 256        dir="$1"
 257        latest_old="$2"
 258        latest_new="$3"
 259        cat <<-EOF
 260                Add '$dir/' from commit '$latest_new'
 261                
 262                git-subtree-dir: $dir
 263                git-subtree-mainline: $latest_old
 264                git-subtree-split: $latest_new
 265        EOF
 266}
 267
 268rejoin_msg()
 269{
 270        dir="$1"
 271        latest_old="$2"
 272        latest_new="$3"
 273        cat <<-EOF
 274                Split '$dir/' into commit '$latest_new'
 275                
 276                git-subtree-dir: $dir
 277                git-subtree-mainline: $latest_old
 278                git-subtree-split: $latest_new
 279        EOF
 280}
 281
 282squash_msg()
 283{
 284        dir="$1"
 285        oldsub="$2"
 286        newsub="$3"
 287        oldsub_short=$(git rev-parse --short "$oldsub")
 288        newsub_short=$(git rev-parse --short "$newsub")
 289        cat <<-EOF
 290                Squashed '$dir/' changes from $oldsub_short..$newsub_short
 291        
 292        EOF
 293        
 294        git log --pretty=tformat:'%h %s' "$oldsub..$newsub"
 295        git log --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
 296        
 297        cat <<-EOF
 298                
 299                git-subtree-dir: $dir
 300                git-subtree-split: $newsub
 301        EOF
 302}
 303
 304toptree_for_commit()
 305{
 306        commit="$1"
 307        git log -1 --pretty=format:'%T' "$commit" -- || exit $?
 308}
 309
 310subtree_for_commit()
 311{
 312        commit="$1"
 313        dir="$2"
 314        git ls-tree "$commit" -- "$dir" |
 315        while read mode type tree name; do
 316                assert [ "$name" = "$dir" ]
 317                echo $tree
 318                break
 319        done
 320}
 321
 322tree_changed()
 323{
 324        tree=$1
 325        shift
 326        if [ $# -ne 1 ]; then
 327                return 0   # weird parents, consider it changed
 328        else
 329                ptree=$(toptree_for_commit $1)
 330                if [ "$ptree" != "$tree" ]; then
 331                        return 0   # changed
 332                else
 333                        return 1   # not changed
 334                fi
 335        fi
 336}
 337
 338new_squash_commit()
 339{
 340        old="$1"
 341        oldsub="$2"
 342        newsub="$3"
 343        tree=$(toptree_for_commit $newsub) || exit $?
 344        squash_msg "$dir" "$oldsub" "$newsub" | 
 345                git commit-tree "$tree" -p "$old" || exit $?
 346}
 347
 348copy_or_skip()
 349{
 350        rev="$1"
 351        tree="$2"
 352        newparents="$3"
 353        assert [ -n "$tree" ]
 354
 355        identical=
 356        nonidentical=
 357        p=
 358        gotparents=
 359        for parent in $newparents; do
 360                ptree=$(toptree_for_commit $parent) || exit $?
 361                [ -z "$ptree" ] && continue
 362                if [ "$ptree" = "$tree" ]; then
 363                        # an identical parent could be used in place of this rev.
 364                        identical="$parent"
 365                else
 366                        nonidentical="$parent"
 367                fi
 368                
 369                # sometimes both old parents map to the same newparent;
 370                # eliminate duplicates
 371                is_new=1
 372                for gp in $gotparents; do
 373                        if [ "$gp" = "$parent" ]; then
 374                                is_new=
 375                                break
 376                        fi
 377                done
 378                if [ -n "$is_new" ]; then
 379                        gotparents="$gotparents $parent"
 380                        p="$p -p $parent"
 381                fi
 382        done
 383        
 384        if [ -n "$identical" ]; then
 385                echo $identical
 386        else
 387                copy_commit $rev $tree "$p" || exit $?
 388        fi
 389}
 390
 391ensure_clean()
 392{
 393        if ! git diff-index HEAD --exit-code --quiet; then
 394                die "Working tree has modifications.  Cannot add."
 395        fi
 396        if ! git diff-index --cached HEAD --exit-code --quiet; then
 397                die "Index has modifications.  Cannot add."
 398        fi
 399}
 400
 401cmd_add()
 402{
 403        if [ -e "$dir" ]; then
 404                die "'$dir' already exists.  Cannot add."
 405        fi
 406        ensure_clean
 407        
 408        set -- $revs
 409        if [ $# -ne 1 ]; then
 410                die "You must provide exactly one revision.  Got: '$revs'"
 411        fi
 412        rev="$1"
 413        
 414        debug "Adding $dir as '$rev'..."
 415        git read-tree --prefix="$dir" $rev || exit $?
 416        git checkout "$dir" || exit $?
 417        tree=$(git write-tree) || exit $?
 418        
 419        headrev=$(git rev-parse HEAD) || exit $?
 420        if [ -n "$headrev" -a "$headrev" != "$rev" ]; then
 421                headp="-p $headrev"
 422        else
 423                headp=
 424        fi
 425        commit=$(add_msg "$dir" "$headrev" "$rev" |
 426                 git commit-tree $tree $headp -p "$rev") || exit $?
 427        git reset "$commit" || exit $?
 428}
 429
 430cmd_split()
 431{
 432        if [ -n "$branch" ] && rev_exists "refs/heads/$branch"; then
 433                die "Branch '$branch' already exists."
 434        fi
 435
 436        debug "Splitting $dir..."
 437        cache_setup || exit $?
 438        
 439        if [ -n "$onto" ]; then
 440                debug "Reading history for --onto=$onto..."
 441                git rev-list $onto |
 442                while read rev; do
 443                        # the 'onto' history is already just the subdir, so
 444                        # any parent we find there can be used verbatim
 445                        debug "  cache: $rev"
 446                        cache_set $rev $rev
 447                done
 448        fi
 449        
 450        if [ -n "$ignore_joins" ]; then
 451                unrevs=
 452        else
 453                unrevs="$(find_existing_splits "$dir" "$revs")"
 454        fi
 455        
 456        # We can't restrict rev-list to only $dir here, because some of our
 457        # parents have the $dir contents the root, and those won't match.
 458        # (and rev-list --follow doesn't seem to solve this)
 459        grl='git rev-list --reverse --parents $revs $unrevs'
 460        revmax=$(eval "$grl" | wc -l)
 461        revcount=0
 462        createcount=0
 463        eval "$grl" |
 464        while read rev parents; do
 465                revcount=$(($revcount + 1))
 466                say -n "$revcount/$revmax ($createcount)
"
 467                debug "Processing commit: $rev"
 468                exists=$(cache_get $rev)
 469                if [ -n "$exists" ]; then
 470                        debug "  prior: $exists"
 471                        continue
 472                fi
 473                createcount=$(($createcount + 1))
 474                debug "  parents: $parents"
 475                newparents=$(cache_get $parents)
 476                debug "  newparents: $newparents"
 477                
 478                tree=$(subtree_for_commit $rev "$dir")
 479                debug "  tree is: $tree"
 480                
 481                # ugly.  is there no better way to tell if this is a subtree
 482                # vs. a mainline commit?  Does it matter?
 483                [ -z $tree ] && continue
 484
 485                newrev=$(copy_or_skip "$rev" "$tree" "$newparents") || exit $?
 486                debug "  newrev is: $newrev"
 487                cache_set $rev $newrev
 488                cache_set latest_new $newrev
 489                cache_set latest_old $rev
 490        done || exit $?
 491        latest_new=$(cache_get latest_new)
 492        if [ -z "$latest_new" ]; then
 493                die "No new revisions were found"
 494        fi
 495        
 496        if [ -n "$rejoin" ]; then
 497                debug "Merging split branch into HEAD..."
 498                latest_old=$(cache_get latest_old)
 499                git merge -s ours \
 500                        -m "$(rejoin_msg $dir $latest_old $latest_new)" \
 501                        $latest_new >&2 || exit $?
 502        fi
 503        if [ -n "$branch" ]; then
 504                git update-ref -m 'subtree split' "refs/heads/$branch" \
 505                        $latest_new "" || exit $?
 506                say "Created branch '$branch'"
 507        fi
 508        echo $latest_new
 509        exit 0
 510}
 511
 512cmd_merge()
 513{
 514        ensure_clean
 515        
 516        set -- $revs
 517        if [ $# -ne 1 ]; then
 518                die "You must provide exactly one revision.  Got: '$revs'"
 519        fi
 520        rev="$1"
 521        
 522        if [ -n "$squash" ]; then
 523                first_split="$(find_latest_squash "$dir")"
 524                if [ -z "$first_split" ]; then
 525                        die "Can't squash-merge: '$dir' was never added."
 526                fi
 527                set $first_split
 528                old=$1
 529                sub=$2
 530                if [ "$sub" = "$rev" ]; then
 531                        say "Subtree is already at commit $rev."
 532                        exit 0
 533                fi
 534                new=$(new_squash_commit "$old" "$sub" "$rev") || exit $?
 535                debug "New squash commit: $new"
 536                rev="$new"
 537        fi
 538        
 539        git merge -s subtree $rev
 540}
 541
 542cmd_pull()
 543{
 544        ensure_clean
 545        set -x
 546        git pull -s subtree "$@"
 547}
 548
 549"cmd_$command" "$@"