8b9c2950e853b71887d3fcf6374cfd57c0db60e4
   1#!/bin/sh
   2
   3test_description='git p4 rename'
   4
   5. ./lib-git-p4.sh
   6
   7test_expect_success 'start p4d' '
   8        start_p4d
   9'
  10
  11# We rely on this behavior to detect for p4 move availability.
  12test_expect_success 'p4 help unknown returns 1' '
  13        (
  14                cd "$cli" &&
  15                (
  16                        p4 help client >errs 2>&1
  17                        echo $? >retval
  18                )
  19                echo 0 >expected &&
  20                test_cmp expected retval &&
  21                rm retval &&
  22                (
  23                        p4 help nosuchcommand >errs 2>&1
  24                        echo $? >retval
  25                )
  26                echo 1 >expected &&
  27                test_cmp expected retval &&
  28                rm retval
  29        )
  30'
  31
  32test_expect_success 'create files' '
  33        (
  34                cd "$cli" &&
  35                p4 client -o | sed "/LineEnd/s/:.*/:unix/" | p4 client -i &&
  36                cat >file1 <<-EOF &&
  37                A large block of text
  38                in file1 that will generate
  39                enough context so that rename
  40                and copy detection will find
  41                something interesting to do.
  42                EOF
  43                cat >file2 <<-EOF &&
  44                /*
  45                 * This blob looks a bit
  46                 * different.
  47                 */
  48                int main(int argc, char **argv)
  49                {
  50                        char text[200];
  51
  52                        strcpy(text, "copy/rename this");
  53                        printf("text is %s\n", text);
  54                        return 0;
  55                }
  56                EOF
  57                p4 add file1 file2 &&
  58                p4 submit -d "add files"
  59        )
  60'
  61
  62# Rename a file and confirm that rename is not detected in P4.
  63# Rename the new file again with detectRenames option enabled and confirm that
  64# this is detected in P4.
  65# Rename the new file again adding an extra line, configure a big threshold in
  66# detectRenames and confirm that rename is not detected in P4.
  67# Repeat, this time with a smaller threshold and confirm that the rename is
  68# detected in P4.
  69test_expect_success 'detect renames' '
  70        git p4 clone --dest="$git" //depot@all &&
  71        test_when_finished cleanup_git &&
  72        (
  73                cd "$git" &&
  74                git config git-p4.skipSubmitEdit true &&
  75
  76                git mv file1 file4 &&
  77                git commit -a -m "Rename file1 to file4" &&
  78                git diff-tree -r -M HEAD &&
  79                git p4 submit &&
  80                p4 filelog //depot/file4 >filelog &&
  81                ! grep " from //depot" filelog &&
  82
  83                git mv file4 file5 &&
  84                git commit -a -m "Rename file4 to file5" &&
  85                git diff-tree -r -M HEAD &&
  86                git config git-p4.detectRenames true &&
  87                git p4 submit &&
  88                p4 filelog //depot/file5 >filelog &&
  89                grep " from //depot/file4" filelog &&
  90
  91                git mv file5 file6 &&
  92                echo update >>file6 &&
  93                git add file6 &&
  94                git commit -a -m "Rename file5 to file6 with changes" &&
  95                git diff-tree -r -M HEAD &&
  96                level=$(git diff-tree -r -M HEAD | sed 1d | cut -f1 | cut -d" " -f5 | sed "s/R0*//") &&
  97                test -n "$level" && test "$level" -gt 0 && test "$level" -lt 98 &&
  98                git config git-p4.detectRenames $(($level + 2)) &&
  99                git p4 submit &&
 100                p4 filelog //depot/file6 >filelog &&
 101                ! grep " from //depot" filelog &&
 102
 103                git mv file6 file7 &&
 104                echo update >>file7 &&
 105                git add file7 &&
 106                git commit -a -m "Rename file6 to file7 with changes" &&
 107                git diff-tree -r -M HEAD &&
 108                level=$(git diff-tree -r -M HEAD | sed 1d | cut -f1 | cut -d" " -f5 | sed "s/R0*//") &&
 109                test -n "$level" && test "$level" -gt 2 && test "$level" -lt 100 &&
 110                git config git-p4.detectRenames $(($level - 2)) &&
 111                git p4 submit &&
 112                p4 filelog //depot/file7 >filelog &&
 113                grep " from //depot/file6" filelog
 114        )
 115'
 116
 117# Copy a file and confirm that copy is not detected in P4.
 118# Copy a file with detectCopies option enabled and confirm that copy is not
 119# detected in P4.
 120# Modify and copy a file with detectCopies option enabled and confirm that copy
 121# is detected in P4.
 122# Copy a file with detectCopies and detectCopiesHarder options enabled and
 123# confirm that copy is detected in P4.
 124# Modify and copy a file, configure a bigger threshold in detectCopies and
 125# confirm that copy is not detected in P4.
 126# Modify and copy a file, configure a smaller threshold in detectCopies and
 127# confirm that copy is detected in P4.
 128test_expect_success 'detect copies' '
 129        git p4 clone --dest="$git" //depot@all &&
 130        test_when_finished cleanup_git &&
 131        (
 132                cd "$git" &&
 133                git config git-p4.skipSubmitEdit true &&
 134
 135                cp file2 file8 &&
 136                git add file8 &&
 137                git commit -a -m "Copy file2 to file8" &&
 138                git diff-tree -r -C HEAD &&
 139                git p4 submit &&
 140                p4 filelog //depot/file8 &&
 141                p4 filelog //depot/file8 | test_must_fail grep -q "branch from" &&
 142
 143                cp file2 file9 &&
 144                git add file9 &&
 145                git commit -a -m "Copy file2 to file9" &&
 146                git diff-tree -r -C HEAD &&
 147                git config git-p4.detectCopies true &&
 148                git p4 submit &&
 149                p4 filelog //depot/file9 &&
 150                p4 filelog //depot/file9 | test_must_fail grep -q "branch from" &&
 151
 152                echo "file2" >>file2 &&
 153                cp file2 file10 &&
 154                git add file2 file10 &&
 155                git commit -a -m "Modify and copy file2 to file10" &&
 156                git diff-tree -r -C HEAD &&
 157                git p4 submit &&
 158                p4 filelog //depot/file10 &&
 159                p4 filelog //depot/file10 | grep -q "branch from //depot/file" &&
 160
 161                cp file2 file11 &&
 162                git add file11 &&
 163                git commit -a -m "Copy file2 to file11" &&
 164                git diff-tree -r -C --find-copies-harder HEAD &&
 165                src=$(git diff-tree -r -C --find-copies-harder HEAD | sed 1d | cut -f2) &&
 166                case "$src" in
 167                file2 | file10) : ;; # happy
 168                *) false ;; # not
 169                esac &&
 170                git config git-p4.detectCopiesHarder true &&
 171                git p4 submit &&
 172                p4 filelog //depot/file11 &&
 173                p4 filelog //depot/file11 | grep -q "branch from //depot/file" &&
 174
 175                cp file2 file12 &&
 176                echo "some text" >>file12 &&
 177                git add file12 &&
 178                git commit -a -m "Copy file2 to file12 with changes" &&
 179                git diff-tree -r -C --find-copies-harder HEAD &&
 180                level=$(git diff-tree -r -C --find-copies-harder HEAD | sed 1d | cut -f1 | cut -d" " -f5 | sed "s/C0*//") &&
 181                test -n "$level" && test "$level" -gt 0 && test "$level" -lt 98 &&
 182                src=$(git diff-tree -r -C --find-copies-harder HEAD | sed 1d | cut -f2) &&
 183                case "$src" in
 184                file10 | file11) : ;; # happy
 185                *) false ;; # not
 186                esac &&
 187                git config git-p4.detectCopies $(($level + 2)) &&
 188                git p4 submit &&
 189                p4 filelog //depot/file12 &&
 190                p4 filelog //depot/file12 | test_must_fail grep -q "branch from" &&
 191
 192                cp file2 file13 &&
 193                echo "different text" >>file13 &&
 194                git add file13 &&
 195                git commit -a -m "Copy file2 to file13 with changes" &&
 196                git diff-tree -r -C --find-copies-harder HEAD &&
 197                level=$(git diff-tree -r -C --find-copies-harder HEAD | sed 1d | cut -f1 | cut -d" " -f5 | sed "s/C0*//") &&
 198                test -n "$level" && test "$level" -gt 2 && test "$level" -lt 100 &&
 199                src=$(git diff-tree -r -C --find-copies-harder HEAD | sed 1d | cut -f2) &&
 200                case "$src" in
 201                file10 | file11 | file12) : ;; # happy
 202                *) false ;; # not
 203                esac &&
 204                git config git-p4.detectCopies $(($level - 2)) &&
 205                git p4 submit &&
 206                p4 filelog //depot/file13 &&
 207                p4 filelog //depot/file13 | grep -q "branch from //depot/file"
 208        )
 209'
 210
 211# See if configurables can be set, and in particular if the run.move.allow
 212# variable exists, which allows admins to disable the "p4 move" command.
 213test_expect_success 'p4 configure command and run.move.allow are available' '
 214        p4 configure show run.move.allow >out ; retval=$? &&
 215        test $retval = 0 &&
 216        {
 217                egrep ^run.move.allow: out &&
 218                test_set_prereq P4D_HAVE_CONFIGURABLE_RUN_MOVE_ALLOW ||
 219                true
 220        } || true
 221'
 222
 223# If move can be disabled, turn it off and test p4 move handling
 224test_expect_success P4D_HAVE_CONFIGURABLE_RUN_MOVE_ALLOW \
 225                    'do not use p4 move when administratively disabled' '
 226        test_when_finished "p4 configure set run.move.allow=1" &&
 227        p4 configure set run.move.allow=0 &&
 228        (
 229                cd "$cli" &&
 230                echo move-disallow-file >move-disallow-file &&
 231                p4 add move-disallow-file &&
 232                p4 submit -d "add move-disallow-file"
 233        ) &&
 234        test_when_finished cleanup_git &&
 235        git p4 clone --dest="$git" //depot &&
 236        (
 237                cd "$git" &&
 238                git config git-p4.skipSubmitEdit true &&
 239                git config git-p4.detectRenames true &&
 240                git mv move-disallow-file move-disallow-file-moved &&
 241                git commit -m "move move-disallow-file" &&
 242                git p4 submit
 243        )
 244'
 245
 246test_expect_success 'kill p4d' '
 247        kill_p4d
 248'
 249
 250test_done