t / t3511-cherry-pick-x.shon commit Merge branch 'sb/unpack-trees-super-prefix' (2243d22)
   1#!/bin/sh
   2
   3test_description='Test cherry-pick -x and -s'
   4
   5. ./test-lib.sh
   6
   7pristine_detach () {
   8        git cherry-pick --quit &&
   9        git checkout -f "$1^0" &&
  10        git read-tree -u --reset HEAD &&
  11        git clean -d -f -f -q -x
  12}
  13
  14mesg_one_line='base: commit message'
  15
  16mesg_no_footer="$mesg_one_line
  17
  18OneWordBodyThatsNotA-S-o-B"
  19
  20mesg_with_footer="$mesg_no_footer
  21
  22Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
  23Signed-off-by: A.U. Thor <author@example.com>
  24Signed-off-by: B.U. Thor <buthor@example.com>"
  25
  26mesg_broken_footer="$mesg_no_footer
  27
  28This is not recognized as a footer because Myfooter is not a recognized token.
  29Myfooter: A.U. Thor <author@example.com>"
  30
  31mesg_with_footer_sob="$mesg_with_footer
  32Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
  33
  34mesg_with_cherry_footer="$mesg_with_footer_sob
  35(cherry picked from commit da39a3ee5e6b4b0d3255bfef95601890afd80709)
  36Tested-by: C.U. Thor <cuthor@example.com>"
  37
  38mesg_unclean="$mesg_one_line
  39
  40
  41leading empty lines
  42
  43
  44consecutive empty lines
  45
  46# hash tag comment
  47
  48trailing empty lines
  49
  50
  51"
  52
  53test_expect_success setup '
  54        git config advice.detachedhead false &&
  55        echo unrelated >unrelated &&
  56        git add unrelated &&
  57        test_commit initial foo a &&
  58        test_commit "$mesg_one_line" foo b mesg-one-line &&
  59        git reset --hard initial &&
  60        test_commit "$mesg_no_footer" foo b mesg-no-footer &&
  61        git reset --hard initial &&
  62        test_commit "$mesg_broken_footer" foo b mesg-broken-footer &&
  63        git reset --hard initial &&
  64        test_commit "$mesg_with_footer" foo b mesg-with-footer &&
  65        git reset --hard initial &&
  66        test_commit "$mesg_with_footer_sob" foo b mesg-with-footer-sob &&
  67        git reset --hard initial &&
  68        test_commit "$mesg_with_cherry_footer" foo b mesg-with-cherry-footer &&
  69        git reset --hard initial &&
  70        test_config commit.cleanup verbatim &&
  71        test_commit "$mesg_unclean" foo b mesg-unclean &&
  72        test_unconfig commit.cleanup &&
  73        pristine_detach initial &&
  74        test_commit conflicting unrelated
  75'
  76
  77test_expect_success 'cherry-pick -x inserts blank line after one line subject' '
  78        pristine_detach initial &&
  79        sha1=$(git rev-parse mesg-one-line^0) &&
  80        git cherry-pick -x mesg-one-line &&
  81        cat <<-EOF >expect &&
  82                $mesg_one_line
  83
  84                (cherry picked from commit $sha1)
  85        EOF
  86        git log -1 --pretty=format:%B >actual &&
  87        test_cmp expect actual
  88'
  89
  90test_expect_success 'cherry-pick -s inserts blank line after one line subject' '
  91        pristine_detach initial &&
  92        git cherry-pick -s mesg-one-line &&
  93        cat <<-EOF >expect &&
  94                $mesg_one_line
  95
  96                Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
  97        EOF
  98        git log -1 --pretty=format:%B >actual &&
  99        test_cmp expect actual
 100'
 101
 102test_expect_success 'cherry-pick -s inserts blank line after non-conforming footer' '
 103        pristine_detach initial &&
 104        git cherry-pick -s mesg-broken-footer &&
 105        cat <<-EOF >expect &&
 106                $mesg_broken_footer
 107
 108                Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
 109        EOF
 110        git log -1 --pretty=format:%B >actual &&
 111        test_cmp expect actual
 112'
 113
 114test_expect_success 'cherry-pick -s recognizes trailer config' '
 115        pristine_detach initial &&
 116        git -c "trailer.Myfooter.ifexists=add" cherry-pick -s mesg-broken-footer &&
 117        cat <<-EOF >expect &&
 118                $mesg_broken_footer
 119                Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
 120        EOF
 121        git log -1 --pretty=format:%B >actual &&
 122        test_cmp expect actual
 123'
 124
 125test_expect_success 'cherry-pick -x inserts blank line when conforming footer not found' '
 126        pristine_detach initial &&
 127        sha1=$(git rev-parse mesg-no-footer^0) &&
 128        git cherry-pick -x mesg-no-footer &&
 129        cat <<-EOF >expect &&
 130                $mesg_no_footer
 131
 132                (cherry picked from commit $sha1)
 133        EOF
 134        git log -1 --pretty=format:%B >actual &&
 135        test_cmp expect actual
 136'
 137
 138test_expect_success 'cherry-pick -s inserts blank line when conforming footer not found' '
 139        pristine_detach initial &&
 140        git cherry-pick -s mesg-no-footer &&
 141        cat <<-EOF >expect &&
 142                $mesg_no_footer
 143
 144                Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
 145        EOF
 146        git log -1 --pretty=format:%B >actual &&
 147        test_cmp expect actual
 148'
 149
 150test_expect_success 'cherry-pick -x -s inserts blank line when conforming footer not found' '
 151        pristine_detach initial &&
 152        sha1=$(git rev-parse mesg-no-footer^0) &&
 153        git cherry-pick -x -s mesg-no-footer &&
 154        cat <<-EOF >expect &&
 155                $mesg_no_footer
 156
 157                (cherry picked from commit $sha1)
 158                Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
 159        EOF
 160        git log -1 --pretty=format:%B >actual &&
 161        test_cmp expect actual
 162'
 163
 164test_expect_success 'cherry-pick -s adds sob when last sob doesnt match committer' '
 165        pristine_detach initial &&
 166        git cherry-pick -s mesg-with-footer &&
 167        cat <<-EOF >expect &&
 168                $mesg_with_footer
 169                Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
 170        EOF
 171        git log -1 --pretty=format:%B >actual &&
 172        test_cmp expect actual
 173'
 174
 175test_expect_success 'cherry-pick -x -s adds sob when last sob doesnt match committer' '
 176        pristine_detach initial &&
 177        sha1=$(git rev-parse mesg-with-footer^0) &&
 178        git cherry-pick -x -s mesg-with-footer &&
 179        cat <<-EOF >expect &&
 180                $mesg_with_footer
 181                (cherry picked from commit $sha1)
 182                Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
 183        EOF
 184        git log -1 --pretty=format:%B >actual &&
 185        test_cmp expect actual
 186'
 187
 188test_expect_success 'cherry-pick -s refrains from adding duplicate trailing sob' '
 189        pristine_detach initial &&
 190        git cherry-pick -s mesg-with-footer-sob &&
 191        cat <<-EOF >expect &&
 192                $mesg_with_footer_sob
 193        EOF
 194        git log -1 --pretty=format:%B >actual &&
 195        test_cmp expect actual
 196'
 197
 198test_expect_success 'cherry-pick -x -s adds sob even when trailing sob exists for committer' '
 199        pristine_detach initial &&
 200        sha1=$(git rev-parse mesg-with-footer-sob^0) &&
 201        git cherry-pick -x -s mesg-with-footer-sob &&
 202        cat <<-EOF >expect &&
 203                $mesg_with_footer_sob
 204                (cherry picked from commit $sha1)
 205                Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
 206        EOF
 207        git log -1 --pretty=format:%B >actual &&
 208        test_cmp expect actual
 209'
 210
 211test_expect_success 'cherry-pick -x treats "(cherry picked from..." line as part of footer' '
 212        pristine_detach initial &&
 213        sha1=$(git rev-parse mesg-with-cherry-footer^0) &&
 214        git cherry-pick -x mesg-with-cherry-footer &&
 215        cat <<-EOF >expect &&
 216                $mesg_with_cherry_footer
 217                (cherry picked from commit $sha1)
 218        EOF
 219        git log -1 --pretty=format:%B >actual &&
 220        test_cmp expect actual
 221'
 222
 223test_expect_success 'cherry-pick -s treats "(cherry picked from..." line as part of footer' '
 224        pristine_detach initial &&
 225        git cherry-pick -s mesg-with-cherry-footer &&
 226        cat <<-EOF >expect &&
 227                $mesg_with_cherry_footer
 228                Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
 229        EOF
 230        git log -1 --pretty=format:%B >actual &&
 231        test_cmp expect actual
 232'
 233
 234test_expect_success 'cherry-pick -x -s treats "(cherry picked from..." line as part of footer' '
 235        pristine_detach initial &&
 236        sha1=$(git rev-parse mesg-with-cherry-footer^0) &&
 237        git cherry-pick -x -s mesg-with-cherry-footer &&
 238        cat <<-EOF >expect &&
 239                $mesg_with_cherry_footer
 240                (cherry picked from commit $sha1)
 241                Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
 242        EOF
 243        git log -1 --pretty=format:%B >actual &&
 244        test_cmp expect actual
 245'
 246
 247test_expect_success 'cherry-pick preserves commit message' '
 248        pristine_detach initial &&
 249        printf "$mesg_unclean" >expect &&
 250        git log -1 --pretty=format:%B mesg-unclean >actual &&
 251        test_cmp expect actual &&
 252        git cherry-pick mesg-unclean &&
 253        git log -1 --pretty=format:%B >actual &&
 254        test_cmp expect actual
 255'
 256
 257test_done