Skip to content

Commit 167ac02

Browse files
committed
options
1 parent c5e1a91 commit 167ac02

File tree

2 files changed

+79
-19
lines changed

2 files changed

+79
-19
lines changed

git-incrypt

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ plugin.decryptdata.argtypes = [
4444
ctypes.c_void_p, ctypes.POINTER(ctypes.c_size_t)]
4545
plugin.encryptrefname.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
4646
plugin.decryptrefname.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
47+
plugin.set_option.argtypes = [ctypes.c_char_p, ctypes.c_size_t, ctypes.c_char_p]
4748

4849
if not hasattr(pygit2.enums, 'FileMode'):
4950
class FileMode(enum.IntFlag):
@@ -144,12 +145,7 @@ class CryptRepo:
144145

145146
def __init__(self, clearname, url, init=None, forcetrust=False):
146147
assert clearname, 'This does not work yet outside a git repository'
147-
self.settings = {
148-
'atomic': False,
149-
'progress': True,
150-
'verbosity': 1,
151-
'followtags': False
152-
}
148+
plugin.globalinit()
153149
hashstr = hashlib.sha1(url.encode('utf-8')).hexdigest()
154150
self.prefix = f'refs/incrypt/{hashstr}/'
155151
self.url = url
@@ -195,7 +191,7 @@ class CryptRepo:
195191
'iterate over iterable and show progress'
196192
def update():
197193
'update progress'
198-
if self.settings['progress'] and total:
194+
if plugin.getprogress() and total:
199195
nonlocal progress
200196
progress += 1
201197
sys.stderr.write(f'\rremote: {what}'
@@ -208,7 +204,7 @@ class CryptRepo:
208204
update()
209205
function(i)
210206
update()
211-
if self.settings['progress'] and total:
207+
if plugin.getprogress() and total:
212208
sys.stderr.write(', done.\n')
213209
sys.stderr.flush()
214210

@@ -237,8 +233,8 @@ class CryptRepo:
237233
def _fetch(self, pattern):
238234
subprocess.run(
239235
['git', 'fetch',
240-
CryptRepo.verbosityflags[self.settings['verbosity']],
241-
'--progress' if self.settings['progress'] else '--no-progress',
236+
CryptRepo.verbosityflags[plugin.getverbosity()],
237+
'--progress' if plugin.getprogress() else '--no-progress',
242238
'--no-write-fetch-head', '-p', self.url,
243239
f'+refs/heads/{pattern}:{self.prefix}1/{pattern}'],
244240
cwd=self.repo.path, check=True, stdout=sys.stderr)
@@ -377,16 +373,16 @@ class CryptRepo:
377373
resdict = {}
378374
for line in subprocess.run(
379375
['git', 'push'] +
380-
([CryptRepo.verbosityflags[self.settings['verbosity']]]
381-
if self.settings['verbosity'] > 1 else []) +
382-
['--progress' if self.settings['progress']
376+
([CryptRepo.verbosityflags[plugin.getverbosity()]]
377+
if plugin.getverbosity() > 1 else []) +
378+
['--progress' if plugin.getprogress()
383379
else '--no-progress', '--porcelain'] +
384-
(['--atomic'] if self.settings['atomic'] else []) +
380+
(['--atomic'] if plugin.getatomic() else []) +
385381
[self.url, '+' + self.prefix + '1/_:' + MetaData.REFNAME] +
386382
[r[3] for r in xrefs],
387383
cwd=self.repo.path, check=False, text=True,
388384
stdout=subprocess.PIPE,
389-
stderr=sys.stderr if self.settings['verbosity'] > 1
385+
stderr=sys.stderr if plugin.getverbosity() > 1
390386
else subprocess.DEVNULL
391387
).stdout.split('\n'):
392388
e = line.split('\t', 2)
@@ -442,10 +438,7 @@ def remotehelperloop():
442438
elif command[0] == 'push':
443439
pushcommand(line)
444440
elif command[0] == 'option':
445-
if command[1] in ['atomic', 'progress', 'verbosity', 'followtags']:
446-
crypt.settings[command[1]] = command[2] == 'true' \
447-
if isinstance(crypt.settings[command[1]], bool) \
448-
else int(command[2])
441+
if plugin.set_option(command[1].encode('utf-8'), len(command[1].encode('utf-8')), command[2].encode('utf-8')):
449442
sys.stdout.write('ok\n')
450443
else:
451444
sys.stdout.write('unsupported\n')

incrypt-plugin.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
#include "git-compat-util.h"
1010
#include "hash.h"
1111

12+
void globalinit(void);
13+
int set_option(const char *name, size_t namelen, const char *value);
14+
int getverbosity(void);
15+
int getprogress(void);
16+
int getatomic(void);
1217
void setcryptkey(const unsigned char* k);
1318
unsigned char* encryptdata(const unsigned char* input, size_t inputlen,
1419
unsigned char* output, size_t* outputlen);
@@ -17,6 +22,68 @@ unsigned char* decryptdata(const unsigned char* input, size_t inputlen,
1722
char* encryptrefname(const char* input, char* output);
1823
char* decryptrefname(const char* input, char* output);
1924

25+
struct options {
26+
int verbosity;
27+
unsigned progress : 1,
28+
atomic : 1;
29+
};
30+
static struct options options;
31+
32+
void globalinit(void) {
33+
options.verbosity = 1;
34+
options.progress = !!isatty(2);
35+
options.atomic = 0;
36+
}
37+
38+
/*static*/ int set_option(const char *name, size_t namelen, const char *value)
39+
{
40+
if (!strncmp(name, "verbosity", namelen)) {
41+
char *end;
42+
int v = strtol(value, &end, 10);
43+
if (value == end || *end)
44+
return -1;
45+
options.verbosity = v;
46+
return 0;
47+
}
48+
else if (!strncmp(name, "progress", namelen)) {
49+
if (!strcmp(value, "true"))
50+
options.progress = 1;
51+
else if (!strcmp(value, "false"))
52+
options.progress = 0;
53+
else
54+
return -1;
55+
return 0;
56+
}
57+
else if (!strncmp(name, "followtags", namelen)) {
58+
return 0;
59+
} else if (!strncmp(name, "atomic", namelen)) {
60+
if (!strcmp(value, "true"))
61+
options.atomic = 1;
62+
else if (!strcmp(value, "false"))
63+
options.atomic = 0;
64+
else
65+
return -1;
66+
return 0;
67+
} else {
68+
return 1 /* unsupported */;
69+
}
70+
}
71+
72+
int getverbosity(void)
73+
{
74+
return options.verbosity;
75+
}
76+
77+
int getprogress(void)
78+
{
79+
return options.progress;
80+
}
81+
82+
int getatomic(void)
83+
{
84+
return options.atomic;
85+
}
86+
2087
static unsigned char key[48];
2188

2289
void setcryptkey(const unsigned char* k) {

0 commit comments

Comments
 (0)