1

I'm trying to use the merge-doc.js script in my Git config for merge driver and mergetool for .docx, but the TortoiseGit helper script that starts a compare in Word using DCOM requires absolute paths to work.

So after installing TortoiseGit on my Windows system and adding the following to Git .attributes and config, I need to also provide the current path, PWD, somehow.

.gitattributes

*.docx merge=word

config

[merge "word"]
    name = Word document merge
    driver = "wscript //nologo \"C:/Program Files/TortoiseGit/Diff-Scripts/merge-doc.js\" %A %B %A %O"

[mergetool "word"]
    cmd = "wscript //nologo \"C:/Program Files/TortoiseGit/Diff-Scripts/merge-doc.js\" "$MERGED" "$REMOTE" "$LOCAL" "$LOCAL""
    trustExitCode = true

I've tried adding PWD in different ways. But the Git trace keep showing that the variable isn't expanded.

This is probably very easy when you know GNU/Git bash.

22:23:26.042283 run-command.c:935  
      trace: start_command: 'C:/Program Files/Git/usr/bin/sh.exe' -c 'wscript //nologo "C:/Program Files/TortoiseGit/Diff-Scripts/merge-doc.js" "$(pwd.path)/.merge_file_rlnNRU" "%cd%/.merge_file_IiJUOk" "%pwd/.merge_file_rlnNRU" "%pwd/.merge_file_cMl86i"'
22:24:29.183367 run-command.c:674  
      run_command: 'wscript //nologo "C:/Program Files/TortoiseGit/Diff-Scripts/merge-doc.js" "`pwd`/.merge_file_yHhDa3" "%cd%/.merge_file_orFupt" "%pwd/.merge_file_yHhDa3" "%pwd/.merge_file_spZLIe"'
22:24:29.183367 run-command.c:935

Edit 1

So far I've deducted that sratch file variables presenting absolute paths, depending on what Git version used. Git for Windows apparently does not use absolute paths to avoid the legacy Win32 API issue with long file name limitations of 260 character path limit.

This was resolved in Windows 10 version 1607 (Anniversary Update) in August 2016, bit still has to be manually enabled by setting LongPathsEnabled to 1 under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem.
See MS Learn - Maximum Path Length Limitation.

Edit 2

No environment variables are available to Git bash when starting using only sh.exe.
Besides, according to @phd, I shoudn't be looking for the environment variable $pwd but the command $(pwd).

Edit 3

The merge-doc.js script expects the scratch files to have correct file extensions, where as Git driver provides non.

Edit 4

After I provide renamed scratch files, Git bash doesn't wait for the manual merger and tries to clean up the scratch files prematurely. If I use cscript, instead of wscript, this will at least hand over the work flow to merge-doc.js. But the Tortoise helper script also exits before Word is closed down. I was hoping for being able to integrate the Tortoise helper scripts with Git merge as is.

5
  • Try $(pwd) everywhere instead of $(pwd.path) and %pwd. Commented Oct 2 at 15:54
  • No environment variables are available to sh.exe Commented Oct 2 at 18:09
  • 2
    $(pwd) is not an environment variable, it's a call to program pwd. With $(pwd) shell runs pwd and replaces $(pwd) with the output (stdout) of pwd. Commented Oct 2 at 18:14
  • Regarding Edit 3: merge.word.driver and mergetool.word.cmd could be anything; they could be shell scripts that rename files passed to them and pass renamed files to merge-doc.js. Commented Oct 4 at 16:13
  • Yes. I'm working along that line right now 👍 Commented Oct 6 at 7:18

1 Answer 1

0

This is how I solved the question I asked. But it doesn't handle my actual problem properly as merge-doc.js doesn't wait for me to manually handle a doc-conflict.

I opted for getting the repo root instead of the current folder.

To start handling conflicts for docx files, I can either use a driver or a mergetool configuration.

Driver

The driver launches the command automatically but the scratch files has to be renamed first.

.gitattributes

*.docx merge=word

config

[merge "word"]
  name = Word document merge
  driver = REPO_ROOT="$(git rev-parse --show-toplevel)" && \
  cp "%A" "%A.docx" && cp "%B" "%B.docx" && cp "%O" "%O.docx" && \
  cscript //nologo "\"C:/Program Files/TortoiseGit/Diff-Scripts/merge-doc.js\" $REPO_ROOT/%A.docx $REPO_ROOT/%B.docx $REPO_ROOT/%A.docx $REPO_ROOT/%O.docx" && \
  rm -f "%A.docx" && rm -f "%B.docx" && rm -f "%O.docx"

git merge conflicting_branch_name

This will successfully launch the script automatically. But merge-doc.js doesn't wait for Word to exit, which causes the Git merge process to fail.

Mergetool

The merge tool has to be launched manually after a conflict is detected by Git but the scratch files are properly named.

config

[mergetool]
    prompt = false

[mergetool "word"]
    cmd = REPO_ROOT="$(git rev-parse --show-toplevel)" && \
    cscript //nologo "\"C:/Program Files/TortoiseGit/Diff-Scripts/merge-doc.js\" \"$REPO_ROOT/$MERGED\" \"$REPO_ROOT/$REMOTE\" \"$REPO_ROOT/$LOCAL\" \"$REPO_ROOT/$BASE\""
    trustExitCode = true

git merge conflicting_branch_name

git mergetool --tool=word

This will also run the script, but there is no need to rename the scratch files.

Again, merge-doc.js is of course still not waiting for Word to exit. So some scratch files will be locked and can't be removed at the correct point.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.