mirror of
https://github.com/git/git
synced 2024-08-24 18:26:02 +00:00
![Johannes Schindelin](/assets/img/avatar_default.png)
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
71 lines
1.6 KiB
C
71 lines
1.6 KiB
C
#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);
|
|
}
|