| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 3 | |
| 4 | #include "git-compat-util.h" |
| 5 | #include "advice.h" |
| 6 | #include "wt-status.h" |
| 7 | #include "object.h" |
| 8 | #include "dir.h" |
| 9 | #include "commit.h" |
| 10 | #include "diff.h" |
| 11 | #include "environment.h" |
| 12 | #include "gettext.h" |
| 13 | #include "hash.h" |
| 14 | #include "hex.h" |
| 15 | #include "object-name.h" |
| 16 | #include "path.h" |
| 17 | #include "revision.h" |
| 18 | #include "diffcore.h" |
| 19 | #include "quote.h" |
| 20 | #include "repository.h" |
| 21 | #include "run-command.h" |
| 22 | #include "strvec.h" |
| 23 | #include "remote.h" |
| 24 | #include "refs.h" |
| 25 | #include "submodule.h" |
| 26 | #include "column.h" |
| 27 | #include "read-cache.h" |
| 28 | #include "setup.h" |
| 29 | #include "strbuf.h" |
| 30 | #include "trace.h" |
| 31 | #include "trace2.h" |
| 32 | #include "tree.h" |
| 33 | #include "utf8.h" |
| 34 | #include "worktree.h" |
| 35 | #include "lockfile.h" |
| 36 | #include "sequencer.h" |
| 37 | #include "fsmonitor-settings.h" |
| 38 | |
| 39 | #define AB_DELAY_WARNING_IN_MS (2 * 1000) |
| 40 | #define UF_DELAY_WARNING_IN_MS (2 * 1000) |
| 41 | |
| 42 | static const char cut_line[] = |
| 43 | "------------------------ >8 ------------------------\n"; |
| 44 | |
| 45 | static char default_wt_status_colors[][COLOR_MAXLEN] = { |
| 46 | GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */ |
| 47 | GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */ |
| 48 | GIT_COLOR_RED, /* WT_STATUS_CHANGED */ |
| 49 | GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */ |
| 50 | GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */ |
| 51 | GIT_COLOR_RED, /* WT_STATUS_UNMERGED */ |
| 52 | GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */ |
| 53 | GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */ |
| 54 | GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */ |
| 55 | }; |
| 56 | |
| 57 | static const char *color(int slot, struct wt_status *s) |
| 58 | { |
| 59 | const char *c = ""; |
| 60 | if (want_color(s->use_color)) |
| 61 | c = s->color_palette[slot]; |
| 62 | if (slot == WT_STATUS_ONBRANCH && color_is_nil(c)) |
| 63 | c = s->color_palette[WT_STATUS_HEADER]; |
| 64 | return c; |
| 65 | } |
| 66 | |
| 67 | static void status_vprintf(struct wt_status *s, int at_bol, const char *color, |
| 68 | const char *fmt, va_list ap, const char *trail) |
| 69 | { |
| 70 | struct strbuf sb = STRBUF_INIT; |
| 71 | struct strbuf linebuf = STRBUF_INIT; |
| 72 | const char *line, *eol; |
| 73 | |
| 74 | strbuf_vaddf(&sb, fmt, ap); |
| 75 | if (!sb.len) { |
| 76 | if (s->display_comment_prefix) { |
| 77 | strbuf_addstr(&sb, comment_line_str); |
| 78 | if (!trail) |
| 79 | strbuf_addch(&sb, ' '); |
| 80 | } |
| 81 | color_print_strbuf(s->fp, color, &sb); |
| 82 | if (trail) |
| 83 | fprintf(s->fp, "%s", trail); |
| 84 | strbuf_release(&sb); |
| 85 | return; |
| 86 | } |
| 87 | for (line = sb.buf; *line; line = eol + 1) { |
| 88 | eol = strchr(line, '\n'); |
| 89 | |
| 90 | strbuf_reset(&linebuf); |
| 91 | if (at_bol && s->display_comment_prefix) { |
| 92 | strbuf_addstr(&linebuf, comment_line_str); |
| 93 | if (*line != '\n' && *line != '\t') |
| 94 | strbuf_addch(&linebuf, ' '); |
| 95 | } |
| 96 | if (eol) |
| 97 | strbuf_add(&linebuf, line, eol - line); |
| 98 | else |
| 99 | strbuf_addstr(&linebuf, line); |
| 100 | color_print_strbuf(s->fp, color, &linebuf); |
| 101 | if (eol) |
| 102 | fprintf(s->fp, "\n"); |
| 103 | else |
| 104 | break; |
| 105 | at_bol = 1; |
| 106 | } |
| 107 | if (trail) |
| 108 | fprintf(s->fp, "%s", trail); |
| 109 | strbuf_release(&linebuf); |
| 110 | strbuf_release(&sb); |
| 111 | } |
| 112 | |
| 113 | void status_printf_ln(struct wt_status *s, const char *color, |
| 114 | const char *fmt, ...) |
| 115 | { |
| 116 | va_list ap; |
| 117 | |
| 118 | va_start(ap, fmt); |
| 119 | status_vprintf(s, 1, color, fmt, ap, "\n"); |
| 120 | va_end(ap); |
| 121 | } |
| 122 | |
| 123 | void status_printf(struct wt_status *s, const char *color, |
| 124 | const char *fmt, ...) |
| 125 | { |
| 126 | va_list ap; |
| 127 | |
| 128 | va_start(ap, fmt); |
| 129 | status_vprintf(s, 1, color, fmt, ap, NULL); |
| 130 | va_end(ap); |
| 131 | } |
| 132 | |
| 133 | __attribute__((format (printf, 3, 4))) |
| 134 | static void status_printf_more(struct wt_status *s, const char *color, |
| 135 | const char *fmt, ...) |
| 136 | { |
| 137 | va_list ap; |
| 138 | |
| 139 | va_start(ap, fmt); |
| 140 | status_vprintf(s, 0, color, fmt, ap, NULL); |
| 141 | va_end(ap); |
| 142 | } |
| 143 | |
| 144 | void wt_status_prepare(struct repository *r, struct wt_status *s) |
| 145 | { |
| 146 | memset(s, 0, sizeof(*s)); |
| 147 | s->repo = r; |
| 148 | memcpy(s->color_palette, default_wt_status_colors, |
| 149 | sizeof(default_wt_status_colors)); |
| 150 | s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; |
| 151 | s->use_color = GIT_COLOR_UNKNOWN; |
| 152 | s->relative_paths = 1; |
| 153 | s->branch = refs_resolve_refdup(get_main_ref_store(r), |
| 154 | "HEAD", 0, NULL, NULL); |
| 155 | s->reference = "HEAD"; |
| 156 | s->fp = stdout; |
| 157 | s->index_file = repo_get_index_file(r); |
| 158 | s->change.strdup_strings = 1; |
| 159 | s->untracked.strdup_strings = 1; |
| 160 | s->ignored.strdup_strings = 1; |
| 161 | s->show_branch = -1; /* unspecified */ |
| 162 | s->show_stash = 0; |
| 163 | s->ahead_behind_flags = AHEAD_BEHIND_UNSPECIFIED; |
| 164 | s->display_comment_prefix = 0; |
| 165 | s->detect_rename = -1; |
| 166 | s->rename_score = -1; |
| 167 | s->rename_limit = -1; |
| 168 | } |
| 169 | |
| 170 | static void wt_longstatus_print_unmerged_header(struct wt_status *s) |
| 171 | { |
| 172 | int i; |
| 173 | int del_mod_conflict = 0; |
| 174 | int both_deleted = 0; |
| 175 | int not_deleted = 0; |
| 176 | const char *c = color(WT_STATUS_HEADER, s); |
| 177 | |
| 178 | status_printf_ln(s, c, _("Unmerged paths:")); |
| 179 | |
| 180 | for (i = 0; i < s->change.nr; i++) { |
| 181 | struct string_list_item *it = &(s->change.items[i]); |
| 182 | struct wt_status_change_data *d = it->util; |
| 183 | |
| 184 | switch (d->stagemask) { |
| 185 | case 0: |
| 186 | break; |
| 187 | case 1: |
| 188 | both_deleted = 1; |
| 189 | break; |
| 190 | case 3: |
| 191 | case 5: |
| 192 | del_mod_conflict = 1; |
| 193 | break; |
| 194 | default: |
| 195 | not_deleted = 1; |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if (!s->hints) |
| 201 | return; |
| 202 | if (s->whence != FROM_COMMIT) |
| 203 | ; |
| 204 | else if (!s->is_initial) { |
| 205 | if (!strcmp(s->reference, "HEAD")) |
| 206 | status_printf_ln(s, c, |
| 207 | _(" (use \"git restore --staged <file>...\" to unstage)")); |
| 208 | else |
| 209 | status_printf_ln(s, c, |
| 210 | _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"), |
| 211 | s->reference); |
| 212 | } else |
| 213 | status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)")); |
| 214 | |
| 215 | if (!both_deleted) { |
| 216 | if (!del_mod_conflict) |
| 217 | status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)")); |
| 218 | else |
| 219 | status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)")); |
| 220 | } else if (!del_mod_conflict && !not_deleted) { |
| 221 | status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)")); |
| 222 | } else { |
| 223 | status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)")); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | static void wt_longstatus_print_cached_header(struct wt_status *s) |
| 228 | { |
| 229 | const char *c = color(WT_STATUS_HEADER, s); |
| 230 | |
| 231 | status_printf_ln(s, c, _("Changes to be committed:")); |
| 232 | if (!s->hints) |
| 233 | return; |
| 234 | if (s->whence != FROM_COMMIT) |
| 235 | ; /* NEEDSWORK: use "git reset --unresolve"??? */ |
| 236 | else if (!s->is_initial) { |
| 237 | if (!strcmp(s->reference, "HEAD")) |
| 238 | status_printf_ln(s, c |
| 239 | , _(" (use \"git restore --staged <file>...\" to unstage)")); |
| 240 | else |
| 241 | status_printf_ln(s, c, |
| 242 | _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"), |
| 243 | s->reference); |
| 244 | } else |
| 245 | status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)")); |
| 246 | } |
| 247 | |
| 248 | static void wt_longstatus_print_dirty_header(struct wt_status *s, |
| 249 | int has_deleted, |
| 250 | int has_dirty_submodules) |
| 251 | { |
| 252 | const char *c = color(WT_STATUS_HEADER, s); |
| 253 | |
| 254 | status_printf_ln(s, c, _("Changes not staged for commit:")); |
| 255 | if (!s->hints) |
| 256 | return; |
| 257 | if (!has_deleted) |
| 258 | status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)")); |
| 259 | else |
| 260 | status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)")); |
| 261 | status_printf_ln(s, c, _(" (use \"git restore <file>...\" to discard changes in working directory)")); |
| 262 | if (has_dirty_submodules) |
| 263 | status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)")); |
| 264 | } |
| 265 | |
| 266 | static void wt_longstatus_print_other_header(struct wt_status *s, |
| 267 | const char *what, |
| 268 | const char *how) |
| 269 | { |
| 270 | const char *c = color(WT_STATUS_HEADER, s); |
| 271 | status_printf_ln(s, c, "%s:", what); |
| 272 | if (!s->hints) |
| 273 | return; |
| 274 | status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how); |
| 275 | } |
| 276 | |
| 277 | static void wt_longstatus_print_trailer(struct wt_status *s) |
| 278 | { |
| 279 | status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", ""); |
| 280 | } |
| 281 | |
| 282 | static const char *wt_status_unmerged_status_string(int stagemask) |
| 283 | { |
| 284 | switch (stagemask) { |
| 285 | case 1: |
| 286 | return _("both deleted:"); |
| 287 | case 2: |
| 288 | return _("added by us:"); |
| 289 | case 3: |
| 290 | return _("deleted by them:"); |
| 291 | case 4: |
| 292 | return _("added by them:"); |
| 293 | case 5: |
| 294 | return _("deleted by us:"); |
| 295 | case 6: |
| 296 | return _("both added:"); |
| 297 | case 7: |
| 298 | return _("both modified:"); |
| 299 | default: |
| 300 | BUG("unhandled unmerged status %x", stagemask); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | static const char *wt_status_diff_status_string(int status) |
| 305 | { |
| 306 | switch (status) { |
| 307 | case DIFF_STATUS_ADDED: |
| 308 | return _("new file:"); |
| 309 | case DIFF_STATUS_COPIED: |
| 310 | return _("copied:"); |
| 311 | case DIFF_STATUS_DELETED: |
| 312 | return _("deleted:"); |
| 313 | case DIFF_STATUS_MODIFIED: |
| 314 | return _("modified:"); |
| 315 | case DIFF_STATUS_RENAMED: |
| 316 | return _("renamed:"); |
| 317 | case DIFF_STATUS_TYPE_CHANGED: |
| 318 | return _("typechange:"); |
| 319 | case DIFF_STATUS_UNKNOWN: |
| 320 | return _("unknown:"); |
| 321 | case DIFF_STATUS_UNMERGED: |
| 322 | return _("unmerged:"); |
| 323 | default: |
| 324 | return NULL; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | static int maxwidth(const char *(*label)(int), int minval, int maxval) |
| 329 | { |
| 330 | int result = 0, i; |
| 331 | |
| 332 | for (i = minval; i <= maxval; i++) { |
| 333 | const char *s = label(i); |
| 334 | int len = s ? utf8_strwidth(s) : 0; |
| 335 | if (len > result) |
| 336 | result = len; |
| 337 | } |
| 338 | return result; |
| 339 | } |
| 340 | |
| 341 | static void wt_longstatus_print_unmerged_data(struct wt_status *s, |
| 342 | struct string_list_item *it) |
| 343 | { |
| 344 | const char *c = color(WT_STATUS_UNMERGED, s); |
| 345 | struct wt_status_change_data *d = it->util; |
| 346 | struct strbuf onebuf = STRBUF_INIT; |
| 347 | static char *padding; |
| 348 | static int label_width; |
| 349 | const char *one, *how; |
| 350 | int len; |
| 351 | |
| 352 | if (!padding) { |
| 353 | label_width = maxwidth(wt_status_unmerged_status_string, 1, 7); |
| 354 | label_width += strlen(" "); |
| 355 | padding = xmallocz(label_width); |
| 356 | memset(padding, ' ', label_width); |
| 357 | } |
| 358 | |
| 359 | one = quote_path(it->string, s->prefix, &onebuf, 0); |
| 360 | status_printf(s, color(WT_STATUS_HEADER, s), "\t"); |
| 361 | |
| 362 | how = wt_status_unmerged_status_string(d->stagemask); |
| 363 | len = label_width - utf8_strwidth(how); |
| 364 | status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one); |
| 365 | strbuf_release(&onebuf); |
| 366 | } |
| 367 | |
| 368 | static void wt_longstatus_print_change_data(struct wt_status *s, |
| 369 | int change_type, |
| 370 | struct string_list_item *it) |
| 371 | { |
| 372 | struct wt_status_change_data *d = it->util; |
| 373 | const char *c = color(change_type, s); |
| 374 | int status; |
| 375 | char *one_name; |
| 376 | char *two_name; |
| 377 | const char *one, *two; |
| 378 | struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT; |
| 379 | struct strbuf extra = STRBUF_INIT; |
| 380 | static char *padding; |
| 381 | static int label_width; |
| 382 | const char *what; |
| 383 | int len; |
| 384 | |
| 385 | if (!padding) { |
| 386 | /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */ |
| 387 | label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z'); |
| 388 | label_width += strlen(" "); |
| 389 | padding = xmallocz(label_width); |
| 390 | memset(padding, ' ', label_width); |
| 391 | } |
| 392 | |
| 393 | one_name = two_name = it->string; |
| 394 | switch (change_type) { |
| 395 | case WT_STATUS_UPDATED: |
| 396 | status = d->index_status; |
| 397 | break; |
| 398 | case WT_STATUS_CHANGED: |
| 399 | if (d->new_submodule_commits || d->dirty_submodule) { |
| 400 | strbuf_addstr(&extra, " ("); |
| 401 | if (d->new_submodule_commits) |
| 402 | strbuf_addstr(&extra, _("new commits, ")); |
| 403 | if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) |
| 404 | strbuf_addstr(&extra, _("modified content, ")); |
| 405 | if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) |
| 406 | strbuf_addstr(&extra, _("untracked content, ")); |
| 407 | strbuf_setlen(&extra, extra.len - 2); |
| 408 | strbuf_addch(&extra, ')'); |
| 409 | } |
| 410 | status = d->worktree_status; |
| 411 | break; |
| 412 | default: |
| 413 | BUG("unhandled change_type %d in wt_longstatus_print_change_data", |
| 414 | change_type); |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * Only pick up the rename it's relevant. If the rename is for |
| 419 | * the changed section and we're printing the updated section, |
| 420 | * ignore it. |
| 421 | */ |
| 422 | if (d->rename_status == status) |
| 423 | one_name = d->rename_source; |
| 424 | |
| 425 | one = quote_path(one_name, s->prefix, &onebuf, 0); |
| 426 | two = quote_path(two_name, s->prefix, &twobuf, 0); |
| 427 | |
| 428 | status_printf(s, color(WT_STATUS_HEADER, s), "\t"); |
| 429 | what = wt_status_diff_status_string(status); |
| 430 | if (!what) |
| 431 | BUG("unhandled diff status %c", status); |
| 432 | len = label_width - utf8_strwidth(what); |
| 433 | assert(len >= 0); |
| 434 | if (one_name != two_name) |
| 435 | status_printf_more(s, c, "%s%.*s%s -> %s", |
| 436 | what, len, padding, one, two); |
| 437 | else |
| 438 | status_printf_more(s, c, "%s%.*s%s", |
| 439 | what, len, padding, one); |
| 440 | if (extra.len) { |
| 441 | status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf); |
| 442 | strbuf_release(&extra); |
| 443 | } |
| 444 | status_printf_more(s, GIT_COLOR_NORMAL, "\n"); |
| 445 | strbuf_release(&onebuf); |
| 446 | strbuf_release(&twobuf); |
| 447 | } |
| 448 | |
| 449 | static char short_submodule_status(struct wt_status_change_data *d) |
| 450 | { |
| 451 | if (d->new_submodule_commits) |
| 452 | return 'M'; |
| 453 | if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) |
| 454 | return 'm'; |
| 455 | if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) |
| 456 | return '?'; |
| 457 | return d->worktree_status; |
| 458 | } |
| 459 | |
| 460 | static void wt_status_collect_changed_cb(struct diff_queue_struct *q, |
| 461 | struct diff_options *options UNUSED, |
| 462 | void *data) |
| 463 | { |
| 464 | struct wt_status *s = data; |
| 465 | int i; |
| 466 | |
| 467 | if (!q->nr) |
| 468 | return; |
| 469 | s->workdir_dirty = 1; |
| 470 | for (i = 0; i < q->nr; i++) { |
| 471 | struct diff_filepair *p; |
| 472 | struct string_list_item *it; |
| 473 | struct wt_status_change_data *d; |
| 474 | |
| 475 | p = q->queue[i]; |
| 476 | it = string_list_insert(&s->change, p->two->path); |
| 477 | d = it->util; |
| 478 | if (!d) { |
| 479 | CALLOC_ARRAY(d, 1); |
| 480 | it->util = d; |
| 481 | } |
| 482 | if (!d->worktree_status) |
| 483 | d->worktree_status = p->status; |
| 484 | if (S_ISGITLINK(p->two->mode)) { |
| 485 | d->dirty_submodule = p->two->dirty_submodule; |
| 486 | d->new_submodule_commits = !oideq(&p->one->oid, |
| 487 | &p->two->oid); |
| 488 | if (s->status_format == STATUS_FORMAT_SHORT) |
| 489 | d->worktree_status = short_submodule_status(d); |
| 490 | } |
| 491 | |
| 492 | switch (p->status) { |
| 493 | case DIFF_STATUS_ADDED: |
| 494 | d->mode_worktree = p->two->mode; |
| 495 | break; |
| 496 | |
| 497 | case DIFF_STATUS_DELETED: |
| 498 | d->mode_index = p->one->mode; |
| 499 | oidcpy(&d->oid_index, &p->one->oid); |
| 500 | /* mode_worktree is zero for a delete. */ |
| 501 | break; |
| 502 | |
| 503 | case DIFF_STATUS_COPIED: |
| 504 | case DIFF_STATUS_RENAMED: |
| 505 | if (d->rename_status) |
| 506 | BUG("multiple renames on the same target? how?"); |
| 507 | d->rename_source = xstrdup(p->one->path); |
| 508 | d->rename_score = p->score * 100 / MAX_SCORE; |
| 509 | d->rename_status = p->status; |
| 510 | /* fallthru */ |
| 511 | case DIFF_STATUS_MODIFIED: |
| 512 | case DIFF_STATUS_TYPE_CHANGED: |
| 513 | case DIFF_STATUS_UNMERGED: |
| 514 | d->mode_index = p->one->mode; |
| 515 | d->mode_worktree = p->two->mode; |
| 516 | oidcpy(&d->oid_index, &p->one->oid); |
| 517 | break; |
| 518 | |
| 519 | default: |
| 520 | BUG("unhandled diff-files status '%c'", p->status); |
| 521 | break; |
| 522 | } |
| 523 | |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | static int unmerged_mask(struct index_state *istate, const char *path) |
| 528 | { |
| 529 | int pos, mask; |
| 530 | const struct cache_entry *ce; |
| 531 | |
| 532 | pos = index_name_pos(istate, path, strlen(path)); |
| 533 | if (0 <= pos) |
| 534 | return 0; |
| 535 | |
| 536 | mask = 0; |
| 537 | pos = -pos-1; |
| 538 | while (pos < istate->cache_nr) { |
| 539 | ce = istate->cache[pos++]; |
| 540 | if (strcmp(ce->name, path) || !ce_stage(ce)) |
| 541 | break; |
| 542 | mask |= (1 << (ce_stage(ce) - 1)); |
| 543 | } |
| 544 | return mask; |
| 545 | } |
| 546 | |
| 547 | static void wt_status_collect_updated_cb(struct diff_queue_struct *q, |
| 548 | struct diff_options *options UNUSED, |
| 549 | void *data) |
| 550 | { |
| 551 | struct wt_status *s = data; |
| 552 | int i; |
| 553 | |
| 554 | for (i = 0; i < q->nr; i++) { |
| 555 | struct diff_filepair *p; |
| 556 | struct string_list_item *it; |
| 557 | struct wt_status_change_data *d; |
| 558 | |
| 559 | p = q->queue[i]; |
| 560 | it = string_list_insert(&s->change, p->two->path); |
| 561 | d = it->util; |
| 562 | if (!d) { |
| 563 | CALLOC_ARRAY(d, 1); |
| 564 | it->util = d; |
| 565 | } |
| 566 | if (!d->index_status) |
| 567 | d->index_status = p->status; |
| 568 | switch (p->status) { |
| 569 | case DIFF_STATUS_ADDED: |
| 570 | /* Leave {mode,oid}_head zero for an add. */ |
| 571 | d->mode_index = p->two->mode; |
| 572 | oidcpy(&d->oid_index, &p->two->oid); |
| 573 | s->committable = 1; |
| 574 | break; |
| 575 | case DIFF_STATUS_DELETED: |
| 576 | d->mode_head = p->one->mode; |
| 577 | oidcpy(&d->oid_head, &p->one->oid); |
| 578 | s->committable = 1; |
| 579 | /* Leave {mode,oid}_index zero for a delete. */ |
| 580 | break; |
| 581 | |
| 582 | case DIFF_STATUS_COPIED: |
| 583 | case DIFF_STATUS_RENAMED: |
| 584 | if (d->rename_status) |
| 585 | BUG("multiple renames on the same target? how?"); |
| 586 | d->rename_source = xstrdup(p->one->path); |
| 587 | d->rename_score = p->score * 100 / MAX_SCORE; |
| 588 | d->rename_status = p->status; |
| 589 | /* fallthru */ |
| 590 | case DIFF_STATUS_MODIFIED: |
| 591 | case DIFF_STATUS_TYPE_CHANGED: |
| 592 | d->mode_head = p->one->mode; |
| 593 | d->mode_index = p->two->mode; |
| 594 | oidcpy(&d->oid_head, &p->one->oid); |
| 595 | oidcpy(&d->oid_index, &p->two->oid); |
| 596 | s->committable = 1; |
| 597 | break; |
| 598 | case DIFF_STATUS_UNMERGED: |
| 599 | d->stagemask = unmerged_mask(s->repo->index, |
| 600 | p->two->path); |
| 601 | /* |
| 602 | * Don't bother setting {mode,oid}_{head,index} since the print |
| 603 | * code will output the stage values directly and not use the |
| 604 | * values in these fields. |
| 605 | */ |
| 606 | break; |
| 607 | |
| 608 | default: |
| 609 | BUG("unhandled diff-index status '%c'", p->status); |
| 610 | break; |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | void wt_status_collect_changes_trees(struct wt_status *s, |
| 616 | const struct object_id *old_treeish, |
| 617 | const struct object_id *new_treeish) |
| 618 | { |
| 619 | struct diff_options opts = { 0 }; |
| 620 | |
| 621 | repo_diff_setup(s->repo, &opts); |
| 622 | opts.output_format = DIFF_FORMAT_CALLBACK; |
| 623 | opts.format_callback = wt_status_collect_updated_cb; |
| 624 | opts.format_callback_data = s; |
| 625 | opts.detect_rename = s->detect_rename >= 0 ? s->detect_rename : opts.detect_rename; |
| 626 | opts.rename_limit = s->rename_limit >= 0 ? s->rename_limit : opts.rename_limit; |
| 627 | opts.rename_score = s->rename_score >= 0 ? s->rename_score : opts.rename_score; |
| 628 | opts.flags.recursive = 1; |
| 629 | diff_setup_done(&opts); |
| 630 | |
| 631 | diff_tree_oid(old_treeish, new_treeish, "", &opts); |
| 632 | diffcore_std(&opts); |
| 633 | diff_flush(&opts); |
| 634 | wt_status_get_state(s->repo, &s->state, 0); |
| 635 | |
| 636 | diff_free(&opts); |
| 637 | } |
| 638 | |
| 639 | static void wt_status_collect_changes_worktree(struct wt_status *s) |
| 640 | { |
| 641 | struct rev_info rev; |
| 642 | |
| 643 | repo_init_revisions(s->repo, &rev, NULL); |
| 644 | setup_revisions(0, NULL, &rev, NULL); |
| 645 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; |
| 646 | rev.diffopt.flags.dirty_submodules = 1; |
| 647 | rev.diffopt.ita_invisible_in_index = 1; |
| 648 | if (!s->show_untracked_files) |
| 649 | rev.diffopt.flags.ignore_untracked_in_submodules = 1; |
| 650 | if (s->ignore_submodule_arg) { |
| 651 | rev.diffopt.flags.override_submodule_config = 1; |
| 652 | handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg); |
| 653 | } else if (!rev.diffopt.flags.ignore_submodule_set && |
| 654 | s->show_untracked_files != SHOW_NO_UNTRACKED_FILES) |
| 655 | handle_ignore_submodules_arg(&rev.diffopt, "none"); |
| 656 | rev.diffopt.format_callback = wt_status_collect_changed_cb; |
| 657 | rev.diffopt.format_callback_data = s; |
| 658 | rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename; |
| 659 | rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit; |
| 660 | rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score; |
| 661 | copy_pathspec(&rev.prune_data, &s->pathspec); |
| 662 | run_diff_files(&rev, 0); |
| 663 | release_revisions(&rev); |
| 664 | } |
| 665 | |
| 666 | static void wt_status_collect_changes_index(struct wt_status *s) |
| 667 | { |
| 668 | struct rev_info rev; |
| 669 | struct setup_revision_opt opt; |
| 670 | |
| 671 | repo_init_revisions(s->repo, &rev, NULL); |
| 672 | memset(&opt, 0, sizeof(opt)); |
| 673 | opt.def = s->is_initial ? empty_tree_oid_hex(s->repo->hash_algo) : s->reference; |
| 674 | setup_revisions(0, NULL, &rev, &opt); |
| 675 | |
| 676 | rev.diffopt.flags.override_submodule_config = 1; |
| 677 | rev.diffopt.ita_invisible_in_index = 1; |
| 678 | if (s->ignore_submodule_arg) { |
| 679 | handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg); |
| 680 | } else { |
| 681 | /* |
| 682 | * Unless the user did explicitly request a submodule ignore |
| 683 | * mode by passing a command line option we do not ignore any |
| 684 | * changed submodule SHA-1s when comparing index and HEAD, no |
| 685 | * matter what is configured. Otherwise the user won't be |
| 686 | * shown any submodules manually added (and which are |
| 687 | * staged to be committed), which would be really confusing. |
| 688 | */ |
| 689 | handle_ignore_submodules_arg(&rev.diffopt, "dirty"); |
| 690 | } |
| 691 | |
| 692 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; |
| 693 | rev.diffopt.format_callback = wt_status_collect_updated_cb; |
| 694 | rev.diffopt.format_callback_data = s; |
| 695 | rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename; |
| 696 | rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit; |
| 697 | rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score; |
| 698 | |
| 699 | /* |
| 700 | * The `recursive` option must be enabled to allow the diff to recurse |
| 701 | * into subdirectories of sparse directory index entries. If it is not |
| 702 | * enabled, a subdirectory containing file(s) with changes is reported |
| 703 | * as "modified", rather than the modified files themselves. |
| 704 | */ |
| 705 | rev.diffopt.flags.recursive = 1; |
| 706 | |
| 707 | copy_pathspec(&rev.prune_data, &s->pathspec); |
| 708 | run_diff_index(&rev, DIFF_INDEX_CACHED); |
| 709 | release_revisions(&rev); |
| 710 | } |
| 711 | |
| 712 | static int add_file_to_list(const struct object_id *oid, |
| 713 | struct strbuf *base, const char *path, |
| 714 | unsigned int mode, void *context) |
| 715 | { |
| 716 | struct string_list_item *it; |
| 717 | struct wt_status_change_data *d; |
| 718 | struct wt_status *s = context; |
| 719 | struct strbuf full_name = STRBUF_INIT; |
| 720 | |
| 721 | if (S_ISDIR(mode)) |
| 722 | return READ_TREE_RECURSIVE; |
| 723 | |
| 724 | strbuf_add(&full_name, base->buf, base->len); |
| 725 | strbuf_addstr(&full_name, path); |
| 726 | it = string_list_insert(&s->change, full_name.buf); |
| 727 | d = it->util; |
| 728 | if (!d) { |
| 729 | CALLOC_ARRAY(d, 1); |
| 730 | it->util = d; |
| 731 | } |
| 732 | |
| 733 | d->index_status = DIFF_STATUS_ADDED; |
| 734 | /* Leave {mode,oid}_head zero for adds. */ |
| 735 | d->mode_index = mode; |
| 736 | oidcpy(&d->oid_index, oid); |
| 737 | s->committable = 1; |
| 738 | strbuf_release(&full_name); |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | static void wt_status_collect_changes_initial(struct wt_status *s) |
| 743 | { |
| 744 | struct index_state *istate = s->repo->index; |
| 745 | struct strbuf base = STRBUF_INIT; |
| 746 | int i; |
| 747 | |
| 748 | for (i = 0; i < istate->cache_nr; i++) { |
| 749 | struct string_list_item *it; |
| 750 | struct wt_status_change_data *d; |
| 751 | const struct cache_entry *ce = istate->cache[i]; |
| 752 | |
| 753 | if (!ce_path_match(istate, ce, &s->pathspec, NULL)) |
| 754 | continue; |
| 755 | if (ce_intent_to_add(ce)) |
| 756 | continue; |
| 757 | if (S_ISSPARSEDIR(ce->ce_mode)) { |
| 758 | /* |
| 759 | * This is a sparse directory entry, so we want to collect all |
| 760 | * of the added files within the tree. This requires recursively |
| 761 | * expanding the trees to find the elements that are new in this |
| 762 | * tree and marking them with DIFF_STATUS_ADDED. |
| 763 | */ |
| 764 | struct pathspec ps = { 0 }; |
| 765 | struct tree *tree = lookup_tree(istate->repo, &ce->oid); |
| 766 | |
| 767 | ps.recursive = 1; |
| 768 | ps.has_wildcard = 1; |
| 769 | ps.max_depth = -1; |
| 770 | |
| 771 | strbuf_reset(&base); |
| 772 | strbuf_add(&base, ce->name, ce->ce_namelen); |
| 773 | read_tree_at(istate->repo, tree, &base, 0, &ps, |
| 774 | add_file_to_list, s); |
| 775 | |
| 776 | continue; |
| 777 | } |
| 778 | |
| 779 | it = string_list_insert(&s->change, ce->name); |
| 780 | d = it->util; |
| 781 | if (!d) { |
| 782 | CALLOC_ARRAY(d, 1); |
| 783 | it->util = d; |
| 784 | } |
| 785 | if (ce_stage(ce)) { |
| 786 | d->index_status = DIFF_STATUS_UNMERGED; |
| 787 | d->stagemask |= (1 << (ce_stage(ce) - 1)); |
| 788 | /* |
| 789 | * Don't bother setting {mode,oid}_{head,index} since the print |
| 790 | * code will output the stage values directly and not use the |
| 791 | * values in these fields. |
| 792 | */ |
| 793 | s->committable = 1; |
| 794 | } else { |
| 795 | d->index_status = DIFF_STATUS_ADDED; |
| 796 | /* Leave {mode,oid}_head zero for adds. */ |
| 797 | d->mode_index = ce->ce_mode; |
| 798 | oidcpy(&d->oid_index, &ce->oid); |
| 799 | s->committable = 1; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | strbuf_release(&base); |
| 804 | } |
| 805 | |
| 806 | static void wt_status_collect_untracked(struct wt_status *s) |
| 807 | { |
| 808 | int i; |
| 809 | struct dir_struct dir = DIR_INIT; |
| 810 | uint64_t t_begin = getnanotime(); |
| 811 | struct index_state *istate = s->repo->index; |
| 812 | |
| 813 | if (!s->show_untracked_files) |
| 814 | return; |
| 815 | |
| 816 | if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES) |
| 817 | dir.flags |= |
| 818 | DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES; |
| 819 | if (s->show_ignored_mode) { |
| 820 | dir.flags |= DIR_SHOW_IGNORED_TOO; |
| 821 | |
| 822 | if (s->show_ignored_mode == SHOW_MATCHING_IGNORED) |
| 823 | dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING; |
| 824 | } else { |
| 825 | dir.untracked = istate->untracked; |
| 826 | } |
| 827 | |
| 828 | setup_standard_excludes(&dir); |
| 829 | |
| 830 | fill_directory(&dir, istate, &s->pathspec); |
| 831 | |
| 832 | for (i = 0; i < dir.nr; i++) { |
| 833 | struct dir_entry *ent = dir.entries[i]; |
| 834 | if (index_name_is_other(istate, ent->name, ent->len)) |
| 835 | string_list_append(&s->untracked, ent->name); |
| 836 | } |
| 837 | string_list_sort_u(&s->untracked, 0); |
| 838 | |
| 839 | for (i = 0; i < dir.ignored_nr; i++) { |
| 840 | struct dir_entry *ent = dir.ignored[i]; |
| 841 | if (index_name_is_other(istate, ent->name, ent->len)) |
| 842 | string_list_append(&s->ignored, ent->name); |
| 843 | } |
| 844 | string_list_sort_u(&s->ignored, 0); |
| 845 | |
| 846 | dir_clear(&dir); |
| 847 | |
| 848 | if (advice_enabled(ADVICE_STATUS_U_OPTION)) |
| 849 | s->untracked_in_ms = (getnanotime() - t_begin) / 1000000; |
| 850 | } |
| 851 | |
| 852 | static int has_unmerged(struct wt_status *s) |
| 853 | { |
| 854 | int i; |
| 855 | |
| 856 | for (i = 0; i < s->change.nr; i++) { |
| 857 | struct wt_status_change_data *d; |
| 858 | d = s->change.items[i].util; |
| 859 | if (d->stagemask) |
| 860 | return 1; |
| 861 | } |
| 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | void wt_status_collect(struct wt_status *s) |
| 866 | { |
| 867 | trace2_region_enter("status", "worktrees", s->repo); |
| 868 | wt_status_collect_changes_worktree(s); |
| 869 | trace2_region_leave("status", "worktrees", s->repo); |
| 870 | |
| 871 | if (s->is_initial) { |
| 872 | trace2_region_enter("status", "initial", s->repo); |
| 873 | wt_status_collect_changes_initial(s); |
| 874 | trace2_region_leave("status", "initial", s->repo); |
| 875 | } else { |
| 876 | trace2_region_enter("status", "index", s->repo); |
| 877 | wt_status_collect_changes_index(s); |
| 878 | trace2_region_leave("status", "index", s->repo); |
| 879 | } |
| 880 | |
| 881 | trace2_region_enter("status", "untracked", s->repo); |
| 882 | wt_status_collect_untracked(s); |
| 883 | trace2_region_leave("status", "untracked", s->repo); |
| 884 | |
| 885 | wt_status_get_state(s->repo, &s->state, s->branch && !strcmp(s->branch, "HEAD")); |
| 886 | if (s->state.merge_in_progress && !has_unmerged(s)) |
| 887 | s->committable = 1; |
| 888 | } |
| 889 | |
| 890 | void wt_status_collect_free_buffers(struct wt_status *s) |
| 891 | { |
| 892 | wt_status_state_free_buffers(&s->state); |
| 893 | } |
| 894 | |
| 895 | void wt_status_state_free_buffers(struct wt_status_state *state) |
| 896 | { |
| 897 | FREE_AND_NULL(state->branch); |
| 898 | FREE_AND_NULL(state->onto); |
| 899 | FREE_AND_NULL(state->detached_from); |
| 900 | FREE_AND_NULL(state->bisecting_from); |
| 901 | } |
| 902 | |
| 903 | static void wt_longstatus_print_unmerged(struct wt_status *s) |
| 904 | { |
| 905 | int shown_header = 0; |
| 906 | int i; |
| 907 | |
| 908 | for (i = 0; i < s->change.nr; i++) { |
| 909 | struct wt_status_change_data *d; |
| 910 | struct string_list_item *it; |
| 911 | it = &(s->change.items[i]); |
| 912 | d = it->util; |
| 913 | if (!d->stagemask) |
| 914 | continue; |
| 915 | if (!shown_header) { |
| 916 | wt_longstatus_print_unmerged_header(s); |
| 917 | shown_header = 1; |
| 918 | } |
| 919 | wt_longstatus_print_unmerged_data(s, it); |
| 920 | } |
| 921 | if (shown_header) |
| 922 | wt_longstatus_print_trailer(s); |
| 923 | |
| 924 | } |
| 925 | |
| 926 | static void wt_longstatus_print_updated(struct wt_status *s) |
| 927 | { |
| 928 | int shown_header = 0; |
| 929 | int i; |
| 930 | |
| 931 | for (i = 0; i < s->change.nr; i++) { |
| 932 | struct wt_status_change_data *d; |
| 933 | struct string_list_item *it; |
| 934 | it = &(s->change.items[i]); |
| 935 | d = it->util; |
| 936 | if (!d->index_status || |
| 937 | d->index_status == DIFF_STATUS_UNMERGED) |
| 938 | continue; |
| 939 | if (!shown_header) { |
| 940 | wt_longstatus_print_cached_header(s); |
| 941 | shown_header = 1; |
| 942 | } |
| 943 | wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it); |
| 944 | } |
| 945 | if (shown_header) |
| 946 | wt_longstatus_print_trailer(s); |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * -1 : has delete |
| 951 | * 0 : no change |
| 952 | * 1 : some change but no delete |
| 953 | */ |
| 954 | static int wt_status_check_worktree_changes(struct wt_status *s, |
| 955 | int *dirty_submodules) |
| 956 | { |
| 957 | int i; |
| 958 | int changes = 0; |
| 959 | |
| 960 | *dirty_submodules = 0; |
| 961 | |
| 962 | for (i = 0; i < s->change.nr; i++) { |
| 963 | struct wt_status_change_data *d; |
| 964 | d = s->change.items[i].util; |
| 965 | if (!d->worktree_status || |
| 966 | d->worktree_status == DIFF_STATUS_UNMERGED) |
| 967 | continue; |
| 968 | if (!changes) |
| 969 | changes = 1; |
| 970 | if (d->dirty_submodule) |
| 971 | *dirty_submodules = 1; |
| 972 | if (d->worktree_status == DIFF_STATUS_DELETED) |
| 973 | changes = -1; |
| 974 | } |
| 975 | return changes; |
| 976 | } |
| 977 | |
| 978 | static void wt_longstatus_print_changed(struct wt_status *s) |
| 979 | { |
| 980 | int i, dirty_submodules; |
| 981 | int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules); |
| 982 | |
| 983 | if (!worktree_changes) |
| 984 | return; |
| 985 | |
| 986 | wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules); |
| 987 | |
| 988 | for (i = 0; i < s->change.nr; i++) { |
| 989 | struct wt_status_change_data *d; |
| 990 | struct string_list_item *it; |
| 991 | it = &(s->change.items[i]); |
| 992 | d = it->util; |
| 993 | if (!d->worktree_status || |
| 994 | d->worktree_status == DIFF_STATUS_UNMERGED) |
| 995 | continue; |
| 996 | wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it); |
| 997 | } |
| 998 | wt_longstatus_print_trailer(s); |
| 999 | } |
| 1000 | |
| 1001 | static int stash_count_refs(const char *refname UNUSED, |
| 1002 | struct object_id *ooid UNUSED, |
| 1003 | struct object_id *noid UNUSED, |
| 1004 | const char *email UNUSED, |
| 1005 | timestamp_t timestamp UNUSED, int tz UNUSED, |
| 1006 | const char *message UNUSED, void *cb_data) |
| 1007 | { |
| 1008 | int *c = cb_data; |
| 1009 | (*c)++; |
| 1010 | return 0; |
| 1011 | } |
| 1012 | |
| 1013 | static int count_stash_entries(struct repository *r) |
| 1014 | { |
| 1015 | int n = 0; |
| 1016 | refs_for_each_reflog_ent(get_main_ref_store(r), |
| 1017 | "refs/stash", stash_count_refs, &n); |
| 1018 | return n; |
| 1019 | } |
| 1020 | |
| 1021 | static void wt_longstatus_print_stash_summary(struct wt_status *s) |
| 1022 | { |
| 1023 | int stash_count = count_stash_entries(s->repo); |
| 1024 | |
| 1025 | if (stash_count > 0) |
| 1026 | status_printf_ln(s, GIT_COLOR_NORMAL, |
| 1027 | Q_("Your stash currently has %d entry", |
| 1028 | "Your stash currently has %d entries", stash_count), |
| 1029 | stash_count); |
| 1030 | } |
| 1031 | |
| 1032 | static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted) |
| 1033 | { |
| 1034 | struct child_process sm_summary = CHILD_PROCESS_INIT; |
| 1035 | struct strbuf cmd_stdout = STRBUF_INIT; |
| 1036 | struct strbuf summary = STRBUF_INIT; |
| 1037 | char *summary_content; |
| 1038 | |
| 1039 | strvec_pushf(&sm_summary.env, "GIT_INDEX_FILE=%s", s->index_file); |
| 1040 | |
| 1041 | strvec_push(&sm_summary.args, "submodule"); |
| 1042 | strvec_push(&sm_summary.args, "summary"); |
| 1043 | strvec_push(&sm_summary.args, uncommitted ? "--files" : "--cached"); |
| 1044 | strvec_push(&sm_summary.args, "--for-status"); |
| 1045 | strvec_push(&sm_summary.args, "--summary-limit"); |
| 1046 | strvec_pushf(&sm_summary.args, "%d", s->submodule_summary); |
| 1047 | if (!uncommitted) |
| 1048 | strvec_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD"); |
| 1049 | |
| 1050 | sm_summary.git_cmd = 1; |
| 1051 | sm_summary.no_stdin = 1; |
| 1052 | |
| 1053 | capture_command(&sm_summary, &cmd_stdout, 1024); |
| 1054 | |
| 1055 | /* prepend header, only if there's an actual output */ |
| 1056 | if (cmd_stdout.len) { |
| 1057 | if (uncommitted) |
| 1058 | strbuf_addstr(&summary, _("Submodules changed but not updated:")); |
| 1059 | else |
| 1060 | strbuf_addstr(&summary, _("Submodule changes to be committed:")); |
| 1061 | strbuf_addstr(&summary, "\n\n"); |
| 1062 | } |
| 1063 | strbuf_addbuf(&summary, &cmd_stdout); |
| 1064 | strbuf_release(&cmd_stdout); |
| 1065 | |
| 1066 | if (s->display_comment_prefix) { |
| 1067 | size_t len; |
| 1068 | summary_content = strbuf_detach(&summary, &len); |
| 1069 | strbuf_add_commented_lines(&summary, summary_content, len, comment_line_str); |
| 1070 | free(summary_content); |
| 1071 | } |
| 1072 | |
| 1073 | fputs(summary.buf, s->fp); |
| 1074 | strbuf_release(&summary); |
| 1075 | } |
| 1076 | |
| 1077 | static void wt_longstatus_print_other(struct wt_status *s, |
| 1078 | struct string_list *l, |
| 1079 | const char *what, |
| 1080 | const char *how) |
| 1081 | { |
| 1082 | int i; |
| 1083 | struct strbuf buf = STRBUF_INIT; |
| 1084 | static struct string_list output = STRING_LIST_INIT_DUP; |
| 1085 | struct column_options copts; |
| 1086 | |
| 1087 | if (!l->nr) |
| 1088 | return; |
| 1089 | |
| 1090 | wt_longstatus_print_other_header(s, what, how); |
| 1091 | |
| 1092 | for (i = 0; i < l->nr; i++) { |
| 1093 | struct string_list_item *it; |
| 1094 | const char *path; |
| 1095 | it = &(l->items[i]); |
| 1096 | path = quote_path(it->string, s->prefix, &buf, 0); |
| 1097 | if (column_active(s->colopts)) { |
| 1098 | string_list_append(&output, path); |
| 1099 | continue; |
| 1100 | } |
| 1101 | status_printf(s, color(WT_STATUS_HEADER, s), "\t"); |
| 1102 | status_printf_more(s, color(WT_STATUS_UNTRACKED, s), |
| 1103 | "%s\n", path); |
| 1104 | } |
| 1105 | |
| 1106 | strbuf_release(&buf); |
| 1107 | if (!column_active(s->colopts)) |
| 1108 | goto conclude; |
| 1109 | |
| 1110 | strbuf_addf(&buf, "%s%s\t%s", |
| 1111 | color(WT_STATUS_HEADER, s), |
| 1112 | s->display_comment_prefix ? "#" : "", |
| 1113 | color(WT_STATUS_UNTRACKED, s)); |
| 1114 | memset(&copts, 0, sizeof(copts)); |
| 1115 | copts.padding = 1; |
| 1116 | copts.indent = buf.buf; |
| 1117 | if (want_color(s->use_color)) |
| 1118 | copts.nl = GIT_COLOR_RESET "\n"; |
| 1119 | print_columns(&output, s->colopts, &copts); |
| 1120 | string_list_clear(&output, 0); |
| 1121 | strbuf_release(&buf); |
| 1122 | conclude: |
| 1123 | status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); |
| 1124 | } |
| 1125 | |
| 1126 | size_t wt_status_locate_end(const char *s, size_t len) |
| 1127 | { |
| 1128 | const char *p; |
| 1129 | struct strbuf pattern = STRBUF_INIT; |
| 1130 | |
| 1131 | strbuf_addf(&pattern, "\n%s %s", comment_line_str, cut_line); |
| 1132 | if (starts_with(s, pattern.buf + 1)) |
| 1133 | len = 0; |
| 1134 | else if ((p = strstr(s, pattern.buf))) { |
| 1135 | size_t newlen = p - s + 1; |
| 1136 | if (newlen < len) |
| 1137 | len = newlen; |
| 1138 | } |
| 1139 | strbuf_release(&pattern); |
| 1140 | return len; |
| 1141 | } |
| 1142 | |
| 1143 | void wt_status_append_cut_line(struct strbuf *buf) |
| 1144 | { |
| 1145 | const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored."); |
| 1146 | |
| 1147 | strbuf_commented_addf(buf, comment_line_str, "%s", cut_line); |
| 1148 | strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_str); |
| 1149 | } |
| 1150 | |
| 1151 | void wt_status_add_cut_line(struct wt_status *s) |
| 1152 | { |
| 1153 | struct strbuf buf = STRBUF_INIT; |
| 1154 | |
| 1155 | if (s->added_cut_line) |
| 1156 | return; |
| 1157 | s->added_cut_line = 1; |
| 1158 | wt_status_append_cut_line(&buf); |
| 1159 | fputs(buf.buf, s->fp); |
| 1160 | strbuf_release(&buf); |
| 1161 | } |
| 1162 | |
| 1163 | static void wt_longstatus_print_verbose(struct wt_status *s) |
| 1164 | { |
| 1165 | struct rev_info rev; |
| 1166 | struct setup_revision_opt opt; |
| 1167 | int dirty_submodules; |
| 1168 | const char *c = color(WT_STATUS_HEADER, s); |
| 1169 | |
| 1170 | repo_init_revisions(s->repo, &rev, NULL); |
| 1171 | rev.diffopt.flags.allow_textconv = 1; |
| 1172 | rev.diffopt.ita_invisible_in_index = 1; |
| 1173 | |
| 1174 | memset(&opt, 0, sizeof(opt)); |
| 1175 | opt.def = s->is_initial ? empty_tree_oid_hex(s->repo->hash_algo) : s->reference; |
| 1176 | setup_revisions(0, NULL, &rev, &opt); |
| 1177 | |
| 1178 | rev.diffopt.output_format |= DIFF_FORMAT_PATCH; |
| 1179 | rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename; |
| 1180 | rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit; |
| 1181 | rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score; |
| 1182 | rev.diffopt.file = s->fp; |
| 1183 | rev.diffopt.close_file = 0; |
| 1184 | /* |
| 1185 | * If we're not going to stdout, then we definitely don't |
| 1186 | * want color, since we are going to the commit message |
| 1187 | * file (and even the "auto" setting won't work, since it |
| 1188 | * will have checked isatty on stdout). But we then do want |
| 1189 | * to insert the scissor line here to reliably remove the |
| 1190 | * diff before committing, if we didn't already include one |
| 1191 | * before. |
| 1192 | */ |
| 1193 | if (s->fp != stdout) { |
| 1194 | rev.diffopt.use_color = GIT_COLOR_NEVER; |
| 1195 | wt_status_add_cut_line(s); |
| 1196 | } |
| 1197 | if (s->verbose > 1 && s->committable) { |
| 1198 | /* print_updated() printed a header, so do we */ |
| 1199 | if (s->fp != stdout) |
| 1200 | wt_longstatus_print_trailer(s); |
| 1201 | status_printf_ln(s, c, _("Changes to be committed:")); |
| 1202 | rev.diffopt.a_prefix = "c/"; |
| 1203 | rev.diffopt.b_prefix = "i/"; |
| 1204 | } /* else use prefix as per user config */ |
| 1205 | run_diff_index(&rev, DIFF_INDEX_CACHED); |
| 1206 | if (s->verbose > 1 && |
| 1207 | wt_status_check_worktree_changes(s, &dirty_submodules)) { |
| 1208 | status_printf_ln(s, c, |
| 1209 | "--------------------------------------------------"); |
| 1210 | status_printf_ln(s, c, _("Changes not staged for commit:")); |
| 1211 | setup_work_tree(the_repository); |
| 1212 | rev.diffopt.a_prefix = "i/"; |
| 1213 | rev.diffopt.b_prefix = "w/"; |
| 1214 | run_diff_files(&rev, 0); |
| 1215 | } |
| 1216 | release_revisions(&rev); |
| 1217 | } |
| 1218 | |
| 1219 | static void wt_longstatus_print_tracking(struct wt_status *s) |
| 1220 | { |
| 1221 | struct strbuf sb = STRBUF_INIT; |
| 1222 | const char *cp, *ep, *branch_name; |
| 1223 | struct branch *branch; |
| 1224 | uint64_t t_begin = 0; |
| 1225 | |
| 1226 | assert(s->branch && !s->is_initial); |
| 1227 | if (!skip_prefix(s->branch, "refs/heads/", &branch_name)) |
| 1228 | return; |
| 1229 | branch = branch_get(branch_name); |
| 1230 | |
| 1231 | t_begin = getnanotime(); |
| 1232 | |
| 1233 | if (!format_tracking_info(branch, &sb, s->ahead_behind_flags, |
| 1234 | !s->commit_template)) |
| 1235 | return; |
| 1236 | |
| 1237 | if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING) && |
| 1238 | s->ahead_behind_flags == AHEAD_BEHIND_FULL) { |
| 1239 | uint64_t t_delta_in_ms = (getnanotime() - t_begin) / 1000000; |
| 1240 | if (t_delta_in_ms > AB_DELAY_WARNING_IN_MS) { |
| 1241 | strbuf_addf(&sb, _("\n" |
| 1242 | "It took %.2f seconds to compute the branch ahead/behind values.\n" |
| 1243 | "You can use '--no-ahead-behind' to avoid this.\n"), |
| 1244 | t_delta_in_ms / 1000.0); |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1) |
| 1249 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), |
| 1250 | "%s%s%.*s", |
| 1251 | s->display_comment_prefix ? comment_line_str : "", |
| 1252 | s->display_comment_prefix ? " " : "", |
| 1253 | (int)(ep - cp), cp); |
| 1254 | if (s->display_comment_prefix) |
| 1255 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%s", |
| 1256 | comment_line_str); |
| 1257 | else |
| 1258 | fputs("\n", s->fp); |
| 1259 | strbuf_release(&sb); |
| 1260 | } |
| 1261 | |
| 1262 | static int uf_was_slow(struct wt_status *s) |
| 1263 | { |
| 1264 | if (getenv("GIT_TEST_UF_DELAY_WARNING")) |
| 1265 | s->untracked_in_ms = 3250; |
| 1266 | return UF_DELAY_WARNING_IN_MS < s->untracked_in_ms; |
| 1267 | } |
| 1268 | |
| 1269 | static void show_merge_in_progress(struct wt_status *s, |
| 1270 | const char *color) |
| 1271 | { |
| 1272 | if (has_unmerged(s)) { |
| 1273 | status_printf_ln(s, color, _("You have unmerged paths.")); |
| 1274 | if (s->hints) { |
| 1275 | status_printf_ln(s, color, |
| 1276 | _(" (fix conflicts and run \"git commit\")")); |
| 1277 | status_printf_ln(s, color, |
| 1278 | _(" (use \"git merge --abort\" to abort the merge)")); |
| 1279 | } |
| 1280 | } else { |
| 1281 | status_printf_ln(s, color, |
| 1282 | _("All conflicts fixed but you are still merging.")); |
| 1283 | if (s->hints) |
| 1284 | status_printf_ln(s, color, |
| 1285 | _(" (use \"git commit\" to conclude merge)")); |
| 1286 | } |
| 1287 | wt_longstatus_print_trailer(s); |
| 1288 | } |
| 1289 | |
| 1290 | static void show_am_in_progress(struct wt_status *s, |
| 1291 | const char *color) |
| 1292 | { |
| 1293 | int am_empty_patch; |
| 1294 | |
| 1295 | status_printf_ln(s, color, |
| 1296 | _("You are in the middle of an am session.")); |
| 1297 | if (s->state.am_empty_patch) |
| 1298 | status_printf_ln(s, color, |
| 1299 | _("The current patch is empty.")); |
| 1300 | if (s->hints) { |
| 1301 | am_empty_patch = s->state.am_empty_patch; |
| 1302 | if (!am_empty_patch) |
| 1303 | status_printf_ln(s, color, |
| 1304 | _(" (fix conflicts and then run \"git am --continue\")")); |
| 1305 | status_printf_ln(s, color, |
| 1306 | _(" (use \"git am --skip\" to skip this patch)")); |
| 1307 | if (am_empty_patch) |
| 1308 | status_printf_ln(s, color, |
| 1309 | _(" (use \"git am --allow-empty\" to record this patch as an empty commit)")); |
| 1310 | status_printf_ln(s, color, |
| 1311 | _(" (use \"git am --abort\" to restore the original branch)")); |
| 1312 | } |
| 1313 | wt_longstatus_print_trailer(s); |
| 1314 | } |
| 1315 | |
| 1316 | static char *read_line_from_git_path(struct repository *r, const char *filename) |
| 1317 | { |
| 1318 | struct strbuf buf = STRBUF_INIT; |
| 1319 | FILE *fp = fopen_or_warn(repo_git_path_append(r, &buf, |
| 1320 | "%s", filename), "r"); |
| 1321 | |
| 1322 | if (!fp) { |
| 1323 | strbuf_release(&buf); |
| 1324 | return NULL; |
| 1325 | } |
| 1326 | strbuf_getline_lf(&buf, fp); |
| 1327 | if (!fclose(fp)) { |
| 1328 | return strbuf_detach(&buf, NULL); |
| 1329 | } else { |
| 1330 | strbuf_release(&buf); |
| 1331 | return NULL; |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | static int split_commit_in_progress(struct wt_status *s) |
| 1336 | { |
| 1337 | int split_in_progress = 0; |
| 1338 | struct object_id head_oid, orig_head_oid; |
| 1339 | char *rebase_amend, *rebase_orig_head; |
| 1340 | int head_flags, orig_head_flags; |
| 1341 | |
| 1342 | if ((!s->amend && !s->nowarn && !s->workdir_dirty) || |
| 1343 | !s->branch || strcmp(s->branch, "HEAD")) |
| 1344 | return 0; |
| 1345 | |
| 1346 | if (refs_read_ref_full(get_main_ref_store(s->repo), "HEAD", RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, |
| 1347 | &head_oid, &head_flags) || |
| 1348 | refs_read_ref_full(get_main_ref_store(s->repo), "ORIG_HEAD", RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, |
| 1349 | &orig_head_oid, &orig_head_flags)) |
| 1350 | return 0; |
| 1351 | if (head_flags & REF_ISSYMREF || orig_head_flags & REF_ISSYMREF) |
| 1352 | return 0; |
| 1353 | |
| 1354 | rebase_amend = read_line_from_git_path(s->repo, "rebase-merge/amend"); |
| 1355 | rebase_orig_head = read_line_from_git_path(s->repo, "rebase-merge/orig-head"); |
| 1356 | |
| 1357 | if (!rebase_amend || !rebase_orig_head) |
| 1358 | ; /* fall through, no split in progress */ |
| 1359 | else if (!strcmp(rebase_amend, rebase_orig_head)) |
| 1360 | split_in_progress = !!strcmp(oid_to_hex(&head_oid), rebase_amend); |
| 1361 | else if (strcmp(oid_to_hex(&orig_head_oid), rebase_orig_head)) |
| 1362 | split_in_progress = 1; |
| 1363 | |
| 1364 | free(rebase_amend); |
| 1365 | free(rebase_orig_head); |
| 1366 | |
| 1367 | return split_in_progress; |
| 1368 | } |
| 1369 | |
| 1370 | /* |
| 1371 | * If the whitespace-delimited token starting at or just after *pp |
| 1372 | * is a hex object id that is longer than its default abbreviation, |
| 1373 | * abbreviate it in-place, shrinking `line` accordingly. On return |
| 1374 | * *pp points one past the (possibly abbreviated) token. Leaves both |
| 1375 | * `line` and *pp-advanced-past-the-token unchanged in all other cases |
| 1376 | * (non-hex token, label name, unresolvable, or a refname that happens |
| 1377 | * to consist only of hex digits). |
| 1378 | */ |
| 1379 | static void abbrev_oid_in_line(struct repository *r, struct strbuf *scratch, |
| 1380 | struct strbuf *line, bool maybe_label, char **pp) |
| 1381 | { |
| 1382 | char *p = *pp; |
| 1383 | char *end_of_object_name, saved; |
| 1384 | const char *abbrev; |
| 1385 | struct object_id oid; |
| 1386 | bool have_oid; |
| 1387 | |
| 1388 | p += strspn(p, " \t"); |
| 1389 | end_of_object_name = p + strcspn(p, " \t"); |
| 1390 | /* |
| 1391 | * For "merge" and "reset" the object name may be a label or |
| 1392 | * ref rather than a hex object id. Only abbreviate the object |
| 1393 | * name if it is a hex object id. |
| 1394 | */ |
| 1395 | for (const char *q = p; q < end_of_object_name; q++) { |
| 1396 | if (!isxdigit(*q)) |
| 1397 | goto out; |
| 1398 | } |
| 1399 | if (maybe_label) { |
| 1400 | strbuf_reset(scratch); |
| 1401 | strbuf_addf(scratch, "refs/rewritten/%.*s", |
| 1402 | (int)(end_of_object_name - p), p); |
| 1403 | if (refs_ref_exists(get_main_ref_store(r), scratch->buf)) |
| 1404 | goto out; /* object name was a label */ |
| 1405 | } |
| 1406 | saved = *end_of_object_name; |
| 1407 | *end_of_object_name = '\0'; |
| 1408 | have_oid = !repo_get_oid(r, p, &oid); |
| 1409 | *end_of_object_name = saved; |
| 1410 | if (!have_oid) |
| 1411 | goto out; /* invalid object name */ |
| 1412 | abbrev = repo_find_unique_abbrev(r, &oid, DEFAULT_ABBREV); |
| 1413 | if (!starts_with(p, abbrev)) |
| 1414 | goto out; /* object name was a refname containing only xdigits */ |
| 1415 | p += strlen(abbrev); |
| 1416 | strbuf_remove(line, p - line->buf, end_of_object_name - p); |
| 1417 | end_of_object_name = p; |
| 1418 | out: |
| 1419 | *pp = end_of_object_name; |
| 1420 | } |
| 1421 | |
| 1422 | /* Skip "[ \t]*(-[cC])?", returns true if "-c/-C" was skipped. */ |
| 1423 | static bool skip_dash_c(char **pp) |
| 1424 | { |
| 1425 | bool ret; |
| 1426 | char *p = *pp; |
| 1427 | |
| 1428 | p += strspn(p, " \t"); |
| 1429 | ret = skip_prefix(p, "-C", &p) || skip_prefix(p, "-c", &p); |
| 1430 | *pp = p; |
| 1431 | |
| 1432 | return ret; |
| 1433 | } |
| 1434 | |
| 1435 | /* |
| 1436 | * Turn |
| 1437 | * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message" |
| 1438 | * into |
| 1439 | * "pick d6a2f03 some message" |
| 1440 | * |
| 1441 | * Returns false on comment lines, true otherwise |
| 1442 | */ |
| 1443 | static bool format_todo_line(struct repository *r, struct strbuf *line) |
| 1444 | { |
| 1445 | enum todo_command cmd; |
| 1446 | struct strbuf scratch = STRBUF_INIT; |
| 1447 | char *p = line->buf; |
| 1448 | |
| 1449 | if (!sequencer_parse_todo_command((const char **)&p, &cmd)) |
| 1450 | return true; /* keep invalid lines */ |
| 1451 | |
| 1452 | switch (cmd) { |
| 1453 | case TODO_COMMENT: |
| 1454 | return false; |
| 1455 | |
| 1456 | case TODO_MERGE: { |
| 1457 | /* |
| 1458 | * The argument to -C cannot be a label, but the parents |
| 1459 | * can be labels. |
| 1460 | */ |
| 1461 | bool maybe_label = !skip_dash_c(&p); |
| 1462 | |
| 1463 | while (true) { |
| 1464 | p += strspn(p, " \t"); |
| 1465 | if (!p[0] || (p[0] == '#' && (!p[1] || isspace(p[1])))) |
| 1466 | break; |
| 1467 | abbrev_oid_in_line(r, &scratch, line, maybe_label, &p); |
| 1468 | maybe_label = true; |
| 1469 | } |
| 1470 | break; |
| 1471 | } |
| 1472 | |
| 1473 | case TODO_FIXUP: |
| 1474 | skip_dash_c(&p); |
| 1475 | /* fallthrough */ |
| 1476 | case TODO_DROP: |
| 1477 | case TODO_EDIT: |
| 1478 | case TODO_PICK: |
| 1479 | case TODO_REVERT: |
| 1480 | case TODO_REWORD: |
| 1481 | case TODO_SQUASH: |
| 1482 | abbrev_oid_in_line(r, &scratch, line, false, &p); |
| 1483 | break; |
| 1484 | |
| 1485 | case TODO_RESET: |
| 1486 | abbrev_oid_in_line(r, &scratch, line, true, &p); |
| 1487 | break; |
| 1488 | /* |
| 1489 | * Avoid "default" and instead list all the other commands so |
| 1490 | * that -Wswitch (which is included in -Wall) warns if a new |
| 1491 | * command is added without handling it in this function. |
| 1492 | */ |
| 1493 | case TODO_BREAK: |
| 1494 | case TODO_EXEC: |
| 1495 | case TODO_LABEL: |
| 1496 | case TODO_NOOP: |
| 1497 | case TODO_UPDATE_REF: |
| 1498 | break; |
| 1499 | } |
| 1500 | |
| 1501 | strbuf_release(&scratch); |
| 1502 | return true; |
| 1503 | } |
| 1504 | |
| 1505 | static int read_rebase_todolist(struct repository *r, const char *fname, struct string_list *lines) |
| 1506 | { |
| 1507 | struct strbuf buf = STRBUF_INIT; |
| 1508 | FILE *f = fopen(repo_git_path_append(r, &buf, "%s", fname), "r"); |
| 1509 | int ret; |
| 1510 | |
| 1511 | if (!f) { |
| 1512 | if (errno == ENOENT) { |
| 1513 | ret = -1; |
| 1514 | goto out; |
| 1515 | } |
| 1516 | die_errno("Could not open file %s for reading", |
| 1517 | repo_git_path_replace(r, &buf, "%s", fname)); |
| 1518 | } |
| 1519 | while (!strbuf_getline_lf(&buf, f)) { |
| 1520 | strbuf_trim(&buf); |
| 1521 | if (format_todo_line(r, &buf)) |
| 1522 | string_list_append(lines, buf.buf); |
| 1523 | } |
| 1524 | fclose(f); |
| 1525 | |
| 1526 | ret = 0; |
| 1527 | out: |
| 1528 | strbuf_release(&buf); |
| 1529 | return ret; |
| 1530 | } |
| 1531 | |
| 1532 | static void show_rebase_information(struct wt_status *s, |
| 1533 | const char *color) |
| 1534 | { |
| 1535 | if (s->state.rebase_interactive_in_progress) { |
| 1536 | int i; |
| 1537 | int nr_lines_to_show = 2; |
| 1538 | |
| 1539 | struct string_list have_done = STRING_LIST_INIT_DUP; |
| 1540 | struct string_list yet_to_do = STRING_LIST_INIT_DUP; |
| 1541 | |
| 1542 | read_rebase_todolist(s->repo, "rebase-merge/done", &have_done); |
| 1543 | if (read_rebase_todolist(s->repo, "rebase-merge/git-rebase-todo", |
| 1544 | &yet_to_do)) |
| 1545 | status_printf_ln(s, color, |
| 1546 | _("git-rebase-todo is missing.")); |
| 1547 | if (have_done.nr == 0) |
| 1548 | status_printf_ln(s, color, _("No commands done.")); |
| 1549 | else { |
| 1550 | status_printf_ln(s, color, |
| 1551 | Q_("Last command done (%"PRIuMAX" command done):", |
| 1552 | "Last commands done (%"PRIuMAX" commands done):", |
| 1553 | have_done.nr), |
| 1554 | (uintmax_t)have_done.nr); |
| 1555 | for (i = (have_done.nr > nr_lines_to_show) |
| 1556 | ? have_done.nr - nr_lines_to_show : 0; |
| 1557 | i < have_done.nr; |
| 1558 | i++) |
| 1559 | status_printf_ln(s, color, " %s", have_done.items[i].string); |
| 1560 | if (have_done.nr > nr_lines_to_show && s->hints) { |
| 1561 | char *path = repo_git_path(s->repo, "rebase-merge/done"); |
| 1562 | status_printf_ln(s, color, |
| 1563 | _(" (see more in file %s)"), path); |
| 1564 | free(path); |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | if (yet_to_do.nr == 0) |
| 1569 | status_printf_ln(s, color, |
| 1570 | _("No commands remaining.")); |
| 1571 | else { |
| 1572 | status_printf_ln(s, color, |
| 1573 | Q_("Next command to do (%"PRIuMAX" remaining command):", |
| 1574 | "Next commands to do (%"PRIuMAX" remaining commands):", |
| 1575 | yet_to_do.nr), |
| 1576 | (uintmax_t)yet_to_do.nr); |
| 1577 | for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++) |
| 1578 | status_printf_ln(s, color, " %s", yet_to_do.items[i].string); |
| 1579 | if (s->hints) |
| 1580 | status_printf_ln(s, color, |
| 1581 | _(" (use \"git rebase --edit-todo\" to view and edit)")); |
| 1582 | } |
| 1583 | string_list_clear(&yet_to_do, 0); |
| 1584 | string_list_clear(&have_done, 0); |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | static void print_rebase_state(struct wt_status *s, |
| 1589 | const char *color) |
| 1590 | { |
| 1591 | if (s->state.branch) |
| 1592 | status_printf_ln(s, color, |
| 1593 | _("You are currently rebasing branch '%s' on '%s'."), |
| 1594 | s->state.branch, |
| 1595 | s->state.onto); |
| 1596 | else |
| 1597 | status_printf_ln(s, color, |
| 1598 | _("You are currently rebasing.")); |
| 1599 | } |
| 1600 | |
| 1601 | static void show_rebase_in_progress(struct wt_status *s, |
| 1602 | const char *color) |
| 1603 | { |
| 1604 | struct stat st; |
| 1605 | |
| 1606 | show_rebase_information(s, color); |
| 1607 | if (has_unmerged(s)) { |
| 1608 | print_rebase_state(s, color); |
| 1609 | if (s->hints) { |
| 1610 | status_printf_ln(s, color, |
| 1611 | _(" (fix conflicts and then run \"git rebase --continue\")")); |
| 1612 | status_printf_ln(s, color, |
| 1613 | _(" (use \"git rebase --skip\" to skip this patch)")); |
| 1614 | status_printf_ln(s, color, |
| 1615 | _(" (use \"git rebase --abort\" to check out the original branch)")); |
| 1616 | } |
| 1617 | } else if (s->state.rebase_in_progress || |
| 1618 | !stat(git_path_merge_msg(s->repo), &st)) { |
| 1619 | print_rebase_state(s, color); |
| 1620 | if (s->hints) |
| 1621 | status_printf_ln(s, color, |
| 1622 | _(" (all conflicts fixed: run \"git rebase --continue\")")); |
| 1623 | } else if (split_commit_in_progress(s)) { |
| 1624 | if (s->state.branch) |
| 1625 | status_printf_ln(s, color, |
| 1626 | _("You are currently splitting a commit while rebasing branch '%s' on '%s'."), |
| 1627 | s->state.branch, |
| 1628 | s->state.onto); |
| 1629 | else |
| 1630 | status_printf_ln(s, color, |
| 1631 | _("You are currently splitting a commit during a rebase.")); |
| 1632 | if (s->hints) |
| 1633 | status_printf_ln(s, color, |
| 1634 | _(" (Once your working directory is clean, run \"git rebase --continue\")")); |
| 1635 | } else { |
| 1636 | if (s->state.branch) |
| 1637 | status_printf_ln(s, color, |
| 1638 | _("You are currently editing a commit while rebasing branch '%s' on '%s'."), |
| 1639 | s->state.branch, |
| 1640 | s->state.onto); |
| 1641 | else |
| 1642 | status_printf_ln(s, color, |
| 1643 | _("You are currently editing a commit during a rebase.")); |
| 1644 | if (s->hints && !s->amend) { |
| 1645 | status_printf_ln(s, color, |
| 1646 | _(" (use \"git commit --amend\" to amend the current commit)")); |
| 1647 | status_printf_ln(s, color, |
| 1648 | _(" (use \"git rebase --continue\" once you are satisfied with your changes)")); |
| 1649 | } |
| 1650 | } |
| 1651 | wt_longstatus_print_trailer(s); |
| 1652 | } |
| 1653 | |
| 1654 | static void show_cherry_pick_in_progress(struct wt_status *s, |
| 1655 | const char *color) |
| 1656 | { |
| 1657 | if (is_null_oid(&s->state.cherry_pick_head_oid)) |
| 1658 | status_printf_ln(s, color, |
| 1659 | _("Cherry-pick currently in progress.")); |
| 1660 | else |
| 1661 | status_printf_ln(s, color, |
| 1662 | _("You are currently cherry-picking commit %s."), |
| 1663 | repo_find_unique_abbrev(s->repo, &s->state.cherry_pick_head_oid, |
| 1664 | DEFAULT_ABBREV)); |
| 1665 | |
| 1666 | if (s->hints) { |
| 1667 | if (has_unmerged(s)) |
| 1668 | status_printf_ln(s, color, |
| 1669 | _(" (fix conflicts and run \"git cherry-pick --continue\")")); |
| 1670 | else if (is_null_oid(&s->state.cherry_pick_head_oid)) |
| 1671 | status_printf_ln(s, color, |
| 1672 | _(" (run \"git cherry-pick --continue\" to continue)")); |
| 1673 | else |
| 1674 | status_printf_ln(s, color, |
| 1675 | _(" (all conflicts fixed: run \"git cherry-pick --continue\")")); |
| 1676 | status_printf_ln(s, color, |
| 1677 | _(" (use \"git cherry-pick --skip\" to skip this patch)")); |
| 1678 | status_printf_ln(s, color, |
| 1679 | _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)")); |
| 1680 | } |
| 1681 | wt_longstatus_print_trailer(s); |
| 1682 | } |
| 1683 | |
| 1684 | static void show_revert_in_progress(struct wt_status *s, |
| 1685 | const char *color) |
| 1686 | { |
| 1687 | if (is_null_oid(&s->state.revert_head_oid)) |
| 1688 | status_printf_ln(s, color, |
| 1689 | _("Revert currently in progress.")); |
| 1690 | else |
| 1691 | status_printf_ln(s, color, |
| 1692 | _("You are currently reverting commit %s."), |
| 1693 | repo_find_unique_abbrev(s->repo, &s->state.revert_head_oid, |
| 1694 | DEFAULT_ABBREV)); |
| 1695 | if (s->hints) { |
| 1696 | if (has_unmerged(s)) |
| 1697 | status_printf_ln(s, color, |
| 1698 | _(" (fix conflicts and run \"git revert --continue\")")); |
| 1699 | else if (is_null_oid(&s->state.revert_head_oid)) |
| 1700 | status_printf_ln(s, color, |
| 1701 | _(" (run \"git revert --continue\" to continue)")); |
| 1702 | else |
| 1703 | status_printf_ln(s, color, |
| 1704 | _(" (all conflicts fixed: run \"git revert --continue\")")); |
| 1705 | status_printf_ln(s, color, |
| 1706 | _(" (use \"git revert --skip\" to skip this patch)")); |
| 1707 | status_printf_ln(s, color, |
| 1708 | _(" (use \"git revert --abort\" to cancel the revert operation)")); |
| 1709 | } |
| 1710 | wt_longstatus_print_trailer(s); |
| 1711 | } |
| 1712 | |
| 1713 | static void show_bisect_in_progress(struct wt_status *s, |
| 1714 | const char *color) |
| 1715 | { |
| 1716 | if (s->state.bisecting_from) |
| 1717 | status_printf_ln(s, color, |
| 1718 | _("You are currently bisecting, started from branch '%s'."), |
| 1719 | s->state.bisecting_from); |
| 1720 | else |
| 1721 | status_printf_ln(s, color, |
| 1722 | _("You are currently bisecting.")); |
| 1723 | if (s->hints) |
| 1724 | status_printf_ln(s, color, |
| 1725 | _(" (use \"git bisect reset\" to get back to the original branch)")); |
| 1726 | wt_longstatus_print_trailer(s); |
| 1727 | } |
| 1728 | |
| 1729 | static void show_sparse_checkout_in_use(struct wt_status *s, |
| 1730 | const char *color) |
| 1731 | { |
| 1732 | if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_DISABLED) |
| 1733 | return; |
| 1734 | |
| 1735 | if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_SPARSE_INDEX) |
| 1736 | status_printf_ln(s, color, _("You are in a sparse checkout.")); |
| 1737 | else |
| 1738 | status_printf_ln(s, color, |
| 1739 | _("You are in a sparse checkout with %d%% of tracked files present."), |
| 1740 | s->state.sparse_checkout_percentage); |
| 1741 | wt_longstatus_print_trailer(s); |
| 1742 | } |
| 1743 | |
| 1744 | /* |
| 1745 | * Extract branch information from rebase/bisect |
| 1746 | */ |
| 1747 | static char *get_branch(const struct worktree *wt, const char *path) |
| 1748 | { |
| 1749 | struct strbuf sb = STRBUF_INIT; |
| 1750 | struct object_id oid; |
| 1751 | const char *branch_name; |
| 1752 | |
| 1753 | if (strbuf_read_file(&sb, worktree_git_path(wt, "%s", path), 0) <= 0) |
| 1754 | goto got_nothing; |
| 1755 | |
| 1756 | while (sb.len && sb.buf[sb.len - 1] == '\n') |
| 1757 | strbuf_setlen(&sb, sb.len - 1); |
| 1758 | if (!sb.len) |
| 1759 | goto got_nothing; |
| 1760 | if (skip_prefix(sb.buf, "refs/heads/", &branch_name)) |
| 1761 | strbuf_remove(&sb, 0, branch_name - sb.buf); |
| 1762 | else if (starts_with(sb.buf, "refs/")) |
| 1763 | ; |
| 1764 | else if (!get_oid_hex(sb.buf, &oid)) { |
| 1765 | strbuf_reset(&sb); |
| 1766 | strbuf_add_unique_abbrev(&sb, &oid, DEFAULT_ABBREV); |
| 1767 | } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */ |
| 1768 | goto got_nothing; |
| 1769 | else /* bisect */ |
| 1770 | ; |
| 1771 | return strbuf_detach(&sb, NULL); |
| 1772 | |
| 1773 | got_nothing: |
| 1774 | strbuf_release(&sb); |
| 1775 | return NULL; |
| 1776 | } |
| 1777 | |
| 1778 | struct grab_1st_switch_cbdata { |
| 1779 | struct strbuf buf; |
| 1780 | struct object_id noid; |
| 1781 | }; |
| 1782 | |
| 1783 | static int grab_1st_switch(const char *refname UNUSED, |
| 1784 | struct object_id *ooid UNUSED, |
| 1785 | struct object_id *noid, |
| 1786 | const char *email UNUSED, |
| 1787 | timestamp_t timestamp UNUSED, int tz UNUSED, |
| 1788 | const char *message, void *cb_data) |
| 1789 | { |
| 1790 | struct grab_1st_switch_cbdata *cb = cb_data; |
| 1791 | const char *target = NULL, *end; |
| 1792 | |
| 1793 | if (!skip_prefix(message, "checkout: moving from ", &message)) |
| 1794 | return 0; |
| 1795 | target = strstr(message, " to "); |
| 1796 | if (!target) |
| 1797 | return 0; |
| 1798 | target += strlen(" to "); |
| 1799 | strbuf_reset(&cb->buf); |
| 1800 | oidcpy(&cb->noid, noid); |
| 1801 | end = strchrnul(target, '\n'); |
| 1802 | strbuf_add(&cb->buf, target, end - target); |
| 1803 | if (!strcmp(cb->buf.buf, "HEAD")) { |
| 1804 | /* HEAD is relative. Resolve it to the right reflog entry. */ |
| 1805 | strbuf_reset(&cb->buf); |
| 1806 | strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV); |
| 1807 | } |
| 1808 | return 1; |
| 1809 | } |
| 1810 | |
| 1811 | static void wt_status_get_detached_from(struct repository *r, |
| 1812 | struct wt_status_state *state) |
| 1813 | { |
| 1814 | struct grab_1st_switch_cbdata cb; |
| 1815 | struct commit *commit; |
| 1816 | struct object_id oid; |
| 1817 | char *ref = NULL; |
| 1818 | |
| 1819 | strbuf_init(&cb.buf, 0); |
| 1820 | if (refs_for_each_reflog_ent_reverse(get_main_ref_store(r), "HEAD", grab_1st_switch, &cb) <= 0) { |
| 1821 | strbuf_release(&cb.buf); |
| 1822 | return; |
| 1823 | } |
| 1824 | |
| 1825 | if (repo_dwim_ref(r, cb.buf.buf, cb.buf.len, &oid, &ref, |
| 1826 | 1) == 1 && |
| 1827 | /* oid is a commit? match without further lookup */ |
| 1828 | (oideq(&cb.noid, &oid) || |
| 1829 | /* perhaps oid is a tag, try to dereference to a commit */ |
| 1830 | ((commit = lookup_commit_reference_gently(r, &oid, 1)) != NULL && |
| 1831 | oideq(&cb.noid, &commit->object.oid)))) { |
| 1832 | const char *from = ref; |
| 1833 | if (!skip_prefix(from, "refs/tags/", &from)) |
| 1834 | skip_prefix(from, "refs/remotes/", &from); |
| 1835 | state->detached_from = xstrdup(from); |
| 1836 | } else |
| 1837 | state->detached_from = |
| 1838 | xstrdup(repo_find_unique_abbrev(r, &cb.noid, DEFAULT_ABBREV)); |
| 1839 | oidcpy(&state->detached_oid, &cb.noid); |
| 1840 | state->detached_at = !repo_get_oid(r, "HEAD", &oid) && |
| 1841 | oideq(&oid, &state->detached_oid); |
| 1842 | |
| 1843 | free(ref); |
| 1844 | strbuf_release(&cb.buf); |
| 1845 | } |
| 1846 | |
| 1847 | int wt_status_check_rebase(const struct worktree *wt, |
| 1848 | struct wt_status_state *state) |
| 1849 | { |
| 1850 | struct stat st; |
| 1851 | |
| 1852 | if (!wt) |
| 1853 | BUG("wt_status_check_rebase() called with NULL worktree"); |
| 1854 | |
| 1855 | if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) { |
| 1856 | if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) { |
| 1857 | state->am_in_progress = 1; |
| 1858 | if (!stat(worktree_git_path(wt, "rebase-apply/patch"), &st) && !st.st_size) |
| 1859 | state->am_empty_patch = 1; |
| 1860 | } else { |
| 1861 | state->rebase_in_progress = 1; |
| 1862 | state->branch = get_branch(wt, "rebase-apply/head-name"); |
| 1863 | state->onto = get_branch(wt, "rebase-apply/onto"); |
| 1864 | } |
| 1865 | } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) { |
| 1866 | if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st)) |
| 1867 | state->rebase_interactive_in_progress = 1; |
| 1868 | else |
| 1869 | state->rebase_in_progress = 1; |
| 1870 | state->branch = get_branch(wt, "rebase-merge/head-name"); |
| 1871 | state->onto = get_branch(wt, "rebase-merge/onto"); |
| 1872 | } else |
| 1873 | return 0; |
| 1874 | return 1; |
| 1875 | } |
| 1876 | |
| 1877 | int wt_status_check_bisect(const struct worktree *wt, |
| 1878 | struct wt_status_state *state) |
| 1879 | { |
| 1880 | struct stat st; |
| 1881 | |
| 1882 | if (!wt) |
| 1883 | BUG("wt_status_check_bisect() called with NULL worktree"); |
| 1884 | |
| 1885 | if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) { |
| 1886 | state->bisect_in_progress = 1; |
| 1887 | state->bisecting_from = get_branch(wt, "BISECT_START"); |
| 1888 | return 1; |
| 1889 | } |
| 1890 | return 0; |
| 1891 | } |
| 1892 | |
| 1893 | static void wt_status_check_sparse_checkout(struct repository *r, |
| 1894 | struct wt_status_state *state) |
| 1895 | { |
| 1896 | int skip_worktree = 0; |
| 1897 | int i; |
| 1898 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 1899 | |
| 1900 | if (!cfg->apply_sparse_checkout || |
| 1901 | r->index->cache_nr == 0) { |
| 1902 | /* |
| 1903 | * Don't compute percentage of checked out files if we |
| 1904 | * aren't in a sparse checkout or would get division by 0. |
| 1905 | */ |
| 1906 | state->sparse_checkout_percentage = SPARSE_CHECKOUT_DISABLED; |
| 1907 | return; |
| 1908 | } |
| 1909 | |
| 1910 | if (r->index->sparse_index) { |
| 1911 | state->sparse_checkout_percentage = SPARSE_CHECKOUT_SPARSE_INDEX; |
| 1912 | return; |
| 1913 | } |
| 1914 | |
| 1915 | for (i = 0; i < r->index->cache_nr; i++) { |
| 1916 | struct cache_entry *ce = r->index->cache[i]; |
| 1917 | if (ce_skip_worktree(ce)) |
| 1918 | skip_worktree++; |
| 1919 | } |
| 1920 | |
| 1921 | state->sparse_checkout_percentage = |
| 1922 | 100 - (100 * skip_worktree)/r->index->cache_nr; |
| 1923 | } |
| 1924 | |
| 1925 | void wt_status_get_state(struct repository *r, |
| 1926 | struct wt_status_state *state, |
| 1927 | int get_detached_from) |
| 1928 | { |
| 1929 | struct stat st; |
| 1930 | struct object_id oid; |
| 1931 | enum replay_action action; |
| 1932 | struct worktree *wt = get_current_worktree(r); |
| 1933 | |
| 1934 | if (!stat(git_path_merge_head(r), &st)) { |
| 1935 | wt_status_check_rebase(wt, state); |
| 1936 | state->merge_in_progress = 1; |
| 1937 | } else if (wt_status_check_rebase(wt, state)) { |
| 1938 | ; /* all set */ |
| 1939 | } else if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") && |
| 1940 | !repo_get_oid(r, "CHERRY_PICK_HEAD", &oid)) { |
| 1941 | state->cherry_pick_in_progress = 1; |
| 1942 | oidcpy(&state->cherry_pick_head_oid, &oid); |
| 1943 | } |
| 1944 | wt_status_check_bisect(wt, state); |
| 1945 | if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD") && |
| 1946 | !repo_get_oid(r, "REVERT_HEAD", &oid)) { |
| 1947 | state->revert_in_progress = 1; |
| 1948 | oidcpy(&state->revert_head_oid, &oid); |
| 1949 | } |
| 1950 | if (!sequencer_get_last_command(r, &action)) { |
| 1951 | if (action == REPLAY_PICK && !state->cherry_pick_in_progress) { |
| 1952 | state->cherry_pick_in_progress = 1; |
| 1953 | oidcpy(&state->cherry_pick_head_oid, null_oid(r->hash_algo)); |
| 1954 | } else if (action == REPLAY_REVERT && !state->revert_in_progress) { |
| 1955 | state->revert_in_progress = 1; |
| 1956 | oidcpy(&state->revert_head_oid, null_oid(r->hash_algo)); |
| 1957 | } |
| 1958 | } |
| 1959 | if (get_detached_from) |
| 1960 | wt_status_get_detached_from(r, state); |
| 1961 | wt_status_check_sparse_checkout(r, state); |
| 1962 | |
| 1963 | free_worktree(wt); |
| 1964 | } |
| 1965 | |
| 1966 | static void wt_longstatus_print_state(struct wt_status *s) |
| 1967 | { |
| 1968 | const char *state_color = color(WT_STATUS_HEADER, s); |
| 1969 | struct wt_status_state *state = &s->state; |
| 1970 | |
| 1971 | if (state->merge_in_progress) { |
| 1972 | if (state->rebase_interactive_in_progress) { |
| 1973 | show_rebase_information(s, state_color); |
| 1974 | fputs("\n", s->fp); |
| 1975 | } |
| 1976 | show_merge_in_progress(s, state_color); |
| 1977 | } else if (state->am_in_progress) |
| 1978 | show_am_in_progress(s, state_color); |
| 1979 | else if (state->rebase_in_progress || state->rebase_interactive_in_progress) |
| 1980 | show_rebase_in_progress(s, state_color); |
| 1981 | else if (state->cherry_pick_in_progress) |
| 1982 | show_cherry_pick_in_progress(s, state_color); |
| 1983 | else if (state->revert_in_progress) |
| 1984 | show_revert_in_progress(s, state_color); |
| 1985 | if (state->bisect_in_progress) |
| 1986 | show_bisect_in_progress(s, state_color); |
| 1987 | |
| 1988 | if (state->sparse_checkout_percentage != SPARSE_CHECKOUT_DISABLED) |
| 1989 | show_sparse_checkout_in_use(s, state_color); |
| 1990 | } |
| 1991 | |
| 1992 | static void wt_longstatus_print(struct wt_status *s) |
| 1993 | { |
| 1994 | const char *branch_color = color(WT_STATUS_ONBRANCH, s); |
| 1995 | const char *branch_status_color = color(WT_STATUS_HEADER, s); |
| 1996 | enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(s->repo); |
| 1997 | |
| 1998 | if (s->branch) { |
| 1999 | const char *on_what = _("On branch "); |
| 2000 | const char *branch_name = s->branch; |
| 2001 | if (!strcmp(branch_name, "HEAD")) { |
| 2002 | branch_status_color = color(WT_STATUS_NOBRANCH, s); |
| 2003 | if (s->state.rebase_in_progress || |
| 2004 | s->state.rebase_interactive_in_progress) { |
| 2005 | if (s->state.rebase_interactive_in_progress) |
| 2006 | on_what = _("interactive rebase in progress; onto "); |
| 2007 | else |
| 2008 | on_what = _("rebase in progress; onto "); |
| 2009 | branch_name = s->state.onto; |
| 2010 | } else if (s->state.detached_from) { |
| 2011 | branch_name = s->state.detached_from; |
| 2012 | if (s->state.detached_at) |
| 2013 | on_what = _("HEAD detached at "); |
| 2014 | else |
| 2015 | on_what = _("HEAD detached from "); |
| 2016 | } else { |
| 2017 | branch_name = ""; |
| 2018 | on_what = _("Not currently on any branch."); |
| 2019 | } |
| 2020 | } else |
| 2021 | skip_prefix(branch_name, "refs/heads/", &branch_name); |
| 2022 | status_printf(s, color(WT_STATUS_HEADER, s), "%s", ""); |
| 2023 | status_printf_more(s, branch_status_color, "%s", on_what); |
| 2024 | status_printf_more(s, branch_color, "%s\n", branch_name); |
| 2025 | if (!s->is_initial) |
| 2026 | wt_longstatus_print_tracking(s); |
| 2027 | } |
| 2028 | |
| 2029 | wt_longstatus_print_state(s); |
| 2030 | |
| 2031 | if (s->is_initial) { |
| 2032 | status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", ""); |
| 2033 | status_printf_ln(s, color(WT_STATUS_HEADER, s), |
| 2034 | s->commit_template |
| 2035 | ? _("Initial commit") |
| 2036 | : _("No commits yet")); |
| 2037 | status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", ""); |
| 2038 | } |
| 2039 | |
| 2040 | wt_longstatus_print_updated(s); |
| 2041 | wt_longstatus_print_unmerged(s); |
| 2042 | wt_longstatus_print_changed(s); |
| 2043 | if (s->submodule_summary && |
| 2044 | (!s->ignore_submodule_arg || |
| 2045 | strcmp(s->ignore_submodule_arg, "all"))) { |
| 2046 | wt_longstatus_print_submodule_summary(s, 0); /* staged */ |
| 2047 | wt_longstatus_print_submodule_summary(s, 1); /* unstaged */ |
| 2048 | } |
| 2049 | if (s->show_untracked_files) { |
| 2050 | wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add"); |
| 2051 | if (s->show_ignored_mode) |
| 2052 | wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f"); |
| 2053 | if (advice_enabled(ADVICE_STATUS_U_OPTION) && uf_was_slow(s)) { |
| 2054 | status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); |
| 2055 | if (fsm_mode > FSMONITOR_MODE_DISABLED) { |
| 2056 | status_printf_ln(s, GIT_COLOR_NORMAL, |
| 2057 | _("It took %.2f seconds to enumerate untracked files,\n" |
| 2058 | "but the results were cached, and subsequent runs may be faster."), |
| 2059 | s->untracked_in_ms / 1000.0); |
| 2060 | } else { |
| 2061 | status_printf_ln(s, GIT_COLOR_NORMAL, |
| 2062 | _("It took %.2f seconds to enumerate untracked files."), |
| 2063 | s->untracked_in_ms / 1000.0); |
| 2064 | } |
| 2065 | status_printf_ln(s, GIT_COLOR_NORMAL, |
| 2066 | _("See 'git help status' for information on how to improve this.")); |
| 2067 | status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); |
| 2068 | } |
| 2069 | } else if (s->committable) |
| 2070 | status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"), |
| 2071 | s->hints |
| 2072 | ? _(" (use -u option to show untracked files)") : ""); |
| 2073 | |
| 2074 | if (s->verbose) |
| 2075 | wt_longstatus_print_verbose(s); |
| 2076 | if (!s->committable) { |
| 2077 | if (s->amend) |
| 2078 | status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes")); |
| 2079 | else if (s->nowarn) |
| 2080 | ; /* nothing */ |
| 2081 | else if (s->workdir_dirty) { |
| 2082 | if (s->hints) |
| 2083 | fprintf(s->fp, _("no changes added to commit " |
| 2084 | "(use \"git add\" and/or " |
| 2085 | "\"git commit -a\")\n")); |
| 2086 | else |
| 2087 | fprintf(s->fp, _("no changes added to " |
| 2088 | "commit\n")); |
| 2089 | } else if (s->untracked.nr) { |
| 2090 | if (s->hints) |
| 2091 | fprintf(s->fp, _("nothing added to commit but " |
| 2092 | "untracked files present (use " |
| 2093 | "\"git add\" to track)\n")); |
| 2094 | else |
| 2095 | fprintf(s->fp, _("nothing added to commit but " |
| 2096 | "untracked files present\n")); |
| 2097 | } else if (s->is_initial) { |
| 2098 | if (s->hints) |
| 2099 | fprintf(s->fp, _("nothing to commit (create/" |
| 2100 | "copy files and use \"git " |
| 2101 | "add\" to track)\n")); |
| 2102 | else |
| 2103 | fprintf(s->fp, _("nothing to commit\n")); |
| 2104 | } else if (!s->show_untracked_files) { |
| 2105 | if (s->hints) |
| 2106 | fprintf(s->fp, _("nothing to commit (use -u to " |
| 2107 | "show untracked files)\n")); |
| 2108 | else |
| 2109 | fprintf(s->fp, _("nothing to commit\n")); |
| 2110 | } else |
| 2111 | fprintf(s->fp, _("nothing to commit, working tree " |
| 2112 | "clean\n")); |
| 2113 | } |
| 2114 | if(s->show_stash) |
| 2115 | wt_longstatus_print_stash_summary(s); |
| 2116 | } |
| 2117 | |
| 2118 | static void wt_shortstatus_unmerged(struct string_list_item *it, |
| 2119 | struct wt_status *s) |
| 2120 | { |
| 2121 | struct wt_status_change_data *d = it->util; |
| 2122 | const char *how = "??"; |
| 2123 | |
| 2124 | switch (d->stagemask) { |
| 2125 | case 1: how = "DD"; break; /* both deleted */ |
| 2126 | case 2: how = "AU"; break; /* added by us */ |
| 2127 | case 3: how = "UD"; break; /* deleted by them */ |
| 2128 | case 4: how = "UA"; break; /* added by them */ |
| 2129 | case 5: how = "DU"; break; /* deleted by us */ |
| 2130 | case 6: how = "AA"; break; /* both added */ |
| 2131 | case 7: how = "UU"; break; /* both modified */ |
| 2132 | } |
| 2133 | color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how); |
| 2134 | if (s->null_termination) { |
| 2135 | fprintf(s->fp, " %s%c", it->string, 0); |
| 2136 | } else { |
| 2137 | struct strbuf onebuf = STRBUF_INIT; |
| 2138 | const char *one; |
| 2139 | one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP); |
| 2140 | fprintf(s->fp, " %s\n", one); |
| 2141 | strbuf_release(&onebuf); |
| 2142 | } |
| 2143 | } |
| 2144 | |
| 2145 | static void wt_shortstatus_status(struct string_list_item *it, |
| 2146 | struct wt_status *s) |
| 2147 | { |
| 2148 | struct wt_status_change_data *d = it->util; |
| 2149 | |
| 2150 | if (d->index_status) |
| 2151 | color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status); |
| 2152 | else |
| 2153 | fputc(' ', s->fp); |
| 2154 | if (d->worktree_status) |
| 2155 | color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status); |
| 2156 | else |
| 2157 | fputc(' ', s->fp); |
| 2158 | fputc(' ', s->fp); |
| 2159 | if (s->null_termination) { |
| 2160 | fprintf(s->fp, "%s%c", it->string, 0); |
| 2161 | if (d->rename_source) |
| 2162 | fprintf(s->fp, "%s%c", d->rename_source, 0); |
| 2163 | } else { |
| 2164 | struct strbuf onebuf = STRBUF_INIT; |
| 2165 | const char *one; |
| 2166 | |
| 2167 | if (d->rename_source) { |
| 2168 | one = quote_path(d->rename_source, s->prefix, &onebuf, |
| 2169 | QUOTE_PATH_QUOTE_SP); |
| 2170 | fprintf(s->fp, "%s -> ", one); |
| 2171 | strbuf_release(&onebuf); |
| 2172 | } |
| 2173 | one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP); |
| 2174 | fprintf(s->fp, "%s\n", one); |
| 2175 | strbuf_release(&onebuf); |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | static void wt_shortstatus_other(struct string_list_item *it, |
| 2180 | struct wt_status *s, const char *sign) |
| 2181 | { |
| 2182 | color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign); |
| 2183 | if (s->null_termination) { |
| 2184 | fprintf(s->fp, " %s%c", it->string, 0); |
| 2185 | } else { |
| 2186 | struct strbuf onebuf = STRBUF_INIT; |
| 2187 | const char *one; |
| 2188 | one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP); |
| 2189 | fprintf(s->fp, " %s\n", one); |
| 2190 | strbuf_release(&onebuf); |
| 2191 | } |
| 2192 | } |
| 2193 | |
| 2194 | static void wt_shortstatus_print_tracking(struct wt_status *s) |
| 2195 | { |
| 2196 | struct branch *branch; |
| 2197 | const char *header_color = color(WT_STATUS_HEADER, s); |
| 2198 | const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s); |
| 2199 | const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s); |
| 2200 | |
| 2201 | const char *base; |
| 2202 | char *short_base; |
| 2203 | const char *branch_name; |
| 2204 | int num_ours, num_theirs, sti; |
| 2205 | int upstream_is_gone = 0; |
| 2206 | |
| 2207 | color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## "); |
| 2208 | |
| 2209 | if (!s->branch) |
| 2210 | return; |
| 2211 | branch_name = s->branch; |
| 2212 | |
| 2213 | #define LABEL(string) (s->no_gettext ? (string) : _(string)) |
| 2214 | |
| 2215 | if (s->is_initial) |
| 2216 | color_fprintf(s->fp, header_color, LABEL(N_("No commits yet on "))); |
| 2217 | |
| 2218 | if (!strcmp(s->branch, "HEAD")) { |
| 2219 | color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s", |
| 2220 | LABEL(N_("HEAD (no branch)"))); |
| 2221 | goto conclude; |
| 2222 | } |
| 2223 | |
| 2224 | skip_prefix(branch_name, "refs/heads/", &branch_name); |
| 2225 | |
| 2226 | branch = branch_get(branch_name); |
| 2227 | |
| 2228 | color_fprintf(s->fp, branch_color_local, "%s", branch_name); |
| 2229 | |
| 2230 | sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base, |
| 2231 | 0, s->ahead_behind_flags); |
| 2232 | if (sti < 0) { |
| 2233 | if (!base) |
| 2234 | goto conclude; |
| 2235 | |
| 2236 | upstream_is_gone = 1; |
| 2237 | } |
| 2238 | |
| 2239 | short_base = refs_shorten_unambiguous_ref(get_main_ref_store(s->repo), |
| 2240 | base, 0); |
| 2241 | color_fprintf(s->fp, header_color, "..."); |
| 2242 | color_fprintf(s->fp, branch_color_remote, "%s", short_base); |
| 2243 | free(short_base); |
| 2244 | |
| 2245 | if (!upstream_is_gone && !sti) |
| 2246 | goto conclude; |
| 2247 | |
| 2248 | color_fprintf(s->fp, header_color, " ["); |
| 2249 | if (upstream_is_gone) { |
| 2250 | color_fprintf(s->fp, header_color, LABEL(N_("gone"))); |
| 2251 | } else if (s->ahead_behind_flags == AHEAD_BEHIND_QUICK) { |
| 2252 | color_fprintf(s->fp, header_color, LABEL(N_("different"))); |
| 2253 | } else if (!num_ours) { |
| 2254 | color_fprintf(s->fp, header_color, LABEL(N_("behind "))); |
| 2255 | color_fprintf(s->fp, branch_color_remote, "%d", num_theirs); |
| 2256 | } else if (!num_theirs) { |
| 2257 | color_fprintf(s->fp, header_color, LABEL(N_("ahead "))); |
| 2258 | color_fprintf(s->fp, branch_color_local, "%d", num_ours); |
| 2259 | } else { |
| 2260 | color_fprintf(s->fp, header_color, LABEL(N_("ahead "))); |
| 2261 | color_fprintf(s->fp, branch_color_local, "%d", num_ours); |
| 2262 | color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind "))); |
| 2263 | color_fprintf(s->fp, branch_color_remote, "%d", num_theirs); |
| 2264 | } |
| 2265 | |
| 2266 | color_fprintf(s->fp, header_color, "]"); |
| 2267 | conclude: |
| 2268 | fputc(s->null_termination ? '\0' : '\n', s->fp); |
| 2269 | } |
| 2270 | |
| 2271 | static void wt_shortstatus_print(struct wt_status *s) |
| 2272 | { |
| 2273 | struct string_list_item *it; |
| 2274 | |
| 2275 | if (s->show_branch) |
| 2276 | wt_shortstatus_print_tracking(s); |
| 2277 | |
| 2278 | for_each_string_list_item(it, &s->change) { |
| 2279 | struct wt_status_change_data *d = it->util; |
| 2280 | |
| 2281 | if (d->stagemask) |
| 2282 | wt_shortstatus_unmerged(it, s); |
| 2283 | else |
| 2284 | wt_shortstatus_status(it, s); |
| 2285 | } |
| 2286 | for_each_string_list_item(it, &s->untracked) |
| 2287 | wt_shortstatus_other(it, s, "??"); |
| 2288 | |
| 2289 | for_each_string_list_item(it, &s->ignored) |
| 2290 | wt_shortstatus_other(it, s, "!!"); |
| 2291 | } |
| 2292 | |
| 2293 | static void wt_porcelain_print(struct wt_status *s) |
| 2294 | { |
| 2295 | s->use_color = GIT_COLOR_NEVER; |
| 2296 | s->relative_paths = 0; |
| 2297 | s->prefix = NULL; |
| 2298 | s->no_gettext = 1; |
| 2299 | wt_shortstatus_print(s); |
| 2300 | } |
| 2301 | |
| 2302 | /* |
| 2303 | * Print branch information for porcelain v2 output. These lines |
| 2304 | * are printed when the '--branch' parameter is given. |
| 2305 | * |
| 2306 | * # branch.oid <commit><eol> |
| 2307 | * # branch.head <head><eol> |
| 2308 | * [# branch.upstream <upstream><eol> |
| 2309 | * [# branch.ab +<ahead> -<behind><eol>]] |
| 2310 | * |
| 2311 | * <commit> ::= the current commit hash or the literal |
| 2312 | * "(initial)" to indicate an initialized repo |
| 2313 | * with no commits. |
| 2314 | * |
| 2315 | * <head> ::= <branch_name> the current branch name or |
| 2316 | * "(detached)" literal when detached head or |
| 2317 | * "(unknown)" when something is wrong. |
| 2318 | * |
| 2319 | * <upstream> ::= the upstream branch name, when set. |
| 2320 | * |
| 2321 | * <ahead> ::= integer ahead value or '?'. |
| 2322 | * |
| 2323 | * <behind> ::= integer behind value or '?'. |
| 2324 | * |
| 2325 | * The end-of-line is defined by the -z flag. |
| 2326 | * |
| 2327 | * <eol> ::= NUL when -z, |
| 2328 | * LF when NOT -z. |
| 2329 | * |
| 2330 | * When an upstream is set and present, the 'branch.ab' line will |
| 2331 | * be printed with the ahead/behind counts for the branch and the |
| 2332 | * upstream. When AHEAD_BEHIND_QUICK is requested and the branches |
| 2333 | * are different, '?' will be substituted for the actual count. |
| 2334 | */ |
| 2335 | static void wt_porcelain_v2_print_tracking(struct wt_status *s) |
| 2336 | { |
| 2337 | struct branch *branch; |
| 2338 | const char *base; |
| 2339 | const char *branch_name; |
| 2340 | int ab_info, nr_ahead, nr_behind; |
| 2341 | char eol = s->null_termination ? '\0' : '\n'; |
| 2342 | |
| 2343 | fprintf(s->fp, "# branch.oid %s%c", |
| 2344 | (s->is_initial ? "(initial)" : oid_to_hex(&s->oid_commit)), |
| 2345 | eol); |
| 2346 | |
| 2347 | if (!s->branch) |
| 2348 | fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol); |
| 2349 | else { |
| 2350 | if (!strcmp(s->branch, "HEAD")) { |
| 2351 | fprintf(s->fp, "# branch.head %s%c", "(detached)", eol); |
| 2352 | |
| 2353 | if (s->state.rebase_in_progress || |
| 2354 | s->state.rebase_interactive_in_progress) |
| 2355 | branch_name = s->state.onto; |
| 2356 | else if (s->state.detached_from) |
| 2357 | branch_name = s->state.detached_from; |
| 2358 | else |
| 2359 | branch_name = ""; |
| 2360 | } else { |
| 2361 | branch_name = NULL; |
| 2362 | skip_prefix(s->branch, "refs/heads/", &branch_name); |
| 2363 | |
| 2364 | fprintf(s->fp, "# branch.head %s%c", branch_name, eol); |
| 2365 | } |
| 2366 | |
| 2367 | /* Lookup stats on the upstream tracking branch, if set. */ |
| 2368 | branch = branch_get(branch_name); |
| 2369 | base = NULL; |
| 2370 | ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind, |
| 2371 | &base, 0, s->ahead_behind_flags); |
| 2372 | if (base) { |
| 2373 | base = refs_shorten_unambiguous_ref(get_main_ref_store(s->repo), |
| 2374 | base, 0); |
| 2375 | fprintf(s->fp, "# branch.upstream %s%c", base, eol); |
| 2376 | free((char *)base); |
| 2377 | |
| 2378 | if (ab_info > 0) { |
| 2379 | /* different */ |
| 2380 | if (nr_ahead || nr_behind) |
| 2381 | fprintf(s->fp, "# branch.ab +%d -%d%c", |
| 2382 | nr_ahead, nr_behind, eol); |
| 2383 | else |
| 2384 | fprintf(s->fp, "# branch.ab +? -?%c", |
| 2385 | eol); |
| 2386 | } else if (!ab_info) { |
| 2387 | /* same */ |
| 2388 | fprintf(s->fp, "# branch.ab +0 -0%c", eol); |
| 2389 | } |
| 2390 | } |
| 2391 | } |
| 2392 | } |
| 2393 | |
| 2394 | /* |
| 2395 | * Print the stash count in a porcelain-friendly format |
| 2396 | */ |
| 2397 | static void wt_porcelain_v2_print_stash(struct wt_status *s) |
| 2398 | { |
| 2399 | int stash_count = count_stash_entries(s->repo); |
| 2400 | char eol = s->null_termination ? '\0' : '\n'; |
| 2401 | |
| 2402 | if (stash_count > 0) |
| 2403 | fprintf(s->fp, "# stash %d%c", stash_count, eol); |
| 2404 | } |
| 2405 | |
| 2406 | /* |
| 2407 | * Convert various submodule status values into a |
| 2408 | * fixed-length string of characters in the buffer provided. |
| 2409 | */ |
| 2410 | static void wt_porcelain_v2_submodule_state( |
| 2411 | struct wt_status_change_data *d, |
| 2412 | char sub[5]) |
| 2413 | { |
| 2414 | if (S_ISGITLINK(d->mode_head) || |
| 2415 | S_ISGITLINK(d->mode_index) || |
| 2416 | S_ISGITLINK(d->mode_worktree)) { |
| 2417 | sub[0] = 'S'; |
| 2418 | sub[1] = d->new_submodule_commits ? 'C' : '.'; |
| 2419 | sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.'; |
| 2420 | sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.'; |
| 2421 | } else { |
| 2422 | sub[0] = 'N'; |
| 2423 | sub[1] = '.'; |
| 2424 | sub[2] = '.'; |
| 2425 | sub[3] = '.'; |
| 2426 | } |
| 2427 | sub[4] = 0; |
| 2428 | } |
| 2429 | |
| 2430 | /* |
| 2431 | * Fix-up changed entries before we print them. |
| 2432 | */ |
| 2433 | static void wt_porcelain_v2_fix_up_changed(struct string_list_item *it) |
| 2434 | { |
| 2435 | struct wt_status_change_data *d = it->util; |
| 2436 | |
| 2437 | if (!d->index_status) { |
| 2438 | /* |
| 2439 | * This entry is unchanged in the index (relative to the head). |
| 2440 | * Therefore, the collect_updated_cb was never called for this |
| 2441 | * entry (during the head-vs-index scan) and so the head column |
| 2442 | * fields were never set. |
| 2443 | * |
| 2444 | * We must have data for the index column (from the |
| 2445 | * index-vs-worktree scan (otherwise, this entry should not be |
| 2446 | * in the list of changes)). |
| 2447 | * |
| 2448 | * Copy index column fields to the head column, so that our |
| 2449 | * output looks complete. |
| 2450 | */ |
| 2451 | assert(d->mode_head == 0); |
| 2452 | d->mode_head = d->mode_index; |
| 2453 | oidcpy(&d->oid_head, &d->oid_index); |
| 2454 | } |
| 2455 | |
| 2456 | if (!d->worktree_status) { |
| 2457 | /* |
| 2458 | * This entry is unchanged in the worktree (relative to the index). |
| 2459 | * Therefore, the collect_changed_cb was never called for this entry |
| 2460 | * (during the index-vs-worktree scan) and so the worktree column |
| 2461 | * fields were never set. |
| 2462 | * |
| 2463 | * We must have data for the index column (from the head-vs-index |
| 2464 | * scan). |
| 2465 | * |
| 2466 | * Copy the index column fields to the worktree column so that |
| 2467 | * our output looks complete. |
| 2468 | * |
| 2469 | * Note that we only have a mode field in the worktree column |
| 2470 | * because the scan code tries really hard to not have to compute it. |
| 2471 | */ |
| 2472 | assert(d->mode_worktree == 0); |
| 2473 | d->mode_worktree = d->mode_index; |
| 2474 | } |
| 2475 | } |
| 2476 | |
| 2477 | /* |
| 2478 | * Print porcelain v2 info for tracked entries with changes. |
| 2479 | */ |
| 2480 | static void wt_porcelain_v2_print_changed_entry( |
| 2481 | struct string_list_item *it, |
| 2482 | struct wt_status *s) |
| 2483 | { |
| 2484 | struct wt_status_change_data *d = it->util; |
| 2485 | struct strbuf buf = STRBUF_INIT; |
| 2486 | struct strbuf buf_from = STRBUF_INIT; |
| 2487 | const char *path = NULL; |
| 2488 | const char *path_from = NULL; |
| 2489 | char key[3]; |
| 2490 | char submodule_token[5]; |
| 2491 | char sep_char, eol_char; |
| 2492 | |
| 2493 | wt_porcelain_v2_fix_up_changed(it); |
| 2494 | wt_porcelain_v2_submodule_state(d, submodule_token); |
| 2495 | |
| 2496 | key[0] = d->index_status ? d->index_status : '.'; |
| 2497 | key[1] = d->worktree_status ? d->worktree_status : '.'; |
| 2498 | key[2] = 0; |
| 2499 | |
| 2500 | if (s->null_termination) { |
| 2501 | /* |
| 2502 | * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first. |
| 2503 | * A single NUL character separates them. |
| 2504 | */ |
| 2505 | sep_char = '\0'; |
| 2506 | eol_char = '\0'; |
| 2507 | path = it->string; |
| 2508 | path_from = d->rename_source; |
| 2509 | } else { |
| 2510 | /* |
| 2511 | * Path(s) are C-quoted if necessary. Current path is ALWAYS first. |
| 2512 | * The source path is only present when necessary. |
| 2513 | * A single TAB separates them (because paths can contain spaces |
| 2514 | * which are not escaped and C-quoting does escape TAB characters). |
| 2515 | */ |
| 2516 | sep_char = '\t'; |
| 2517 | eol_char = '\n'; |
| 2518 | path = quote_path(it->string, s->prefix, &buf, 0); |
| 2519 | if (d->rename_source) |
| 2520 | path_from = quote_path(d->rename_source, s->prefix, &buf_from, 0); |
| 2521 | } |
| 2522 | |
| 2523 | if (path_from) |
| 2524 | fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c", |
| 2525 | key, submodule_token, |
| 2526 | d->mode_head, d->mode_index, d->mode_worktree, |
| 2527 | oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index), |
| 2528 | d->rename_status, d->rename_score, |
| 2529 | path, sep_char, path_from, eol_char); |
| 2530 | else |
| 2531 | fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c", |
| 2532 | key, submodule_token, |
| 2533 | d->mode_head, d->mode_index, d->mode_worktree, |
| 2534 | oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index), |
| 2535 | path, eol_char); |
| 2536 | |
| 2537 | strbuf_release(&buf); |
| 2538 | strbuf_release(&buf_from); |
| 2539 | } |
| 2540 | |
| 2541 | /* |
| 2542 | * Print porcelain v2 status info for unmerged entries. |
| 2543 | */ |
| 2544 | static void wt_porcelain_v2_print_unmerged_entry( |
| 2545 | struct string_list_item *it, |
| 2546 | struct wt_status *s) |
| 2547 | { |
| 2548 | struct wt_status_change_data *d = it->util; |
| 2549 | struct index_state *istate = s->repo->index; |
| 2550 | const struct cache_entry *ce; |
| 2551 | struct strbuf buf_index = STRBUF_INIT; |
| 2552 | const char *path_index = NULL; |
| 2553 | int pos, stage, sum; |
| 2554 | struct { |
| 2555 | int mode; |
| 2556 | struct object_id oid; |
| 2557 | } stages[3]; |
| 2558 | const char *key; |
| 2559 | char submodule_token[5]; |
| 2560 | char unmerged_prefix = 'u'; |
| 2561 | char eol_char = s->null_termination ? '\0' : '\n'; |
| 2562 | |
| 2563 | wt_porcelain_v2_submodule_state(d, submodule_token); |
| 2564 | |
| 2565 | switch (d->stagemask) { |
| 2566 | case 1: key = "DD"; break; /* both deleted */ |
| 2567 | case 2: key = "AU"; break; /* added by us */ |
| 2568 | case 3: key = "UD"; break; /* deleted by them */ |
| 2569 | case 4: key = "UA"; break; /* added by them */ |
| 2570 | case 5: key = "DU"; break; /* deleted by us */ |
| 2571 | case 6: key = "AA"; break; /* both added */ |
| 2572 | case 7: key = "UU"; break; /* both modified */ |
| 2573 | default: |
| 2574 | BUG("unhandled unmerged status %x", d->stagemask); |
| 2575 | } |
| 2576 | |
| 2577 | /* |
| 2578 | * Disregard d.aux.porcelain_v2 data that we accumulated |
| 2579 | * for the head and index columns during the scans and |
| 2580 | * replace with the actual stage data. |
| 2581 | * |
| 2582 | * Note that this is a last-one-wins for each the individual |
| 2583 | * stage [123] columns in the event of multiple cache entries |
| 2584 | * for same stage. |
| 2585 | */ |
| 2586 | memset(stages, 0, sizeof(stages)); |
| 2587 | sum = 0; |
| 2588 | pos = index_name_pos(istate, it->string, strlen(it->string)); |
| 2589 | assert(pos < 0); |
| 2590 | pos = -pos-1; |
| 2591 | while (pos < istate->cache_nr) { |
| 2592 | ce = istate->cache[pos++]; |
| 2593 | stage = ce_stage(ce); |
| 2594 | if (strcmp(ce->name, it->string) || !stage) |
| 2595 | break; |
| 2596 | stages[stage - 1].mode = ce->ce_mode; |
| 2597 | oidcpy(&stages[stage - 1].oid, &ce->oid); |
| 2598 | sum |= (1 << (stage - 1)); |
| 2599 | } |
| 2600 | if (sum != d->stagemask) |
| 2601 | BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask); |
| 2602 | |
| 2603 | if (s->null_termination) |
| 2604 | path_index = it->string; |
| 2605 | else |
| 2606 | path_index = quote_path(it->string, s->prefix, &buf_index, 0); |
| 2607 | |
| 2608 | fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c", |
| 2609 | unmerged_prefix, key, submodule_token, |
| 2610 | stages[0].mode, /* stage 1 */ |
| 2611 | stages[1].mode, /* stage 2 */ |
| 2612 | stages[2].mode, /* stage 3 */ |
| 2613 | d->mode_worktree, |
| 2614 | oid_to_hex(&stages[0].oid), /* stage 1 */ |
| 2615 | oid_to_hex(&stages[1].oid), /* stage 2 */ |
| 2616 | oid_to_hex(&stages[2].oid), /* stage 3 */ |
| 2617 | path_index, |
| 2618 | eol_char); |
| 2619 | |
| 2620 | strbuf_release(&buf_index); |
| 2621 | } |
| 2622 | |
| 2623 | /* |
| 2624 | * Print porcelain V2 status info for untracked and ignored entries. |
| 2625 | */ |
| 2626 | static void wt_porcelain_v2_print_other( |
| 2627 | struct string_list_item *it, |
| 2628 | struct wt_status *s, |
| 2629 | char prefix) |
| 2630 | { |
| 2631 | struct strbuf buf = STRBUF_INIT; |
| 2632 | const char *path; |
| 2633 | char eol_char; |
| 2634 | |
| 2635 | if (s->null_termination) { |
| 2636 | path = it->string; |
| 2637 | eol_char = '\0'; |
| 2638 | } else { |
| 2639 | path = quote_path(it->string, s->prefix, &buf, 0); |
| 2640 | eol_char = '\n'; |
| 2641 | } |
| 2642 | |
| 2643 | fprintf(s->fp, "%c %s%c", prefix, path, eol_char); |
| 2644 | |
| 2645 | strbuf_release(&buf); |
| 2646 | } |
| 2647 | |
| 2648 | /* |
| 2649 | * Print porcelain V2 status. |
| 2650 | * |
| 2651 | * [<v2_branch>] |
| 2652 | * [<v2_changed_items>]* |
| 2653 | * [<v2_unmerged_items>]* |
| 2654 | * [<v2_untracked_items>]* |
| 2655 | * [<v2_ignored_items>]* |
| 2656 | * |
| 2657 | */ |
| 2658 | static void wt_porcelain_v2_print(struct wt_status *s) |
| 2659 | { |
| 2660 | struct wt_status_change_data *d; |
| 2661 | struct string_list_item *it; |
| 2662 | int i; |
| 2663 | |
| 2664 | if (s->show_branch) |
| 2665 | wt_porcelain_v2_print_tracking(s); |
| 2666 | |
| 2667 | if (s->show_stash) |
| 2668 | wt_porcelain_v2_print_stash(s); |
| 2669 | |
| 2670 | for (i = 0; i < s->change.nr; i++) { |
| 2671 | it = &(s->change.items[i]); |
| 2672 | d = it->util; |
| 2673 | if (!d->stagemask) |
| 2674 | wt_porcelain_v2_print_changed_entry(it, s); |
| 2675 | } |
| 2676 | |
| 2677 | for (i = 0; i < s->change.nr; i++) { |
| 2678 | it = &(s->change.items[i]); |
| 2679 | d = it->util; |
| 2680 | if (d->stagemask) |
| 2681 | wt_porcelain_v2_print_unmerged_entry(it, s); |
| 2682 | } |
| 2683 | |
| 2684 | for (i = 0; i < s->untracked.nr; i++) { |
| 2685 | it = &(s->untracked.items[i]); |
| 2686 | wt_porcelain_v2_print_other(it, s, '?'); |
| 2687 | } |
| 2688 | |
| 2689 | for (i = 0; i < s->ignored.nr; i++) { |
| 2690 | it = &(s->ignored.items[i]); |
| 2691 | wt_porcelain_v2_print_other(it, s, '!'); |
| 2692 | } |
| 2693 | } |
| 2694 | |
| 2695 | void wt_status_print(struct wt_status *s) |
| 2696 | { |
| 2697 | trace2_data_intmax("status", s->repo, "count/changed", s->change.nr); |
| 2698 | trace2_data_intmax("status", s->repo, "count/untracked", |
| 2699 | s->untracked.nr); |
| 2700 | trace2_data_intmax("status", s->repo, "count/ignored", s->ignored.nr); |
| 2701 | |
| 2702 | trace2_region_enter("status", "print", s->repo); |
| 2703 | |
| 2704 | switch (s->status_format) { |
| 2705 | case STATUS_FORMAT_SHORT: |
| 2706 | wt_shortstatus_print(s); |
| 2707 | break; |
| 2708 | case STATUS_FORMAT_PORCELAIN: |
| 2709 | wt_porcelain_print(s); |
| 2710 | break; |
| 2711 | case STATUS_FORMAT_PORCELAIN_V2: |
| 2712 | wt_porcelain_v2_print(s); |
| 2713 | break; |
| 2714 | case STATUS_FORMAT_UNSPECIFIED: |
| 2715 | BUG("finalize_deferred_config() should have been called"); |
| 2716 | break; |
| 2717 | case STATUS_FORMAT_NONE: |
| 2718 | case STATUS_FORMAT_LONG: |
| 2719 | wt_longstatus_print(s); |
| 2720 | break; |
| 2721 | } |
| 2722 | |
| 2723 | trace2_region_leave("status", "print", s->repo); |
| 2724 | } |
| 2725 | |
| 2726 | /** |
| 2727 | * Returns 1 if there are unstaged changes, 0 otherwise. |
| 2728 | */ |
| 2729 | int has_unstaged_changes(struct repository *r, int ignore_submodules) |
| 2730 | { |
| 2731 | struct rev_info rev_info; |
| 2732 | int result; |
| 2733 | |
| 2734 | repo_init_revisions(r, &rev_info, NULL); |
| 2735 | if (ignore_submodules) { |
| 2736 | rev_info.diffopt.flags.ignore_submodules = 1; |
| 2737 | rev_info.diffopt.flags.override_submodule_config = 1; |
| 2738 | } |
| 2739 | rev_info.diffopt.flags.quick = 1; |
| 2740 | diff_setup_done(&rev_info.diffopt); |
| 2741 | run_diff_files(&rev_info, 0); |
| 2742 | result = diff_result_code(&rev_info); |
| 2743 | release_revisions(&rev_info); |
| 2744 | return result; |
| 2745 | } |
| 2746 | |
| 2747 | /** |
| 2748 | * Returns 1 if there are uncommitted changes, 0 otherwise. |
| 2749 | */ |
| 2750 | int has_uncommitted_changes(struct repository *r, |
| 2751 | int ignore_submodules) |
| 2752 | { |
| 2753 | struct rev_info rev_info; |
| 2754 | int result; |
| 2755 | |
| 2756 | if (is_index_unborn(r->index)) |
| 2757 | return 0; |
| 2758 | |
| 2759 | repo_init_revisions(r, &rev_info, NULL); |
| 2760 | if (ignore_submodules) |
| 2761 | rev_info.diffopt.flags.ignore_submodules = 1; |
| 2762 | rev_info.diffopt.flags.quick = 1; |
| 2763 | |
| 2764 | add_head_to_pending(&rev_info); |
| 2765 | if (!rev_info.pending.nr) { |
| 2766 | /* |
| 2767 | * We have no head (or it's corrupt); use the empty tree, |
| 2768 | * which will complain if the index is non-empty. |
| 2769 | */ |
| 2770 | struct tree *tree = lookup_tree(r, r->hash_algo->empty_tree); |
| 2771 | add_pending_object(&rev_info, &tree->object, ""); |
| 2772 | } |
| 2773 | |
| 2774 | diff_setup_done(&rev_info.diffopt); |
| 2775 | run_diff_index(&rev_info, DIFF_INDEX_CACHED); |
| 2776 | result = diff_result_code(&rev_info); |
| 2777 | release_revisions(&rev_info); |
| 2778 | return result; |
| 2779 | } |
| 2780 | |
| 2781 | /** |
| 2782 | * If the work tree has unstaged or uncommitted changes, dies with the |
| 2783 | * appropriate message. |
| 2784 | */ |
| 2785 | int require_clean_work_tree(struct repository *r, |
| 2786 | const char *action, |
| 2787 | const char *hint, |
| 2788 | int ignore_submodules, |
| 2789 | int gently) |
| 2790 | { |
| 2791 | struct lock_file lock_file = LOCK_INIT; |
| 2792 | int err = 0, fd; |
| 2793 | |
| 2794 | fd = repo_hold_locked_index(r, &lock_file, 0); |
| 2795 | refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL); |
| 2796 | if (0 <= fd) |
| 2797 | repo_update_index_if_able(r, &lock_file); |
| 2798 | rollback_lock_file(&lock_file); |
| 2799 | |
| 2800 | if (has_unstaged_changes(r, ignore_submodules)) { |
| 2801 | /* TRANSLATORS: the action is e.g. "pull with rebase" */ |
| 2802 | error(_("cannot %s: You have unstaged changes."), _(action)); |
| 2803 | err = 1; |
| 2804 | } |
| 2805 | |
| 2806 | if (has_uncommitted_changes(r, ignore_submodules)) { |
| 2807 | if (err) |
| 2808 | error(_("additionally, your index contains uncommitted changes.")); |
| 2809 | else |
| 2810 | error(_("cannot %s: Your index contains uncommitted changes."), |
| 2811 | _(action)); |
| 2812 | err = 1; |
| 2813 | } |
| 2814 | |
| 2815 | if (err) { |
| 2816 | if (hint) { |
| 2817 | if (!*hint) |
| 2818 | BUG("empty hint passed to require_clean_work_tree();" |
| 2819 | " use NULL instead"); |
| 2820 | error("%s", hint); |
| 2821 | } |
| 2822 | if (!gently) |
| 2823 | exit(128); |
| 2824 | } |
| 2825 | |
| 2826 | return err; |
| 2827 | } |