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

Commit 879ef24

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. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 5832d1a commit 879ef24

9 files changed

Lines changed: 107 additions & 0 deletions

File tree

Documentation/config.txt

Lines changed: 15 additions & 0 deletions

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ LIB_H += ll-merge.h
370370
LIB_H += log-tree.h
371371
LIB_H += mailmap.h
372372
LIB_H += merge-recursive.h
373+
LIB_H += notes.h
373374
LIB_H += object.h
374375
LIB_H += pack.h
375376
LIB_H += pack-refs.h
@@ -451,6 +452,7 @@ LIB_OBJS += match-trees.o
451452
LIB_OBJS += merge-file.o
452453
LIB_OBJS += merge-recursive.o
453454
LIB_OBJS += name-hash.o
455+
LIB_OBJS += notes.o
454456
LIB_OBJS += object.o
455457
LIB_OBJS += pack-check.o
456458
LIB_OBJS += pack-refs.o

cache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ static inline enum object_type object_type(unsigned int mode)
367367
#define GITATTRIBUTES_FILE ".gitattributes"
368368
#define INFOATTRIBUTES_FILE "info/attributes"
369369
#define ATTRIBUTE_MACRO_PREFIX "[attr]"
370+
#define GIT_NOTES_REF_ENVIRONMENT "GIT_NOTES_REF"
371+
#define GIT_NOTES_DEFAULT_REF "refs/notes/commits"
370372

371373
extern int is_bare_repository_cfg;
372374
extern int is_bare_repository(void);
@@ -538,6 +540,7 @@ enum rebase_setup_type {
538540

539541
extern enum branch_track git_branch_track;
540542
extern enum rebase_setup_type autorebase;
543+
extern char *notes_ref_name;
541544

542545
#define GIT_REPO_VERSION 0
543546
extern int repository_format_version;

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
@@ -469,6 +469,11 @@ static int git_default_core_config(const char *var, const char *value)
469469
return 0;
470470
}
471471

472+
if (!strcmp(var, "core.notesref")) {
473+
notes_ref_name = xstrdup(value);
474+
return 0;
475+
}
476+
472477
if (!strcmp(var, "core.pager"))
473478
return git_config_string(&pager_program, var, value);
474479

environment.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
4545

4646
/* Parallel index stat data preload? */
4747
int core_preload_index = 0;
48+
char *notes_ref_name;
4849

4950
/* This is set by setup_git_dir_gently() and/or git_default_config() */
5051
char *git_work_tree_cfg;

notes.c

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

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)