Source

repoman-ext / repomq.py

  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
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
'''mq extension - stack of patch management across RepoMan modules'''

import codecs
import os
import socket
import sys

from hgext import mq, purge, graphlog
import mercurial
from mercurial import commands

from repoman import util

def qnew(ui, f, patch, *args, **opts):
    'create a new patch in each module'

    for mod in f:
        ui.write_module_header(mod)
        mq.new(ui, f[mod], patch, *args, **opts)
        ui.write('\n')

def qpop(ui, f, patch=None, **opts):
    'pop the current patch off the stack in each module'

    for mod in f:
        ui.write_module_header(mod)
        mq.pop(ui, f[mod], patch, **opts)
        ui.write('\n')

def qpush(ui, f, patch=None, **opts):
    'push the next patch onto the stack in each module'

    for mod in f:
        ui.write_module_header(mod)
        mq.push(ui, f[mod], patch, **opts)
        ui.write('\n')

def qapplied(ui, f, patch=None, **opts):
    'show the patches already applied in each module'

    for mod in f:
        ui.write_module_header(mod)
        mq.applied(ui, f[mod], patch, **opts)
        ui.write('\n')

def qunapplied(ui, f, patch=None, **opts):
    'show patches not yet applied in each module'

    for mod in f:
        ui.write_module_header(mod)
        mq.unapplied(ui, f[mod], patch, **opts)
        ui.write('\n')

def qremove(ui, f, *patches, **opts):
    'remove specified patches from the patch queue in each module'

    for mod in f:
        ui.write_module_header(mod)
        try:
            mq.delete(ui, f[mod], *patches, **opts)
        except:
            ui.status('%s\n' % (sys.exc_info()[1],))
        ui.write('\n')

def qrefresh(ui, f, *pats, **opts):
    'update the current patch in each module'

    if opts.get('desc'):
        with codecs.open(opts.get('desc'), 'r', 'utf-8') as fh:
            opts['message'] = fh.read().encode(mercurial.encoding.encoding)
        del opts['desc']

    for mod in f:
        ui.write_module_header(mod)
        mq.refresh(ui, f[mod], *pats, **opts)
        ui.write('\n')

def qcommit(ui, f, **opts):
    '''commits each queue repository in each module
    
    This command expects that an existing patch queue repository has been
    initialised, e.g. using repo qinit.'''

    for mod in f:
        commands.commit(ui, f[mod].mq.qrepo(),
                **util.get_mercurial_default_opts('commit', **opts))

def qfinish(ui, f, **opts):
    'move applied patches into repository history in each module'

    for mod in f:
        mq.finish(ui, f[mod], **opts)

def qimport(ui, f, *filename, **opts):
    'import a patch in each module'

    for mod in f:
        mq.qimport(ui, f[mod], *filename, **opts)

def qfold(ui, f, *files, **opts):
    'fold named patches into current patch'

    for mod in f:
        mq.fold(ui, f[mod], *files, **opts)

def qinit(ui, f):
    'initialize a new queue repository in each module'

    for mod in f.get_all_modules():
        mq.qinit(ui, f[mod], True)

def _purge(ui, f, **opts):
    '''remove files not tracked by Mercurial in each module
    
    This will irrevocably remove files without any backup. Use with care.'''

    for mod in f:
        purge.purge(ui, f[mod], **opts)

def serve(ui, f, port=8000, allow_push=False):
    '''start stand-alone webserver
    
    Serves all forest repositories and versioned patch queues via HTTP on the
    specified port.

    While the server is active, everyone with access may pull from your
    repository; push is not allowed unless --allow-push is specified.

    Use ctrl + c to stop the server.
    '''
    config = f.ljoin('hgweb.config')

    for mod in f.get_all_modules():
        rc = os.path.join(f.path, mod, '.hg', 'hgrc')
        data = None
        if os.path.exists(rc):
            with open(rc, 'r') as fl:
                data = fl.read()

        if not os.path.exists(rc) or '[web]' not in data:
            with open(rc, 'a') as fl:
                fl.write('\n[web]\nstyle = gitweb\n')

    with open(config, 'w') as fl:
        fl.write('[web]\nstyle = gitweb\n%s[paths]\n' %
                ('push_ssl=False\nallow_push=*\n' if allow_push else ''))
        for mod in f.get_all_modules():
            fl.write('%(mod)s = %(mod)s\n' % {'mod': mod})
            fl.write('%(mod)s/.hg/patches = %(modmq)s\n' % {
                'mod': mod, 'modmq': f[mod].mq.path})

    ui.status('Publishing %s to http://%s:%d/\n' % (f.path,
        socket.gethostname(), port))
    opts = util.get_mercurial_default_opts('serve')
    opts['port'] = port
    opts['webdir_conf'] = config

    cwd = os.getcwd()
    try:
        os.chdir(f.path)
        mercurial.commands.serve(ui, f.lmd_repo, **opts)
    finally:
        os.chdir(cwd)

def qoutgoing(ui, f, path=None, **opts):
    'show changesets not found in the destination in each versioned patch queue'

    for mod in f:
        ui.write_module_header('%s' % (mod,))
        if hasattr(f[mod], 'mq'):
            if not os.path.exists(f[mod].mq.path):
                continue
            modpath = None if not path else ('%s/%s/.hg/patches' % (path, mod))
            mercurial.commands.outgoing(ui, f[mod].mq.qrepo(), modpath, **opts)
            ui.status('\n')

def qsend(ui, f, path=None, **opts):
    '''push changes to each module's versioned patch queue to the specified destination'''

    for mod in f:
        ui.write_module_header('%s' % (mod,))
        if hasattr(f[mod], 'mq'):
            if not os.path.exists(f[mod].mq.path):
                continue
            modpath = None if not path else ('%s/%s/.hg/patches' % (path, mod))
            mercurial.commands.push(ui, f[mod].mq.qrepo(), modpath, **opts)
            ui.status('\n')

def qincoming(ui, f, path=None, **opts):
    "show new changesets found in source for each module's versioned patch queue"

    for mod in f:
        ui.write_module_header('%s' % (mod,))
        if hasattr(f[mod], 'mq'):
            if not os.path.exists(f[mod].mq.path):
                continue
            modpath = None if not path else ('%s/%s/.hg/patches' % (path, mod))
            mercurial.commands.incoming(ui, f[mod].mq.qrepo(), modpath, **opts)
            ui.status('\n')

def qreceive(ui, f, path=None, **opts):
    '''pull in changes from source for each module's versioned patch queue'''

    for mod in f:
        ui.write_module_header('%s' % (mod,))
        if hasattr(f[mod], 'mq'):
            if not os.path.exists(f[mod].mq.path):
                continue
            modpath = None if not path else ('%s/%s/.hg/patches' % (path, mod))
            mercurial.commands.pull(ui, f[mod].mq.qrepo(), modpath, **opts)
            ui.status('\n')

def qupdate(ui, f, **opts):
    "update each module's patch queue to the specified revision(s)"

    for mod in f:
        ui.write_module_header(mod)
        mercurial.commands.update(ui, f[mod].mq.qrepo(), **opts)

def qparents(ui, f, **opts):
    "show the parents of each module's patch queue"

    for mod in f:
        ui.write_module_header(mod)
        mercurial.commands.parents(ui, f[mod].mq.qrepo(), **opts)

def qstatus(ui, f, **opts):
    '''show changed files in each module's versioned patch queue'''

    for mod in f:
        ui.write_module_header(mod)
        mercurial.commands.status(ui, f[mod].mq.qrepo(), **opts)

def qdiff(ui, f, **opts):
    '''diff each module's versioned patch queue (or selected files therein)'''

    for mod in f:
        ui.write_module_header(mod)
        mercurial.commands.diff(ui, f[mod].mq.qrepo(), **opts)

def qrevert(ui, f, *args, **opts):
    '''restore individual files in each module's versioned patch queue'''

    for mod in f:
        ui.write_module_header(mod)
        mqrepo = f[mod].mq.qrepo()
        modargs = [mqrepo.wjoin(arg) for arg in args]
        mercurial.commands.revert(ui, mqrepo, *modargs, **opts)

def qtip(ui, f, **opts):
    '''show the tip changeset in each module's versioned patch queue'''

    for mod in f:
        ui.write_module_header(mod)
        mercurial.commands.tip(ui, f[mod].mq.qrepo(), **opts)

def qlog(ui, f, *args, **opts):
    '''show revision history of each module's versioned patch queue'''

    for mod in f:
        ui.write_module_header(mod)
        mqrepo = f[mod].mq.qrepo()
        modargs = [mqrepo.wjoin(arg) for arg in args]
        mercurial.commands.log(ui, mqrepo, *modargs, **opts)

def qglog(ui, f, *args, **opts):
    '''show graphical revision history of each module's versioned patch queue'''

    for mod in f:
        ui.write_module_header(mod)
        mqrepo = f[mod].mq.qrepo()
        modargs = [mqrepo.wjoin(arg) for arg in args]
        graphlog.graphlog(ui, mqrepo, *modargs, **opts)

def qheads(ui, f, *args, **opts):
    '''show current repository heads in each module's versioned patch queue'''

    for mod in f:
        ui.write_module_header(mod)
        mercurial.commands.heads(ui, f[mod].mq.qrepo(), *args, **opts)

def strip(ui, f, *args, **opts):
    'strip changes from the repository'

    for mod in f:
        mq.strip(ui, f[mod], *args, **opts)

cmdtable = {
        'qnew':
            (qnew, [], mq.cmdtable['^qnew'][1],
                mq.cmdtable['^qnew'][2][len('hg qnew '):]),

        'qpop':
            (qpop, [], mq.cmdtable['^qpop'][1],
                mq.cmdtable['^qpop'][2][len('hg qpop '):]),

        'qpush':
            (qpush, [], mq.cmdtable['^qpush'][1],
                mq.cmdtable['^qpush'][2][len('hg qpush '):]),

        'strip':
            (strip, [], mq.cmdtable['strip'][1],
                mq.cmdtable['strip'][2][len('hg strip '):]),

        'qapplied':
            (qapplied, ['qap'], mq.cmdtable['qapplied'][1],
                mq.cmdtable['qapplied'][2][len('hg qapplied '):]),

        'qunapplied':
            (qunapplied, ['qun'], mq.cmdtable['qunapplied'][1],
                mq.cmdtable['qunapplied'][2][len('hg qunapplied '):]),

        'qremove':
            (qremove, ['qrm', 'qdelete'],
                mq.cmdtable['qdelete|qremove|qrm'][1],
                mq.cmdtable['qdelete|qremove|qrm'][2][len('hg qdelete '):]),

        'qrefresh':
            (qrefresh, ['qref'], mq.cmdtable['^qrefresh'][1] +
                [('', 'desc', '', 'file with commit message (in utf-8)')],
                mq.cmdtable['^qrefresh'][2][len('hg qrefresh '):]),

        'qcommit':
            (qcommit, ['qci'],
                [('m', 'message', '', 'commit description')],
                '[-m MESSAGE]'),

        'qfinish':
            (qfinish, ['qfin'], mq.cmdtable['qfinish'][1],
                mq.cmdtable['qfinish'][2][len('hg qfinish '):]),

        'qimport':
            (qimport, ['qim'], mq.cmdtable['qimport'][1],
                mq.cmdtable['qimport'][2][len('hg qimport '):]),

        'qfold':
            (qfold, [], mq.cmdtable['qfold'][1],
                mq.cmdtable['qfold'][2][len('hg qfold '):]),

        'purge':
            (_purge, [], purge.cmdtable['purge|clean'][1],
                purge.cmdtable['purge|clean'][2][len('hg purge '):]),

        'qserve':
            (serve, [],
                [
                    ('p', 'port', 8000, 'port to listen on (default: 8000)'),
                    ('', 'allow-push', None,
                        'allow users to push to your served forest'),
                ],
                '[-p PORT]'),

        'qinit':
            (qinit, [], [], ''),

        'qoutgoing':
            (qoutgoing, ['qout'], mercurial.commands.table['outgoing|out'][1],
                    mercurial.commands.table['outgoing|out'][2]),

        'qsend':
            (qsend, [], mercurial.commands.table['^push'][1],
                    mercurial.commands.table['^push'][2]),

        'qincoming':
            (qincoming, ['qin'], mercurial.commands.table['incoming|in'][1],
                    mercurial.commands.table['incoming|in'][2]),

        'qreceive':
            (qreceive, ['qrecv', 'qpull'],
                    mercurial.commands.table['^pull'][1],
                    mercurial.commands.table['^pull'][2]),

        'qupdate':
            (qupdate, ['qup'],
                    mercurial.commands.table['^update|up|checkout|co'][1],
                    mercurial.commands.table['^update|up|checkout|co'][2]),

        'qparents':
            (qparents, ['qpar'], mercurial.commands.table['parents'][1],
                    mercurial.commands.table['parents'][2]),

        'qstatus':
            (qstatus, ['qst'], mercurial.commands.table['^status|st'][1],
                mercurial.commands.table['^status|st'][2]),

        'qdiff':
            (qdiff, [], mercurial.commands.table['^diff'][1],
                    mercurial.commands.table['^diff'][2]),

        'qrevert':
            (qrevert, [], mercurial.commands.table['revert'][1],
                    mercurial.commands.table['revert'][2]),

        'qtip':
            (qtip, [], mercurial.commands.table['tip'][1],
                    mercurial.commands.table['tip'][2]),

        'qlog':
            (qlog, [], mercurial.commands.table['^log|history'][1],
                    mercurial.commands.table['^log|history'][2]),

        'qglog':
            (qglog, [], graphlog.cmdtable['glog'][1],
                    graphlog.cmdtable['glog'][2][len('hg glog '):]),

        'qheads':
            (qheads, [], mercurial.commands.table['heads'][1],
                    mercurial.commands.table['heads'][2]),
        }