@matheusbsbs / matheus-bagliano / commits / 879ef2485d
Fork 0

Introduce commit notes

Commit notes are blobs which are shown together with the commit message. These blobs are taken from the notes ref, which you can configure by the config variable core.notesRef, which in turn can be overridden by the environment variable GIT_NOTES_REF. The notes ref is a branch which contains "files" whose names are the names of the corresponding commits (i.e. the SHA-1). The rationale for putting this information into a ref is this: we want to be able to fetch and possibly union-merge the notes, maybe even look at the date when a note was introduced, and we want to store them efficiently together with the other objects. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Dec 20, 2008 at 13:05 UTC 879ef2485d6ced20845ca626ecb45a9b65aa3a70
9 files changed +107
Documentation/config.txt
+15
@@ -422,6 +422,21 @@ relatively high IO latencies. With this set to 'true', git will do the
422 index comparison to the filesystem data in parallel, allowing
423 overlapping IO's.
424
425 +core.notesRef::
426 + When showing commit messages, also show notes which are stored in
427 + the given ref. This ref is expected to contain paths of the form
428 + ??/*, where the directory name consists of the first two
429 + characters of the commit name, and the base name consists of
430 + the remaining 38 characters.
431 ++
432 +If such a path exists in the given ref, the referenced blob is read, and
433 +appended to the commit message, separated by a "Notes:" line. If the
434 +given ref itself does not exist, it is not an error, but means that no
435 +notes should be print.
436 ++
437 +This setting defaults to "refs/notes/commits", and can be overridden by
438 +the `GIT_NOTES_REF` environment variable.
439 +
440 alias.*::
441 Command aliases for the linkgit:git[1] command wrapper - e.g.
442 after defining "alias.last = cat-file commit HEAD", the invocation
Makefile
+2
@@ -370,6 +370,7 @@ LIB_H += ll-merge.h
370 LIB_H += log-tree.h
371 LIB_H += mailmap.h
372 LIB_H += merge-recursive.h
373 +LIB_H += notes.h
374 LIB_H += object.h
375 LIB_H += pack.h
376 LIB_H += pack-refs.h
@@ -451,6 +452,7 @@ LIB_OBJS += match-trees.o
452 LIB_OBJS += merge-file.o
453 LIB_OBJS += merge-recursive.o
454 LIB_OBJS += name-hash.o
455 +LIB_OBJS += notes.o
456 LIB_OBJS += object.o
457 LIB_OBJS += pack-check.o
458 LIB_OBJS += pack-refs.o
cache.h
+3
@@ -367,6 +367,8 @@ static inline enum object_type object_type(unsigned int mode)
367 #define GITATTRIBUTES_FILE ".gitattributes"
368 #define INFOATTRIBUTES_FILE "info/attributes"
369 #define ATTRIBUTE_MACRO_PREFIX "[attr]"
370 +#define GIT_NOTES_REF_ENVIRONMENT "GIT_NOTES_REF"
371 +#define GIT_NOTES_DEFAULT_REF "refs/notes/commits"
372
373 extern int is_bare_repository_cfg;
374 extern int is_bare_repository(void);
@@ -538,6 +540,7 @@ enum rebase_setup_type {
540
541 extern enum branch_track git_branch_track;
542 extern enum rebase_setup_type autorebase;
543 +extern char *notes_ref_name;
544
545 #define GIT_REPO_VERSION 0
546 extern int repository_format_version;
commit.c
+1
@@ -5,6 +5,7 @@
5 #include "utf8.h"
6 #include "diff.h"
7 #include "revision.h"
8 +#include "notes.h"
9
10 int save_commit_buffer = 1;
11
config.c
+5
@@ -469,6 +469,11 @@ static int git_default_core_config(const char *var, const char *value)
469 return 0;
470 }
471
472 + if (!strcmp(var, "core.notesref")) {
473 + notes_ref_name = xstrdup(value);
474 + return 0;
475 + }
476 +
477 if (!strcmp(var, "core.pager"))
478 return git_config_string(&pager_program, var, value);
479
environment.c
+1
@@ -45,6 +45,7 @@ enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
45
46 /* Parallel index stat data preload? */
47 int core_preload_index = 0;
48 +char *notes_ref_name;
49
50 /* This is set by setup_git_dir_gently() and/or git_default_config() */
51 char *git_work_tree_cfg;
notes.c new
+68
@@ -0,0 +1,68 @@
1 +#include "cache.h"
2 +#include "commit.h"
3 +#include "notes.h"
4 +#include "refs.h"
5 +#include "utf8.h"
6 +#include "strbuf.h"
7 +
8 +static int initialized;
9 +
10 +void get_commit_notes(const struct commit *commit, struct strbuf *sb,
11 + const char *output_encoding)
12 +{
13 + static const char *utf8 = "utf-8";
14 + struct strbuf name = STRBUF_INIT;
15 + const char *hex;
16 + unsigned char sha1[20];
17 + char *msg;
18 + unsigned long msgoffset, msglen;
19 + enum object_type type;
20 +
21 + if (!initialized) {
22 + const char *env = getenv(GIT_NOTES_REF_ENVIRONMENT);
23 + if (env)
24 + notes_ref_name = getenv(GIT_NOTES_REF_ENVIRONMENT);
25 + else if (!notes_ref_name)
26 + notes_ref_name = GIT_NOTES_DEFAULT_REF;
27 + if (notes_ref_name && read_ref(notes_ref_name, sha1))
28 + notes_ref_name = NULL;
29 + initialized = 1;
30 + }
31 +
32 + if (!notes_ref_name)
33 + return;
34 +
35 + strbuf_addf(&name, "%s:%s", notes_ref_name,
36 + sha1_to_hex(commit->object.sha1));
37 + if (get_sha1(name.buf, sha1))
38 + return;
39 +
40 + if (!(msg = read_sha1_file(sha1, &type, &msglen)) || !msglen ||
41 + type != OBJ_BLOB)
42 + return;
43 +
44 + if (output_encoding && *output_encoding &&
45 + strcmp(utf8, output_encoding)) {
46 + char *reencoded = reencode_string(msg, output_encoding, utf8);
47 + if (reencoded) {
48 + free(msg);
49 + msg = reencoded;
50 + msglen = strlen(msg);
51 + }
52 + }
53 +
54 + /* we will end the annotation by a newline anyway */
55 + if (msglen && msg[msglen - 1] == '\n')
56 + msglen--;
57 +
58 + strbuf_addstr(sb, "\nNotes:\n");
59 +
60 + for (msgoffset = 0; msgoffset < msglen;) {
61 + int linelen = strchrnul(msg, '\n') - msg;
62 +
63 + strbuf_addstr(sb, " ");
64 + strbuf_add(sb, msg + msgoffset, linelen);
65 + msgoffset += linelen;
66 + }
67 + free(msg);
68 +}
notes.h new
+7
@@ -0,0 +1,7 @@
1 +#ifndef NOTES_H
2 +#define NOTES_H
3 +
4 +void get_commit_notes(const struct commit *commit, struct strbuf *sb,
5 + const char *output_encoding);
6 +
7 +#endif
pretty.c
+5
@@ -6,6 +6,7 @@
6 #include "string-list.h"
7 #include "mailmap.h"
8 #include "log-tree.h"
9 +#include "notes.h"
10
11 static char *user_format;
12
@@ -881,5 +882,9 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
882 */
883 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
884 strbuf_addch(sb, '\n');
885 +
886 + if (fmt != CMIT_FMT_ONELINE)
887 + get_commit_notes(commit, sb, encoding);
888 +
889 free(reencoded);
890 }