Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change git-cp to use cleaner branch approach #1169

Merged
merged 5 commits into from
Oct 8, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 15 additions & 23 deletions bin/git-cp
Original file line number Diff line number Diff line change
Expand Up @@ -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-cp-$(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 "Copying $CURRENT_FILENAME into $DESTINATION_FILENAME"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This produces a commit with the repeated message (there is already a message Copying .... before it).
Could we optimize it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be happy to change the commit messages to be more descriptive. The reason I made them like this is because this merge message is the key one directly on the branch, so I figured it should be a summary message for the whole operation.

Here's an example of what results when I used this version of git-cp, run thrice, to turn one file into four copies--setting up for a commit that prunes down all four into just the parts each should keep:
image

With this context, what do you think are the best messages?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you prefer these messages?
image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These messages look good to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! I have pushed the better commit messages and taken this PR out of Draft. Thanks, @spacewander .


# 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
Loading