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