Mercurial > hg
annotate hgext/fastannotate/protocol.py @ 47120:7109a38830c9
dirstate-tree: Fold "tracked descendants" counter update in main walk
For the purpose of implementing `has_tracked_dir` (which means "has tracked
descendants) without an expensive sub-tree traversal, we maintaing a counter
of tracked descendants on each "directory" node of the tree-shaped dirstate.
Before this changeset, mutating or inserting a node at a given path would
involve:
* Walking the tree from root through ancestors to find the node or the spot
where to insert it
* Looking at the previous node if any to decide what counter update is needed
* Performing any node mutation
* Walking the tree *again* to update counters in ancestor nodes
When profiling `hg status` on a large repo, this second walk takes times
while loading a the dirstate from disk.
It turns out we have enough information to decide before he first tree walk
what counter update is needed. This changeset merges the two walks, gaining
~10% of the total time for `hg update` (in the same hyperfine benchmark as
the previous changeset).
---
Profiling was done by compiling with this `.cargo/config`:
[profile.release]
debug = true
then running with:
py-spy record -r 500 -n -o /tmp/hg.json --format speedscope -- \
./hg status -R $REPO --config experimental.dirstate-tree.in-memory=1
then visualizing the recorded JSON file in https://www.speedscope.app/
Differential Revision: https://phab.mercurial-scm.org/D10554
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Fri, 30 Apr 2021 14:22:14 +0200 |
parents | b6b696442a4d |
children | c424ff4807e6 |
rev | line source |
---|---|
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
1 # Copyright 2016-present Facebook. All Rights Reserved. |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
2 # |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
3 # protocol: logic for a server providing fastannotate support |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
4 # |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
6 # GNU General Public License version 2 or any later version. |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
7 from __future__ import absolute_import |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
8 |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
9 import contextlib |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
10 import os |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
11 |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
12 from mercurial.i18n import _ |
43085
eef9a2d67051
py3: manually import pycompat.open into files that need it
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43077
diff
changeset
|
13 from mercurial.pycompat import open |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
14 from mercurial import ( |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
15 error, |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
16 extensions, |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
17 hg, |
43105
649d3ac37a12
py3: define and use pycompat.iteritems() for hgext/
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43085
diff
changeset
|
18 pycompat, |
39251
bb2b462f81da
fastannotate: pconvert paths from the server for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents:
39215
diff
changeset
|
19 util, |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
20 wireprotov1peer, |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
21 wireprotov1server, |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
22 ) |
46946
b6b696442a4d
fastannotate: use `get_unique_pull_path`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43105
diff
changeset
|
23 from mercurial.utils import ( |
b6b696442a4d
fastannotate: use `get_unique_pull_path`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43105
diff
changeset
|
24 urlutil, |
b6b696442a4d
fastannotate: use `get_unique_pull_path`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43105
diff
changeset
|
25 ) |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
26 from . import context |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
27 |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
28 # common |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
29 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
30 |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
31 def _getmaster(ui): |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
32 """get the mainbranch, and enforce it is set""" |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
33 master = ui.config(b'fastannotate', b'mainbranch') |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
34 if not master: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
35 raise error.Abort( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
36 _( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
37 b'fastannotate.mainbranch is required ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
38 b'for both the client and the server' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
39 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
40 ) |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
41 return master |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
42 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
43 |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
44 # server-side |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
45 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
46 |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
47 def _capabilities(orig, repo, proto): |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
48 result = orig(repo, proto) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
49 result.append(b'getannotate') |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
50 return result |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
51 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
52 |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
53 def _getannotate(repo, proto, path, lastnode): |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
54 # output: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
55 # FILE := vfspath + '\0' + str(size) + '\0' + content |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
56 # OUTPUT := '' | FILE + OUTPUT |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
57 result = b'' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
58 buildondemand = repo.ui.configbool( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
59 b'fastannotate', b'serverbuildondemand', True |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
60 ) |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
61 with context.annotatecontext(repo, path) as actx: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
62 if buildondemand: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
63 # update before responding to the client |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
64 master = _getmaster(repo.ui) |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
65 try: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
66 if not actx.isuptodate(master): |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
67 actx.annotate(master, master) |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
68 except Exception: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
69 # non-fast-forward move or corrupted. rebuild automically. |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
70 actx.rebuild() |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
71 try: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
72 actx.annotate(master, master) |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
73 except Exception: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
74 actx.rebuild() # delete files |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
75 finally: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
76 # although the "with" context will also do a close/flush, we |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
77 # need to do it early so we can send the correct respond to |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
78 # client. |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
79 actx.close() |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
80 # send back the full content of revmap and linelog, in the future we |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
81 # may want to do some rsync-like fancy updating. |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
82 # the lastnode check is not necessary if the client and the server |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
83 # agree where the main branch is. |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
84 if actx.lastnode != lastnode: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
85 for p in [actx.revmappath, actx.linelogpath]: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
86 if not os.path.exists(p): |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
87 continue |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
88 with open(p, b'rb') as f: |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
89 content = f.read() |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
90 vfsbaselen = len(repo.vfs.base + b'/') |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
91 relpath = p[vfsbaselen:] |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
92 result += b'%s\0%d\0%s' % (relpath, len(content), content) |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
93 return result |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
94 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
95 |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
96 def _registerwireprotocommand(): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
97 if b'getannotate' in wireprotov1server.commands: |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
98 return |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
99 wireprotov1server.wireprotocommand(b'getannotate', b'path lastnode')( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
100 _getannotate |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
101 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
102 |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
103 |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
104 def serveruisetup(ui): |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
105 _registerwireprotocommand() |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
106 extensions.wrapfunction(wireprotov1server, b'_capabilities', _capabilities) |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
107 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
108 |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
109 # client-side |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
110 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
111 |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
112 def _parseresponse(payload): |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
113 result = {} |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
114 i = 0 |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
115 l = len(payload) - 1 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
116 state = 0 # 0: vfspath, 1: size |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
117 vfspath = size = b'' |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
118 while i < l: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
119 ch = payload[i : i + 1] |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
120 if ch == b'\0': |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
121 if state == 1: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
122 result[vfspath] = payload[i + 1 : i + 1 + int(size)] |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
123 i += int(size) |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
124 state = 0 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
125 vfspath = size = b'' |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
126 elif state == 0: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
127 state = 1 |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
128 else: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
129 if state == 1: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
130 size += ch |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
131 elif state == 0: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
132 vfspath += ch |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
133 i += 1 |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
134 return result |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
135 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
136 |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
137 def peersetup(ui, peer): |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
138 class fastannotatepeer(peer.__class__): |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
139 @wireprotov1peer.batchable |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
140 def getannotate(self, path, lastnode=None): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
141 if not self.capable(b'getannotate'): |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
142 ui.warn(_(b'remote peer cannot provide annotate cache\n')) |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
143 yield None, None |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
144 else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
145 args = {b'path': path, b'lastnode': lastnode or b''} |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
146 f = wireprotov1peer.future() |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
147 yield args, f |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
148 yield _parseresponse(f.value) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
149 |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
150 peer.__class__ = fastannotatepeer |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
151 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
152 |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
153 @contextlib.contextmanager |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
154 def annotatepeer(repo): |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
155 ui = repo.ui |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
156 |
46946
b6b696442a4d
fastannotate: use `get_unique_pull_path`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43105
diff
changeset
|
157 remotedest = ui.config(b'fastannotate', b'remotepath', b'default') |
b6b696442a4d
fastannotate: use `get_unique_pull_path`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43105
diff
changeset
|
158 r = urlutil.get_unique_pull_path(b'fastannotate', repo, ui, remotedest) |
b6b696442a4d
fastannotate: use `get_unique_pull_path`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43105
diff
changeset
|
159 remotepath = r[0] |
39213
303dae0136b0
fastannotate: rip out specialized support for remotefilelog
Augie Fackler <augie@google.com>
parents:
39211
diff
changeset
|
160 peer = hg.peer(ui, {}, remotepath) |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
161 |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
162 try: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
163 yield peer |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
164 finally: |
39213
303dae0136b0
fastannotate: rip out specialized support for remotefilelog
Augie Fackler <augie@google.com>
parents:
39211
diff
changeset
|
165 peer.close() |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
166 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
167 |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
168 def clientfetch(repo, paths, lastnodemap=None, peer=None): |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
169 """download annotate cache from the server for paths""" |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
170 if not paths: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
171 return |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
172 |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
173 if peer is None: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
174 with annotatepeer(repo) as peer: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
175 return clientfetch(repo, paths, lastnodemap, peer) |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
176 |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
177 if lastnodemap is None: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
178 lastnodemap = {} |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
179 |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
180 ui = repo.ui |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
181 results = [] |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
182 with peer.commandexecutor() as batcher: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
183 ui.debug(b'fastannotate: requesting %d files\n' % len(paths)) |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
184 for p in paths: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
185 results.append( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
186 batcher.callcommand( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
187 b'getannotate', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
188 {b'path': p, b'lastnode': lastnodemap.get(p)}, |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
189 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
190 ) |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
191 |
39715
d8a7690ccc74
fastannotate: process files as they arrive
Martin von Zweigbergk <martinvonz@google.com>
parents:
39620
diff
changeset
|
192 for result in results: |
d8a7690ccc74
fastannotate: process files as they arrive
Martin von Zweigbergk <martinvonz@google.com>
parents:
39620
diff
changeset
|
193 r = result.result() |
d8a7690ccc74
fastannotate: process files as they arrive
Martin von Zweigbergk <martinvonz@google.com>
parents:
39620
diff
changeset
|
194 # TODO: pconvert these paths on the server? |
43105
649d3ac37a12
py3: define and use pycompat.iteritems() for hgext/
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43085
diff
changeset
|
195 r = {util.pconvert(p): v for p, v in pycompat.iteritems(r)} |
39715
d8a7690ccc74
fastannotate: process files as they arrive
Martin von Zweigbergk <martinvonz@google.com>
parents:
39620
diff
changeset
|
196 for path in sorted(r): |
d8a7690ccc74
fastannotate: process files as they arrive
Martin von Zweigbergk <martinvonz@google.com>
parents:
39620
diff
changeset
|
197 # ignore malicious paths |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
198 if not path.startswith(b'fastannotate/') or b'/../' in ( |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
199 path + b'/' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
200 ): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
201 ui.debug( |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
202 b'fastannotate: ignored malicious path %s\n' % path |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
203 ) |
39715
d8a7690ccc74
fastannotate: process files as they arrive
Martin von Zweigbergk <martinvonz@google.com>
parents:
39620
diff
changeset
|
204 continue |
d8a7690ccc74
fastannotate: process files as they arrive
Martin von Zweigbergk <martinvonz@google.com>
parents:
39620
diff
changeset
|
205 content = r[path] |
d8a7690ccc74
fastannotate: process files as they arrive
Martin von Zweigbergk <martinvonz@google.com>
parents:
39620
diff
changeset
|
206 if ui.debugflag: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
207 ui.debug( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
208 b'fastannotate: writing %d bytes to %s\n' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
209 % (len(content), path) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
210 ) |
39715
d8a7690ccc74
fastannotate: process files as they arrive
Martin von Zweigbergk <martinvonz@google.com>
parents:
39620
diff
changeset
|
211 repo.vfs.makedirs(os.path.dirname(path)) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
212 with repo.vfs(path, b'wb') as f: |
39715
d8a7690ccc74
fastannotate: process files as they arrive
Martin von Zweigbergk <martinvonz@google.com>
parents:
39620
diff
changeset
|
213 f.write(content) |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
214 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
215 |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
216 def _filterfetchpaths(repo, paths): |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
217 """return a subset of paths whose history is long and need to fetch linelog |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
218 from the server. works with remotefilelog and non-remotefilelog repos. |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
219 """ |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
220 threshold = repo.ui.configint(b'fastannotate', b'clientfetchthreshold', 10) |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
221 if threshold <= 0: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
222 return paths |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
223 |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
224 result = [] |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
225 for path in paths: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
226 try: |
39213
303dae0136b0
fastannotate: rip out specialized support for remotefilelog
Augie Fackler <augie@google.com>
parents:
39211
diff
changeset
|
227 if len(repo.file(path)) >= threshold: |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
228 result.append(path) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
229 except Exception: # file not found etc. |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
230 result.append(path) |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
231 |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
232 return result |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
233 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
234 |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
235 def localreposetup(ui, repo): |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
236 class fastannotaterepo(repo.__class__): |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
237 def prefetchfastannotate(self, paths, peer=None): |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
238 master = _getmaster(self.ui) |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
239 needupdatepaths = [] |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
240 lastnodemap = {} |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
241 try: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
242 for path in _filterfetchpaths(self, paths): |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
243 with context.annotatecontext(self, path) as actx: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
244 if not actx.isuptodate(master, strict=False): |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
245 needupdatepaths.append(path) |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
246 lastnodemap[path] = actx.lastnode |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
247 if needupdatepaths: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
248 clientfetch(self, needupdatepaths, lastnodemap, peer) |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
249 except Exception as ex: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
250 # could be directory not writable or so, not fatal |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
251 self.ui.debug(b'fastannotate: prefetch failed: %r\n' % ex) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
252 |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
253 repo.__class__ = fastannotaterepo |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
254 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41365
diff
changeset
|
255 |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
256 def clientreposetup(ui, repo): |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
257 _registerwireprotocommand() |
39543
2182e67ea912
fastannotate: use repo.local()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39251
diff
changeset
|
258 if repo.local(): |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
259 localreposetup(ui, repo) |
39214
8da20fc9fc07
fastannotate: move some global state mutation to extsetup()
Augie Fackler <augie@google.com>
parents:
39213
diff
changeset
|
260 # TODO: this mutates global state, but only if at least one repo |
8da20fc9fc07
fastannotate: move some global state mutation to extsetup()
Augie Fackler <augie@google.com>
parents:
39213
diff
changeset
|
261 # has the extension enabled. This is probably bad for hgweb. |
39210
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
262 if peersetup not in hg.wirepeersetupfuncs: |
1ddb296e0dee
fastannotate: initial import from Facebook's hg-experimental
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
263 hg.wirepeersetupfuncs.append(peersetup) |