#include "cache.h"
+#include "config.h"
#include "notes.h"
#include "blob.h"
#include "tree.h"
* subtree.
*/
struct leaf_node {
- unsigned char key_sha1[20];
- unsigned char val_sha1[20];
+ struct object_id key_oid;
+ struct object_id val_oid;
};
/*
struct non_note *next; /* grounded (last->next == NULL) */
char *path;
unsigned int mode;
- unsigned char sha1[20];
+ struct object_id oid;
};
#define PTR_TYPE_NULL 0
#define GET_NIBBLE(n, sha1) (((sha1[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
+#define KEY_INDEX (GIT_SHA1_RAWSZ - 1)
+#define FANOUT_PATH_SEPARATORS ((GIT_SHA1_HEXSZ / 2) - 1)
#define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
- (memcmp(key_sha1, subtree_sha1, subtree_sha1[19]))
+ (memcmp(key_sha1, subtree_sha1, subtree_sha1[KEY_INDEX]))
struct notes_tree default_notes_tree;
if (GET_PTR_TYPE(p) == PTR_TYPE_SUBTREE) {
l = (struct leaf_node *) CLR_PTR_TYPE(p);
- if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) {
+ if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
/* unpack tree and resume search */
(*tree)->a[0] = NULL;
load_subtree(t, l, *tree, *n);
return note_tree_search(t, tree, n, key_sha1);
case PTR_TYPE_SUBTREE:
l = (struct leaf_node *) CLR_PTR_TYPE(p);
- if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) {
+ if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
/* unpack tree and resume search */
(*tree)->a[i] = NULL;
load_subtree(t, l, *tree, *n);
void **p = note_tree_search(t, &tree, &n, key_sha1);
if (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) {
struct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p);
- if (!hashcmp(key_sha1, l->key_sha1))
+ if (!hashcmp(key_sha1, l->key_oid.hash))
return l;
}
return NULL;
struct leaf_node *entry)
{
struct leaf_node *l;
- struct int_node *parent_stack[20];
+ struct int_node *parent_stack[GIT_SHA1_RAWSZ];
unsigned char i, j;
- void **p = note_tree_search(t, &tree, &n, entry->key_sha1);
+ void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)
return; /* type mismatch, nothing to remove */
l = (struct leaf_node *) CLR_PTR_TYPE(*p);
- if (hashcmp(l->key_sha1, entry->key_sha1))
+ if (oidcmp(&l->key_oid, &entry->key_oid))
return; /* key mismatch, nothing to remove */
/* we have found a matching entry */
- hashcpy(entry->val_sha1, l->val_sha1);
+ oidcpy(&entry->val_oid, &l->val_oid);
free(l);
*p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);
/* first, build stack of ancestors between root and current node */
parent_stack[0] = t->root;
for (i = 0; i < n; i++) {
- j = GET_NIBBLE(i, entry->key_sha1);
+ j = GET_NIBBLE(i, entry->key_oid.hash);
parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);
}
assert(i == n && parent_stack[i] == tree);
/* next, unwind stack until note_tree_consolidate() is done */
while (i > 0 &&
!note_tree_consolidate(parent_stack[i], parent_stack[i - 1],
- GET_NIBBLE(i - 1, entry->key_sha1)))
+ GET_NIBBLE(i - 1, entry->key_oid.hash)))
i--;
}
{
struct int_node *new_node;
struct leaf_node *l;
- void **p = note_tree_search(t, &tree, &n, entry->key_sha1);
+ void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
int ret = 0;
assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
switch (GET_PTR_TYPE(*p)) {
case PTR_TYPE_NULL:
assert(!*p);
- if (is_null_sha1(entry->val_sha1))
+ if (is_null_oid(&entry->val_oid))
free(entry);
else
*p = SET_PTR_TYPE(entry, type);
case PTR_TYPE_NOTE:
switch (type) {
case PTR_TYPE_NOTE:
- if (!hashcmp(l->key_sha1, entry->key_sha1)) {
+ if (!oidcmp(&l->key_oid, &entry->key_oid)) {
/* skip concatenation if l == entry */
- if (!hashcmp(l->val_sha1, entry->val_sha1))
+ if (!oidcmp(&l->val_oid, &entry->val_oid))
return 0;
- ret = combine_notes(l->val_sha1,
- entry->val_sha1);
- if (!ret && is_null_sha1(l->val_sha1))
+ ret = combine_notes(l->val_oid.hash,
+ entry->val_oid.hash);
+ if (!ret && is_null_oid(&l->val_oid))
note_tree_remove(t, tree, n, entry);
free(entry);
return ret;
}
break;
case PTR_TYPE_SUBTREE:
- if (!SUBTREE_SHA1_PREFIXCMP(l->key_sha1,
- entry->key_sha1)) {
+ if (!SUBTREE_SHA1_PREFIXCMP(l->key_oid.hash,
+ entry->key_oid.hash)) {
/* unpack 'entry' */
load_subtree(t, entry, tree, n);
free(entry);
}
break;
case PTR_TYPE_SUBTREE:
- if (!SUBTREE_SHA1_PREFIXCMP(entry->key_sha1, l->key_sha1)) {
+ if (!SUBTREE_SHA1_PREFIXCMP(entry->key_oid.hash, l->key_oid.hash)) {
/* unpack 'l' and restart insert */
*p = NULL;
load_subtree(t, l, tree, n);
/* non-matching leaf_node */
assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||
GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);
- if (is_null_sha1(entry->val_sha1)) { /* skip insertion of empty note */
+ if (is_null_oid(&entry->val_oid)) { /* skip insertion of empty note */
free(entry);
return 0;
}
* Otherwise, returns number of bytes written to sha1 (i.e. hex_len / 2).
* Pads sha1 with NULs up to sha1_len (not included in returned length).
*/
-static int get_sha1_hex_segment(const char *hex, unsigned int hex_len,
- unsigned char *sha1, unsigned int sha1_len)
+static int get_oid_hex_segment(const char *hex, unsigned int hex_len,
+ unsigned char *oid, unsigned int oid_len)
{
unsigned int i, len = hex_len >> 1;
- if (hex_len % 2 != 0 || len > sha1_len)
+ if (hex_len % 2 != 0 || len > oid_len)
return -1;
for (i = 0; i < len; i++) {
unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
if (val & ~0xff)
return -1;
- *sha1++ = val;
+ *oid++ = val;
hex += 2;
}
- for (; i < sha1_len; i++)
- *sha1++ = 0;
+ for (; i < oid_len; i++)
+ *oid++ = 0;
return len;
}
n->next = NULL;
n->path = path;
n->mode = mode;
- hashcpy(n->sha1, sha1);
+ hashcpy(n->oid.hash, sha1);
t->prev_non_note = n;
if (!t->first_non_note) {
if (non_note_cmp(p, n) == 0) { /* n ~= p; overwrite p with n */
assert(strcmp(p->path, n->path) == 0);
p->mode = n->mode;
- hashcpy(p->sha1, n->sha1);
+ oidcpy(&p->oid, &n->oid);
free(n);
t->prev_non_note = p;
return;
static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
struct int_node *node, unsigned int n)
{
- unsigned char object_sha1[20];
+ struct object_id object_oid;
unsigned int prefix_len;
void *buf;
struct tree_desc desc;
unsigned char type;
struct leaf_node *l;
- buf = fill_tree_descriptor(&desc, subtree->val_sha1);
+ buf = fill_tree_descriptor(&desc, subtree->val_oid.hash);
if (!buf)
die("Could not read %s for notes-index",
- sha1_to_hex(subtree->val_sha1));
+ oid_to_hex(&subtree->val_oid));
- prefix_len = subtree->key_sha1[19];
+ prefix_len = subtree->key_oid.hash[KEY_INDEX];
assert(prefix_len * 2 >= n);
- memcpy(object_sha1, subtree->key_sha1, prefix_len);
+ memcpy(object_oid.hash, subtree->key_oid.hash, prefix_len);
while (tree_entry(&desc, &entry)) {
path_len = strlen(entry.path);
- len = get_sha1_hex_segment(entry.path, path_len,
- object_sha1 + prefix_len, 20 - prefix_len);
+ len = get_oid_hex_segment(entry.path, path_len,
+ object_oid.hash + prefix_len, GIT_SHA1_RAWSZ - prefix_len);
if (len < 0)
goto handle_non_note; /* entry.path is not a SHA1 */
len += prefix_len;
* If object SHA1 is incomplete (len < 20), and current
* component consists of 2 hex chars, assume note subtree
*/
- if (len <= 20) {
+ if (len <= GIT_SHA1_RAWSZ) {
type = PTR_TYPE_NOTE;
l = (struct leaf_node *)
xcalloc(1, sizeof(struct leaf_node));
- hashcpy(l->key_sha1, object_sha1);
- hashcpy(l->val_sha1, entry.oid->hash);
- if (len < 20) {
+ oidcpy(&l->key_oid, &object_oid);
+ oidcpy(&l->val_oid, entry.oid);
+ if (len < GIT_SHA1_RAWSZ) {
if (!S_ISDIR(entry.mode) || path_len != 2)
goto handle_non_note; /* not subtree */
- l->key_sha1[19] = (unsigned char) len;
+ l->key_oid.hash[KEY_INDEX] = (unsigned char) len;
type = PTR_TYPE_SUBTREE;
}
if (note_tree_insert(t, node, n, l, type,
die("Failed to load %s %s into notes tree "
"from %s",
type == PTR_TYPE_NOTE ? "note" : "subtree",
- sha1_to_hex(l->key_sha1), t->ref);
+ oid_to_hex(&l->key_oid), t->ref);
}
continue;
*/
{
struct strbuf non_note_path = STRBUF_INIT;
- const char *q = sha1_to_hex(subtree->key_sha1);
+ const char *q = oid_to_hex(&subtree->key_oid);
int i;
for (i = 0; i < prefix_len; i++) {
strbuf_addch(&non_note_path, *q++);
}
/* hex SHA1 + 19 * '/' + NUL */
-#define FANOUT_PATH_MAX 40 + 19 + 1
+#define FANOUT_PATH_MAX GIT_SHA1_HEXSZ + FANOUT_PATH_SEPARATORS + 1
static void construct_path_with_fanout(const unsigned char *sha1,
unsigned char fanout, char *path)
{
unsigned int i = 0, j = 0;
const char *hex_sha1 = sha1_to_hex(sha1);
- assert(fanout < 20);
+ assert(fanout < GIT_SHA1_RAWSZ);
while (fanout) {
path[i++] = hex_sha1[j++];
path[i++] = hex_sha1[j++];
flags & FOR_EACH_NOTE_YIELD_SUBTREES) {
/* invoke callback with subtree */
unsigned int path_len =
- l->key_sha1[19] * 2 + fanout;
+ l->key_oid.hash[KEY_INDEX] * 2 + fanout;
assert(path_len < FANOUT_PATH_MAX - 1);
- construct_path_with_fanout(l->key_sha1, fanout,
+ construct_path_with_fanout(l->key_oid.hash,
+ fanout,
path);
/* Create trailing slash, if needed */
if (path[path_len - 1] != '/')
path[path_len++] = '/';
path[path_len] = '\0';
- ret = fn(l->key_sha1, l->val_sha1, path,
+ ret = fn(&l->key_oid, &l->val_oid,
+ path,
cb_data);
}
if (n > fanout * 2 ||
break;
case PTR_TYPE_NOTE:
l = (struct leaf_node *) CLR_PTR_TYPE(p);
- construct_path_with_fanout(l->key_sha1, fanout, path);
- ret = fn(l->key_sha1, l->val_sha1, path, cb_data);
+ construct_path_with_fanout(l->key_oid.hash, fanout,
+ path);
+ ret = fn(&l->key_oid, &l->val_oid, path,
+ cb_data);
break;
}
if (ret)
unsigned char *sha1)
{
strbuf_addf(buf, "%o %.*s%c", mode, path_len, path, '\0');
- strbuf_add(buf, sha1, 20);
+ strbuf_add(buf, sha1, GIT_SHA1_RAWSZ);
}
static void tree_write_stack_init_subtree(struct tree_write_stack *tws,
n = (struct tree_write_stack *)
xmalloc(sizeof(struct tree_write_stack));
n->next = NULL;
- strbuf_init(&n->buf, 256 * (32 + 40)); /* assume 256 entries per tree */
+ strbuf_init(&n->buf, 256 * (32 + GIT_SHA1_HEXSZ)); /* assume 256 entries per tree */
n->path[0] = n->path[1] = '\0';
tws->next = n;
tws->path[0] = path[0];
{
int ret;
struct tree_write_stack *n = tws->next;
- unsigned char s[20];
+ struct object_id s;
if (n) {
ret = tree_write_stack_finish_subtree(n);
if (ret)
return ret;
- ret = write_sha1_file(n->buf.buf, n->buf.len, tree_type, s);
+ ret = write_sha1_file(n->buf.buf, n->buf.len, tree_type, s.hash);
if (ret)
return ret;
strbuf_release(&n->buf);
free(n);
tws->next = NULL;
- write_tree_entry(&tws->buf, 040000, tws->path, 2, s);
+ write_tree_entry(&tws->buf, 040000, tws->path, 2, s.hash);
tws->path[0] = tws->path[1] = '\0';
}
return 0;
static int write_each_note_helper(struct tree_write_stack *tws,
const char *path, unsigned int mode,
- const unsigned char *sha1)
+ const struct object_id *oid)
{
size_t path_len = strlen(path);
unsigned int n = 0;
/* Finally add given entry to the current tree object */
write_tree_entry(&tws->buf, mode, path + 3 * n, path_len - (3 * n),
- sha1);
+ oid->hash);
return 0;
}
; /* do nothing, prefer note to non-note */
else {
ret = write_each_note_helper(d->root, n->path, n->mode,
- n->sha1);
+ &n->oid);
if (ret)
return ret;
}
return 0;
}
-static int write_each_note(const unsigned char *object_sha1,
- const unsigned char *note_sha1, char *note_path,
+static int write_each_note(const struct object_id *object_oid,
+ const struct object_id *note_oid, char *note_path,
void *cb_data)
{
struct write_each_note_data *d =
note_path[note_path_len] = '\0';
mode = 040000;
}
- assert(note_path_len <= 40 + 19);
+ assert(note_path_len <= GIT_SHA1_HEXSZ + FANOUT_PATH_SEPARATORS);
/* Weave non-note entries into note entries */
return write_each_non_note_until(note_path, d) ||
- write_each_note_helper(d->root, note_path, mode, note_sha1);
+ write_each_note_helper(d->root, note_path, mode, note_oid);
}
struct note_delete_list {
const unsigned char *sha1;
};
-static int prune_notes_helper(const unsigned char *object_sha1,
- const unsigned char *note_sha1, char *note_path,
+static int prune_notes_helper(const struct object_id *object_oid,
+ const struct object_id *note_oid, char *note_path,
void *cb_data)
{
struct note_delete_list **l = (struct note_delete_list **) cb_data;
struct note_delete_list *n;
- if (has_sha1_file(object_sha1))
+ if (has_object_file(object_oid))
return 0; /* nothing to do for this note */
/* failed to find object => prune this note */
n = (struct note_delete_list *) xmalloc(sizeof(*n));
n->next = *l;
- n->sha1 = object_sha1;
+ n->sha1 = object_oid->hash;
*l = n;
return 0;
}
if (has_glob_specials(glob)) {
for_each_glob_ref(string_list_add_one_ref, glob, list);
} else {
- unsigned char sha1[20];
- if (get_sha1(glob, sha1))
+ struct object_id oid;
+ if (get_oid(glob, &oid))
warning("notes ref %s is invalid", glob);
if (!unsorted_string_list_has_string(list, glob))
string_list_append(list, glob);
die("Failed to read notes tree referenced by %s (%s)",
notes_ref, oid_to_hex(&object_oid));
- hashclr(root_tree.key_sha1);
- hashcpy(root_tree.val_sha1, oid.hash);
+ oidclr(&root_tree.key_oid);
+ oidcpy(&root_tree.val_oid, &oid);
load_subtree(t, &root_tree, t->root, 0);
}
string_list_clear(&display_notes_refs, 0);
}
-int add_note(struct notes_tree *t, const unsigned char *object_sha1,
- const unsigned char *note_sha1, combine_notes_fn combine_notes)
+int add_note(struct notes_tree *t, const struct object_id *object_oid,
+ const struct object_id *note_oid, combine_notes_fn combine_notes)
{
struct leaf_node *l;
if (!combine_notes)
combine_notes = t->combine_notes;
l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node));
- hashcpy(l->key_sha1, object_sha1);
- hashcpy(l->val_sha1, note_sha1);
+ oidcpy(&l->key_oid, object_oid);
+ oidcpy(&l->val_oid, note_oid);
return note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);
}
if (!t)
t = &default_notes_tree;
assert(t->initialized);
- hashcpy(l.key_sha1, object_sha1);
- hashclr(l.val_sha1);
+ hashcpy(l.key_oid.hash, object_sha1);
+ oidclr(&l.val_oid);
note_tree_remove(t, t->root, 0, &l);
- if (is_null_sha1(l.val_sha1)) /* no note was removed */
+ if (is_null_oid(&l.val_oid)) /* no note was removed */
return 1;
t->dirty = 1;
return 0;
}
-const unsigned char *get_note(struct notes_tree *t,
- const unsigned char *object_sha1)
+const struct object_id *get_note(struct notes_tree *t,
+ const struct object_id *oid)
{
struct leaf_node *found;
if (!t)
t = &default_notes_tree;
assert(t->initialized);
- found = note_tree_find(t, t->root, 0, object_sha1);
- return found ? found->val_sha1 : NULL;
+ found = note_tree_find(t, t->root, 0, oid->hash);
+ return found ? &found->val_oid : NULL;
}
int for_each_note(struct notes_tree *t, int flags, each_note_fn fn,
/* Prepare for traversal of current notes tree */
root.next = NULL; /* last forward entry in list is grounded */
- strbuf_init(&root.buf, 256 * (32 + 40)); /* assume 256 entries */
+ strbuf_init(&root.buf, 256 * (32 + GIT_SHA1_HEXSZ)); /* assume 256 entries */
root.path[0] = root.path[1] = '\0';
cb_data.root = &root;
cb_data.next_non_note = t->first_non_note;
* (raw != 0) gives the %N userformat; otherwise, the note message is given
* for human consumption.
*/
-static void format_note(struct notes_tree *t, const unsigned char *object_sha1,
+static void format_note(struct notes_tree *t, const struct object_id *object_oid,
struct strbuf *sb, const char *output_encoding, int raw)
{
static const char utf8[] = "utf-8";
- const unsigned char *sha1;
+ const struct object_id *oid;
char *msg, *msg_p;
unsigned long linelen, msglen;
enum object_type type;
if (!t->initialized)
init_notes(t, NULL, NULL, 0);
- sha1 = get_note(t, object_sha1);
- if (!sha1)
+ oid = get_note(t, object_oid);
+ if (!oid)
return;
- if (!(msg = read_sha1_file(sha1, &type, &msglen)) || type != OBJ_BLOB) {
+ if (!(msg = read_sha1_file(oid->hash, &type, &msglen)) || type != OBJ_BLOB) {
free(msg);
return;
}
free(msg);
}
-void format_display_notes(const unsigned char *object_sha1,
+void format_display_notes(const struct object_id *object_oid,
struct strbuf *sb, const char *output_encoding, int raw)
{
int i;
assert(display_notes_trees);
for (i = 0; display_notes_trees[i]; i++)
- format_note(display_notes_trees[i], object_sha1, sb,
+ format_note(display_notes_trees[i], object_oid, sb,
output_encoding, raw);
}
int copy_note(struct notes_tree *t,
- const unsigned char *from_obj, const unsigned char *to_obj,
+ const struct object_id *from_obj, const struct object_id *to_obj,
int force, combine_notes_fn combine_notes)
{
- const unsigned char *note = get_note(t, from_obj);
- const unsigned char *existing_note = get_note(t, to_obj);
+ const struct object_id *note = get_note(t, from_obj);
+ const struct object_id *existing_note = get_note(t, to_obj);
if (!force && existing_note)
return 1;
if (note)
return add_note(t, to_obj, note, combine_notes);
else if (existing_note)
- return add_note(t, to_obj, null_sha1, combine_notes);
+ return add_note(t, to_obj, &null_oid, combine_notes);
return 0;
}
void expand_loose_notes_ref(struct strbuf *sb)
{
- unsigned char object[20];
+ struct object_id object;
- if (get_sha1(sb->buf, object)) {
+ if (get_oid(sb->buf, &object)) {
/* fallback to expand_notes_ref */
expand_notes_ref(sb);
}