How to save output result of hg commands in a file? - mercurial

I am trying to save output of Mercurial commands in a file
hg init > log.txt
but its not working. Any ideas?

The output might be on standard error.
Try hg init 2>&1 > log.txt.
Note: As bjlaub noted you might have to reverse the order to hg init > log.txt 2>&1 on Windows.

Are you sure the output is not simply silent by default?
hg init, for instance, rarely (if ever?) actually prints any output. Many Mercurial commands are the same way. You can often specify -v to get more verbose output, but in the case of the example you gave, I would expect an empty log.txt to appear. Have you tried other commands? If so, what exactly is not working? Do you get an empty log.txt or no log.txt at all?

I had a similar problem with hg log. With special templates the output on console worked, but when I tried to redirect into a file, no file was created.
I used the following workaround (for windows cmd):
In the batch file, use a subroutine, like shown below:
call :hgInit > log.txt
goto :eof
:hgInit
hg init
goto :eof
That worked for me.

Related

mercurial diff + unxutil "patch"

How do you make the mercurial "diff" command produce output that is compatible with the unix or unxutil patch command?
I need to create a patch file that I can send to a coworker who doesn't have Mercurial installed.
I've tried using hg diff -r 3:5 > patch1.diff and I get an error from the patch command when applying it. (hold on, I will post the error message as soon as I have a chance....)
OK, here is a test case that I've uploaded to bitbucket:
hg clone https://bitbucket.org/jason_s/test-patch-apply P2base
hg update -r 2 -R P2base
hg diff -r 2:4 -R P2base > p2base.patch
rm -r P2base/.hg
cd P2base
patch < ../p2base.patch
I get this on my Windows PC:
C:\tmp\hg\P2base>patch < ../p2base.patch
patching file bar.txt
Assertion failed: hunk, file ../patch-2.5.9-src/patch.c, line 354
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Never mind, this is a documented problem (with a REALLY POOR ERROR MESSAGE) that can be overcome. From http://gnuwin32.sourceforge.net/packages/patch.htm :
On MS-Windows, the patchfile must be a text file, i.e. CR-LF must be
used as line endings. A file with LF may give the error: "Assertion
failed, hunk, file patch.c, line 343," unless the option '--binary' is
given.
I used --binary and it worked fine.

Sort hg status by date

Im looking for a convenient way to sort the ouput of
hg status
to see the newest file at top.
Here is a possible solution that will only work in Linux-like environments (I am trying it in Windows using MSYS). You could use ls to list files sorted by time, passing it the output of hg status:
$ hg st
M modified.txt
A added.txt
R removed.txt
? unknown.txt
$ ls -t1 `hg st -n -a -m -u`
unknown.txt
modified.txt
added.txt
Using this method you lose the MAR?... status, but it shows the files that are changed, added, or are untracked, sorted by modification time. However, it does kind of rely on your allowed parameters to ls.
Effectively you're using the backquoted mercurial command to provide a list of filenames to ls, which will do the sorting for you. Don't think there's a simple way to do this in vanilla Windows. Possibly by using a for loop?
First, create a file with this content:
changeset = "{files}"
file = "{file}\n"
Let's say you call it sorted.txt and put it in your home directory. Then you can give this command:
hg -q outgoing --style ~/sorted.txt | sort -u

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.
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.
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.

How to change directory before executing HG commands from firefox extension?

I am trying to execute hgtk log command for a particular file. But you cannot do it until you are in the repository directory.
something like this...
process.initwithpath("cmd.exe");
args = ["CD","c:\\MY_REPO"];
process.run(true,args,args.length);
process.initwithpath("hg.exe");
args= ["hgtk","my_file.txt"];
process.run(true,args,args.length);
But the problem is second process will not keep track of first one....
Any suggestions will be highly appreciated....
See the --repository command line option (short from is -R). It will change the current working directory of hgtk before executing log, annotate, etc. If you provide a filename on the command line, then you should beware that they are understood relative to the argument of -R. So this works:
hgtk -R ~/src/mercurial log README
because the README file is found relative to ~/src/mercurial. This also works
hgtk -R ~/src/mercurial log ~/src/mercurial/README
since we give the full path to the README file. In this sense it works like Mercurial's --cwd option. Mercurial also has a -R option, but this does not change the current working directory.

How to open HG repository browser from command prompt for a specific file?

I know about hgtk log command but it is working for directory not for a file.
If I execute something like this
hgtk log -R D:\bmutilities\big_repo\chrome\content\br_editor.js
It throws an error saying---directory not found
But if run hgtk command in D:\bmutilities\big_repo\chrome\content\ directory
hgtk log -R br_editor.js
it works fine...
Suggestions please ????
You use the --repository or -R option to specify the path to the repository. After that, the filenames are relative to that directory. In this sense, hgtk -R works like hg --cwd meaning that it actually changes the current working directory.
Mercurial also has a --repository (-R) command like switch, but this does not change the current working directory of the process.
UPDATE
I understand your question better now. You mean that you cannot view the history of a file from the command line if you are not in a folder under the .hg root folder.
That appears to be a design decision by mercurial.
I just make sure I cd to a folder inside the repo before I do hgtk log on a specific file.