Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hg extdiff -p and %~dp0

myprogram.cmd is in PATH;

myprogram.cmd uses %~dp0 to determine the folder it is in;

I have included @echo %~dp0 into myprogram.cmd for debugging;

When I call myprogram.cmd from anywhere, it works perfectly, displaying the folder myprogram.cmd is in;

When I call hg extdiff -p myprogram.cmd, it does not work, displaying something like c:\Users\Username\AppData\Local\Temp\extdiff.3n8op2\.

Here is the related part of hgrc file:

[extensions]
hgext.extdiff = 

What do I do wrong? Should not %~dp0 return drive and path of the batch file? What do I use instead? Do I have to apply some special configuration to Mercurial repository? Passing the full path of myprogram.cmd to hg extdiff -p is not an option, unless it is done automatically.

like image 915
utapyngo Avatar asked May 15 '26 12:05

utapyngo


2 Answers

The %~dp0 trick is a big lie. It is not actually a magic variable, it's simply a manipulation of %0 (or whichever var you stick the ~dp in front of). It simply takes whatever string is in that variable and tells you what its drive and path components appear to be. If that string is simply a name like "myprogram", it says "well, filenames without drives and paths are assumed to be in the current directory".

So the %~dp0 trick only works if either:

a) you've launched your script by its full name or b) you happen to be in the directory where it resides

In this case, you run:

hg extdiff -p myprogram

gets turned into the following call to Windows:

CreateProcess(NULL, "cmd.exe /c myprogram some diff args", ..., "c:/some/temp/path", ...)

which is morally equivalent to opening a shell and running:

C:\>cd c:\some\temp\path
C:\some\temp\path>myprogram some diff args
%0 is myprogram
%~dp0 is C:\some\temp\path

I recommend passing the full program name for your tool like this via your .hgrc:

[extdiff]
myprogram=c:/tools/myprogram.cmd

But be warned, this can get confused by the presence of spaces in your filename, you may need to experiment with quoting.

like image 55
mpm Avatar answered May 17 '26 14:05

mpm


You can try using %~dp$PATH:0, but you need to always specify the extension. For example, for the following test.cmd:

rem test.cmd
@echo dp0 == %~dp0
@echo dp$PATH:0 == %~dp$PATH:0

these are two sample runs from the d:\hg folder:

$ hg extdiff -p test.cmd
dp0 == c:\Users\estefan\AppData\Local\Temp\extdiff.pamj6n\
dp$PATH:0 == k:\home\Scripts\

$ hg extdiff -p test
dp0 == c:\Users\estefan\AppData\Local\Temp\extdiff.dgp0qz\
dp$PATH:0 ==

From http://ss64.com/nt/syntax-args.html:

%~$PATH:1 Search the PATH environment variable and expand %1 to the fully qualified name of the first match found.

like image 32
alexandrul Avatar answered May 17 '26 16:05

alexandrul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!