-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
281 lines (197 loc) · 7.8 KB
/
cli.py
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import argparse
import os
import subprocess
import sys
import textwrap
import base
import data
import diff
import remote
def main():
with data.change_git_dir('.'):
args = parse_args()
args.func(args)
def parse_args():
parser = argparse.ArgumentParser()
commands = parser.add_subparsers(dest='command')
commands.required = True
get_oid = base.get_oid
init_parser = commands.add_parser('init')
init_parser.set_defaults(func=init)
hash_object_parser = commands.add_parser('hash-object')
hash_object_parser.set_defaults(func=hash_object)
hash_object_parser.add_argument('file')
cat_file_parser = commands.add_parser('cat-file')
cat_file_parser.set_defaults(func=cat_file)
cat_file_parser.add_argument('object', type=get_oid)
write_tree_parser = commands.add_parser('write-tree')
write_tree_parser.set_defaults(func=write_tree)
read_tree_parser = commands.add_parser('read-tree')
read_tree_parser.set_defaults(func=read_tree)
read_tree_parser.add_argument('tree', type=get_oid)
commit_parser = commands.add_parser('commit')
commit_parser.set_defaults(func=commit)
commit_parser.add_argument('-m', '--message', required=True)
log_parser = commands.add_parser('log')
log_parser.set_defaults(func=log)
log_parser.add_argument('oid', default='@', type=get_oid, nargs='?')
show_parser = commands.add_parser('show')
show_parser.set_defaults(func=show)
show_parser.add_argument('oid', default='@', type=get_oid, nargs='?')
diff_parser = commands.add_parser('diff')
diff_parser.set_defaults(func=_diff)
diff_parser.add_argument('--cached', action='store_true')
diff_parser.add_argument('commit', nargs='?')
checkout_parser = commands.add_parser('checkout')
checkout_parser.set_defaults(func=checkout)
checkout_parser.add_argument('commit')
tag_parser = commands.add_parser('tag')
tag_parser.set_defaults(func=tag)
tag_parser.add_argument('name')
tag_parser.add_argument('oid', default='@', type=get_oid, nargs='?')
branch_parser = commands.add_parser('branch')
branch_parser.set_defaults(func=branch)
branch_parser.add_argument('name', nargs='?')
branch_parser.add_argument('start_point', default='@', type=get_oid, nargs='?')
k_parser = commands.add_parser('k')
k_parser.set_defaults(func=k)
status_parser = commands.add_parser('status')
status_parser.set_defaults(func=status)
reset_parser = commands.add_parser('reset')
reset_parser.set_defaults(func=reset)
reset_parser.add_argument('commit', type=get_oid)
merge_parser = commands.add_parser('merge')
merge_parser.set_defaults(func=merge)
merge_parser.add_argument('commit', type=get_oid)
merge_base_parser = commands.add_parser('merge-base')
merge_base_parser.set_defaults(func=merge_base)
merge_base_parser.add_argument('commit1', type=get_oid)
merge_base_parser.add_argument('commit2', type=get_oid)
fetch_parser = commands.add_parser('fetch')
fetch_parser.set_defaults(func=fetch)
fetch_parser.add_argument('remote')
push_parser = commands.add_parser('push')
push_parser.set_defaults(func=push)
push_parser.add_argument('remote')
push_parser.add_argument('branch')
add_parser = commands.add_parser('add')
add_parser.set_defaults(func=add)
add_parser.add_argument('files', nargs='+')
return parser.parse_args()
def init(args):
base.init()
print(f'Initialized empty ugit repository in {os.getcwd()}/{data.GIT_DIR}')
def hash_object(args):
with open(args.file, 'rb') as f:
print(data.hash_object(f.read()))
def cat_file(args):
sys.stdout.flush()
sys.stdout.buffer.write(data.get_object(args.object, expected=None))
def write_tree(args):
print(base.write_tree())
def read_tree(args):
base.read_tree(args.tree)
def commit(args):
print(base.commit(args.message))
def _print_commit(oid, commit_contents, refs=None):
refs_str = f' ({", ".join(refs)})' if refs else ''
print(f'commit {oid}{refs_str}\n')
print(textwrap.indent(commit_contents.message, '\t'))
print('')
def log(args):
refs = {}
for ref_name, ref in data.iter_refs():
refs.setdefault(ref.value, []).append(ref_name)
for oid in base.iter_commits_and_parents({args.oid}):
commit_contents = base.get_commit(oid)
_print_commit(oid, commit_contents, refs.get(oid))
def show(args):
if not args.oid:
return
commit_contents = base.get_commit(args.oid)
parent_tree = None
if commit_contents.parents:
parent_tree = base.get_commit(commit_contents.parents[0]).tree
_print_commit(args.oid, commit_contents)
result = diff.diff_trees(base.get_tree(parent_tree), base.get_tree(commit_contents.tree))
sys.stdout.flush()
sys.stdout.buffer.write(result)
def _diff(args):
oid = args.commit and base.get_oid(args.commit)
if args.commit:
# If a commit was provided explicitly, diff from it
tree_from = base.get_tree(oid and base.get_commit(oid).tree)
if args.cached:
tree_to = base.get_index_tree()
if not args.commit:
# If no commit was provided, diff from HEAD
oid = base.get_oid('@')
tree_from = base.get_tree(oid and base.get_commit(oid).tree)
else:
tree_to = base.get_working_tree()
if not args.commit:
# If no commit was provided, diff from index
tree_from = base.get_index_tree()
result = diff.diff_trees(tree_from, tree_to)
sys.stdout.flush()
sys.stdout.buffer.write(result)
def checkout(args):
base.checkout(args.commit)
def tag(args):
base.create_tag(args.name, args.oid)
def branch(args):
if not args.name:
current_branch_name = base.get_branch_name()
for branch_name in base.iter_branch_names():
prefix = '*' if branch_name == current_branch_name else ' '
print(f'{prefix} {branch_name}')
else:
base.create_branch(args.name, args.start_point)
print(f'Branch {args.name} created at {args.start_point[:10]}')
def k(args):
dot = 'digraph commits {\n'
oids = set()
for ref_name, ref in data.iter_refs(deref=False):
dot += f'"{ref_name}" [shape=note]\n'
dot += f'"{ref_name}" -> "{ref.value}"\n'
if not ref.symbolic:
oids.add(ref.value)
for oid in base.iter_commits_and_parents(oids):
commit_contents = base.get_commit(oid)
dot += f'"{oid}" [shape=box style=filled label="{oid[:10]}"]\n'
for parent in commit_contents.parents:
dot += f'"{oid}" -> "{parent}"\n'
dot += '}'
print(dot)
with subprocess.Popen(['dot', '-Tpng', '/dev/stdin'], stdin=subprocess.PIPE) as proc:
proc.communicate(dot.encode())
def status(args):
HEAD = base.get_oid('@')
branch_name = base.get_branch_name()
if branch_name:
print(f'On branch {branch_name}')
else:
print(f'HEAD detached at {HEAD[:10]}')
MERGE_HEAD = data.get_ref('MERGE_HEAD').value
if MERGE_HEAD:
print(f'Merging with {MERGE_HEAD[:10]}')
print('\nChanges to be committed:\n')
HEAD_tree = HEAD and base.get_commit(HEAD).tree
for path, action in diff.iter_changed_files(base.get_tree(HEAD_tree), base.get_index_tree()):
print(f'{action:>12}: {path}')
print('\nChanges not staged for commit:\n')
for path, action in diff.iter_changed_files(base.get_index_tree(), base.get_working_tree()):
print(f'{action:>12}: {path}')
def reset(args):
base.reset(args.commit)
def merge(args):
base.merge(args.commit)
def merge_base(args):
print(base.get_merge_base(args.commit1, args.commit2))
def fetch(args):
remote.fetch(args.remote)
def push(args):
remote.push(args.remote, f'refs/heads/{args.branch}')
def add(args):
base.add(args.files)
main()