forked from Nginlion/git-in-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.py
More file actions
executable file
·258 lines (228 loc) · 7.95 KB
/
git.py
File metadata and controls
executable file
·258 lines (228 loc) · 7.95 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on Jun 8, 2014
@author: lzrak47
'''
import argparse
import sys
from command import Command
class Parser(object):
'''
parse args from command line.
'''
def __init__(self, argv):
self.argv = argv
self.commands = {
'init' : {
'func' : self._init,
'help' : 'Create an empty Git repository or reinitialize an existing one',
'args' : [
{
'name' : ['directory'],
'properties' :
{
'help' : 'Directory of the git repository',
'nargs' : '?',
'default' : './',
},
},
]
},
'add' : {
'func' : self._add,
'help' : 'Add file contents to the index',
'args' : [
{
'name' : ['file'],
'properties' :
{
'help' : 'Files to add content from',
},
},
]
},
'rm' : {
'func' : self._rm,
'help' : 'Remove files from the working tree and from the index',
'args' : [
{
'name' : ['file'],
'properties' :
{
'help' : 'Files to remove',
},
},
{
'name' : ['--cached'],
'properties' :
{
'help' : 'Remove files only from the index',
'action' : 'store_true',
},
},
]
},
'commit' : {
'func' : self._commit,
'help' : 'Record changes to the repository',
'args' : [
{
'name' : ['-m', '--message'],
'properties' :
{
'help' : 'Use the given msg as the commit message',
'dest' : 'msg',
}
}
],
},
'log' : {
'func' : self._log,
'help' : 'Show commit logs',
'args' : [
{
'name' : ['-n'],
'properties' :
{
'help' : 'Limit the number of commits to output',
'nargs' : '?',
'type' : int,
'dest' : 'num',
'default' : float('infinity'),
}
},
],
},
'status' : {
'func' : self._status,
'help' : 'Show the working tree status',
'args' : [],
},
'branch' : {
'func' : self._branch,
'help' : 'List, create, or delete branches',
'args' : [
{
'name' : ['name'],
'properties' :
{
'help' : 'The name of the branch to create or delete',
'nargs' : '?',
'default' : '',
}
},
{
'name' : ['-d'],
'properties' :
{
'help' : 'Delete a branch.',
'action' : 'store_true',
'dest' : 'is_deleted',
}
}
],
},
'reset' : {
'func' : self._reset,
'help' : 'Reset current HEAD to the specified state',
'args' : [
{
'name' : ['commit_sha1'],
'properties' :
{
'help' : 'Commit to reset',
}
},
{
'name' : ['--soft'],
'properties' :
{
'help' : 'Does not touch the index file or the working tree at all',
'action' : 'store_true',
}
},
{
'name' : ['--hard'],
'properties' :
{
'help' : 'Resets the index and working tree',
'action' : 'store_true',
}
},
],
},
'checkout' : {
'func' : self._checkout,
'help' : 'Checkout a branch to the working tree',
'args' : [
{
'name' : ['branch'],
'properties' :
{
'help' : 'Branch to checkout',
}
},
],
},
'diff' : {
'func' : self._diff,
'help' : 'Show changes between commits, commit and working tree, etc',
'args' : [
{
'name' : ['--cached'],
'properties' :
{
'help' : 'Show changes between and the index and the head tree',
'action' : 'store_true',
}
},
],
},
'push' : {
'func' : self._push,
'help' : 'Update remote refs along with associated objects',
'args' : [],
},
'clone' : {
'func' : self._clone,
'help' : 'Clone a repository into a new directory',
'args' : [],
},
}
def _init(self, args):
Command.cmd_init(workspace=args.directory)
def _add(self, args):
Command.cmd_add(args.file)
def _rm(self, args):
Command.cmd_rm(args.file, args.cached)
def _commit(self, args):
Command.cmd_commit(args.msg)
def _log(self, args):
Command.cmd_log(args.num)
def _status(self, args):
Command.cmd_status()
def _branch(self, args):
Command.cmd_branch(args.name, args.is_deleted)
def _reset(self, args):
Command.cmd_reset(args.commit_sha1, is_soft=args.soft, is_hard=args.hard)
def _checkout(self, args):
Command.cmd_checkout(args.branch)
def _diff(self, args):
Command.cmd_diff(args.cached)
def _push(self, args):
pass
def _clone(self, args):
pass
def parse(self):
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
for name, detail in self.commands.iteritems():
subparser = subparsers.add_parser(name, help=detail['help'])
for arg in detail['args']:
subparser.add_argument(*arg['name'], **arg['properties'])
args_res = parser.parse_args()
self.commands[sys.argv[1]]['func'](args_res)
if __name__ == '__main__':
p = Parser(sys.argv)
p.parse()