Introduce commit notes · wolfpython/git@a97a746 · GitHub
Skip to content

Commit a97a746

Browse files
dschogitster
authored andcommitted
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. 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
1 parent 78d553b commit a97a746

9 files changed

Lines changed: 108 additions & 0 deletions

File tree

Documentation/config.txt

Lines changed: 13 additions & 0 deletions

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ LIB_H += ll-merge.h
432432
LIB_H += log-tree.h
433433
LIB_H += mailmap.h
434434
LIB_H += merge-recursive.h
435+
LIB_H += notes.h
435436
LIB_H += object.h
436437
LIB_H += pack.h
437438
LIB_H += pack-refs.h
@@ -516,6 +517,7 @@ LIB_OBJS += match-trees.o
516517
LIB_OBJS += merge-file.o
517518
LIB_OBJS += merge-recursive.o
518519
LIB_OBJS += name-hash.o
520+
LIB_OBJS += notes.o
519521
LIB_OBJS += object.o
520522
LIB_OBJS += pack-check.o
521523
LIB_OBJS += pack-refs.o

cache.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ static inline enum object_type object_type(unsigned int mode)
372372
#define GITATTRIBUTES_FILE ".gitattributes"
373373
#define INFOATTRIBUTES_FILE "info/attributes"
374374
#define ATTRIBUTE_MACRO_PREFIX "[attr]"
375+
#define GIT_NOTES_REF_ENVIRONMENT "GIT_NOTES_REF"
376+
#define GIT_NOTES_DEFAULT_REF "refs/notes/commits"
375377

376378
extern int is_bare_repository_cfg;
377379
extern int is_bare_repository(void);
@@ -567,6 +569,8 @@ enum object_creation_mode {
567569

568570
extern enum object_creation_mode object_creation_mode;
569571

572+
extern char *notes_ref_name;
573+
570574
extern int grafts_replace_parents;
571575

572576
#define GIT_REPO_VERSION 0

commit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "utf8.h"
66
#include "diff.h"
77
#include "revision.h"
8+
#include "notes.h"
89

910
int save_commit_buffer = 1;
1011

config.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,11 @@ static int git_default_core_config(const char *var, const char *value)
467467
return 0;
468468
}
469469

470+
if (!strcmp(var, "core.notesref")) {
471+
notes_ref_name = xstrdup(value);
472+
return 0;
473+
}
474+
470475
if (!strcmp(var, "core.pager"))
471476
return git_config_string(&pager_program, var, value);
472477

environment.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ enum push_default_type push_default = PUSH_DEFAULT_MATCHING;
4949
#define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
5050
#endif
5151
enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
52+
char *notes_ref_name;
5253
int grafts_replace_parents = 1;
5354

5455
/* Parallel index stat data preload? */

notes.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
unsigned char sha1[20];
16+
char *msg, *msg_p;
17+
unsigned long linelen, msglen;
18+
enum object_type type;
19+
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;
29+
}
30+
31+
if (!notes_ref_name)
32+
return;
33+
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;
38+
39+
if (!(msg = read_sha1_file(sha1, &type, &msglen)) || !msglen ||
40+
type != OBJ_BLOB) {
41+
free(msg);
42+
return;
43+
}
44+
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);
52+
}
53+
}
54+
55+
/* we will end the annotation by a newline anyway */
56+
if (msglen && msg[msglen - 1] == '\n')
57+
msglen--;
58+
59+
strbuf_addstr(sb, "\nNotes:\n");
60+
61+
for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) {
62+
linelen = strchrnul(msg_p, '\n') - msg_p;
63+
64+
strbuf_addstr(sb, " ");
65+
strbuf_add(sb, msg_p, linelen);
66+
strbuf_addch(sb, '\n');
67+
}
68+
69+
free(msg);
70+
}

notes.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 5 additions & 0 deletions

0 commit comments

Comments
 (0)