|
50 | 50 | fi
|
51 | 51 | fi
|
52 | 52 |
|
53 |
| - MERGE_OPT= |
54 |
| - ff=$(git config --get merge.ff || true) |
55 |
| - if [[ "$ff" == "only" ]]; then |
56 |
| - MERGE_OPT="--ff" |
57 |
| - fi |
58 | 53 |
|
59 | 54 | echo "Copying $CURRENT_FILENAME into $DESTINATION_FILENAME"
|
60 | 55 |
|
61 |
| - INTERMEDIATE_FILENAME="${CURRENT_FILENAME//\//__}-move-to-${DESTINATION_FILENAME//\//__}" |
62 |
| - |
63 |
| - # We keep the existing file on the side in a commit |
64 |
| - git mv "${CURRENT_FILENAME}" "${INTERMEDIATE_FILENAME}" |
65 |
| - git commit -nm "Keep $CURRENT_FILENAME" |
| 56 | + # Pre-check that the source and destination will work |
| 57 | + git mv --dry-run "${CURRENT_FILENAME}" "${DESTINATION_FILENAME}" |
66 | 58 |
|
67 |
| - # We come back to the previous state and revert that change |
68 |
| - INTERMEDIATE_SAVED=$(git rev-parse HEAD) |
69 |
| - git reset --hard HEAD^ |
| 59 | + # Make a new branch and switch to it |
| 60 | + BRANCH_NAME="git-cp-$(date +%s)" |
| 61 | + git checkout -b "$BRANCH_NAME" |
70 | 62 |
|
71 |
| - # We move the file to its new destination |
| 63 | + # Move the original file to the new destination, in this branch |
72 | 64 | git mv "${CURRENT_FILENAME}" "${DESTINATION_FILENAME}"
|
73 |
| - git commit -nm "Copy $CURRENT_FILENAME into $DESTINATION_FILENAME" |
| 65 | + git commit -nm "--Duplicate $CURRENT_FILENAME history into $DESTINATION_FILENAME" |
74 | 66 |
|
75 |
| - # We come back to the previous state and revert that change again |
76 |
| - DESTINATION_SAVED=$(git rev-parse HEAD) |
77 |
| - git reset --hard HEAD^ |
| 67 | + # Restore the original file to keep it in the history |
| 68 | + git checkout HEAD~ "${CURRENT_FILENAME}" |
| 69 | + git commit -nm "--Restore $CURRENT_FILENAME" |
78 | 70 |
|
79 |
| - # We keep both files |
80 |
| - # shellcheck disable=SC2086 |
81 |
| - git merge $MERGE_OPT "${DESTINATION_SAVED}" "${INTERMEDIATE_SAVED}" -m "Duplicate ${CURRENT_FILENAME} history." |
| 71 | + # Switch to the original branch and merge this back in. |
| 72 | + git checkout - |
| 73 | + git merge --no-ff "$BRANCH_NAME" -m "Copy $CURRENT_FILENAME into $DESTINATION_FILENAME" |
82 | 74 |
|
83 |
| - # We get back our original name |
84 |
| - git mv "${INTERMEDIATE_FILENAME}" "${CURRENT_FILENAME}" |
85 |
| - git commit -nm "Set back ${CURRENT_FILENAME} file" |
| 75 | + # We're now done with the branch, so delete it. |
| 76 | + # We shouldn't need -D here, as we've already merged it back in. |
| 77 | + git branch -d "$BRANCH_NAME" |
86 | 78 | fi
|
0 commit comments