submodule.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292
  1. #include "cache.h"
  2. #include "repository.h"
  3. #include "config.h"
  4. #include "submodule-config.h"
  5. #include "submodule.h"
  6. #include "dir.h"
  7. #include "diff.h"
  8. #include "commit.h"
  9. #include "revision.h"
  10. #include "run-command.h"
  11. #include "diffcore.h"
  12. #include "refs.h"
  13. #include "string-list.h"
  14. #include "oid-array.h"
  15. #include "strvec.h"
  16. #include "blob.h"
  17. #include "thread-utils.h"
  18. #include "quote.h"
  19. #include "remote.h"
  20. #include "worktree.h"
  21. #include "parse-options.h"
  22. #include "object-store.h"
  23. #include "commit-reach.h"
  24. static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
  25. static int initialized_fetch_ref_tips;
  26. static struct oid_array ref_tips_before_fetch;
  27. static struct oid_array ref_tips_after_fetch;
  28. /*
  29. * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file
  30. * will be disabled because we can't guess what might be configured in
  31. * .gitmodules unless the user resolves the conflict.
  32. */
  33. int is_gitmodules_unmerged(const struct index_state *istate)
  34. {
  35. int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
  36. if (pos < 0) { /* .gitmodules not found or isn't merged */
  37. pos = -1 - pos;
  38. if (istate->cache_nr > pos) { /* there is a .gitmodules */
  39. const struct cache_entry *ce = istate->cache[pos];
  40. if (ce_namelen(ce) == strlen(GITMODULES_FILE) &&
  41. !strcmp(ce->name, GITMODULES_FILE))
  42. return 1;
  43. }
  44. }
  45. return 0;
  46. }
  47. /*
  48. * Check if the .gitmodules file is safe to write.
  49. *
  50. * Writing to the .gitmodules file requires that the file exists in the
  51. * working tree or, if it doesn't, that a brand new .gitmodules file is going
  52. * to be created (i.e. it's neither in the index nor in the current branch).
  53. *
  54. * It is not safe to write to .gitmodules if it's not in the working tree but
  55. * it is in the index or in the current branch, because writing new values
  56. * (and staging them) would blindly overwrite ALL the old content.
  57. */
  58. int is_writing_gitmodules_ok(void)
  59. {
  60. struct object_id oid;
  61. return file_exists(GITMODULES_FILE) ||
  62. (get_oid(GITMODULES_INDEX, &oid) < 0 && get_oid(GITMODULES_HEAD, &oid) < 0);
  63. }
  64. /*
  65. * Check if the .gitmodules file has unstaged modifications. This must be
  66. * checked before allowing modifications to the .gitmodules file with the
  67. * intention to stage them later, because when continuing we would stage the
  68. * modifications the user didn't stage herself too. That might change in a
  69. * future version when we learn to stage the changes we do ourselves without
  70. * staging any previous modifications.
  71. */
  72. int is_staging_gitmodules_ok(struct index_state *istate)
  73. {
  74. int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
  75. if ((pos >= 0) && (pos < istate->cache_nr)) {
  76. struct stat st;
  77. if (lstat(GITMODULES_FILE, &st) == 0 &&
  78. ie_modified(istate, istate->cache[pos], &st, 0) & DATA_CHANGED)
  79. return 0;
  80. }
  81. return 1;
  82. }
  83. static int for_each_remote_ref_submodule(const char *submodule,
  84. each_ref_fn fn, void *cb_data)
  85. {
  86. return refs_for_each_remote_ref(get_submodule_ref_store(submodule),
  87. fn, cb_data);
  88. }
  89. /*
  90. * Try to update the "path" entry in the "submodule.<name>" section of the
  91. * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
  92. * with the correct path=<oldpath> setting was found and we could update it.
  93. */
  94. int update_path_in_gitmodules(const char *oldpath, const char *newpath)
  95. {
  96. struct strbuf entry = STRBUF_INIT;
  97. const struct submodule *submodule;
  98. int ret;
  99. if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
  100. return -1;
  101. if (is_gitmodules_unmerged(the_repository->index))
  102. die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
  103. submodule = submodule_from_path(the_repository, &null_oid, oldpath);
  104. if (!submodule || !submodule->name) {
  105. warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
  106. return -1;
  107. }
  108. strbuf_addstr(&entry, "submodule.");
  109. strbuf_addstr(&entry, submodule->name);
  110. strbuf_addstr(&entry, ".path");
  111. ret = config_set_in_gitmodules_file_gently(entry.buf, newpath);
  112. strbuf_release(&entry);
  113. return ret;
  114. }
  115. /*
  116. * Try to remove the "submodule.<name>" section from .gitmodules where the given
  117. * path is configured. Return 0 only if a .gitmodules file was found, a section
  118. * with the correct path=<path> setting was found and we could remove it.
  119. */
  120. int remove_path_from_gitmodules(const char *path)
  121. {
  122. struct strbuf sect = STRBUF_INIT;
  123. const struct submodule *submodule;
  124. if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
  125. return -1;
  126. if (is_gitmodules_unmerged(the_repository->index))
  127. die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
  128. submodule = submodule_from_path(the_repository, &null_oid, path);
  129. if (!submodule || !submodule->name) {
  130. warning(_("Could not find section in .gitmodules where path=%s"), path);
  131. return -1;
  132. }
  133. strbuf_addstr(&sect, "submodule.");
  134. strbuf_addstr(&sect, submodule->name);
  135. if (git_config_rename_section_in_file(GITMODULES_FILE, sect.buf, NULL) < 0) {
  136. /* Maybe the user already did that, don't error out here */
  137. warning(_("Could not remove .gitmodules entry for %s"), path);
  138. strbuf_release(&sect);
  139. return -1;
  140. }
  141. strbuf_release(&sect);
  142. return 0;
  143. }
  144. void stage_updated_gitmodules(struct index_state *istate)
  145. {
  146. if (add_file_to_index(istate, GITMODULES_FILE, 0))
  147. die(_("staging updated .gitmodules failed"));
  148. }
  149. /* TODO: remove this function, use repo_submodule_init instead. */
  150. int add_submodule_odb(const char *path)
  151. {
  152. struct strbuf objects_directory = STRBUF_INIT;
  153. int ret = 0;
  154. ret = strbuf_git_path_submodule(&objects_directory, path, "objects/");
  155. if (ret)
  156. goto done;
  157. if (!is_directory(objects_directory.buf)) {
  158. ret = -1;
  159. goto done;
  160. }
  161. add_to_alternates_memory(objects_directory.buf);
  162. done:
  163. strbuf_release(&objects_directory);
  164. return ret;
  165. }
  166. void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
  167. const char *path)
  168. {
  169. const struct submodule *submodule = submodule_from_path(the_repository,
  170. &null_oid, path);
  171. if (submodule) {
  172. const char *ignore;
  173. char *key;
  174. key = xstrfmt("submodule.%s.ignore", submodule->name);
  175. if (repo_config_get_string_tmp(the_repository, key, &ignore))
  176. ignore = submodule->ignore;
  177. free(key);
  178. if (ignore)
  179. handle_ignore_submodules_arg(diffopt, ignore);
  180. else if (is_gitmodules_unmerged(the_repository->index))
  181. diffopt->flags.ignore_submodules = 1;
  182. }
  183. }
  184. /* Cheap function that only determines if we're interested in submodules at all */
  185. int git_default_submodule_config(const char *var, const char *value, void *cb)
  186. {
  187. if (!strcmp(var, "submodule.recurse")) {
  188. int v = git_config_bool(var, value) ?
  189. RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
  190. config_update_recurse_submodules = v;
  191. }
  192. return 0;
  193. }
  194. int option_parse_recurse_submodules_worktree_updater(const struct option *opt,
  195. const char *arg, int unset)
  196. {
  197. if (unset) {
  198. config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
  199. return 0;
  200. }
  201. if (arg)
  202. config_update_recurse_submodules =
  203. parse_update_recurse_submodules_arg(opt->long_name,
  204. arg);
  205. else
  206. config_update_recurse_submodules = RECURSE_SUBMODULES_ON;
  207. return 0;
  208. }
  209. /*
  210. * Determine if a submodule has been initialized at a given 'path'
  211. */
  212. int is_submodule_active(struct repository *repo, const char *path)
  213. {
  214. int ret = 0;
  215. char *key = NULL;
  216. char *value = NULL;
  217. const struct string_list *sl;
  218. const struct submodule *module;
  219. module = submodule_from_path(repo, &null_oid, path);
  220. /* early return if there isn't a path->module mapping */
  221. if (!module)
  222. return 0;
  223. /* submodule.<name>.active is set */
  224. key = xstrfmt("submodule.%s.active", module->name);
  225. if (!repo_config_get_bool(repo, key, &ret)) {
  226. free(key);
  227. return ret;
  228. }
  229. free(key);
  230. /* submodule.active is set */
  231. sl = repo_config_get_value_multi(repo, "submodule.active");
  232. if (sl) {
  233. struct pathspec ps;
  234. struct strvec args = STRVEC_INIT;
  235. const struct string_list_item *item;
  236. for_each_string_list_item(item, sl) {
  237. strvec_push(&args, item->string);
  238. }
  239. parse_pathspec(&ps, 0, 0, NULL, args.v);
  240. ret = match_pathspec(repo->index, &ps, path, strlen(path), 0, NULL, 1);
  241. strvec_clear(&args);
  242. clear_pathspec(&ps);
  243. return ret;
  244. }
  245. /* fallback to checking if the URL is set */
  246. key = xstrfmt("submodule.%s.url", module->name);
  247. ret = !repo_config_get_string(repo, key, &value);
  248. free(value);
  249. free(key);
  250. return ret;
  251. }
  252. int is_submodule_populated_gently(const char *path, int *return_error_code)
  253. {
  254. int ret = 0;
  255. char *gitdir = xstrfmt("%s/.git", path);
  256. if (resolve_gitdir_gently(gitdir, return_error_code))
  257. ret = 1;
  258. free(gitdir);
  259. return ret;
  260. }
  261. /*
  262. * Dies if the provided 'prefix' corresponds to an unpopulated submodule
  263. */
  264. void die_in_unpopulated_submodule(const struct index_state *istate,
  265. const char *prefix)
  266. {
  267. int i, prefixlen;
  268. if (!prefix)
  269. return;
  270. prefixlen = strlen(prefix);
  271. for (i = 0; i < istate->cache_nr; i++) {
  272. struct cache_entry *ce = istate->cache[i];
  273. int ce_len = ce_namelen(ce);
  274. if (!S_ISGITLINK(ce->ce_mode))
  275. continue;
  276. if (prefixlen <= ce_len)
  277. continue;
  278. if (strncmp(ce->name, prefix, ce_len))
  279. continue;
  280. if (prefix[ce_len] != '/')
  281. continue;
  282. die(_("in unpopulated submodule '%s'"), ce->name);
  283. }
  284. }
  285. /*
  286. * Dies if any paths in the provided pathspec descends into a submodule
  287. */
  288. void die_path_inside_submodule(const struct index_state *istate,
  289. const struct pathspec *ps)
  290. {
  291. int i, j;
  292. for (i = 0; i < istate->cache_nr; i++) {
  293. struct cache_entry *ce = istate->cache[i];
  294. int ce_len = ce_namelen(ce);
  295. if (!S_ISGITLINK(ce->ce_mode))
  296. continue;
  297. for (j = 0; j < ps->nr ; j++) {
  298. const struct pathspec_item *item = &ps->items[j];
  299. if (item->len <= ce_len)
  300. continue;
  301. if (item->match[ce_len] != '/')
  302. continue;
  303. if (strncmp(ce->name, item->match, ce_len))
  304. continue;
  305. if (item->len == ce_len + 1)
  306. continue;
  307. die(_("Pathspec '%s' is in submodule '%.*s'"),
  308. item->original, ce_len, ce->name);
  309. }
  310. }
  311. }
  312. enum submodule_update_type parse_submodule_update_type(const char *value)
  313. {
  314. if (!strcmp(value, "none"))
  315. return SM_UPDATE_NONE;
  316. else if (!strcmp(value, "checkout"))
  317. return SM_UPDATE_CHECKOUT;
  318. else if (!strcmp(value, "rebase"))
  319. return SM_UPDATE_REBASE;
  320. else if (!strcmp(value, "merge"))
  321. return SM_UPDATE_MERGE;
  322. else if (*value == '!')
  323. return SM_UPDATE_COMMAND;
  324. else
  325. return SM_UPDATE_UNSPECIFIED;
  326. }
  327. int parse_submodule_update_strategy(const char *value,
  328. struct submodule_update_strategy *dst)
  329. {
  330. enum submodule_update_type type;
  331. free((void*)dst->command);
  332. dst->command = NULL;
  333. type = parse_submodule_update_type(value);
  334. if (type == SM_UPDATE_UNSPECIFIED)
  335. return -1;
  336. dst->type = type;
  337. if (type == SM_UPDATE_COMMAND)
  338. dst->command = xstrdup(value + 1);
  339. return 0;
  340. }
  341. const char *submodule_strategy_to_string(const struct submodule_update_strategy *s)
  342. {
  343. struct strbuf sb = STRBUF_INIT;
  344. switch (s->type) {
  345. case SM_UPDATE_CHECKOUT:
  346. return "checkout";
  347. case SM_UPDATE_MERGE:
  348. return "merge";
  349. case SM_UPDATE_REBASE:
  350. return "rebase";
  351. case SM_UPDATE_NONE:
  352. return "none";
  353. case SM_UPDATE_UNSPECIFIED:
  354. return NULL;
  355. case SM_UPDATE_COMMAND:
  356. strbuf_addf(&sb, "!%s", s->command);
  357. return strbuf_detach(&sb, NULL);
  358. }
  359. return NULL;
  360. }
  361. void handle_ignore_submodules_arg(struct diff_options *diffopt,
  362. const char *arg)
  363. {
  364. diffopt->flags.ignore_submodules = 0;
  365. diffopt->flags.ignore_untracked_in_submodules = 0;
  366. diffopt->flags.ignore_dirty_submodules = 0;
  367. if (!strcmp(arg, "all"))
  368. diffopt->flags.ignore_submodules = 1;
  369. else if (!strcmp(arg, "untracked"))
  370. diffopt->flags.ignore_untracked_in_submodules = 1;
  371. else if (!strcmp(arg, "dirty"))
  372. diffopt->flags.ignore_dirty_submodules = 1;
  373. else if (strcmp(arg, "none"))
  374. die(_("bad --ignore-submodules argument: %s"), arg);
  375. /*
  376. * Please update _git_status() in git-completion.bash when you
  377. * add new options
  378. */
  379. }
  380. static int prepare_submodule_diff_summary(struct repository *r, struct rev_info *rev,
  381. const char *path,
  382. struct commit *left, struct commit *right,
  383. struct commit_list *merge_bases)
  384. {
  385. struct commit_list *list;
  386. repo_init_revisions(r, rev, NULL);
  387. setup_revisions(0, NULL, rev, NULL);
  388. rev->left_right = 1;
  389. rev->first_parent_only = 1;
  390. left->object.flags |= SYMMETRIC_LEFT;
  391. add_pending_object(rev, &left->object, path);
  392. add_pending_object(rev, &right->object, path);
  393. for (list = merge_bases; list; list = list->next) {
  394. list->item->object.flags |= UNINTERESTING;
  395. add_pending_object(rev, &list->item->object,
  396. oid_to_hex(&list->item->object.oid));
  397. }
  398. return prepare_revision_walk(rev);
  399. }
  400. static void print_submodule_diff_summary(struct repository *r, struct rev_info *rev, struct diff_options *o)
  401. {
  402. static const char format[] = " %m %s";
  403. struct strbuf sb = STRBUF_INIT;
  404. struct commit *commit;
  405. while ((commit = get_revision(rev))) {
  406. struct pretty_print_context ctx = {0};
  407. ctx.date_mode = rev->date_mode;
  408. ctx.output_encoding = get_log_output_encoding();
  409. strbuf_setlen(&sb, 0);
  410. repo_format_commit_message(r, commit, format, &sb,
  411. &ctx);
  412. strbuf_addch(&sb, '\n');
  413. if (commit->object.flags & SYMMETRIC_LEFT)
  414. diff_emit_submodule_del(o, sb.buf);
  415. else
  416. diff_emit_submodule_add(o, sb.buf);
  417. }
  418. strbuf_release(&sb);
  419. }
  420. static void prepare_submodule_repo_env_no_git_dir(struct strvec *out)
  421. {
  422. const char * const *var;
  423. for (var = local_repo_env; *var; var++) {
  424. if (strcmp(*var, CONFIG_DATA_ENVIRONMENT))
  425. strvec_push(out, *var);
  426. }
  427. }
  428. void prepare_submodule_repo_env(struct strvec *out)
  429. {
  430. prepare_submodule_repo_env_no_git_dir(out);
  431. strvec_pushf(out, "%s=%s", GIT_DIR_ENVIRONMENT,
  432. DEFAULT_GIT_DIR_ENVIRONMENT);
  433. }
  434. static void prepare_submodule_repo_env_in_gitdir(struct strvec *out)
  435. {
  436. prepare_submodule_repo_env_no_git_dir(out);
  437. strvec_pushf(out, "%s=.", GIT_DIR_ENVIRONMENT);
  438. }
  439. /*
  440. * Initialize a repository struct for a submodule based on the provided 'path'.
  441. *
  442. * Unlike repo_submodule_init, this tolerates submodules not present
  443. * in .gitmodules. This function exists only to preserve historical behavior,
  444. *
  445. * Returns the repository struct on success,
  446. * NULL when the submodule is not present.
  447. */
  448. static struct repository *open_submodule(const char *path)
  449. {
  450. struct strbuf sb = STRBUF_INIT;
  451. struct repository *out = xmalloc(sizeof(*out));
  452. if (submodule_to_gitdir(&sb, path) || repo_init(out, sb.buf, NULL)) {
  453. strbuf_release(&sb);
  454. free(out);
  455. return NULL;
  456. }
  457. /* Mark it as a submodule */
  458. out->submodule_prefix = xstrdup(path);
  459. strbuf_release(&sb);
  460. return out;
  461. }
  462. /*
  463. * Helper function to display the submodule header line prior to the full
  464. * summary output.
  465. *
  466. * If it can locate the submodule git directory it will create a repository
  467. * handle for the submodule and lookup both the left and right commits and
  468. * put them into the left and right pointers.
  469. */
  470. static void show_submodule_header(struct diff_options *o,
  471. const char *path,
  472. struct object_id *one, struct object_id *two,
  473. unsigned dirty_submodule,
  474. struct repository *sub,
  475. struct commit **left, struct commit **right,
  476. struct commit_list **merge_bases)
  477. {
  478. const char *message = NULL;
  479. struct strbuf sb = STRBUF_INIT;
  480. int fast_forward = 0, fast_backward = 0;
  481. if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
  482. diff_emit_submodule_untracked(o, path);
  483. if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
  484. diff_emit_submodule_modified(o, path);
  485. if (is_null_oid(one))
  486. message = "(new submodule)";
  487. else if (is_null_oid(two))
  488. message = "(submodule deleted)";
  489. if (!sub) {
  490. if (!message)
  491. message = "(commits not present)";
  492. goto output_header;
  493. }
  494. /*
  495. * Attempt to lookup the commit references, and determine if this is
  496. * a fast forward or fast backwards update.
  497. */
  498. *left = lookup_commit_reference(sub, one);
  499. *right = lookup_commit_reference(sub, two);
  500. /*
  501. * Warn about missing commits in the submodule project, but only if
  502. * they aren't null.
  503. */
  504. if ((!is_null_oid(one) && !*left) ||
  505. (!is_null_oid(two) && !*right))
  506. message = "(commits not present)";
  507. *merge_bases = repo_get_merge_bases(sub, *left, *right);
  508. if (*merge_bases) {
  509. if ((*merge_bases)->item == *left)
  510. fast_forward = 1;
  511. else if ((*merge_bases)->item == *right)
  512. fast_backward = 1;
  513. }
  514. if (oideq(one, two)) {
  515. strbuf_release(&sb);
  516. return;
  517. }
  518. output_header:
  519. strbuf_addf(&sb, "Submodule %s ", path);
  520. strbuf_add_unique_abbrev(&sb, one, DEFAULT_ABBREV);
  521. strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
  522. strbuf_add_unique_abbrev(&sb, two, DEFAULT_ABBREV);
  523. if (message)
  524. strbuf_addf(&sb, " %s\n", message);
  525. else
  526. strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
  527. diff_emit_submodule_header(o, sb.buf);
  528. strbuf_release(&sb);
  529. }
  530. void show_submodule_diff_summary(struct diff_options *o, const char *path,
  531. struct object_id *one, struct object_id *two,
  532. unsigned dirty_submodule)
  533. {
  534. struct rev_info rev;
  535. struct commit *left = NULL, *right = NULL;
  536. struct commit_list *merge_bases = NULL;
  537. struct repository *sub;
  538. sub = open_submodule(path);
  539. show_submodule_header(o, path, one, two, dirty_submodule,
  540. sub, &left, &right, &merge_bases);
  541. /*
  542. * If we don't have both a left and a right pointer, there is no
  543. * reason to try and display a summary. The header line should contain
  544. * all the information the user needs.
  545. */
  546. if (!left || !right || !sub)
  547. goto out;
  548. /* Treat revision walker failure the same as missing commits */
  549. if (prepare_submodule_diff_summary(sub, &rev, path, left, right, merge_bases)) {
  550. diff_emit_submodule_error(o, "(revision walker failed)\n");
  551. goto out;
  552. }
  553. print_submodule_diff_summary(sub, &rev, o);
  554. out:
  555. if (merge_bases)
  556. free_commit_list(merge_bases);
  557. clear_commit_marks(left, ~0);
  558. clear_commit_marks(right, ~0);
  559. if (sub) {
  560. repo_clear(sub);
  561. free(sub);
  562. }
  563. }
  564. void show_submodule_inline_diff(struct diff_options *o, const char *path,
  565. struct object_id *one, struct object_id *two,
  566. unsigned dirty_submodule)
  567. {
  568. const struct object_id *old_oid = the_hash_algo->empty_tree, *new_oid = the_hash_algo->empty_tree;
  569. struct commit *left = NULL, *right = NULL;
  570. struct commit_list *merge_bases = NULL;
  571. struct child_process cp = CHILD_PROCESS_INIT;
  572. struct strbuf sb = STRBUF_INIT;
  573. struct repository *sub;
  574. sub = open_submodule(path);
  575. show_submodule_header(o, path, one, two, dirty_submodule,
  576. sub, &left, &right, &merge_bases);
  577. /* We need a valid left and right commit to display a difference */
  578. if (!(left || is_null_oid(one)) ||
  579. !(right || is_null_oid(two)))
  580. goto done;
  581. if (left)
  582. old_oid = one;
  583. if (right)
  584. new_oid = two;
  585. cp.git_cmd = 1;
  586. cp.dir = path;
  587. cp.out = -1;
  588. cp.no_stdin = 1;
  589. /* TODO: other options may need to be passed here. */
  590. strvec_pushl(&cp.args, "diff", "--submodule=diff", NULL);
  591. strvec_pushf(&cp.args, "--color=%s", want_color(o->use_color) ?
  592. "always" : "never");
  593. if (o->flags.reverse_diff) {
  594. strvec_pushf(&cp.args, "--src-prefix=%s%s/",
  595. o->b_prefix, path);
  596. strvec_pushf(&cp.args, "--dst-prefix=%s%s/",
  597. o->a_prefix, path);
  598. } else {
  599. strvec_pushf(&cp.args, "--src-prefix=%s%s/",
  600. o->a_prefix, path);
  601. strvec_pushf(&cp.args, "--dst-prefix=%s%s/",
  602. o->b_prefix, path);
  603. }
  604. strvec_push(&cp.args, oid_to_hex(old_oid));
  605. /*
  606. * If the submodule has modified content, we will diff against the
  607. * work tree, under the assumption that the user has asked for the
  608. * diff format and wishes to actually see all differences even if they
  609. * haven't yet been committed to the submodule yet.
  610. */
  611. if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED))
  612. strvec_push(&cp.args, oid_to_hex(new_oid));
  613. prepare_submodule_repo_env(&cp.env_array);
  614. if (start_command(&cp))
  615. diff_emit_submodule_error(o, "(diff failed)\n");
  616. while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF)
  617. diff_emit_submodule_pipethrough(o, sb.buf, sb.len);
  618. if (finish_command(&cp))
  619. diff_emit_submodule_error(o, "(diff failed)\n");
  620. done:
  621. strbuf_release(&sb);
  622. if (merge_bases)
  623. free_commit_list(merge_bases);
  624. if (left)
  625. clear_commit_marks(left, ~0);
  626. if (right)
  627. clear_commit_marks(right, ~0);
  628. if (sub) {
  629. repo_clear(sub);
  630. free(sub);
  631. }
  632. }
  633. int should_update_submodules(void)
  634. {
  635. return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
  636. }
  637. const struct submodule *submodule_from_ce(const struct cache_entry *ce)
  638. {
  639. if (!S_ISGITLINK(ce->ce_mode))
  640. return NULL;
  641. if (!should_update_submodules())
  642. return NULL;
  643. return submodule_from_path(the_repository, &null_oid, ce->name);
  644. }
  645. static struct oid_array *submodule_commits(struct string_list *submodules,
  646. const char *name)
  647. {
  648. struct string_list_item *item;
  649. item = string_list_insert(submodules, name);
  650. if (item->util)
  651. return (struct oid_array *) item->util;
  652. /* NEEDSWORK: should we have oid_array_init()? */
  653. item->util = xcalloc(1, sizeof(struct oid_array));
  654. return (struct oid_array *) item->util;
  655. }
  656. struct collect_changed_submodules_cb_data {
  657. struct repository *repo;
  658. struct string_list *changed;
  659. const struct object_id *commit_oid;
  660. };
  661. /*
  662. * this would normally be two functions: default_name_from_path() and
  663. * path_from_default_name(). Since the default name is the same as
  664. * the submodule path we can get away with just one function which only
  665. * checks whether there is a submodule in the working directory at that
  666. * location.
  667. */
  668. static const char *default_name_or_path(const char *path_or_name)
  669. {
  670. int error_code;
  671. if (!is_submodule_populated_gently(path_or_name, &error_code))
  672. return NULL;
  673. return path_or_name;
  674. }
  675. static void collect_changed_submodules_cb(struct diff_queue_struct *q,
  676. struct diff_options *options,
  677. void *data)
  678. {
  679. struct collect_changed_submodules_cb_data *me = data;
  680. struct string_list *changed = me->changed;
  681. const struct object_id *commit_oid = me->commit_oid;
  682. int i;
  683. for (i = 0; i < q->nr; i++) {
  684. struct diff_filepair *p = q->queue[i];
  685. struct oid_array *commits;
  686. const struct submodule *submodule;
  687. const char *name;
  688. if (!S_ISGITLINK(p->two->mode))
  689. continue;
  690. submodule = submodule_from_path(me->repo,
  691. commit_oid, p->two->path);
  692. if (submodule)
  693. name = submodule->name;
  694. else {
  695. name = default_name_or_path(p->two->path);
  696. /* make sure name does not collide with existing one */
  697. if (name)
  698. submodule = submodule_from_name(me->repo,
  699. commit_oid, name);
  700. if (submodule) {
  701. warning(_("Submodule in commit %s at path: "
  702. "'%s' collides with a submodule named "
  703. "the same. Skipping it."),
  704. oid_to_hex(commit_oid), p->two->path);
  705. name = NULL;
  706. }
  707. }
  708. if (!name)
  709. continue;
  710. commits = submodule_commits(changed, name);
  711. oid_array_append(commits, &p->two->oid);
  712. }
  713. }
  714. /*
  715. * Collect the paths of submodules in 'changed' which have changed based on
  716. * the revisions as specified in 'argv'. Each entry in 'changed' will also
  717. * have a corresponding 'struct oid_array' (in the 'util' field) which lists
  718. * what the submodule pointers were updated to during the change.
  719. */
  720. static void collect_changed_submodules(struct repository *r,
  721. struct string_list *changed,
  722. struct strvec *argv)
  723. {
  724. struct rev_info rev;
  725. const struct commit *commit;
  726. int save_warning;
  727. struct setup_revision_opt s_r_opt = {
  728. .assume_dashdash = 1,
  729. };
  730. save_warning = warn_on_object_refname_ambiguity;
  731. warn_on_object_refname_ambiguity = 0;
  732. repo_init_revisions(r, &rev, NULL);
  733. setup_revisions(argv->nr, argv->v, &rev, &s_r_opt);
  734. warn_on_object_refname_ambiguity = save_warning;
  735. if (prepare_revision_walk(&rev))
  736. die(_("revision walk setup failed"));
  737. while ((commit = get_revision(&rev))) {
  738. struct rev_info diff_rev;
  739. struct collect_changed_submodules_cb_data data;
  740. data.repo = r;
  741. data.changed = changed;
  742. data.commit_oid = &commit->object.oid;
  743. repo_init_revisions(r, &diff_rev, NULL);
  744. diff_rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
  745. diff_rev.diffopt.format_callback = collect_changed_submodules_cb;
  746. diff_rev.diffopt.format_callback_data = &data;
  747. diff_rev.dense_combined_merges = 1;
  748. diff_tree_combined_merge(commit, &diff_rev);
  749. }
  750. reset_revision_walk();
  751. }
  752. static void free_submodules_oids(struct string_list *submodules)
  753. {
  754. struct string_list_item *item;
  755. for_each_string_list_item(item, submodules)
  756. oid_array_clear((struct oid_array *) item->util);
  757. string_list_clear(submodules, 1);
  758. }
  759. static int has_remote(const char *refname, const struct object_id *oid,
  760. int flags, void *cb_data)
  761. {
  762. return 1;
  763. }
  764. static int append_oid_to_argv(const struct object_id *oid, void *data)
  765. {
  766. struct strvec *argv = data;
  767. strvec_push(argv, oid_to_hex(oid));
  768. return 0;
  769. }
  770. struct has_commit_data {
  771. struct repository *repo;
  772. int result;
  773. const char *path;
  774. };
  775. static int check_has_commit(const struct object_id *oid, void *data)
  776. {
  777. struct has_commit_data *cb = data;
  778. enum object_type type = oid_object_info(cb->repo, oid, NULL);
  779. switch (type) {
  780. case OBJ_COMMIT:
  781. return 0;
  782. case OBJ_BAD:
  783. /*
  784. * Object is missing or invalid. If invalid, an error message
  785. * has already been printed.
  786. */
  787. cb->result = 0;
  788. return 0;
  789. default:
  790. die(_("submodule entry '%s' (%s) is a %s, not a commit"),
  791. cb->path, oid_to_hex(oid), type_name(type));
  792. }
  793. }
  794. static int submodule_has_commits(struct repository *r,
  795. const char *path,
  796. struct oid_array *commits)
  797. {
  798. struct has_commit_data has_commit = { r, 1, path };
  799. /*
  800. * Perform a cheap, but incorrect check for the existence of 'commits'.
  801. * This is done by adding the submodule's object store to the in-core
  802. * object store, and then querying for each commit's existence. If we
  803. * do not have the commit object anywhere, there is no chance we have
  804. * it in the object store of the correct submodule and have it
  805. * reachable from a ref, so we can fail early without spawning rev-list
  806. * which is expensive.
  807. */
  808. if (add_submodule_odb(path))
  809. return 0;
  810. oid_array_for_each_unique(commits, check_has_commit, &has_commit);
  811. if (has_commit.result) {
  812. /*
  813. * Even if the submodule is checked out and the commit is
  814. * present, make sure it exists in the submodule's object store
  815. * and that it is reachable from a ref.
  816. */
  817. struct child_process cp = CHILD_PROCESS_INIT;
  818. struct strbuf out = STRBUF_INIT;
  819. strvec_pushl(&cp.args, "rev-list", "-n", "1", NULL);
  820. oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
  821. strvec_pushl(&cp.args, "--not", "--all", NULL);
  822. prepare_submodule_repo_env(&cp.env_array);
  823. cp.git_cmd = 1;
  824. cp.no_stdin = 1;
  825. cp.dir = path;
  826. if (capture_command(&cp, &out, GIT_MAX_HEXSZ + 1) || out.len)
  827. has_commit.result = 0;
  828. strbuf_release(&out);
  829. }
  830. return has_commit.result;
  831. }
  832. static int submodule_needs_pushing(struct repository *r,
  833. const char *path,
  834. struct oid_array *commits)
  835. {
  836. if (!submodule_has_commits(r, path, commits))
  837. /*
  838. * NOTE: We do consider it safe to return "no" here. The
  839. * correct answer would be "We do not know" instead of
  840. * "No push needed", but it is quite hard to change
  841. * the submodule pointer without having the submodule
  842. * around. If a user did however change the submodules
  843. * without having the submodule around, this indicates
  844. * an expert who knows what they are doing or a
  845. * maintainer integrating work from other people. In
  846. * both cases it should be safe to skip this check.
  847. */
  848. return 0;
  849. if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
  850. struct child_process cp = CHILD_PROCESS_INIT;
  851. struct strbuf buf = STRBUF_INIT;
  852. int needs_pushing = 0;
  853. strvec_push(&cp.args, "rev-list");
  854. oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
  855. strvec_pushl(&cp.args, "--not", "--remotes", "-n", "1" , NULL);
  856. prepare_submodule_repo_env(&cp.env_array);
  857. cp.git_cmd = 1;
  858. cp.no_stdin = 1;
  859. cp.out = -1;
  860. cp.dir = path;
  861. if (start_command(&cp))
  862. die(_("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s"),
  863. path);
  864. if (strbuf_read(&buf, cp.out, the_hash_algo->hexsz + 1))
  865. needs_pushing = 1;
  866. finish_command(&cp);
  867. close(cp.out);
  868. strbuf_release(&buf);
  869. return needs_pushing;
  870. }
  871. return 0;
  872. }
  873. int find_unpushed_submodules(struct repository *r,
  874. struct oid_array *commits,
  875. const char *remotes_name,
  876. struct string_list *needs_pushing)
  877. {
  878. struct string_list submodules = STRING_LIST_INIT_DUP;
  879. struct string_list_item *name;
  880. struct strvec argv = STRVEC_INIT;
  881. /* argv.v[0] will be ignored by setup_revisions */
  882. strvec_push(&argv, "find_unpushed_submodules");
  883. oid_array_for_each_unique(commits, append_oid_to_argv, &argv);
  884. strvec_push(&argv, "--not");
  885. strvec_pushf(&argv, "--remotes=%s", remotes_name);
  886. collect_changed_submodules(r, &submodules, &argv);
  887. for_each_string_list_item(name, &submodules) {
  888. struct oid_array *commits = name->util;
  889. const struct submodule *submodule;
  890. const char *path = NULL;
  891. submodule = submodule_from_name(r, &null_oid, name->string);
  892. if (submodule)
  893. path = submodule->path;
  894. else
  895. path = default_name_or_path(name->string);
  896. if (!path)
  897. continue;
  898. if (submodule_needs_pushing(r, path, commits))
  899. string_list_insert(needs_pushing, path);
  900. }
  901. free_submodules_oids(&submodules);
  902. strvec_clear(&argv);
  903. return needs_pushing->nr;
  904. }
  905. static int push_submodule(const char *path,
  906. const struct remote *remote,
  907. const struct refspec *rs,
  908. const struct string_list *push_options,
  909. int dry_run)
  910. {
  911. if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
  912. struct child_process cp = CHILD_PROCESS_INIT;
  913. strvec_push(&cp.args, "push");
  914. if (dry_run)
  915. strvec_push(&cp.args, "--dry-run");
  916. if (push_options && push_options->nr) {
  917. const struct string_list_item *item;
  918. for_each_string_list_item(item, push_options)
  919. strvec_pushf(&cp.args, "--push-option=%s",
  920. item->string);
  921. }
  922. if (remote->origin != REMOTE_UNCONFIGURED) {
  923. int i;
  924. strvec_push(&cp.args, remote->name);
  925. for (i = 0; i < rs->raw_nr; i++)
  926. strvec_push(&cp.args, rs->raw[i]);
  927. }
  928. prepare_submodule_repo_env(&cp.env_array);
  929. cp.git_cmd = 1;
  930. cp.no_stdin = 1;
  931. cp.dir = path;
  932. if (run_command(&cp))
  933. return 0;
  934. close(cp.out);
  935. }
  936. return 1;
  937. }
  938. /*
  939. * Perform a check in the submodule to see if the remote and refspec work.
  940. * Die if the submodule can't be pushed.
  941. */
  942. static void submodule_push_check(const char *path, const char *head,
  943. const struct remote *remote,
  944. const struct refspec *rs)
  945. {
  946. struct child_process cp = CHILD_PROCESS_INIT;
  947. int i;
  948. strvec_push(&cp.args, "submodule--helper");
  949. strvec_push(&cp.args, "push-check");
  950. strvec_push(&cp.args, head);
  951. strvec_push(&cp.args, remote->name);
  952. for (i = 0; i < rs->raw_nr; i++)
  953. strvec_push(&cp.args, rs->raw[i]);
  954. prepare_submodule_repo_env(&cp.env_array);
  955. cp.git_cmd = 1;
  956. cp.no_stdin = 1;
  957. cp.no_stdout = 1;
  958. cp.dir = path;
  959. /*
  960. * Simply indicate if 'submodule--helper push-check' failed.
  961. * More detailed error information will be provided by the
  962. * child process.
  963. */
  964. if (run_command(&cp))
  965. die(_("process for submodule '%s' failed"), path);
  966. }
  967. int push_unpushed_submodules(struct repository *r,
  968. struct oid_array *commits,
  969. const struct remote *remote,
  970. const struct refspec *rs,
  971. const struct string_list *push_options,
  972. int dry_run)
  973. {
  974. int i, ret = 1;
  975. struct string_list needs_pushing = STRING_LIST_INIT_DUP;
  976. if (!find_unpushed_submodules(r, commits,
  977. remote->name, &needs_pushing))
  978. return 1;
  979. /*
  980. * Verify that the remote and refspec can be propagated to all
  981. * submodules. This check can be skipped if the remote and refspec
  982. * won't be propagated due to the remote being unconfigured (e.g. a URL
  983. * instead of a remote name).
  984. */
  985. if (remote->origin != REMOTE_UNCONFIGURED) {
  986. char *head;
  987. struct object_id head_oid;
  988. head = resolve_refdup("HEAD", 0, &head_oid, NULL);
  989. if (!head)
  990. die(_("Failed to resolve HEAD as a valid ref."));
  991. for (i = 0; i < needs_pushing.nr; i++)
  992. submodule_push_check(needs_pushing.items[i].string,
  993. head, remote, rs);
  994. free(head);
  995. }
  996. /* Actually push the submodules */
  997. for (i = 0; i < needs_pushing.nr; i++) {
  998. const char *path = needs_pushing.items[i].string;
  999. fprintf(stderr, _("Pushing submodule '%s'\n"), path);
  1000. if (!push_submodule(path, remote, rs,
  1001. push_options, dry_run)) {
  1002. fprintf(stderr, _("Unable to push submodule '%s'\n"), path);
  1003. ret = 0;
  1004. }
  1005. }
  1006. string_list_clear(&needs_pushing, 0);
  1007. return ret;
  1008. }
  1009. static int append_oid_to_array(const char *ref, const struct object_id *oid,
  1010. int flags, void *data)
  1011. {
  1012. struct oid_array *array = data;
  1013. oid_array_append(array, oid);
  1014. return 0;
  1015. }
  1016. void check_for_new_submodule_commits(struct object_id *oid)
  1017. {
  1018. if (!initialized_fetch_ref_tips) {
  1019. for_each_ref(append_oid_to_array, &ref_tips_before_fetch);
  1020. initialized_fetch_ref_tips = 1;
  1021. }
  1022. oid_array_append(&ref_tips_after_fetch, oid);
  1023. }
  1024. static void calculate_changed_submodule_paths(struct repository *r,
  1025. struct string_list *changed_submodule_names)
  1026. {
  1027. struct strvec argv = STRVEC_INIT;
  1028. struct string_list_item *name;
  1029. /* No need to check if there are no submodules configured */
  1030. if (!submodule_from_path(r, NULL, NULL))
  1031. return;
  1032. strvec_push(&argv, "--"); /* argv[0] program name */
  1033. oid_array_for_each_unique(&ref_tips_after_fetch,
  1034. append_oid_to_argv, &argv);
  1035. strvec_push(&argv, "--not");
  1036. oid_array_for_each_unique(&ref_tips_before_fetch,
  1037. append_oid_to_argv, &argv);
  1038. /*
  1039. * Collect all submodules (whether checked out or not) for which new
  1040. * commits have been recorded upstream in "changed_submodule_names".
  1041. */
  1042. collect_changed_submodules(r, changed_submodule_names, &argv);
  1043. for_each_string_list_item(name, changed_submodule_names) {
  1044. struct oid_array *commits = name->util;
  1045. const struct submodule *submodule;
  1046. const char *path = NULL;
  1047. submodule = submodule_from_name(r, &null_oid, name->string);
  1048. if (submodule)
  1049. path = submodule->path;
  1050. else
  1051. path = default_name_or_path(name->string);
  1052. if (!path)
  1053. continue;
  1054. if (submodule_has_commits(r, path, commits)) {
  1055. oid_array_clear(commits);
  1056. *name->string = '\0';
  1057. }
  1058. }
  1059. string_list_remove_empty_items(changed_submodule_names, 1);
  1060. strvec_clear(&argv);
  1061. oid_array_clear(&ref_tips_before_fetch);
  1062. oid_array_clear(&ref_tips_after_fetch);
  1063. initialized_fetch_ref_tips = 0;
  1064. }
  1065. int submodule_touches_in_range(struct repository *r,
  1066. struct object_id *excl_oid,
  1067. struct object_id *incl_oid)
  1068. {
  1069. struct string_list subs = STRING_LIST_INIT_DUP;
  1070. struct strvec args = STRVEC_INIT;
  1071. int ret;
  1072. /* No need to check if there are no submodules configured */
  1073. if (!submodule_from_path(r, NULL, NULL))
  1074. return 0;
  1075. strvec_push(&args, "--"); /* args[0] program name */
  1076. strvec_push(&args, oid_to_hex(incl_oid));
  1077. if (!is_null_oid(excl_oid)) {
  1078. strvec_push(&args, "--not");
  1079. strvec_push(&args, oid_to_hex(excl_oid));
  1080. }
  1081. collect_changed_submodules(r, &subs, &args);
  1082. ret = subs.nr;
  1083. strvec_clear(&args);
  1084. free_submodules_oids(&subs);
  1085. return ret;
  1086. }
  1087. struct submodule_parallel_fetch {
  1088. int count;
  1089. struct strvec args;
  1090. struct repository *r;
  1091. const char *prefix;
  1092. int command_line_option;
  1093. int default_option;
  1094. int quiet;
  1095. int result;
  1096. struct string_list changed_submodule_names;
  1097. /* Pending fetches by OIDs */
  1098. struct fetch_task **oid_fetch_tasks;
  1099. int oid_fetch_tasks_nr, oid_fetch_tasks_alloc;
  1100. struct strbuf submodules_with_errors;
  1101. };
  1102. #define SPF_INIT {0, STRVEC_INIT, NULL, NULL, 0, 0, 0, 0, \
  1103. STRING_LIST_INIT_DUP, \
  1104. NULL, 0, 0, STRBUF_INIT}
  1105. static int get_fetch_recurse_config(const struct submodule *submodule,
  1106. struct submodule_parallel_fetch *spf)
  1107. {
  1108. if (spf->command_line_option != RECURSE_SUBMODULES_DEFAULT)
  1109. return spf->command_line_option;
  1110. if (submodule) {
  1111. char *key;
  1112. const char *value;
  1113. int fetch_recurse = submodule->fetch_recurse;
  1114. key = xstrfmt("submodule.%s.fetchRecurseSubmodules", submodule->name);
  1115. if (!repo_config_get_string_tmp(spf->r, key, &value)) {
  1116. fetch_recurse = parse_fetch_recurse_submodules_arg(key, value);
  1117. }
  1118. free(key);
  1119. if (fetch_recurse != RECURSE_SUBMODULES_NONE)
  1120. /* local config overrules everything except commandline */
  1121. return fetch_recurse;
  1122. }
  1123. return spf->default_option;
  1124. }
  1125. /*
  1126. * Fetch in progress (if callback data) or
  1127. * pending (if in oid_fetch_tasks in struct submodule_parallel_fetch)
  1128. */
  1129. struct fetch_task {
  1130. struct repository *repo;
  1131. const struct submodule *sub;
  1132. unsigned free_sub : 1; /* Do we need to free the submodule? */
  1133. struct oid_array *commits; /* Ensure these commits are fetched */
  1134. };
  1135. /**
  1136. * When a submodule is not defined in .gitmodules, we cannot access it
  1137. * via the regular submodule-config. Create a fake submodule, which we can
  1138. * work on.
  1139. */
  1140. static const struct submodule *get_non_gitmodules_submodule(const char *path)
  1141. {
  1142. struct submodule *ret = NULL;
  1143. const char *name = default_name_or_path(path);
  1144. if (!name)
  1145. return NULL;
  1146. ret = xmalloc(sizeof(*ret));
  1147. memset(ret, 0, sizeof(*ret));
  1148. ret->path = name;
  1149. ret->name = name;
  1150. return (const struct submodule *) ret;
  1151. }
  1152. static struct fetch_task *fetch_task_create(struct repository *r,
  1153. const char *path)
  1154. {
  1155. struct fetch_task *task = xmalloc(sizeof(*task));
  1156. memset(task, 0, sizeof(*task));
  1157. task->sub = submodule_from_path(r, &null_oid, path);
  1158. if (!task->sub) {
  1159. /*
  1160. * No entry in .gitmodules? Technically not a submodule,
  1161. * but historically we supported repositories that happen to be
  1162. * in-place where a gitlink is. Keep supporting them.
  1163. */
  1164. task->sub = get_non_gitmodules_submodule(path);
  1165. if (!task->sub) {
  1166. free(task);
  1167. return NULL;
  1168. }
  1169. task->free_sub = 1;
  1170. }
  1171. return task;
  1172. }
  1173. static void fetch_task_release(struct fetch_task *p)
  1174. {
  1175. if (p->free_sub)
  1176. free((void*)p->sub);
  1177. p->free_sub = 0;
  1178. p->sub = NULL;
  1179. if (p->repo)
  1180. repo_clear(p->repo);
  1181. FREE_AND_NULL(p->repo);
  1182. }
  1183. static struct repository *get_submodule_repo_for(struct repository *r,
  1184. const struct submodule *sub)
  1185. {
  1186. struct repository *ret = xmalloc(sizeof(*ret));
  1187. if (repo_submodule_init(ret, r, sub)) {
  1188. /*
  1189. * No entry in .gitmodules? Technically not a submodule,
  1190. * but historically we supported repositories that happen to be
  1191. * in-place where a gitlink is. Keep supporting them.
  1192. */
  1193. struct strbuf gitdir = STRBUF_INIT;
  1194. strbuf_repo_worktree_path(&gitdir, r, "%s/.git", sub->path);
  1195. if (repo_init(ret, gitdir.buf, NULL)) {
  1196. strbuf_release(&gitdir);
  1197. free(ret);
  1198. return NULL;
  1199. }
  1200. strbuf_release(&gitdir);
  1201. }
  1202. return ret;
  1203. }
  1204. static int get_next_submodule(struct child_process *cp,
  1205. struct strbuf *err, void *data, void **task_cb)
  1206. {
  1207. struct submodule_parallel_fetch *spf = data;
  1208. for (; spf->count < spf->r->index->cache_nr; spf->count++) {
  1209. const struct cache_entry *ce = spf->r->index->cache[spf->count];
  1210. const char *default_argv;
  1211. struct fetch_task *task;
  1212. if (!S_ISGITLINK(ce->ce_mode))
  1213. continue;
  1214. task = fetch_task_create(spf->r, ce->name);
  1215. if (!task)
  1216. continue;
  1217. switch (get_fetch_recurse_config(task->sub, spf))
  1218. {
  1219. default:
  1220. case RECURSE_SUBMODULES_DEFAULT:
  1221. case RECURSE_SUBMODULES_ON_DEMAND:
  1222. if (!task->sub ||
  1223. !string_list_lookup(
  1224. &spf->changed_submodule_names,
  1225. task->sub->name))
  1226. continue;
  1227. default_argv = "on-demand";
  1228. break;
  1229. case RECURSE_SUBMODULES_ON:
  1230. default_argv = "yes";
  1231. break;
  1232. case RECURSE_SUBMODULES_OFF:
  1233. continue;
  1234. }
  1235. task->repo = get_submodule_repo_for(spf->r, task->sub);
  1236. if (task->repo) {
  1237. struct strbuf submodule_prefix = STRBUF_INIT;
  1238. child_process_init(cp);
  1239. cp->dir = task->repo->gitdir;
  1240. prepare_submodule_repo_env_in_gitdir(&cp->env_array);
  1241. cp->git_cmd = 1;
  1242. if (!spf->quiet)
  1243. strbuf_addf(err, _("Fetching submodule %s%s\n"),
  1244. spf->prefix, ce->name);
  1245. strvec_init(&cp->args);
  1246. strvec_pushv(&cp->args, spf->args.v);
  1247. strvec_push(&cp->args, default_argv);
  1248. strvec_push(&cp->args, "--submodule-prefix");
  1249. strbuf_addf(&submodule_prefix, "%s%s/",
  1250. spf->prefix,
  1251. task->sub->path);
  1252. strvec_push(&cp->args, submodule_prefix.buf);
  1253. spf->count++;
  1254. *task_cb = task;
  1255. strbuf_release(&submodule_prefix);
  1256. return 1;
  1257. } else {
  1258. fetch_task_release(task);
  1259. free(task);
  1260. /*
  1261. * An empty directory is normal,
  1262. * the submodule is not initialized
  1263. */
  1264. if (S_ISGITLINK(ce->ce_mode) &&
  1265. !is_empty_dir(ce->name)) {
  1266. spf->result = 1;
  1267. strbuf_addf(err,
  1268. _("Could not access submodule '%s'\n"),
  1269. ce->name);
  1270. }
  1271. }
  1272. }
  1273. if (spf->oid_fetch_tasks_nr) {
  1274. struct fetch_task *task =
  1275. spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr - 1];
  1276. struct strbuf submodule_prefix = STRBUF_INIT;
  1277. spf->oid_fetch_tasks_nr--;
  1278. strbuf_addf(&submodule_prefix, "%s%s/",
  1279. spf->prefix, task->sub->path);
  1280. child_process_init(cp);
  1281. prepare_submodule_repo_env_in_gitdir(&cp->env_array);
  1282. cp->git_cmd = 1;
  1283. cp->dir = task->repo->gitdir;
  1284. strvec_init(&cp->args);
  1285. strvec_pushv(&cp->args, spf->args.v);
  1286. strvec_push(&cp->args, "on-demand");
  1287. strvec_push(&cp->args, "--submodule-prefix");
  1288. strvec_push(&cp->args, submodule_prefix.buf);
  1289. /* NEEDSWORK: have get_default_remote from submodule--helper */
  1290. strvec_push(&cp->args, "origin");
  1291. oid_array_for_each_unique(task->commits,
  1292. append_oid_to_argv, &cp->args);
  1293. *task_cb = task;
  1294. strbuf_release(&submodule_prefix);
  1295. return 1;
  1296. }
  1297. return 0;
  1298. }
  1299. static int fetch_start_failure(struct strbuf *err,
  1300. void *cb, void *task_cb)
  1301. {
  1302. struct submodule_parallel_fetch *spf = cb;
  1303. struct fetch_task *task = task_cb;
  1304. spf->result = 1;
  1305. fetch_task_release(task);
  1306. return 0;
  1307. }
  1308. static int commit_missing_in_sub(const struct object_id *oid, void *data)
  1309. {
  1310. struct repository *subrepo = data;
  1311. enum object_type type = oid_object_info(subrepo, oid, NULL);
  1312. return type != OBJ_COMMIT;
  1313. }
  1314. static int fetch_finish(int retvalue, struct strbuf *err,
  1315. void *cb, void *task_cb)
  1316. {
  1317. struct submodule_parallel_fetch *spf = cb;
  1318. struct fetch_task *task = task_cb;
  1319. struct string_list_item *it;
  1320. struct oid_array *commits;
  1321. if (!task || !task->sub)
  1322. BUG("callback cookie bogus");
  1323. if (retvalue) {
  1324. /*
  1325. * NEEDSWORK: This indicates that the overall fetch
  1326. * failed, even though there may be a subsequent fetch
  1327. * by commit hash that might work. It may be a good
  1328. * idea to not indicate failure in this case, and only
  1329. * indicate failure if the subsequent fetch fails.
  1330. */
  1331. spf->result = 1;
  1332. strbuf_addf(&spf->submodules_with_errors, "\t%s\n",
  1333. task->sub->name);
  1334. }
  1335. /* Is this the second time we process this submodule? */
  1336. if (task->commits)
  1337. goto out;
  1338. it = string_list_lookup(&spf->changed_submodule_names, task->sub->name);
  1339. if (!it)
  1340. /* Could be an unchanged submodule, not contained in the list */
  1341. goto out;
  1342. commits = it->util;
  1343. oid_array_filter(commits,
  1344. commit_missing_in_sub,
  1345. task->repo);
  1346. /* Are there commits we want, but do not exist? */
  1347. if (commits->nr) {
  1348. task->commits = commits;
  1349. ALLOC_GROW(spf->oid_fetch_tasks,
  1350. spf->oid_fetch_tasks_nr + 1,
  1351. spf->oid_fetch_tasks_alloc);
  1352. spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr] = task;
  1353. spf->oid_fetch_tasks_nr++;
  1354. return 0;
  1355. }
  1356. out:
  1357. fetch_task_release(task);
  1358. return 0;
  1359. }
  1360. int fetch_populated_submodules(struct repository *r,
  1361. const struct strvec *options,
  1362. const char *prefix, int command_line_option,
  1363. int default_option,
  1364. int quiet, int max_parallel_jobs)
  1365. {
  1366. int i;
  1367. struct submodule_parallel_fetch spf = SPF_INIT;
  1368. spf.r = r;
  1369. spf.command_line_option = command_line_option;
  1370. spf.default_option = default_option;
  1371. spf.quiet = quiet;
  1372. spf.prefix = prefix;
  1373. if (!r->worktree)
  1374. goto out;
  1375. if (repo_read_index(r) < 0)
  1376. die(_("index file corrupt"));
  1377. strvec_push(&spf.args, "fetch");
  1378. for (i = 0; i < options->nr; i++)
  1379. strvec_push(&spf.args, options->v[i]);
  1380. strvec_push(&spf.args, "--recurse-submodules-default");
  1381. /* default value, "--submodule-prefix" and its value are added later */
  1382. calculate_changed_submodule_paths(r, &spf.changed_submodule_names);
  1383. string_list_sort(&spf.changed_submodule_names);
  1384. run_processes_parallel_tr2(max_parallel_jobs,
  1385. get_next_submodule,
  1386. fetch_start_failure,
  1387. fetch_finish,
  1388. &spf,
  1389. "submodule", "parallel/fetch");
  1390. if (spf.submodules_with_errors.len > 0)
  1391. fprintf(stderr, _("Errors during submodule fetch:\n%s"),
  1392. spf.submodules_with_errors.buf);
  1393. strvec_clear(&spf.args);
  1394. out:
  1395. free_submodules_oids(&spf.changed_submodule_names);
  1396. return spf.result;
  1397. }
  1398. unsigned is_submodule_modified(const char *path, int ignore_untracked)
  1399. {
  1400. struct child_process cp = CHILD_PROCESS_INIT;
  1401. struct strbuf buf = STRBUF_INIT;
  1402. FILE *fp;
  1403. unsigned dirty_submodule = 0;
  1404. const char *git_dir;
  1405. int ignore_cp_exit_code = 0;
  1406. strbuf_addf(&buf, "%s/.git", path);
  1407. git_dir = read_gitfile(buf.buf);
  1408. if (!git_dir)
  1409. git_dir = buf.buf;
  1410. if (!is_git_directory(git_dir)) {
  1411. if (is_directory(git_dir))
  1412. die(_("'%s' not recognized as a git repository"), git_dir);
  1413. strbuf_release(&buf);
  1414. /* The submodule is not checked out, so it is not modified */
  1415. return 0;
  1416. }
  1417. strbuf_reset(&buf);
  1418. strvec_pushl(&cp.args, "status", "--porcelain=2", NULL);
  1419. if (ignore_untracked)
  1420. strvec_push(&cp.args, "-uno");
  1421. prepare_submodule_repo_env(&cp.env_array);
  1422. cp.git_cmd = 1;
  1423. cp.no_stdin = 1;
  1424. cp.out = -1;
  1425. cp.dir = path;
  1426. if (start_command(&cp))
  1427. die(_("Could not run 'git status --porcelain=2' in submodule %s"), path);
  1428. fp = xfdopen(cp.out, "r");
  1429. while (strbuf_getwholeline(&buf, fp, '\n') != EOF) {
  1430. /* regular untracked files */
  1431. if (buf.buf[0] == '?')
  1432. dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
  1433. if (buf.buf[0] == 'u' ||
  1434. buf.buf[0] == '1' ||
  1435. buf.buf[0] == '2') {
  1436. /* T = line type, XY = status, SSSS = submodule state */
  1437. if (buf.len < strlen("T XY SSSS"))
  1438. BUG("invalid status --porcelain=2 line %s",
  1439. buf.buf);
  1440. if (buf.buf[5] == 'S' && buf.buf[8] == 'U')
  1441. /* nested untracked file */
  1442. dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
  1443. if (buf.buf[0] == 'u' ||
  1444. buf.buf[0] == '2' ||
  1445. memcmp(buf.buf + 5, "S..U", 4))
  1446. /* other change */
  1447. dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
  1448. }
  1449. if ((dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
  1450. ((dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
  1451. ignore_untracked)) {
  1452. /*
  1453. * We're not interested in any further information from
  1454. * the child any more, neither output nor its exit code.
  1455. */
  1456. ignore_cp_exit_code = 1;
  1457. break;
  1458. }
  1459. }
  1460. fclose(fp);
  1461. if (finish_command(&cp) && !ignore_cp_exit_code)
  1462. die(_("'git status --porcelain=2' failed in submodule %s"), path);
  1463. strbuf_release(&buf);
  1464. return dirty_submodule;
  1465. }
  1466. int submodule_uses_gitfile(const char *path)
  1467. {
  1468. struct child_process cp = CHILD_PROCESS_INIT;
  1469. struct strbuf buf = STRBUF_INIT;
  1470. const char *git_dir;
  1471. strbuf_addf(&buf, "%s/.git", path);
  1472. git_dir = read_gitfile(buf.buf);
  1473. if (!git_dir) {
  1474. strbuf_release(&buf);
  1475. return 0;
  1476. }
  1477. strbuf_release(&buf);
  1478. /* Now test that all nested submodules use a gitfile too */
  1479. strvec_pushl(&cp.args,
  1480. "submodule", "foreach", "--quiet", "--recursive",
  1481. "test -f .git", NULL);
  1482. prepare_submodule_repo_env(&cp.env_array);
  1483. cp.git_cmd = 1;
  1484. cp.no_stdin = 1;
  1485. cp.no_stderr = 1;
  1486. cp.no_stdout = 1;
  1487. cp.dir = path;
  1488. if (run_command(&cp))
  1489. return 0;
  1490. return 1;
  1491. }
  1492. /*
  1493. * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
  1494. * when doing so.
  1495. *
  1496. * Return 1 if we'd lose data, return 0 if the removal is fine,
  1497. * and negative values for errors.
  1498. */
  1499. int bad_to_remove_submodule(const char *path, unsigned flags)
  1500. {
  1501. ssize_t len;
  1502. struct child_process cp = CHILD_PROCESS_INIT;
  1503. struct strbuf buf = STRBUF_INIT;
  1504. int ret = 0;
  1505. if (!file_exists(path) || is_empty_dir(path))
  1506. return 0;
  1507. if (!submodule_uses_gitfile(path))
  1508. return 1;
  1509. strvec_pushl(&cp.args, "status", "--porcelain",
  1510. "--ignore-submodules=none", NULL);
  1511. if (flags & SUBMODULE_REMOVAL_IGNORE_UNTRACKED)
  1512. strvec_push(&cp.args, "-uno");
  1513. else
  1514. strvec_push(&cp.args, "-uall");
  1515. if (!(flags & SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))
  1516. strvec_push(&cp.args, "--ignored");
  1517. prepare_submodule_repo_env(&cp.env_array);
  1518. cp.git_cmd = 1;
  1519. cp.no_stdin = 1;
  1520. cp.out = -1;
  1521. cp.dir = path;
  1522. if (start_command(&cp)) {
  1523. if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
  1524. die(_("could not start 'git status' in submodule '%s'"),
  1525. path);
  1526. ret = -1;
  1527. goto out;
  1528. }
  1529. len = strbuf_read(&buf, cp.out, 1024);
  1530. if (len > 2)
  1531. ret = 1;
  1532. close(cp.out);
  1533. if (finish_command(&cp)) {
  1534. if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
  1535. die(_("could not run 'git status' in submodule '%s'"),
  1536. path);
  1537. ret = -1;
  1538. }
  1539. out:
  1540. strbuf_release(&buf);
  1541. return ret;
  1542. }
  1543. void submodule_unset_core_worktree(const struct submodule *sub)
  1544. {
  1545. char *config_path = xstrfmt("%s/modules/%s/config",
  1546. get_git_dir(), sub->name);
  1547. if (git_config_set_in_file_gently(config_path, "core.worktree", NULL))
  1548. warning(_("Could not unset core.worktree setting in submodule '%s'"),
  1549. sub->path);
  1550. free(config_path);
  1551. }
  1552. static const char *get_super_prefix_or_empty(void)
  1553. {
  1554. const char *s = get_super_prefix();
  1555. if (!s)
  1556. s = "";
  1557. return s;
  1558. }
  1559. static int submodule_has_dirty_index(const struct submodule *sub)
  1560. {
  1561. struct child_process cp = CHILD_PROCESS_INIT;
  1562. prepare_submodule_repo_env(&cp.env_array);
  1563. cp.git_cmd = 1;
  1564. strvec_pushl(&cp.args, "diff-index", "--quiet",
  1565. "--cached", "HEAD", NULL);
  1566. cp.no_stdin = 1;
  1567. cp.no_stdout = 1;
  1568. cp.dir = sub->path;
  1569. if (start_command(&cp))
  1570. die(_("could not recurse into submodule '%s'"), sub->path);
  1571. return finish_command(&cp);
  1572. }
  1573. static void submodule_reset_index(const char *path)
  1574. {
  1575. struct child_process cp = CHILD_PROCESS_INIT;
  1576. prepare_submodule_repo_env(&cp.env_array);
  1577. cp.git_cmd = 1;
  1578. cp.no_stdin = 1;
  1579. cp.dir = path;
  1580. strvec_pushf(&cp.args, "--super-prefix=%s%s/",
  1581. get_super_prefix_or_empty(), path);
  1582. strvec_pushl(&cp.args, "read-tree", "-u", "--reset", NULL);
  1583. strvec_push(&cp.args, empty_tree_oid_hex());
  1584. if (run_command(&cp))
  1585. die(_("could not reset submodule index"));
  1586. }
  1587. /**
  1588. * Moves a submodule at a given path from a given head to another new head.
  1589. * For edge cases (a submodule coming into existence or removing a submodule)
  1590. * pass NULL for old or new respectively.
  1591. */
  1592. int submodule_move_head(const char *path,
  1593. const char *old_head,
  1594. const char *new_head,
  1595. unsigned flags)
  1596. {
  1597. int ret = 0;
  1598. struct child_process cp = CHILD_PROCESS_INIT;
  1599. const struct submodule *sub;
  1600. int *error_code_ptr, error_code;
  1601. if (!is_submodule_active(the_repository, path))
  1602. return 0;
  1603. if (flags & SUBMODULE_MOVE_HEAD_FORCE)
  1604. /*
  1605. * Pass non NULL pointer to is_submodule_populated_gently
  1606. * to prevent die()-ing. We'll use connect_work_tree_and_git_dir
  1607. * to fixup the submodule in the force case later.
  1608. */
  1609. error_code_ptr = &error_code;
  1610. else
  1611. error_code_ptr = NULL;
  1612. if (old_head && !is_submodule_populated_gently(path, error_code_ptr))
  1613. return 0;
  1614. sub = submodule_from_path(the_repository, &null_oid, path);
  1615. if (!sub)
  1616. BUG("could not get submodule information for '%s'", path);
  1617. if (old_head && !(flags & SUBMODULE_MOVE_HEAD_FORCE)) {
  1618. /* Check if the submodule has a dirty index. */
  1619. if (submodule_has_dirty_index(sub))
  1620. return error(_("submodule '%s' has dirty index"), path);
  1621. }
  1622. if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
  1623. if (old_head) {
  1624. if (!submodule_uses_gitfile(path))
  1625. absorb_git_dir_into_superproject(path,
  1626. ABSORB_GITDIR_RECURSE_SUBMODULES);
  1627. } else {
  1628. char *gitdir = xstrfmt("%s/modules/%s",
  1629. get_git_dir(), sub->name);
  1630. connect_work_tree_and_git_dir(path, gitdir, 0);
  1631. free(gitdir);
  1632. /* make sure the index is clean as well */
  1633. submodule_reset_index(path);
  1634. }
  1635. if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
  1636. char *gitdir = xstrfmt("%s/modules/%s",
  1637. get_git_dir(), sub->name);
  1638. connect_work_tree_and_git_dir(path, gitdir, 1);
  1639. free(gitdir);
  1640. }
  1641. }
  1642. prepare_submodule_repo_env(&cp.env_array);
  1643. cp.git_cmd = 1;
  1644. cp.no_stdin = 1;
  1645. cp.dir = path;
  1646. strvec_pushf(&cp.args, "--super-prefix=%s%s/",
  1647. get_super_prefix_or_empty(), path);
  1648. strvec_pushl(&cp.args, "read-tree", "--recurse-submodules", NULL);
  1649. if (flags & SUBMODULE_MOVE_HEAD_DRY_RUN)
  1650. strvec_push(&cp.args, "-n");
  1651. else
  1652. strvec_push(&cp.args, "-u");
  1653. if (flags & SUBMODULE_MOVE_HEAD_FORCE)
  1654. strvec_push(&cp.args, "--reset");
  1655. else
  1656. strvec_push(&cp.args, "-m");
  1657. if (!(flags & SUBMODULE_MOVE_HEAD_FORCE))
  1658. strvec_push(&cp.args, old_head ? old_head : empty_tree_oid_hex());
  1659. strvec_push(&cp.args, new_head ? new_head : empty_tree_oid_hex());
  1660. if (run_command(&cp)) {
  1661. ret = error(_("Submodule '%s' could not be updated."), path);
  1662. goto out;
  1663. }
  1664. if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
  1665. if (new_head) {
  1666. child_process_init(&cp);
  1667. /* also set the HEAD accordingly */
  1668. cp.git_cmd = 1;
  1669. cp.no_stdin = 1;
  1670. cp.dir = path;
  1671. prepare_submodule_repo_env(&cp.env_array);
  1672. strvec_pushl(&cp.args, "update-ref", "HEAD",
  1673. "--no-deref", new_head, NULL);
  1674. if (run_command(&cp)) {
  1675. ret = -1;
  1676. goto out;
  1677. }
  1678. } else {
  1679. struct strbuf sb = STRBUF_INIT;
  1680. strbuf_addf(&sb, "%s/.git", path);
  1681. unlink_or_warn(sb.buf);
  1682. strbuf_release(&sb);
  1683. if (is_empty_dir(path))
  1684. rmdir_or_warn(path);
  1685. submodule_unset_core_worktree(sub);
  1686. }
  1687. }
  1688. out:
  1689. return ret;
  1690. }
  1691. int validate_submodule_git_dir(char *git_dir, const char *submodule_name)
  1692. {
  1693. size_t len = strlen(git_dir), suffix_len = strlen(submodule_name);
  1694. char *p;
  1695. int ret = 0;
  1696. if (len <= suffix_len || (p = git_dir + len - suffix_len)[-1] != '/' ||
  1697. strcmp(p, submodule_name))
  1698. BUG("submodule name '%s' not a suffix of git dir '%s'",
  1699. submodule_name, git_dir);
  1700. /*
  1701. * We prevent the contents of sibling submodules' git directories to
  1702. * clash.
  1703. *
  1704. * Example: having a submodule named `hippo` and another one named
  1705. * `hippo/hooks` would result in the git directories
  1706. * `.git/modules/hippo/` and `.git/modules/hippo/hooks/`, respectively,
  1707. * but the latter directory is already designated to contain the hooks
  1708. * of the former.
  1709. */
  1710. for (; *p; p++) {
  1711. if (is_dir_sep(*p)) {
  1712. char c = *p;
  1713. *p = '\0';
  1714. if (is_git_directory(git_dir))
  1715. ret = -1;
  1716. *p = c;
  1717. if (ret < 0)
  1718. return error(_("submodule git dir '%s' is "
  1719. "inside git dir '%.*s'"),
  1720. git_dir,
  1721. (int)(p - git_dir), git_dir);
  1722. }
  1723. }
  1724. return 0;
  1725. }
  1726. /*
  1727. * Embeds a single submodules git directory into the superprojects git dir,
  1728. * non recursively.
  1729. */
  1730. static void relocate_single_git_dir_into_superproject(const char *path)
  1731. {
  1732. char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
  1733. char *new_git_dir;
  1734. const struct submodule *sub;
  1735. if (submodule_uses_worktrees(path))
  1736. die(_("relocate_gitdir for submodule '%s' with "
  1737. "more than one worktree not supported"), path);
  1738. old_git_dir = xstrfmt("%s/.git", path);
  1739. if (read_gitfile(old_git_dir))
  1740. /* If it is an actual gitfile, it doesn't need migration. */
  1741. return;
  1742. real_old_git_dir = real_pathdup(old_git_dir, 1);
  1743. sub = submodule_from_path(the_repository, &null_oid, path);
  1744. if (!sub)
  1745. die(_("could not lookup name for submodule '%s'"), path);
  1746. new_git_dir = git_pathdup("modules/%s", sub->name);
  1747. if (validate_submodule_git_dir(new_git_dir, sub->name) < 0)
  1748. die(_("refusing to move '%s' into an existing git dir"),
  1749. real_old_git_dir);
  1750. if (safe_create_leading_directories_const(new_git_dir) < 0)
  1751. die(_("could not create directory '%s'"), new_git_dir);
  1752. real_new_git_dir = real_pathdup(new_git_dir, 1);
  1753. free(new_git_dir);
  1754. fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
  1755. get_super_prefix_or_empty(), path,
  1756. real_old_git_dir, real_new_git_dir);
  1757. relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
  1758. free(old_git_dir);
  1759. free(real_old_git_dir);
  1760. free(real_new_git_dir);
  1761. }
  1762. /*
  1763. * Migrate the git directory of the submodule given by path from
  1764. * having its git directory within the working tree to the git dir nested
  1765. * in its superprojects git dir under modules/.
  1766. */
  1767. void absorb_git_dir_into_superproject(const char *path,
  1768. unsigned flags)
  1769. {
  1770. int err_code;
  1771. const char *sub_git_dir;
  1772. struct strbuf gitdir = STRBUF_INIT;
  1773. strbuf_addf(&gitdir, "%s/.git", path);
  1774. sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code);
  1775. /* Not populated? */
  1776. if (!sub_git_dir) {
  1777. const struct submodule *sub;
  1778. if (err_code == READ_GITFILE_ERR_STAT_FAILED) {
  1779. /* unpopulated as expected */
  1780. strbuf_release(&gitdir);
  1781. return;
  1782. }
  1783. if (err_code != READ_GITFILE_ERR_NOT_A_REPO)
  1784. /* We don't know what broke here. */
  1785. read_gitfile_error_die(err_code, path, NULL);
  1786. /*
  1787. * Maybe populated, but no git directory was found?
  1788. * This can happen if the superproject is a submodule
  1789. * itself and was just absorbed. The absorption of the
  1790. * superproject did not rewrite the git file links yet,
  1791. * fix it now.
  1792. */
  1793. sub = submodule_from_path(the_repository, &null_oid, path);
  1794. if (!sub)
  1795. die(_("could not lookup name for submodule '%s'"), path);
  1796. connect_work_tree_and_git_dir(path,
  1797. git_path("modules/%s", sub->name), 0);
  1798. } else {
  1799. /* Is it already absorbed into the superprojects git dir? */
  1800. char *real_sub_git_dir = real_pathdup(sub_git_dir, 1);
  1801. char *real_common_git_dir = real_pathdup(get_git_common_dir(), 1);
  1802. if (!starts_with(real_sub_git_dir, real_common_git_dir))
  1803. relocate_single_git_dir_into_superproject(path);
  1804. free(real_sub_git_dir);
  1805. free(real_common_git_dir);
  1806. }
  1807. strbuf_release(&gitdir);
  1808. if (flags & ABSORB_GITDIR_RECURSE_SUBMODULES) {
  1809. struct child_process cp = CHILD_PROCESS_INIT;
  1810. struct strbuf sb = STRBUF_INIT;
  1811. if (flags & ~ABSORB_GITDIR_RECURSE_SUBMODULES)
  1812. BUG("we don't know how to pass the flags down?");
  1813. strbuf_addstr(&sb, get_super_prefix_or_empty());
  1814. strbuf_addstr(&sb, path);
  1815. strbuf_addch(&sb, '/');
  1816. cp.dir = path;
  1817. cp.git_cmd = 1;
  1818. cp.no_stdin = 1;
  1819. strvec_pushl(&cp.args, "--super-prefix", sb.buf,
  1820. "submodule--helper",
  1821. "absorb-git-dirs", NULL);
  1822. prepare_submodule_repo_env(&cp.env_array);
  1823. if (run_command(&cp))
  1824. die(_("could not recurse into submodule '%s'"), path);
  1825. strbuf_release(&sb);
  1826. }
  1827. }
  1828. int get_superproject_working_tree(struct strbuf *buf)
  1829. {
  1830. struct child_process cp = CHILD_PROCESS_INIT;
  1831. struct strbuf sb = STRBUF_INIT;
  1832. struct strbuf one_up = STRBUF_INIT;
  1833. const char *cwd = xgetcwd();
  1834. int ret = 0;
  1835. const char *subpath;
  1836. int code;
  1837. ssize_t len;
  1838. if (!is_inside_work_tree())
  1839. /*
  1840. * FIXME:
  1841. * We might have a superproject, but it is harder
  1842. * to determine.
  1843. */
  1844. return 0;
  1845. if (!strbuf_realpath(&one_up, "../", 0))
  1846. return 0;
  1847. subpath = relative_path(cwd, one_up.buf, &sb);
  1848. strbuf_release(&one_up);
  1849. prepare_submodule_repo_env(&cp.env_array);
  1850. strvec_pop(&cp.env_array);
  1851. strvec_pushl(&cp.args, "--literal-pathspecs", "-C", "..",
  1852. "ls-files", "-z", "--stage", "--full-name", "--",
  1853. subpath, NULL);
  1854. strbuf_reset(&sb);
  1855. cp.no_stdin = 1;
  1856. cp.no_stderr = 1;
  1857. cp.out = -1;
  1858. cp.git_cmd = 1;
  1859. if (start_command(&cp))
  1860. die(_("could not start ls-files in .."));
  1861. len = strbuf_read(&sb, cp.out, PATH_MAX);
  1862. close(cp.out);
  1863. if (starts_with(sb.buf, "160000")) {
  1864. int super_sub_len;
  1865. int cwd_len = strlen(cwd);
  1866. char *super_sub, *super_wt;
  1867. /*
  1868. * There is a superproject having this repo as a submodule.
  1869. * The format is <mode> SP <hash> SP <stage> TAB <full name> \0,
  1870. * We're only interested in the name after the tab.
  1871. */
  1872. super_sub = strchr(sb.buf, '\t') + 1;
  1873. super_sub_len = strlen(super_sub);
  1874. if (super_sub_len > cwd_len ||
  1875. strcmp(&cwd[cwd_len - super_sub_len], super_sub))
  1876. BUG("returned path string doesn't match cwd?");
  1877. super_wt = xstrdup(cwd);
  1878. super_wt[cwd_len - super_sub_len] = '\0';
  1879. strbuf_realpath(buf, super_wt, 1);
  1880. ret = 1;
  1881. free(super_wt);
  1882. }
  1883. strbuf_release(&sb);
  1884. code = finish_command(&cp);
  1885. if (code == 128)
  1886. /* '../' is not a git repository */
  1887. return 0;
  1888. if (code == 0 && len == 0)
  1889. /* There is an unrelated git repository at '../' */
  1890. return 0;
  1891. if (code)
  1892. die(_("ls-tree returned unexpected return code %d"), code);
  1893. return ret;
  1894. }
  1895. /*
  1896. * Put the gitdir for a submodule (given relative to the main
  1897. * repository worktree) into `buf`, or return -1 on error.
  1898. */
  1899. int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
  1900. {
  1901. const struct submodule *sub;
  1902. const char *git_dir;
  1903. int ret = 0;
  1904. strbuf_reset(buf);
  1905. strbuf_addstr(buf, submodule);
  1906. strbuf_complete(buf, '/');
  1907. strbuf_addstr(buf, ".git");
  1908. git_dir = read_gitfile(buf->buf);
  1909. if (git_dir) {
  1910. strbuf_reset(buf);
  1911. strbuf_addstr(buf, git_dir);
  1912. }
  1913. if (!is_git_directory(buf->buf)) {
  1914. sub = submodule_from_path(the_repository, &null_oid, submodule);
  1915. if (!sub) {
  1916. ret = -1;
  1917. goto cleanup;
  1918. }
  1919. strbuf_reset(buf);
  1920. strbuf_git_path(buf, "%s/%s", "modules", sub->name);
  1921. }
  1922. cleanup:
  1923. return ret;
  1924. }