Skip to content

Commit 4417df8

Browse files
bk2204gitster
authored andcommitted
refs/files-backend: convert many internals to struct object_id
Convert many of the internals of the files backend to use struct object_id. Avoid converting public APIs (except one change to refs/ref-cache.c) to limit the scope of the changes. Convert one use of get_sha1_hex to parse_oid_hex, and rely on the fact that a strbuf will be NUL-terminated and that parse_oid_hex will fail on truncated input to avoid the need to check the length. This is a requirement to convert parse_object later on. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 9849129 commit 4417df8

File tree

3 files changed

+60
-72
lines changed

3 files changed

+60
-72
lines changed

refs/files-backend.c

Lines changed: 57 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -195,27 +195,15 @@ static const char PACKED_REFS_HEADER[] =
195195
* Return a pointer to the refname within the line (null-terminated),
196196
* or NULL if there was a problem.
197197
*/
198-
static const char *parse_ref_line(struct strbuf *line, unsigned char *sha1)
198+
static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
199199
{
200200
const char *ref;
201201

202-
/*
203-
* 42: the answer to everything.
204-
*
205-
* In this case, it happens to be the answer to
206-
* 40 (length of sha1 hex representation)
207-
* +1 (space in between hex and name)
208-
* +1 (newline at the end of the line)
209-
*/
210-
if (line->len <= 42)
211-
return NULL;
212-
213-
if (get_sha1_hex(line->buf, sha1) < 0)
202+
if (parse_oid_hex(line->buf, oid, &ref) < 0)
214203
return NULL;
215-
if (!isspace(line->buf[40]))
204+
if (!isspace(*ref++))
216205
return NULL;
217206

218-
ref = line->buf + 41;
219207
if (isspace(*ref))
220208
return NULL;
221209

@@ -260,7 +248,7 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
260248
enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
261249

262250
while (strbuf_getwholeline(&line, f, '\n') != EOF) {
263-
unsigned char sha1[20];
251+
struct object_id oid;
264252
const char *refname;
265253
const char *traits;
266254

@@ -273,17 +261,17 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
273261
continue;
274262
}
275263

276-
refname = parse_ref_line(&line, sha1);
264+
refname = parse_ref_line(&line, &oid);
277265
if (refname) {
278266
int flag = REF_ISPACKED;
279267

280268
if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
281269
if (!refname_is_safe(refname))
282270
die("packed refname is dangerous: %s", refname);
283-
hashclr(sha1);
271+
oidclr(&oid);
284272
flag |= REF_BAD_NAME | REF_ISBROKEN;
285273
}
286-
last = create_ref_entry(refname, sha1, flag, 0);
274+
last = create_ref_entry(refname, &oid, flag, 0);
287275
if (peeled == PEELED_FULLY ||
288276
(peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
289277
last->flag |= REF_KNOWS_PEELED;
@@ -294,8 +282,8 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
294282
line.buf[0] == '^' &&
295283
line.len == PEELED_LINE_LENGTH &&
296284
line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
297-
!get_sha1_hex(line.buf + 1, sha1)) {
298-
hashcpy(last->u.value.peeled.hash, sha1);
285+
!get_oid_hex(line.buf + 1, &oid)) {
286+
oidcpy(&last->u.value.peeled, &oid);
299287
/*
300288
* Regardless of what the file header said,
301289
* we definitely know the value of *this*
@@ -404,14 +392,14 @@ static struct ref_dir *get_packed_refs(struct files_ref_store *refs)
404392
* commit_packed_refs().
405393
*/
406394
static void add_packed_ref(struct files_ref_store *refs,
407-
const char *refname, const unsigned char *sha1)
395+
const char *refname, const struct object_id *oid)
408396
{
409397
struct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);
410398

411399
if (!packed_ref_cache->lock)
412400
die("internal error: packed refs not locked");
413401
add_ref_entry(get_packed_ref_dir(packed_ref_cache),
414-
create_ref_entry(refname, sha1, REF_ISPACKED, 1));
402+
create_ref_entry(refname, oid, REF_ISPACKED, 1));
415403
}
416404

417405
/*
@@ -444,7 +432,7 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
444432
strbuf_add(&refname, dirname, dirnamelen);
445433

446434
while ((de = readdir(d)) != NULL) {
447-
unsigned char sha1[20];
435+
struct object_id oid;
448436
struct stat st;
449437
int flag;
450438

@@ -465,10 +453,10 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
465453
if (!refs_resolve_ref_unsafe(&refs->base,
466454
refname.buf,
467455
RESOLVE_REF_READING,
468-
sha1, &flag)) {
469-
hashclr(sha1);
456+
oid.hash, &flag)) {
457+
oidclr(&oid);
470458
flag |= REF_ISBROKEN;
471-
} else if (is_null_sha1(sha1)) {
459+
} else if (is_null_oid(&oid)) {
472460
/*
473461
* It is so astronomically unlikely
474462
* that NULL_SHA1 is the SHA-1 of an
@@ -484,11 +472,11 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
484472
REFNAME_ALLOW_ONELEVEL)) {
485473
if (!refname_is_safe(refname.buf))
486474
die("loose refname is dangerous: %s", refname.buf);
487-
hashclr(sha1);
475+
oidclr(&oid);
488476
flag |= REF_BAD_NAME | REF_ISBROKEN;
489477
}
490478
add_entry_to_dir(dir,
491-
create_ref_entry(refname.buf, sha1, flag, 0));
479+
create_ref_entry(refname.buf, &oid, flag, 0));
492480
}
493481
strbuf_setlen(&refname, dirnamelen);
494482
strbuf_setlen(&path, path_baselen);
@@ -1526,7 +1514,7 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
15261514
packed_entry->flag = REF_ISPACKED;
15271515
oidcpy(&packed_entry->u.value.oid, iter->oid);
15281516
} else {
1529-
packed_entry = create_ref_entry(iter->refname, iter->oid->hash,
1517+
packed_entry = create_ref_entry(iter->refname, iter->oid,
15301518
REF_ISPACKED, 0);
15311519
add_ref_entry(packed_refs, packed_entry);
15321520
}
@@ -1709,10 +1697,10 @@ static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)
17091697
}
17101698

17111699
static int write_ref_to_lockfile(struct ref_lock *lock,
1712-
const unsigned char *sha1, struct strbuf *err);
1700+
const struct object_id *oid, struct strbuf *err);
17131701
static int commit_ref_update(struct files_ref_store *refs,
17141702
struct ref_lock *lock,
1715-
const unsigned char *sha1, const char *logmsg,
1703+
const struct object_id *oid, const char *logmsg,
17161704
struct strbuf *err);
17171705

17181706
static int files_rename_ref(struct ref_store *ref_store,
@@ -1721,7 +1709,7 @@ static int files_rename_ref(struct ref_store *ref_store,
17211709
{
17221710
struct files_ref_store *refs =
17231711
files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
1724-
unsigned char sha1[20], orig_sha1[20];
1712+
struct object_id oid, orig_oid;
17251713
int flag = 0, logmoved = 0;
17261714
struct ref_lock *lock;
17271715
struct stat loginfo;
@@ -1743,7 +1731,7 @@ static int files_rename_ref(struct ref_store *ref_store,
17431731

17441732
if (!refs_resolve_ref_unsafe(&refs->base, oldrefname,
17451733
RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1746-
orig_sha1, &flag)) {
1734+
orig_oid.hash, &flag)) {
17471735
ret = error("refname %s not found", oldrefname);
17481736
goto out;
17491737
}
@@ -1765,21 +1753,21 @@ static int files_rename_ref(struct ref_store *ref_store,
17651753
}
17661754

17671755
if (refs_delete_ref(&refs->base, logmsg, oldrefname,
1768-
orig_sha1, REF_NODEREF)) {
1756+
orig_oid.hash, REF_NODEREF)) {
17691757
error("unable to delete old %s", oldrefname);
17701758
goto rollback;
17711759
}
17721760

17731761
/*
1774-
* Since we are doing a shallow lookup, sha1 is not the
1775-
* correct value to pass to delete_ref as old_sha1. But that
1776-
* doesn't matter, because an old_sha1 check wouldn't add to
1762+
* Since we are doing a shallow lookup, oid is not the
1763+
* correct value to pass to delete_ref as old_oid. But that
1764+
* doesn't matter, because an old_oid check wouldn't add to
17771765
* the safety anyway; we want to delete the reference whatever
17781766
* its current value.
17791767
*/
17801768
if (!refs_read_ref_full(&refs->base, newrefname,
17811769
RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1782-
sha1, NULL) &&
1770+
oid.hash, NULL) &&
17831771
refs_delete_ref(&refs->base, NULL, newrefname,
17841772
NULL, REF_NODEREF)) {
17851773
if (errno == EISDIR) {
@@ -1812,10 +1800,10 @@ static int files_rename_ref(struct ref_store *ref_store,
18121800
strbuf_release(&err);
18131801
goto rollback;
18141802
}
1815-
hashcpy(lock->old_oid.hash, orig_sha1);
1803+
oidcpy(&lock->old_oid, &orig_oid);
18161804

1817-
if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
1818-
commit_ref_update(refs, lock, orig_sha1, logmsg, &err)) {
1805+
if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
1806+
commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) {
18191807
error("unable to write current sha1 into %s: %s", newrefname, err.buf);
18201808
strbuf_release(&err);
18211809
goto rollback;
@@ -1835,8 +1823,8 @@ static int files_rename_ref(struct ref_store *ref_store,
18351823

18361824
flag = log_all_ref_updates;
18371825
log_all_ref_updates = LOG_REFS_NONE;
1838-
if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
1839-
commit_ref_update(refs, lock, orig_sha1, NULL, &err)) {
1826+
if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
1827+
commit_ref_update(refs, lock, &orig_oid, NULL, &err)) {
18401828
error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
18411829
strbuf_release(&err);
18421830
}
@@ -1986,8 +1974,8 @@ static int files_create_reflog(struct ref_store *ref_store,
19861974
return 0;
19871975
}
19881976

1989-
static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
1990-
const unsigned char *new_sha1,
1977+
static int log_ref_write_fd(int fd, const struct object_id *old_oid,
1978+
const struct object_id *new_oid,
19911979
const char *committer, const char *msg)
19921980
{
19931981
int msglen, written;
@@ -1998,8 +1986,8 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
19981986
maxlen = strlen(committer) + msglen + 100;
19991987
logrec = xmalloc(maxlen);
20001988
len = xsnprintf(logrec, maxlen, "%s %s %s\n",
2001-
sha1_to_hex(old_sha1),
2002-
sha1_to_hex(new_sha1),
1989+
oid_to_hex(old_oid),
1990+
oid_to_hex(new_oid),
20031991
committer);
20041992
if (msglen)
20051993
len += copy_reflog_msg(logrec + len - 1, msg) - 1;
@@ -2013,8 +2001,8 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
20132001
}
20142002

20152003
static int files_log_ref_write(struct files_ref_store *refs,
2016-
const char *refname, const unsigned char *old_sha1,
2017-
const unsigned char *new_sha1, const char *msg,
2004+
const char *refname, const struct object_id *old_oid,
2005+
const struct object_id *new_oid, const char *msg,
20182006
int flags, struct strbuf *err)
20192007
{
20202008
int logfd, result;
@@ -2031,7 +2019,7 @@ static int files_log_ref_write(struct files_ref_store *refs,
20312019

20322020
if (logfd < 0)
20332021
return 0;
2034-
result = log_ref_write_fd(logfd, old_sha1, new_sha1,
2022+
result = log_ref_write_fd(logfd, old_oid, new_oid,
20352023
git_committer_info(0), msg);
20362024
if (result) {
20372025
struct strbuf sb = STRBUF_INIT;
@@ -2063,29 +2051,29 @@ static int files_log_ref_write(struct files_ref_store *refs,
20632051
* return -1.
20642052
*/
20652053
static int write_ref_to_lockfile(struct ref_lock *lock,
2066-
const unsigned char *sha1, struct strbuf *err)
2054+
const struct object_id *oid, struct strbuf *err)
20672055
{
20682056
static char term = '\n';
20692057
struct object *o;
20702058
int fd;
20712059

2072-
o = parse_object(sha1);
2060+
o = parse_object(oid->hash);
20732061
if (!o) {
20742062
strbuf_addf(err,
20752063
"trying to write ref '%s' with nonexistent object %s",
2076-
lock->ref_name, sha1_to_hex(sha1));
2064+
lock->ref_name, oid_to_hex(oid));
20772065
unlock_ref(lock);
20782066
return -1;
20792067
}
20802068
if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
20812069
strbuf_addf(err,
20822070
"trying to write non-commit object %s to branch '%s'",
2083-
sha1_to_hex(sha1), lock->ref_name);
2071+
oid_to_hex(oid), lock->ref_name);
20842072
unlock_ref(lock);
20852073
return -1;
20862074
}
20872075
fd = get_lock_file_fd(lock->lk);
2088-
if (write_in_full(fd, sha1_to_hex(sha1), 40) != 40 ||
2076+
if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||
20892077
write_in_full(fd, &term, 1) != 1 ||
20902078
close_ref(lock) < 0) {
20912079
strbuf_addf(err,
@@ -2103,14 +2091,14 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
21032091
*/
21042092
static int commit_ref_update(struct files_ref_store *refs,
21052093
struct ref_lock *lock,
2106-
const unsigned char *sha1, const char *logmsg,
2094+
const struct object_id *oid, const char *logmsg,
21072095
struct strbuf *err)
21082096
{
21092097
files_assert_main_repository(refs, "commit_ref_update");
21102098

21112099
clear_loose_ref_cache(refs);
21122100
if (files_log_ref_write(refs, lock->ref_name,
2113-
lock->old_oid.hash, sha1,
2101+
&lock->old_oid, oid,
21142102
logmsg, 0, err)) {
21152103
char *old_msg = strbuf_detach(err, NULL);
21162104
strbuf_addf(err, "cannot update the ref '%s': %s",
@@ -2133,18 +2121,18 @@ static int commit_ref_update(struct files_ref_store *refs,
21332121
* check with HEAD only which should cover 99% of all usage
21342122
* scenarios (even 100% of the default ones).
21352123
*/
2136-
unsigned char head_sha1[20];
2124+
struct object_id head_oid;
21372125
int head_flag;
21382126
const char *head_ref;
21392127

21402128
head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",
21412129
RESOLVE_REF_READING,
2142-
head_sha1, &head_flag);
2130+
head_oid.hash, &head_flag);
21432131
if (head_ref && (head_flag & REF_ISSYMREF) &&
21442132
!strcmp(head_ref, lock->ref_name)) {
21452133
struct strbuf log_err = STRBUF_INIT;
21462134
if (files_log_ref_write(refs, "HEAD",
2147-
lock->old_oid.hash, sha1,
2135+
&lock->old_oid, oid,
21482136
logmsg, 0, &log_err)) {
21492137
error("%s", log_err.buf);
21502138
strbuf_release(&log_err);
@@ -2182,12 +2170,12 @@ static void update_symref_reflog(struct files_ref_store *refs,
21822170
const char *target, const char *logmsg)
21832171
{
21842172
struct strbuf err = STRBUF_INIT;
2185-
unsigned char new_sha1[20];
2173+
struct object_id new_oid;
21862174
if (logmsg &&
21872175
!refs_read_ref_full(&refs->base, target,
2188-
RESOLVE_REF_READING, new_sha1, NULL) &&
2189-
files_log_ref_write(refs, refname, lock->old_oid.hash,
2190-
new_sha1, logmsg, 0, &err)) {
2176+
RESOLVE_REF_READING, new_oid.hash, NULL) &&
2177+
files_log_ref_write(refs, refname, &lock->old_oid,
2178+
&new_oid, logmsg, 0, &err)) {
21912179
error("%s", err.buf);
21922180
strbuf_release(&err);
21932181
}
@@ -2866,7 +2854,7 @@ static int lock_ref_for_update(struct files_ref_store *refs,
28662854
* The reference already has the desired
28672855
* value, so we don't need to write it.
28682856
*/
2869-
} else if (write_ref_to_lockfile(lock, update->new_oid.hash,
2857+
} else if (write_ref_to_lockfile(lock, &update->new_oid,
28702858
err)) {
28712859
char *write_err = strbuf_detach(err, NULL);
28722860

@@ -3001,8 +2989,8 @@ static int files_transaction_commit(struct ref_store *ref_store,
30012989
update->flags & REF_LOG_ONLY) {
30022990
if (files_log_ref_write(refs,
30032991
lock->ref_name,
3004-
lock->old_oid.hash,
3005-
update->new_oid.hash,
2992+
&lock->old_oid,
2993+
&update->new_oid,
30062994
update->msg, update->flags,
30072995
err)) {
30082996
char *old_msg = strbuf_detach(err, NULL);
@@ -3174,7 +3162,7 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
31743162
if ((update->flags & REF_HAVE_NEW) &&
31753163
!is_null_oid(&update->new_oid))
31763164
add_packed_ref(refs, update->refname,
3177-
update->new_oid.hash);
3165+
&update->new_oid);
31783166
}
31793167

31803168
if (commit_packed_refs(refs)) {

refs/ref-cache.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct ref_dir *get_ref_dir(struct ref_entry *entry)
3232
}
3333

3434
struct ref_entry *create_ref_entry(const char *refname,
35-
const unsigned char *sha1, int flag,
35+
const struct object_id *oid, int flag,
3636
int check_name)
3737
{
3838
struct ref_entry *ref;
@@ -41,7 +41,7 @@ struct ref_entry *create_ref_entry(const char *refname,
4141
check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
4242
die("Reference has invalid format: '%s'", refname);
4343
FLEX_ALLOC_STR(ref, name, refname);
44-
hashcpy(ref->u.value.oid.hash, sha1);
44+
oidcpy(&ref->u.value.oid, oid);
4545
oidclr(&ref->u.value.peeled);
4646
ref->flag = flag;
4747
return ref;

0 commit comments

Comments
 (0)