Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

In External Diff tool, can I get the path to the file?

TamarGranor
January 22, 2025

Four years ago, I built a little EXE to use for external diffs that let me apply BeyondCompare to the paired files that represent various things in Visual FoxPro. Along the way, I ran into the fact that the $REMOTE parameter passed into external diffs is relative to the repository's home folder. (I asked about that then: https://community.atlassian.com/t5/Sourcetree-questions/Custom-diff-tool-finding-the-REMOTE-file/qaq-p/1551704)

Back then, I found a workaround in VFP that let the thing work for me. Now, I have a new computer and that workaround isn't working anymore.

Is there a way to get my hands on the repository's home folder? If I had that, I'm pretty sure I can do the rest.

Alternatively, is there any way to specify that $REMOTE include a full path?

Thanks, Tamar

1 answer

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
February 16, 2026

Hi @TamarGranor

I'm not very good at Source Tree, so I asked Rovo this question to see if it is able to provide some advice, and this is the response provided:-

 

Short version: Sourcetree still doesn’t expose the repository root to your external diff command, and there’s no way to make $REMOTE a fully‑qualified path from within Sourcetree’s config. You’ll need to reconstruct the full path in your EXE again.

1. What Sourcetree actually passes

From your original post at
Custom diff tool: finding the $REMOTE$ file

  • $LOCAL → a fully‑pathed temporary file (the committed side).

  • $REMOTE → a relative path from the repo root, e.g. CLASSLIBS\MyClassLib.VCX.

Sourcetree’s external diff arguments only support the documented placeholders ($LOCAL, $REMOTE, etc.), and there’s no additional “repository root” or “working directory” token you can use. There’s also no option to say “pass $REMOTE as an absolute path instead”.

So:

Is there a way to get my hands on the repository's home folder?
Is there any way to specify that $REMOTE include a full path?

From Sourcetree itself: no to both.

2. Practical ways to recover the full path in your EXE

You’re back to the same class of workaround you used in VFP before, but you can probably make it more robust so it doesn’t depend on machine‑specific quirks.

Assuming $REMOTE is always “path inside repo”, these are your realistic options:

Option A – Use the current working directory

Sourcetree typically launches the external diff tool with the repository as the working directory. If that’s true in your setup, your EXE can:

  1. Get the process current directory.

  2. Combine it with the $REMOTE parameter.

Pseudo‑flow in your EXE:

repoRoot := GetCurrentDirectory() remoteRel := param2 && e.g., "CLASSLIBS\MyClassLib.VCX" remoteFull := PathCombine(repoRoot, remoteRel)

Then pass remoteFull to Beyond Compare along with $LOCAL.

If this used to work and now doesn’t, there are a couple of likely culprits on the new machine:

  • The EXE is being launched from a different working directory (e.g., your tool’s own folder).

  • Some “run as admin” or shortcut wrappers changed the working directory.

  • You’re now calling it through a script/launcher that changes CWD.

You can test this easily by having your EXE just log the current directory and arguments to a text file.

Option B – Discover the repo root using Git/Hg from $REMOTE

If the working directory isn’t reliable, you can:

  1. Use $REMOTE to locate the repo folder from the file system:

    • Use the relative path to find a matching file somewhere under your usual dev roots, or

    • Accept a base “search root” in a config file/registry, and walk up the path until you find .git or .hg.

  2. Or, if you know you’re always inside a Git repo, you can shell out:

    git rev-parse --show-toplevel

    executed in any directory inside the repo gives you the absolute repo root.

    So your EXE can:

    • Change directory to where you think the file lives (or to a likely base).

    • Run git rev-parse --show-toplevel.

    • Combine that result with $REMOTE.

That’s more work, but it decouples you from Sourcetree’s launch environment.

Option C – Per‑repo configuration for the EXE

If you don’t mind a little per‑repo setup, have your EXE read a simple config:

  • E.g. .<yourtool>.ini at the repo root containing:

    [repo] root=C:\Dev\MyProject
  • Your EXE:

    1. Walks upwards from its own location or from a guessed base until it finds that ini file.

    2. Reads the root path.

    3. Combines root + $REMOTE.

This keeps it reusable but lets you override edge cases.

3. Why Sourcetree can’t be configured to pass a full $REMOTE

There’s nothing in the current Sourcetree docs or support articles that indicates:

  • A token like $REPO / $WORKDIR / $ROOT.

  • A checkbox/setting to “pass absolute path for REMOTE”.

The external diff arguments are fixed to the existing placeholders; you can see this pattern echoed in other Sourcetree usage docs like:

None of those introduce extra diff‑tool macros.

So the only realistic route is to let your EXE reconstruct the full path using one of the strategies above.


 

Upon doing some further checking, this time with Google's AI and a different response is provided:-

 

In Sourcetree, the $REMOTE and $LOCAL parameters passed to custom diff tools are indeed paths to temporary files created by Git, not the original files in your working directory.
Because these are temporary files, they do not inherently "know" their original full path within your repository. To get the repository's home folder from within your EXE, you can use the following methods:
1. Use Git to Find the Root Path
The most reliable way to find the repository's home folder from any script or EXE launched by Sourcetree is to call a Git command. Since Sourcetree launches your tool from within the repository's process context, you can run:
  • Command: git rev-parse --show-toplevel
  • Result: This returns the absolute path to the root of the current Git repository. Your EXE can capture this output to reconstruct any relative paths.

 

2. Capture the $MERGED Parameter
While $REMOTE and $LOCAL point to temp files, the $MERGED parameter (often available in diff contexts as well) typically contains the relative path of the file from the repository root.
  • Tip: If you pass "$MERGED" as a third argument to your EXE, you can combine the root path (from step 1) with this relative path to get the full original filename.

 

3. Update Your "Custom" Arguments
In Sourcetree under Tools > Options > Diff, ensure your Arguments field includes the necessary variables to help your EXE:
  • Suggested Arguments: "$LOCAL" "$REMOTE" "$MERGED"
  • Your EXE can then:
    1. Read the third argument ($MERGED) to see the file's name and relative path.
    2. Run git rev-parse --show-toplevel to find where the repo lives on your new computer.
    3. Locate the secondary "paired" FoxPro files based on that root path.

 

4. Verify Environment Variables
On a new machine, ensure the Git "bin" folder is in your system PATH. If your EXE cannot find git.exe to run the rev-parse command, it will fail. You can check this by typing git --version in a standard command prompt.
If you need a specific batch script wrapper to handle this logic before calling your EXE, let me know and I can provide a template.
I hope this helps. :)
Let me know how it goes.
Thank you and Kind regards,
Ram

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events