Commit Graph

65 Commits

Author SHA1 Message Date
Pierre-Yves David
be4dcab05b hooks: back e7b51de6e8eb out
Changeset e7b51de6e8eb alters the 'HG_PENDING' mechanism to be "always" there.
This change is made under the assumption than we previously did it only when
"writepending() actually wrote something". This assumption was wrong,
'writepending()' informs of pending changes the first time something is written
and for all following calls. We back this change out to restore the former
behavior, which was already correct.
2015-11-06 11:08:11 -05:00
Durham Goode
8bdfd3d55c hooks: always include HG_PENDING
Previously we would only include HG_PENDING in the hook args if the
transaction's writepending() actually wrote something. This is a bad criteria,
since it's possible that a previous call to writepending() wrote stuff and the
hooks want to still see that.

The solution is to always have hooks execute within the scope of the pending
changes by always putting HG_PENDING in the environment.
2015-11-03 16:58:13 -08:00
FUJIWARA Katsunori
8f93d72f88 hook: centralize passing HG_PENDING to external hook process
This patch centralizes passing HG_PENDING to external hook process
into '_exthook()'. To make in-memory changes visible to external hook
process, this patch does:

  - write (or schedule to write) in-memory dirstate changes, and
  - set HG_PENDING environment variable, if:
    - a transaction is running, and
    - there are in-memory changes to be visible

This patch tests some commands with some hooks, because transaction
activity of a same hook differs from each other ("---": "not tested").

    ======== ========= ========= ============
    command  preupdate precommit pretxncommit
    ======== ========= ========= ============
    unshelve   o        ---       ---
    backout    x        ---       ---
    import     ---       o         o
    qrefresh   ---       x         o
    ======== ========= ========= ============

Each hooks are examined separately to prevent in-memory changes from
being visible to external process accidentally by side effect of hooks
previously invoked.
2015-10-17 01:15:34 +09:00
Siddharth Agarwal
b27d150373 hook: for python hooks, also return whether an exception was raised
The hook code treats python hooks raising an exception and returning True as
the exact same. This is OK for hooks themselves, but other code that wants to
invoke external code using the same underlying code is a bit more interested in
making a distinction.
2015-10-14 16:27:10 -07:00
Siddharth Agarwal
220ff1aedf hook.runhooks: return a dict of result values
This will be useful to other calling code that would be interested in what the
individual hooks return.
2015-10-14 16:19:47 -07:00
Siddharth Agarwal
4ff268b773 hook: factor out determination of hooks from running them
This will allow other code to run a predetermined series of hooks.
2015-10-14 16:13:31 -07:00
Siddharth Agarwal
d01bcde561 hook: raise a separate exception for when loading a hook fails
For easier catching.
2015-10-12 18:49:23 -07:00
Pierre-Yves David
30913031d4 error: get Abort from 'error' instead of 'util'
The home of 'Abort' is 'error' not 'util' however, a lot of code seems to be
confused about that and gives all the credit to 'util' instead of the
hardworking 'error'. In a spirit of equity, we break the cycle of injustice and
give back to 'error' the respect it deserves. And screw that 'util' poser.

For great justice.
2015-10-08 12:55:45 -07:00
Gregory Szorc
518abbde79 hook: use absolute_import 2015-08-08 19:24:40 -07:00
Gregory Szorc
5380dea2a7 global: mass rewrite to use modern exception syntax
Python 2.6 introduced the "except type as instance" syntax, replacing
the "except type, instance" syntax that came before. Python 3 dropped
support for the latter syntax. Since we no longer support Python 2.4 or
2.5, we have no need to continue supporting the "except type, instance".

This patch mass rewrites the exception syntax to be Python 2.6+ and
Python 3 compatible.

This patch was produced by running `2to3 -f except -w -n .`.
2015-06-23 22:20:08 -07:00
Jordi Gutiérrez Hermoso
e8b1877d8f hooks: replace if-try-finally with a "with" statement
This seems like a textbook case for the new demandimport.deactivated
context manager: check if something must be done, do it, and cleanup
at the end regardless of exceptions.

The diff isn't as bad as it seems. It's just all the whitespace
changes due to needing an extra level of indentation. It looks cleaner
with `hg diff -w`.
2015-05-28 16:42:04 -04:00
Pierre-Yves David
62bf35f35b hook: drop dedicated catch for 'KeyboardInterrupt'
This is no longer under 'Exception' in Python 2.6.
2015-05-18 15:40:23 -05:00
Matt Mackall
c71e6b4a5d hooks: use try/except/finally 2015-05-15 09:56:43 -05:00
Matt Harbison
14e6825ce0 hook: forcibly flush stderr for Windows test stability
There are a handful of SSH related test failures on Windows.

  --- c:/Users/Matt/Projects/hg/tests/test-bundle2-exchange.t
  +++ c:/Users/Matt/Projects/hg/tests/test-bundle2-exchange.t.err
  @@ -305,16 +305,16 @@
     remote: added 1 changesets with 1 changes to 1 files
     remote: 1 new obsolescence markers
     updating bookmark book_5fdd
  +  pre-close-tip:02de42196ebe draft book_02de
  +  postclose-tip:02de42196ebe draft book_02de
  +  txnclose hook: HG_SOURCE=push-response HG_TXNNAME=push-response
  +  ssh://user@dummy/other HG_URL=ssh://user@dummy/other
     remote: pre-close-tip:5fddd98957c8 draft book_5fdd
     remote: pushkey: lock state after "bookmarks"
     remote: lock:  free
     remote: wlock: free
     remote: postclose-tip:5fddd98957c8 draft book_5fdd
     remote: txnclose hook: (env vars truncated)
  -  pre-close-tip:02de42196ebe draft book_02de
  -  postclose-tip:02de42196ebe draft book_02de
  -  txnclose hook: HG_SOURCE=push-response HG_TXNNAME=push-response
  -  ssh://user@dummy/other HG_URL=ssh://user@dummy/other
     $ hg -R other log -G
     o  6:5fddd98957c8 draft Nicolas Dumazet <...> book_5fdd C
     |

  --- c:/Users/Matt/Projects/hg/tests/test-ssh.t
  +++ c:/Users/Matt/Projects/hg/tests/test-ssh.t.err
  @@ -438,12 +438,12 @@
     $ hg push
     pushing to ssh://user@dummy/remote
     searching for changes
  +  local stdout
     remote: adding changesets
     remote: adding manifests
     remote: adding file changes
     remote: added 1 changesets with 1 changes to 1 files
  -  remote: KABOOM
  -  local stdout
  +  remote: KABOOM\r (esc)

     $ cd ..


What is happening is that no data is available in 'sshpeer.pipee' while the
command is executing.  As the command completes, local output is printed, and
then sshpeer.cleanup() is called.  When it calls 'self.pipeo.close()', the child
process is shutdown, flushing stderr.

As an experiment, I printed a line to stdout and another to stderr instead this
flush().  The stdout data was immediately available to the hg client, and none
of the stderr data was until the child exited.  At that point, pipee has all of
the buffered data, and it is read out and printed before the pipe is closed in
sshpeer.cleanup().  This is probably a known issue, since ui.write_err()
mentions that stderr may be buffered, and also flushes stderr.

It would be nice if there was a more general fix (there is one more test that
fails), but I'm not sure what it is.  I've seen (ancient) references [1] to
setvbuf() "crashing spectacularly" on some systems if any I/O has been done
already, so it seems worth avoiding.


https://sourceware.org/ml/gdb-patches/2013-08/msg00422.html

[1] https://groups.google.com/forum/#!msg/comp.lang.python/JT8LiYzYDEY/Qg9d1HwyjScJ
2015-04-10 22:30:19 -04:00
Matt Mackall
289d6b53bc merge with stable 2014-12-01 19:34:11 -06:00
Pierre-Yves David
6cf24b99de hook: raise a more specialized HookAbort exception when a hook fails
We need to gracefully handle some aborts for pushkey, especially
because it leads to a user-facing crash over the wireprotocols. So we
need a more specialized exception to catch.
2014-11-29 19:13:35 -08:00
Yuya Nishihara
ceda6fbba9 util.system: use ui.system() in place of optional ui.fout parameter 2014-11-08 13:06:22 +09:00
Yuya Nishihara
990e90eeb6 hook: remove redundant code to redirect http hook output to client stream
out=ui and out=ui.fout should be the same here.  ui.fout was introduced at
7d217b512ae2, which was not available when out=ui was added at 5716839ccf7d.
2014-11-12 22:21:51 +09:00
Augie Fackler
523e972164 hook: restore use of callable() since it was readded in Python 3.2 2014-06-23 09:24:38 -04:00
Mads Kiilerich
906d7f81d3 hooks: for python hooks, consistently use __name__ etc as name, not the repr
There is no reason to expose unnecessary Python implementation details and
memory locations, also not in debug mode.

readablefunc was already creating a nice name - we move that functionality
up and reuse it.

We consider having a __call__ and being types.FunctionType sufficiently
similar and unify these two to just using the existing check for __call__.
2014-02-15 01:23:12 +01:00
Mads Kiilerich
ae52b69f2f hooks: move logging of hook name to after we have found the hook
We do not expect the finding process to take a significant amount of time - it
is the execution time of the hook we care about.
2014-02-15 01:23:12 +01:00
Brodie Rao
fc55ccd1ef hooks: only disable/re-enable demandimport when it's already enabled
This fixes an issue introduced in 818c8992811a where, when disabling
demandimport while running hooks, it's inadvertently re-enabled even when
it was never enabled in the first place.

This doesn't affect normal command line usage of Mercurial; it only matters
when Mercurial is run with demandimport intentionally disabled.
2014-02-10 14:51:06 -08:00
Durham Goode
7b7c4c6333 blackbox: do not translate the log messages
User 'timeless' in irc mentioned that having the blackbox be
translated would result in logs that:

- may be mixed language, if multiple users use the same repo
- are not google searchable (since searching for english gives more
  results)
- might not be readable by an admin if the employee is using hg in
  his native language

And therefore we should log everything in english.
2013-02-13 12:51:30 -08:00
Durham Goode
86c94b36d9 blackbox: logs python and extension hooks via ui.log()
Logs python and extension hooks to ui.log() for viewing in the blackbox.

Example log lines:
2013/02/09 08:35:19 durham> pythonhook-preupdate: hgext.eol.preupdate finished in 0.01 seconds
2013/02/09 08:35:19 durham> exthook-update: echo hooked finished in 0.02 seconds
2013-02-09 09:04:32 -08:00
Dirkjan Ochtman
73f0d55371 hook: disable demandimport before importing hooks
This solved an obscure bug for me. In upgrading Distribute on the server, a
patch was added to has a try: import a except ImportError thing that's only
supposed to work with Python 3.3. I'm using 2.7. My hook failed with an
ImportError because of this. It seems kind of sensible to turn off
demandimport before importing the hook, since the except ImportError pattern
is used quite a bit in Python code (including in other Distribute code).
2012-12-20 21:26:30 +01:00
Matt Mackall
1c34c93b25 hooks: be even more forgiven of non-fd descriptors (issue3711)
Looks like there are instances where sys.stdout/stderr contain file
handles that are invalid. We should be tolerant of this for hook I/O
redirection, as our primary concern is not garbling our own output stream.
2012-11-26 17:48:39 -06:00
Matt Mackall
c6fe65e9e2 hooks: delay I/O redirection until we actually run a hook (issue3711)
We were attempting to redirect I/O even if no hook was actually
getting called. This defers redirection until we've found something to
do.
2012-11-26 16:14:22 -06:00
Mads Kiilerich
5e3dc3e383 avoid using abbreviations that look like spelling errors 2012-08-27 23:14:27 +02:00
Mads Kiilerich
377db36818 help: fix some instances of 'the the' 2012-07-26 02:54:13 +02:00
Simon Heimberg
8eb4380be9 hooks: print out more information when loading a python hook fails
When loading a python hook with file syntax fails, there is no
information that this happened while loading a hook. When the python
file does not exist even the file name is not printed. (Only that a
file is missing.)

This patch adds this information and a test for loading a non existing file and
a directory not being a python module.
2012-07-06 18:41:25 +02:00
Idan Kamara
7875dccb0b ui: add a variable to control whether hooks should be called
So hooks can be temporarily disabled.
2012-06-23 19:57:07 +03:00
Matt Zuba
60b19beb18 hooks: prioritize run order of hooks
As of Mercurial 1.3, hooks are sorted in the order they are read into
Mercurial.  There are many instances when someone may want the hooks
sorted in a specific order; this patch allows prioritizing hooks, while
maintaining the existing enumeration for hooks without a priority.
2012-01-15 13:50:12 -07:00
Thomas De Schampheleire
09f4a92184 hook: flush stdout before redirecting to stderr
When hook output redirection is enabled (e.g. when cloning over ssh), hook
output on stdout is redirected to stderr, to prevent the repository data on
stdout from being corrupted.
In certain cases, the redirection could cause part of the repository data to
end up on stderr as well. In case of a clone, this causes:
"abort: consistency error in delta!"

This was seen with a clone over ssh, an outgoing hook present (any
non-python type, e.g. 'pwd'), on certain repositories only,
probably depending on the distribution of the sent data)

This patch updates the hook redirection code to flush stdout before
redirecting, removing the problem.
2011-11-16 08:34:36 +01:00
Matt Mackall
e75325116a merge with stable 2011-08-01 10:54:34 -05:00
Idan Kamara
a315a7adaf hook: be prepared for __stdout/err__ not having fileno()
it may have been replaced, see https://bitbucket.org/tortoisehg/thg/issue/937
2011-07-30 23:41:10 +03:00
Lee Cantey
22431d693c hooks: use python 2.4 compatible exception handling 2011-07-22 08:03:47 -07:00
Idan Kamara
76465b4a18 hooks: redirect stdout/err/in to the ui descriptors when calling python hooks
We need to make sure that python hooks I/O goes through the ui descriptors so
it doesn't mess the command server protocol.
2011-07-09 19:06:59 +03:00
Idan Kamara
2a64fb6699 hook: write hook output to ui fout descriptor
output on stderr will also be written to ui.fout, unless sys.__stdout__
is passed in (see util.system), thus not changing previous behavior.

This fixes a bug where hooks run through the command server would mess up
with the command protocol, sending non-channeled data to the client.
2011-06-21 16:55:21 +03:00
Augie Fackler
fdd2f9d735 globally: use safehasattr(x, '__call__') instead of hasattr(x, '__call__') 2011-07-25 16:24:37 -05:00
Augie Fackler
c1bfd9e71f windows: check util.mainfrozen() instead of ad-hoc checks everywhere 2011-07-25 16:14:02 -05:00
Adrian Buehlmann
e94d06bb79 rename explain_exit to explainexit 2011-05-06 15:31:09 +02:00
Dan Villiom Podlaski Christiansen
83b3b076d6 hooks: sort any dictionaries set in the environment
The actual order of dictionary items is implementation-defined in
Python, and differs between CPython and PyPy. With this change,
test-hooks.t passes with PyPy.
2010-12-24 15:22:00 +01:00
Matt Mackall
be1d11e804 hook: fix import path handling for repo=None 2010-12-10 19:18:02 -06:00
Alexander Solovyov
e86de517fe hook: assume relative path to hook is given from repo root 2010-12-07 15:27:04 +01:00
Maxim Khitrov
1cf52a2e29 http: deliver hook output to client 2010-06-30 18:15:23 -05:00
Matt Mackall
595d66f424 Update license to GPLv2+ 2010-01-19 22:20:08 -06:00
Sune Foldager
afbe5c8543 hook: fix bug (reuse of variable) introduced in 4f2cae90dd77
For binary installs, the 'name' argument would be reused as a local variable,
destroying its original value. The patch fixes that, and also avoids copying
sys.path when it's not necessary.
2009-12-21 16:12:43 +01:00
Bryan O'Sullivan
4035f1dfaa Make it possible to debug failed hook imports via use of --traceback
Prior to this change, if a Python hook module failed to load (e.g. due
to an import error or path problem), it was impossible to figure out
why the error occurred, because the ImportErrors that got raised were
caught but never displayed.

If run with --traceback or ui.traceback=True, hg now prints tracebacks
of both of the ImportError instances that get raised before it bails.
2009-11-12 14:05:52 -08:00
Sune Foldager
ba068758a6 hook: only redirect stdout if it and stderr are valid files
When using hgwebdir with WSGI via the IIS ISAPI-WSGI extension, both
stdout and stderr filenos are set to -2, which makes the os.dup call
in hook.py fail.
2009-10-28 21:35:57 +01:00
Steve Borho
c96b2aca86 hook: fix full path imports on Windows (issue1779)
Bottom portion fixes full path imports on source installs on Windows.
The top portion further fixes full path imports on binary installs.

Initial patch by Roman V. Kiseliov
2009-08-05 21:45:54 -05:00