Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the "humanish" part of a git repository

Tags:

git

scripting

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?

like image 235
johanv Avatar asked Dec 12 '12 12:12

johanv


1 Answers

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).

like image 175
VonC Avatar answered Sep 30 '22 06:09

VonC