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

Conversation

weiw005
Copy link
Contributor

@weiw005 weiw005 commented Oct 20, 2023

fixed #1089

Copy link
Collaborator

@hyperupcall hyperupcall left a comment

Choose a reason for hiding this comment

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

It looks like there is a race and an extra subshell here. What about something like this?:

DESTINATION_FILENAME=${DESTINATION_FILENAME%/}
if ! mkdir -p "${DESTINATION_FILENAME%/*}"; then
  echo 1>&2 "Failed to create destination directory: $DESTINATION_DIR"
  exit 80
fi

@weiw005
Copy link
Contributor Author

weiw005 commented Oct 20, 2023

It looks like there is a race and an extra subshell here. What about something like this?:

DESTINATION_FILENAME=${DESTINATION_FILENAME%/}
if ! mkdir -p "${DESTINATION_FILENAME%/*}"; then
  echo 1>&2 "Failed to create destination directory: $DESTINATION_DIR"
  exit 80
fi

Thanks! Removed the subprocess command. PTAL.

(BTW my knowledge with shell script is very limited, so open to further suggestions)

bin/git-cp Outdated
exit 80
fi
DESTINATION_DIR="${DESTINATION_FILENAME%/*}"
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.

bin/git-cp Outdated
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.

@spacewander
Copy link
Collaborator

@hyperupcall
Please take a look when you have free time.

@hyperupcall
Copy link
Collaborator

The new revision looks good to me - fixed that big.

My only comment is that it seems git-cp would previously silently work if a directory name were passed, it ended with /, and the directory already existed. If this new failure is acceptable then these changes LGTM!

@weiw005
Copy link
Contributor Author

weiw005 commented Oct 24, 2023

@spacewander @hyperupcall Thanks for the review! Would either of you be able to click the merge button for me? I don't have write access to this repo.

@hyperupcall
Copy link
Collaborator

@weiw005 @spacewander will be able to do it

@spacewander spacewander merged commit 90221f2 into tj:main Oct 25, 2023
5 checks passed
@spacewander
Copy link
Collaborator

Merged. Thanks!
Also thanks @hyperupcall for the review :)

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.

Create destination folder during git cp if it doesn't exist
3 participants