25

With github we can use URLs of the form

www.github.com/username/repo/filename.c#L20-L45

Note the #L20-L45 at the end which highlights the 20th to 45th lines of code when the page loads in the browser.

This convention doesn't appear to work the same way in stash / bitbucket - is it possible to link to certain lines of code somehow?

stevec
  • 41,291
  • 27
  • 223
  • 311

3 Answers3

29

In BitBucket, an URL should end with #lines-x:y

Example:

https://bitbucket.org/stangenberg/docker-docker/src/1ac537b84855db2cc5ee0caf31c782900e094d8e/docker.sh#lines-13:15

Regarding Stash (now 2019+ renamed BitBucket Server), Stash 3.8 in 2015 is supposed to support deep linking with multiline selection. Issue BSERV-2477

With just shift+click, a developer can select a range of lines, then add separate lines to the selection with command+click (or control+click), then copy the URL and share their selection with other developers.

https://info.seibert-media.net/download/attachments/11113140/Stash-38.png?version=1&modificationDate=1474904524000&api=v2

The OP confirms in the comments:

The stash url generated has #13-15 at the end, i.e. it is of the form:

https://bitbucket.org/stangenberg/docker-docker/src/1ac537b84855db2cc5ee0caf31c782900e094d8e/docker.sh#13-15

As cellepo notes in the comments:

  • this cannot be done in a pull request/code-review page (https://bitbucket.../pull-requests/...),
    but
  • it CAN be done on a source page (https://bitbucket.../src/...) like in the aforementioned example URL.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Thanks, works perfectly. The stash url generated has `#13-15` at the end, i.e. it is of the form `https://bitbucket.org/stangenberg/docker-docker/src/1ac537b84855db2cc5ee0caf31c782900e094d8e/docker.sh#13-15` – stevec Jan 15 '20 at 08:16
  • 1
    @user5783745 Great! I have included your comment in the answer for more visibility. – VonC Jan 15 '20 at 08:51
  • Note that this can't be done on a pull-request/code-review page (`https://bitbucket.../pull-requests/...`), but CAN be done on a source page (`https://bitbucket.../src/...`) like in this Answer's example URL. – cellepo Jul 08 '20 at 19:19
  • 1
    @cellepo Good point. I have included your comment in the answer for more visibility. – VonC Jul 08 '20 at 22:04
3

It appears to me that there may be different syntax for the the url lines ending, maybe according to different Bitbucket version...

@VonC Answer is good - I think it worked for me before, but apparently it does not for me anymore, and I'm guessing different BitBucket version may make the difference..

Whatever the reason for the difference, if @VonC Answer does not work for you, try this (emphasis on #7-8):

bitbucket.your.domain/.../your/project/structure/.../yourfile.filetype#7-8

The UI rendering of that is a bit different from @VonC linked example - just blue highlighting of line-number gutter (instead of yellow highlighting of code):

enter image description here That rendering looks looks identical to that from @VonC for [non-contiguous] multiline selection - and in my Bitbucket version, non-contiguous selection with CMD + click similarly works (as below - emphasis on ,10,12)

bitbucket.your.domain/.../your/project/structure/.../yourfile.filetype#7-8,10,12
cellepo
  • 4,001
  • 2
  • 38
  • 57
  • Oh, I see now @VonC accounts for this per their reference at the end of their Answer to OP comments - I'll just leave this as providing more details/focus on that case :) – cellepo Jul 31 '20 at 20:09
  • For some reason, the very *last* code line can not be selected with cursor (maybe a UI bug), but it can be specified manually by typing its line number into the URL. – cellepo Jul 31 '20 at 20:11
  • Wow, you're quick on the ball @VonC! Thanks again for your prior Answer :) – cellepo Jul 31 '20 at 20:15
  • No problem. It is easy to be quick when you *never* leave (https://meta.stackexchange.com/q/122976/6309). – VonC Jul 31 '20 at 20:21
1

For highlighting multiple blocks in BitBucket, an URL should end with #lines-a:b,c:d, ...

Caveat: Remove %20 if it comes for whitespace in URL

afghani
  • 467
  • 5
  • 7