Skip to content

Commit 298a924

Browse files
committed
string refactoring
1 parent 1028ae4 commit 298a924

1 file changed

Lines changed: 22 additions & 16 deletions

File tree

incrypt-plugin.c

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ unsigned char* hashdata(const unsigned char* input, size_t inputlen,
4343
char* hashdatahex(const unsigned char* input, size_t inputlen,
4444
char* output);
4545
void hashdatabuf(struct strbuf* out, struct strbuf* in);
46+
void hashdatabufhex(struct strbuf* out, struct strbuf* in);
4647
void initbare(const char* dir);
4748
char* mktemplate(const char* name, const char* email, const char* date, const char* msg);
4849
void fetchpattern(const char pattern);
@@ -65,19 +66,18 @@ struct options {
6566
};
6667
static struct options options;
6768

68-
static char* url = NULL;
69-
static char prefix[] = "refs/incrypt/......................................../";
69+
static struct strbuf url = STRBUF_INIT;
70+
static struct strbuf prefix = STRBUF_INIT;
7071

7172
void globalinit(const char* dir, const char* url_arg) {
72-
size_t urllen = strlen(url_arg);
7373
chdir(dir);
7474
options.verbosity = 1;
7575
options.progress = !!isatty(2);
7676
options.atomic = 0;
77-
url = malloc(urllen + 1);
78-
memcpy(url, url_arg, urllen + 1);
79-
hashdatahex((const unsigned char*)url, urllen, prefix + 13);
80-
prefix[13 + GIT_SHA1_HEXSZ] = '/';
77+
strbuf_addstr(&url, url_arg);
78+
strbuf_addstr(&prefix, "refs/incrypt/");
79+
hashdatabufhex(&prefix, &url);
80+
strbuf_addch(&prefix, '/');
8181
}
8282

8383
/*static*/ int set_option(const char *name, size_t namelen, const char *value)
@@ -130,11 +130,11 @@ int getatomic(void)
130130
}
131131

132132
const char* geturl(void) {
133-
return url;
133+
return url.buf;
134134
}
135135

136136
const char* getprefix(void) {
137-
return prefix;
137+
return prefix.buf;
138138
}
139139

140140
static unsigned char key[48];
@@ -352,6 +352,12 @@ void hashdatabuf(struct strbuf* out, struct strbuf* in) {
352352
strbuf_add(out, hash, GIT_SHA1_RAWSZ);
353353
}
354354

355+
void hashdatabufhex(struct strbuf* out, struct strbuf* in) {
356+
unsigned char hash[GIT_SHA1_RAWSZ];
357+
hashdata((const unsigned char*)in->buf, in->len, hash);
358+
strbuf_addstr(out, hash_to_hex_algop(hash, &hash_algos[GIT_HASH_SHA1]));
359+
}
360+
355361
void initbare(const char* dir) {
356362
init_db(dir, NULL, NULL, GIT_HASH_UNKNOWN, REF_STORAGE_FORMAT_UNKNOWN, NULL, -1, 0);
357363
}
@@ -374,14 +380,13 @@ static const char* progressflags[2] = {"--no-progress", "--progress"};
374380
void fetchpattern(const char pattern) {
375381
struct child_process cmd = CHILD_PROCESS_INIT;
376382

377-
char refspec[14+GIT_SHA1_HEXSZ+14+4];
378-
memcpy(refspec, "+refs/heads/.:refs/incrypt/......................................../1/.", 14+GIT_SHA1_HEXSZ+14+4);
379-
refspec[12] = refspec[70] = pattern;
380-
memcpy(refspec + 14, prefix, GIT_SHA1_HEXSZ + 14);
383+
struct strbuf refspec = STRBUF_INIT;
384+
strbuf_addf(&refspec, "+refs/heads/%c:%s1/%c", pattern, prefix.buf, pattern);
381385

382386
strvec_pushl(&cmd.args, "fetch", verbosityflags[options.verbosity],
383387
progressflags[options.progress], "--no-write-fetch-head",
384-
"-p", url, refspec, NULL);
388+
"-p", url.buf, refspec.buf, NULL);
389+
strbuf_release(&refspec);
385390

386391
cmd.git_cmd = 1;
387392
//This causes a crash! : cmd.close_object_store = 1;
@@ -489,12 +494,13 @@ char* writemeta(char* output) {
489494
odb_write_object(the_repository->objects, tb.buf, tb.len, OBJ_TREE, &tid);
490495
strbuf_release(&tb);
491496
secretcommit(&tid, &oid);
492-
strbuf_addf(&refname, "%s1/_", prefix);
497+
strbuf_addbuf(&refname, &prefix);
498+
strbuf_addstr(&refname, "1/_");
493499
// refs_update_ref(get_main_ref_store(the_repository), NULL, refname.buf,
494500
// &oid, NULL, 0, UPDATE_REFS_MSG_ON_ERR);
495501
myupdaterefs(refname.buf, oid_to_hex(&oid));
496502
strbuf_release(&refname);
497-
memcpy(output, oid_to_hex(&oid), 41);
503+
oid_to_hex_r(output, &oid);
498504
return output;
499505
}
500506

0 commit comments

Comments
 (0)