Skip to content

Commit a7e7eff

Browse files
jherlandgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0ab1faa commit a7e7eff

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

notes.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "cache.h"
2-
#include "commit.h"
32
#include "notes.h"
43
#include "utf8.h"
54
#include "strbuf.h"
@@ -24,10 +23,10 @@ struct int_node {
2423
/*
2524
* Leaf nodes come in two variants, note entries and subtree entries,
2625
* 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
2827
* value is the SHA1 of the note object.
2928
* 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
3130
* the prefix. The value is the SHA1 of the tree object containing the notes
3231
* subtree.
3332
*/
@@ -210,7 +209,7 @@ static void note_tree_insert(struct int_node *tree, unsigned char n,
210209
if (concatenate_notes(l->val_sha1,
211210
entry->val_sha1))
212211
die("failed to concatenate note %s "
213-
"into note %s for commit %s",
212+
"into note %s for object %s",
214213
sha1_to_hex(entry->val_sha1),
215214
sha1_to_hex(l->val_sha1),
216215
sha1_to_hex(l->key_sha1));
@@ -298,7 +297,7 @@ static int get_sha1_hex_segment(const char *hex, unsigned int hex_len,
298297
static void load_subtree(struct leaf_node *subtree, struct int_node *node,
299298
unsigned int n)
300299
{
301-
unsigned char commit_sha1[20];
300+
unsigned char object_sha1[20];
302301
unsigned int prefix_len;
303302
void *buf;
304303
struct tree_desc desc;
@@ -311,23 +310,23 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node,
311310

312311
prefix_len = subtree->key_sha1[19];
313312
assert(prefix_len * 2 >= n);
314-
memcpy(commit_sha1, subtree->key_sha1, prefix_len);
313+
memcpy(object_sha1, subtree->key_sha1, prefix_len);
315314
while (tree_entry(&desc, &entry)) {
316315
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);
318317
if (len < 0)
319318
continue; /* entry.path is not a SHA1 sum. Skip */
320319
len += prefix_len;
321320

322321
/*
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
325324
*/
326325
if (len <= 20) {
327326
unsigned char type = PTR_TYPE_NOTE;
328327
struct leaf_node *l = (struct leaf_node *)
329328
xcalloc(sizeof(struct leaf_node), 1);
330-
hashcpy(l->key_sha1, commit_sha1);
329+
hashcpy(l->key_sha1, object_sha1);
331330
hashcpy(l->val_sha1, entry.sha1);
332331
if (len < 20) {
333332
if (!S_ISDIR(entry.mode))
@@ -343,22 +342,22 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node,
343342

344343
static void initialize_notes(const char *notes_ref_name)
345344
{
346-
unsigned char sha1[20], commit_sha1[20];
345+
unsigned char sha1[20], object_sha1[20];
347346
unsigned mode;
348347
struct leaf_node root_tree;
349348

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))
352351
return;
353352

354353
hashclr(root_tree.key_sha1);
355354
hashcpy(root_tree.val_sha1, sha1);
356355
load_subtree(&root_tree, &root_node, 0);
357356
}
358357

359-
static unsigned char *lookup_notes(const unsigned char *commit_sha1)
358+
static unsigned char *lookup_notes(const unsigned char *object_sha1)
360359
{
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);
362361
if (found)
363362
return found->val_sha1;
364363
return NULL;
@@ -371,7 +370,7 @@ void free_notes(void)
371370
initialized = 0;
372371
}
373372

374-
void get_commit_notes(const struct commit *commit, struct strbuf *sb,
373+
void format_note(const unsigned char *object_sha1, struct strbuf *sb,
375374
const char *output_encoding, int flags)
376375
{
377376
static const char utf8[] = "utf-8";
@@ -390,7 +389,7 @@ void get_commit_notes(const struct commit *commit, struct strbuf *sb,
390389
initialized = 1;
391390
}
392391

393-
sha1 = lookup_notes(commit->object.sha1);
392+
sha1 = lookup_notes(object_sha1);
394393
if (!sha1)
395394
return;
396395

notes.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@
44
/* Free (and de-initialize) the internal notes tree structure */
55
void free_notes(void);
66

7+
/* Flags controlling how notes are formatted */
78
#define NOTES_SHOW_HEADER 1
89
#define NOTES_INDENT 2
910

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,
1120
const char *output_encoding, int flags);
1221

1322
#endif

pretty.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,8 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
775775
}
776776
return 0; /* unknown %g placeholder */
777777
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);
780780
return 1;
781781
}
782782

@@ -1095,8 +1095,8 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
10951095
strbuf_addch(sb, '\n');
10961096

10971097
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);
11001100

11011101
free(reencoded);
11021102
}

0 commit comments

Comments
 (0)