Linking to a line of code in a particular version of a file in Azure DevOps (VSTS)
Asked Answered
M

3

27

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.

Moidore answered 16/4, 2019 at 17:36 Comment(0)
M
27

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.

Mindszenty answered 16/4, 2019 at 18:23 Comment(16)
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.Moidore
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.Mindszenty
Go to git repository and hit 't' to enter the file finder, followed by typing the name of the file.Moidore
Assuming that your code is in the latest commit you might use 'h' > (click ...) > browse files > 't'Mindszenty
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.Mindszenty
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!Moidore
:-( 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.Moidore
@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)Mindszenty
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.Absolutely
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. developercommunity.visualstudio.com/search?space=21Hostetter
@Hostetter 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.Mindszenty
@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.Hostetter
@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.Repentance
@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.Mindszenty
@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 :(Fanfani
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?!Servant
A
6

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.

Absolutely answered 8/9, 2021 at 15:17 Comment(1)
This is ridiculous! It's amazing how unpolished ADO is compared to other systems. Thanks for the summary, and without using shortcuts!Lilithe
E
2

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
Elburt answered 22/8, 2022 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.