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

Allow git cp to create destination folder automatically #1091

Merged
merged 4 commits into from
Oct 25, 2023
Merged
Changes from 2 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
12 changes: 12 additions & 0 deletions bin/git-cp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ else
exit 40
fi

if [[ "$DESTINATION_FILENAME" == */ ]]; then
echo 1>&2 "$DESTINATION_FILENAME is not a file path."
exit 80
fi
DESTINATION_DIR="${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.

What if the DESTINATION_FILENAME doesn't contain the directory, like ./foo?

Copy link
Contributor Author

@weiw005 weiw005 Oct 23, 2023

Choose a reason for hiding this comment

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

Good question! Although the code still works for ./foo (because DESTINATION_DIR will simply be ./ and creating it is a harmless no-op), another case that I tested did lead to incorrect behavior which is when the DESTINATION_FILENAME is just a filename. The previous revision would create a folder of that name, and make a copy of the source file into that folder (rather than copy to a file of that name).

I've now switched the condition to be checking whether DESTINATION_FILENAME contains any slash characters - if not the folder creation is skipped.

if [ ! -d "$DESTINATION_DIR" ]; then
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can use mkdir -p directly as the cmd won't return error if the dir exists?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Make sense. Removed this check.

if ! mkdir -p "$DESTINATION_DIR"; then
echo 1>&2 "Failed to create destination directory: $DESTINATION_DIR"
exit 160
fi
fi

MERGE_OPT=
ff=$(git config --get merge.ff)
if [[ "$ff" == "only" ]]; then
Expand Down
Loading