summaryrefslogtreecommitdiff
path: root/notes.c
diff options
context:
space:
mode:
Diffstat (limited to 'notes.c')
-rw-r--r--notes.c138
1 files changed, 92 insertions, 46 deletions
diff --git a/notes.c b/notes.c
index 03e7d0c..fed1eda 100644
--- a/notes.c
+++ b/notes.c
@@ -1,9 +1,10 @@
-#include "cache.h"
+#include "git-compat-util.h"
#include "config.h"
+#include "environment.h"
+#include "hex.h"
#include "notes.h"
-#include "object-store.h"
-#include "blob.h"
-#include "tree.h"
+#include "object-name.h"
+#include "object-store-ll.h"
#include "utf8.h"
#include "strbuf.h"
#include "tree-walk.h"
@@ -352,7 +353,7 @@ static void add_non_note(struct notes_tree *t, char *path,
n->next = NULL;
n->path = path;
n->mode = mode;
- hashcpy(n->oid.hash, sha1);
+ oidread(&n->oid, sha1);
t->prev_non_note = n;
if (!t->first_non_note) {
@@ -452,9 +453,11 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
goto handle_non_note;
}
- l = xcalloc(1, sizeof(*l));
+ CALLOC_ARRAY(l, 1);
oidcpy(&l->key_oid, &object_oid);
oidcpy(&l->val_oid, &entry.oid);
+ oid_set_algo(&l->key_oid, the_hash_algo);
+ oid_set_algo(&l->val_oid, the_hash_algo);
if (note_tree_insert(t, node, n, l, type,
combine_notes_concatenate))
die("Failed to load %s %s into notes tree "
@@ -484,6 +487,7 @@ handle_non_note:
strbuf_addch(&non_note_path, '/');
}
strbuf_addstr(&non_note_path, entry.path);
+ oid_set_algo(&entry.oid, the_hash_algo);
add_non_note(t, strbuf_detach(&non_note_path, NULL),
entry.mode, entry.oid.hash);
}
@@ -576,16 +580,16 @@ redo:
* the note tree that have not yet been explored. There
* is a direct relationship between subtree entries at
* level 'n' in the tree, and the 'fanout' variable:
- * Subtree entries at level 'n <= 2 * fanout' should be
+ * Subtree entries at level 'n < 2 * fanout' should be
* preserved, since they correspond exactly to a fanout
* directory in the on-disk structure. However, subtree
- * entries at level 'n > 2 * fanout' should NOT be
+ * entries at level 'n >= 2 * fanout' should NOT be
* preserved, but rather consolidated into the above
* notes tree level. We achieve this by unconditionally
* unpacking subtree entries that exist below the
* threshold level at 'n = 2 * fanout'.
*/
- if (n <= 2 * fanout &&
+ if (n < 2 * fanout &&
flags & FOR_EACH_NOTE_YIELD_SUBTREES) {
/* invoke callback with subtree */
unsigned int path_len =
@@ -602,7 +606,7 @@ redo:
path,
cb_data);
}
- if (n > fanout * 2 ||
+ if (n >= 2 * fanout ||
!(flags & FOR_EACH_NOTE_DONT_UNPACK_SUBTREES)) {
/* unpack subtree and resume traversal */
tree->a[i] = NULL;
@@ -672,7 +676,7 @@ static int tree_write_stack_finish_subtree(struct tree_write_stack *tws)
ret = tree_write_stack_finish_subtree(n);
if (ret)
return ret;
- ret = write_object_file(n->buf.buf, n->buf.len, tree_type, &s);
+ ret = write_object_file(n->buf.buf, n->buf.len, OBJ_TREE, &s);
if (ret)
return ret;
strbuf_release(&n->buf);
@@ -723,13 +727,15 @@ static int write_each_note_helper(struct tree_write_stack *tws,
struct write_each_note_data {
struct tree_write_stack *root;
- struct non_note *next_non_note;
+ struct non_note **nn_list;
+ struct non_note *nn_prev;
};
static int write_each_non_note_until(const char *note_path,
struct write_each_note_data *d)
{
- struct non_note *n = d->next_non_note;
+ struct non_note *p = d->nn_prev;
+ struct non_note *n = p ? p->next : *d->nn_list;
int cmp = 0, ret;
while (n && (!note_path || (cmp = strcmp(n->path, note_path)) <= 0)) {
if (note_path && cmp == 0)
@@ -740,13 +746,14 @@ static int write_each_non_note_until(const char *note_path,
if (ret)
return ret;
}
+ p = n;
n = n->next;
}
- d->next_non_note = n;
+ d->nn_prev = p;
return 0;
}
-static int write_each_note(const struct object_id *object_oid,
+static int write_each_note(const struct object_id *object_oid UNUSED,
const struct object_id *note_oid, char *note_path,
void *cb_data)
{
@@ -774,13 +781,14 @@ struct note_delete_list {
};
static int prune_notes_helper(const struct object_id *object_oid,
- const struct object_id *note_oid, char *note_path,
- void *cb_data)
+ const struct object_id *note_oid UNUSED,
+ char *note_path UNUSED,
+ void *cb_data)
{
struct note_delete_list **l = (struct note_delete_list **) cb_data;
struct note_delete_list *n;
- if (has_object_file(object_oid))
+ if (repo_has_object_file(the_repository, object_oid))
return 0; /* nothing to do for this note */
/* failed to find object => prune this note */
@@ -801,13 +809,15 @@ int combine_notes_concatenate(struct object_id *cur_oid,
/* read in both note blob objects */
if (!is_null_oid(new_oid))
- new_msg = read_object_file(new_oid, &new_type, &new_len);
+ new_msg = repo_read_object_file(the_repository, new_oid,
+ &new_type, &new_len);
if (!new_msg || !new_len || new_type != OBJ_BLOB) {
free(new_msg);
return 0;
}
if (!is_null_oid(cur_oid))
- cur_msg = read_object_file(cur_oid, &cur_type, &cur_len);
+ cur_msg = repo_read_object_file(the_repository, cur_oid,
+ &cur_type, &cur_len);
if (!cur_msg || !cur_len || cur_type != OBJ_BLOB) {
free(cur_msg);
free(new_msg);
@@ -830,7 +840,7 @@ int combine_notes_concatenate(struct object_id *cur_oid,
free(new_msg);
/* create a new blob object from buf */
- ret = write_object_file(buf, buf_len, blob_type, cur_oid);
+ ret = write_object_file(buf, buf_len, OBJ_BLOB, cur_oid);
free(buf);
return ret;
}
@@ -842,8 +852,8 @@ int combine_notes_overwrite(struct object_id *cur_oid,
return 0;
}
-int combine_notes_ignore(struct object_id *cur_oid,
- const struct object_id *new_oid)
+int combine_notes_ignore(struct object_id *cur_oid UNUSED,
+ const struct object_id *new_oid UNUSED)
{
return 0;
}
@@ -863,7 +873,7 @@ static int string_list_add_note_lines(struct string_list *list,
return 0;
/* read_sha1_file NUL-terminates */
- data = read_object_file(oid, &t, &len);
+ data = repo_read_object_file(the_repository, oid, &t, &len);
if (t != OBJ_BLOB || !data || !len) {
free(data);
return t != OBJ_BLOB || !data;
@@ -910,7 +920,7 @@ int combine_notes_cat_sort_uniq(struct object_id *cur_oid,
string_list_join_lines_helper, &buf))
goto out;
- ret = write_object_file(buf.buf, buf.len, blob_type, cur_oid);
+ ret = write_object_file(buf.buf, buf.len, OBJ_BLOB, cur_oid);
out:
strbuf_release(&buf);
@@ -918,8 +928,9 @@ out:
return ret;
}
-static int string_list_add_one_ref(const char *refname, const struct object_id *oid,
- int flag, void *cb)
+static int string_list_add_one_ref(const char *refname,
+ const struct object_id *oid UNUSED,
+ int flag UNUSED, void *cb)
{
struct string_list *refs = cb;
if (!unsorted_string_list_has_string(refs, refname))
@@ -937,7 +948,7 @@ void string_list_add_refs_by_glob(struct string_list *list, const char *glob)
for_each_glob_ref(string_list_add_one_ref, glob, list);
} else {
struct object_id oid;
- if (get_oid(glob, &oid))
+ if (repo_get_oid(the_repository, glob, &oid))
warning("notes ref %s is invalid", glob);
if (!unsorted_string_list_has_string(list, glob))
string_list_append(list, glob);
@@ -951,7 +962,7 @@ void string_list_add_refs_from_colon_sep(struct string_list *list,
char *globs_copy = xstrdup(globs);
int i;
- string_list_split_in_place(&split, globs_copy, ':', -1);
+ string_list_split_in_place(&split, globs_copy, ":", -1);
string_list_remove_empty_items(&split, 0);
for (i = 0; i < split.nr; i++)
@@ -961,13 +972,15 @@ void string_list_add_refs_from_colon_sep(struct string_list *list,
free(globs_copy);
}
-static int notes_display_config(const char *k, const char *v, void *cb)
+static int notes_display_config(const char *k, const char *v,
+ const struct config_context *ctx UNUSED,
+ void *cb)
{
int *load_refs = cb;
if (*load_refs && !strcmp(k, "notes.displayref")) {
if (!v)
- config_error_nonbool(k);
+ return config_error_nonbool(k);
string_list_add_refs_by_glob(&display_notes_refs, v);
}
@@ -999,6 +1012,7 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
if (!notes_ref)
notes_ref = default_notes_ref();
+ update_ref_namespace(NAMESPACE_NOTES, xstrdup(notes_ref));
if (!combine_notes)
combine_notes = combine_notes_concatenate;
@@ -1006,14 +1020,14 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
t->root = (struct int_node *) xcalloc(1, sizeof(struct int_node));
t->first_non_note = NULL;
t->prev_non_note = NULL;
- t->ref = xstrdup_or_null(notes_ref);
+ t->ref = xstrdup(notes_ref);
t->update_ref = (flags & NOTES_INIT_WRITABLE) ? t->ref : NULL;
t->combine_notes = combine_notes;
t->initialized = 1;
t->dirty = 0;
- if (flags & NOTES_INIT_EMPTY || !notes_ref ||
- get_oid_treeish(notes_ref, &object_oid))
+ if (flags & NOTES_INIT_EMPTY ||
+ repo_get_oid_treeish(the_repository, notes_ref, &object_oid))
return;
if (flags & NOTES_INIT_WRITABLE && read_ref(notes_ref, &object_oid))
die("Cannot use notes ref %s", notes_ref);
@@ -1043,6 +1057,39 @@ struct notes_tree **load_notes_trees(struct string_list *refs, int flags)
void init_display_notes(struct display_notes_opt *opt)
{
+ memset(opt, 0, sizeof(*opt));
+ opt->use_default_notes = -1;
+}
+
+void enable_default_display_notes(struct display_notes_opt *opt, int *show_notes)
+{
+ opt->use_default_notes = 1;
+ *show_notes = 1;
+}
+
+void enable_ref_display_notes(struct display_notes_opt *opt, int *show_notes,
+ const char *ref) {
+ struct strbuf buf = STRBUF_INIT;
+ strbuf_addstr(&buf, ref);
+ expand_notes_ref(&buf);
+ string_list_append(&opt->extra_notes_refs,
+ strbuf_detach(&buf, NULL));
+ *show_notes = 1;
+}
+
+void disable_display_notes(struct display_notes_opt *opt, int *show_notes)
+{
+ opt->use_default_notes = -1;
+ /* we have been strdup'ing ourselves, so trick
+ * string_list into free()ing strings */
+ opt->extra_notes_refs.strdup_strings = 1;
+ string_list_clear(&opt->extra_notes_refs, 0);
+ opt->extra_notes_refs.strdup_strings = 0;
+ *show_notes = 0;
+}
+
+void load_display_notes(struct display_notes_opt *opt)
+{
char *display_ref_env;
int load_config_refs = 0;
display_notes_refs.strdup_strings = 1;
@@ -1098,7 +1145,7 @@ int remove_note(struct notes_tree *t, const unsigned char *object_sha1)
if (!t)
t = &default_notes_tree;
assert(t->initialized);
- hashcpy(l.key_oid.hash, object_sha1);
+ oidread(&l.key_oid, object_sha1);
oidclr(&l.val_oid);
note_tree_remove(t, t->root, 0, &l);
if (is_null_oid(&l.val_oid)) /* no note was removed */
@@ -1144,7 +1191,8 @@ int write_notes_tree(struct notes_tree *t, struct object_id *result)
strbuf_init(&root.buf, 256 * (32 + the_hash_algo->hexsz)); /* assume 256 entries */
root.path[0] = root.path[1] = '\0';
cb_data.root = &root;
- cb_data.next_non_note = t->first_non_note;
+ cb_data.nn_list = &(t->first_non_note);
+ cb_data.nn_prev = NULL;
/* Write tree objects representing current notes tree */
flags = FOR_EACH_NOTE_DONT_UNPACK_SUBTREES |
@@ -1152,7 +1200,7 @@ int write_notes_tree(struct notes_tree *t, struct object_id *result)
ret = for_each_note(t, flags, write_each_note, &cb_data) ||
write_each_non_note_until(NULL, &cb_data) ||
tree_write_stack_finish_subtree(&root) ||
- write_object_file(root.buf.buf, root.buf.len, tree_type, result);
+ write_object_file(root.buf.buf, root.buf.len, OBJ_TREE, result);
strbuf_release(&root.buf);
return ret;
}
@@ -1222,7 +1270,7 @@ static void format_note(struct notes_tree *t, const struct object_id *object_oid
if (!oid)
return;
- if (!(msg = read_object_file(oid, &type, &msglen)) || type != OBJ_BLOB) {
+ if (!(msg = repo_read_object_file(the_repository, oid, &type, &msglen)) || type != OBJ_BLOB) {
free(msg);
return;
}
@@ -1246,10 +1294,8 @@ static void format_note(struct notes_tree *t, const struct object_id *object_oid
if (!ref || !strcmp(ref, GIT_NOTES_DEFAULT_REF)) {
strbuf_addstr(sb, "\nNotes:\n");
} else {
- if (starts_with(ref, "refs/"))
- ref += 5;
- if (starts_with(ref, "notes/"))
- ref += 6;
+ skip_prefix(ref, "refs/", &ref);
+ skip_prefix(ref, "notes/", &ref);
strbuf_addf(sb, "\nNotes (%s):\n", ref);
}
}
@@ -1289,7 +1335,7 @@ int copy_note(struct notes_tree *t,
if (note)
return add_note(t, to_obj, note, combine_notes);
else if (existing_note)
- return add_note(t, to_obj, &null_oid, combine_notes);
+ return add_note(t, to_obj, null_oid(), combine_notes);
return 0;
}
@@ -1299,16 +1345,16 @@ void expand_notes_ref(struct strbuf *sb)
if (starts_with(sb->buf, "refs/notes/"))
return; /* we're happy */
else if (starts_with(sb->buf, "notes/"))
- strbuf_insert(sb, 0, "refs/", 5);
+ strbuf_insertstr(sb, 0, "refs/");
else
- strbuf_insert(sb, 0, "refs/notes/", 11);
+ strbuf_insertstr(sb, 0, "refs/notes/");
}
void expand_loose_notes_ref(struct strbuf *sb)
{
struct object_id object;
- if (get_oid(sb->buf, &object)) {
+ if (repo_get_oid(the_repository, sb->buf, &object)) {
/* fallback to expand_notes_ref */
expand_notes_ref(sb);
}