Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure "Commit text links" feature in SourceTree for windows across many repositories

TL;DR: SourceTree for Windows recently added the "commit text links" feature, but it appears that the replacements must be set up per-repository. Is there a way to apply them globally or a config file that could be modified programmatically to set them?

Long version: The "commit text links" feature looks incredibly useful but I have a bit of a problem: We have about a dozen JIRA projects and over 25 repositories that each of them could be related to (none of them are 1-to-1 mappings). While I could set up a single regular expression to match each of the JIRA projects, it's a bit much to ask all of my developers to set it up through the UI for each and every repository. To really take advantage of this I ideally need to be able to give them instructions on a single file to modify or I need to generate a setup script that I can distribute to our developers.

Is there a config file that this setting is saved in? I was expecting to see it in something like .hg/hgrc but I couldn't find anything. I also couldn't find any relevant settings in the SourceTree Program Files folder.

Alternatively, is there a global or default setting that can be applied across all repositories? That plus the regex version could make setup significantly less painful if still manual.

Thanks!

(Note: I'm in version 1.3.3.0 of SourceTree for Windows, which I believe is the most recent stable version)

like image 793
Josh Avatar asked Jan 10 '14 19:01

Josh


2 Answers

May be a bit late, but I've found a relatively easy way to do this.

Underneath your .hg/.git folder within your repository should exist a file called 'sourcetreeconfig.' This is where the links live and can be manually edited.

First make sure that you have closed all of the existing repository tabs within sourcetree, and additionally close sourcetree afterwards. Then, (assuming you have already configured a repository) copy the block from the respective repository's sourcetreeconfig and do a replace across all of your sourcetreeconfig files. This would be if you have multiple tied to the same project. It should be relatively easy to throw something together that can configure for different projects, just replace the url/project within the config.

Upon reopening sourcetree, each of your repositories should reflect this change.

This was performed using version 1.6.5.0 of sourcetree.

like image 163
BrettDuclos Avatar answered Nov 20 '22 03:11

BrettDuclos


Programmatic Solution

Here in late 2019, the ability to globally configure commit text links in Sourcetree 3.2.6 for Windows still does not exist. Since this question was one of the few hits with a decent answer, I figured I would add an automated solution to the answers. I'm not a programmer, and I know the RegEx isn't the best, but this simple PowerShell script I cobbled together gets the job done. Make sure Sourcetree is closed before you run the script.

  1. Copy the sourcelinker script into a Notepad++ or similar text editor application.
  2. In order get the specific string for your setup, configure one of your Git repos with one or more commit text links specific to your organization.
    A. Launch Sourcetree, select a Git repo, and click Settings.
    B. In the Repository Settings window, click the Advanced tab.
    C. In the Commit text links area, click Add.
    D. From the Replacement type drop-down list, select Other.
    E. Enter the Regex pattern and Link to URL for your specific setup, and click OK.
    F. In the Repository Settings window, click OK.
    G. Close Sourcetree.
    1. Navigate to the .git sub-directory of the repo you configured, and open sourcetreesonfig.json.
    2. Copy everything starting with "CommitTextLinks": [ through the closing bracket and comma ],. For example:
"CommitTextLinks": [
    {
      "$id": "11",
      "LinkType": 99,
      "Regex": "[fF][bB][#\\s]*(\\d+)",
      "LinkToUrl": "https://companyname.fogbugz.com/f/cases/$1",
      "Project": null,
      "RootUrl": null,
      "Description": "[fF][bB][#\\s]*(\\d+) => https://companyname.fogbugz.com/f/cases/$1"
    }
  ],
  1. Paste the copied content into your sourcelinker script between the single quotes that follow $New1 =.
  2. Save the script as sourcelinker.ps1.
  3. Copy sourcelinker.ps1 to the root folder where your Git repos reside.
  4. Right-click the script, and select Run with PowerShell.
  5. Launch Sourcetree, and check the Commit text links for your other Git repos.

Sourcelinker script

This script example contains Regex examples that link to Fogbugz and handles variations such as:

  • case12345
  • fb12345
  • bugzid12

Script

# Sourcelinker script
$InputFiles = Get-Item ".\*\.git\sourcetreeconfig.json"
$Old1 = '"CommitTextLinks": null,'
$New1 = '"CommitTextLinks": [
    {
      "$id": "9",
      "LinkType": 99,
      "Regex": "[bB][Uu][gG][sSzZ]\\s*[Ii][Dd]s?\\s*[#:; ]+(\\d+)",
      "LinkToUrl": "https://companyname.fogbugz.com/f/cases/$1",
      "Project": null,
      "RootUrl": null,
      "Description": "[bB][Uu][gG][sSzZ]\\s*[Ii][Dd]s?\\s*[#:; ]+(\\d+) => https://companyname.fogbugz.com/f/cases/$1"
    },
    {
      "$id": "10",
      "LinkType": 99,
      "Regex": "[cC][aA][Ss][Ee]+\\s*(\\d+)",
      "LinkToUrl": "https://companyname.fogbugz.com/f/cases/$1",
      "Project": null,
      "RootUrl": null,
      "Description": "[cC][aA][Ss][Ee]+\\s*(\\d+) => https://companyname.fogbugz.com/f/cases/$1"
    },
    {
      "$id": "11",
      "LinkType": 99,
      "Regex": "[fF][bB][#\\s]*(\\d+)",
      "LinkToUrl": "https://companyname.fogbugz.com/f/cases/$1",
      "Project": null,
      "RootUrl": null,
      "Description": "[fF][bB][#\\s]*(\\d+) => https://companyname.fogbugz.com/f/cases/$1"
    }
  ],'
$InputFiles | ForEach {
    (Get-Content -Path $_.FullName).Replace($Old1,$New1) | Set-Content -Path $_.Fullname
}

Solution inspired thanks to suggestion by Andrew Pearce from this thread.

like image 2
Curtis Masuda Avatar answered Nov 20 '22 02:11

Curtis Masuda