| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='Test ref-filter and pretty APIs for commit and tag messages using CRLF' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | LIB_CRLF_BRANCHES="" |
| 8 | |
| 9 | create_crlf_ref () { |
| 10 | branch="$1" && |
| 11 | cat >.crlf-orig-$branch.txt && |
| 12 | append_cr <.crlf-orig-$branch.txt >.crlf-message-$branch.txt && |
| 13 | grep 'Subject' .crlf-orig-$branch.txt | tr '\n' ' ' | sed 's/[ ]*$//' | tr -d '\n' >.crlf-subject-$branch.txt && |
| 14 | grep 'Body' .crlf-orig-$branch.txt | append_cr >.crlf-body-$branch.txt && |
| 15 | LIB_CRLF_BRANCHES="${LIB_CRLF_BRANCHES} ${branch}" && |
| 16 | test_tick && |
| 17 | hash=$(git commit-tree HEAD^{tree} -p HEAD -F .crlf-message-${branch}.txt) && |
| 18 | git branch ${branch} ${hash} && |
| 19 | git tag tag-${branch} ${branch} -F .crlf-message-${branch}.txt --cleanup=verbatim |
| 20 | } |
| 21 | |
| 22 | create_crlf_refs () { |
| 23 | create_crlf_ref crlf <<-\EOF && |
| 24 | Subject first line |
| 25 | |
| 26 | Body first line |
| 27 | Body second line |
| 28 | EOF |
| 29 | create_crlf_ref crlf-empty-lines-after-subject <<-\EOF && |
| 30 | Subject first line |
| 31 | |
| 32 | |
| 33 | Body first line |
| 34 | Body second line |
| 35 | EOF |
| 36 | create_crlf_ref crlf-two-line-subject <<-\EOF && |
| 37 | Subject first line |
| 38 | Subject second line |
| 39 | |
| 40 | Body first line |
| 41 | Body second line |
| 42 | EOF |
| 43 | create_crlf_ref crlf-two-line-subject-no-body <<-\EOF && |
| 44 | Subject first line |
| 45 | Subject second line |
| 46 | EOF |
| 47 | create_crlf_ref crlf-two-line-subject-no-body-trailing-newline <<-\EOF |
| 48 | Subject first line |
| 49 | Subject second line |
| 50 | |
| 51 | EOF |
| 52 | } |
| 53 | |
| 54 | test_crlf_subject_body_and_contents() { |
| 55 | command_and_args="$@" && |
| 56 | command=$1 && |
| 57 | if test ${command} = "branch" || test ${command} = "for-each-ref" || test ${command} = "tag" |
| 58 | then |
| 59 | atoms="(contents:subject) (contents:body) (contents)" |
| 60 | elif test ${command} = "log" || test ${command} = "show" |
| 61 | then |
| 62 | atoms="s b B" |
| 63 | fi && |
| 64 | files="subject body message" && |
| 65 | while test -n "${atoms}" |
| 66 | do |
| 67 | set ${atoms} && atom=$1 && shift && atoms="$*" && |
| 68 | set ${files} && file=$1 && shift && files="$*" && |
| 69 | test_expect_success "${command}: --format='%${atom}' works with messages using CRLF" " |
| 70 | rm -f expect && |
| 71 | for ref in ${LIB_CRLF_BRANCHES} |
| 72 | do |
| 73 | cat .crlf-${file}-\"\${ref}\".txt >>expect && |
| 74 | printf \"\n\" >>expect || return 1 |
| 75 | done && |
| 76 | git $command_and_args --format=\"%${atom}\" >actual && |
| 77 | test_cmp expect actual |
| 78 | " |
| 79 | done |
| 80 | } |
| 81 | |
| 82 | |
| 83 | test_expect_success 'Setup refs with commit and tag messages using CRLF' ' |
| 84 | test_commit initial && |
| 85 | create_crlf_refs |
| 86 | ' |
| 87 | |
| 88 | test_expect_success 'branch: --verbose works with messages using CRLF' ' |
| 89 | rm -f expect && |
| 90 | for branch in $LIB_CRLF_BRANCHES |
| 91 | do |
| 92 | printf " " >>expect && |
| 93 | cat .crlf-subject-${branch}.txt >>expect && |
| 94 | printf "\n" >>expect || return 1 |
| 95 | done && |
| 96 | git branch -v >tmp && |
| 97 | # Remove first two columns, and the line for the currently checked out branch |
| 98 | current=$(git branch --show-current) && |
| 99 | awk "/$current/ { next } { \$1 = \$2 = \"\" } 1" <tmp >actual && |
| 100 | test_cmp expect actual |
| 101 | ' |
| 102 | |
| 103 | test_crlf_subject_body_and_contents branch --list crlf* |
| 104 | |
| 105 | test_crlf_subject_body_and_contents tag --list tag-crlf* |
| 106 | |
| 107 | test_crlf_subject_body_and_contents for-each-ref refs/heads/crlf* |
| 108 | |
| 109 | test_expect_success 'log: --oneline works with messages using CRLF' ' |
| 110 | for branch in $LIB_CRLF_BRANCHES |
| 111 | do |
| 112 | cat .crlf-subject-${branch}.txt >expect && |
| 113 | printf "\n" >>expect && |
| 114 | git log --oneline -1 ${branch} >tmp-branch && |
| 115 | git log --oneline -1 tag-${branch} >tmp-tag && |
| 116 | cut -d" " -f2- <tmp-branch >actual-branch && |
| 117 | cut -d" " -f2- <tmp-tag >actual-tag && |
| 118 | test_cmp expect actual-branch && |
| 119 | test_cmp expect actual-tag || return 1 |
| 120 | done |
| 121 | ' |
| 122 | |
| 123 | test_crlf_subject_body_and_contents log --all --reverse --grep Subject |
| 124 | |
| 125 | test_crlf_subject_body_and_contents show $LIB_CRLF_BRANCHES |
| 126 | |
| 127 | test_done |