I am trying to create an alias to use in my Cygwin shell to determine if the Mercurial-managed directory I'm currently in has outstanding changes. The command "hg st -m" lists all modified files. Unfortunately it doesn't set an exit value based on whether there are changes or not. So I have tried capturing the output and seeing if it's not empty, piping it through "wc -l" and checking if that's -gt 0, but I seem to have a larger problem in that lines are clearly not executing in the order I would expect them to.
Here is what I've boiled it down to:
alias hgfoo='
pwd
echo L1
localChanges="NOT SET YET"
hasLocal="NOT SET YET"
echo "Before: A${localChanges}Z A${hasLocal}Z"
localChanges=`hg st -m`
echo L2
echo "localChanges: A${localChanges}Z"
echo L3
if [ -n "${localChanges}" ] ; then
echo L3
hasLocal="YES"
else
echo L4
hasLocal="NO"
fi
echo L5
echo "RESULTS: A${hasLocal}Z"
echo L6
'
I put in the "Ln" echo statements for debugging. When I run it by typing in "hgfoo" in a directory that has changes, I see the following output:
/d/hg/succession > hgfoo
/d/hg/succession
L1
Before: ANOT SET YETZ ANOT SET YETZ
L2
localChanges: AL2
localChanges: ANOT SET YETZ
L3
L3
L5
RESULTS: AYESZ
L6
M succession-lib\.actionScriptProperties
M succession-lib\src\main\flex\com\workscape\SuccessionConstants.as
M succession-lib\src\main\flex\com\workscape\succession\nomination\tabmodules\BenchStrengthTab.mxml
M succession-lib\src\main\flex\com\workscape\succession\nomination\tabmodules\BenchStrengthTabMediator.as
M succession-lib\src\main\flex\com\workscape\succession\puremvc\controller\StartupSuccession.asZ
L3
L3
L5
RESULTS: AYESZ
L6
When I run it in a directory without changes, I see:
/d/hg/employee-profile > hgfoo
/d/hg/employee-profile
L1
Before: ANOT SET YETZ ANOT SET YETZ
L2
localChanges: AL2
localChanges: ANOT SET YETZ
L3
L3
L5
RESULTS: AYESZ
L6Z
L3
L3
L5
RESULTS: AYESZ
L6
Even forgetting about the fact that the comparison is not working right, in both cases it seems to be running lines multiple times and in the wrong place. This is driving me crazy.
Any clues? Thanks.
hgcommand run in the background? Does it send its output to stderr? – glenn jackman Mar 2 '11 at 17:40localChanges: AL2notlocalChanges: ALZwhich indicates the alias you're actually running is not what you're showing us. Clean up or refresh the aliases in your shell, run it again and then show us what's happening. – glenn jackman Mar 2 '11 at 17:42