Mr. JWolf
Mr. JWolf

Reputation: 1395

How to git diff Word docx files in Sourcetree on Windows

I would like to see git diff of Word .docx files in SourceTree on Windows.

Per default they shows up like this: Word .docx files in SourceTree history

I have tried changing .gitattributes and .git\config but it does not work for the history in SourceTree. This sadly seems like a limitation in SourceTree 1, 2

Are there any ways to get a diff for Word .docx files in SourceTree on Windows?

Upvotes: 1

Views: 1680

Answers (1)

Mr. JWolf
Mr. JWolf

Reputation: 1395

End result

This solution will merge for comparison using the Word - Compare and merge two versions of a document. Using it like this:

  1. Select a commit in SourceTree
  2. Select a .docx in the file list
  3. Click the gear icon to the far right
  4. Select External Diff

You can also use Ctrl + D as a shortcut for External Diff

Drawbacks

  • Does not work in SourceTree's own History view
  • Overwrites the External Diff Tool in case you are using another

Setup

This solution is based on ForNeVeR/ExtDiff.

First you create 2 files:

CompareWord.cmd

@echo off
powershell.exe -File "%~dpn0.ps1" %*

CompareWord.ps1

param(
    [string] $BaseFileName,
    [string] $ChangedFileName
)

$ErrorActionPreference = 'Stop'

function resolve($relativePath) {
    (Resolve-Path $relativePath).Path
}

$BaseFileName = resolve $BaseFileName
$ChangedFileName = resolve $ChangedFileName

# Remove the readonly attribute because Word is unable to compare readonly
# files:
$baseFile = Get-ChildItem $BaseFileName
if ($baseFile.IsReadOnly) {
    $baseFile.IsReadOnly = $false
}

# Constants
$wdDoNotSaveChanges = 0
$wdCompareTargetNew = 2

try {
    $word = New-Object -ComObject Word.Application
    $word.Visible = $true
    $document = $word.Documents.Open($BaseFileName, $false, $false)
    $document.Compare($ChangedFileName, [ref]"Comparison", [ref]$wdCompareTargetNew, [ref]$true, [ref]$true)

    $word.ActiveDocument.Saved = 1

    # Now close the document so only compare results window persists:
    $document.Close([ref]$wdDoNotSaveChanges)
} catch {
    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.MessageBox]::Show($_.Exception)
}
  1. Launch SourceTree
  2. Open ToolsOptions
  3. Go to the Diff tab
  4. Under External Diff / MergeExternal Diff Tool: choose Custom
  5. Under Diff Command: write the full path to the CompareWord.cmd, i.e. <your\path\to\>CompareWord.cmd
  6. Under Arguments: write \"$LOCAL\" \"$REMOTE\"
  7. Click OK to save your settings

Upvotes: 1

Related Questions