1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 import os
44
45 from mercurial.node import bin, hex, nullid
46 from mercurial.context import workingctx
47
48
49
50
51 try:
52 from mercurial import demandimport
53 demandimport.disable()
54 except ImportError:
55 pass
56
57 -def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
58
59 master = ui.config('hgbuildbot', 'master')
60 if master:
61 branchtype = ui.config('hgbuildbot', 'branchtype')
62 branch = ui.config('hgbuildbot', 'branch')
63 fork = ui.configbool('hgbuildbot', 'fork', False)
64
65 stripcount = int(ui.config('notify','strip') or ui.config('hgbuildbot','strip',3))
66 category = ui.config('hgbuildbot', 'category', None)
67 project = ui.config('hgbuildbot', 'project', '')
68 else:
69 ui.write("* You must add a [hgbuildbot] section to .hg/hgrc in "
70 "order to use buildbot hook\n")
71 return
72
73 if hooktype != "changegroup":
74 ui.status("hgbuildbot: hooktype %s not supported.\n" % hooktype)
75 return
76
77 if fork:
78 child_pid = os.fork()
79 if child_pid == 0:
80
81 pass
82 else:
83
84 ui.status("Notifying buildbot...\n")
85 return
86
87
88 from buildbot.clients import sendchange
89 from twisted.internet import defer, reactor
90
91 if branch is None:
92 if branchtype is not None:
93 if branchtype == 'dirname':
94 branch = os.path.basename(repo.root)
95 if branchtype == 'inrepo':
96 branch = workingctx(repo).branch()
97
98 s = sendchange.Sender(master, None)
99 d = defer.Deferred()
100 reactor.callLater(0, d.callback, None)
101
102 def _send(res, c):
103 if not fork:
104 ui.status("rev %s sent\n" % c['revision'])
105 return s.send(c['branch'], c['revision'], c['comments'],
106 c['files'], c['username'], category=category,
107 repository=repository, project=project)
108
109 try:
110 start = repo[node].rev()
111 end = len(repo)
112 except TypeError:
113 start = repo.changelog.rev(bin(node))
114 end = repo.changelog.count()
115
116 repository = strip(repo.root, stripcount)
117
118 for rev in xrange(start, end):
119
120 node = repo.changelog.node(rev)
121 manifest, user, (time, timezone), files, desc, extra = repo.changelog.read(node)
122 parents = filter(lambda p: not p == nullid, repo.changelog.parents(node))
123 if branchtype == 'inrepo':
124 branch = extra['branch']
125
126 if len(parents) > 1 and not files:
127 files = ["merge"]
128 change = {
129 'master': master,
130 'username': user,
131 'revision': hex(node),
132 'comments': desc,
133 'files': files,
134 'branch': branch
135 }
136 d.addCallback(_send, change)
137
138 d.addCallbacks(s.printSuccess, s.printFailure)
139 d.addBoth(s.stop)
140 s.run()
141
142 if fork:
143 os._exit(os.EX_OK)
144 else:
145 return
146
147
149 '''Strip the count first slash of the path'''
150
151
152 path = '/'.join(path.split(os.sep))
153
154 while count > 0:
155 c = path.find('/')
156 if c == -1:
157 break
158 path = path[c + 1:]
159 count -= 1
160 return path
161