Coverage Report

Created: 2023-11-19 07:08

/src/git/builtin/notes.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Builtin "git notes"
3
 *
4
 * Copyright (c) 2010 Johan Herland <johan@herland.net>
5
 *
6
 * Based on git-notes.sh by Johannes Schindelin,
7
 * and builtin/tag.c by Kristian Høgsberg and Carlos Rica.
8
 */
9
10
#include "builtin.h"
11
#include "config.h"
12
#include "alloc.h"
13
#include "editor.h"
14
#include "environment.h"
15
#include "gettext.h"
16
#include "hex.h"
17
#include "notes.h"
18
#include "object-name.h"
19
#include "object-store-ll.h"
20
#include "path.h"
21
#include "repository.h"
22
#include "blob.h"
23
#include "pretty.h"
24
#include "refs.h"
25
#include "exec-cmd.h"
26
#include "run-command.h"
27
#include "parse-options.h"
28
#include "string-list.h"
29
#include "notes-merge.h"
30
#include "notes-utils.h"
31
#include "worktree.h"
32
#include "write-or-die.h"
33
34
static const char *separator = "\n";
35
static const char * const git_notes_usage[] = {
36
  N_("git notes [--ref <notes-ref>] [list [<object>]]"),
37
  N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
38
  N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
39
  N_("git notes [--ref <notes-ref>] append [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
40
  N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
41
  N_("git notes [--ref <notes-ref>] show [<object>]"),
42
  N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
43
  "git notes merge --commit [-v | -q]",
44
  "git notes merge --abort [-v | -q]",
45
  N_("git notes [--ref <notes-ref>] remove [<object>...]"),
46
  N_("git notes [--ref <notes-ref>] prune [-n] [-v]"),
47
  N_("git notes [--ref <notes-ref>] get-ref"),
48
  NULL
49
};
50
51
static const char * const git_notes_list_usage[] = {
52
  N_("git notes [list [<object>]]"),
53
  NULL
54
};
55
56
static const char * const git_notes_add_usage[] = {
57
  N_("git notes add [<options>] [<object>]"),
58
  NULL
59
};
60
61
static const char * const git_notes_copy_usage[] = {
62
  N_("git notes copy [<options>] <from-object> <to-object>"),
63
  N_("git notes copy --stdin [<from-object> <to-object>]..."),
64
  NULL
65
};
66
67
static const char * const git_notes_append_usage[] = {
68
  N_("git notes append [<options>] [<object>]"),
69
  NULL
70
};
71
72
static const char * const git_notes_edit_usage[] = {
73
  N_("git notes edit [<object>]"),
74
  NULL
75
};
76
77
static const char * const git_notes_show_usage[] = {
78
  N_("git notes show [<object>]"),
79
  NULL
80
};
81
82
static const char * const git_notes_merge_usage[] = {
83
  N_("git notes merge [<options>] <notes-ref>"),
84
  N_("git notes merge --commit [<options>]"),
85
  N_("git notes merge --abort [<options>]"),
86
  NULL
87
};
88
89
static const char * const git_notes_remove_usage[] = {
90
  N_("git notes remove [<object>]"),
91
  NULL
92
};
93
94
static const char * const git_notes_prune_usage[] = {
95
  N_("git notes prune [<options>]"),
96
  NULL
97
};
98
99
static const char * const git_notes_get_ref_usage[] = {
100
  "git notes get-ref",
101
  NULL
102
};
103
104
static const char note_template[] =
105
  N_("Write/edit the notes for the following object:");
106
107
enum notes_stripspace {
108
  UNSPECIFIED = -1,
109
  NO_STRIPSPACE = 0,
110
  STRIPSPACE = 1,
111
};
112
113
struct note_msg {
114
  enum notes_stripspace stripspace;
115
  struct strbuf buf;
116
};
117
118
struct note_data {
119
  int given;
120
  int use_editor;
121
  int stripspace;
122
  char *edit_path;
123
  struct strbuf buf;
124
  struct note_msg **messages;
125
  size_t msg_nr;
126
  size_t msg_alloc;
127
};
128
129
static void free_note_data(struct note_data *d)
130
0
{
131
0
  if (d->edit_path) {
132
0
    unlink_or_warn(d->edit_path);
133
0
    free(d->edit_path);
134
0
  }
135
0
  strbuf_release(&d->buf);
136
137
0
  while (d->msg_nr--) {
138
0
    strbuf_release(&d->messages[d->msg_nr]->buf);
139
0
    free(d->messages[d->msg_nr]);
140
0
  }
141
0
  free(d->messages);
142
0
}
143
144
static int list_each_note(const struct object_id *object_oid,
145
        const struct object_id *note_oid,
146
        char *note_path UNUSED,
147
        void *cb_data UNUSED)
148
0
{
149
0
  printf("%s %s\n", oid_to_hex(note_oid), oid_to_hex(object_oid));
150
0
  return 0;
151
0
}
152
153
static void copy_obj_to_fd(int fd, const struct object_id *oid)
154
0
{
155
0
  unsigned long size;
156
0
  enum object_type type;
157
0
  char *buf = repo_read_object_file(the_repository, oid, &type, &size);
158
0
  if (buf) {
159
0
    if (size)
160
0
      write_or_die(fd, buf, size);
161
0
    free(buf);
162
0
  }
163
0
}
164
165
static void write_commented_object(int fd, const struct object_id *object)
166
0
{
167
0
  struct child_process show = CHILD_PROCESS_INIT;
168
0
  struct strbuf buf = STRBUF_INIT;
169
0
  struct strbuf cbuf = STRBUF_INIT;
170
171
  /* Invoke "git show --stat --no-notes $object" */
172
0
  strvec_pushl(&show.args, "show", "--stat", "--no-notes",
173
0
         oid_to_hex(object), NULL);
174
0
  show.no_stdin = 1;
175
0
  show.out = -1;
176
0
  show.err = 0;
177
0
  show.git_cmd = 1;
178
0
  if (start_command(&show))
179
0
    die(_("unable to start 'show' for object '%s'"),
180
0
        oid_to_hex(object));
181
182
0
  if (strbuf_read(&buf, show.out, 0) < 0)
183
0
    die_errno(_("could not read 'show' output"));
184
0
  strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_char);
185
0
  write_or_die(fd, cbuf.buf, cbuf.len);
186
187
0
  strbuf_release(&cbuf);
188
0
  strbuf_release(&buf);
189
190
0
  if (finish_command(&show))
191
0
    die(_("failed to finish 'show' for object '%s'"),
192
0
        oid_to_hex(object));
193
0
}
194
195
static void prepare_note_data(const struct object_id *object, struct note_data *d,
196
    const struct object_id *old_note)
197
0
{
198
0
  if (d->use_editor || !d->given) {
199
0
    int fd;
200
0
    struct strbuf buf = STRBUF_INIT;
201
202
    /* write the template message before editing: */
203
0
    d->edit_path = git_pathdup("NOTES_EDITMSG");
204
0
    fd = xopen(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
205
206
0
    if (d->given)
207
0
      write_or_die(fd, d->buf.buf, d->buf.len);
208
0
    else if (old_note)
209
0
      copy_obj_to_fd(fd, old_note);
210
211
0
    strbuf_addch(&buf, '\n');
212
0
    strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
213
0
    strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
214
0
             comment_line_char);
215
0
    strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
216
0
    write_or_die(fd, buf.buf, buf.len);
217
218
0
    write_commented_object(fd, object);
219
220
0
    close(fd);
221
0
    strbuf_release(&buf);
222
0
    strbuf_reset(&d->buf);
223
224
0
    if (launch_editor(d->edit_path, &d->buf, NULL)) {
225
0
      die(_("please supply the note contents using either -m or -F option"));
226
0
    }
227
0
    if (d->stripspace)
228
0
      strbuf_stripspace(&d->buf, comment_line_char);
229
0
  }
230
0
}
231
232
static void write_note_data(struct note_data *d, struct object_id *oid)
233
0
{
234
0
  if (write_object_file(d->buf.buf, d->buf.len, OBJ_BLOB, oid)) {
235
0
    int status = die_message(_("unable to write note object"));
236
237
0
    if (d->edit_path)
238
0
      die_message(_("the note contents have been left in %s"),
239
0
            d->edit_path);
240
0
    exit(status);
241
0
  }
242
0
}
243
244
static void append_separator(struct strbuf *message)
245
0
{
246
0
  size_t sep_len = 0;
247
248
0
  if (!separator)
249
0
    return;
250
0
  else if ((sep_len = strlen(separator)) && separator[sep_len - 1] == '\n')
251
0
    strbuf_addstr(message, separator);
252
0
  else
253
0
    strbuf_addf(message, "%s%s", separator, "\n");
254
0
}
255
256
static void concat_messages(struct note_data *d)
257
0
{
258
0
  struct strbuf msg = STRBUF_INIT;
259
0
  size_t i;
260
261
0
  for (i = 0; i < d->msg_nr ; i++) {
262
0
    if (d->buf.len)
263
0
      append_separator(&d->buf);
264
0
    strbuf_add(&msg, d->messages[i]->buf.buf, d->messages[i]->buf.len);
265
0
    strbuf_addbuf(&d->buf, &msg);
266
0
    if ((d->stripspace == UNSPECIFIED &&
267
0
         d->messages[i]->stripspace == STRIPSPACE) ||
268
0
        d->stripspace == STRIPSPACE)
269
0
      strbuf_stripspace(&d->buf, 0);
270
0
    strbuf_reset(&msg);
271
0
  }
272
0
  strbuf_release(&msg);
273
0
}
274
275
static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
276
0
{
277
0
  struct note_data *d = opt->value;
278
0
  struct note_msg *msg = xmalloc(sizeof(*msg));
279
280
0
  BUG_ON_OPT_NEG(unset);
281
282
0
  strbuf_init(&msg->buf, strlen(arg));
283
0
  strbuf_addstr(&msg->buf, arg);
284
0
  ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc);
285
0
  d->messages[d->msg_nr - 1] = msg;
286
0
  msg->stripspace = STRIPSPACE;
287
0
  return 0;
288
0
}
289
290
static int parse_file_arg(const struct option *opt, const char *arg, int unset)
291
0
{
292
0
  struct note_data *d = opt->value;
293
0
  struct note_msg *msg = xmalloc(sizeof(*msg));
294
295
0
  BUG_ON_OPT_NEG(unset);
296
297
0
  strbuf_init(&msg->buf , 0);
298
0
  if (!strcmp(arg, "-")) {
299
0
    if (strbuf_read(&msg->buf, 0, 1024) < 0)
300
0
      die_errno(_("cannot read '%s'"), arg);
301
0
  } else if (strbuf_read_file(&msg->buf, arg, 1024) < 0)
302
0
    die_errno(_("could not open or read '%s'"), arg);
303
304
0
  ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc);
305
0
  d->messages[d->msg_nr - 1] = msg;
306
0
  msg->stripspace = STRIPSPACE;
307
0
  return 0;
308
0
}
309
310
static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
311
0
{
312
0
  struct note_data *d = opt->value;
313
0
  struct note_msg *msg = xmalloc(sizeof(*msg));
314
0
  char *value;
315
0
  struct object_id object;
316
0
  enum object_type type;
317
0
  unsigned long len;
318
319
0
  BUG_ON_OPT_NEG(unset);
320
321
0
  strbuf_init(&msg->buf, 0);
322
0
  if (repo_get_oid(the_repository, arg, &object))
323
0
    die(_("failed to resolve '%s' as a valid ref."), arg);
324
0
  if (!(value = repo_read_object_file(the_repository, &object, &type, &len)))
325
0
    die(_("failed to read object '%s'."), arg);
326
0
  if (type != OBJ_BLOB) {
327
0
    strbuf_release(&msg->buf);
328
0
    free(value);
329
0
    free(msg);
330
0
    die(_("cannot read note data from non-blob object '%s'."), arg);
331
0
  }
332
333
0
  strbuf_add(&msg->buf, value, len);
334
0
  free(value);
335
336
0
  msg->buf.len = len;
337
0
  ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc);
338
0
  d->messages[d->msg_nr - 1] = msg;
339
0
  msg->stripspace = NO_STRIPSPACE;
340
0
  return 0;
341
0
}
342
343
static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
344
0
{
345
0
  struct note_data *d = opt->value;
346
0
  BUG_ON_OPT_NEG(unset);
347
0
  d->use_editor = 1;
348
0
  return parse_reuse_arg(opt, arg, unset);
349
0
}
350
351
static int parse_separator_arg(const struct option *opt, const char *arg,
352
             int unset)
353
0
{
354
0
  if (unset)
355
0
    *(const char **)opt->value = NULL;
356
0
  else
357
0
    *(const char **)opt->value = arg ? arg : "\n";
358
0
  return 0;
359
0
}
360
361
static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
362
0
{
363
0
  struct strbuf buf = STRBUF_INIT;
364
0
  struct notes_rewrite_cfg *c = NULL;
365
0
  struct notes_tree *t = NULL;
366
0
  int ret = 0;
367
0
  const char *msg = "Notes added by 'git notes copy'";
368
369
0
  if (rewrite_cmd) {
370
0
    c = init_copy_notes_for_rewrite(rewrite_cmd);
371
0
    if (!c)
372
0
      return 0;
373
0
  } else {
374
0
    init_notes(NULL, NULL, NULL, NOTES_INIT_WRITABLE);
375
0
    t = &default_notes_tree;
376
0
  }
377
378
0
  while (strbuf_getline_lf(&buf, stdin) != EOF) {
379
0
    struct object_id from_obj, to_obj;
380
0
    struct strbuf **split;
381
0
    int err;
382
383
0
    split = strbuf_split(&buf, ' ');
384
0
    if (!split[0] || !split[1])
385
0
      die(_("malformed input line: '%s'."), buf.buf);
386
0
    strbuf_rtrim(split[0]);
387
0
    strbuf_rtrim(split[1]);
388
0
    if (repo_get_oid(the_repository, split[0]->buf, &from_obj))
389
0
      die(_("failed to resolve '%s' as a valid ref."), split[0]->buf);
390
0
    if (repo_get_oid(the_repository, split[1]->buf, &to_obj))
391
0
      die(_("failed to resolve '%s' as a valid ref."), split[1]->buf);
392
393
0
    if (rewrite_cmd)
394
0
      err = copy_note_for_rewrite(c, &from_obj, &to_obj);
395
0
    else
396
0
      err = copy_note(t, &from_obj, &to_obj, force,
397
0
          combine_notes_overwrite);
398
399
0
    if (err) {
400
0
      error(_("failed to copy notes from '%s' to '%s'"),
401
0
            split[0]->buf, split[1]->buf);
402
0
      ret = 1;
403
0
    }
404
405
0
    strbuf_list_free(split);
406
0
  }
407
408
0
  if (!rewrite_cmd) {
409
0
    commit_notes(the_repository, t, msg);
410
0
    free_notes(t);
411
0
  } else {
412
0
    finish_copy_notes_for_rewrite(the_repository, c, msg);
413
0
  }
414
0
  strbuf_release(&buf);
415
0
  return ret;
416
0
}
417
418
static struct notes_tree *init_notes_check(const char *subcommand,
419
             int flags)
420
0
{
421
0
  struct notes_tree *t;
422
0
  const char *ref;
423
0
  init_notes(NULL, NULL, NULL, flags);
424
0
  t = &default_notes_tree;
425
426
0
  ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref;
427
0
  if (!starts_with(ref, "refs/notes/"))
428
    /*
429
     * TRANSLATORS: the first %s will be replaced by a git
430
     * notes command: 'add', 'merge', 'remove', etc.
431
     */
432
0
    die(_("refusing to %s notes in %s (outside of refs/notes/)"),
433
0
        subcommand, ref);
434
0
  return t;
435
0
}
436
437
static int list(int argc, const char **argv, const char *prefix)
438
0
{
439
0
  struct notes_tree *t;
440
0
  struct object_id object;
441
0
  const struct object_id *note;
442
0
  int retval = -1;
443
0
  struct option options[] = {
444
0
    OPT_END()
445
0
  };
446
447
0
  if (argc)
448
0
    argc = parse_options(argc, argv, prefix, options,
449
0
             git_notes_list_usage, 0);
450
451
0
  if (1 < argc) {
452
0
    error(_("too many arguments"));
453
0
    usage_with_options(git_notes_list_usage, options);
454
0
  }
455
456
0
  t = init_notes_check("list", 0);
457
0
  if (argc) {
458
0
    if (repo_get_oid(the_repository, argv[0], &object))
459
0
      die(_("failed to resolve '%s' as a valid ref."), argv[0]);
460
0
    note = get_note(t, &object);
461
0
    if (note) {
462
0
      puts(oid_to_hex(note));
463
0
      retval = 0;
464
0
    } else
465
0
      retval = error(_("no note found for object %s."),
466
0
               oid_to_hex(&object));
467
0
  } else
468
0
    retval = for_each_note(t, 0, list_each_note, NULL);
469
470
0
  free_notes(t);
471
0
  return retval;
472
0
}
473
474
static int append_edit(int argc, const char **argv, const char *prefix);
475
476
static int add(int argc, const char **argv, const char *prefix)
477
0
{
478
0
  int force = 0, allow_empty = 0;
479
0
  const char *object_ref;
480
0
  struct notes_tree *t;
481
0
  struct object_id object, new_note;
482
0
  const struct object_id *note;
483
0
  struct note_data d = { .buf = STRBUF_INIT, .stripspace = UNSPECIFIED };
484
485
0
  struct option options[] = {
486
0
    OPT_CALLBACK_F('m', "message", &d, N_("message"),
487
0
      N_("note contents as a string"), PARSE_OPT_NONEG,
488
0
      parse_msg_arg),
489
0
    OPT_CALLBACK_F('F', "file", &d, N_("file"),
490
0
      N_("note contents in a file"), PARSE_OPT_NONEG,
491
0
      parse_file_arg),
492
0
    OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"),
493
0
      N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
494
0
      parse_reedit_arg),
495
0
    OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
496
0
      N_("reuse specified note object"), PARSE_OPT_NONEG,
497
0
      parse_reuse_arg),
498
0
    OPT_BOOL(0, "allow-empty", &allow_empty,
499
0
      N_("allow storing empty note")),
500
0
    OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
501
0
    OPT_CALLBACK_F(0, "separator", &separator,
502
0
      N_("<paragraph-break>"),
503
0
      N_("insert <paragraph-break> between paragraphs"),
504
0
      PARSE_OPT_OPTARG, parse_separator_arg),
505
0
    OPT_BOOL(0, "stripspace", &d.stripspace,
506
0
      N_("remove unnecessary whitespace")),
507
0
    OPT_END()
508
0
  };
509
510
0
  argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
511
0
           PARSE_OPT_KEEP_ARGV0);
512
513
0
  if (2 < argc) {
514
0
    error(_("too many arguments"));
515
0
    usage_with_options(git_notes_add_usage, options);
516
0
  }
517
518
0
  if (d.msg_nr)
519
0
    concat_messages(&d);
520
0
  d.given = !!d.buf.len;
521
522
0
  object_ref = argc > 1 ? argv[1] : "HEAD";
523
524
0
  if (repo_get_oid(the_repository, object_ref, &object))
525
0
    die(_("failed to resolve '%s' as a valid ref."), object_ref);
526
527
0
  t = init_notes_check("add", NOTES_INIT_WRITABLE);
528
0
  note = get_note(t, &object);
529
530
0
  if (note) {
531
0
    if (!force) {
532
0
      free_notes(t);
533
0
      if (d.given) {
534
0
        free_note_data(&d);
535
0
        return error(_("Cannot add notes. "
536
0
          "Found existing notes for object %s. "
537
0
          "Use '-f' to overwrite existing notes"),
538
0
          oid_to_hex(&object));
539
0
      }
540
      /*
541
       * Redirect to "edit" subcommand.
542
       *
543
       * We only end up here if none of -m/-F/-c/-C or -f are
544
       * given. The original args are therefore still in
545
       * argv[0-1].
546
       */
547
0
      argv[0] = "edit";
548
0
      return append_edit(argc, argv, prefix);
549
0
    }
550
0
    fprintf(stderr, _("Overwriting existing notes for object %s\n"),
551
0
      oid_to_hex(&object));
552
0
  }
553
554
0
  prepare_note_data(&object, &d, note);
555
0
  if (d.buf.len || allow_empty) {
556
0
    write_note_data(&d, &new_note);
557
0
    if (add_note(t, &object, &new_note, combine_notes_overwrite))
558
0
      BUG("combine_notes_overwrite failed");
559
0
    commit_notes(the_repository, t,
560
0
           "Notes added by 'git notes add'");
561
0
  } else {
562
0
    fprintf(stderr, _("Removing note for object %s\n"),
563
0
      oid_to_hex(&object));
564
0
    remove_note(t, object.hash);
565
0
    commit_notes(the_repository, t,
566
0
           "Notes removed by 'git notes add'");
567
0
  }
568
569
0
  free_note_data(&d);
570
0
  free_notes(t);
571
0
  return 0;
572
0
}
573
574
static int copy(int argc, const char **argv, const char *prefix)
575
0
{
576
0
  int retval = 0, force = 0, from_stdin = 0;
577
0
  const struct object_id *from_note, *note;
578
0
  const char *object_ref;
579
0
  struct object_id object, from_obj;
580
0
  struct notes_tree *t;
581
0
  const char *rewrite_cmd = NULL;
582
0
  struct option options[] = {
583
0
    OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
584
0
    OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
585
0
    OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
586
0
         N_("load rewriting config for <command> (implies "
587
0
            "--stdin)")),
588
0
    OPT_END()
589
0
  };
590
591
0
  argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
592
0
           0);
593
594
0
  if (from_stdin || rewrite_cmd) {
595
0
    if (argc) {
596
0
      error(_("too many arguments"));
597
0
      usage_with_options(git_notes_copy_usage, options);
598
0
    } else {
599
0
      return notes_copy_from_stdin(force, rewrite_cmd);
600
0
    }
601
0
  }
602
603
0
  if (argc < 1) {
604
0
    error(_("too few arguments"));
605
0
    usage_with_options(git_notes_copy_usage, options);
606
0
  }
607
0
  if (2 < argc) {
608
0
    error(_("too many arguments"));
609
0
    usage_with_options(git_notes_copy_usage, options);
610
0
  }
611
612
0
  if (repo_get_oid(the_repository, argv[0], &from_obj))
613
0
    die(_("failed to resolve '%s' as a valid ref."), argv[0]);
614
615
0
  object_ref = 1 < argc ? argv[1] : "HEAD";
616
617
0
  if (repo_get_oid(the_repository, object_ref, &object))
618
0
    die(_("failed to resolve '%s' as a valid ref."), object_ref);
619
620
0
  t = init_notes_check("copy", NOTES_INIT_WRITABLE);
621
0
  note = get_note(t, &object);
622
623
0
  if (note) {
624
0
    if (!force) {
625
0
      retval = error(_("Cannot copy notes. Found existing "
626
0
               "notes for object %s. Use '-f' to "
627
0
               "overwrite existing notes"),
628
0
               oid_to_hex(&object));
629
0
      goto out;
630
0
    }
631
0
    fprintf(stderr, _("Overwriting existing notes for object %s\n"),
632
0
      oid_to_hex(&object));
633
0
  }
634
635
0
  from_note = get_note(t, &from_obj);
636
0
  if (!from_note) {
637
0
    retval = error(_("missing notes on source object %s. Cannot "
638
0
             "copy."), oid_to_hex(&from_obj));
639
0
    goto out;
640
0
  }
641
642
0
  if (add_note(t, &object, from_note, combine_notes_overwrite))
643
0
    BUG("combine_notes_overwrite failed");
644
0
  commit_notes(the_repository, t,
645
0
         "Notes added by 'git notes copy'");
646
0
out:
647
0
  free_notes(t);
648
0
  return retval;
649
0
}
650
651
static int append_edit(int argc, const char **argv, const char *prefix)
652
0
{
653
0
  int allow_empty = 0;
654
0
  const char *object_ref;
655
0
  struct notes_tree *t;
656
0
  struct object_id object, new_note;
657
0
  const struct object_id *note;
658
0
  char *logmsg;
659
0
  const char * const *usage;
660
0
  struct note_data d = { .buf = STRBUF_INIT, .stripspace = UNSPECIFIED };
661
0
  struct option options[] = {
662
0
    OPT_CALLBACK_F('m', "message", &d, N_("message"),
663
0
      N_("note contents as a string"), PARSE_OPT_NONEG,
664
0
      parse_msg_arg),
665
0
    OPT_CALLBACK_F('F', "file", &d, N_("file"),
666
0
      N_("note contents in a file"), PARSE_OPT_NONEG,
667
0
      parse_file_arg),
668
0
    OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"),
669
0
      N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
670
0
      parse_reedit_arg),
671
0
    OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
672
0
      N_("reuse specified note object"), PARSE_OPT_NONEG,
673
0
      parse_reuse_arg),
674
0
    OPT_BOOL(0, "allow-empty", &allow_empty,
675
0
      N_("allow storing empty note")),
676
0
    OPT_CALLBACK_F(0, "separator", &separator,
677
0
      N_("<paragraph-break>"),
678
0
      N_("insert <paragraph-break> between paragraphs"),
679
0
      PARSE_OPT_OPTARG, parse_separator_arg),
680
0
    OPT_BOOL(0, "stripspace", &d.stripspace,
681
0
      N_("remove unnecessary whitespace")),
682
0
    OPT_END()
683
0
  };
684
0
  int edit = !strcmp(argv[0], "edit");
685
686
0
  usage = edit ? git_notes_edit_usage : git_notes_append_usage;
687
0
  argc = parse_options(argc, argv, prefix, options, usage,
688
0
           PARSE_OPT_KEEP_ARGV0);
689
690
0
  if (2 < argc) {
691
0
    error(_("too many arguments"));
692
0
    usage_with_options(usage, options);
693
0
  }
694
695
0
  if (d.msg_nr)
696
0
    concat_messages(&d);
697
0
  d.given = !!d.buf.len;
698
699
0
  if (d.given && edit)
700
0
    fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
701
0
      "for the 'edit' subcommand.\n"
702
0
      "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
703
704
0
  object_ref = 1 < argc ? argv[1] : "HEAD";
705
706
0
  if (repo_get_oid(the_repository, object_ref, &object))
707
0
    die(_("failed to resolve '%s' as a valid ref."), object_ref);
708
709
0
  t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
710
0
  note = get_note(t, &object);
711
712
0
  prepare_note_data(&object, &d, edit && note ? note : NULL);
713
714
0
  if (note && !edit) {
715
    /* Append buf to previous note contents */
716
0
    unsigned long size;
717
0
    enum object_type type;
718
0
    struct strbuf buf = STRBUF_INIT;
719
0
    char *prev_buf = repo_read_object_file(the_repository, note, &type, &size);
720
721
0
    if (prev_buf && size)
722
0
      strbuf_add(&buf, prev_buf, size);
723
0
    if (d.buf.len && prev_buf && size)
724
0
      append_separator(&buf);
725
0
    strbuf_insert(&d.buf, 0, buf.buf, buf.len);
726
727
0
    free(prev_buf);
728
0
    strbuf_release(&buf);
729
0
  }
730
731
0
  if (d.buf.len || allow_empty) {
732
0
    write_note_data(&d, &new_note);
733
0
    if (add_note(t, &object, &new_note, combine_notes_overwrite))
734
0
      BUG("combine_notes_overwrite failed");
735
0
    logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
736
0
  } else {
737
0
    fprintf(stderr, _("Removing note for object %s\n"),
738
0
      oid_to_hex(&object));
739
0
    remove_note(t, object.hash);
740
0
    logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
741
0
  }
742
0
  commit_notes(the_repository, t, logmsg);
743
744
0
  free(logmsg);
745
0
  free_note_data(&d);
746
0
  free_notes(t);
747
0
  return 0;
748
0
}
749
750
static int show(int argc, const char **argv, const char *prefix)
751
0
{
752
0
  const char *object_ref;
753
0
  struct notes_tree *t;
754
0
  struct object_id object;
755
0
  const struct object_id *note;
756
0
  int retval;
757
0
  struct option options[] = {
758
0
    OPT_END()
759
0
  };
760
761
0
  argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
762
0
           0);
763
764
0
  if (1 < argc) {
765
0
    error(_("too many arguments"));
766
0
    usage_with_options(git_notes_show_usage, options);
767
0
  }
768
769
0
  object_ref = argc ? argv[0] : "HEAD";
770
771
0
  if (repo_get_oid(the_repository, object_ref, &object))
772
0
    die(_("failed to resolve '%s' as a valid ref."), object_ref);
773
774
0
  t = init_notes_check("show", 0);
775
0
  note = get_note(t, &object);
776
777
0
  if (!note)
778
0
    retval = error(_("no note found for object %s."),
779
0
             oid_to_hex(&object));
780
0
  else {
781
0
    const char *show_args[3] = {"show", oid_to_hex(note), NULL};
782
0
    retval = execv_git_cmd(show_args);
783
0
  }
784
0
  free_notes(t);
785
0
  return retval;
786
0
}
787
788
static int merge_abort(struct notes_merge_options *o)
789
0
{
790
0
  int ret = 0;
791
792
  /*
793
   * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
794
   * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
795
   */
796
797
0
  if (delete_ref(NULL, "NOTES_MERGE_PARTIAL", NULL, 0))
798
0
    ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
799
0
  if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NO_DEREF))
800
0
    ret += error(_("failed to delete ref NOTES_MERGE_REF"));
801
0
  if (notes_merge_abort(o))
802
0
    ret += error(_("failed to remove 'git notes merge' worktree"));
803
0
  return ret;
804
0
}
805
806
static int merge_commit(struct notes_merge_options *o)
807
0
{
808
0
  struct strbuf msg = STRBUF_INIT;
809
0
  struct object_id oid, parent_oid;
810
0
  struct notes_tree *t;
811
0
  struct commit *partial;
812
0
  struct pretty_print_context pretty_ctx;
813
0
  void *local_ref_to_free;
814
0
  int ret;
815
816
  /*
817
   * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
818
   * and target notes ref from .git/NOTES_MERGE_REF.
819
   */
820
821
0
  if (repo_get_oid(the_repository, "NOTES_MERGE_PARTIAL", &oid))
822
0
    die(_("failed to read ref NOTES_MERGE_PARTIAL"));
823
0
  else if (!(partial = lookup_commit_reference(the_repository, &oid)))
824
0
    die(_("could not find commit from NOTES_MERGE_PARTIAL."));
825
0
  else if (repo_parse_commit(the_repository, partial))
826
0
    die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
827
828
0
  if (partial->parents)
829
0
    oidcpy(&parent_oid, &partial->parents->item->object.oid);
830
0
  else
831
0
    oidclr(&parent_oid);
832
833
0
  CALLOC_ARRAY(t, 1);
834
0
  init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
835
836
0
  o->local_ref = local_ref_to_free =
837
0
    resolve_refdup("NOTES_MERGE_REF", 0, &oid, NULL);
838
0
  if (!o->local_ref)
839
0
    die(_("failed to resolve NOTES_MERGE_REF"));
840
841
0
  if (notes_merge_commit(o, t, partial, &oid))
842
0
    die(_("failed to finalize notes merge"));
843
844
  /* Reuse existing commit message in reflog message */
845
0
  memset(&pretty_ctx, 0, sizeof(pretty_ctx));
846
0
  repo_format_commit_message(the_repository, partial, "%s", &msg,
847
0
           &pretty_ctx);
848
0
  strbuf_trim(&msg);
849
0
  strbuf_insertstr(&msg, 0, "notes: ");
850
0
  update_ref(msg.buf, o->local_ref, &oid,
851
0
       is_null_oid(&parent_oid) ? NULL : &parent_oid,
852
0
       0, UPDATE_REFS_DIE_ON_ERR);
853
854
0
  free_notes(t);
855
0
  strbuf_release(&msg);
856
0
  ret = merge_abort(o);
857
0
  free(local_ref_to_free);
858
0
  return ret;
859
0
}
860
861
static int git_config_get_notes_strategy(const char *key,
862
           enum notes_merge_strategy *strategy)
863
0
{
864
0
  char *value;
865
866
0
  if (git_config_get_string(key, &value))
867
0
    return 1;
868
0
  if (parse_notes_merge_strategy(value, strategy))
869
0
    git_die_config(key, _("unknown notes merge strategy %s"), value);
870
871
0
  free(value);
872
0
  return 0;
873
0
}
874
875
static int merge(int argc, const char **argv, const char *prefix)
876
0
{
877
0
  struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
878
0
  struct object_id result_oid;
879
0
  struct notes_tree *t;
880
0
  struct notes_merge_options o;
881
0
  int do_merge = 0, do_commit = 0, do_abort = 0;
882
0
  int verbosity = 0, result;
883
0
  const char *strategy = NULL;
884
0
  struct option options[] = {
885
0
    OPT_GROUP(N_("General options")),
886
0
    OPT__VERBOSITY(&verbosity),
887
0
    OPT_GROUP(N_("Merge options")),
888
0
    OPT_STRING('s', "strategy", &strategy, N_("strategy"),
889
0
         N_("resolve notes conflicts using the given strategy "
890
0
            "(manual/ours/theirs/union/cat_sort_uniq)")),
891
0
    OPT_GROUP(N_("Committing unmerged notes")),
892
0
    OPT_SET_INT_F(0, "commit", &do_commit,
893
0
            N_("finalize notes merge by committing unmerged notes"),
894
0
            1, PARSE_OPT_NONEG),
895
0
    OPT_GROUP(N_("Aborting notes merge resolution")),
896
0
    OPT_SET_INT_F(0, "abort", &do_abort,
897
0
            N_("abort notes merge"),
898
0
            1, PARSE_OPT_NONEG),
899
0
    OPT_END()
900
0
  };
901
902
0
  argc = parse_options(argc, argv, prefix, options,
903
0
           git_notes_merge_usage, 0);
904
905
0
  if (strategy || do_commit + do_abort == 0)
906
0
    do_merge = 1;
907
0
  if (do_merge + do_commit + do_abort != 1) {
908
0
    error(_("cannot mix --commit, --abort or -s/--strategy"));
909
0
    usage_with_options(git_notes_merge_usage, options);
910
0
  }
911
912
0
  if (do_merge && argc != 1) {
913
0
    error(_("must specify a notes ref to merge"));
914
0
    usage_with_options(git_notes_merge_usage, options);
915
0
  } else if (!do_merge && argc) {
916
0
    error(_("too many arguments"));
917
0
    usage_with_options(git_notes_merge_usage, options);
918
0
  }
919
920
0
  init_notes_merge_options(the_repository, &o);
921
0
  o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
922
923
0
  if (do_abort)
924
0
    return merge_abort(&o);
925
0
  if (do_commit)
926
0
    return merge_commit(&o);
927
928
0
  o.local_ref = default_notes_ref();
929
0
  strbuf_addstr(&remote_ref, argv[0]);
930
0
  expand_loose_notes_ref(&remote_ref);
931
0
  o.remote_ref = remote_ref.buf;
932
933
0
  t = init_notes_check("merge", NOTES_INIT_WRITABLE);
934
935
0
  if (strategy) {
936
0
    if (parse_notes_merge_strategy(strategy, &o.strategy)) {
937
0
      error(_("unknown -s/--strategy: %s"), strategy);
938
0
      usage_with_options(git_notes_merge_usage, options);
939
0
    }
940
0
  } else {
941
0
    struct strbuf merge_key = STRBUF_INIT;
942
0
    const char *short_ref = NULL;
943
944
0
    if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref))
945
0
      BUG("local ref %s is outside of refs/notes/",
946
0
          o.local_ref);
947
948
0
    strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref);
949
950
0
    if (git_config_get_notes_strategy(merge_key.buf, &o.strategy))
951
0
      git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
952
953
0
    strbuf_release(&merge_key);
954
0
  }
955
956
0
  strbuf_addf(&msg, "notes: Merged notes from %s into %s",
957
0
        remote_ref.buf, default_notes_ref());
958
0
  strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
959
960
0
  result = notes_merge(&o, t, &result_oid);
961
962
0
  if (result >= 0) /* Merge resulted (trivially) in result_oid */
963
    /* Update default notes ref with new commit */
964
0
    update_ref(msg.buf, default_notes_ref(), &result_oid, NULL, 0,
965
0
         UPDATE_REFS_DIE_ON_ERR);
966
0
  else { /* Merge has unresolved conflicts */
967
0
    struct worktree **worktrees;
968
0
    const struct worktree *wt;
969
    /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
970
0
    update_ref(msg.buf, "NOTES_MERGE_PARTIAL", &result_oid, NULL,
971
0
         0, UPDATE_REFS_DIE_ON_ERR);
972
    /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
973
0
    worktrees = get_worktrees();
974
0
    wt = find_shared_symref(worktrees, "NOTES_MERGE_REF",
975
0
          default_notes_ref());
976
0
    if (wt)
977
0
      die(_("a notes merge into %s is already in-progress at %s"),
978
0
          default_notes_ref(), wt->path);
979
0
    free_worktrees(worktrees);
980
0
    if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
981
0
      die(_("failed to store link to current notes ref (%s)"),
982
0
          default_notes_ref());
983
0
    fprintf(stderr, _("Automatic notes merge failed. Fix conflicts in %s "
984
0
          "and commit the result with 'git notes merge --commit', "
985
0
          "or abort the merge with 'git notes merge --abort'.\n"),
986
0
      git_path(NOTES_MERGE_WORKTREE));
987
0
  }
988
989
0
  free_notes(t);
990
0
  strbuf_release(&remote_ref);
991
0
  strbuf_release(&msg);
992
0
  return result < 0; /* return non-zero on conflicts */
993
0
}
994
995
0
#define IGNORE_MISSING 1
996
997
static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
998
0
{
999
0
  int status;
1000
0
  struct object_id oid;
1001
0
  if (repo_get_oid(the_repository, name, &oid))
1002
0
    return error(_("Failed to resolve '%s' as a valid ref."), name);
1003
0
  status = remove_note(t, oid.hash);
1004
0
  if (status)
1005
0
    fprintf(stderr, _("Object %s has no note\n"), name);
1006
0
  else
1007
0
    fprintf(stderr, _("Removing note for object %s\n"), name);
1008
0
  return (flag & IGNORE_MISSING) ? 0 : status;
1009
0
}
1010
1011
static int remove_cmd(int argc, const char **argv, const char *prefix)
1012
0
{
1013
0
  unsigned flag = 0;
1014
0
  int from_stdin = 0;
1015
0
  struct option options[] = {
1016
0
    OPT_BIT(0, "ignore-missing", &flag,
1017
0
      N_("attempt to remove non-existent note is not an error"),
1018
0
      IGNORE_MISSING),
1019
0
    OPT_BOOL(0, "stdin", &from_stdin,
1020
0
          N_("read object names from the standard input")),
1021
0
    OPT_END()
1022
0
  };
1023
0
  struct notes_tree *t;
1024
0
  int retval = 0;
1025
1026
0
  argc = parse_options(argc, argv, prefix, options,
1027
0
           git_notes_remove_usage, 0);
1028
1029
0
  t = init_notes_check("remove", NOTES_INIT_WRITABLE);
1030
1031
0
  if (!argc && !from_stdin) {
1032
0
    retval = remove_one_note(t, "HEAD", flag);
1033
0
  } else {
1034
0
    while (*argv) {
1035
0
      retval |= remove_one_note(t, *argv, flag);
1036
0
      argv++;
1037
0
    }
1038
0
  }
1039
0
  if (from_stdin) {
1040
0
    struct strbuf sb = STRBUF_INIT;
1041
0
    while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1042
0
      strbuf_rtrim(&sb);
1043
0
      retval |= remove_one_note(t, sb.buf, flag);
1044
0
    }
1045
0
    strbuf_release(&sb);
1046
0
  }
1047
0
  if (!retval)
1048
0
    commit_notes(the_repository, t,
1049
0
           "Notes removed by 'git notes remove'");
1050
0
  free_notes(t);
1051
0
  return retval;
1052
0
}
1053
1054
static int prune(int argc, const char **argv, const char *prefix)
1055
0
{
1056
0
  struct notes_tree *t;
1057
0
  int show_only = 0, verbose = 0;
1058
0
  struct option options[] = {
1059
0
    OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
1060
0
    OPT__VERBOSE(&verbose, N_("report pruned notes")),
1061
0
    OPT_END()
1062
0
  };
1063
1064
0
  argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
1065
0
           0);
1066
1067
0
  if (argc) {
1068
0
    error(_("too many arguments"));
1069
0
    usage_with_options(git_notes_prune_usage, options);
1070
0
  }
1071
1072
0
  t = init_notes_check("prune", NOTES_INIT_WRITABLE);
1073
1074
0
  prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
1075
0
    (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
1076
0
  if (!show_only)
1077
0
    commit_notes(the_repository, t,
1078
0
           "Notes removed by 'git notes prune'");
1079
0
  free_notes(t);
1080
0
  return 0;
1081
0
}
1082
1083
static int get_ref(int argc, const char **argv, const char *prefix)
1084
0
{
1085
0
  struct option options[] = { OPT_END() };
1086
0
  argc = parse_options(argc, argv, prefix, options,
1087
0
           git_notes_get_ref_usage, 0);
1088
1089
0
  if (argc) {
1090
0
    error(_("too many arguments"));
1091
0
    usage_with_options(git_notes_get_ref_usage, options);
1092
0
  }
1093
1094
0
  puts(default_notes_ref());
1095
0
  return 0;
1096
0
}
1097
1098
int cmd_notes(int argc, const char **argv, const char *prefix)
1099
0
{
1100
0
  const char *override_notes_ref = NULL;
1101
0
  parse_opt_subcommand_fn *fn = NULL;
1102
0
  struct option options[] = {
1103
0
    OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
1104
0
         N_("use notes from <notes-ref>")),
1105
0
    OPT_SUBCOMMAND("list", &fn, list),
1106
0
    OPT_SUBCOMMAND("add", &fn, add),
1107
0
    OPT_SUBCOMMAND("copy", &fn, copy),
1108
0
    OPT_SUBCOMMAND("append", &fn, append_edit),
1109
0
    OPT_SUBCOMMAND("edit", &fn, append_edit),
1110
0
    OPT_SUBCOMMAND("show", &fn, show),
1111
0
    OPT_SUBCOMMAND("merge", &fn, merge),
1112
0
    OPT_SUBCOMMAND("remove", &fn, remove_cmd),
1113
0
    OPT_SUBCOMMAND("prune", &fn, prune),
1114
0
    OPT_SUBCOMMAND("get-ref", &fn, get_ref),
1115
0
    OPT_END()
1116
0
  };
1117
1118
0
  git_config(git_default_config, NULL);
1119
0
  argc = parse_options(argc, argv, prefix, options, git_notes_usage,
1120
0
           PARSE_OPT_SUBCOMMAND_OPTIONAL);
1121
0
  if (!fn) {
1122
0
    if (argc) {
1123
0
      error(_("unknown subcommand: `%s'"), argv[0]);
1124
0
      usage_with_options(git_notes_usage, options);
1125
0
    }
1126
0
    fn = list;
1127
0
  }
1128
1129
0
  if (override_notes_ref) {
1130
0
    struct strbuf sb = STRBUF_INIT;
1131
0
    strbuf_addstr(&sb, override_notes_ref);
1132
0
    expand_notes_ref(&sb);
1133
0
    setenv("GIT_NOTES_REF", sb.buf, 1);
1134
0
    strbuf_release(&sb);
1135
0
  }
1136
1137
0
  return !!fn(argc, argv, prefix);
1138
0
}