Skip to content

Commit b63e4d5

Browse files
committed
port some more global variables
1 parent adac90c commit b63e4d5

2 files changed

Lines changed: 43 additions & 34 deletions

File tree

git-incrypt

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ plugin.decryptrefname.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
4646
plugin.set_option.argtypes = [ctypes.c_char_p, ctypes.c_size_t,
4747
ctypes.c_char_p]
4848
plugin.geturl.restype = ctypes.c_char_p
49+
plugin.getprefix.restype = ctypes.c_char_p
4950
plugin.hashdata.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_void_p]
50-
plugin.hashdatahex.argtypes = [ctypes.c_void_p, ctypes.c_size_t]
51+
plugin.hashdatahex.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_char_p]
5152
plugin.hashdatahex.restype = ctypes.c_char_p
5253
plugin.globalinit.argtypes = [ctypes.c_char_p]
5354

@@ -72,14 +73,6 @@ if not hasattr(pygit2.enums, 'ObjectType'):
7273
TAG = pygit2._pygit2.GIT_OBJ_TAG
7374
pygit2.enums.ObjectType = ObjectType
7475

75-
CRYPTREADME = '''# 401 Unauthorized
76-
77-
This is an encrypted git repository. You can clone it, but you will not be
78-
able to see the contents of the commits. If you have the right key, you can
79-
decrypt the repository using
80-
[git-incrypt](https://github.com/schiele/git-incrypt).
81-
'''
82-
8376

8477
def encryptdata(data: bytes, key: bytes) -> (bytes, bytes):
8578
'encrypt raw data'
@@ -132,7 +125,9 @@ def sha1(data):
132125

133126
def sha1hex(data):
134127
'sha1 hash of data'
135-
return plugin.hashdatahex(data, len(data)).decode('utf-8')
128+
output = ctypes.create_string_buffer(41)
129+
poutput = ctypes.c_char_p(ctypes.addressof(output))
130+
return plugin.hashdatahex(data, len(data), poutput).decode('utf-8')
136131

137132

138133
# pylint: disable=pointless-string-statement
@@ -163,18 +158,17 @@ class CryptRepo:
163158
def __init__(self, clearname, url, init=None, forcetrust=False):
164159
assert clearname, 'This does not work yet outside a git repository'
165160
plugin.globalinit(url.encode('utf-8'))
166-
self.prefix = f'refs/incrypt/{sha1hex(plugin.geturl())}/'
167161
if init:
168162
self.repo = pygit2.init_repository(clearname, bare=True)
169163
template = self._mktemplate(init.name, init.email,
170164
init.date, init.m)
171-
self.meta = MetaData(self.repo, self.prefix + '1/').init(
165+
self.meta = MetaData(self.repo).init(
172166
init.keys, template, 'refs/heads/master')
173167
self.trust(force=True, sign=True)
174168
else:
175169
self.repo = pygit2.Repository(clearname)
176170
self._fetch('_')
177-
self.meta = MetaData(self.repo, self.prefix + '1/').read()
171+
self.meta = MetaData(self.repo).read()
178172
if forcetrust:
179173
self.trust(force=forcetrust)
180174

@@ -250,7 +244,7 @@ class CryptRepo:
250244
CryptRepo.verbosityflags[plugin.getverbosity()],
251245
'--progress' if plugin.getprogress() else '--no-progress',
252246
'--no-write-fetch-head', '-p', plugin.geturl().decode('utf-8'),
253-
f'+refs/heads/{pattern}:{self.prefix}1/{pattern}'],
247+
f'+refs/heads/{pattern}:{plugin.getprefix().decode("utf-8")}1/{pattern}'],
254248
cwd=self.repo.path, check=True, stdout=sys.stderr)
255249

256250
def trust(self, force=False, sign=False):
@@ -260,7 +254,7 @@ class CryptRepo:
260254
else:
261255
try:
262256
expectedhash = self.repo.revparse_single(
263-
self.prefix +
257+
plugin.getprefix().decode('utf-8') +
264258
'keyhash').tree['_'].read_raw().decode('utf-8')
265259
except KeyError:
266260
expectedhash = None
@@ -279,7 +273,8 @@ class CryptRepo:
279273
colid = collector.write()
280274
commit = self.meta.secretcommit(
281275
colid, [])
282-
self.repo.create_reference(self.prefix + 'keyhash', commit, force=True)
276+
self.repo.create_reference(
277+
plugin.getprefix().decode('utf-8') + 'keyhash', commit, force=True)
283278

284279
def getrefs(self):
285280
'list all cleartext references'
@@ -291,11 +286,11 @@ class CryptRepo:
291286
self.meta.read()
292287
self.trust()
293288
# [ dec(crypt) , prefdec(crypt), crypt ]
294-
refs = [[r[0], self.prefix + '0/' + r[0],
289+
refs = [[r[0], plugin.getprefix().decode('utf-8') + '0/' + r[0],
295290
self.repo.revparse_single(r[1])]
296291
for r in [[decryptrefname(r, self.meta.key), r] for r in
297-
filter(lambda r: len(r) > len(self.prefix)+3 and
298-
r.startswith(self.prefix + '1/'),
292+
filter(lambda r: len(r) > len(plugin.getprefix().decode('utf-8'))+3 and
293+
r.startswith(plugin.getprefix().decode('utf-8') + '1/'),
299294
self.repo.references)]]
300295
cryptmap = self.meta.readmap(reverse=True)
301296
self._progress_for(
@@ -310,9 +305,9 @@ class CryptRepo:
310305
expected = [r[1] for r in refs]
311306
result = [['HEAD', f'@{self.meta.defaultbranch}']]
312307
for r in self.repo.references:
313-
if r.startswith(self.prefix + '0/'):
308+
if r.startswith(plugin.getprefix().decode('utf-8') + '0/'):
314309
if r in expected:
315-
result.append([r[len(self.prefix)+2:],
310+
result.append([r[len(plugin.getprefix().decode('utf-8'))+2:],
316311
self.repo.lookup_reference(r).target])
317312
else:
318313
self.repo.references.delete(r)
@@ -358,7 +353,7 @@ class CryptRepo:
358353
else [cryptmap[str(obj.target)]])
359354

360355
xrefs = [[r[0], r[1], r[3], ('+' if r[2] else '') +
361-
(self.prefix + '1/' + r[3] if r[0] else '') +
356+
(plugin.getprefix().decode('utf-8') + '1/' + r[3] if r[0] else '') +
362357
':refs/heads/' + r[3],
363358
self.repo.revparse_single(r[0]) if r[0] else None]
364359
for r in [[r[0], r[1], r[2],
@@ -377,11 +372,11 @@ class CryptRepo:
377372
for r in xrefs:
378373
if r[4]:
379374
self.repo.create_reference(
380-
self.prefix + '1/' + r[2], cryptmap[str(r[4].id)],
375+
plugin.getprefix().decode('utf-8') + '1/' + r[2], cryptmap[str(r[4].id)],
381376
force=True)
382377
else:
383378
try:
384-
self.repo.references.delete(self.prefix + '1/' + r[2])
379+
self.repo.references.delete(plugin.getprefix().decode('utf-8') + '1/' + r[2])
385380
except KeyError:
386381
pass
387382
resdict = {}
@@ -393,7 +388,7 @@ class CryptRepo:
393388
else '--no-progress', '--porcelain'] +
394389
(['--atomic'] if plugin.getatomic() else []) +
395390
[plugin.geturl().decode('utf-8'), '+' +
396-
self.prefix + '1/_:' + MetaData.REFNAME] +
391+
plugin.getprefix().decode('utf-8') + '1/_:' + MetaData.REFNAME] +
397392
[r[3] for r in xrefs],
398393
cwd=self.repo.path, check=False, text=True,
399394
stdout=subprocess.PIPE,
@@ -470,9 +465,8 @@ class MetaData:
470465
KEYVER = b'AES-256-CBC+IV'
471466
REFNAME = 'refs/heads/_'
472467

473-
def __init__(self, repo, prefix):
468+
def __init__(self, repo):
474469
self.repo = repo
475-
self.prefix = prefix
476470
self.files = None
477471
self.key = None
478472
self.keyhash = None
@@ -518,7 +512,7 @@ class MetaData:
518512
def read(self):
519513
'read the metadata'
520514
self.files = {}
521-
tree = self.repo.revparse_single(self.prefix + '_').tree
515+
tree = self.repo.revparse_single(plugin.getprefix().decode('utf-8') + '1/_').tree
522516
obj = tree['ver']
523517
self.files['ver'] = obj.id
524518
data = obj.read_raw()
@@ -610,16 +604,16 @@ class MetaData:
610604
mapfile = self.repo.create_blob(encryptdata(
611605
sha1(rawdata) + rawdata, self.key))
612606
collector.insert('map', mapfile, pygit2.enums.FileMode.BLOB)
613-
readmefile = self.repo.create_blob(CRYPTREADME.encode('utf-8'))
607+
readmefile = self.repo.create_blob(ctypes.c_char_p.in_dll(plugin, 'CRYPTREADME').value)
614608
collector.insert('README.md', readmefile, pygit2.enums.FileMode.BLOB)
615609
colid = collector.write()
616610
commit = self.secretcommit(colid, [])
617-
self.repo.create_reference(self.prefix + '_', commit, force=True)
611+
self.repo.create_reference(plugin.getprefix().decode('utf-8') + '1/_', commit, force=True)
618612
return self
619613

620614
def readmap(self, reverse=False):
621615
'read the mapping table'
622-
tree = self.repo.revparse_single(self.prefix + '_').tree
616+
tree = self.repo.revparse_single(plugin.getprefix().decode('utf-8') + '1/_').tree
623617
o = 20 if reverse else 0
624618
processed = {}
625619
rawdata = decryptdata(tree['map'].read_raw(), self.key)

incrypt-plugin.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ int getverbosity(void);
1818
int getprogress(void);
1919
int getatomic(void);
2020
const char* geturl(void);
21+
const char* getprefix(void);
2122
void setcryptkey(const unsigned char* k);
2223
unsigned char* encryptdata(const unsigned char* input, size_t inputlen,
2324
unsigned char* output, size_t* outputlen);
@@ -27,7 +28,14 @@ char* encryptrefname(const char* input, char* output);
2728
char* decryptrefname(const char* input, char* output);
2829
unsigned char* hashdata(const unsigned char* input, size_t inputlen,
2930
unsigned char* output);
30-
char* hashdatahex(const unsigned char* input, size_t inputlen);
31+
char* hashdatahex(const unsigned char* input, size_t inputlen,
32+
char* output);
33+
34+
/*static*/ const char* CRYPTREADME = "# 401 Unauthorized\n\n"
35+
"This is an encrypted git repository. You can clone it, but you will not be\n"
36+
"able to see the contents of the commits. If you have the right key, you can\n"
37+
"decrypt the repository using\n"
38+
"[git-incrypt](https://github.com/schiele/git-incrypt).\n";
3139

3240
struct options {
3341
int verbosity;
@@ -37,6 +45,7 @@ struct options {
3745
static struct options options;
3846

3947
static char* url = NULL;
48+
static char prefix[] = "refs/incrypt/......................................../";
4049

4150
void globalinit(const char* url_arg) {
4251
size_t urllen = strlen(url_arg);
@@ -45,6 +54,8 @@ void globalinit(const char* url_arg) {
4554
options.atomic = 0;
4655
url = malloc(urllen + 1);
4756
memcpy(url, url_arg, urllen + 1);
57+
hashdatahex((const unsigned char*)url, urllen, prefix + 13);
58+
prefix[13 + GIT_SHA1_HEXSZ] = '/';
4859
}
4960

5061
/*static*/ int set_option(const char *name, size_t namelen, const char *value)
@@ -100,6 +111,10 @@ const char* geturl(void) {
100111
return url;
101112
}
102113

114+
const char* getprefix(void) {
115+
return prefix;
116+
}
117+
103118
static unsigned char key[48];
104119

105120
void setcryptkey(const unsigned char* k) {
@@ -299,10 +314,10 @@ unsigned char* hashdata(const unsigned char* input, size_t inputlen,
299314
return output;
300315
}
301316

302-
char* hashdatahex(const unsigned char* input, size_t inputlen) {
317+
char* hashdatahex(const unsigned char* input, size_t inputlen, char* output) {
303318
unsigned char hash[GIT_SHA1_RAWSZ];
304319
hashdata(input, inputlen, hash);
305-
return hash_to_hex_algop(hash, &hash_algos[GIT_HASH_SHA1]);
320+
return hash_to_hex_algop_r(output, hash, &hash_algos[GIT_HASH_SHA1]);
306321
}
307322

308323
int cmd_main(int argc, const char** argv) {

0 commit comments

Comments
 (0)