dc08275b7d467cd49b9c204664a9497f90d5f82f
   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
   9ROOT_PATH="$PWD"
  10. "$TEST_DIRECTORY"/lib-gpg.sh
  11. "$TEST_DIRECTORY"/lib-httpd.sh
  12. "$TEST_DIRECTORY"/lib-terminal.sh
  13start_httpd
  14
  15test_expect_success 'setup remote repository' '
  16        cd "$ROOT_PATH" &&
  17        mkdir test_repo &&
  18        cd test_repo &&
  19        git init &&
  20        : >path1 &&
  21        git add path1 &&
  22        test_tick &&
  23        git commit -m initial &&
  24        cd - &&
  25        git clone --bare test_repo test_repo.git &&
  26        cd test_repo.git &&
  27        git config http.receivepack true &&
  28        git config core.logallrefupdates true &&
  29        ORIG_HEAD=$(git rev-parse --verify HEAD) &&
  30        cd - &&
  31        mv test_repo.git "$HTTPD_DOCUMENT_ROOT_PATH"
  32'
  33
  34setup_askpass_helper
  35
  36cat >exp <<EOF
  37GET  /smart/test_repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
  38POST /smart/test_repo.git/git-upload-pack HTTP/1.1 200
  39EOF
  40test_expect_success 'no empty path components' '
  41        # Clear the log, so that it does not affect the "used receive-pack
  42        # service" test which reads the log too.
  43        test_when_finished ">\"\$HTTPD_ROOT_PATH\"/access.log" &&
  44
  45        # In the URL, add a trailing slash, and see if git appends yet another
  46        # slash.
  47        cd "$ROOT_PATH" &&
  48        git clone $HTTPD_URL/smart/test_repo.git/ test_repo_clone &&
  49
  50        sed -e "
  51                s/^.* \"//
  52                s/\"//
  53                s/ [1-9][0-9]*\$//
  54                s/^GET /GET  /
  55        " >act <"$HTTPD_ROOT_PATH"/access.log &&
  56        test_cmp exp act
  57'
  58
  59test_expect_success 'clone remote repository' '
  60        rm -rf test_repo_clone &&
  61        git clone $HTTPD_URL/smart/test_repo.git test_repo_clone &&
  62        (
  63                cd test_repo_clone && git config push.default matching
  64        )
  65'
  66
  67test_expect_success 'push to remote repository (standard)' '
  68        cd "$ROOT_PATH"/test_repo_clone &&
  69        : >path2 &&
  70        git add path2 &&
  71        test_tick &&
  72        git commit -m path2 &&
  73        HEAD=$(git rev-parse --verify HEAD) &&
  74        GIT_TRACE_CURL=true git push -v -v 2>err &&
  75        ! grep "Expect: 100-continue" err &&
  76        grep "POST git-receive-pack ([0-9]* bytes)" err &&
  77        (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
  78         test $HEAD = $(git rev-parse --verify HEAD))
  79'
  80
  81test_expect_success 'push already up-to-date' '
  82        git push
  83'
  84
  85test_expect_success 'create and delete remote branch' '
  86        cd "$ROOT_PATH"/test_repo_clone &&
  87        git checkout -b dev &&
  88        : >path3 &&
  89        git add path3 &&
  90        test_tick &&
  91        git commit -m dev &&
  92        git push origin dev &&
  93        git push origin :dev &&
  94        test_must_fail git show-ref --verify refs/remotes/origin/dev
  95'
  96
  97cat >"$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update" <<EOF
  98#!/bin/sh
  99exit 1
 100EOF
 101chmod a+x "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update"
 102
 103cat >exp <<EOF
 104remote: error: hook declined to update refs/heads/dev2
 105To http://127.0.0.1:$LIB_HTTPD_PORT/smart/test_repo.git
 106 ! [remote rejected] dev2 -> dev2 (hook declined)
 107error: failed to push some refs to 'http://127.0.0.1:$LIB_HTTPD_PORT/smart/test_repo.git'
 108EOF
 109
 110test_expect_success 'rejected update prints status' '
 111        cd "$ROOT_PATH"/test_repo_clone &&
 112        git checkout -b dev2 &&
 113        : >path4 &&
 114        git add path4 &&
 115        test_tick &&
 116        git commit -m dev2 &&
 117        test_must_fail git push origin dev2 2>act &&
 118        sed -e "/^remote: /s/ *$//" <act >cmp &&
 119        test_i18ncmp exp cmp
 120'
 121rm -f "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update"
 122
 123cat >exp <<EOF
 124GET  /smart/test_repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
 125POST /smart/test_repo.git/git-upload-pack HTTP/1.1 200
 126GET  /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
 127POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
 128GET  /smart/test_repo.git/info/refs?service=git-receive-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
 132POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
 133GET  /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
 134POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
 135EOF
 136test_expect_success 'used receive-pack service' '
 137        sed -e "
 138                s/^.* \"//
 139                s/\"//
 140                s/ [1-9][0-9]*\$//
 141                s/^GET /GET  /
 142        " >act <"$HTTPD_ROOT_PATH"/access.log &&
 143        test_cmp exp act
 144'
 145
 146test_http_push_nonff "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git \
 147        "$ROOT_PATH"/test_repo_clone master             success
 148
 149test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper' '
 150        # create a dissimilarly-named remote ref so that git is unable to match the
 151        # two refs (viz. local, remote) unless an explicit refspec is provided.
 152        git push origin master:retsam &&
 153
 154        echo "change changed" > path2 &&
 155        git commit -a -m path2 --amend &&
 156
 157        # push master too; this ensures there is at least one '"'push'"' command to
 158        # the remote helper and triggers interaction with the helper.
 159        test_must_fail git push -v origin +master master:retsam >output 2>&1'
 160
 161test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper: remote output' '
 162        grep "^ + [a-f0-9]*\.\.\.[a-f0-9]* *master -> master (forced update)$" output &&
 163        grep "^ ! \[rejected\] *master -> retsam (non-fast-forward)$" output
 164'
 165
 166test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper: our output' '
 167        test_i18ngrep "Updates were rejected because" \
 168                output
 169'
 170
 171test_expect_success 'push (chunked)' '
 172        git checkout master &&
 173        test_commit commit path3 &&
 174        HEAD=$(git rev-parse --verify HEAD) &&
 175        test_config http.postbuffer 4 &&
 176        git push -v -v origin $BRANCH 2>err &&
 177        grep "POST git-receive-pack (chunked)" err &&
 178        (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
 179         test $HEAD = $(git rev-parse --verify HEAD))
 180'
 181
 182test_expect_success 'push --all can push to empty repo' '
 183        d=$HTTPD_DOCUMENT_ROOT_PATH/empty-all.git &&
 184        git init --bare "$d" &&
 185        git --git-dir="$d" config http.receivepack true &&
 186        git push --all "$HTTPD_URL"/smart/empty-all.git
 187'
 188
 189test_expect_success 'push --mirror can push to empty repo' '
 190        d=$HTTPD_DOCUMENT_ROOT_PATH/empty-mirror.git &&
 191        git init --bare "$d" &&
 192        git --git-dir="$d" config http.receivepack true &&
 193        git push --mirror "$HTTPD_URL"/smart/empty-mirror.git
 194'
 195
 196test_expect_success 'push --all to repo with alternates' '
 197        s=$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git &&
 198        d=$HTTPD_DOCUMENT_ROOT_PATH/alternates-all.git &&
 199        git clone --bare --shared "$s" "$d" &&
 200        git --git-dir="$d" config http.receivepack true &&
 201        git --git-dir="$d" repack -adl &&
 202        git push --all "$HTTPD_URL"/smart/alternates-all.git
 203'
 204
 205test_expect_success 'push --mirror to repo with alternates' '
 206        s=$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git &&
 207        d=$HTTPD_DOCUMENT_ROOT_PATH/alternates-mirror.git &&
 208        git clone --bare --shared "$s" "$d" &&
 209        git --git-dir="$d" config http.receivepack true &&
 210        git --git-dir="$d" repack -adl &&
 211        git push --mirror "$HTTPD_URL"/smart/alternates-mirror.git
 212'
 213
 214test_expect_success TTY 'push shows progress when stderr is a tty' '
 215        cd "$ROOT_PATH"/test_repo_clone &&
 216        test_commit noisy &&
 217        test_terminal git push >output 2>&1 &&
 218        test_i18ngrep "^Writing objects" output
 219'
 220
 221test_expect_success TTY 'push --quiet silences status and progress' '
 222        cd "$ROOT_PATH"/test_repo_clone &&
 223        test_commit quiet &&
 224        test_terminal git push --quiet >output 2>&1 &&
 225        test_cmp /dev/null output
 226'
 227
 228test_expect_success TTY 'push --no-progress silences progress but not status' '
 229        cd "$ROOT_PATH"/test_repo_clone &&
 230        test_commit no-progress &&
 231        test_terminal git push --no-progress >output 2>&1 &&
 232        test_i18ngrep "^To http" output &&
 233        test_i18ngrep ! "^Writing objects"
 234'
 235
 236test_expect_success 'push --progress shows progress to non-tty' '
 237        cd "$ROOT_PATH"/test_repo_clone &&
 238        test_commit progress &&
 239        git push --progress >output 2>&1 &&
 240        test_i18ngrep "^To http" output &&
 241        test_i18ngrep "^Writing objects" output
 242'
 243
 244test_expect_success 'http push gives sane defaults to reflog' '
 245        cd "$ROOT_PATH"/test_repo_clone &&
 246        test_commit reflog-test &&
 247        git push "$HTTPD_URL"/smart/test_repo.git &&
 248        git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
 249                log -g -1 --format="%gn <%ge>" >actual &&
 250        echo "anonymous <anonymous@http.127.0.0.1>" >expect &&
 251        test_cmp expect actual
 252'
 253
 254test_expect_success 'http push respects GIT_COMMITTER_* in reflog' '
 255        cd "$ROOT_PATH"/test_repo_clone &&
 256        test_commit custom-reflog-test &&
 257        git push "$HTTPD_URL"/smart_custom_env/test_repo.git &&
 258        git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
 259                log -g -1 --format="%gn <%ge>" >actual &&
 260        echo "Custom User <custom@example.com>" >expect &&
 261        test_cmp expect actual
 262'
 263
 264test_expect_success 'push over smart http with auth' '
 265        cd "$ROOT_PATH/test_repo_clone" &&
 266        echo push-auth-test >expect &&
 267        test_commit push-auth-test &&
 268        set_askpass user@host pass@host &&
 269        git push "$HTTPD_URL"/auth/smart/test_repo.git &&
 270        git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
 271                log -1 --format=%s >actual &&
 272        expect_askpass both user@host &&
 273        test_cmp expect actual
 274'
 275
 276test_expect_success 'push to auth-only-for-push repo' '
 277        cd "$ROOT_PATH/test_repo_clone" &&
 278        echo push-half-auth >expect &&
 279        test_commit push-half-auth &&
 280        set_askpass user@host pass@host &&
 281        git push "$HTTPD_URL"/auth-push/smart/test_repo.git &&
 282        git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
 283                log -1 --format=%s >actual &&
 284        expect_askpass both user@host &&
 285        test_cmp expect actual
 286'
 287
 288test_expect_success 'create repo without http.receivepack set' '
 289        cd "$ROOT_PATH" &&
 290        git init half-auth &&
 291        (
 292                cd half-auth &&
 293                test_commit one
 294        ) &&
 295        git clone --bare half-auth "$HTTPD_DOCUMENT_ROOT_PATH/half-auth.git"
 296'
 297
 298test_expect_success 'clone via half-auth-complete does not need password' '
 299        cd "$ROOT_PATH" &&
 300        set_askpass wrong &&
 301        git clone "$HTTPD_URL"/half-auth-complete/smart/half-auth.git \
 302                half-auth-clone &&
 303        expect_askpass none
 304'
 305
 306test_expect_success 'push into half-auth-complete requires password' '
 307        cd "$ROOT_PATH/half-auth-clone" &&
 308        echo two >expect &&
 309        test_commit two &&
 310        set_askpass user@host pass@host &&
 311        git push "$HTTPD_URL/half-auth-complete/smart/half-auth.git" &&
 312        git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/half-auth.git" \
 313                log -1 --format=%s >actual &&
 314        expect_askpass both user@host &&
 315        test_cmp expect actual
 316'
 317
 318test_expect_success CMDLINE_LIMIT 'push 2000 tags over http' '
 319        sha1=$(git rev-parse HEAD) &&
 320        test_seq 2000 |
 321          sort |
 322          sed "s|.*|$sha1 refs/tags/really-long-tag-name-&|" \
 323          >.git/packed-refs &&
 324        run_with_limited_cmdline git push --mirror
 325'
 326
 327test_expect_success GPG 'push with post-receive to inspect certificate' '
 328        (
 329                cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
 330                mkdir -p hooks &&
 331                write_script hooks/post-receive <<-\EOF &&
 332                # discard the update list
 333                cat >/dev/null
 334                # record the push certificate
 335                if test -n "${GIT_PUSH_CERT-}"
 336                then
 337                        git cat-file blob $GIT_PUSH_CERT >../push-cert
 338                fi &&
 339                cat >../push-cert-status <<E_O_F
 340                SIGNER=${GIT_PUSH_CERT_SIGNER-nobody}
 341                KEY=${GIT_PUSH_CERT_KEY-nokey}
 342                STATUS=${GIT_PUSH_CERT_STATUS-nostatus}
 343                NONCE_STATUS=${GIT_PUSH_CERT_NONCE_STATUS-nononcestatus}
 344                NONCE=${GIT_PUSH_CERT_NONCE-nononce}
 345                E_O_F
 346                EOF
 347
 348                git config receive.certnonceseed sekrit &&
 349                git config receive.certnonceslop 30
 350        ) &&
 351        cd "$ROOT_PATH/test_repo_clone" &&
 352        test_commit cert-test &&
 353        git push --signed "$HTTPD_URL/smart/test_repo.git" &&
 354        (
 355                cd "$HTTPD_DOCUMENT_ROOT_PATH" &&
 356                cat <<-\EOF &&
 357                SIGNER=C O Mitter <committer@example.com>
 358                KEY=13B6F51ECDDE430D
 359                STATUS=G
 360                NONCE_STATUS=OK
 361                EOF
 362                sed -n -e "s/^nonce /NONCE=/p" -e "/^$/q" push-cert
 363        ) >expect &&
 364        test_cmp expect "$HTTPD_DOCUMENT_ROOT_PATH/push-cert-status"
 365'
 366
 367test_expect_success 'push status output scrubs password' '
 368        cd "$ROOT_PATH/test_repo_clone" &&
 369        git push --porcelain \
 370                "$HTTPD_URL_USER_PASS/smart/test_repo.git" \
 371                +HEAD:scrub >status &&
 372        # should have been scrubbed down to vanilla URL
 373        grep "^To $HTTPD_URL/smart/test_repo.git" status
 374'
 375
 376stop_httpd
 377test_done