23

I want to link to a line of code in such a way that the link will continue to work even when the file is updated in future commits.

In Github I would do this by pressing 'y' to move to a version of the page that includes the blob SHA:

https://github.com/rails/rails/blob/b49e38b76b0998b0a8312d8c08c98728d3de2006/activerecord/lib/arel/attributes/attribute.rb#L30

(Alternatively GitHub has a 'copy permalink' option in the '...' menu that appears in the margin when you select a line - documented here.)

Is there an equivalent in Azure DevOps?

The link that I get when I select a line has the form:

https://.../_git/project?path=XXXX&version=GBmaster&line=426&lineStyle=plain&lineEnd=427&lineStartColumn=1&lineEndColumn=1

Lots of parameters defining the selection but nothing pinning the file version.

Josh Gust
  • 4,102
  • 25
  • 41
David Waller
  • 3,057
  • 4
  • 27
  • 28

3 Answers3

25

Edit:

The below illustrations can be summarized in a few keyboard shortcuts.

  1. Open the repo Files view using the 'e' global shortcut
  2. Use 't' to put focus on the path selection to quickly navigate to the file in question
  • point 2 and 3 are order agnostic in relation to one another
  1. Use 'y' to change the Files view to be based on the latest commit instead of on the branch
  2. Make selection
  3. Copy link as shown in illustration below

Browse files on the commit

Make sure you're browsing the entire source for the commit. Selecting lines when viewing the commit itself (ie AzDOs fancy git show) is for adding comments to the commit, but it doesn't help produce a link to the line @ that version, nor is there a way (at least not that I could find) to get a link to the comment made on the line. Getting a link to a comment on a line in a commit might have been helpful to provide additional direct context to what is linked and why, but that's also not what you asked for.

enter image description here
don't click on the commit itself

enter image description here
make sure you're browsing all files as of a commit (not a branch)


Make selection

You should be able to select a line in the file contents and get a link to that line(s) in that version.

note: the link produced here is even more lengthy than the one you provided in your question.

enter image description here

Joy

When you navigate to the copied link, you will be directed to the commit and file contents with the line highlighted.

enter image description here


Understand, this link is bound to the commit you used to create it not to the tip of the branch. I expect this was how links are surfaced to be more sure that the link wouldn't break or lose context as the commit at the tip of the branch changes over time.

Josh Gust
  • 4,102
  • 25
  • 41
  • 1
    Wow, I'd never have found that on my own! So if I start with a selection in a file and need to share it, I have three extra clicks (History > ... > Browse Files) plus finding the location again and re-selecting - but at least I can do it! Thank you. Still would love a shortcut for this like GitHub does. – David Waller Apr 16 '19 at 20:51
  • Where are you when you "start with a selection in a file"? Maybe this is a feature that should be requested on the developer community. – Josh Gust Apr 16 '19 at 21:27
  • Go to git repository and hit 't' to enter the file finder, followed by typing the name of the file. – David Waller Apr 17 '19 at 00:36
  • Assuming that your code is in the latest commit you might use 'h' > (click **...**) > browse files > 't' – Josh Gust Apr 17 '19 at 15:16
  • 1
    after looking at shortcuts for a non-related task, you can use 'y' from the files browser to take you to the latest commit on the repo. This would help you if you know the change you want is in that commit. – Josh Gust Apr 19 '19 at 14:58
  • You're right! 'y' does work - but only if you do it *before* selecting the text. Afterwards you get a little tool-tip error "Cannot edit in read-only editor", which is why I didn't think this worked! – David Waller Apr 30 '19 at 01:00
  • 1
    :-( Azure DevOps seems to have been updated so that 'y' no longer works as a shortcut to go to the latest commit. Back to the laborious method of needing to get to the commit from the repo history before navigating to the file. – David Waller May 05 '20 at 18:52
  • @DavidWaller you're right. They seem to have poopooed on a few different ks values. Maybe this should be an extension that could be installed (whenever the magical blessing of free dev time comes around to landing on me) – Josh Gust May 05 '20 at 23:29
  • Thank you for this. For anyone else following: Files --> (your repo branch) --> History --> Click A Commit hash/number (Latest/Head usually) --> Browse Files --> Your file --> Select text & get a link. Sheesh... quite some hoops to jump through, but glad it's possible. – m1m1k Sep 08 '21 at 15:14
  • I would just recommend people should keep asking for this in user voice and also vote for the stuff already there. Looking at the history, this has been requested a number of times, but there are never enough votes. https://developercommunity.visualstudio.com/search?space=21 – Mike Nov 12 '21 at 00:57
  • @Mike while I agree with the idea, MS has been pretty not great at implementing user voice items. By all means, increment the ticker, but sadly I remain cynical. – Josh Gust Nov 13 '21 at 02:42
  • @JoshGust, yeah, I can't disagree. As far as I can tell, a lot of things just never get enough votes. I think something needs a minimum of 20, maybe even 50 votes. After something like a year, it gets closed. Problem is, most people are going to User Voice to ask for something, and not enough people go there to look at what's open and vote. It would help if something automatically found and combined duplicates. – Mike Nov 14 '21 at 16:18
  • @JoshGust, what are you trying to convey with the top two images? I can't see how they relate to steps 1-5 and they are confusing me. I also can't get `y` to do anything noticeable. Is it supposed to change the current URL to reflect the commit hash, as GitHub does? If so, that doesn't work for me in AzDO. – Jamie Jackson Oct 13 '22 at 16:38
  • @JamieJackson img-1 relates to what I mention in the first paragraph `Make sure you're browsing the entire source for the commit. Selecting lines when viewing the commit itself (ie AzDOs fancy git show) is for adding comments to the commit`. img-2 relates directly to step 3 as the highlighted dropdown displays a commit rather than a branch. – Josh Gust Oct 14 '22 at 14:49
  • @JamieJackson "I also can't get `y` to do anything noticeable". Does not work for me either. It was discussed before in this sequence of comments, that it has stopped working :( – Kirill Jan 13 '23 at 17:57
  • Has anyone found a way to do this directly in vsc? So I want to highlight some code in vsc (or right-click a line) and generate a permalink to the same code in DevOps Repos, without having to visit the DevOps front-end in a browser... possible?! – Adam Jul 06 '23 at 11:39
5

Thank you @JoshGust. For anyone else who wants the abbreviated version:

  1. Files -->
  2. (your repo branch) -->
  3. History -->
  4. Click A Commit hash/number (Latest/Head usually) -->
  5. Browse Files -->
  6. Your file -->
  7. Select text & get a link.

Sheesh... quite some hoops to jump through, but glad it's possible.

m1m1k
  • 1,375
  • 13
  • 14
1

To create a web (repository) link to a specific line from within your IDE, there are extensions with options to link to files, selections, to a branch or to a specific commit.

I am using these ones:

Editor Plugin
Visual Studio Code Git Web Links for VS Code
Visual Studio 2022 Git Web Links
veertien
  • 457
  • 5
  • 18