jimeh / git-aware-prompt

Display current Git branch name in your terminal prompt when in a Git working directory.
Creative Commons Zero v1.0 Universal
2.15k stars 340 forks source link

Add a variable for showing repo name #65

Open paulaltin opened 5 years ago

paulaltin commented 5 years ago

I find that when working with multiple repositories – or especially with git submodules – it can be useful to see in the prompt which one I'm currently in. (This will often be the current working directory, but not always.)

AaronDMarasco-VSI commented 5 years ago

IMHO this is kind of useless; it uses the local file system to assume that you checked it out in a way that matches the repo name. Personally, I almost never do that.

I think it would be much more useful if it used this instead: basename "$(git config --local remote.origin.url)" .git

(The quotes are if, for some reason I can't think of, they undefined 'origin'; it will return blank.)

paulaltin commented 5 years ago

Hi @AaronDMarasco-VSI, thanks for looking at this.

I guess everyone has their own way of using Git. Personally, I always check out repositories using git clone so the local directory name does match, but my remote isn't always called "origin".

However, we should be able to make it robust to both use cases. How about something like this:

if repo=$(git config --local remote.origin.url 2> /dev/null); then
    git_repo="($(basename "$repo" .git))"
elif repo=$(git rev-parse --show-toplevel 2> /dev/null); then
    git_repo="($(basename "$repo"))"
else
    git_repo=""
fi
AaronDMarasco-VSI commented 5 years ago

@paulaltin that sounds good to me if you add some inline comments to each concerning what you're trying to compute (I know rev-parse is kinda esoteric).

That said, I'm just a bystander here; I have no special privs on this repo.

paulaltin commented 5 years ago

OK, I've updated it and added some inline comments. Thanks for your input @AaronDMarasco-VSI

7urkm3n commented 5 years ago

@paulaltin No need it, if repo name is huge then its gonna annoy. Ignore, nor add into alternate preferences, do not suggest it to make it default.

Default: Yes! PROMPT_COMMAND="find_git_branch; find_git_dirty; $PROMPT_COMMAND" No! PROMPT_COMMAND="find_git_repo; find_git_branch; find_git_dirty; $PROMPT_COMMAND"

lethosor commented 5 years ago

find_git_repo just makes the variable available. It doesn't put it in your prompt. Leaving it in PROMPT_COMMAND is reasonable if you ask me; it's likely much faster than find_git_dirty.