#!/bin/sh global test_description := ''pushing to a repository using the atomic push option'' source ./test-lib.sh proc mk_repo_pair { rm -rf workbench upstream && test_create_repo upstream && test_create_repo workbench && shell { cd upstream && git config receive.denyCurrentBranch warn } && shell { cd workbench && git remote add up ../upstream } } # Compare the ref ($1) in upstream with a ref value from workbench ($2) # i.e. test_refs second HEAD@{2} proc test_refs { test $Argc = 2 && git -C upstream rev-parse --verify $1 >expect && git -C workbench rev-parse --verify $2 >actual && test_cmp expect actual } test_expect_success 'atomic push works for a single branch' ' mk_repo_pair && ( cd workbench && test_commit one && git push --mirror up && test_commit two && git push --atomic up master ) && test_refs master master ' test_expect_success 'atomic push works for two branches' ' mk_repo_pair && ( cd workbench && test_commit one && git branch second && git push --mirror up && test_commit two && git checkout second && test_commit three && git push --atomic up master second ) && test_refs master master && test_refs second second ' test_expect_success 'atomic push works in combination with --mirror' ' mk_repo_pair && ( cd workbench && test_commit one && git checkout -b second && test_commit two && git push --atomic --mirror up ) && test_refs master master && test_refs second second ' test_expect_success 'atomic push works in combination with --force' ' mk_repo_pair && ( cd workbench && test_commit one && git branch second master && test_commit two_a && git checkout second && test_commit two_b && test_commit three_b && test_commit four && git push --mirror up && # The actual test is below git checkout master && test_commit three_a && git checkout second && git reset --hard HEAD^ && git push --force --atomic up master second ) && test_refs master master && test_refs second second ' # set up two branches where master can be pushed but second can not # (non-fast-forward). Since second can not be pushed the whole operation # will fail and leave master untouched. test_expect_success 'atomic push fails if one branch fails' ' mk_repo_pair && ( cd workbench && test_commit one && git checkout -b second master && test_commit two && test_commit three && test_commit four && git push --mirror up && git reset --hard HEAD~2 && test_commit five && git checkout master && test_commit six && test_must_fail git push --atomic --all up ) && test_refs master HEAD@{7} && test_refs second HEAD@{4} ' test_expect_success 'atomic push fails if one tag fails remotely' ' # prepare the repo mk_repo_pair && ( cd workbench && test_commit one && git checkout -b second master && test_commit two && git push --mirror up ) && # a third party modifies the server side: ( cd upstream && git checkout second && git tag test_tag second ) && # see if we can now push both branches. ( cd workbench && git checkout master && test_commit three && git checkout second && test_commit four && git tag test_tag && test_must_fail git push --tags --atomic up master second ) && test_refs master HEAD@{3} && test_refs second HEAD@{1} ' test_expect_success 'atomic push obeys update hook preventing a branch to be pushed' ' mk_repo_pair && ( cd workbench && test_commit one && git checkout -b second master && test_commit two && git push --mirror up ) && ( cd upstream && HOOKDIR="$(git rev-parse --git-dir)/hooks" && HOOK="$HOOKDIR/update" && mkdir -p "$HOOKDIR" && write_script "$HOOK" <<-\EOF # only allow update to master from now on test "$1" = "refs/heads/master" EOF ) && ( cd workbench && git checkout master && test_commit three && git checkout second && test_commit four && test_must_fail git push --atomic up master second ) && test_refs master HEAD@{3} && test_refs second HEAD@{1} ' test_expect_success 'atomic push is not advertised if configured' ' mk_repo_pair && ( cd upstream git config receive.advertiseatomic 0 ) && ( cd workbench && test_commit one && git push --mirror up && test_commit two && test_must_fail git push --atomic up master ) && test_refs master HEAD@{1} ' test_done (CommandList children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:test_description) op: Equal rhs: {(SQ <"pushing to a repository using the atomic push option">)} spids: [4] ) ] spids: [4] ) (C {(.)} {(./test-lib.sh)}) (FuncDef name: mk_repo_pair body: (BraceGroup children: [ (AndOr children: [ (C {(rm)} {(-rf)} {(workbench)} {(upstream)}) (AndOr children: [ (C {(test_create_repo)} {(upstream)}) (AndOr children: [ (C {(test_create_repo)} {(workbench)}) (AndOr children: [ (Subshell child: (AndOr children: [ (C {(cd)} {(upstream)}) (C {(git)} {(config)} {(receive.denyCurrentBranch)} {(warn)}) ] op_id: Op_DAmp ) spids: [48 67] ) (Subshell child: (AndOr children: [ (C {(cd)} {(workbench)}) (C {(git)} {(remote)} {(add)} {(up)} {(../upstream)}) ] op_id: Op_DAmp ) spids: [72 93] ) ] op_id: Op_DAmp ) ] op_id: Op_DAmp ) ] op_id: Op_DAmp ) ] op_id: Op_DAmp ) ] spids: [20] ) spids: [15 19] ) (FuncDef name: test_refs body: (BraceGroup children: [ (AndOr children: [ (C {(test)} {($ VSub_Pound "$#")} {(Lit_Other "=")} {(2)}) (AndOr children: [ (SimpleCommand words: [ {(git)} {(-C)} {(upstream)} {(rev-parse)} {(--verify)} {(DQ ($ VSub_Number "$1"))} ] redirects: [(Redir op_id:Redir_Great fd:-1 arg_word:{(expect)} spids:[137])] ) (AndOr children: [ (SimpleCommand words: [ {(git)} {(-C)} {(workbench)} {(rev-parse)} {(--verify)} {(DQ ($ VSub_Number "$2"))} ] redirects: [(Redir op_id:Redir_Great fd:-1 arg_word:{(actual)} spids:[157])] ) (C {(test_cmp)} {(expect)} {(actual)}) ] op_id: Op_DAmp ) ] op_id: Op_DAmp ) ] op_id: Op_DAmp ) ] spids: [109] ) spids: [104 108] ) (C {(test_expect_success)} {(SQ <"atomic push works for a single branch">)} { (SQ <"\n"> <"\tmk_repo_pair &&\n"> <"\t(\n"> <"\t\tcd workbench &&\n"> <"\t\ttest_commit one &&\n"> <"\t\tgit push --mirror up &&\n"> <"\t\ttest_commit two &&\n"> <"\t\tgit push --atomic up master\n"> <"\t) &&\n"> <"\ttest_refs master master\n"> ) } ) (C {(test_expect_success)} {(SQ <"atomic push works for two branches">)} { (SQ <"\n"> <"\tmk_repo_pair &&\n"> <"\t(\n"> <"\t\tcd workbench &&\n"> <"\t\ttest_commit one &&\n"> <"\t\tgit branch second &&\n"> <"\t\tgit push --mirror up &&\n"> <"\t\ttest_commit two &&\n"> <"\t\tgit checkout second &&\n"> <"\t\ttest_commit three &&\n"> <"\t\tgit push --atomic up master second\n"> <"\t) &&\n"> <"\ttest_refs master master &&\n"> <"\ttest_refs second second\n"> ) } ) (C {(test_expect_success)} {(SQ <"atomic push works in combination with --mirror">)} { (SQ <"\n"> <"\tmk_repo_pair &&\n"> <"\t(\n"> <"\t\tcd workbench &&\n"> <"\t\ttest_commit one &&\n"> <"\t\tgit checkout -b second &&\n"> <"\t\ttest_commit two &&\n"> <"\t\tgit push --atomic --mirror up\n"> <"\t) &&\n"> <"\ttest_refs master master &&\n"> <"\ttest_refs second second\n"> ) } ) (C {(test_expect_success)} {(SQ <"atomic push works in combination with --force">)} { (SQ <"\n"> <"\tmk_repo_pair &&\n"> <"\t(\n"> <"\t\tcd workbench &&\n"> <"\t\ttest_commit one &&\n"> <"\t\tgit branch second master &&\n"> <"\t\ttest_commit two_a &&\n"> <"\t\tgit checkout second &&\n"> <"\t\ttest_commit two_b &&\n"> <"\t\ttest_commit three_b &&\n"> <"\t\ttest_commit four &&\n"> <"\t\tgit push --mirror up &&\n"> <"\t\t# The actual test is below\n"> <"\t\tgit checkout master &&\n"> <"\t\ttest_commit three_a &&\n"> <"\t\tgit checkout second &&\n"> <"\t\tgit reset --hard HEAD^ &&\n"> <"\t\tgit push --force --atomic up master second\n"> <"\t) &&\n"> <"\ttest_refs master master &&\n"> <"\ttest_refs second second\n"> ) } ) (C {(test_expect_success)} {(SQ <"atomic push fails if one branch fails">)} { (SQ <"\n"> <"\tmk_repo_pair &&\n"> <"\t(\n"> <"\t\tcd workbench &&\n"> <"\t\ttest_commit one &&\n"> <"\t\tgit checkout -b second master &&\n"> <"\t\ttest_commit two &&\n"> <"\t\ttest_commit three &&\n"> <"\t\ttest_commit four &&\n"> <"\t\tgit push --mirror up &&\n"> <"\t\tgit reset --hard HEAD~2 &&\n"> <"\t\ttest_commit five &&\n"> <"\t\tgit checkout master &&\n"> <"\t\ttest_commit six &&\n"> <"\t\ttest_must_fail git push --atomic --all up\n"> <"\t) &&\n"> <"\ttest_refs master HEAD@{7} &&\n"> <"\ttest_refs second HEAD@{4}\n"> ) } ) (C {(test_expect_success)} {(SQ <"atomic push fails if one tag fails remotely">)} { (SQ <"\n"> <"\t# prepare the repo\n"> <"\tmk_repo_pair &&\n"> <"\t(\n"> <"\t\tcd workbench &&\n"> <"\t\ttest_commit one &&\n"> <"\t\tgit checkout -b second master &&\n"> <"\t\ttest_commit two &&\n"> <"\t\tgit push --mirror up\n"> <"\t) &&\n"> <"\t# a third party modifies the server side:\n"> <"\t(\n"> <"\t\tcd upstream &&\n"> <"\t\tgit checkout second &&\n"> <"\t\tgit tag test_tag second\n"> <"\t) &&\n"> <"\t# see if we can now push both branches.\n"> <"\t(\n"> <"\t\tcd workbench &&\n"> <"\t\tgit checkout master &&\n"> <"\t\ttest_commit three &&\n"> <"\t\tgit checkout second &&\n"> <"\t\ttest_commit four &&\n"> <"\t\tgit tag test_tag &&\n"> <"\t\ttest_must_fail git push --tags --atomic up master second\n"> <"\t) &&\n"> <"\ttest_refs master HEAD@{3} &&\n"> <"\ttest_refs second HEAD@{1}\n"> ) } ) (C {(test_expect_success)} {(SQ <"atomic push obeys update hook preventing a branch to be pushed">)} { (SQ <"\n"> <"\tmk_repo_pair &&\n"> <"\t(\n"> <"\t\tcd workbench &&\n"> <"\t\ttest_commit one &&\n"> <"\t\tgit checkout -b second master &&\n"> <"\t\ttest_commit two &&\n"> <"\t\tgit push --mirror up\n"> <"\t) &&\n"> <"\t(\n"> <"\t\tcd upstream &&\n"> <"\t\tHOOKDIR=\"$(git rev-parse --git-dir)/hooks\" &&\n"> <"\t\tHOOK=\"$HOOKDIR/update\" &&\n"> <"\t\tmkdir -p \"$HOOKDIR\" &&\n"> <"\t\twrite_script \"$HOOK\" <<-\\EOF\n"> <"\t\t\t# only allow update to master from now on\n"> <"\t\t\ttest \"$1\" = \"refs/heads/master\"\n"> <"\t\tEOF\n"> <"\t) &&\n"> <"\t(\n"> <"\t\tcd workbench &&\n"> <"\t\tgit checkout master &&\n"> <"\t\ttest_commit three &&\n"> <"\t\tgit checkout second &&\n"> <"\t\ttest_commit four &&\n"> <"\t\ttest_must_fail git push --atomic up master second\n"> <"\t) &&\n"> <"\ttest_refs master HEAD@{3} &&\n"> <"\ttest_refs second HEAD@{1}\n"> ) } ) (C {(test_expect_success)} {(SQ <"atomic push is not advertised if configured">)} { (SQ <"\n"> <"\tmk_repo_pair &&\n"> <"\t(\n"> <"\t\tcd upstream\n"> <"\t\tgit config receive.advertiseatomic 0\n"> <"\t) &&\n"> <"\t(\n"> <"\t\tcd workbench &&\n"> <"\t\ttest_commit one &&\n"> <"\t\tgit push --mirror up &&\n"> <"\t\ttest_commit two &&\n"> <"\t\ttest_must_fail git push --atomic up master\n"> <"\t) &&\n"> <"\ttest_refs master HEAD@{1}\n"> ) } ) (C {(test_done)}) ] )