t / t7509-commit.shon commit Merge branch 'en/merge-recursive' (ac9666f)
   1#!/bin/sh
   2#
   3# Copyright (c) 2009 Erick Mattos
   4#
   5
   6test_description='git commit --reset-author'
   7
   8. ./test-lib.sh
   9
  10author_header () {
  11        git cat-file commit "$1" |
  12        sed -n -e '/^$/q' -e '/^author /p'
  13}
  14
  15message_body () {
  16        git cat-file commit "$1" |
  17        sed -e '1,/^$/d'
  18}
  19
  20test_expect_success '-C option copies authorship and message' '
  21        echo "Initial" >foo &&
  22        git add foo &&
  23        test_tick &&
  24        git commit -m "Initial Commit" --author Frigate\ \<flying@over.world\> &&
  25        git tag Initial &&
  26        echo "Test 1" >>foo &&
  27        test_tick &&
  28        git commit -a -C Initial &&
  29        author_header Initial >expect &&
  30        author_header HEAD >actual &&
  31        test_cmp expect actual &&
  32
  33        message_body Initial >expect &&
  34        message_body HEAD >actual &&
  35        test_cmp expect actual
  36'
  37
  38test_expect_success '-C option copies only the message with --reset-author' '
  39        echo "Test 2" >>foo &&
  40        test_tick &&
  41        git commit -a -C Initial --reset-author &&
  42        echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
  43        author_header HEAD >actual &&
  44        test_cmp expect actual &&
  45
  46        message_body Initial >expect &&
  47        message_body HEAD >actual &&
  48        test_cmp expect actual
  49'
  50
  51test_expect_success '-c option copies authorship and message' '
  52        echo "Test 3" >>foo &&
  53        test_tick &&
  54        EDITOR=: VISUAL=: git commit -a -c Initial &&
  55        author_header Initial >expect &&
  56        author_header HEAD >actual &&
  57        test_cmp expect actual
  58'
  59
  60test_expect_success '-c option copies only the message with --reset-author' '
  61        echo "Test 4" >>foo &&
  62        test_tick &&
  63        EDITOR=: VISUAL=: git commit -a -c Initial --reset-author &&
  64        echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
  65        author_header HEAD >actual &&
  66        test_cmp expect actual &&
  67
  68        message_body Initial >expect &&
  69        message_body HEAD >actual &&
  70        test_cmp expect actual
  71'
  72
  73test_expect_success '--amend option copies authorship' '
  74        git checkout Initial &&
  75        echo "Test 5" >>foo &&
  76        test_tick &&
  77        git commit -a --amend -m "amend test" &&
  78        author_header Initial >expect &&
  79        author_header HEAD >actual &&
  80
  81        echo "amend test" >expect &&
  82        message_body HEAD >actual &&
  83        test_cmp expect actual
  84'
  85
  86sha1_file() {
  87        echo "$*" | sed "s#..#.git/objects/&/#"
  88}
  89remove_object() {
  90        rm -f $(sha1_file "$*")
  91}
  92no_reflog() {
  93        cp .git/config .git/config.saved &&
  94        echo "[core] logallrefupdates = false" >>.git/config &&
  95        test_when_finished "mv -f .git/config.saved .git/config" &&
  96
  97        if test -e .git/logs
  98        then
  99                mv .git/logs . &&
 100                test_when_finished "mv logs .git/"
 101        fi
 102}
 103
 104test_expect_success '--amend option with empty author' '
 105        git cat-file commit Initial >tmp &&
 106        sed "s/author [^<]* </author  </" tmp >empty-author &&
 107        no_reflog &&
 108        sha=$(git hash-object -t commit -w empty-author) &&
 109        test_when_finished "remove_object $sha" &&
 110        git checkout $sha &&
 111        test_when_finished "git checkout Initial" &&
 112        echo "Empty author test" >>foo &&
 113        test_tick &&
 114        test_must_fail git commit -a -m "empty author" --amend 2>err &&
 115        grep "empty ident" err
 116'
 117
 118test_expect_success '--amend option with missing author' '
 119        git cat-file commit Initial >tmp &&
 120        sed "s/author [^<]* </author </" tmp >malformed &&
 121        no_reflog &&
 122        sha=$(git hash-object -t commit -w malformed) &&
 123        test_when_finished "remove_object $sha" &&
 124        git checkout $sha &&
 125        test_when_finished "git checkout Initial" &&
 126        echo "Missing author test" >>foo &&
 127        test_tick &&
 128        test_must_fail git commit -a -m "malformed author" --amend 2>err &&
 129        grep "empty ident" err
 130'
 131
 132test_expect_success '--reset-author makes the commit ours even with --amend option' '
 133        git checkout Initial &&
 134        echo "Test 6" >>foo &&
 135        test_tick &&
 136        git commit -a --reset-author -m "Changed again" --amend &&
 137        echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
 138        author_header HEAD >actual &&
 139        test_cmp expect actual &&
 140
 141        echo "Changed again" >expect &&
 142        message_body HEAD >actual &&
 143        test_cmp expect actual
 144'
 145
 146test_expect_success '--reset-author and --author are mutually exclusive' '
 147        git checkout Initial &&
 148        echo "Test 7" >>foo &&
 149        test_tick &&
 150        test_must_fail git commit -a --reset-author --author="Xyzzy <frotz@nitfol.xz>"
 151'
 152
 153test_expect_success '--reset-author should be rejected without -c/-C/--amend' '
 154        git checkout Initial &&
 155        echo "Test 7" >>foo &&
 156        test_tick &&
 157        test_must_fail git commit -a --reset-author -m done
 158'
 159
 160test_expect_success 'commit respects CHERRY_PICK_HEAD and MERGE_MSG' '
 161        echo "cherry-pick 1a" >>foo &&
 162        test_tick &&
 163        git commit -am "cherry-pick 1" --author="Cherry <cherry@pick.er>" &&
 164        git tag cherry-pick-head &&
 165        git rev-parse cherry-pick-head >.git/CHERRY_PICK_HEAD &&
 166        echo "This is a MERGE_MSG" >.git/MERGE_MSG &&
 167        echo "cherry-pick 1b" >>foo &&
 168        test_tick &&
 169        git commit -a &&
 170        author_header cherry-pick-head >expect &&
 171        author_header HEAD >actual &&
 172        test_cmp expect actual &&
 173
 174        echo "This is a MERGE_MSG" >expect &&
 175        message_body HEAD >actual &&
 176        test_cmp expect actual
 177'
 178
 179test_expect_success '--reset-author with CHERRY_PICK_HEAD' '
 180        git rev-parse cherry-pick-head >.git/CHERRY_PICK_HEAD &&
 181        echo "cherry-pick 2" >>foo &&
 182        test_tick &&
 183        git commit -am "cherry-pick 2" --reset-author &&
 184        echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
 185        author_header HEAD >actual &&
 186        test_cmp expect actual
 187'
 188
 189test_done