Mercurial > hg > hq
annotate hq/main.py @ 11:f7fee857c69a
remove more crap
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 12 Mar 2013 14:43:25 -0700 |
parents | 5ea675cfecd9 |
children | 92484d1db1b2 |
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 """ |
4 mercurial queue extension front-end policy manager | |
5 """ | |
6 | |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
7 # TODO: migrate to http://k0s.org/hg/CommandParser/ |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
8 |
0 | 9 import os |
10 import subprocess | |
11 import sys | |
12 | |
13 from command import CommandParser | |
14 | |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
15 call = subprocess.check_output |
0 | 16 |
17 class HQ(object): | |
18 """ | |
19 mercurial queue extension front-end policy manager | |
20 """ | |
21 | |
11 | 22 def __init__(self, network=True, root=None, binary='hg'): |
0 | 23 """initialize global options""" |
24 # TODO: look at hgrc file | |
1 | 25 # 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
|
26 # XXX ??? |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
27 |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
28 # check for network |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
29 self.network = network |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
30 |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
31 if root is None: |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
32 root = call(['hg', 'root']).strip() |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
33 self.root = root |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
34 |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
35 self.binary = binary |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
36 |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
37 ### subcommands |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
38 |
3 | 39 def clone(self, repo, patch=None, queue=None): |
40 """ | |
41 clone the repository and begin a patch queue | |
42 - path: name of a new patch to initiate | |
43 - queue: name of the remote queue | |
44 """ | |
0 | 45 directory = repo.rsplit('/', 1) |
46 call(['hg', 'clone', repo, directory]) | |
47 os.chdir(directory) | |
48 call(['hg', 'qinit', '-c']) | |
3 | 49 if queue: |
4
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
50 # pull from the given repository |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
51 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
|
52 else: |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
53 # (optionally) setup a new repo |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
54 pass # TODO |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
55 |
0 | 56 if patch: |
4
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
57 # create a new patch |
0 | 58 call(['hg', 'qnew', patch]) |
59 | |
60 def commit(self, message): | |
61 """ | |
62 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
|
63 - message : commit message |
0 | 64 """ |
65 call(['hg', 'qrefresh']) | |
66 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
|
67 if self.network: |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
68 self._patch_command(*['hg', 'push']) |
0 | 69 |
70 def pull(self, repo=None): | |
71 """ | |
72 pull from the root repository | |
73 """ | |
74 # TODO: commit prior to realignment | |
75 call(['hg', 'qpop', '--all']) | |
76 call(['hg', 'pull'] + repo and [repo] or []) | |
77 call(['hg', 'qpush', '--all']) | |
2
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
78 |
3 | 79 def goto(self, patch): |
80 """ | |
5
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
81 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
|
82 - patch: name of patch to go to |
3 | 83 """ |
84 # TODO | |
85 process = subprocess.Popen(['hg', 'qapplied'], stdout=subprocess.PIPE) | |
86 stdout, stderr = process.communicate() | |
87 applied = [ i.strip() for i in stdout.splitlines() | |
88 if i ] | |
89 raise NotImplementedError | |
90 | |
2
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
91 def files(self): |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
92 """ |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
93 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
|
94 """ |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
95 # 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
|
96 _oldcwd = os.getcwd() |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
97 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
|
98 stdout, stderr = process.communicate() |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
99 return stdout |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
100 |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
101 def status(self): |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
102 """ |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
103 display status |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
104 """ |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
105 |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
106 # TODO: once this is on CommandParser, there should be a utility in |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
107 # command parser to allow aliases; e.g. if you do ``st = status`` at |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
108 # the class scope, you can run through the methods and only display |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
109 # one as canon but allow aliases as such. |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
110 # Alternatively, you can allow any short non-ambiguous abbreviation. |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
111 |
8 | 112 return '\n'.join([self._call(i).strip() for i in ('root', 'status', 'qseries')]) |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
113 |
9 | 114 def directory(self): |
115 """ | |
116 patch queue directory | |
117 """ | |
118 patch_dir = os.path.join(self.root, '.hg', 'patches') | |
119 return patch_dir if os.path.isdir(patch_dir) else None | |
120 | |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
121 ### internals |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
122 |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
123 def _call(self, *args): |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
124 command = [self.binary] + list(args) |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
125 return call(command, cwd=self.root) |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
126 |
4
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
127 def _patch_repo(self): |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
128 """the location of the patch repository""" |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
129 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
|
130 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
|
131 |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
132 def _patch_command(self, *command): |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
133 """perform a command in the patch repository""" |
10 | 134 call(command, cwd=self.directory()) |
0 | 135 |
11 | 136 |
0 | 137 def main(args=sys.argv[1:]): |
138 parser = CommandParser(HQ) | |
139 options, args = parser.parse_args(args) | |
140 parser.invoke(args) | |
141 | |
142 if __name__ == '__main__': | |
143 main() |