Mercurial > hg > hq
annotate hq/main.py @ 17:44ec940c86a6
pull works, i think
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 17 May 2013 04:01:21 -0700 |
parents | b878f4ce93fc |
children | d4e5d09e007c |
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 | |
43 - path: name of a new patch to initiate | |
44 - queue: name of the remote queue | |
45 """ | |
0 | 46 directory = repo.rsplit('/', 1) |
47 call(['hg', 'clone', repo, directory]) | |
48 os.chdir(directory) | |
49 call(['hg', 'qinit', '-c']) | |
3 | 50 if queue: |
4
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
51 # pull from the given repository |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
52 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
|
53 else: |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
54 # (optionally) setup a new repo |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
55 pass # TODO |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
56 |
0 | 57 if patch: |
4
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
58 # create a new patch |
0 | 59 call(['hg', 'qnew', patch]) |
60 | |
61 def commit(self, message): | |
62 """ | |
63 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
|
64 - message : commit message |
0 | 65 """ |
66 call(['hg', 'qrefresh']) | |
67 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
|
68 if self.network: |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
69 self._patch_command(*['hg', 'push']) |
0 | 70 |
15 | 71 def pull(self, repo=None, mq=True): |
72 """ | |
73 pull from the root repository | |
74 if mq is true, update the patch queue, if versioned | |
75 """ | |
76 # check for outstanding changes | |
17 | 77 output = self._status(self.root) |
78 if output: | |
15 | 79 print "Outstanding changes:" |
80 print output | |
81 raise AssertionError | |
82 | |
83 applied, unapplied = self._series() | |
17 | 84 self._call('qpop', '--all') |
85 self._call(*(['pull'] + (repo and [repo] or []))) | |
15 | 86 # TODO: pull queue repo |
17 | 87 if self._versioned(): |
88 if self.incoming(): | |
89 print >> sys.stderr, "Incoming changes, cannot pull" | |
90 # TODO: yes you can if they're compatible | |
91 else: | |
92 output = self._status(self.directory()) | |
93 if output: | |
94 print >> sys.stderr, "Cannot pull %s; outstanding changes" % (self.directory()) | |
95 print output | |
96 else: | |
97 self._call('pull', '--mq') | |
15 | 98 for patch in applied: |
17 | 99 self._call('qpush') |
2
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
100 |
3 | 101 def goto(self, patch): |
102 """ | |
5
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
103 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
|
104 - patch: name of patch to go to |
3 | 105 """ |
106 # TODO | |
107 process = subprocess.Popen(['hg', 'qapplied'], stdout=subprocess.PIPE) | |
108 stdout, stderr = process.communicate() | |
109 applied = [ i.strip() for i in stdout.splitlines() | |
110 if i ] | |
111 raise NotImplementedError | |
112 | |
2
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
113 def files(self): |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
114 """ |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
115 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
|
116 """ |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
117 # TODO: should only list top-level directories, otherwise it's silly |
15 | 118 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
|
119 stdout, stderr = process.communicate() |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
120 return stdout |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
121 |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
122 def status(self): |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
123 """ |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
124 display status |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
125 """ |
8 | 126 return '\n'.join([self._call(i).strip() for i in ('root', 'status', 'qseries')]) |
16 | 127 st = status |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
128 |
9 | 129 def directory(self): |
13 | 130 """patch queue directory""" |
131 if os.path.isdir(self._patch_repo): | |
132 return self._patch_repo | |
9 | 133 |
15 | 134 def incoming(self): |
135 """are there incoming changes to the patch queue""" | |
136 if not self._versioned(): | |
137 return False | |
138 try: | |
139 call([self.binary, 'incoming'], cwd=self.directory()) | |
140 return True | |
141 except subprocess.CalledProcessError: | |
142 return False | |
143 | |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
144 ### internals |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
145 |
15 | 146 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
|
147 command = [self.binary] + list(args) |
15 | 148 kwargs.setdefault('cwd', self.root) |
16 | 149 return call(command, **kwargs) |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
150 |
15 | 151 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
|
152 """perform a command in the patch repository""" |
15 | 153 kwargs.setdefault(cwd=self.directory()) |
16 | 154 return call(command) |
15 | 155 |
156 def _versioned(self): | |
157 """is the patch queue versioned?""" | |
158 return os.path.isdir(os.path.join(self.directory(), '.hg')) | |
0 | 159 |
15 | 160 def _series(self): |
161 """returns a 2-tuple of applied, unapplied""" | |
17 | 162 lines = [line.strip() |
163 for line in self._call('qseries').strip().splitlines()] | |
15 | 164 applied = [] |
165 unapplied = [] | |
166 for line in lines: | |
17 | 167 |
168 try: | |
169 index, status, name = line.split() | |
170 except: | |
171 print line | |
172 raise | |
173 | |
15 | 174 if status == 'A': |
175 applied.append(name) | |
176 else: | |
177 assert status == 'U' | |
178 unapplied.append(name) | |
179 return applied, unapplied | |
11 | 180 |
17 | 181 def _status(self, repo): |
182 """get the status of a repo; if clean, return None, else | |
183 return the output of hg st""" | |
184 | |
185 output = call([self.binary, 'st'], cwd=repo).strip() | |
186 lines = [line for line in output.splitlines() | |
187 if not line.startswith('?')] | |
188 if lines: | |
189 return output | |
190 | |
191 | |
192 | |
0 | 193 def main(args=sys.argv[1:]): |
194 parser = CommandParser(HQ) | |
195 options, args = parser.parse_args(args) | |
196 parser.invoke(args) | |
197 | |
198 if __name__ == '__main__': | |
199 main() |