f2b551dfafcf7aa08c4fc23c315f8e6eb0bb12ce
   1#!/bin/sh
   2# Copyright (c) 2012 Felipe Contreras
   3
   4# The first argument can be a url when the fetch/push command was a url
   5# instead of a configured remote. In this case, use a generic alias.
   6if test "$1" = "testgit::$2"; then
   7        alias=_
   8else
   9        alias=$1
  10fi
  11url=$2
  12
  13dir="$GIT_DIR/testgit/$alias"
  14
  15refspec="refs/heads/*:refs/testgit/$alias/heads/*"
  16
  17test -n "$GIT_REMOTE_TESTGIT_NOREFSPEC" && refspec=""
  18
  19GIT_DIR="$url/.git"
  20export GIT_DIR
  21
  22force=
  23
  24mkdir -p "$dir"
  25
  26if test -z "$GIT_REMOTE_TESTGIT_NO_MARKS"
  27then
  28        gitmarks="$dir/git.marks"
  29        testgitmarks="$dir/testgit.marks"
  30        test -e "$gitmarks" || >"$gitmarks"
  31        test -e "$testgitmarks" || >"$testgitmarks"
  32fi
  33
  34while read line
  35do
  36        case $line in
  37        capabilities)
  38                echo 'import'
  39                echo 'export'
  40                test -n "$refspec" && echo "refspec $refspec"
  41                if test -n "$gitmarks"
  42                then
  43                        echo "*import-marks $gitmarks"
  44                        echo "*export-marks $gitmarks"
  45                fi
  46                test -n "$GIT_REMOTE_TESTGIT_SIGNED_TAGS" && echo "signed-tags"
  47                test -n "$GIT_REMOTE_TESTGIT_NO_PRIVATE_UPDATE" && echo "no-private-update"
  48                echo 'option'
  49                echo
  50                ;;
  51        list)
  52                git for-each-ref --format='? %(refname)' 'refs/heads/'
  53                head=$(git symbolic-ref HEAD)
  54                echo "@$head HEAD"
  55                echo
  56                ;;
  57        import*)
  58                # read all import lines
  59                while true
  60                do
  61                        ref="${line#* }"
  62                        refs="$refs $ref"
  63                        read line
  64                        test "${line%% *}" != "import" && break
  65                done
  66
  67                if test -n "$gitmarks"
  68                then
  69                        echo "feature import-marks=$gitmarks"
  70                        echo "feature export-marks=$gitmarks"
  71                fi
  72
  73                if test -n "$GIT_REMOTE_TESTGIT_FAILURE"
  74                then
  75                        echo "feature done"
  76                        exit 1
  77                fi
  78
  79                echo "feature done"
  80                git fast-export \
  81                        ${refspec:+"--refspec=$refspec"} \
  82                        ${testgitmarks:+"--import-marks=$testgitmarks"} \
  83                        ${testgitmarks:+"--export-marks=$testgitmarks"} \
  84                        $refs
  85                echo "done"
  86                ;;
  87        export)
  88                if test -n "$GIT_REMOTE_TESTGIT_FAILURE"
  89                then
  90                        # consume input so fast-export doesn't get SIGPIPE;
  91                        # git would also notice that case, but we want
  92                        # to make sure we are exercising the later
  93                        # error checks
  94                        while read line; do
  95                                test "done" = "$line" && break
  96                        done
  97                        exit 1
  98                fi
  99
 100                before=$(git for-each-ref --format=' %(refname) %(objectname) ')
 101
 102                git fast-import \
 103                        ${force:+--force} \
 104                        ${testgitmarks:+"--import-marks=$testgitmarks"} \
 105                        ${testgitmarks:+"--export-marks=$testgitmarks"} \
 106                        --quiet
 107
 108                # figure out which refs were updated
 109                git for-each-ref --format='%(refname) %(objectname)' |
 110                while read ref a
 111                do
 112                        case "$before" in
 113                        *" $ref $a "*)
 114                                continue ;;     # unchanged
 115                        esac
 116                        if test -z "$GIT_REMOTE_TESTGIT_PUSH_ERROR"
 117                        then
 118                                echo "ok $ref"
 119                        else
 120                                echo "error $ref $GIT_REMOTE_TESTGIT_PUSH_ERROR"
 121                        fi
 122                done
 123
 124                echo
 125                ;;
 126        option\ *)
 127                read cmd opt val <<-EOF
 128                $line
 129                EOF
 130                case $opt in
 131                force)
 132                        test $val = "true" && force="true" || force=
 133                        echo "ok"
 134                        ;;
 135                *)
 136                        echo "unsupported"
 137                        ;;
 138                esac
 139                ;;
 140        '')
 141                exit
 142                ;;
 143        esac
 144done