Notes API: get_commit_notes() -> format_note() + remove the commit restriction
There is really no reason why only commit objects can be annotated. By changing the struct commit parameter to get_commit_notes() into a sha1 we gain the ability to annotate any object type. To reflect this in the function naming as well, we rename get_commit_notes() to format_note(). This patch also fixes comments and variable names throughout notes.c as a consequence of the removal of the unnecessary 'commit' restriction. Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johan Herland committed
Feb 13, 2010 at 22:28 UTC
a7e7eff66206829f7752c565198dbe6f40ef72a0
3 files changed
+30
-22
notes.c
+16
-17
@@ -1,5 +1,4 @@
1
#include "cache.h"
2
-#include "commit.h"
2
#include "notes.h"
3
#include "utf8.h"
4
#include "strbuf.h"
@@ -24,10 +23,10 @@ struct int_node {
23
/*
24
* Leaf nodes come in two variants, note entries and subtree entries,
25
* distinguished by the LSb of the leaf node pointer (see above).
27
- * As a note entry, the key is the SHA1 of the referenced commit, and the
26
+ * As a note entry, the key is the SHA1 of the referenced object, and the
27
* value is the SHA1 of the note object.
28
* As a subtree entry, the key is the prefix SHA1 (w/trailing NULs) of the
30
- * referenced commit, using the last byte of the key to store the length of
29
+ * referenced object, using the last byte of the key to store the length of
30
* the prefix. The value is the SHA1 of the tree object containing the notes
31
* subtree.
32
*/
@@ -210,7 +209,7 @@ static void note_tree_insert(struct int_node *tree, unsigned char n,
209
if (concatenate_notes(l->val_sha1,
210
entry->val_sha1))
211
die("failed to concatenate note %s "
213
- "into note %s for commit %s",
212
+ "into note %s for object %s",
213
sha1_to_hex(entry->val_sha1),
214
sha1_to_hex(l->val_sha1),
215
sha1_to_hex(l->key_sha1));
@@ -298,7 +297,7 @@ static int get_sha1_hex_segment(const char *hex, unsigned int hex_len,
297
static void load_subtree(struct leaf_node *subtree, struct int_node *node,
298
unsigned int n)
299
{
301
- unsigned char commit_sha1[20];
300
+ unsigned char object_sha1[20];
301
unsigned int prefix_len;
302
void *buf;
303
struct tree_desc desc;
@@ -311,23 +310,23 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node,
310
311
prefix_len = subtree->key_sha1[19];
312
assert(prefix_len * 2 >= n);
314
- memcpy(commit_sha1, subtree->key_sha1, prefix_len);
313
+ memcpy(object_sha1, subtree->key_sha1, prefix_len);
314
while (tree_entry(&desc, &entry)) {
315
int len = get_sha1_hex_segment(entry.path, strlen(entry.path),
317
- commit_sha1 + prefix_len, 20 - prefix_len);
316
+ object_sha1 + prefix_len, 20 - prefix_len);
317
if (len < 0)
318
continue; /* entry.path is not a SHA1 sum. Skip */
319
len += prefix_len;
320
321
/*
323
- * If commit SHA1 is complete (len == 20), assume note object
324
- * If commit SHA1 is incomplete (len < 20), assume note subtree
322
+ * If object SHA1 is complete (len == 20), assume note object
323
+ * If object SHA1 is incomplete (len < 20), assume note subtree
324
*/
325
if (len <= 20) {
326
unsigned char type = PTR_TYPE_NOTE;
327
struct leaf_node *l = (struct leaf_node *)
328
xcalloc(sizeof(struct leaf_node), 1);
330
- hashcpy(l->key_sha1, commit_sha1);
329
+ hashcpy(l->key_sha1, object_sha1);
330
hashcpy(l->val_sha1, entry.sha1);
331
if (len < 20) {
332
if (!S_ISDIR(entry.mode))
@@ -343,12 +342,12 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node,
342
343
static void initialize_notes(const char *notes_ref_name)
344
{
346
- unsigned char sha1[20], commit_sha1[20];
345
+ unsigned char sha1[20], object_sha1[20];
346
unsigned mode;
347
struct leaf_node root_tree;
348
350
- if (!notes_ref_name || read_ref(notes_ref_name, commit_sha1) ||
351
- get_tree_entry(commit_sha1, "", sha1, &mode))
349
+ if (!notes_ref_name || read_ref(notes_ref_name, object_sha1) ||
350
+ get_tree_entry(object_sha1, "", sha1, &mode))
351
return;
352
353
hashclr(root_tree.key_sha1);
@@ -356,9 +355,9 @@ static void initialize_notes(const char *notes_ref_name)
355
load_subtree(&root_tree, &root_node, 0);
356
}
357
359
-static unsigned char *lookup_notes(const unsigned char *commit_sha1)
358
+static unsigned char *lookup_notes(const unsigned char *object_sha1)
359
{
361
- struct leaf_node *found = note_tree_find(&root_node, 0, commit_sha1);
360
+ struct leaf_node *found = note_tree_find(&root_node, 0, object_sha1);
361
if (found)
362
return found->val_sha1;
363
return NULL;
@@ -371,7 +370,7 @@ void free_notes(void)
370
initialized = 0;
371
}
372
374
-void get_commit_notes(const struct commit *commit, struct strbuf *sb,
373
+void format_note(const unsigned char *object_sha1, struct strbuf *sb,
374
const char *output_encoding, int flags)
375
{
376
static const char utf8[] = "utf-8";
@@ -390,7 +389,7 @@ void get_commit_notes(const struct commit *commit, struct strbuf *sb,
389
initialized = 1;
390
}
391
393
- sha1 = lookup_notes(commit->object.sha1);
392
+ sha1 = lookup_notes(object_sha1);
393
if (!sha1)
394
return;
395
notes.h
+10
-1
@@ -4,10 +4,19 @@
4
/* Free (and de-initialize) the internal notes tree structure */
5
void free_notes(void);
6
7
+/* Flags controlling how notes are formatted */
8
#define NOTES_SHOW_HEADER 1
9
#define NOTES_INDENT 2
10
10
-void get_commit_notes(const struct commit *commit, struct strbuf *sb,
11
+/*
12
+ * Fill the given strbuf with the notes associated with the given object.
13
+ *
14
+ * If the internal notes structure is not initialized, it will be auto-
15
+ * initialized to the default value (see documentation for init_notes() above).
16
+ *
17
+ * 'flags' is a bitwise combination of the above formatting flags.
18
+ */
19
+void format_note(const unsigned char *object_sha1, struct strbuf *sb,
20
const char *output_encoding, int flags);
21
22
#endif
pretty.c
+4
-4
@@ -775,8 +775,8 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
775
}
776
return 0; /* unknown %g placeholder */
777
case 'N':
778
- get_commit_notes(commit, sb, git_log_output_encoding ?
779
- git_log_output_encoding : git_commit_encoding, 0);
778
+ format_note(commit->object.sha1, sb, git_log_output_encoding ?
779
+ git_log_output_encoding : git_commit_encoding, 0);
780
return 1;
781
}
782
@@ -1095,8 +1095,8 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
1095
strbuf_addch(sb, '\n');
1096
1097
if (context->show_notes)
1098
- get_commit_notes(commit, sb, encoding,
1099
- NOTES_SHOW_HEADER | NOTES_INDENT);
1098
+ format_note(commit->object.sha1, sb, encoding,
1099
+ NOTES_SHOW_HEADER | NOTES_INDENT);
1100
1101
free(reencoded);
1102
}