From c2366d92990da71ee2cc0d3c5166fc471186d7e6 Mon Sep 17 00:00:00 2001 From: Mattias Andersson Date: Sun, 29 Sep 2024 14:29:10 -0700 Subject: [PATCH 1/4] 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 --- bin/git-cp | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/bin/git-cp b/bin/git-cp index 091182482..6456e017e 100755 --- a/bin/git-cp +++ b/bin/git-cp @@ -50,37 +50,29 @@ else 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" - INTERMEDIATE_FILENAME="${CURRENT_FILENAME//\//__}-move-to-${DESTINATION_FILENAME//\//__}" - - # We keep the existing file on the side in a commit - git mv "${CURRENT_FILENAME}" "${INTERMEDIATE_FILENAME}" - git commit -nm "Keep $CURRENT_FILENAME" + # Pre-check that the source and destination will work + git mv --dry-run "${CURRENT_FILENAME}" "${DESTINATION_FILENAME}" - # We come back to the previous state and revert that change - INTERMEDIATE_SAVED=$(git rev-parse HEAD) - git reset --hard HEAD^ + # Make a new branch and switch to it + BRANCH_NAME="git-split-$(date +%s)" + git checkout -b "$BRANCH_NAME" - # We move the file to its new destination + # Move the original file to the new destination, in this branch git mv "${CURRENT_FILENAME}" "${DESTINATION_FILENAME}" git commit -nm "Copy $CURRENT_FILENAME into $DESTINATION_FILENAME" - # We come back to the previous state and revert that change again - DESTINATION_SAVED=$(git rev-parse HEAD) - git reset --hard HEAD^ + # Restore the original file to keep it in the history + git checkout HEAD~ "${CURRENT_FILENAME}" + git commit -nm "Restore $CURRENT_FILENAME" - # We keep both files - # shellcheck disable=SC2086 - git merge $MERGE_OPT "${DESTINATION_SAVED}" "${INTERMEDIATE_SAVED}" -m "Duplicate ${CURRENT_FILENAME} history." + # Switch to the original branch and merge this back in. + git checkout - + git merge --no-ff "$BRANCH_NAME" -m "Split $CURRENT_FILENAME into $DESTINATION_FILENAME" - # We get back our original name - git mv "${INTERMEDIATE_FILENAME}" "${CURRENT_FILENAME}" - git commit -nm "Set back ${CURRENT_FILENAME} file" + # We're now done with the branch, so delete it. + # We shouldn't need -D here, as we've already merged it back in. + git branch -d "$BRANCH_NAME" fi From d4264469fdcfbb6c7dfe7bf07701827b511a1a33 Mon Sep 17 00:00:00 2001 From: Mattias Andersson Date: Sun, 29 Sep 2024 14:30:54 -0700 Subject: [PATCH 2/4] Name the temporary branch to better fit in with git-extras --- bin/git-cp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/git-cp b/bin/git-cp index 6456e017e..107c380b5 100755 --- a/bin/git-cp +++ b/bin/git-cp @@ -57,7 +57,7 @@ else git mv --dry-run "${CURRENT_FILENAME}" "${DESTINATION_FILENAME}" # Make a new branch and switch to it - BRANCH_NAME="git-split-$(date +%s)" + BRANCH_NAME="git-cp-$(date +%s)" git checkout -b "$BRANCH_NAME" # Move the original file to the new destination, in this branch From 83cb9c397fca891c69c2d4803027800cde542a5f Mon Sep 17 00:00:00 2001 From: Mattias Andersson Date: Sun, 29 Sep 2024 20:49:41 -0700 Subject: [PATCH 3/4] Again, git-extras naming is "copy", not "split" --- bin/git-cp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/git-cp b/bin/git-cp index 107c380b5..8777170be 100755 --- a/bin/git-cp +++ b/bin/git-cp @@ -70,7 +70,7 @@ else # Switch to the original branch and merge this back in. git checkout - - git merge --no-ff "$BRANCH_NAME" -m "Split $CURRENT_FILENAME into $DESTINATION_FILENAME" + git merge --no-ff "$BRANCH_NAME" -m "Copying $CURRENT_FILENAME into $DESTINATION_FILENAME" # We're now done with the branch, so delete it. # We shouldn't need -D here, as we've already merged it back in. From 6a2582d053e71aa409ee064d324185af17f907a7 Mon Sep 17 00:00:00 2001 From: Mattias Andersson Date: Sun, 6 Oct 2024 11:24:59 -0700 Subject: [PATCH 4/4] Make the commit messages more clear --- bin/git-cp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/git-cp b/bin/git-cp index 8777170be..cabb564b1 100755 --- a/bin/git-cp +++ b/bin/git-cp @@ -62,15 +62,15 @@ else # Move the original file to the new destination, in this branch 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" # Restore the original file to keep it in the history git checkout HEAD~ "${CURRENT_FILENAME}" - git commit -nm "Restore $CURRENT_FILENAME" + git commit -nm "--Restore $CURRENT_FILENAME" # Switch to the original branch and merge this back in. git checkout - - git merge --no-ff "$BRANCH_NAME" -m "Copying $CURRENT_FILENAME into $DESTINATION_FILENAME" + git merge --no-ff "$BRANCH_NAME" -m "Copy $CURRENT_FILENAME into $DESTINATION_FILENAME" # We're now done with the branch, so delete it. # We shouldn't need -D here, as we've already merged it back in.