Skip to content

fix(cp): refuse to run with staged changes or an edited source file - #1300

Open
jayhemnani9910 wants to merge 2 commits into
tj:mainfrom
jayhemnani9910:fix/cp-dirty-tree
Open

jayhemnani9910 wants to merge 2 commits into
tj:mainfrom
jayhemnani9910:fix/cp-dirty-tree

Conversation

@jayhemnani9910

Copy link
Copy Markdown
Contributor

git cp makes the copy with two commits on a temporary branch, and those commits take the whole index. Anything already staged ends up in the copy commits and is merged into the current branch:

echo b2 >> b.txt; git add b.txt
git cp a.txt c.txt
git log --stat   # "--Duplicate a.txt history into c.txt" also changes b.txt

If the source file has unsaved edits, the edit is moved into the copy and git checkout - then stops ("Please commit your changes or stash them"). The repo is left on git-cp-<timestamp>, and the source is back to its committed text.

git cp now stops before doing anything if something is staged or the source file has changes. Unstaged changes to other files are not touched and still work as before.

Testing: added tests/git-cp.bats. The two refusal tests fail on master and pass with this change, and the full bats suite passes.

AI tools assisted with this contribution.

Comment thread bin/git-cp Outdated

# The copy is made with commits, which would take in anything staged
# and any unsaved edit of the source file.
if ! git diff --cached --quiet || ! git diff --quiet -- "$CURRENT_FILENAME"; then

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

git diff --quiet -- "$CURRENT_FILENAME" treat name as pathspec, not literal. Try git diff --quiet -- ":(literal)$CURRENT_FILENAME"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 8502310, with a test that copies a file named fil? while file has edits.

Comment thread bin/git-cp
echo 1>&2 "You have staged changes or changes to $CURRENT_FILENAME."
echo 1>&2 "Commit or stash them first."
exit 50
fi

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Better to guard run after destination check

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved it after the */ destination check in 8502310, so it runs before any directory is created.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants