2

How can I get the current remote for my post-receive hook?

I need to get both my current remote and my current branch:

  • I know how to get my current branch (using: branch=$(git rev-parse --symbolic --abbrev-ref $1) )
  • but I don't know how to get my current remote.

Any suggestions?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250

2 Answers2

3

A post-receive hook is on the server side (the side referenced by a remote url).

As such, that hook has no notion of "remote": it has no knowledge about how the repo it is in has been accessed.


Any git config branch.$branch.remote command wouldn't work on the server upstream side, since a remote is set on the client downstream repo.


The OP Omar Chacin adds in the comments:

Basically what I am trying to do is deploy dozens of websites (each website has a develop and master branch).

The thing is that I know how to select which branch I want, but how can get the website I want to deploy?
Let's say I have three websites I want to deploy separately. How can I push either my master or develop branch to one of these websites (using UNC)?

Thus usual solutions could involve:

  • having one branch per destination repo (that way, the name of the branch alone is enough for the hook to checkout in the right target site)
  • or: push a commit which has in its commit message the relevant information (which target site to consider)
  • or: having n bare repos, each with their post-receive hook: that way, you push to the right bare repo url, upstream remote repo which in turn update the right live site.

I would go with the last solution, as the UNC (remote) used from the client would make explicit the intended target site to be updated on the server.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for the information Von. However, I tried these commands and didn't work. Basically what I am trying to do is deploy dozens of websites (each website has a develop and master branch). The thing is that I know how to select which branch I want, but how can get the website I want to deploy? Let's say I have three websites I want to deploy separately. How can I push either my master or develop branch to one of these websites (using UNC)? – Omar Chacin Jun 24 '14 at 10:37
  • @OmarChacin I have edited the answer for you to select. – VonC Jun 24 '14 at 10:51
  • Thanks for the suggestions @VonC I will go with the latter. Thanks again! – Omar Chacin Jun 24 '14 at 11:04
0

Git does not really have a concept of the "current remote". You have a current branch, and that may or may not have a remote associated with it, and may have a "pushremote" set to override the remote (in which case you'd need to change the commands below).

You can find the remote that the current branch will be pushed to (by default) using git config branch.<name>.remote where <name> is the name of the current branch. That will give you the name of the remote, you can get its URL with git config remote.<name>.url, or full information about it with git remote show <name>

So if $branch is the branch name, this will tell you the push URL:

git config remote.$(git config branch.$branch.remote).url

To handle the case where there's a pushremote overriding the remote:

git config remote.$(git config branch.$branch.pushremote || git config branch.$branch.remote).url
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • Thanks for the information Jonathan. However, I tried these commands and didn't work. Basically what I am trying to do is deploy dozens of websites (each website has a develop and master branch). The thing is that I know how to select which branch I want, but how can get the website I want to deploy? Let's say I have three websites I want to deploy separately. How can I push either my master or develop branch to one of these websites (using UNC)? – Omar Chacin Jun 24 '14 at 10:30