* Return a pointer to the refname within the line (null-terminated),
* or NULL if there was a problem.
*/
-static const char *parse_ref_line(struct strbuf *line, unsigned char *sha1)
+static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
{
const char *ref;
- /*
- * 42: the answer to everything.
- *
- * In this case, it happens to be the answer to
- * 40 (length of sha1 hex representation)
- * +1 (space in between hex and name)
- * +1 (newline at the end of the line)
- */
- if (line->len <= 42)
- return NULL;
-
- if (get_sha1_hex(line->buf, sha1) < 0)
+ if (parse_oid_hex(line->buf, oid, &ref) < 0)
return NULL;
- if (!isspace(line->buf[40]))
+ if (!isspace(*ref++))
return NULL;
- ref = line->buf + 41;
if (isspace(*ref))
return NULL;
enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
while (strbuf_getwholeline(&line, f, '\n') != EOF) {
- unsigned char sha1[20];
+ struct object_id oid;
const char *refname;
const char *traits;
continue;
}
- refname = parse_ref_line(&line, sha1);
+ refname = parse_ref_line(&line, &oid);
if (refname) {
int flag = REF_ISPACKED;
if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
if (!refname_is_safe(refname))
die("packed refname is dangerous: %s", refname);
- hashclr(sha1);
+ oidclr(&oid);
flag |= REF_BAD_NAME | REF_ISBROKEN;
}
- last = create_ref_entry(refname, sha1, flag, 0);
+ last = create_ref_entry(refname, &oid, flag, 0);
if (peeled == PEELED_FULLY ||
(peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
last->flag |= REF_KNOWS_PEELED;
line.buf[0] == '^' &&
line.len == PEELED_LINE_LENGTH &&
line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
- !get_sha1_hex(line.buf + 1, sha1)) {
- hashcpy(last->u.value.peeled.hash, sha1);
+ !get_oid_hex(line.buf + 1, &oid)) {
+ oidcpy(&last->u.value.peeled, &oid);
/*
* Regardless of what the file header said,
* we definitely know the value of *this*
* commit_packed_refs().
*/
static void add_packed_ref(struct files_ref_store *refs,
- const char *refname, const unsigned char *sha1)
+ const char *refname, const struct object_id *oid)
{
struct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);
if (!packed_ref_cache->lock)
die("internal error: packed refs not locked");
add_ref_entry(get_packed_ref_dir(packed_ref_cache),
- create_ref_entry(refname, sha1, REF_ISPACKED, 1));
+ create_ref_entry(refname, oid, REF_ISPACKED, 1));
}
/*
strbuf_add(&refname, dirname, dirnamelen);
while ((de = readdir(d)) != NULL) {
- unsigned char sha1[20];
+ struct object_id oid;
struct stat st;
int flag;
if (!refs_resolve_ref_unsafe(&refs->base,
refname.buf,
RESOLVE_REF_READING,
- sha1, &flag)) {
- hashclr(sha1);
+ oid.hash, &flag)) {
+ oidclr(&oid);
flag |= REF_ISBROKEN;
- } else if (is_null_sha1(sha1)) {
+ } else if (is_null_oid(&oid)) {
/*
* It is so astronomically unlikely
* that NULL_SHA1 is the SHA-1 of an
REFNAME_ALLOW_ONELEVEL)) {
if (!refname_is_safe(refname.buf))
die("loose refname is dangerous: %s", refname.buf);
- hashclr(sha1);
+ oidclr(&oid);
flag |= REF_BAD_NAME | REF_ISBROKEN;
}
add_entry_to_dir(dir,
- create_ref_entry(refname.buf, sha1, flag, 0));
+ create_ref_entry(refname.buf, &oid, flag, 0));
}
strbuf_setlen(&refname, dirnamelen);
strbuf_setlen(&path, path_baselen);
packed_entry->flag = REF_ISPACKED;
oidcpy(&packed_entry->u.value.oid, iter->oid);
} else {
- packed_entry = create_ref_entry(iter->refname, iter->oid->hash,
+ packed_entry = create_ref_entry(iter->refname, iter->oid,
REF_ISPACKED, 0);
add_ref_entry(packed_refs, packed_entry);
}
}
static int write_ref_to_lockfile(struct ref_lock *lock,
- const unsigned char *sha1, struct strbuf *err);
+ const struct object_id *oid, struct strbuf *err);
static int commit_ref_update(struct files_ref_store *refs,
struct ref_lock *lock,
- const unsigned char *sha1, const char *logmsg,
+ const struct object_id *oid, const char *logmsg,
struct strbuf *err);
static int files_rename_ref(struct ref_store *ref_store,
{
struct files_ref_store *refs =
files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
- unsigned char sha1[20], orig_sha1[20];
+ struct object_id oid, orig_oid;
int flag = 0, logmoved = 0;
struct ref_lock *lock;
struct stat loginfo;
if (!refs_resolve_ref_unsafe(&refs->base, oldrefname,
RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
- orig_sha1, &flag)) {
+ orig_oid.hash, &flag)) {
ret = error("refname %s not found", oldrefname);
goto out;
}
}
if (refs_delete_ref(&refs->base, logmsg, oldrefname,
- orig_sha1, REF_NODEREF)) {
+ orig_oid.hash, REF_NODEREF)) {
error("unable to delete old %s", oldrefname);
goto rollback;
}
/*
- * Since we are doing a shallow lookup, sha1 is not the
- * correct value to pass to delete_ref as old_sha1. But that
- * doesn't matter, because an old_sha1 check wouldn't add to
+ * Since we are doing a shallow lookup, oid is not the
+ * correct value to pass to delete_ref as old_oid. But that
+ * doesn't matter, because an old_oid check wouldn't add to
* the safety anyway; we want to delete the reference whatever
* its current value.
*/
if (!refs_read_ref_full(&refs->base, newrefname,
RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
- sha1, NULL) &&
+ oid.hash, NULL) &&
refs_delete_ref(&refs->base, NULL, newrefname,
NULL, REF_NODEREF)) {
if (errno == EISDIR) {
strbuf_release(&err);
goto rollback;
}
- hashcpy(lock->old_oid.hash, orig_sha1);
+ oidcpy(&lock->old_oid, &orig_oid);
- if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
- commit_ref_update(refs, lock, orig_sha1, logmsg, &err)) {
+ if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
+ commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) {
error("unable to write current sha1 into %s: %s", newrefname, err.buf);
strbuf_release(&err);
goto rollback;
flag = log_all_ref_updates;
log_all_ref_updates = LOG_REFS_NONE;
- if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
- commit_ref_update(refs, lock, orig_sha1, NULL, &err)) {
+ if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
+ commit_ref_update(refs, lock, &orig_oid, NULL, &err)) {
error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
strbuf_release(&err);
}
return 0;
}
-static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
- const unsigned char *new_sha1,
+static int log_ref_write_fd(int fd, const struct object_id *old_oid,
+ const struct object_id *new_oid,
const char *committer, const char *msg)
{
int msglen, written;
maxlen = strlen(committer) + msglen + 100;
logrec = xmalloc(maxlen);
len = xsnprintf(logrec, maxlen, "%s %s %s\n",
- sha1_to_hex(old_sha1),
- sha1_to_hex(new_sha1),
+ oid_to_hex(old_oid),
+ oid_to_hex(new_oid),
committer);
if (msglen)
len += copy_reflog_msg(logrec + len - 1, msg) - 1;
}
static int files_log_ref_write(struct files_ref_store *refs,
- const char *refname, const unsigned char *old_sha1,
- const unsigned char *new_sha1, const char *msg,
+ const char *refname, const struct object_id *old_oid,
+ const struct object_id *new_oid, const char *msg,
int flags, struct strbuf *err)
{
int logfd, result;
if (logfd < 0)
return 0;
- result = log_ref_write_fd(logfd, old_sha1, new_sha1,
+ result = log_ref_write_fd(logfd, old_oid, new_oid,
git_committer_info(0), msg);
if (result) {
struct strbuf sb = STRBUF_INIT;
* return -1.
*/
static int write_ref_to_lockfile(struct ref_lock *lock,
- const unsigned char *sha1, struct strbuf *err)
+ const struct object_id *oid, struct strbuf *err)
{
static char term = '\n';
struct object *o;
int fd;
- o = parse_object(sha1);
+ o = parse_object(oid->hash);
if (!o) {
strbuf_addf(err,
"trying to write ref '%s' with nonexistent object %s",
- lock->ref_name, sha1_to_hex(sha1));
+ lock->ref_name, oid_to_hex(oid));
unlock_ref(lock);
return -1;
}
if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
strbuf_addf(err,
"trying to write non-commit object %s to branch '%s'",
- sha1_to_hex(sha1), lock->ref_name);
+ oid_to_hex(oid), lock->ref_name);
unlock_ref(lock);
return -1;
}
fd = get_lock_file_fd(lock->lk);
- if (write_in_full(fd, sha1_to_hex(sha1), 40) != 40 ||
+ if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||
write_in_full(fd, &term, 1) != 1 ||
close_ref(lock) < 0) {
strbuf_addf(err,
*/
static int commit_ref_update(struct files_ref_store *refs,
struct ref_lock *lock,
- const unsigned char *sha1, const char *logmsg,
+ const struct object_id *oid, const char *logmsg,
struct strbuf *err)
{
files_assert_main_repository(refs, "commit_ref_update");
clear_loose_ref_cache(refs);
if (files_log_ref_write(refs, lock->ref_name,
- lock->old_oid.hash, sha1,
+ &lock->old_oid, oid,
logmsg, 0, err)) {
char *old_msg = strbuf_detach(err, NULL);
strbuf_addf(err, "cannot update the ref '%s': %s",
* check with HEAD only which should cover 99% of all usage
* scenarios (even 100% of the default ones).
*/
- unsigned char head_sha1[20];
+ struct object_id head_oid;
int head_flag;
const char *head_ref;
head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",
RESOLVE_REF_READING,
- head_sha1, &head_flag);
+ head_oid.hash, &head_flag);
if (head_ref && (head_flag & REF_ISSYMREF) &&
!strcmp(head_ref, lock->ref_name)) {
struct strbuf log_err = STRBUF_INIT;
if (files_log_ref_write(refs, "HEAD",
- lock->old_oid.hash, sha1,
+ &lock->old_oid, oid,
logmsg, 0, &log_err)) {
error("%s", log_err.buf);
strbuf_release(&log_err);
const char *target, const char *logmsg)
{
struct strbuf err = STRBUF_INIT;
- unsigned char new_sha1[20];
+ struct object_id new_oid;
if (logmsg &&
!refs_read_ref_full(&refs->base, target,
- RESOLVE_REF_READING, new_sha1, NULL) &&
- files_log_ref_write(refs, refname, lock->old_oid.hash,
- new_sha1, logmsg, 0, &err)) {
+ RESOLVE_REF_READING, new_oid.hash, NULL) &&
+ files_log_ref_write(refs, refname, &lock->old_oid,
+ &new_oid, logmsg, 0, &err)) {
error("%s", err.buf);
strbuf_release(&err);
}
new_update = ref_transaction_add_update(
transaction, "HEAD",
update->flags | REF_LOG_ONLY | REF_NODEREF,
- update->new_sha1, update->old_sha1,
+ update->new_oid.hash, update->old_oid.hash,
update->msg);
item->util = new_update;
new_update = ref_transaction_add_update(
transaction, referent, new_flags,
- update->new_sha1, update->old_sha1,
+ update->new_oid.hash, update->old_oid.hash,
update->msg);
new_update->parent_update = update;
struct strbuf *err)
{
if (!(update->flags & REF_HAVE_OLD) ||
- !hashcmp(oid->hash, update->old_sha1))
+ !oidcmp(oid, &update->old_oid))
return 0;
- if (is_null_sha1(update->old_sha1))
+ if (is_null_oid(&update->old_oid))
strbuf_addf(err, "cannot lock ref '%s': "
"reference already exists",
original_update_refname(update));
strbuf_addf(err, "cannot lock ref '%s': "
"reference is missing but expected %s",
original_update_refname(update),
- sha1_to_hex(update->old_sha1));
+ oid_to_hex(&update->old_oid));
else
strbuf_addf(err, "cannot lock ref '%s': "
"is at %s but expected %s",
original_update_refname(update),
oid_to_hex(oid),
- sha1_to_hex(update->old_sha1));
+ oid_to_hex(&update->old_oid));
return -1;
}
{
struct strbuf referent = STRBUF_INIT;
int mustexist = (update->flags & REF_HAVE_OLD) &&
- !is_null_sha1(update->old_sha1);
+ !is_null_oid(&update->old_oid);
int ret;
struct ref_lock *lock;
files_assert_main_repository(refs, "lock_ref_for_update");
- if ((update->flags & REF_HAVE_NEW) && is_null_sha1(update->new_sha1))
+ if ((update->flags & REF_HAVE_NEW) && is_null_oid(&update->new_oid))
update->flags |= REF_DELETING;
if (head_ref) {
!(update->flags & REF_DELETING) &&
!(update->flags & REF_LOG_ONLY)) {
if (!(update->type & REF_ISSYMREF) &&
- !hashcmp(lock->old_oid.hash, update->new_sha1)) {
+ !oidcmp(&lock->old_oid, &update->new_oid)) {
/*
* The reference already has the desired
* value, so we don't need to write it.
*/
- } else if (write_ref_to_lockfile(lock, update->new_sha1,
+ } else if (write_ref_to_lockfile(lock, &update->new_oid,
err)) {
char *write_err = strbuf_detach(err, NULL);
update->flags & REF_LOG_ONLY) {
if (files_log_ref_write(refs,
lock->ref_name,
- lock->old_oid.hash,
- update->new_sha1,
+ &lock->old_oid,
+ &update->new_oid,
update->msg, update->flags,
err)) {
char *old_msg = strbuf_detach(err, NULL);
struct ref_update *update = transaction->updates[i];
if ((update->flags & REF_HAVE_OLD) &&
- !is_null_sha1(update->old_sha1))
+ !is_null_oid(&update->old_oid))
die("BUG: initial ref transaction with old_sha1 set");
if (refs_verify_refname_available(&refs->base, update->refname,
&affected_refnames, NULL,
struct ref_update *update = transaction->updates[i];
if ((update->flags & REF_HAVE_NEW) &&
- !is_null_sha1(update->new_sha1))
- add_packed_ref(refs, update->refname, update->new_sha1);
+ !is_null_oid(&update->new_oid))
+ add_packed_ref(refs, update->refname,
+ &update->new_oid);
}
if (commit_packed_refs(refs)) {