t / t5541-http-push.shon commit Sync with maint (6f5e880)
   1#!/bin/sh
   2#
   3# Copyright (c) 2008 Clemens Buchacher <drizzd@aon.at>
   4#
   5
   6test_description='test smart pushing over http via http-backend'
   7. ./test-lib.sh
   8
   9if test -n "$NO_CURL"; then
  10        skip_all='skipping test, git built without http support'
  11        test_done
  12fi
  13
  14ROOT_PATH="$PWD"
  15LIB_HTTPD_PORT=${LIB_HTTPD_PORT-'5541'}
  16. "$TEST_DIRECTORY"/lib-httpd.sh
  17. "$TEST_DIRECTORY"/lib-terminal.sh
  18start_httpd
  19
  20test_expect_success 'setup remote repository' '
  21        cd "$ROOT_PATH" &&
  22        mkdir test_repo &&
  23        cd test_repo &&
  24        git init &&
  25        : >path1 &&
  26        git add path1 &&
  27        test_tick &&
  28        git commit -m initial &&
  29        cd - &&
  30        git clone --bare test_repo test_repo.git &&
  31        cd test_repo.git &&
  32        git config http.receivepack true &&
  33        ORIG_HEAD=$(git rev-parse --verify HEAD) &&
  34        cd - &&
  35        mv test_repo.git "$HTTPD_DOCUMENT_ROOT_PATH"
  36'
  37
  38cat >exp <<EOF
  39GET  /smart/test_repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
  40POST /smart/test_repo.git/git-upload-pack HTTP/1.1 200
  41EOF
  42test_expect_success 'no empty path components' '
  43        # In the URL, add a trailing slash, and see if git appends yet another
  44        # slash.
  45        cd "$ROOT_PATH" &&
  46        git clone $HTTPD_URL/smart/test_repo.git/ test_repo_clone &&
  47
  48        sed -e "
  49                s/^.* \"//
  50                s/\"//
  51                s/ [1-9][0-9]*\$//
  52                s/^GET /GET  /
  53        " >act <"$HTTPD_ROOT_PATH"/access.log &&
  54
  55        # Clear the log, so that it does not affect the "used receive-pack
  56        # service" test which reads the log too.
  57        #
  58        # We do this before the actual comparison to ensure the log is cleared.
  59        echo > "$HTTPD_ROOT_PATH"/access.log &&
  60
  61        test_cmp exp act
  62'
  63
  64test_expect_success 'clone remote repository' '
  65        rm -rf test_repo_clone &&
  66        git clone $HTTPD_URL/smart/test_repo.git test_repo_clone
  67'
  68
  69test_expect_success 'push to remote repository (standard)' '
  70        cd "$ROOT_PATH"/test_repo_clone &&
  71        : >path2 &&
  72        git add path2 &&
  73        test_tick &&
  74        git commit -m path2 &&
  75        HEAD=$(git rev-parse --verify HEAD) &&
  76        GIT_CURL_VERBOSE=1 git push -v -v 2>err &&
  77        ! grep "Expect: 100-continue" err &&
  78        grep "POST git-receive-pack ([0-9]* bytes)" err &&
  79        (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
  80         test $HEAD = $(git rev-parse --verify HEAD))
  81'
  82
  83test_expect_success 'push already up-to-date' '
  84        git push
  85'
  86
  87test_expect_success 'create and delete remote branch' '
  88        cd "$ROOT_PATH"/test_repo_clone &&
  89        git checkout -b dev &&
  90        : >path3 &&
  91        git add path3 &&
  92        test_tick &&
  93        git commit -m dev &&
  94        git push origin dev &&
  95        git push origin :dev &&
  96        test_must_fail git show-ref --verify refs/remotes/origin/dev
  97'
  98
  99cat >"$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update" <<EOF
 100#!/bin/sh
 101exit 1
 102EOF
 103chmod a+x "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update"
 104
 105cat >exp <<EOF
 106remote: error: hook declined to update refs/heads/dev2
 107To http://127.0.0.1:$LIB_HTTPD_PORT/smart/test_repo.git
 108 ! [remote rejected] dev2 -> dev2 (hook declined)
 109error: failed to push some refs to 'http://127.0.0.1:5541/smart/test_repo.git'
 110EOF
 111
 112test_expect_success 'rejected update prints status' '
 113        cd "$ROOT_PATH"/test_repo_clone &&
 114        git checkout -b dev2 &&
 115        : >path4 &&
 116        git add path4 &&
 117        test_tick &&
 118        git commit -m dev2 &&
 119        test_must_fail git push origin dev2 2>act &&
 120        sed -e "/^remote: /s/ *$//" <act >cmp &&
 121        test_cmp exp cmp
 122'
 123rm -f "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update"
 124
 125cat >exp <<EOF
 126
 127GET  /smart/test_repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
 128POST /smart/test_repo.git/git-upload-pack HTTP/1.1 200
 129GET  /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
 130POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
 131GET  /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
 132GET  /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
 133POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
 134GET  /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
 135POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
 136GET  /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
 137POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
 138EOF
 139test_expect_success 'used receive-pack service' '
 140        sed -e "
 141                s/^.* \"//
 142                s/\"//
 143                s/ [1-9][0-9]*\$//
 144                s/^GET /GET  /
 145        " >act <"$HTTPD_ROOT_PATH"/access.log &&
 146        test_cmp exp act
 147'
 148
 149test_http_push_nonff "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git \
 150        "$ROOT_PATH"/test_repo_clone master
 151
 152test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper' '
 153        # create a dissimilarly-named remote ref so that git is unable to match the
 154        # two refs (viz. local, remote) unless an explicit refspec is provided.
 155        git push origin master:retsam
 156
 157        echo "change changed" > path2 &&
 158        git commit -a -m path2 --amend &&
 159
 160        # push master too; this ensures there is at least one '"'push'"' command to
 161        # the remote helper and triggers interaction with the helper.
 162        test_must_fail git push -v origin +master master:retsam >output 2>&1'
 163
 164test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper: remote output' '
 165        grep "^ + [a-f0-9]*\.\.\.[a-f0-9]* *master -> master (forced update)$" output &&
 166        grep "^ ! \[rejected\] *master -> retsam (non-fast-forward)$" output
 167'
 168
 169test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper: our output' '
 170        test_i18ngrep "To prevent you from losing history, non-fast-forward updates were rejected" \
 171                output
 172'
 173
 174test_expect_success 'push (chunked)' '
 175        git checkout master &&
 176        test_commit commit path3 &&
 177        HEAD=$(git rev-parse --verify HEAD) &&
 178        git config http.postbuffer 4 &&
 179        test_when_finished "git config --unset http.postbuffer" &&
 180        git push -v -v origin $BRANCH 2>err &&
 181        grep "POST git-receive-pack (chunked)" err &&
 182        (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
 183         test $HEAD = $(git rev-parse --verify HEAD))
 184'
 185
 186test_expect_success 'push --all can push to empty repo' '
 187        d=$HTTPD_DOCUMENT_ROOT_PATH/empty-all.git &&
 188        git init --bare "$d" &&
 189        git --git-dir="$d" config http.receivepack true &&
 190        git push --all "$HTTPD_URL"/smart/empty-all.git
 191'
 192
 193test_expect_success 'push --mirror can push to empty repo' '
 194        d=$HTTPD_DOCUMENT_ROOT_PATH/empty-mirror.git &&
 195        git init --bare "$d" &&
 196        git --git-dir="$d" config http.receivepack true &&
 197        git push --mirror "$HTTPD_URL"/smart/empty-mirror.git
 198'
 199
 200test_expect_success 'push --all to repo with alternates' '
 201        s=$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git &&
 202        d=$HTTPD_DOCUMENT_ROOT_PATH/alternates-all.git &&
 203        git clone --bare --shared "$s" "$d" &&
 204        git --git-dir="$d" config http.receivepack true &&
 205        git --git-dir="$d" repack -adl &&
 206        git push --all "$HTTPD_URL"/smart/alternates-all.git
 207'
 208
 209test_expect_success 'push --mirror to repo with alternates' '
 210        s=$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git &&
 211        d=$HTTPD_DOCUMENT_ROOT_PATH/alternates-mirror.git &&
 212        git clone --bare --shared "$s" "$d" &&
 213        git --git-dir="$d" config http.receivepack true &&
 214        git --git-dir="$d" repack -adl &&
 215        git push --mirror "$HTTPD_URL"/smart/alternates-mirror.git
 216'
 217
 218test_expect_success TTY 'quiet push' '
 219        cd "$ROOT_PATH"/test_repo_clone &&
 220        test_commit quiet &&
 221        test_terminal git push --quiet --no-progress 2>&1 | tee output &&
 222        test_cmp /dev/null output
 223'
 224
 225stop_httpd
 226test_done