Commit Graph

91 Commits

Author SHA1 Message Date
Pulkit Goyal
43464e00e4 py3: convert keys of kwargs back to bytes using pycompat.byteskwargs() 2017-06-17 15:29:26 +05:30
Pulkit Goyal
2e30032f2c py3: convert exception to bytes to pass into ui.warn()
Here encoding.strtolocal() is used because exc maybe an IOError which could
contain a valid non-ascii unicode.
2017-06-02 10:35:21 +05:30
Pulkit Goyal
e95681a9cb py3: ensure that we don't concat bytes and str and the end result is bytes
Here obj.__module__ and obj.__name__ are str. Either we can convert them to
bytes or use an r'' and convert back to bytes when concat is done. I preferred
the later one since we are encoding only once here.
2017-06-01 01:41:34 +05:30
Pierre-Yves David
e3a8075346 hook: add hook name information to external hook
While we are here, we can also add the hook name information to external hook.
2017-03-31 11:53:56 +02:00
Pierre-Yves David
833b1335ba hook: provide hook type information to external hook
The python hooks have access to the hook type information. There is not reason
for external hook to not be aware of it too.

For the record my use case is to make sure a hook script is configured for the
right type.
2017-03-31 11:08:11 +02:00
Pierre-Yves David
e6f763fb31 hook: use 'htype' in 'hook'
Same rational as for 'runhooks', we fix the naming in another function.
2017-03-31 11:06:42 +02:00
Pierre-Yves David
9eacfb523e hook: use 'htype' in 'runhooks'
Same rational as for '_pythonhook', 'htype' is more accurate and less error
prone. We just fixed an error from the 'name'/'hname' confusion and this should
prevent them in the future.
2017-03-31 11:03:23 +02:00
Pierre-Yves David
be64e99e37 hook: fix name used in untrusted message
The name used in the message we issue when a hook is untrusted was using "name"
which is actually the hook type and not the name of the hook.
2017-03-31 11:02:05 +02:00
Pierre-Yves David
80012baa72 hook: use "htype" as variable name in _pythonhook
We rename 'name' to 'htype' because it fits the variable content better.
Multiple python hooks already use 'htype' as a name for the argument. This makes
the difference with "hname" clearer and the code less error prone.
2017-03-31 10:59:37 +02:00
Simon Farnsworth
8539b5a667 hook: give exthooks tags for blocking time
The ui.system autogenerated tag isn't really useful - as they're named, let's
use the name the user gave us.
2017-03-06 03:09:15 -08:00
Simon Farnsworth
e0b70e4f7f mercurial: switch to util.timer for all interval timings
util.timer is now the best available interval timer, at the expense of not
having a known epoch. Let's use it whenever the epoch is irrelevant.
2017-02-15 13:17:39 -08:00
Pulkit Goyal
97f340e354 py3: use pycompat.getcwd() instead of os.getcwd()
We have pycompat.getcwd() which returns bytes path on Python 3. This patch
changes most of the occurences of the os.getcwd() with pycompat one.
2016-11-23 00:03:11 +05:30
Yuya Nishihara
47f9c8b52e py3: bulk replace sys.stdin/out/err by util's
Almost all sys.stdin/out/err in hgext/ and mercurial/ are replaced by util's.
There are a few exceptions:

 - lsprof.py and statprof.py are untouched since they are a kind of vendor
   code and they never import mercurial modules right now.
 - ui._readline() needs to replace sys.stdin and stdout to pass them to
   raw_input(). We'll need another workaround here.
2016-10-20 23:53:36 +09:00
Yuya Nishihara
70fcbf2a7e hook: lower inflated use of sys.__stdout__ and __stderr__
They were introduced at 07994b208874, where sys.stdout could be replaced by
sys.stderr. After that, we've changed the way of stdout redirection by
7d217b512ae2, so we no longer need to reference the original __stdout__ and
__stderr__ objects.

Let's move away from using __std*__ objects so we can simply wrap sys.std*
objects for Python 3 porting.
2016-11-08 22:41:45 +09:00
Yuya Nishihara
33994d4697 hook: flush stdout before restoring stderr redirection
There was a similar issue to febfe464a80f. If an in-process hook writes
to stdout, the data may be buffered. In which case, stdout must be flushed
before restoring its file descriptor. Otherwise, remaining data would be sent
over the ssh wire and corrupts the protocol.

Note that this is a different redirection from the one I've just removed.
2016-11-08 22:22:22 +09:00
Yuya Nishihara
cf2dbd1027 hook: do not redirect stdout/err/in to ui while running in-process hooks (BC)
It was introduced by e0df29fe80bf to address command-server issues. After
that, I've made a complete fix by b096b3ac9391, so we don't need to replace
sys.stdio objects to protect the IPC channels.

This change means we no longer see data written to sys.stdout/err by an
in-process hook on command server. I think that's okay because the canonical
way is to use ui functions and in-process hooks should respect the Mercurial
API.

This will help Python 3 porting, where sys.stdout is TextIO but ui.fout is
BytesIO.
2016-10-20 22:39:59 +09:00
Pierre-Yves David
85af4946a6 hook: report untrusted hooks as failure (issue5110) (BC)
Before this patch, there was no way for a repository owner to ensure that
validation hooks would be run by people with write access. If someone had write
access but did not trust the user owning the repository, the config and its hook
would simply be ignored.

After this patch, hooks from untrusted configs are taken into account but never
actually run. Instead they are reported as failures right away. This will ensure
validation performed by a hook is not ignored.

As a side effect writer can be forced to trust a repository hgrc by adding a
'pretxnopen.trust=true' hook to the file.

This was discussed during the 3.8 sprint with Matt Mackall, Augie Fackler and
Kevin Bullock.
2016-04-14 02:41:15 -07:00
Pierre-Yves David
4d33b1dcbd hook: split config reading further
We want an easy way to fetch the hook config with and without honoring
"trusted" so that we can compare the values. So we extract the part
retrieving raw hook data from the config to be able to call it twice
in the next patch.
2016-04-14 17:03:49 -07:00
Pierre-Yves David
c43606ce3c hook: small refactor to store hooks as dict instead of list
We are about to take untrusted hooks into account (to report them as failures)
so we need to rearrange the code a bit to allow config overwriting each other
in a later patch.
2016-04-14 02:28:46 -07:00
Siddharth Agarwal
392dc5ff01 hook: don't crash on syntax errors in python hooks
We had some real-world cases where syntax errors in Python hooks would crash
the whole process and leave it in an indeterminate state. Handle those better.
2016-02-12 14:50:10 -08:00
Siddharth Agarwal
e25b5c0e93 hook: for python hook exceptions, add note to run with --traceback
Just like with ImportErrors, it isn't obvious that --traceback will produce
helpful debugging output here.
2016-02-12 11:44:35 -08:00
Siddharth Agarwal
1605280a66 hook: even fewer parentheses for load errors
Missed this one.
2016-02-12 11:34:04 -08:00
Siddharth Agarwal
2e2fbd19a4 hook: for python hook ImportErrors, add note to run with --traceback
I personally found it completely non-obvious that --traceback prints out stack
traces for failed imports.
2016-02-11 22:52:23 -08:00
Siddharth Agarwal
d77e1d7bef hook: fewer parentheses for hook load errors
This matches 'hook failed' warnings.

We're also going to add hints to some of the hook load errors. Without this
change we'd have two pairs of parens for a single error message, which looks
really cluttered.
2016-02-11 22:41:20 -08:00
Siddharth Agarwal
f93c751fee hook: use sys.exc_info rather than the deprecated equivalents
sys.exc_type etc have been deprecated since Python 1.5.
2016-02-11 22:02:52 -08:00
Sietse Brouwer
f5fed5a270 dirstate: don't write repo.currenttransaction to repo.dirstate if repo
is None (issue4983)

Some hooks, such as post-init and post-clone, do not get a repo parameter in
their environment. If there is no repo, there is no repo.currenttransaction();
attempting to retrieve it anyway was causing crashes. Now currenttransaction is
only retrieved and written if the repo is not None.
2015-12-03 01:38:21 +01:00
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