wt-status.c 70 KB

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