-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitopt.py
More file actions
executable file
·202 lines (158 loc) · 5.36 KB
/
gitopt.py
File metadata and controls
executable file
·202 lines (158 loc) · 5.36 KB
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
#!/usr/bin/env python
# coding: utf-8
import copy
import logging
logger = logging.getLogger(__name__)
class GitOpt(object):
"""
GitOpt parses and builds git command line arguments.
Attributes:
cmds: parsed command, e.g. the cmds of parsed ``git --git-dir=/foo fetch origin`` is ``['fetch', 'origin']``.
opt: parsed options.
informative_cmds: defines the git options that is a query-like command,
such as ``--version`` or ``--help``.
additional: GitOpt is able to parse user defined options::
o = GitOpt().parse_args(['--foo', 'fetch'], additional=['--foo'])
o.additional
# {'--foo': '--foo'}
"""
# informative options just query for some info instead of doing anything.
informative_opts = (
"--version",
"--help",
"--html-path",
"--info-path",
"--man-path",
"--exec-path",
)
def __init__(self):
self.opt = {
"startpath": [],
"confkv": [],
"paging": None,
"no_replace_objects": False,
"bare": False,
"git_dir": None,
"work_tree": None,
"namespace": None,
"super_prefix": None,
"exec_path": None,
}
self.informative_cmds = {}
self.additional = {}
self.cmds = []
def update(self, d):
"""
update ``opt`` with a dictionary ``d``.
Returns:
self
"""
for k, v in d.items():
self.opt[k] = v
return self
def clone(self):
"""
clone a GitOpt object so that the returned object share nothing with the original.
Returns:
GitOpt: a same and standalone object.
"""
o = GitOpt()
o.opt = copy.deepcopy(self.opt)
o.informative_cmds = copy.deepcopy(self.informative_cmds)
o.additional = copy.deepcopy(self.additional)
return o
def parse_args(self, args, additional=None):
"""
Parse a command line input(without the "git").
Additional user defined arguments can be specified.
Returns:
self
"""
while len(args) > 0:
arg = args.pop(0)
if arg in self.informative_opts:
self.informative_cmds[arg] = arg
continue
if arg == "-C":
self.opt["startpath"].append(args.pop(0))
continue
if arg == "-c":
self.opt["confkv"].append(args.pop(0))
continue
if arg.startswith("--exec-path="):
self.opt["exec_path"] = arg.split("=", 1)[1]
continue
if arg in ("-p", "--paginate"):
self.opt["paging"] = True
continue
if arg == "--no-pager":
self.opt["paging"] = False
continue
if arg == "--no-replace-objects":
# TODO
self.opt["no_replace_objects"] = True
continue
if arg == "--bare":
# TODO
self.opt["bare"] = True
continue
if arg.startswith("--git-dir="):
self.opt["git_dir"] = arg.split("=", 1)[1]
continue
if arg.startswith("--work-tree="):
self.opt["work_tree"] = arg.split("=", 1)[1]
continue
if arg.startswith("--namespace="):
# TODO
self.opt["namespace"] = arg.split("=", 1)[1]
continue
if arg.startswith("--super-prefix="):
# TODO
self.opt["super_prefix"] = arg.split("=", 1)[1]
continue
if additional is not None:
if arg in additional:
self.additional[arg] = arg
continue
# no match, push back
self.cmds = [arg] + args
break
return self
def to_args(self):
"""
Build git command line argument.
E.g.::
o = GitOpt().parse_args(['--git-dir=/foo', 'fetch'])
o.opt['work_tree'] = '/bar'
o.to_args() # ['--git-dir=/foo', '--work-tree=/bar']
o.cmds # ['fetch']
Returns:
list: of str that can be used in commandline.
"""
o = self.opt
rst = []
for p in o["startpath"]:
rst.append("-C")
rst.append(p)
for kv in o["confkv"]:
rst.append("-c")
rst.append(kv)
if o["exec_path"] is not None:
rst.append("--exec-path=" + o["exec_path"])
if o["paging"] is True:
rst.append("-p")
if o["paging"] is False:
rst.append("--no-pager")
if o["no_replace_objects"] is True:
rst.append("--no-replace-objects")
if o["bare"] is True:
rst.append("--bare")
if o["git_dir"] is not None:
rst.append("--git-dir=" + o["git_dir"])
if o["work_tree"] is not None:
rst.append("--work-tree=" + o["work_tree"])
if o["namespace"] is not None:
rst.append("--namespace=" + o["namespace"])
if o["super_prefix"] is not None:
rst.append("--super-prefix=" + o["super_prefix"])
return rst