Linking to commits from comments in Azure DevOps

2024-11-26

Azure DevOps currently doesn't allow linking to specific commits in review comments/various other places like GitHub does, as discussed here.

Here is a JS snippet that you can add as a bookmark in your browser. It can be copied into the URL field of your browser's bookmark creation dialog.

javascript:(function() {
    const url = window.location.href;
    const regex = /commit\/([a-f0-9]{40})/;
    const match = url.match(regex);
    if (match) {
        const shortHash = match[1].substring(0, 8);
        const markdownLink = `[${shortHash}](${url})`;
        navigator.clipboard.writeText(markdownLink);
    } else {
        alert("Commit hash not found in URL.");
    }
})();

Clicking the bookmark while on an AZDO commit page will copy a Markdown-formatted commit link to the keyboard, like so:

[a1d3f3f2](http://link/to/azdo/commit/a1d3f2XXXXXXXXXXXXXXXX)

By default, 8 characters should be enough to distinguish commits (and is the number of characters displayed by AZDO by the title upon viewing a commit, unless this changes if non-unique!).


2024-12-4: Stevie Howard replied to my suggestion with a script for Tampermonkey that automatically adds a correctly-styled button to any commit pages in place of a bookmark, which I've switched to for bookmark-bar real-estate purposes. Note that the @match field might need to be adjusted for your AZDO domain.