Skip to content

Commit a86ed83

Browse files
committed
Merge branch 'tr/notes-display'
* tr/notes-display: git-notes(1): add a section about the meaning of history notes: track whether notes_trees were changed at all notes: add shorthand --ref to override GIT_NOTES_REF commit --amend: copy notes to the new commit rebase: support automatic notes copying notes: implement helpers needed for note copying during rewrite notes: implement 'git notes copy --stdin' rebase -i: invoke post-rewrite hook rebase: invoke post-rewrite hook commit --amend: invoke post-rewrite hook Documentation: document post-rewrite hook Support showing notes from more than one notes tree test-lib: unset GIT_NOTES_REF to stop it from influencing tests Conflicts: git-am.sh refs.c
2 parents b6a7a06 + 66d6819 commit a86ed83

25 files changed

+1366
-33
lines changed

Documentation/config.txt

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,10 +519,12 @@ check that makes sure that existing object files will not get overwritten.
519519
core.notesRef::
520520
When showing commit messages, also show notes which are stored in
521521
the given ref. This ref is expected to contain files named
522-
after the full SHA-1 of the commit they annotate.
522+
after the full SHA-1 of the commit they annotate. The ref
523+
must be fully qualified.
523524
+
524525
If such a file exists in the given ref, the referenced blob is read, and
525-
appended to the commit message, separated by a "Notes:" line. If the
526+
appended to the commit message, separated by a "Notes (<refname>):"
527+
line (shortened to "Notes:" in the case of "refs/notes/commits"). If the
526528
given ref itself does not exist, it is not an error, but means that no
527529
notes should be printed.
528530
+
@@ -1334,6 +1336,53 @@ mergetool.keepTemporaries::
13341336
mergetool.prompt::
13351337
Prompt before each invocation of the merge resolution program.
13361338

1339+
notes.displayRef::
1340+
The (fully qualified) refname from which to show notes when
1341+
showing commit messages. The value of this variable can be set
1342+
to a glob, in which case notes from all matching refs will be
1343+
shown. You may also specify this configuration variable
1344+
several times. A warning will be issued for refs that do not
1345+
exist, but a glob that does not match any refs is silently
1346+
ignored.
1347+
+
1348+
This setting can be overridden with the `GIT_NOTES_DISPLAY_REF`
1349+
environment variable, which must be a colon separated list of refs or
1350+
globs.
1351+
+
1352+
The effective value of "core.notesRef" (possibly overridden by
1353+
GIT_NOTES_REF) is also implicitly added to the list of refs to be
1354+
displayed.
1355+
1356+
notes.rewrite.<command>::
1357+
When rewriting commits with <command> (currently `amend` or
1358+
`rebase`) and this variable is set to `true`, git
1359+
automatically copies your notes from the original to the
1360+
rewritten commit. Defaults to `true`, but see
1361+
"notes.rewriteRef" below.
1362+
+
1363+
This setting can be overridden with the `GIT_NOTES_REWRITE_REF`
1364+
environment variable, which must be a colon separated list of refs or
1365+
globs.
1366+
1367+
notes.rewriteMode::
1368+
When copying notes during a rewrite (see the
1369+
"notes.rewrite.<command>" option), determines what to do if
1370+
the target commit already has a note. Must be one of
1371+
`overwrite`, `concatenate`, or `ignore`. Defaults to
1372+
`concatenate`.
1373+
+
1374+
This setting can be overridden with the `GIT_NOTES_REWRITE_MODE`
1375+
environment variable.
1376+
1377+
notes.rewriteRef::
1378+
When copying notes during a rewrite, specifies the (fully
1379+
qualified) ref whose notes should be copied. The ref may be a
1380+
glob, in which case notes in all matching refs will be copied.
1381+
You may also specify this configuration several times.
1382+
+
1383+
Does not have a default value; you must configure this variable to
1384+
enable note rewriting.
1385+
13371386
pack.window::
13381387
The size of the window used by linkgit:git-pack-objects[1] when no
13391388
window size is given on the command line. Defaults to 10.

Documentation/git-notes.txt

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SYNOPSIS
1010
[verse]
1111
'git notes' [list [<object>]]
1212
'git notes' add [-f] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
13-
'git notes' copy [-f] <from-object> <to-object>
13+
'git notes' copy [-f] ( --stdin | <from-object> <to-object> )
1414
'git notes' append [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
1515
'git notes' edit [<object>]
1616
'git notes' show [<object>]
@@ -27,12 +27,17 @@ A typical use of notes is to extend a commit message without having
2727
to change the commit itself. Such commit notes can be shown by `git log`
2828
along with the original commit message. To discern these notes from the
2929
message stored in the commit object, the notes are indented like the
30-
message, after an unindented line saying "Notes:".
30+
message, after an unindented line saying "Notes (<refname>):" (or
31+
"Notes:" for the default setting).
3132

32-
To disable notes, you have to set the config variable core.notesRef to
33-
the empty string. Alternatively, you can set it to a different ref,
34-
something like "refs/notes/bugzilla". This setting can be overridden
35-
by the environment variable "GIT_NOTES_REF".
33+
This command always manipulates the notes specified in "core.notesRef"
34+
(see linkgit:git-config[1]), which can be overridden by GIT_NOTES_REF.
35+
To change which notes are shown by 'git-log', see the
36+
"notes.displayRef" configuration.
37+
38+
See the description of "notes.rewrite.<command>" in
39+
linkgit:git-config[1] for a way of carrying your notes across commands
40+
that rewrite commits.
3641

3742

3843
SUBCOMMANDS
@@ -55,6 +60,16 @@ copy::
5560
object has none (use -f to overwrite existing notes to the
5661
second object). This subcommand is equivalent to:
5762
`git notes add [-f] -C $(git notes list <from-object>) <to-object>`
63+
+
64+
In `\--stdin` mode, take lines in the format
65+
+
66+
----------
67+
<from-object> SP <to-object> [ SP <rest> ] LF
68+
----------
69+
+
70+
on standard input, and copy the notes from each <from-object> to its
71+
corresponding <to-object>. (The optional `<rest>` is ignored so that
72+
the command can read the input given to the `post-rewrite` hook.)
5873

5974
append::
6075
Append to the notes of an existing object (defaults to HEAD).
@@ -101,6 +116,25 @@ OPTIONS
101116
Like '-C', but with '-c' the editor is invoked, so that
102117
the user can further edit the note message.
103118

119+
--ref <ref>::
120+
Manipulate the notes tree in <ref>. This overrides both
121+
GIT_NOTES_REF and the "core.notesRef" configuration. The ref
122+
is taken to be in `refs/notes/` if it is not qualified.
123+
124+
125+
NOTES
126+
-----
127+
128+
Every notes change creates a new commit at the specified notes ref.
129+
You can therefore inspect the history of the notes by invoking, e.g.,
130+
`git log -p notes/commits`.
131+
132+
Currently the commit message only records which operation triggered
133+
the update, and the commit authorship is determined according to the
134+
usual rules (see linkgit:git-commit[1]). These details may change in
135+
the future.
136+
137+
104138
Author
105139
------
106140
Written by Johannes Schindelin <[email protected]> and

Documentation/githooks.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,44 @@ This hook is invoked by 'git gc --auto'. It takes no parameter, and
317317
exiting with non-zero status from this script causes the 'git gc --auto'
318318
to abort.
319319

320+
post-rewrite
321+
~~~~~~~~~~~~
322+
323+
This hook is invoked by commands that rewrite commits (`git commit
324+
--amend`, 'git-rebase'; currently 'git-filter-branch' does 'not' call
325+
it!). Its first argument denotes the command it was invoked by:
326+
currently one of `amend` or `rebase`. Further command-dependent
327+
arguments may be passed in the future.
328+
329+
The hook receives a list of the rewritten commits on stdin, in the
330+
format
331+
332+
<old-sha1> SP <new-sha1> [ SP <extra-info> ] LF
333+
334+
The 'extra-info' is again command-dependent. If it is empty, the
335+
preceding SP is also omitted. Currently, no commands pass any
336+
'extra-info'.
337+
338+
The hook always runs after the automatic note copying (see
339+
"notes.rewrite.<command>" in linkgit:git-config.txt) has happened, and
340+
thus has access to these notes.
341+
342+
The following command-specific comments apply:
343+
344+
rebase::
345+
For the 'squash' and 'fixup' operation, all commits that were
346+
squashed are listed as being rewritten to the squashed commit.
347+
This means that there will be several lines sharing the same
348+
'new-sha1'.
349+
+
350+
The commits are guaranteed to be listed in the order that they were
351+
processed by rebase.
352+
353+
There is no default 'post-rewrite' hook, but see the
354+
`post-receive-copy-notes` script in `contrib/hooks` for an example
355+
that copies your git-notes to the rewritten commits.
356+
357+
320358
GIT
321359
---
322360
Part of the linkgit:git[1] suite

Documentation/pretty-options.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,18 @@ people using 80-column terminals.
3030
defaults to UTF-8.
3131

3232
--no-notes::
33-
--show-notes::
33+
--show-notes[=<ref>]::
3434
Show the notes (see linkgit:git-notes[1]) that annotate the
3535
commit, when showing the commit log message. This is the default
3636
for `git log`, `git show` and `git whatchanged` commands when
3737
there is no `--pretty`, `--format` nor `--oneline` option is
3838
given on the command line.
39+
+
40+
With an optional argument, add this ref to the list of notes. The ref
41+
is taken to be in `refs/notes/` if it is not qualified.
42+
43+
--[no-]standard-notes::
44+
Enable or disable populating the notes ref list from the
45+
'core.notesRef' and 'notes.displayRef' variables (or
46+
corresponding environment overrides). Enabled by default.
47+
See linkgit:git-config[1].

builtin.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ extern int commit_tree(const char *msg, unsigned char *tree,
2020
struct commit_list *parents, unsigned char *ret,
2121
const char *author);
2222
extern int commit_notes(struct notes_tree *t, const char *msg);
23+
24+
struct notes_rewrite_cfg {
25+
struct notes_tree **trees;
26+
const char *cmd;
27+
int enabled;
28+
combine_notes_fn *combine;
29+
struct string_list *refs;
30+
int refs_from_env;
31+
int mode_from_env;
32+
};
33+
34+
combine_notes_fn *parse_combine_notes_fn(const char *v);
35+
struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd);
36+
int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
37+
const unsigned char *from_obj, const unsigned char *to_obj);
38+
void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c);
39+
2340
extern int check_pager_config(const char *cmd);
2441

2542
extern int cmd_add(int argc, const char **argv, const char *prefix);

builtin/commit.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ static char *edit_message, *use_message;
6666
static char *author_name, *author_email, *author_date;
6767
static int all, edit_flag, also, interactive, only, amend, signoff;
6868
static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
69+
static int no_post_rewrite;
6970
static char *untracked_files_arg, *force_date;
7071
/*
7172
* The default commit message cleanup mode will remove the lines
@@ -137,6 +138,7 @@ static struct option builtin_commit_options[] = {
137138
OPT_BOOLEAN('z', "null", &null_termination,
138139
"terminate entries with NUL"),
139140
OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
141+
OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite, "bypass post-rewrite hook"),
140142
{ OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, "mode", "show untracked files, optional modes: all, normal, no. (Default: all)", PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
141143
OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"),
142144
/* end commit contents options */
@@ -1160,6 +1162,40 @@ static int git_commit_config(const char *k, const char *v, void *cb)
11601162
return git_status_config(k, v, s);
11611163
}
11621164

1165+
static const char post_rewrite_hook[] = "hooks/post-rewrite";
1166+
1167+
static int run_rewrite_hook(const unsigned char *oldsha1,
1168+
const unsigned char *newsha1)
1169+
{
1170+
/* oldsha1 SP newsha1 LF NUL */
1171+
static char buf[2*40 + 3];
1172+
struct child_process proc;
1173+
const char *argv[3];
1174+
int code;
1175+
size_t n;
1176+
1177+
if (access(git_path(post_rewrite_hook), X_OK) < 0)
1178+
return 0;
1179+
1180+
argv[0] = git_path(post_rewrite_hook);
1181+
argv[1] = "amend";
1182+
argv[2] = NULL;
1183+
1184+
memset(&proc, 0, sizeof(proc));
1185+
proc.argv = argv;
1186+
proc.in = -1;
1187+
proc.stdout_to_stderr = 1;
1188+
1189+
code = start_command(&proc);
1190+
if (code)
1191+
return code;
1192+
n = snprintf(buf, sizeof(buf), "%s %s\n",
1193+
sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
1194+
write_in_full(proc.in, buf, n);
1195+
close(proc.in);
1196+
return finish_command(&proc);
1197+
}
1198+
11631199
int cmd_commit(int argc, const char **argv, const char *prefix)
11641200
{
11651201
struct strbuf sb = STRBUF_INIT;
@@ -1303,6 +1339,15 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
13031339

13041340
rerere(0);
13051341
run_hook(get_index_file(), "post-commit", NULL);
1342+
if (amend && !no_post_rewrite) {
1343+
struct notes_rewrite_cfg *cfg;
1344+
cfg = init_copy_notes_for_rewrite("amend");
1345+
if (cfg) {
1346+
copy_note_for_rewrite(cfg, head_sha1, commit_sha1);
1347+
finish_copy_notes_for_rewrite(cfg);
1348+
}
1349+
run_rewrite_hook(head_sha1, commit_sha1);
1350+
}
13061351
if (!quiet)
13071352
print_summary(prefix, commit_sha1);
13081353

builtin/log.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
6060

6161
if (!rev->show_notes_given && !rev->pretty_given)
6262
rev->show_notes = 1;
63+
if (rev->show_notes)
64+
init_display_notes(&rev->notes_opt);
6365

6466
if (rev->diffopt.pickaxe || rev->diffopt.filter)
6567
rev->always_show_header = 0;
@@ -1105,6 +1107,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
11051107
if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
11061108
DIFF_OPT_SET(&rev.diffopt, BINARY);
11071109

1110+
if (rev.show_notes)
1111+
init_display_notes(&rev.notes_opt);
1112+
11081113
if (!use_stdout)
11091114
output_directory = set_outdir(prefix, output_directory);
11101115

0 commit comments

Comments
 (0)