Mercurial > hg > hq
annotate hq/main.py @ 4:44ea39c3e98f
add methods for dealing with the patch repositories
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Tue, 25 May 2010 19:02:21 -0700 |
| parents | 8bc27dbf0214 |
| children | 448c248b3738 |
| rev | line source |
|---|---|
| 0 | 1 #!/usr/bin/env python |
| 2 """ | |
| 3 mercurial queue extension front-end policy manager | |
| 4 """ | |
| 5 | |
| 6 import os | |
| 7 import subprocess | |
| 8 import sys | |
| 9 | |
| 10 from command import CommandParser | |
| 11 | |
| 12 def call(command, *args, **kw): | |
| 13 code = subprocess.call(command, *args, **kw) | |
| 14 if code: | |
| 15 if isinstance(command, basestring): | |
| 16 cmdstr = command | |
| 17 else: | |
| 18 cmdstr = ' '.join(command) | |
| 19 raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code)) | |
| 20 | |
| 21 class HQ(object): | |
| 22 """ | |
| 23 mercurial queue extension front-end policy manager | |
| 24 """ | |
| 25 | |
| 26 def __init__(self, parser, options): | |
| 27 """initialize global options""" | |
| 28 # TODO: look at hgrc file | |
| 1 | 29 # for [defaults] repository_host |
| 0 | 30 |
| 3 | 31 def clone(self, repo, patch=None, queue=None): |
| 32 """ | |
| 33 clone the repository and begin a patch queue | |
| 34 - path: name of a new patch to initiate | |
| 35 - queue: name of the remote queue | |
| 36 """ | |
| 0 | 37 directory = repo.rsplit('/', 1) |
| 38 call(['hg', 'clone', repo, directory]) | |
| 39 os.chdir(directory) | |
| 40 call(['hg', 'qinit', '-c']) | |
| 3 | 41 if queue: |
|
4
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
42 # pull from the given repository |
|
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
43 self._patch_command(*['hg', 'pull', '--update', queue]) |
|
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
44 else: |
|
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
45 # (optionally) setup a new repo |
|
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
46 pass # TODO |
|
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
47 |
| 0 | 48 if patch: |
|
4
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
49 # create a new patch |
| 0 | 50 call(['hg', 'qnew', patch]) |
| 51 | |
| 52 def edit(self, patch=None): | |
| 53 """ | |
| 54 edit a patch | |
| 55 - patch: the patch to edit | |
| 56 """ | |
| 57 | |
| 58 def commit(self, message): | |
| 59 """ | |
| 60 commit a patch and push it to the master repository | |
| 61 """ | |
| 62 call(['hg', 'qrefresh']) | |
| 63 call(['hg', 'qcommit', '-m', message]) | |
|
4
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
64 self._patch_command(*['hg', 'push']) |
| 0 | 65 |
| 66 def pull(self, repo=None): | |
| 67 """ | |
| 68 pull from the root repository | |
| 69 """ | |
| 70 # TODO: commit prior to realignment | |
| 71 call(['hg', 'qpop', '--all']) | |
| 72 call(['hg', 'pull'] + repo and [repo] or []) | |
| 73 call(['hg', 'qpush', '--all']) | |
|
2
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
74 |
| 3 | 75 def goto(self, patch): |
| 76 """ | |
| 77 goto a specific patch | |
| 78 """ | |
| 79 # TODO | |
| 80 process = subprocess.Popen(['hg', 'qapplied'], stdout=subprocess.PIPE) | |
| 81 stdout, stderr = process.communicate() | |
| 82 applied = [ i.strip() for i in stdout.splitlines() | |
| 83 if i ] | |
| 84 raise NotImplementedError | |
| 85 | |
|
2
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
86 def files(self): |
|
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
87 """ |
|
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
88 list the files added by the top patch |
|
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
89 """ |
|
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
90 # TODO: should only list top-level directories, otherwise it's silly |
|
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
91 _oldcwd = os.getcwd() |
|
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
92 process = subprocess.Popen("hg qdiff | grep '^+++ ' | sed 's/+++ b\///'", stdout=subprocess.PIPE) |
|
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
93 stdout, stderr = process.communicate() |
|
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
94 return stdout |
|
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
95 |
|
4
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
96 def _patch_repo(self): |
|
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
97 """the location of the patch repository""" |
|
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
98 root = subprocess.Popen(['hg', 'root'], stdout=subprocess.PIPE).communicate()[0] |
|
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
99 return os.path.join(root, '.hg', 'patches') |
|
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
100 |
|
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
101 def _patch_command(self, *command): |
|
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
102 """perform a command in the patch repository""" |
|
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
103 _oldpwd = os.getcwd() |
|
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
104 os.chdir(self._patch_repo()) |
|
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
105 call(command) |
|
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
106 os.chdir(_oldpwd) |
| 0 | 107 |
| 108 def main(args=sys.argv[1:]): | |
| 109 parser = CommandParser(HQ) | |
| 110 options, args = parser.parse_args(args) | |
| 111 parser.invoke(args) | |
| 112 | |
| 113 if __name__ == '__main__': | |
| 114 main() |
