-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
203 lines (181 loc) · 7.06 KB
/
commands.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
import time
import urllib
import argparse
import enum
import difflib
import operator
import struct
import collections
import configparser
import hashlib
import os
import re
import sys
import zlib
from objects import IndexEntry
from functions import get_remote_master_hash, get_local_master_hash, find_missing_objects, http_request
from functions import write_file, read_object, read_index, get_status, build_lines_data, create_pack, extract_lines
from functions import find_tree_objects, read_tree, write_tree, hash_object, read_file, write_index
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def init(repo):
"""Cria um repositório .git."""
os.mkdir(repo)
os.mkdir(os.path.join(repo, '.git'))
for name in ['objects', 'refs', 'refs/heads']:
os.mkdir(os.path.join(repo, '.git', name))
write_file(os.path.join(repo, '.git', 'HEAD'), b'ref: refs/heads/master')
print('repositório vazio inicializado: {}'.format(repo))
def cat_file(mode, sha1_prefix):
"""Escreve o conteúdo (ou info sobre) objeto a partir do prefix SHA-1 no stdout.
Se o modo é commit, tree ou blob, mostra os bytes do objeto.
Se o modo for size, mostra o tamanho do objeto.
Se o modo for type, mostr ao tipo do objeto.
Se o modo for pretty, mostra uma versão mais user-friendly do objeto.
"""
obj_type, data = read_object(sha1_prefix)
if mode in ['commit', 'tree', 'blob']:
if obj_type != mode:
raise ValueError('esperava um objeto do tipo {}, mas recebeu um do tipo {}'.format(
mode, obj_type))
sys.stdout.buffer.write(data)
elif mode == 'size':
print(len(data))
elif mode == 'type':
print(obj_type)
elif mode == 'pretty':
if obj_type in ['commit', 'blob']:
sys.stdout.buffer.write(data)
elif obj_type == 'tree':
for mode, path, sha1 in read_tree(data=data):
type_str = 'tree' if stat.S_ISDIR(mode) else 'blob'
print('{:06o} {} {}\t{}'.format(mode, type_str, sha1, path))
else:
assert False, 'tipo não tratado {!r}'.format(obj_type)
else:
raise ValueError('modo não esperado {!r}'.format(mode))
def ls_files(details=False):
"""Exibe a lista de arquivos no index incluíndo o SHA-1 e o numero de stage.
Para exibir mais detalhes, utilizar True
"""
for entry in read_index():
if details:
stage = (entry.flags >> 12) & 3
print('{:6o} {} {:}\t{}'.format(
entry.mode, entry.sha1.hex(), stage, entry.path))
else:
print(entry.path)
def status():
"""Mostra o estado do worktree atual."""
changed, new, deleted = get_status()
if changed:
print('Modificados:')
for path in changed:
print(' ', path)
if new:
print('Novos:')
for path in new:
print(f" {bcolors.WARNING} {path} {bcolors.ENDC}")
if deleted:
print('Deletados:')
for path in deleted:
print(f" {bcolors.FAIL} {path} {bcolors.ENDC}")
def diff():
"""Mostra as alterações nos arquivos entre o index e a copia de trabalho."""
changed, _, _ = get_status()
entries_by_path = {e.path: e for e in read_index()}
for i, path in enumerate(changed):
sha1 = entries_by_path[path].sha1.hex()
obj_type, data = read_object(sha1)
assert obj_type == 'blob'
index_lines = data.decode().splitlines()
working_lines = read_file(path).decode().splitlines()
diff_lines = difflib.unified_diff(
index_lines, working_lines,
'{} (index)'.format(path),
'{} (working copy)'.format(path),
lineterm='')
for line in diff_lines:
print(line)
if i < len(changed) - 1:
print('-' * 70)
def add(paths):
"""Adiciona os paths ao .git/index."""
paths = [p.replace('\\', '/') for p in paths]
all_entries = read_index()
entries = [e for e in all_entries if e.path not in paths]
for path in paths:
sha1 = hash_object(read_file(path), 'blob')
st = os.stat(path)
flags = len(path.encode())
assert flags < (1 << 12)
entry = IndexEntry(
int(st.st_ctime), 0, int(st.st_mtime), 0, st.st_dev,
st.st_ino, st.st_mode, st.st_uid, st.st_gid, st.st_size,
bytes.fromhex(sha1), flags, path)
entries.append(entry)
entries.sort(key=operator.attrgetter('path'))
write_index(entries)
def commit(message, author=None):
"""Commita o estado atual do index para a master, com a mensagem
Retorna o hash do commit.
"""
tree = write_tree()
parent = get_local_master_hash()
if author is None:
author = '{} <{}>'.format(
os.environ['GIT_AUTHOR_NAME'], os.environ['GIT_AUTHOR_EMAIL'])
timestamp = int(time.mktime(time.localtime()))
utc_offset = -time.timezone
author_time = '{} {}{:02}{:02}'.format(
timestamp,
'+' if utc_offset > 0 else '-',
abs(utc_offset) // 3600,
(abs(utc_offset) // 60) % 60)
lines = ['tree ' + tree]
if parent:
lines.append('parent ' + parent)
lines.append('author {} {}'.format(author, author_time))
lines.append('committer {} {}'.format(author, author_time))
lines.append('')
lines.append(message)
lines.append('')
data = '\n'.join(lines).encode()
sha1 = hash_object(data, 'commit')
master_path = os.path.join('.git', 'refs', 'heads', 'master')
write_file(master_path, (sha1 + '\n').encode())
print('Commitado para a master: {:7}'.format(sha1))
return sha1
def push(git_url, username=None, password=None):
"""Faz o push para um repositório git."""
if username is None:
username = os.environ['GIT_USERNAME']
if password is None:
password = os.environ['GIT_PASSWORD']
remote_sha1 = get_remote_master_hash(git_url, username, password)
local_sha1 = get_local_master_hash()
missing = find_missing_objects(local_sha1, remote_sha1)
print('updating remote master from {} to {} ({} object{})'.format(
remote_sha1 or 'no commits', local_sha1, len(missing),
'' if len(missing) == 1 else 's'))
lines = ['{} {} refs/heads/master\x00 report-status'.format(
remote_sha1 or ('0' * 40), local_sha1).encode()]
data = build_lines_data(lines) + create_pack(missing)
url = git_url + '/git-receive-pack'
response = http_request(url, username, password, data=data)
lines = extract_lines(response)
print(lines)
assert len(lines) >= 2, \
'expected at least 2 lines, got {}'.format(len(lines))
assert lines[0] == b'unpack ok\n', \
"expected line 1 b'unpack ok', got: {}".format(lines[0])
assert lines[1] == b'ok refs/heads/master\n', \
"expected line 2 b'ok refs/heads/master\n', got: {}".format(lines[1])
return (remote_sha1, missing)