Here's a bash function that will print the repository name (if it has been properly set up):
__get_reponame ()
{
local gitdir=$(git rev-parse --git-dir)
if [ $(cat ${gitdir}/description) != "Unnamed repository; edit this file 'description' to name the repository." ]; then
cat ${gitdir}/description
else
echo "Unnamed repository!"
fi
}
Explanation:
local gitdir=$(git rev-parse --git-dir)
This executes git rev-parse --git-dir
, which prints the full path to the .git
directory of the currrent repository. It stores the path in $gitdir
.
if [ $(cat ${gitdir}/description) != "..." ]; then
This executes cat ${gitdir}/description
, which prints the contents of the .git/description
of your current repository. If you've properly named your repository, it will print a name. Otherwise, it will print Unnamed repository; edit this file 'description' to name the repository.
cat ${gitdir}/description
If the repo was properly named, then print the contents.
else
Otherwise...
echo "Unnamed repository!"
Tell the user that the repo was unnamed.
Something similar is implemented in this script.