I'm trying to setup a shortcut, a command which will let me check the history of one file.
I'm interested in when the file was originally commited, in what changesets it was changed and whether the current version in the working directory differs from the "last commited one".
So, in general
hg log --verbose filename.txt
hg status filename.txt
Is there a way to make this into a sort of a shortcut, so I can just type for example
hg file filename.txt
and get the "history" of the file?
Yes, you can add a shell alias:
[alias]
file = !$HG log --verbose "$1" && $HG status "$1"
The $HG environment variable refers to the path of the hg script used to invoke the shell alias, just in case hg is not in your path. The $1 refers to the first command line argument, the file name in your case.
Related
Executing the Mercurial command:
hg in -q
produces a list like below.
123:b64543
124:ef312a
This command will be execute on multiple repo's. How can we append the repository path to the above output to identify the repo? Executing command 'hg root' gives us that information but we only want to know about the path if the repo has new changeset. We looked at template but could not find a variable that gave us the right information.
Apppreciate any help.
The easiest solution for appending the root path to hg incoming is to concatenate the two commands with && (assuming you're using Unix):
hg in -q && hg root
This works because hg incoming will return 0 if there are incoming changes, 1 otherwise (or a non-zero value if there was an error).
For more sophisticated manipulation of the output, sed can usually do it. For example, the following command prepends the root to the output:
hg in -q | sed -e "1i$(hg root)"
I'd resort to a bit bash and call hg root within the template. Assuming all directories are sub-directories of the current one (otherwise give the list of dirs differently):
for i in *; do [ -d $i/.hg ] && hg incoming -R$i --template "$(hg root -R$i) {rev}:{node|short}\n"; done
When I run hg add with no arguments, it is always by mistake, and the result, adding all of the files and directories recursively, is horribly annoying and difficult to undo, especially when other files have been (correctly) added since the last commit. Is there any way to make a plain hg add just print an error message?
Try putting this in your ~/.hgrc:
[defaults]
add = -X .
That tells hg add that unless specifically named it should ignore all files (got matches all). Here's an example:
(df)Ry4ans-MacBook-Air:~ ry4an$ hg init test
(df)Ry4ans-MacBook-Air:~ ry4an$ cd test/
(df)Ry4ans-MacBook-Air:test ry4an$ vi ~/.hgrc # added the section above
(df)Ry4ans-MacBook-Air:test ry4an$ hg status
(df)Ry4ans-MacBook-Air:test ry4an$ echo this > that
(df)Ry4ans-MacBook-Air:test ry4an$ hg add # nothing added
(df)Ry4ans-MacBook-Air:test ry4an$ hg status
? that
(df)Ry4ans-MacBook-Air:test ry4an$ hg add that
(df)Ry4ans-MacBook-Air:test ry4an$ hg status
A that
In general though, you should just make your .hgignore robust enough to ignore all the files you don't want added
In your user config file (~/.hgrc), add the following to your [alias] section:
[alias]
realadd = add
add = add --dry-run
Now, just hg add will always do a dry-run. To actually add, you have to use hg realadd. Note that you could redefine the add alias to do anything, it doesn't have to be add --dry-run.
I don't know a way to do this purely with Mercurial configuration, but if you're willing to tune your bash profile, then you can redefine the hg command as a function. The function would either detect hg add and fail or otherwise do a passthrough to the real hg command.
function hg() {
if [ "$#" -eq 1 ] && [ "$1" = "add" ]; then
echo "hg add with no arguments denied" 1>&2
false # sets exit code to 1, but doesn't close process like exit would
else
command hg $#
fi
}
Here is what it looks like in action after I source in the new function from my profile:
hg > /dev/null; echo $?
0
hg add > /dev/null; echo $?
hg add with no arguments denied
1
hg add . > /dev/null; echo $?
0
hg status
touch afile
hg add afile
hg status
A afile
You can undo a global add using the following command:
hg forget $(hg status -an)
Here, hg status -an will list all added files. hg forget will then remove those files from the list of added files.
You can also create an alias for this in your .hgrc, e.g.:
[alias]
unadd = !$HG forget $($HG status -an)
Note that this will also delist all previously added files that you did mean to add, so you may have to redo that.
Also, operating systems and shells have limits for how many arguments can be passed to a command. If you run into this limit because you accidentally added more than a few thousand files, you can use xargs instead:
hg status -an | xargs hg forget
Or, as an alias:
[alias]
unadd = !$HG status -an | xargs $HG forget
I have to get list of changed, added or removed files since last commit.
command: hg status gives me for example
M file_path
C other_file_path
I need:
file_path
other_file_path
Solution have to work in Unix & Windows.
If you want to list all of the files, you can simply add -n to the hg status command:
$ hg status
M modded.txt
A added.txt
R removed.txt
? unknown.txt
$ hg status -n
modded.txt
added.txt
removed.txt
unknown.txt
However, this will also list unknown files (those that are new, but have not been specifically added to the repository with a hg add command). You can get around this by adding either -q (as Lazy Badger points out), or by using filesets (see hg help filesets) to specify all files that aren't unknown:
$ hg status -n -q
modded.txt
added.txt
removed.txt
$ hg status -n "set:!unknown()"
modded.txt
added.txt
removed.txt
You can specify which types of files are listed by combining the other options (-a -r for example will show added and removed files). Alternatively you can do clever things with filesets: for example, only listing the names of files that are removed by using "set:removed()"
I want my default hg status to recirse into subrepos. This is easy enough to do in .hgrc:
[alias]
status = status -S
But I want to have another alias, say hg status-no-subrepo, that does not.
[alias]
status-no-subrepo = status
Unfortunately, this does not work, because status-no-subrepo --expands-to--> status --expands-to--> status -S. I imagine there is something to stop the recursion at that point.
Any ideas?
By the way, this seems to be a violation of one of Glew's Rules: any command line option that can be turned on should be possible to turn off. Possibly -S == -S:1, -S:0 to turn off.
Simple, have your original alias under a different name
[alias]
sstat = status -S
Not the answer you were looking for, I know, but it's easy. It also means that you don't get confused if you move to a system without your alias installed (you'll get a proper error to remind you), and others don't get confused when they do things in your account.
I cant tell you how many times I've helped someone out just to get annoyed that they've aliased ls to ls -l or rm to rm -i.
In general I see overriding common commands with personalised versions as ill-conceived.
You need to disable the status alias when running status-no-subrepo.
[alias]
status = status -S
status-no-subrepo = !$HG --config alias.status=status status $#
I don't use subrepos, but I tested similar functionality with my glog alias.
glog = !$HG log --graph --branch $($HG branch) $#
glog-all-branches = !$HG --config alias.glog=glog glog $#
The ! tells Mercurial this is a shell command, not a Mercurial sub-command. When running a shell command, Mercurial sets $HG to the path to the running hg executable. Arguments after the alias are not passed into shell commands by default, so $# adds them back. This allows you to run commands like hg status-no-subrepo --no-status to show changes without subrepos and hide the status prefix.
Is there any Mercurial extension that can grep for "console.log" that might have been accidentally left over as debugging code?
Right now this is what I am doing:
1) hg out ssh://....
the above is to see what is the first committed revision in my local repo, say, the smallest revision is 3456
2) hg diff -r 3455 | grep "^+" | grep "console\.log"
The number 3455 is 3456 - 1. the first grep is to see newly added code. the second one is for console.log
This method can tell that I have "console.log" in the new code, but won't say what file it is in.
It sounds like you're in need of a commit hook. Try putting something like this into your .hg/hgrc (or ~/.hgrc if you want it global):
[hooks]
pretxncommit = sh -c 'if hg log -p -r $HG_NODE | grep -q '^\+.*console\.log' ; then exit 1; else exit 0; fi'
That will abort your commits if they would be adding a line that contains console.log. Your commit message will be saved in .hg/last-message.txt.
See http://hgbook.red-bean.com/read/handling-repository-events-with-hooks.html for more details.