]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Introduce commit notes
authorJohannes Schindelin <Johannes.Schindelin@gmx.de>
Fri, 9 Oct 2009 10:21:57 +0000 (12:21 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 20 Oct 2009 01:59:42 +0000 (18:59 -0700)
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.

This patch has been improved by the following contributions:
- Thomas Rast: fix core.notesRef documentation
- Tor Arne Vestbø: fix printing of multi-line notes
- Alex Riesen: Using char array instead of char pointer costs less BSS
- Johan Herland: Plug leak when msg is good, but msglen or type causes return

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Thomas Rast <trast@student.ethz.ch>
Signed-off-by: Tor Arne Vestbø <tavestbo@trolltech.com>
Signed-off-by: Johan Herland <johan@herland.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
get_commit_notes(): Plug memory leak when 'if' triggers, but not because of read_sha1_file() failure

Documentation/config.txt
Makefile
cache.h
commit.c
config.c
environment.c
notes.c [new file with mode: 0644]
notes.h [new file with mode: 0644]
pretty.c

index cd1781498eb92d7dd0d3648a8fe188fc75a6df8c..57d64e4d990bc60d9fd94e9ef55826bb2b2e9643 100644 (file)
@@ -454,6 +454,19 @@ On some file system/operating system combinations, this is unreliable.
 Set this config setting to 'rename' there; However, This will remove the
 check that makes sure that existing object files will not get overwritten.
 
+core.notesRef::
+       When showing commit messages, also show notes which are stored in
+       the given ref.  This ref is expected to contain files named
+       after the full SHA-1 of the commit they annotate.
++
+If such a file exists in the given ref, the referenced blob is read, and
+appended to the commit message, separated by a "Notes:" line.  If the
+given ref itself does not exist, it is not an error, but means that no
+notes should be printed.
++
+This setting defaults to "refs/notes/commits", and can be overridden by
+the `GIT_NOTES_REF` environment variable.
+
 add.ignore-errors::
        Tells 'git-add' to continue adding files when some files cannot be
        added due to indexing errors. Equivalent to the '--ignore-errors'
index fea237bc80978c1011793ee0cbd8398b2e9f773b..9a6a729868d89166f106b4eeb7b8e2b69c08c625 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -432,6 +432,7 @@ LIB_H += ll-merge.h
 LIB_H += log-tree.h
 LIB_H += mailmap.h
 LIB_H += merge-recursive.h
+LIB_H += notes.h
 LIB_H += object.h
 LIB_H += pack.h
 LIB_H += pack-refs.h
@@ -516,6 +517,7 @@ LIB_OBJS += match-trees.o
 LIB_OBJS += merge-file.o
 LIB_OBJS += merge-recursive.o
 LIB_OBJS += name-hash.o
+LIB_OBJS += notes.o
 LIB_OBJS += object.o
 LIB_OBJS += pack-check.o
 LIB_OBJS += pack-refs.o
diff --git a/cache.h b/cache.h
index a5eeead1e275523fbc7bc192a836193720370991..4f0ddeca11b4bdfe9ee8ee5171e19094acf42a05 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -372,6 +372,8 @@ static inline enum object_type object_type(unsigned int mode)
 #define GITATTRIBUTES_FILE ".gitattributes"
 #define INFOATTRIBUTES_FILE "info/attributes"
 #define ATTRIBUTE_MACRO_PREFIX "[attr]"
+#define GIT_NOTES_REF_ENVIRONMENT "GIT_NOTES_REF"
+#define GIT_NOTES_DEFAULT_REF "refs/notes/commits"
 
 extern int is_bare_repository_cfg;
 extern int is_bare_repository(void);
@@ -567,6 +569,8 @@ enum object_creation_mode {
 
 extern enum object_creation_mode object_creation_mode;
 
+extern char *notes_ref_name;
+
 extern int grafts_replace_parents;
 
 #define GIT_REPO_VERSION 0
index fedbd5e5267ec4beaf1f245a879918a2685c10b9..5ade8edf81cb887f425b1e15b4f02a2e25af9a02 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -5,6 +5,7 @@
 #include "utf8.h"
 #include "diff.h"
 #include "revision.h"
+#include "notes.h"
 
 int save_commit_buffer = 1;
 
index c64406113686e8886c0719206ce57d73fee13b29..51f22088e7e20c6928986cc9597a1ebd5a10807e 100644 (file)
--- a/config.c
+++ b/config.c
@@ -467,6 +467,11 @@ static int git_default_core_config(const char *var, const char *value)
                return 0;
        }
 
+       if (!strcmp(var, "core.notesref")) {
+               notes_ref_name = xstrdup(value);
+               return 0;
+       }
+
        if (!strcmp(var, "core.pager"))
                return git_config_string(&pager_program, var, value);
 
index 5de683784039f20b55f58ef36cb04a11f6a10610..571ab56b7e58e2db9e1182de7e78fd2d7596e872 100644 (file)
@@ -49,6 +49,7 @@ enum push_default_type push_default = PUSH_DEFAULT_MATCHING;
 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
 #endif
 enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
+char *notes_ref_name;
 int grafts_replace_parents = 1;
 
 /* Parallel index stat data preload? */
diff --git a/notes.c b/notes.c
new file mode 100644 (file)
index 0000000..66379ff
--- /dev/null
+++ b/notes.c
@@ -0,0 +1,70 @@
+#include "cache.h"
+#include "commit.h"
+#include "notes.h"
+#include "refs.h"
+#include "utf8.h"
+#include "strbuf.h"
+
+static int initialized;
+
+void get_commit_notes(const struct commit *commit, struct strbuf *sb,
+               const char *output_encoding)
+{
+       static const char utf8[] = "utf-8";
+       struct strbuf name = STRBUF_INIT;
+       unsigned char sha1[20];
+       char *msg, *msg_p;
+       unsigned long linelen, msglen;
+       enum object_type type;
+
+       if (!initialized) {
+               const char *env = getenv(GIT_NOTES_REF_ENVIRONMENT);
+               if (env)
+                       notes_ref_name = getenv(GIT_NOTES_REF_ENVIRONMENT);
+               else if (!notes_ref_name)
+                       notes_ref_name = GIT_NOTES_DEFAULT_REF;
+               if (notes_ref_name && read_ref(notes_ref_name, sha1))
+                       notes_ref_name = NULL;
+               initialized = 1;
+       }
+
+       if (!notes_ref_name)
+               return;
+
+       strbuf_addf(&name, "%s:%s", notes_ref_name,
+                       sha1_to_hex(commit->object.sha1));
+       if (get_sha1(name.buf, sha1))
+               return;
+
+       if (!(msg = read_sha1_file(sha1, &type, &msglen)) || !msglen ||
+                       type != OBJ_BLOB) {
+               free(msg);
+               return;
+       }
+
+       if (output_encoding && *output_encoding &&
+                       strcmp(utf8, output_encoding)) {
+               char *reencoded = reencode_string(msg, output_encoding, utf8);
+               if (reencoded) {
+                       free(msg);
+                       msg = reencoded;
+                       msglen = strlen(msg);
+               }
+       }
+
+       /* we will end the annotation by a newline anyway */
+       if (msglen && msg[msglen - 1] == '\n')
+               msglen--;
+
+       strbuf_addstr(sb, "\nNotes:\n");
+
+       for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) {
+               linelen = strchrnul(msg_p, '\n') - msg_p;
+
+               strbuf_addstr(sb, "    ");
+               strbuf_add(sb, msg_p, linelen);
+               strbuf_addch(sb, '\n');
+       }
+
+       free(msg);
+}
diff --git a/notes.h b/notes.h
new file mode 100644 (file)
index 0000000..79d21b6
--- /dev/null
+++ b/notes.h
@@ -0,0 +1,7 @@
+#ifndef NOTES_H
+#define NOTES_H
+
+void get_commit_notes(const struct commit *commit, struct strbuf *sb,
+               const char *output_encoding);
+
+#endif
index f5983f8baa98b69338707b4220e64012ef4e5d11..e25db81eaa531b97141c2dc3e9246ff3010924ed 100644 (file)
--- a/pretty.c
+++ b/pretty.c
@@ -6,6 +6,7 @@
 #include "string-list.h"
 #include "mailmap.h"
 #include "log-tree.h"
+#include "notes.h"
 #include "color.h"
 
 static char *user_format;
@@ -975,5 +976,9 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
         */
        if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
                strbuf_addch(sb, '\n');
+
+       if (fmt != CMIT_FMT_ONELINE)
+               get_commit_notes(commit, sb, encoding);
+
        free(reencoded);
 }