mirror of
https://github.com/tj/git-extras.git
synced 2026-09-10 07:26:17 -04:00
Change git-cp to use cleaner branch approach (#1169)
* Change git-cp to use cleaner branch approach, per https://stackoverflow.com/a/46484848/32841 and https://devblogs.microsoft.com/oldnewthing/20190919-00/?p=102904 * Name the temporary branch to better fit in with git-extras * Again, git-extras naming is "copy", not "split" * Make the commit messages more clear
This commit is contained in:
parent
5c3fb1b062
commit
233686d2a2
40
bin/git-cp
40
bin/git-cp
|
|
@ -50,37 +50,29 @@ else
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
MERGE_OPT=
|
|
||||||
ff=$(git config --get merge.ff || true)
|
|
||||||
if [[ "$ff" == "only" ]]; then
|
|
||||||
MERGE_OPT="--ff"
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Copying $CURRENT_FILENAME into $DESTINATION_FILENAME"
|
echo "Copying $CURRENT_FILENAME into $DESTINATION_FILENAME"
|
||||||
|
|
||||||
INTERMEDIATE_FILENAME="${CURRENT_FILENAME//\//__}-move-to-${DESTINATION_FILENAME//\//__}"
|
# Pre-check that the source and destination will work
|
||||||
|
git mv --dry-run "${CURRENT_FILENAME}" "${DESTINATION_FILENAME}"
|
||||||
|
|
||||||
# We keep the existing file on the side in a commit
|
# Make a new branch and switch to it
|
||||||
git mv "${CURRENT_FILENAME}" "${INTERMEDIATE_FILENAME}"
|
BRANCH_NAME="git-cp-$(date +%s)"
|
||||||
git commit -nm "Keep $CURRENT_FILENAME"
|
git checkout -b "$BRANCH_NAME"
|
||||||
|
|
||||||
# We come back to the previous state and revert that change
|
# Move the original file to the new destination, in this branch
|
||||||
INTERMEDIATE_SAVED=$(git rev-parse HEAD)
|
|
||||||
git reset --hard HEAD^
|
|
||||||
|
|
||||||
# We move the file to its new destination
|
|
||||||
git mv "${CURRENT_FILENAME}" "${DESTINATION_FILENAME}"
|
git mv "${CURRENT_FILENAME}" "${DESTINATION_FILENAME}"
|
||||||
git commit -nm "Copy $CURRENT_FILENAME into $DESTINATION_FILENAME"
|
git commit -nm "--Duplicate $CURRENT_FILENAME history into $DESTINATION_FILENAME"
|
||||||
|
|
||||||
# We come back to the previous state and revert that change again
|
# Restore the original file to keep it in the history
|
||||||
DESTINATION_SAVED=$(git rev-parse HEAD)
|
git checkout HEAD~ "${CURRENT_FILENAME}"
|
||||||
git reset --hard HEAD^
|
git commit -nm "--Restore $CURRENT_FILENAME"
|
||||||
|
|
||||||
# We keep both files
|
# Switch to the original branch and merge this back in.
|
||||||
# shellcheck disable=SC2086
|
git checkout -
|
||||||
git merge $MERGE_OPT "${DESTINATION_SAVED}" "${INTERMEDIATE_SAVED}" -m "Duplicate ${CURRENT_FILENAME} history."
|
git merge --no-ff "$BRANCH_NAME" -m "Copy $CURRENT_FILENAME into $DESTINATION_FILENAME"
|
||||||
|
|
||||||
# We get back our original name
|
# We're now done with the branch, so delete it.
|
||||||
git mv "${INTERMEDIATE_FILENAME}" "${CURRENT_FILENAME}"
|
# We shouldn't need -D here, as we've already merged it back in.
|
||||||
git commit -nm "Set back ${CURRENT_FILENAME} file"
|
git branch -d "$BRANCH_NAME"
|
||||||
fi
|
fi
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue