If I clone a git repository like this
git clone <some-repository>
git creates a directory, named as the 'humanish' part of the source repository. (according to the man page).
Now I want to create a bash script that clones a repository, 'cds' into the newly created directory, and does some stuff. Is there an easy way for the bash script to know the name of the created directory, without explicitly providing a directory to the 'git clone' command?
To add to Hiery's answer, you will find a complete shell script example in the git repo itself:contrib/examples/git-clone.sh
, with the relevant extract:
# Decide the directory name of the new repository
if test -n "$2"
then
dir="$2"
test $# = 2 || die "excess parameter to git-clone"
else
# Derive one from the repository name
# Try using "humanish" part of source repo if user didn't specify one
if test -f "$repo"
then
# Cloning from a bundle
dir=$(echo "$repo" | sed -e 's|/*\.bundle$||' -e 's|.*/||g')
else
dir=$(echo "$repo" |
sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
fi
fi
Note that it takes into account cloning a bundle (which is a repo as one file, useful when working with a repository in the cloud).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With