Introduce commit notes
[git/kirr.git] / notes.c
blob66379ffd22ea2d08f6abc070ddf02a9c1bf45ab5
1 #include "cache.h"
2 #include "commit.h"
3 #include "notes.h"
4 #include "refs.h"
5 #include "utf8.h"
6 #include "strbuf.h"
8 static int initialized;
10 void get_commit_notes(const struct commit *commit, struct strbuf *sb,
11 const char *output_encoding)
13 static const char utf8[] = "utf-8";
14 struct strbuf name = STRBUF_INIT;
15 unsigned char sha1[20];
16 char *msg, *msg_p;
17 unsigned long linelen, msglen;
18 enum object_type type;
20 if (!initialized) {
21 const char *env = getenv(GIT_NOTES_REF_ENVIRONMENT);
22 if (env)
23 notes_ref_name = getenv(GIT_NOTES_REF_ENVIRONMENT);
24 else if (!notes_ref_name)
25 notes_ref_name = GIT_NOTES_DEFAULT_REF;
26 if (notes_ref_name && read_ref(notes_ref_name, sha1))
27 notes_ref_name = NULL;
28 initialized = 1;
31 if (!notes_ref_name)
32 return;
34 strbuf_addf(&name, "%s:%s", notes_ref_name,
35 sha1_to_hex(commit->object.sha1));
36 if (get_sha1(name.buf, sha1))
37 return;
39 if (!(msg = read_sha1_file(sha1, &type, &msglen)) || !msglen ||
40 type != OBJ_BLOB) {
41 free(msg);
42 return;
45 if (output_encoding && *output_encoding &&
46 strcmp(utf8, output_encoding)) {
47 char *reencoded = reencode_string(msg, output_encoding, utf8);
48 if (reencoded) {
49 free(msg);
50 msg = reencoded;
51 msglen = strlen(msg);
55 /* we will end the annotation by a newline anyway */
56 if (msglen && msg[msglen - 1] == '\n')
57 msglen--;
59 strbuf_addstr(sb, "\nNotes:\n");
61 for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) {
62 linelen = strchrnul(msg_p, '\n') - msg_p;
64 strbuf_addstr(sb, " ");
65 strbuf_add(sb, msg_p, linelen);
66 strbuf_addch(sb, '\n');
69 free(msg);