2

My employer uses an on-prem bitbucket server, and it echoes back a pull request URL after I do a git push. Is there a way to have a global hook which lets me open this URL directly in my browser every time I git push from anywhere, be it a terminal or an IDE?

2 Answers 2

1

Not really: there is no post-push client-side hook.
So, as mentioned in here, you could need to make a Git wrapper script in order to:

  • detect the push
  • parse its stderr output
  • extract the URL
  • call a brwoser with it.
Sign up to request clarification or add additional context in comments.

Comments

0

Half-way answer: you can use a git alias which parses the server response output and automatically opens any (trusted) links it receives. Git push prints the remote's response on stderr. You can put this in your ~/.config/git/config (or wherever):

[alias] pusl = "! pusl() { git push \"$@\" 2> >(tee /dev/stderr | grep -o 'https://gitlab.mycompany.example/[^\" ]*' | xargs -r open) ; } ; pusl" 

This makes git pusl a transparent alias for git push which behaves exactly the same, except it also calls open ... on any trusted link (https://gitlab.mycompany.example/...) it finds in the remote's response.

open is the tool for opening links on macos--adapt it for your OS if it's different.

This doesn't answer your question completely because your editor etc still uses regular git push, so you'd need to go one level deeper: create a git command yourself in e.g. ~/bin/git, ensure that directory exists in your PATH before the official git binary, and there intercept the push command, and replace it with the above.

P.S.: while this works for invoking git pusl directly from an interactive bash, for emacs + magit I encountered 2 exceedingly odd bugs that require heretical work-arounds: my magit setup apparently calls push aliases from some weird non-bash compatible shell mode (breaking the pipe syntax), and it messes with the pipes which means the urls never make it to xargs before the subprocess dies. This awful work-around fixes both:

[alias] pusl = "! bash -c 'pusl() { git push \"$@\" 2> >(tee /dev/stderr | grep -o \"https://github.com/[^\\\" ]*\" | xargs -r open) | cat ; } ; pusl \"$@\"' bash" 

Although at this point I would be better off creating a script ~/bin/git-pusl

For completeness, you can hook this up to emacs + magit like this:

(defun hly-magit/push->pusl (argv) (pcase argv (`("push" . ,args) (cons "pusl" args)) (`,x x))) (with-eval-after-load 'magit (advice-add 'magit-run-git-async :filter-args #'hly-magit/push->pusl)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.