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