Coverage Report

Created: 2026-12-01 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/rebase-interactive.c
Line
Count
Source
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "commit.h"
5
#include "editor.h"
6
#include "environment.h"
7
#include "gettext.h"
8
#include "sequencer.h"
9
#include "rebase-interactive.h"
10
#include "repository.h"
11
#include "strbuf.h"
12
#include "commit-slab.h"
13
#include "config.h"
14
#include "dir.h"
15
#include "object-name.h"
16
17
static const char edit_todo_list_advice[] =
18
N_("You can fix this with 'git rebase --edit-todo' "
19
"and then run 'git rebase --continue'.\n"
20
"Or you can abort the rebase with 'git rebase"
21
" --abort'.\n");
22
23
enum missing_commit_check_level {
24
  MISSING_COMMIT_CHECK_IGNORE = 0,
25
  MISSING_COMMIT_CHECK_WARN,
26
  MISSING_COMMIT_CHECK_ERROR
27
};
28
29
static enum missing_commit_check_level get_missing_commit_check_level(void)
30
0
{
31
0
  const char *value;
32
33
0
  if (repo_config_get_value(the_repository, "rebase.missingcommitscheck", &value))
34
0
    return MISSING_COMMIT_CHECK_IGNORE;
35
0
  if (!value) {
36
0
    warning(_("missing value for option "
37
0
        "rebase.missingCommitsCheck. Ignoring."));
38
0
    return MISSING_COMMIT_CHECK_IGNORE;
39
0
  }
40
0
  if (!strcasecmp("ignore", value))
41
0
    return MISSING_COMMIT_CHECK_IGNORE;
42
0
  if (!strcasecmp("warn", value))
43
0
    return MISSING_COMMIT_CHECK_WARN;
44
0
  if (!strcasecmp("error", value))
45
0
    return MISSING_COMMIT_CHECK_ERROR;
46
0
  warning(_("unrecognized setting %s for option "
47
0
      "rebase.missingCommitsCheck. Ignoring."), value);
48
0
  return MISSING_COMMIT_CHECK_IGNORE;
49
0
}
50
51
void append_todo_help(int command_count,
52
          const char *shortrevisions, const char *shortonto,
53
          struct strbuf *buf)
54
0
{
55
0
  const char *msg = _("\nCommands:\n"
56
0
"p, pick <commit> = use commit\n"
57
0
"r, reword <commit> = use commit, but edit the commit message\n"
58
0
"e, edit <commit> = use commit, but stop for amending\n"
59
0
"s, squash <commit> = use commit, but meld into previous commit\n"
60
0
"f, fixup [-C | -c] <commit> = like \"squash\" but keep only the previous\n"
61
0
"                   commit's log message, unless -C is used, in which case\n"
62
0
"                   keep only this commit's message; -c is same as -C but\n"
63
0
"                   opens the editor\n"
64
0
"x, exec <command> = run command (the rest of the line) using shell\n"
65
0
"b, break = stop here (continue rebase later with 'git rebase --continue')\n"
66
0
"d, drop <commit> = remove commit\n"
67
0
"l, label <label> = label current HEAD with a name\n"
68
0
"t, reset <label> = reset HEAD to a label\n"
69
0
"m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]\n"
70
0
"        create a merge commit using the original merge commit's\n"
71
0
"        message (or the oneline, if no original merge commit was\n"
72
0
"        specified); use -c <commit> to reword the commit message\n"
73
0
"u, update-ref <ref> = track a placeholder for the <ref> to be updated\n"
74
0
"                      to this position in the new commits. The <ref> is\n"
75
0
"                      updated at the end of the rebase\n"
76
0
"\n"
77
0
"These lines can be re-ordered; they are executed from top to bottom.\n");
78
0
  unsigned edit_todo = !(shortrevisions && shortonto);
79
80
0
  if (!edit_todo) {
81
0
    strbuf_addch(buf, '\n');
82
0
    strbuf_commented_addf(buf, comment_line_str,
83
0
              Q_("Rebase %s onto %s (%d command)",
84
0
           "Rebase %s onto %s (%d commands)",
85
0
           command_count),
86
0
              shortrevisions, shortonto, command_count);
87
0
  }
88
89
0
  strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_str);
90
91
0
  if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
92
0
    msg = _("\nDo not remove any line. Use 'drop' "
93
0
       "explicitly to remove a commit.\n");
94
0
  else
95
0
    msg = _("\nIf you remove a line here "
96
0
       "THAT COMMIT WILL BE LOST.\n");
97
98
0
  strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_str);
99
100
0
  if (edit_todo)
101
0
    msg = _("\nYou are editing the todo file "
102
0
      "of an ongoing interactive rebase.\n"
103
0
      "To continue rebase after editing, run:\n"
104
0
      "    git rebase --continue\n\n");
105
0
  else
106
0
    msg = _("\nHowever, if you remove everything, "
107
0
      "the rebase will be aborted.\n\n");
108
109
0
  strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_str);
110
0
}
111
112
int edit_todo_list(struct repository *r, struct replay_opts *opts,
113
       struct todo_list *todo_list, struct todo_list *new_todo,
114
       const char *shortrevisions, const char *shortonto,
115
       unsigned flags)
116
0
{
117
0
  const char *todo_file = rebase_path_todo(),
118
0
    *todo_backup = rebase_path_todo_backup();
119
0
  unsigned initial = shortrevisions && shortonto;
120
0
  int incorrect = 0;
121
122
  /* If the user is editing the todo list, we first try to parse
123
   * it.  If there is an error, we do not return, because the user
124
   * might want to fix it in the first place. */
125
0
  if (!initial)
126
0
    incorrect = todo_list_parse_insn_buffer(r, opts,
127
0
              todo_list->buf.buf,
128
0
              todo_list) |
129
0
      file_exists(rebase_path_dropped());
130
131
0
  if (todo_list_write_to_file(r, todo_list, todo_file, shortrevisions, shortonto,
132
0
            -1, flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP))
133
0
    return error_errno(_("could not write '%s'"), todo_file);
134
135
0
  if (!incorrect &&
136
0
      todo_list_write_to_file(r, todo_list, todo_backup,
137
0
            shortrevisions, shortonto, -1,
138
0
            (flags | TODO_LIST_APPEND_TODO_HELP) & ~TODO_LIST_SHORTEN_IDS) < 0)
139
0
    return error(_("could not write '%s'."), rebase_path_todo_backup());
140
141
0
  if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
142
0
    return -2;
143
144
0
  strbuf_stripspace(&new_todo->buf, comment_line_str);
145
0
  if (initial && new_todo->buf.len == 0)
146
0
    return -3;
147
148
0
  if (todo_list_parse_insn_buffer(r, opts, new_todo->buf.buf, new_todo)) {
149
0
    fprintf(stderr, _(edit_todo_list_advice));
150
0
    return -4;
151
0
  }
152
153
0
  if (incorrect) {
154
0
    if (todo_list_check_against_backup(r, opts, new_todo)) {
155
0
      write_file(rebase_path_dropped(), "%s", "");
156
0
      return -4;
157
0
    }
158
159
0
    if (incorrect > 0)
160
0
      unlink(rebase_path_dropped());
161
0
  } else if (todo_list_check(todo_list, new_todo)) {
162
0
    write_file(rebase_path_dropped(), "%s", "");
163
0
    return -4;
164
0
  }
165
166
  /*
167
   * See if branches need to be added or removed from the update-refs
168
   * file based on the new todo list.
169
   */
170
0
  todo_list_filter_update_refs(r, new_todo);
171
172
0
  return 0;
173
0
}
174
175
0
define_commit_slab(commit_seen, unsigned char);
Unexecuted instantiation: rebase-interactive.c:init_commit_seen_with_stride
Unexecuted instantiation: rebase-interactive.c:commit_seen_at_peek
Unexecuted instantiation: rebase-interactive.c:clear_commit_seen
176
0
/*
177
0
 * Check if the user dropped some commits by mistake
178
0
 * Behaviour determined by rebase.missingCommitsCheck.
179
0
 * Check if there is an unrecognized command or a
180
0
 * bad SHA-1 in a command.
181
0
 */
182
0
int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo)
183
0
{
184
0
  enum missing_commit_check_level check_level = get_missing_commit_check_level();
185
0
  struct strbuf missing = STRBUF_INIT;
186
0
  int res = 0, i;
187
0
  struct commit_seen commit_seen;
188
189
0
  init_commit_seen(&commit_seen);
190
191
0
  if (check_level == MISSING_COMMIT_CHECK_IGNORE)
192
0
    goto leave_check;
193
194
  /* Mark the commits in git-rebase-todo as seen */
195
0
  for (i = 0; i < new_todo->nr; i++) {
196
0
    struct commit *commit = new_todo->items[i].commit;
197
0
    if (commit)
198
0
      *commit_seen_at(&commit_seen, commit) = 1;
199
0
  }
200
201
  /* Find commits in git-rebase-todo.backup yet unseen */
202
0
  for (i = old_todo->nr - 1; i >= 0; i--) {
203
0
    struct todo_item *item = old_todo->items + i;
204
0
    struct commit *commit = item->commit;
205
0
    if (commit && !*commit_seen_at(&commit_seen, commit)) {
206
0
      strbuf_addf(&missing, " - %s %.*s\n",
207
0
            repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV),
208
0
            item->arg_len,
209
0
            todo_item_get_arg(old_todo, item));
210
0
      *commit_seen_at(&commit_seen, commit) = 1;
211
0
    }
212
0
  }
213
214
  /* Warn about missing commits */
215
0
  if (!missing.len)
216
0
    goto leave_check;
217
218
0
  if (check_level == MISSING_COMMIT_CHECK_ERROR)
219
0
    res = 1;
220
221
0
  fprintf(stderr,
222
0
    _("Warning: some commits may have been dropped accidentally.\n"
223
0
    "Dropped commits (newer to older):\n"));
224
225
  /* Make the list user-friendly and display */
226
0
  fputs(missing.buf, stderr);
227
0
  strbuf_release(&missing);
228
229
0
  fprintf(stderr, _("To avoid this message, use \"drop\" to "
230
0
    "explicitly remove a commit.\n\n"
231
0
    "Use 'git config rebase.missingCommitsCheck' to change "
232
0
    "the level of warnings.\n"
233
0
    "The possible behaviours are: ignore, warn, error.\n\n"));
234
235
0
  fprintf(stderr, _(edit_todo_list_advice));
236
237
0
leave_check:
238
0
  clear_commit_seen(&commit_seen);
239
0
  return res;
240
0
}
241
242
int todo_list_check_against_backup(struct repository *r,
243
           struct replay_opts *opts,
244
           struct todo_list *todo_list)
245
0
{
246
0
  struct todo_list backup = TODO_LIST_INIT;
247
0
  int res = 0;
248
249
0
  if (strbuf_read_file(&backup.buf, rebase_path_todo_backup(), 0) > 0) {
250
0
    todo_list_parse_insn_buffer(r, opts, backup.buf.buf, &backup);
251
0
    res = todo_list_check(&backup, todo_list);
252
0
  }
253
254
0
  todo_list_release(&backup);
255
0
  return res;
256
0
}