Mercurial > hg > hq
annotate hq/main.py @ 21:aad5083f1117
foo
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 11 Jul 2013 10:53:03 -0700 |
parents | bc17ab3c516e |
children | 7fd599dbef51 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
2 |
0 | 3 """ |
14 | 4 mercurial queue extension front-end |
0 | 5 """ |
6 | |
7 import os | |
8 import subprocess | |
9 import sys | |
10 | |
15 | 11 from commandparser import CommandParser |
0 | 12 |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
13 call = subprocess.check_output |
0 | 14 |
15 class HQ(object): | |
16 """ | |
17 mercurial queue extension front-end policy manager | |
18 """ | |
19 | |
11 | 20 def __init__(self, network=True, root=None, binary='hg'): |
0 | 21 """initialize global options""" |
22 # TODO: look at hgrc file | |
1 | 23 # for [defaults] repository_host |
5
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
24 |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
25 # check for network |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
26 self.network = network |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
27 |
12 | 28 # repository root |
29 self.root = root or call(['hg', 'root']).strip() | |
15 | 30 assert os.path.isdir(self.root), "'%s': not a directory!" |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
31 |
12 | 32 # hg binary |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
33 self.binary = binary |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
34 |
13 | 35 # patch repo; not guaranteed to exit |
14 | 36 self._patch_repo = os.path.join(self.root, '.hg', 'patches') |
13 | 37 |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
38 ### subcommands |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
39 |
3 | 40 def clone(self, repo, patch=None, queue=None): |
41 """ | |
42 clone the repository and begin a patch queue | |
20 | 43 - patch: name of a new patch to initiate |
3 | 44 - queue: name of the remote queue |
45 """ | |
20 | 46 |
47 ### TODO: initialize queue properly | |
48 # hg qinit -c | |
49 # cd `hg root`/ | |
50 # rm -rf patches | |
51 # hg clone ${queue} patches | |
52 # cd patches | |
53 # (fix hgrc) | |
54 | |
0 | 55 directory = repo.rsplit('/', 1) |
56 call(['hg', 'clone', repo, directory]) | |
57 os.chdir(directory) | |
58 call(['hg', 'qinit', '-c']) | |
3 | 59 if queue: |
4
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
60 # pull from the given repository |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
61 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
|
62 else: |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
63 # (optionally) setup a new repo |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
64 pass # TODO |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
65 |
0 | 66 if patch: |
4
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
67 # create a new patch |
0 | 68 call(['hg', 'qnew', patch]) |
69 | |
70 def commit(self, message): | |
71 """ | |
72 commit a patch and push it to the master repository | |
5
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
73 - message : commit message |
0 | 74 """ |
75 call(['hg', 'qrefresh']) | |
76 call(['hg', 'qcommit', '-m', message]) | |
5
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
77 if self.network: |
19 | 78 self._patch_command(*['hg', 'push', '--mq']) |
0 | 79 |
15 | 80 def pull(self, repo=None, mq=True): |
81 """ | |
82 pull from the root repository | |
83 if mq is true, update the patch queue, if versioned | |
84 """ | |
85 # check for outstanding changes | |
17 | 86 output = self._status(self.root) |
87 if output: | |
15 | 88 print "Outstanding changes:" |
89 print output | |
90 raise AssertionError | |
91 | |
92 applied, unapplied = self._series() | |
17 | 93 self._call('qpop', '--all') |
94 self._call(*(['pull'] + (repo and [repo] or []))) | |
15 | 95 # TODO: pull queue repo |
17 | 96 if self._versioned(): |
97 if self.incoming(): | |
21 | 98 print >> sys.stderr, "Incoming changes" |
99 | |
100 output = self._status(self.directory()) | |
101 if output: | |
102 print >> sys.stderr, "Cannot pull %s; outstanding changes" % (self.directory()) | |
103 print output | |
17 | 104 else: |
21 | 105 self._call('pull', '--mq') |
15 | 106 for patch in applied: |
17 | 107 self._call('qpush') |
2
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
108 |
3 | 109 def goto(self, patch): |
110 """ | |
5
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
111 go to a specific patch and apply it |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
112 - patch: name of patch to go to |
3 | 113 """ |
114 # TODO | |
115 process = subprocess.Popen(['hg', 'qapplied'], stdout=subprocess.PIPE) | |
116 stdout, stderr = process.communicate() | |
117 applied = [ i.strip() for i in stdout.splitlines() | |
118 if i ] | |
119 raise NotImplementedError | |
120 | |
2
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
121 def files(self): |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
122 """ |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
123 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
|
124 """ |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
125 # TODO: should only list top-level directories, otherwise it's silly |
15 | 126 process = subprocess.Popen("hg qdiff | grep '^+++ ' | sed 's/+++ b\///'", stdout=subprocess.PIPE, cwd=self._root) |
2
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
127 stdout, stderr = process.communicate() |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
128 return stdout |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
129 |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
130 def status(self): |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
131 """ |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
132 display status |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
133 """ |
8 | 134 return '\n'.join([self._call(i).strip() for i in ('root', 'status', 'qseries')]) |
16 | 135 st = status |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
136 |
9 | 137 def directory(self): |
13 | 138 """patch queue directory""" |
139 if os.path.isdir(self._patch_repo): | |
140 return self._patch_repo | |
9 | 141 |
15 | 142 def incoming(self): |
143 """are there incoming changes to the patch queue""" | |
144 if not self._versioned(): | |
145 return False | |
146 try: | |
147 call([self.binary, 'incoming'], cwd=self.directory()) | |
148 return True | |
149 except subprocess.CalledProcessError: | |
150 return False | |
151 | |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
152 ### internals |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
153 |
15 | 154 def _call(self, *args, **kwargs): |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
155 command = [self.binary] + list(args) |
15 | 156 kwargs.setdefault('cwd', self.root) |
16 | 157 return call(command, **kwargs) |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
158 |
15 | 159 def _patch_command(self, *command, **kwargs): |
4
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
160 """perform a command in the patch repository""" |
18 | 161 kwargs.setdefault('cwd', self.directory()) |
16 | 162 return call(command) |
15 | 163 |
164 def _versioned(self): | |
165 """is the patch queue versioned?""" | |
166 return os.path.isdir(os.path.join(self.directory(), '.hg')) | |
0 | 167 |
15 | 168 def _series(self): |
169 """returns a 2-tuple of applied, unapplied""" | |
17 | 170 lines = [line.strip() |
171 for line in self._call('qseries').strip().splitlines()] | |
15 | 172 applied = [] |
173 unapplied = [] | |
174 for line in lines: | |
17 | 175 |
176 try: | |
177 index, status, name = line.split() | |
178 except: | |
179 print line | |
180 raise | |
181 | |
15 | 182 if status == 'A': |
183 applied.append(name) | |
184 else: | |
185 assert status == 'U' | |
186 unapplied.append(name) | |
187 return applied, unapplied | |
11 | 188 |
17 | 189 def _status(self, repo): |
190 """get the status of a repo; if clean, return None, else | |
191 return the output of hg st""" | |
192 | |
193 output = call([self.binary, 'st'], cwd=repo).strip() | |
194 lines = [line for line in output.splitlines() | |
195 if not line.startswith('?')] | |
196 if lines: | |
197 return output | |
198 | |
199 | |
200 | |
0 | 201 def main(args=sys.argv[1:]): |
202 parser = CommandParser(HQ) | |
203 options, args = parser.parse_args(args) | |
204 parser.invoke(args) | |
205 | |
206 if __name__ == '__main__': | |
207 main() |