Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

azure-devops

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.

like image 290
David Waller Avatar asked Apr 16 '19 17:04

David Waller


People also ask

How do you link work items in Azure DevOps?

Open a work item and choose the Links tab. From the links control tab you can link to new or existing work items, open the linked object, edit the link type, delete a link, or open the list of links in a query or Excel or Project. The work item form opens in the web portal for Visual Studio 2017 and later versions.

Which link type is used to associate source code with work items?

GitHub link types are system-defined link types. They support linking work items to GitHub objects as shown in the following image.

How do I add a hyperlink in DevOps?

My flow needs to update a DevOps work item with a hyperlink. This can be done manually through the DevOps user interface by adding an existing item and selecting Hyperlink as the link type.


2 Answers

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.


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
  3. Use 'y' to change the Files view to be based on the latest commit instead of on the branch
  4. Make selection
  5. Copy link as shown in illustration below

enter image description here


enter image description here


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



Possible alternate method

Using the @username syntax in the comment editor might be a good solution to "share" a selection from the code. However, this method requires that you find the actual commit in which the change was made. Using the "browse files" method above allows you to share the address to a particular selection from any commit with the changes to be shared.

like image 196
Josh Gust Avatar answered Oct 03 '22 03:10

Josh Gust


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.

like image 31
m1m1k Avatar answered Oct 03 '22 03:10

m1m1k