Joshua Soileau
Joshua Soileau

Reputation: 3015

Get git repo name and github username inside BASH script?

I'm trying to create a bash script to open a Github pull request in a browser window.

Here's the command I need to run:


open https://github.com/ParentOwner/RepoName/compare/develop...JoshuaSoileau:CurrentBranch


Where the things in bold need to be dynamic.

I need to figure out how to pull the following things in BASH:

RepoName       -   name of the repo
develop        -   ARGUMENT to my bash script
JoshuaSoileau  -   github username of the current user
CurrentBranch  -   name of the currently checked out git branch.

I know how to do the following:

RepoName       -   ??
develop        -   $1 argument in my bash script
JoshuaSoileau  -   ??
CurrentBranch  -   $(git rev-parse --abbrev-ref HEAD)

How do I pull the 1. RepoName and 2. Current github username in a BASH script?

This is what I have so far:

git-open-merge() {
    open https://github.com/ParentOwner/??/compare/$1...??:$(git rev-parse --abbrev-ref HEAD)
}

And it's called like this:

git-open-merge develop

Upvotes: 1

Views: 1677

Answers (2)

Ivan
Ivan

Reputation: 13

To get the repo name in a bash script I do:

git config --get remote.origin.url | rev | cut -d/ -f1 | rev | cut -d. -f1

Explanation:

  1. git config --get remote.origin.url returns something like [email protected]:my-project/my-repo-name.git
  2. rev reverses the string
  3. cut -d/ -f1 cuts the string on the / then gets the first field (really the last one when in original order)
  4. rev again to put the order back
  5. cut -d. -f1 to get just the repo name and not the .git

Upvotes: 0

tchap
tchap

Reputation: 3432

You can use :

git config --get-regexp user.name

For the user name. for the repository name, you may have different repositories with different names, so I guess parsing :

git remote get-url origin

could help if you only care about the origin ? The format would be (prefixes would differ between ssh or https) [email protected]:{github_handle}/{repo_name}.git

Upvotes: 2

Related Questions