-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequencer.c
5934 lines (5173 loc) · 164 KB
/
sequencer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "cache.h"
#include "config.h"
#include "lockfile.h"
#include "dir.h"
#include "object-store.h"
#include "object.h"
#include "commit.h"
#include "sequencer.h"
#include "tag.h"
#include "run-command.h"
#include "hook.h"
#include "exec-cmd.h"
#include "utf8.h"
#include "cache-tree.h"
#include "diff.h"
#include "revision.h"
#include "rerere.h"
#include "merge-ort.h"
#include "merge-ort-wrappers.h"
#include "refs.h"
#include "strvec.h"
#include "quote.h"
#include "trailer.h"
#include "log-tree.h"
#include "wt-status.h"
#include "hashmap.h"
#include "notes-utils.h"
#include "sigchain.h"
#include "unpack-trees.h"
#include "worktree.h"
#include "oidmap.h"
#include "oidset.h"
#include "commit-slab.h"
#include "alias.h"
#include "commit-reach.h"
#include "rebase-interactive.h"
#include "reset.h"
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
static const char sign_off_header[] = "Signed-off-by: ";
static const char cherry_picked_prefix[] = "(cherry picked from commit ";
GIT_PATH_FUNC(git_path_commit_editmsg, "COMMIT_EDITMSG")
static GIT_PATH_FUNC(git_path_seq_dir, "sequencer")
static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo")
static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts")
static GIT_PATH_FUNC(git_path_head_file, "sequencer/head")
static GIT_PATH_FUNC(git_path_abort_safety_file, "sequencer/abort-safety")
static GIT_PATH_FUNC(rebase_path, "rebase-merge")
/*
* The file containing rebase commands, comments, and empty lines.
* This file is created by "git rebase -i" then edited by the user. As
* the lines are processed, they are removed from the front of this
* file and written to the tail of 'done'.
*/
GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo")
GIT_PATH_FUNC(rebase_path_todo_backup, "rebase-merge/git-rebase-todo.backup")
GIT_PATH_FUNC(rebase_path_dropped, "rebase-merge/dropped")
/*
* The rebase command lines that have already been processed. A line
* is moved here when it is first handled, before any associated user
* actions.
*/
static GIT_PATH_FUNC(rebase_path_done, "rebase-merge/done")
/*
* The file to keep track of how many commands were already processed (e.g.
* for the prompt).
*/
static GIT_PATH_FUNC(rebase_path_msgnum, "rebase-merge/msgnum")
/*
* The file to keep track of how many commands are to be processed in total
* (e.g. for the prompt).
*/
static GIT_PATH_FUNC(rebase_path_msgtotal, "rebase-merge/end")
/*
* The commit message that is planned to be used for any changes that
* need to be committed following a user interaction.
*/
static GIT_PATH_FUNC(rebase_path_message, "rebase-merge/message")
/*
* The file into which is accumulated the suggested commit message for
* squash/fixup commands. When the first of a series of squash/fixups
* is seen, the file is created and the commit message from the
* previous commit and from the first squash/fixup commit are written
* to it. The commit message for each subsequent squash/fixup commit
* is appended to the file as it is processed.
*/
static GIT_PATH_FUNC(rebase_path_squash_msg, "rebase-merge/message-squash")
/*
* If the current series of squash/fixups has not yet included a squash
* command, then this file exists and holds the commit message of the
* original "pick" commit. (If the series ends without a "squash"
* command, then this can be used as the commit message of the combined
* commit without opening the editor.)
*/
static GIT_PATH_FUNC(rebase_path_fixup_msg, "rebase-merge/message-fixup")
/*
* This file contains the list fixup/squash commands that have been
* accumulated into message-fixup or message-squash so far.
*/
static GIT_PATH_FUNC(rebase_path_current_fixups, "rebase-merge/current-fixups")
/*
* A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
* GIT_AUTHOR_DATE that will be used for the commit that is currently
* being rebased.
*/
static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script")
/*
* When an "edit" rebase command is being processed, the SHA1 of the
* commit to be edited is recorded in this file. When "git rebase
* --continue" is executed, if there are any staged changes then they
* will be amended to the HEAD commit, but only provided the HEAD
* commit is still the commit to be edited. When any other rebase
* command is processed, this file is deleted.
*/
static GIT_PATH_FUNC(rebase_path_amend, "rebase-merge/amend")
/*
* When we stop at a given patch via the "edit" command, this file contains
* the commit object name of the corresponding patch.
*/
static GIT_PATH_FUNC(rebase_path_stopped_sha, "rebase-merge/stopped-sha")
/*
* For the post-rewrite hook, we make a list of rewritten commits and
* their new sha1s. The rewritten-pending list keeps the sha1s of
* commits that have been processed, but not committed yet,
* e.g. because they are waiting for a 'squash' command.
*/
static GIT_PATH_FUNC(rebase_path_rewritten_list, "rebase-merge/rewritten-list")
static GIT_PATH_FUNC(rebase_path_rewritten_pending,
"rebase-merge/rewritten-pending")
/*
* The path of the file containing the OID of the "squash onto" commit, i.e.
* the dummy commit used for `reset [new root]`.
*/
static GIT_PATH_FUNC(rebase_path_squash_onto, "rebase-merge/squash-onto")
/*
* The path of the file listing refs that need to be deleted after the rebase
* finishes. This is used by the `label` command to record the need for cleanup.
*/
static GIT_PATH_FUNC(rebase_path_refs_to_delete, "rebase-merge/refs-to-delete")
/*
* The following files are written by git-rebase just after parsing the
* command-line.
*/
static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt")
static GIT_PATH_FUNC(rebase_path_cdate_is_adate, "rebase-merge/cdate_is_adate")
static GIT_PATH_FUNC(rebase_path_ignore_date, "rebase-merge/ignore_date")
static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head")
static GIT_PATH_FUNC(rebase_path_verbose, "rebase-merge/verbose")
static GIT_PATH_FUNC(rebase_path_quiet, "rebase-merge/quiet")
static GIT_PATH_FUNC(rebase_path_signoff, "rebase-merge/signoff")
static GIT_PATH_FUNC(rebase_path_head_name, "rebase-merge/head-name")
static GIT_PATH_FUNC(rebase_path_onto, "rebase-merge/onto")
static GIT_PATH_FUNC(rebase_path_autostash, "rebase-merge/autostash")
static GIT_PATH_FUNC(rebase_path_strategy, "rebase-merge/strategy")
static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts")
static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate, "rebase-merge/allow_rerere_autoupdate")
static GIT_PATH_FUNC(rebase_path_reschedule_failed_exec, "rebase-merge/reschedule-failed-exec")
static GIT_PATH_FUNC(rebase_path_no_reschedule_failed_exec, "rebase-merge/no-reschedule-failed-exec")
static GIT_PATH_FUNC(rebase_path_drop_redundant_commits, "rebase-merge/drop_redundant_commits")
static GIT_PATH_FUNC(rebase_path_keep_redundant_commits, "rebase-merge/keep_redundant_commits")
static int git_sequencer_config(const char *k, const char *v, void *cb)
{
struct replay_opts *opts = cb;
int status;
if (!strcmp(k, "commit.cleanup")) {
const char *s;
status = git_config_string(&s, k, v);
if (status)
return status;
if (!strcmp(s, "verbatim")) {
opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_NONE;
opts->explicit_cleanup = 1;
} else if (!strcmp(s, "whitespace")) {
opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SPACE;
opts->explicit_cleanup = 1;
} else if (!strcmp(s, "strip")) {
opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_ALL;
opts->explicit_cleanup = 1;
} else if (!strcmp(s, "scissors")) {
opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SCISSORS;
opts->explicit_cleanup = 1;
} else {
warning(_("invalid commit message cleanup mode '%s'"),
s);
}
free((char *)s);
return status;
}
if (!strcmp(k, "commit.gpgsign")) {
opts->gpg_sign = git_config_bool(k, v) ? xstrdup("") : NULL;
return 0;
}
if (!opts->default_strategy && !strcmp(k, "pull.twohead")) {
int ret = git_config_string((const char**)&opts->default_strategy, k, v);
if (ret == 0) {
/*
* pull.twohead is allowed to be multi-valued; we only
* care about the first value.
*/
char *tmp = strchr(opts->default_strategy, ' ');
if (tmp)
*tmp = '\0';
}
return ret;
}
status = git_gpg_config(k, v, NULL);
if (status)
return status;
return git_diff_basic_config(k, v, NULL);
}
void sequencer_init_config(struct replay_opts *opts)
{
opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_NONE;
git_config(git_sequencer_config, opts);
}
static inline int is_rebase_i(const struct replay_opts *opts)
{
return opts->action == REPLAY_INTERACTIVE_REBASE;
}
static const char *get_dir(const struct replay_opts *opts)
{
if (is_rebase_i(opts))
return rebase_path();
return git_path_seq_dir();
}
static const char *get_todo_path(const struct replay_opts *opts)
{
if (is_rebase_i(opts))
return rebase_path_todo();
return git_path_todo_file();
}
/*
* Returns 0 for non-conforming footer
* Returns 1 for conforming footer
* Returns 2 when sob exists within conforming footer
* Returns 3 when sob exists within conforming footer as last entry
*/
static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
size_t ignore_footer)
{
struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
struct trailer_info info;
size_t i;
int found_sob = 0, found_sob_last = 0;
char saved_char;
opts.no_divider = 1;
if (ignore_footer) {
saved_char = sb->buf[sb->len - ignore_footer];
sb->buf[sb->len - ignore_footer] = '\0';
}
trailer_info_get(&info, sb->buf, &opts);
if (ignore_footer)
sb->buf[sb->len - ignore_footer] = saved_char;
if (info.trailer_start == info.trailer_end)
return 0;
for (i = 0; i < info.trailer_nr; i++)
if (sob && !strncmp(info.trailers[i], sob->buf, sob->len)) {
found_sob = 1;
if (i == info.trailer_nr - 1)
found_sob_last = 1;
}
trailer_info_release(&info);
if (found_sob_last)
return 3;
if (found_sob)
return 2;
return 1;
}
static const char *gpg_sign_opt_quoted(struct replay_opts *opts)
{
static struct strbuf buf = STRBUF_INIT;
strbuf_reset(&buf);
if (opts->gpg_sign)
sq_quotef(&buf, "-S%s", opts->gpg_sign);
return buf.buf;
}
int sequencer_remove_state(struct replay_opts *opts)
{
struct strbuf buf = STRBUF_INIT;
int i, ret = 0;
if (is_rebase_i(opts) &&
strbuf_read_file(&buf, rebase_path_refs_to_delete(), 0) > 0) {
char *p = buf.buf;
while (*p) {
char *eol = strchr(p, '\n');
if (eol)
*eol = '\0';
if (delete_ref("(rebase) cleanup", p, NULL, 0) < 0) {
warning(_("could not delete '%s'"), p);
ret = -1;
}
if (!eol)
break;
p = eol + 1;
}
}
free(opts->gpg_sign);
free(opts->default_strategy);
free(opts->strategy);
for (i = 0; i < opts->xopts_nr; i++)
free(opts->xopts[i]);
free(opts->xopts);
strbuf_release(&opts->current_fixups);
strbuf_reset(&buf);
strbuf_addstr(&buf, get_dir(opts));
if (remove_dir_recursively(&buf, 0))
ret = error(_("could not remove '%s'"), buf.buf);
strbuf_release(&buf);
return ret;
}
static const char *action_name(const struct replay_opts *opts)
{
switch (opts->action) {
case REPLAY_REVERT:
return N_("revert");
case REPLAY_PICK:
return N_("cherry-pick");
case REPLAY_INTERACTIVE_REBASE:
return N_("rebase");
}
die(_("unknown action: %d"), opts->action);
}
struct commit_message {
char *parent_label;
char *label;
char *subject;
const char *message;
};
static const char *short_commit_name(struct commit *commit)
{
return find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV);
}
static int get_message(struct commit *commit, struct commit_message *out)
{
const char *abbrev, *subject;
int subject_len;
out->message = logmsg_reencode(commit, NULL, get_commit_output_encoding());
abbrev = short_commit_name(commit);
subject_len = find_commit_subject(out->message, &subject);
out->subject = xmemdupz(subject, subject_len);
out->label = xstrfmt("%s (%s)", abbrev, out->subject);
out->parent_label = xstrfmt("parent of %s", out->label);
return 0;
}
static void free_message(struct commit *commit, struct commit_message *msg)
{
free(msg->parent_label);
free(msg->label);
free(msg->subject);
unuse_commit_buffer(commit, msg->message);
}
static void print_advice(struct repository *r, int show_hint,
struct replay_opts *opts)
{
char *msg = getenv("GIT_CHERRY_PICK_HELP");
if (msg) {
advise("%s\n", msg);
/*
* A conflict has occurred but the porcelain
* (typically rebase --interactive) wants to take care
* of the commit itself so remove CHERRY_PICK_HEAD
*/
refs_delete_ref(get_main_ref_store(r), "", "CHERRY_PICK_HEAD",
NULL, 0);
return;
}
if (show_hint) {
if (opts->no_commit)
advise(_("after resolving the conflicts, mark the corrected paths\n"
"with 'git add <paths>' or 'git rm <paths>'"));
else if (opts->action == REPLAY_PICK)
advise(_("After resolving the conflicts, mark them with\n"
"\"git add/rm <pathspec>\", then run\n"
"\"git cherry-pick --continue\".\n"
"You can instead skip this commit with \"git cherry-pick --skip\".\n"
"To abort and get back to the state before \"git cherry-pick\",\n"
"run \"git cherry-pick --abort\"."));
else if (opts->action == REPLAY_REVERT)
advise(_("After resolving the conflicts, mark them with\n"
"\"git add/rm <pathspec>\", then run\n"
"\"git revert --continue\".\n"
"You can instead skip this commit with \"git revert --skip\".\n"
"To abort and get back to the state before \"git revert\",\n"
"run \"git revert --abort\"."));
else
BUG("unexpected pick action in print_advice()");
}
}
static int write_message(const void *buf, size_t len, const char *filename,
int append_eol)
{
struct lock_file msg_file = LOCK_INIT;
int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0);
if (msg_fd < 0)
return error_errno(_("could not lock '%s'"), filename);
if (write_in_full(msg_fd, buf, len) < 0) {
error_errno(_("could not write to '%s'"), filename);
rollback_lock_file(&msg_file);
return -1;
}
if (append_eol && write(msg_fd, "\n", 1) < 0) {
error_errno(_("could not write eol to '%s'"), filename);
rollback_lock_file(&msg_file);
return -1;
}
if (commit_lock_file(&msg_file) < 0)
return error(_("failed to finalize '%s'"), filename);
return 0;
}
int read_oneliner(struct strbuf *buf,
const char *path, unsigned flags)
{
int orig_len = buf->len;
if (strbuf_read_file(buf, path, 0) < 0) {
if ((flags & READ_ONELINER_WARN_MISSING) ||
(errno != ENOENT && errno != ENOTDIR))
warning_errno(_("could not read '%s'"), path);
return 0;
}
if (buf->len > orig_len && buf->buf[buf->len - 1] == '\n') {
if (--buf->len > orig_len && buf->buf[buf->len - 1] == '\r')
--buf->len;
buf->buf[buf->len] = '\0';
}
if ((flags & READ_ONELINER_SKIP_IF_EMPTY) && buf->len == orig_len)
return 0;
return 1;
}
static struct tree *empty_tree(struct repository *r)
{
return lookup_tree(r, the_hash_algo->empty_tree);
}
static int error_dirty_index(struct repository *repo, struct replay_opts *opts)
{
if (repo_read_index_unmerged(repo))
return error_resolve_conflict(_(action_name(opts)));
error(_("your local changes would be overwritten by %s."),
_(action_name(opts)));
if (advice_enabled(ADVICE_COMMIT_BEFORE_MERGE))
advise(_("commit your changes or stash them to proceed."));
return -1;
}
static void update_abort_safety_file(void)
{
struct object_id head;
/* Do nothing on a single-pick */
if (!file_exists(git_path_seq_dir()))
return;
if (!get_oid("HEAD", &head))
write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head));
else
write_file(git_path_abort_safety_file(), "%s", "");
}
static int fast_forward_to(struct repository *r,
const struct object_id *to,
const struct object_id *from,
int unborn,
struct replay_opts *opts)
{
struct ref_transaction *transaction;
struct strbuf sb = STRBUF_INIT;
struct strbuf err = STRBUF_INIT;
repo_read_index(r);
if (checkout_fast_forward(r, from, to, 1))
return -1; /* the callee should have complained already */
strbuf_addf(&sb, _("%s: fast-forward"), _(action_name(opts)));
transaction = ref_transaction_begin(&err);
if (!transaction ||
ref_transaction_update(transaction, "HEAD",
to, unborn && !is_rebase_i(opts) ?
null_oid() : from,
0, sb.buf, &err) ||
ref_transaction_commit(transaction, &err)) {
ref_transaction_free(transaction);
error("%s", err.buf);
strbuf_release(&sb);
strbuf_release(&err);
return -1;
}
strbuf_release(&sb);
strbuf_release(&err);
ref_transaction_free(transaction);
update_abort_safety_file();
return 0;
}
enum commit_msg_cleanup_mode get_cleanup_mode(const char *cleanup_arg,
int use_editor)
{
if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
return use_editor ? COMMIT_MSG_CLEANUP_ALL :
COMMIT_MSG_CLEANUP_SPACE;
else if (!strcmp(cleanup_arg, "verbatim"))
return COMMIT_MSG_CLEANUP_NONE;
else if (!strcmp(cleanup_arg, "whitespace"))
return COMMIT_MSG_CLEANUP_SPACE;
else if (!strcmp(cleanup_arg, "strip"))
return COMMIT_MSG_CLEANUP_ALL;
else if (!strcmp(cleanup_arg, "scissors"))
return use_editor ? COMMIT_MSG_CLEANUP_SCISSORS :
COMMIT_MSG_CLEANUP_SPACE;
else
die(_("Invalid cleanup mode %s"), cleanup_arg);
}
/*
* NB using int rather than enum cleanup_mode to stop clang's
* -Wtautological-constant-out-of-range-compare complaining that the comparison
* is always true.
*/
static const char *describe_cleanup_mode(int cleanup_mode)
{
static const char *modes[] = { "whitespace",
"verbatim",
"scissors",
"strip" };
if (cleanup_mode < ARRAY_SIZE(modes))
return modes[cleanup_mode];
BUG("invalid cleanup_mode provided (%d)", cleanup_mode);
}
void append_conflicts_hint(struct index_state *istate,
struct strbuf *msgbuf, enum commit_msg_cleanup_mode cleanup_mode)
{
int i;
if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
strbuf_addch(msgbuf, '\n');
wt_status_append_cut_line(msgbuf);
strbuf_addch(msgbuf, comment_line_char);
}
strbuf_addch(msgbuf, '\n');
strbuf_commented_addf(msgbuf, "Conflicts:\n");
for (i = 0; i < istate->cache_nr;) {
const struct cache_entry *ce = istate->cache[i++];
if (ce_stage(ce)) {
strbuf_commented_addf(msgbuf, "\t%s\n", ce->name);
while (i < istate->cache_nr &&
!strcmp(ce->name, istate->cache[i]->name))
i++;
}
}
}
static int do_recursive_merge(struct repository *r,
struct commit *base, struct commit *next,
const char *base_label, const char *next_label,
struct object_id *head, struct strbuf *msgbuf,
struct replay_opts *opts)
{
struct merge_options o;
struct merge_result result;
struct tree *next_tree, *base_tree, *head_tree;
int clean, show_output;
int i;
struct lock_file index_lock = LOCK_INIT;
if (repo_hold_locked_index(r, &index_lock, LOCK_REPORT_ON_ERROR) < 0)
return -1;
repo_read_index(r);
init_merge_options(&o, r);
o.ancestor = base ? base_label : "(empty tree)";
o.branch1 = "HEAD";
o.branch2 = next ? next_label : "(empty tree)";
if (is_rebase_i(opts))
o.buffer_output = 2;
o.show_rename_progress = 1;
head_tree = parse_tree_indirect(head);
next_tree = next ? get_commit_tree(next) : empty_tree(r);
base_tree = base ? get_commit_tree(base) : empty_tree(r);
for (i = 0; i < opts->xopts_nr; i++)
parse_merge_opt(&o, opts->xopts[i]);
if (!opts->strategy || !strcmp(opts->strategy, "ort")) {
memset(&result, 0, sizeof(result));
merge_incore_nonrecursive(&o, base_tree, head_tree, next_tree,
&result);
show_output = !is_rebase_i(opts) || !result.clean;
/*
* TODO: merge_switch_to_result will update index/working tree;
* we only really want to do that if !result.clean || this is
* the final patch to be picked. But determining this is the
* final patch would take some work, and "head_tree" would need
* to be replace with the tree the index matched before we
* started doing any picks.
*/
merge_switch_to_result(&o, head_tree, &result, 1, show_output);
clean = result.clean;
} else {
ensure_full_index(r->index);
clean = merge_trees(&o, head_tree, next_tree, base_tree);
if (is_rebase_i(opts) && clean <= 0)
fputs(o.obuf.buf, stdout);
strbuf_release(&o.obuf);
}
if (clean < 0) {
rollback_lock_file(&index_lock);
return clean;
}
if (write_locked_index(r->index, &index_lock,
COMMIT_LOCK | SKIP_IF_UNCHANGED))
/*
* TRANSLATORS: %s will be "revert", "cherry-pick" or
* "rebase".
*/
return error(_("%s: Unable to write new index file"),
_(action_name(opts)));
if (!clean)
append_conflicts_hint(r->index, msgbuf,
opts->default_msg_cleanup);
return !clean;
}
static struct object_id *get_cache_tree_oid(struct index_state *istate)
{
if (!cache_tree_fully_valid(istate->cache_tree))
if (cache_tree_update(istate, 0)) {
error(_("unable to update cache tree"));
return NULL;
}
return &istate->cache_tree->oid;
}
static int is_index_unchanged(struct repository *r)
{
struct object_id head_oid, *cache_tree_oid;
struct commit *head_commit;
struct index_state *istate = r->index;
if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
return error(_("could not resolve HEAD commit"));
head_commit = lookup_commit(r, &head_oid);
/*
* If head_commit is NULL, check_commit, called from
* lookup_commit, would have indicated that head_commit is not
* a commit object already. parse_commit() will return failure
* without further complaints in such a case. Otherwise, if
* the commit is invalid, parse_commit() will complain. So
* there is nothing for us to say here. Just return failure.
*/
if (parse_commit(head_commit))
return -1;
if (!(cache_tree_oid = get_cache_tree_oid(istate)))
return -1;
return oideq(cache_tree_oid, get_commit_tree_oid(head_commit));
}
static int write_author_script(const char *message)
{
struct strbuf buf = STRBUF_INIT;
const char *eol;
int res;
for (;;)
if (!*message || starts_with(message, "\n")) {
missing_author:
/* Missing 'author' line? */
unlink(rebase_path_author_script());
return 0;
} else if (skip_prefix(message, "author ", &message))
break;
else if ((eol = strchr(message, '\n')))
message = eol + 1;
else
goto missing_author;
strbuf_addstr(&buf, "GIT_AUTHOR_NAME='");
while (*message && *message != '\n' && *message != '\r')
if (skip_prefix(message, " <", &message))
break;
else if (*message != '\'')
strbuf_addch(&buf, *(message++));
else
strbuf_addf(&buf, "'\\%c'", *(message++));
strbuf_addstr(&buf, "'\nGIT_AUTHOR_EMAIL='");
while (*message && *message != '\n' && *message != '\r')
if (skip_prefix(message, "> ", &message))
break;
else if (*message != '\'')
strbuf_addch(&buf, *(message++));
else
strbuf_addf(&buf, "'\\%c'", *(message++));
strbuf_addstr(&buf, "'\nGIT_AUTHOR_DATE='@");
while (*message && *message != '\n' && *message != '\r')
if (*message != '\'')
strbuf_addch(&buf, *(message++));
else
strbuf_addf(&buf, "'\\%c'", *(message++));
strbuf_addch(&buf, '\'');
res = write_message(buf.buf, buf.len, rebase_path_author_script(), 1);
strbuf_release(&buf);
return res;
}
/**
* Take a series of KEY='VALUE' lines where VALUE part is
* sq-quoted, and append <KEY, VALUE> at the end of the string list
*/
static int parse_key_value_squoted(char *buf, struct string_list *list)
{
while (*buf) {
struct string_list_item *item;
char *np;
char *cp = strchr(buf, '=');
if (!cp) {
np = strchrnul(buf, '\n');
return error(_("no key present in '%.*s'"),
(int) (np - buf), buf);
}
np = strchrnul(cp, '\n');
*cp++ = '\0';
item = string_list_append(list, buf);
buf = np + (*np == '\n');
*np = '\0';
cp = sq_dequote(cp);
if (!cp)
return error(_("unable to dequote value of '%s'"),
item->string);
item->util = xstrdup(cp);
}
return 0;
}
/**
* Reads and parses the state directory's "author-script" file, and sets name,
* email and date accordingly.
* Returns 0 on success, -1 if the file could not be parsed.
*
* The author script is of the format:
*
* GIT_AUTHOR_NAME='$author_name'
* GIT_AUTHOR_EMAIL='$author_email'
* GIT_AUTHOR_DATE='$author_date'
*
* where $author_name, $author_email and $author_date are quoted. We are strict
* with our parsing, as the file was meant to be eval'd in the now-removed
* git-am.sh/git-rebase--interactive.sh scripts, and thus if the file differs
* from what this function expects, it is better to bail out than to do
* something that the user does not expect.
*/
int read_author_script(const char *path, char **name, char **email, char **date,
int allow_missing)
{
struct strbuf buf = STRBUF_INIT;
struct string_list kv = STRING_LIST_INIT_DUP;
int retval = -1; /* assume failure */
int i, name_i = -2, email_i = -2, date_i = -2, err = 0;
if (strbuf_read_file(&buf, path, 256) <= 0) {
strbuf_release(&buf);
if (errno == ENOENT && allow_missing)
return 0;
else
return error_errno(_("could not open '%s' for reading"),
path);
}
if (parse_key_value_squoted(buf.buf, &kv))
goto finish;
for (i = 0; i < kv.nr; i++) {
if (!strcmp(kv.items[i].string, "GIT_AUTHOR_NAME")) {
if (name_i != -2)
name_i = error(_("'GIT_AUTHOR_NAME' already given"));
else
name_i = i;
} else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_EMAIL")) {
if (email_i != -2)
email_i = error(_("'GIT_AUTHOR_EMAIL' already given"));
else
email_i = i;
} else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_DATE")) {
if (date_i != -2)
date_i = error(_("'GIT_AUTHOR_DATE' already given"));
else
date_i = i;
} else {
err = error(_("unknown variable '%s'"),
kv.items[i].string);
}
}
if (name_i == -2)
error(_("missing 'GIT_AUTHOR_NAME'"));
if (email_i == -2)
error(_("missing 'GIT_AUTHOR_EMAIL'"));
if (date_i == -2)
error(_("missing 'GIT_AUTHOR_DATE'"));
if (date_i < 0 || email_i < 0 || date_i < 0 || err)
goto finish;
*name = kv.items[name_i].util;
*email = kv.items[email_i].util;
*date = kv.items[date_i].util;
retval = 0;
finish:
string_list_clear(&kv, !!retval);
strbuf_release(&buf);
return retval;
}
/*
* Read a GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL AND GIT_AUTHOR_DATE from a
* file with shell quoting into struct strvec. Returns -1 on
* error, 0 otherwise.
*/
static int read_env_script(struct strvec *env)
{
char *name, *email, *date;
if (read_author_script(rebase_path_author_script(),
&name, &email, &date, 0))
return -1;
strvec_pushf(env, "GIT_AUTHOR_NAME=%s", name);
strvec_pushf(env, "GIT_AUTHOR_EMAIL=%s", email);
strvec_pushf(env, "GIT_AUTHOR_DATE=%s", date);
free(name);
free(email);
free(date);
return 0;
}
static char *get_author(const char *message)
{
size_t len;
const char *a;
a = find_commit_header(message, "author", &len);
if (a)
return xmemdupz(a, len);
return NULL;
}
static const char *author_date_from_env_array(const struct strvec *env)
{
int i;
const char *date;
for (i = 0; i < env->nr; i++)
if (skip_prefix(env->v[i],
"GIT_AUTHOR_DATE=", &date))
return date;
/*
* If GIT_AUTHOR_DATE is missing we should have already errored out when
* reading the script
*/
BUG("GIT_AUTHOR_DATE missing from author script");
}
static const char staged_changes_advice[] =
N_("you have staged changes in your working tree\n"
"If these changes are meant to be squashed into the previous commit, run:\n"
"\n"
" git commit --amend %s\n"
"\n"
"If they are meant to go into a new commit, run:\n"
"\n"
" git commit %s\n"
"\n"
"In both cases, once you're done, continue with:\n"
"\n"
" git rebase --continue\n");
#define ALLOW_EMPTY (1<<0)
#define EDIT_MSG (1<<1)
#define AMEND_MSG (1<<2)
#define CLEANUP_MSG (1<<3)
#define VERIFY_MSG (1<<4)
#define CREATE_ROOT_COMMIT (1<<5)
#define VERBATIM_MSG (1<<6)
static int run_command_silent_on_success(struct child_process *cmd)
{
struct strbuf buf = STRBUF_INIT;
int rc;
cmd->stdout_to_stderr = 1;
rc = pipe_command(cmd,
NULL, 0,
NULL, 0,
&buf, 0);
if (rc)
fputs(buf.buf, stderr);
strbuf_release(&buf);
return rc;
}
/*
* If we are cherry-pick, and if the merge did not result in
* hand-editing, we will hit this commit and inherit the original
* author date and name.
*
* If we are revert, or if our cherry-pick results in a hand merge,
* we had better say that the current user is responsible for that.
*
* An exception is when run_git_commit() is called during an
* interactive rebase: in that case, we will want to retain the
* author metadata.
*/
static int run_git_commit(const char *defmsg,
struct replay_opts *opts,
unsigned int flags)
{
struct child_process cmd = CHILD_PROCESS_INIT;
if ((flags & CLEANUP_MSG) && (flags & VERBATIM_MSG))
BUG("CLEANUP_MSG and VERBATIM_MSG are mutually exclusive");
cmd.git_cmd = 1;
if (is_rebase_i(opts) &&