submodule--helper.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801
  1. #define USE_THE_INDEX_COMPATIBILITY_MACROS
  2. #include "builtin.h"
  3. #include "repository.h"
  4. #include "cache.h"
  5. #include "config.h"
  6. #include "parse-options.h"
  7. #include "quote.h"
  8. #include "pathspec.h"
  9. #include "dir.h"
  10. #include "submodule.h"
  11. #include "submodule-config.h"
  12. #include "string-list.h"
  13. #include "run-command.h"
  14. #include "remote.h"
  15. #include "refs.h"
  16. #include "refspec.h"
  17. #include "connect.h"
  18. #include "revision.h"
  19. #include "diffcore.h"
  20. #include "diff.h"
  21. #include "object-store.h"
  22. #include "dir.h"
  23. #include "advice.h"
  24. #define OPT_QUIET (1 << 0)
  25. #define OPT_CACHED (1 << 1)
  26. #define OPT_RECURSIVE (1 << 2)
  27. #define OPT_FORCE (1 << 3)
  28. typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
  29. void *cb_data);
  30. static char *get_default_remote(void)
  31. {
  32. char *dest = NULL, *ret;
  33. struct strbuf sb = STRBUF_INIT;
  34. const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
  35. if (!refname)
  36. die(_("No such ref: %s"), "HEAD");
  37. /* detached HEAD */
  38. if (!strcmp(refname, "HEAD"))
  39. return xstrdup("origin");
  40. if (!skip_prefix(refname, "refs/heads/", &refname))
  41. die(_("Expecting a full ref name, got %s"), refname);
  42. strbuf_addf(&sb, "branch.%s.remote", refname);
  43. if (git_config_get_string(sb.buf, &dest))
  44. ret = xstrdup("origin");
  45. else
  46. ret = dest;
  47. strbuf_release(&sb);
  48. return ret;
  49. }
  50. static int print_default_remote(int argc, const char **argv, const char *prefix)
  51. {
  52. char *remote;
  53. if (argc != 1)
  54. die(_("submodule--helper print-default-remote takes no arguments"));
  55. remote = get_default_remote();
  56. if (remote)
  57. printf("%s\n", remote);
  58. free(remote);
  59. return 0;
  60. }
  61. static int starts_with_dot_slash(const char *str)
  62. {
  63. return str[0] == '.' && is_dir_sep(str[1]);
  64. }
  65. static int starts_with_dot_dot_slash(const char *str)
  66. {
  67. return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]);
  68. }
  69. /*
  70. * Returns 1 if it was the last chop before ':'.
  71. */
  72. static int chop_last_dir(char **remoteurl, int is_relative)
  73. {
  74. char *rfind = find_last_dir_sep(*remoteurl);
  75. if (rfind) {
  76. *rfind = '\0';
  77. return 0;
  78. }
  79. rfind = strrchr(*remoteurl, ':');
  80. if (rfind) {
  81. *rfind = '\0';
  82. return 1;
  83. }
  84. if (is_relative || !strcmp(".", *remoteurl))
  85. die(_("cannot strip one component off url '%s'"),
  86. *remoteurl);
  87. free(*remoteurl);
  88. *remoteurl = xstrdup(".");
  89. return 0;
  90. }
  91. /*
  92. * The `url` argument is the URL that navigates to the submodule origin
  93. * repo. When relative, this URL is relative to the superproject origin
  94. * URL repo. The `up_path` argument, if specified, is the relative
  95. * path that navigates from the submodule working tree to the superproject
  96. * working tree. Returns the origin URL of the submodule.
  97. *
  98. * Return either an absolute URL or filesystem path (if the superproject
  99. * origin URL is an absolute URL or filesystem path, respectively) or a
  100. * relative file system path (if the superproject origin URL is a relative
  101. * file system path).
  102. *
  103. * When the output is a relative file system path, the path is either
  104. * relative to the submodule working tree, if up_path is specified, or to
  105. * the superproject working tree otherwise.
  106. *
  107. * NEEDSWORK: This works incorrectly on the domain and protocol part.
  108. * remote_url url outcome expectation
  109. * http://a.com/b ../c http://a.com/c as is
  110. * http://a.com/b/ ../c http://a.com/c same as previous line, but
  111. * ignore trailing slash in url
  112. * http://a.com/b ../../c http://c error out
  113. * http://a.com/b ../../../c http:/c error out
  114. * http://a.com/b ../../../../c http:c error out
  115. * http://a.com/b ../../../../../c .:c error out
  116. * NEEDSWORK: Given how chop_last_dir() works, this function is broken
  117. * when a local part has a colon in its path component, too.
  118. */
  119. static char *relative_url(const char *remote_url,
  120. const char *url,
  121. const char *up_path)
  122. {
  123. int is_relative = 0;
  124. int colonsep = 0;
  125. char *out;
  126. char *remoteurl = xstrdup(remote_url);
  127. struct strbuf sb = STRBUF_INIT;
  128. size_t len = strlen(remoteurl);
  129. if (is_dir_sep(remoteurl[len-1]))
  130. remoteurl[len-1] = '\0';
  131. if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl))
  132. is_relative = 0;
  133. else {
  134. is_relative = 1;
  135. /*
  136. * Prepend a './' to ensure all relative
  137. * remoteurls start with './' or '../'
  138. */
  139. if (!starts_with_dot_slash(remoteurl) &&
  140. !starts_with_dot_dot_slash(remoteurl)) {
  141. strbuf_reset(&sb);
  142. strbuf_addf(&sb, "./%s", remoteurl);
  143. free(remoteurl);
  144. remoteurl = strbuf_detach(&sb, NULL);
  145. }
  146. }
  147. /*
  148. * When the url starts with '../', remove that and the
  149. * last directory in remoteurl.
  150. */
  151. while (url) {
  152. if (starts_with_dot_dot_slash(url)) {
  153. url += 3;
  154. colonsep |= chop_last_dir(&remoteurl, is_relative);
  155. } else if (starts_with_dot_slash(url))
  156. url += 2;
  157. else
  158. break;
  159. }
  160. strbuf_reset(&sb);
  161. strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url);
  162. if (ends_with(url, "/"))
  163. strbuf_setlen(&sb, sb.len - 1);
  164. free(remoteurl);
  165. if (starts_with_dot_slash(sb.buf))
  166. out = xstrdup(sb.buf + 2);
  167. else
  168. out = xstrdup(sb.buf);
  169. strbuf_reset(&sb);
  170. if (!up_path || !is_relative)
  171. return out;
  172. strbuf_addf(&sb, "%s%s", up_path, out);
  173. free(out);
  174. return strbuf_detach(&sb, NULL);
  175. }
  176. static int resolve_relative_url(int argc, const char **argv, const char *prefix)
  177. {
  178. char *remoteurl = NULL;
  179. char *remote = get_default_remote();
  180. const char *up_path = NULL;
  181. char *res;
  182. const char *url;
  183. struct strbuf sb = STRBUF_INIT;
  184. if (argc != 2 && argc != 3)
  185. die("resolve-relative-url only accepts one or two arguments");
  186. url = argv[1];
  187. strbuf_addf(&sb, "remote.%s.url", remote);
  188. free(remote);
  189. if (git_config_get_string(sb.buf, &remoteurl))
  190. /* the repository is its own authoritative upstream */
  191. remoteurl = xgetcwd();
  192. if (argc == 3)
  193. up_path = argv[2];
  194. res = relative_url(remoteurl, url, up_path);
  195. puts(res);
  196. free(res);
  197. free(remoteurl);
  198. return 0;
  199. }
  200. static int resolve_relative_url_test(int argc, const char **argv, const char *prefix)
  201. {
  202. char *remoteurl, *res;
  203. const char *up_path, *url;
  204. if (argc != 4)
  205. die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
  206. up_path = argv[1];
  207. remoteurl = xstrdup(argv[2]);
  208. url = argv[3];
  209. if (!strcmp(up_path, "(null)"))
  210. up_path = NULL;
  211. res = relative_url(remoteurl, url, up_path);
  212. puts(res);
  213. free(res);
  214. free(remoteurl);
  215. return 0;
  216. }
  217. /* the result should be freed by the caller. */
  218. static char *get_submodule_displaypath(const char *path, const char *prefix)
  219. {
  220. const char *super_prefix = get_super_prefix();
  221. if (prefix && super_prefix) {
  222. BUG("cannot have prefix '%s' and superprefix '%s'",
  223. prefix, super_prefix);
  224. } else if (prefix) {
  225. struct strbuf sb = STRBUF_INIT;
  226. char *displaypath = xstrdup(relative_path(path, prefix, &sb));
  227. strbuf_release(&sb);
  228. return displaypath;
  229. } else if (super_prefix) {
  230. return xstrfmt("%s%s", super_prefix, path);
  231. } else {
  232. return xstrdup(path);
  233. }
  234. }
  235. static char *compute_rev_name(const char *sub_path, const char* object_id)
  236. {
  237. struct strbuf sb = STRBUF_INIT;
  238. const char ***d;
  239. static const char *describe_bare[] = { NULL };
  240. static const char *describe_tags[] = { "--tags", NULL };
  241. static const char *describe_contains[] = { "--contains", NULL };
  242. static const char *describe_all_always[] = { "--all", "--always", NULL };
  243. static const char **describe_argv[] = { describe_bare, describe_tags,
  244. describe_contains,
  245. describe_all_always, NULL };
  246. for (d = describe_argv; *d; d++) {
  247. struct child_process cp = CHILD_PROCESS_INIT;
  248. prepare_submodule_repo_env(&cp.env_array);
  249. cp.dir = sub_path;
  250. cp.git_cmd = 1;
  251. cp.no_stderr = 1;
  252. strvec_push(&cp.args, "describe");
  253. strvec_pushv(&cp.args, *d);
  254. strvec_push(&cp.args, object_id);
  255. if (!capture_command(&cp, &sb, 0)) {
  256. strbuf_strip_suffix(&sb, "\n");
  257. return strbuf_detach(&sb, NULL);
  258. }
  259. }
  260. strbuf_release(&sb);
  261. return NULL;
  262. }
  263. struct module_list {
  264. const struct cache_entry **entries;
  265. int alloc, nr;
  266. };
  267. #define MODULE_LIST_INIT { NULL, 0, 0 }
  268. static int module_list_compute(int argc, const char **argv,
  269. const char *prefix,
  270. struct pathspec *pathspec,
  271. struct module_list *list)
  272. {
  273. int i, result = 0;
  274. char *ps_matched = NULL;
  275. parse_pathspec(pathspec, 0,
  276. PATHSPEC_PREFER_FULL,
  277. prefix, argv);
  278. if (pathspec->nr)
  279. ps_matched = xcalloc(pathspec->nr, 1);
  280. if (read_cache() < 0)
  281. die(_("index file corrupt"));
  282. for (i = 0; i < active_nr; i++) {
  283. const struct cache_entry *ce = active_cache[i];
  284. if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce),
  285. 0, ps_matched, 1) ||
  286. !S_ISGITLINK(ce->ce_mode))
  287. continue;
  288. ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
  289. list->entries[list->nr++] = ce;
  290. while (i + 1 < active_nr &&
  291. !strcmp(ce->name, active_cache[i + 1]->name))
  292. /*
  293. * Skip entries with the same name in different stages
  294. * to make sure an entry is returned only once.
  295. */
  296. i++;
  297. }
  298. if (ps_matched && report_path_error(ps_matched, pathspec))
  299. result = -1;
  300. free(ps_matched);
  301. return result;
  302. }
  303. static void module_list_active(struct module_list *list)
  304. {
  305. int i;
  306. struct module_list active_modules = MODULE_LIST_INIT;
  307. for (i = 0; i < list->nr; i++) {
  308. const struct cache_entry *ce = list->entries[i];
  309. if (!is_submodule_active(the_repository, ce->name))
  310. continue;
  311. ALLOC_GROW(active_modules.entries,
  312. active_modules.nr + 1,
  313. active_modules.alloc);
  314. active_modules.entries[active_modules.nr++] = ce;
  315. }
  316. free(list->entries);
  317. *list = active_modules;
  318. }
  319. static char *get_up_path(const char *path)
  320. {
  321. int i;
  322. struct strbuf sb = STRBUF_INIT;
  323. for (i = count_slashes(path); i; i--)
  324. strbuf_addstr(&sb, "../");
  325. /*
  326. * Check if 'path' ends with slash or not
  327. * for having the same output for dir/sub_dir
  328. * and dir/sub_dir/
  329. */
  330. if (!is_dir_sep(path[strlen(path) - 1]))
  331. strbuf_addstr(&sb, "../");
  332. return strbuf_detach(&sb, NULL);
  333. }
  334. static int module_list(int argc, const char **argv, const char *prefix)
  335. {
  336. int i;
  337. struct pathspec pathspec;
  338. struct module_list list = MODULE_LIST_INIT;
  339. struct option module_list_options[] = {
  340. OPT_STRING(0, "prefix", &prefix,
  341. N_("path"),
  342. N_("alternative anchor for relative paths")),
  343. OPT_END()
  344. };
  345. const char *const git_submodule_helper_usage[] = {
  346. N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
  347. NULL
  348. };
  349. argc = parse_options(argc, argv, prefix, module_list_options,
  350. git_submodule_helper_usage, 0);
  351. if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
  352. return 1;
  353. for (i = 0; i < list.nr; i++) {
  354. const struct cache_entry *ce = list.entries[i];
  355. if (ce_stage(ce))
  356. printf("%06o %s U\t", ce->ce_mode, oid_to_hex(&null_oid));
  357. else
  358. printf("%06o %s %d\t", ce->ce_mode,
  359. oid_to_hex(&ce->oid), ce_stage(ce));
  360. fprintf(stdout, "%s\n", ce->name);
  361. }
  362. return 0;
  363. }
  364. static void for_each_listed_submodule(const struct module_list *list,
  365. each_submodule_fn fn, void *cb_data)
  366. {
  367. int i;
  368. for (i = 0; i < list->nr; i++)
  369. fn(list->entries[i], cb_data);
  370. }
  371. struct foreach_cb {
  372. int argc;
  373. const char **argv;
  374. const char *prefix;
  375. int quiet;
  376. int recursive;
  377. };
  378. #define FOREACH_CB_INIT { 0 }
  379. static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
  380. void *cb_data)
  381. {
  382. struct foreach_cb *info = cb_data;
  383. const char *path = list_item->name;
  384. const struct object_id *ce_oid = &list_item->oid;
  385. const struct submodule *sub;
  386. struct child_process cp = CHILD_PROCESS_INIT;
  387. char *displaypath;
  388. displaypath = get_submodule_displaypath(path, info->prefix);
  389. sub = submodule_from_path(the_repository, &null_oid, path);
  390. if (!sub)
  391. die(_("No url found for submodule path '%s' in .gitmodules"),
  392. displaypath);
  393. if (!is_submodule_populated_gently(path, NULL))
  394. goto cleanup;
  395. prepare_submodule_repo_env(&cp.env_array);
  396. /*
  397. * For the purpose of executing <command> in the submodule,
  398. * separate shell is used for the purpose of running the
  399. * child process.
  400. */
  401. cp.use_shell = 1;
  402. cp.dir = path;
  403. /*
  404. * NEEDSWORK: the command currently has access to the variables $name,
  405. * $sm_path, $displaypath, $sha1 and $toplevel only when the command
  406. * contains a single argument. This is done for maintaining a faithful
  407. * translation from shell script.
  408. */
  409. if (info->argc == 1) {
  410. char *toplevel = xgetcwd();
  411. struct strbuf sb = STRBUF_INIT;
  412. strvec_pushf(&cp.env_array, "name=%s", sub->name);
  413. strvec_pushf(&cp.env_array, "sm_path=%s", path);
  414. strvec_pushf(&cp.env_array, "displaypath=%s", displaypath);
  415. strvec_pushf(&cp.env_array, "sha1=%s",
  416. oid_to_hex(ce_oid));
  417. strvec_pushf(&cp.env_array, "toplevel=%s", toplevel);
  418. /*
  419. * Since the path variable was accessible from the script
  420. * before porting, it is also made available after porting.
  421. * The environment variable "PATH" has a very special purpose
  422. * on windows. And since environment variables are
  423. * case-insensitive in windows, it interferes with the
  424. * existing PATH variable. Hence, to avoid that, we expose
  425. * path via the args strvec and not via env_array.
  426. */
  427. sq_quote_buf(&sb, path);
  428. strvec_pushf(&cp.args, "path=%s; %s",
  429. sb.buf, info->argv[0]);
  430. strbuf_release(&sb);
  431. free(toplevel);
  432. } else {
  433. strvec_pushv(&cp.args, info->argv);
  434. }
  435. if (!info->quiet)
  436. printf(_("Entering '%s'\n"), displaypath);
  437. if (info->argv[0] && run_command(&cp))
  438. die(_("run_command returned non-zero status for %s\n."),
  439. displaypath);
  440. if (info->recursive) {
  441. struct child_process cpr = CHILD_PROCESS_INIT;
  442. cpr.git_cmd = 1;
  443. cpr.dir = path;
  444. prepare_submodule_repo_env(&cpr.env_array);
  445. strvec_pushl(&cpr.args, "--super-prefix", NULL);
  446. strvec_pushf(&cpr.args, "%s/", displaypath);
  447. strvec_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
  448. NULL);
  449. if (info->quiet)
  450. strvec_push(&cpr.args, "--quiet");
  451. strvec_push(&cpr.args, "--");
  452. strvec_pushv(&cpr.args, info->argv);
  453. if (run_command(&cpr))
  454. die(_("run_command returned non-zero status while "
  455. "recursing in the nested submodules of %s\n."),
  456. displaypath);
  457. }
  458. cleanup:
  459. free(displaypath);
  460. }
  461. static int module_foreach(int argc, const char **argv, const char *prefix)
  462. {
  463. struct foreach_cb info = FOREACH_CB_INIT;
  464. struct pathspec pathspec;
  465. struct module_list list = MODULE_LIST_INIT;
  466. struct option module_foreach_options[] = {
  467. OPT__QUIET(&info.quiet, N_("Suppress output of entering each submodule command")),
  468. OPT_BOOL(0, "recursive", &info.recursive,
  469. N_("Recurse into nested submodules")),
  470. OPT_END()
  471. };
  472. const char *const git_submodule_helper_usage[] = {
  473. N_("git submodule--helper foreach [--quiet] [--recursive] [--] <command>"),
  474. NULL
  475. };
  476. argc = parse_options(argc, argv, prefix, module_foreach_options,
  477. git_submodule_helper_usage, 0);
  478. if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0)
  479. return 1;
  480. info.argc = argc;
  481. info.argv = argv;
  482. info.prefix = prefix;
  483. for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info);
  484. return 0;
  485. }
  486. static char *compute_submodule_clone_url(const char *rel_url)
  487. {
  488. char *remoteurl, *relurl;
  489. char *remote = get_default_remote();
  490. struct strbuf remotesb = STRBUF_INIT;
  491. strbuf_addf(&remotesb, "remote.%s.url", remote);
  492. if (git_config_get_string(remotesb.buf, &remoteurl)) {
  493. warning(_("could not look up configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
  494. remoteurl = xgetcwd();
  495. }
  496. relurl = relative_url(remoteurl, rel_url, NULL);
  497. free(remote);
  498. free(remoteurl);
  499. strbuf_release(&remotesb);
  500. return relurl;
  501. }
  502. struct init_cb {
  503. const char *prefix;
  504. unsigned int flags;
  505. };
  506. #define INIT_CB_INIT { NULL, 0 }
  507. static void init_submodule(const char *path, const char *prefix,
  508. unsigned int flags)
  509. {
  510. const struct submodule *sub;
  511. struct strbuf sb = STRBUF_INIT;
  512. char *upd = NULL, *url = NULL, *displaypath;
  513. displaypath = get_submodule_displaypath(path, prefix);
  514. sub = submodule_from_path(the_repository, &null_oid, path);
  515. if (!sub)
  516. die(_("No url found for submodule path '%s' in .gitmodules"),
  517. displaypath);
  518. /*
  519. * NEEDSWORK: In a multi-working-tree world, this needs to be
  520. * set in the per-worktree config.
  521. *
  522. * Set active flag for the submodule being initialized
  523. */
  524. if (!is_submodule_active(the_repository, path)) {
  525. strbuf_addf(&sb, "submodule.%s.active", sub->name);
  526. git_config_set_gently(sb.buf, "true");
  527. strbuf_reset(&sb);
  528. }
  529. /*
  530. * Copy url setting when it is not set yet.
  531. * To look up the url in .git/config, we must not fall back to
  532. * .gitmodules, so look it up directly.
  533. */
  534. strbuf_addf(&sb, "submodule.%s.url", sub->name);
  535. if (git_config_get_string(sb.buf, &url)) {
  536. if (!sub->url)
  537. die(_("No url found for submodule path '%s' in .gitmodules"),
  538. displaypath);
  539. url = xstrdup(sub->url);
  540. /* Possibly a url relative to parent */
  541. if (starts_with_dot_dot_slash(url) ||
  542. starts_with_dot_slash(url)) {
  543. char *oldurl = url;
  544. url = compute_submodule_clone_url(oldurl);
  545. free(oldurl);
  546. }
  547. if (git_config_set_gently(sb.buf, url))
  548. die(_("Failed to register url for submodule path '%s'"),
  549. displaypath);
  550. if (!(flags & OPT_QUIET))
  551. fprintf(stderr,
  552. _("Submodule '%s' (%s) registered for path '%s'\n"),
  553. sub->name, url, displaypath);
  554. }
  555. strbuf_reset(&sb);
  556. /* Copy "update" setting when it is not set yet */
  557. strbuf_addf(&sb, "submodule.%s.update", sub->name);
  558. if (git_config_get_string(sb.buf, &upd) &&
  559. sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
  560. if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
  561. fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
  562. sub->name);
  563. upd = xstrdup("none");
  564. } else
  565. upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
  566. if (git_config_set_gently(sb.buf, upd))
  567. die(_("Failed to register update mode for submodule path '%s'"), displaypath);
  568. }
  569. strbuf_release(&sb);
  570. free(displaypath);
  571. free(url);
  572. free(upd);
  573. }
  574. static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
  575. {
  576. struct init_cb *info = cb_data;
  577. init_submodule(list_item->name, info->prefix, info->flags);
  578. }
  579. static int module_init(int argc, const char **argv, const char *prefix)
  580. {
  581. struct init_cb info = INIT_CB_INIT;
  582. struct pathspec pathspec;
  583. struct module_list list = MODULE_LIST_INIT;
  584. int quiet = 0;
  585. struct option module_init_options[] = {
  586. OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
  587. OPT_END()
  588. };
  589. const char *const git_submodule_helper_usage[] = {
  590. N_("git submodule--helper init [<options>] [<path>]"),
  591. NULL
  592. };
  593. argc = parse_options(argc, argv, prefix, module_init_options,
  594. git_submodule_helper_usage, 0);
  595. if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
  596. return 1;
  597. /*
  598. * If there are no path args and submodule.active is set then,
  599. * by default, only initialize 'active' modules.
  600. */
  601. if (!argc && git_config_get_value_multi("submodule.active"))
  602. module_list_active(&list);
  603. info.prefix = prefix;
  604. if (quiet)
  605. info.flags |= OPT_QUIET;
  606. for_each_listed_submodule(&list, init_submodule_cb, &info);
  607. return 0;
  608. }
  609. struct status_cb {
  610. const char *prefix;
  611. unsigned int flags;
  612. };
  613. #define STATUS_CB_INIT { NULL, 0 }
  614. static void print_status(unsigned int flags, char state, const char *path,
  615. const struct object_id *oid, const char *displaypath)
  616. {
  617. if (flags & OPT_QUIET)
  618. return;
  619. printf("%c%s %s", state, oid_to_hex(oid), displaypath);
  620. if (state == ' ' || state == '+') {
  621. const char *name = compute_rev_name(path, oid_to_hex(oid));
  622. if (name)
  623. printf(" (%s)", name);
  624. }
  625. printf("\n");
  626. }
  627. static int handle_submodule_head_ref(const char *refname,
  628. const struct object_id *oid, int flags,
  629. void *cb_data)
  630. {
  631. struct object_id *output = cb_data;
  632. if (oid)
  633. oidcpy(output, oid);
  634. return 0;
  635. }
  636. static void status_submodule(const char *path, const struct object_id *ce_oid,
  637. unsigned int ce_flags, const char *prefix,
  638. unsigned int flags)
  639. {
  640. char *displaypath;
  641. struct strvec diff_files_args = STRVEC_INIT;
  642. struct rev_info rev;
  643. int diff_files_result;
  644. struct strbuf buf = STRBUF_INIT;
  645. const char *git_dir;
  646. if (!submodule_from_path(the_repository, &null_oid, path))
  647. die(_("no submodule mapping found in .gitmodules for path '%s'"),
  648. path);
  649. displaypath = get_submodule_displaypath(path, prefix);
  650. if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
  651. print_status(flags, 'U', path, &null_oid, displaypath);
  652. goto cleanup;
  653. }
  654. strbuf_addf(&buf, "%s/.git", path);
  655. git_dir = read_gitfile(buf.buf);
  656. if (!git_dir)
  657. git_dir = buf.buf;
  658. if (!is_submodule_active(the_repository, path) ||
  659. !is_git_directory(git_dir)) {
  660. print_status(flags, '-', path, ce_oid, displaypath);
  661. strbuf_release(&buf);
  662. goto cleanup;
  663. }
  664. strbuf_release(&buf);
  665. strvec_pushl(&diff_files_args, "diff-files",
  666. "--ignore-submodules=dirty", "--quiet", "--",
  667. path, NULL);
  668. git_config(git_diff_basic_config, NULL);
  669. repo_init_revisions(the_repository, &rev, NULL);
  670. rev.abbrev = 0;
  671. diff_files_args.nr = setup_revisions(diff_files_args.nr,
  672. diff_files_args.v,
  673. &rev, NULL);
  674. diff_files_result = run_diff_files(&rev, 0);
  675. if (!diff_result_code(&rev.diffopt, diff_files_result)) {
  676. print_status(flags, ' ', path, ce_oid,
  677. displaypath);
  678. } else if (!(flags & OPT_CACHED)) {
  679. struct object_id oid;
  680. struct ref_store *refs = get_submodule_ref_store(path);
  681. if (!refs) {
  682. print_status(flags, '-', path, ce_oid, displaypath);
  683. goto cleanup;
  684. }
  685. if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
  686. die(_("could not resolve HEAD ref inside the "
  687. "submodule '%s'"), path);
  688. print_status(flags, '+', path, &oid, displaypath);
  689. } else {
  690. print_status(flags, '+', path, ce_oid, displaypath);
  691. }
  692. if (flags & OPT_RECURSIVE) {
  693. struct child_process cpr = CHILD_PROCESS_INIT;
  694. cpr.git_cmd = 1;
  695. cpr.dir = path;
  696. prepare_submodule_repo_env(&cpr.env_array);
  697. strvec_push(&cpr.args, "--super-prefix");
  698. strvec_pushf(&cpr.args, "%s/", displaypath);
  699. strvec_pushl(&cpr.args, "submodule--helper", "status",
  700. "--recursive", NULL);
  701. if (flags & OPT_CACHED)
  702. strvec_push(&cpr.args, "--cached");
  703. if (flags & OPT_QUIET)
  704. strvec_push(&cpr.args, "--quiet");
  705. if (run_command(&cpr))
  706. die(_("failed to recurse into submodule '%s'"), path);
  707. }
  708. cleanup:
  709. strvec_clear(&diff_files_args);
  710. free(displaypath);
  711. }
  712. static void status_submodule_cb(const struct cache_entry *list_item,
  713. void *cb_data)
  714. {
  715. struct status_cb *info = cb_data;
  716. status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
  717. info->prefix, info->flags);
  718. }
  719. static int module_status(int argc, const char **argv, const char *prefix)
  720. {
  721. struct status_cb info = STATUS_CB_INIT;
  722. struct pathspec pathspec;
  723. struct module_list list = MODULE_LIST_INIT;
  724. int quiet = 0;
  725. struct option module_status_options[] = {
  726. OPT__QUIET(&quiet, N_("Suppress submodule status output")),
  727. OPT_BIT(0, "cached", &info.flags, N_("Use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
  728. OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
  729. OPT_END()
  730. };
  731. const char *const git_submodule_helper_usage[] = {
  732. N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
  733. NULL
  734. };
  735. argc = parse_options(argc, argv, prefix, module_status_options,
  736. git_submodule_helper_usage, 0);
  737. if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
  738. return 1;
  739. info.prefix = prefix;
  740. if (quiet)
  741. info.flags |= OPT_QUIET;
  742. for_each_listed_submodule(&list, status_submodule_cb, &info);
  743. return 0;
  744. }
  745. static int module_name(int argc, const char **argv, const char *prefix)
  746. {
  747. const struct submodule *sub;
  748. if (argc != 2)
  749. usage(_("git submodule--helper name <path>"));
  750. sub = submodule_from_path(the_repository, &null_oid, argv[1]);
  751. if (!sub)
  752. die(_("no submodule mapping found in .gitmodules for path '%s'"),
  753. argv[1]);
  754. printf("%s\n", sub->name);
  755. return 0;
  756. }
  757. struct module_cb {
  758. unsigned int mod_src;
  759. unsigned int mod_dst;
  760. struct object_id oid_src;
  761. struct object_id oid_dst;
  762. char status;
  763. const char *sm_path;
  764. };
  765. #define MODULE_CB_INIT { 0, 0, NULL, NULL, '\0', NULL }
  766. struct module_cb_list {
  767. struct module_cb **entries;
  768. int alloc, nr;
  769. };
  770. #define MODULE_CB_LIST_INIT { NULL, 0, 0 }
  771. struct summary_cb {
  772. int argc;
  773. const char **argv;
  774. const char *prefix;
  775. unsigned int cached: 1;
  776. unsigned int for_status: 1;
  777. unsigned int files: 1;
  778. int summary_limit;
  779. };
  780. #define SUMMARY_CB_INIT { 0, NULL, NULL, 0, 0, 0, 0 }
  781. enum diff_cmd {
  782. DIFF_INDEX,
  783. DIFF_FILES
  784. };
  785. static char *verify_submodule_committish(const char *sm_path,
  786. const char *committish)
  787. {
  788. struct child_process cp_rev_parse = CHILD_PROCESS_INIT;
  789. struct strbuf result = STRBUF_INIT;
  790. cp_rev_parse.git_cmd = 1;
  791. cp_rev_parse.dir = sm_path;
  792. prepare_submodule_repo_env(&cp_rev_parse.env_array);
  793. strvec_pushl(&cp_rev_parse.args, "rev-parse", "-q", "--short", NULL);
  794. strvec_pushf(&cp_rev_parse.args, "%s^0", committish);
  795. strvec_push(&cp_rev_parse.args, "--");
  796. if (capture_command(&cp_rev_parse, &result, 0))
  797. return NULL;
  798. strbuf_trim_trailing_newline(&result);
  799. return strbuf_detach(&result, NULL);
  800. }
  801. static void print_submodule_summary(struct summary_cb *info, char *errmsg,
  802. int total_commits, const char *displaypath,
  803. const char *src_abbrev, const char *dst_abbrev,
  804. struct module_cb *p)
  805. {
  806. if (p->status == 'T') {
  807. if (S_ISGITLINK(p->mod_dst))
  808. printf(_("* %s %s(blob)->%s(submodule)"),
  809. displaypath, src_abbrev, dst_abbrev);
  810. else
  811. printf(_("* %s %s(submodule)->%s(blob)"),
  812. displaypath, src_abbrev, dst_abbrev);
  813. } else {
  814. printf("* %s %s...%s",
  815. displaypath, src_abbrev, dst_abbrev);
  816. }
  817. if (total_commits < 0)
  818. printf(":\n");
  819. else
  820. printf(" (%d):\n", total_commits);
  821. if (errmsg) {
  822. printf(_("%s"), errmsg);
  823. } else if (total_commits > 0) {
  824. struct child_process cp_log = CHILD_PROCESS_INIT;
  825. cp_log.git_cmd = 1;
  826. cp_log.dir = p->sm_path;
  827. prepare_submodule_repo_env(&cp_log.env_array);
  828. strvec_pushl(&cp_log.args, "log", NULL);
  829. if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst)) {
  830. if (info->summary_limit > 0)
  831. strvec_pushf(&cp_log.args, "-%d",
  832. info->summary_limit);
  833. strvec_pushl(&cp_log.args, "--pretty= %m %s",
  834. "--first-parent", NULL);
  835. strvec_pushf(&cp_log.args, "%s...%s",
  836. src_abbrev, dst_abbrev);
  837. } else if (S_ISGITLINK(p->mod_dst)) {
  838. strvec_pushl(&cp_log.args, "--pretty= > %s",
  839. "-1", dst_abbrev, NULL);
  840. } else {
  841. strvec_pushl(&cp_log.args, "--pretty= < %s",
  842. "-1", src_abbrev, NULL);
  843. }
  844. run_command(&cp_log);
  845. }
  846. printf("\n");
  847. }
  848. static void generate_submodule_summary(struct summary_cb *info,
  849. struct module_cb *p)
  850. {
  851. char *displaypath, *src_abbrev = NULL, *dst_abbrev;
  852. int missing_src = 0, missing_dst = 0;
  853. char *errmsg = NULL;
  854. int total_commits = -1;
  855. if (!info->cached && oideq(&p->oid_dst, &null_oid)) {
  856. if (S_ISGITLINK(p->mod_dst)) {
  857. struct ref_store *refs = get_submodule_ref_store(p->sm_path);
  858. if (refs)
  859. refs_head_ref(refs, handle_submodule_head_ref, &p->oid_dst);
  860. } else if (S_ISLNK(p->mod_dst) || S_ISREG(p->mod_dst)) {
  861. struct stat st;
  862. int fd = open(p->sm_path, O_RDONLY);
  863. if (fd < 0 || fstat(fd, &st) < 0 ||
  864. index_fd(&the_index, &p->oid_dst, fd, &st, OBJ_BLOB,
  865. p->sm_path, 0))
  866. error(_("couldn't hash object from '%s'"), p->sm_path);
  867. } else {
  868. /* for a submodule removal (mode:0000000), don't warn */
  869. if (p->mod_dst)
  870. warning(_("unexpected mode %o\n"), p->mod_dst);
  871. }
  872. }
  873. if (S_ISGITLINK(p->mod_src)) {
  874. if (p->status != 'D')
  875. src_abbrev = verify_submodule_committish(p->sm_path,
  876. oid_to_hex(&p->oid_src));
  877. if (!src_abbrev) {
  878. missing_src = 1;
  879. /*
  880. * As `rev-parse` failed, we fallback to getting
  881. * the abbreviated hash using oid_src. We do
  882. * this as we might still need the abbreviated
  883. * hash in cases like a submodule type change, etc.
  884. */
  885. src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
  886. }
  887. } else {
  888. /*
  889. * The source does not point to a submodule.
  890. * So, we fallback to getting the abbreviation using
  891. * oid_src as we might still need the abbreviated
  892. * hash in cases like submodule add, etc.
  893. */
  894. src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
  895. }
  896. if (S_ISGITLINK(p->mod_dst)) {
  897. dst_abbrev = verify_submodule_committish(p->sm_path,
  898. oid_to_hex(&p->oid_dst));
  899. if (!dst_abbrev) {
  900. missing_dst = 1;
  901. /*
  902. * As `rev-parse` failed, we fallback to getting
  903. * the abbreviated hash using oid_dst. We do
  904. * this as we might still need the abbreviated
  905. * hash in cases like a submodule type change, etc.
  906. */
  907. dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
  908. }
  909. } else {
  910. /*
  911. * The destination does not point to a submodule.
  912. * So, we fallback to getting the abbreviation using
  913. * oid_dst as we might still need the abbreviated
  914. * hash in cases like a submodule removal, etc.
  915. */
  916. dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
  917. }
  918. displaypath = get_submodule_displaypath(p->sm_path, info->prefix);
  919. if (!missing_src && !missing_dst) {
  920. struct child_process cp_rev_list = CHILD_PROCESS_INIT;
  921. struct strbuf sb_rev_list = STRBUF_INIT;
  922. strvec_pushl(&cp_rev_list.args, "rev-list",
  923. "--first-parent", "--count", NULL);
  924. if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst))
  925. strvec_pushf(&cp_rev_list.args, "%s...%s",
  926. src_abbrev, dst_abbrev);
  927. else
  928. strvec_push(&cp_rev_list.args, S_ISGITLINK(p->mod_src) ?
  929. src_abbrev : dst_abbrev);
  930. strvec_push(&cp_rev_list.args, "--");
  931. cp_rev_list.git_cmd = 1;
  932. cp_rev_list.dir = p->sm_path;
  933. prepare_submodule_repo_env(&cp_rev_list.env_array);
  934. if (!capture_command(&cp_rev_list, &sb_rev_list, 0))
  935. total_commits = atoi(sb_rev_list.buf);
  936. strbuf_release(&sb_rev_list);
  937. } else {
  938. /*
  939. * Don't give error msg for modification whose dst is not
  940. * submodule, i.e., deleted or changed to blob
  941. */
  942. if (S_ISGITLINK(p->mod_dst)) {
  943. struct strbuf errmsg_str = STRBUF_INIT;
  944. if (missing_src && missing_dst) {
  945. strbuf_addf(&errmsg_str, " Warn: %s doesn't contain commits %s and %s\n",
  946. displaypath, oid_to_hex(&p->oid_src),
  947. oid_to_hex(&p->oid_dst));
  948. } else {
  949. strbuf_addf(&errmsg_str, " Warn: %s doesn't contain commit %s\n",
  950. displaypath, missing_src ?
  951. oid_to_hex(&p->oid_src) :
  952. oid_to_hex(&p->oid_dst));
  953. }
  954. errmsg = strbuf_detach(&errmsg_str, NULL);
  955. }
  956. }
  957. print_submodule_summary(info, errmsg, total_commits,
  958. displaypath, src_abbrev,
  959. dst_abbrev, p);
  960. free(displaypath);
  961. free(src_abbrev);
  962. free(dst_abbrev);
  963. }
  964. static void prepare_submodule_summary(struct summary_cb *info,
  965. struct module_cb_list *list)
  966. {
  967. int i;
  968. for (i = 0; i < list->nr; i++) {
  969. const struct submodule *sub;
  970. struct module_cb *p = list->entries[i];
  971. struct strbuf sm_gitdir = STRBUF_INIT;
  972. if (p->status == 'D' || p->status == 'T') {
  973. generate_submodule_summary(info, p);
  974. continue;
  975. }
  976. if (info->for_status && p->status != 'A' &&
  977. (sub = submodule_from_path(the_repository,
  978. &null_oid, p->sm_path))) {
  979. char *config_key = NULL;
  980. const char *value;
  981. int ignore_all = 0;
  982. config_key = xstrfmt("submodule.%s.ignore",
  983. sub->name);
  984. if (!git_config_get_string_tmp(config_key, &value))
  985. ignore_all = !strcmp(value, "all");
  986. else if (sub->ignore)
  987. ignore_all = !strcmp(sub->ignore, "all");
  988. free(config_key);
  989. if (ignore_all)
  990. continue;
  991. }
  992. /* Also show added or modified modules which are checked out */
  993. strbuf_addstr(&sm_gitdir, p->sm_path);
  994. if (is_nonbare_repository_dir(&sm_gitdir))
  995. generate_submodule_summary(info, p);
  996. strbuf_release(&sm_gitdir);
  997. }
  998. }
  999. static void submodule_summary_callback(struct diff_queue_struct *q,
  1000. struct diff_options *options,
  1001. void *data)
  1002. {
  1003. int i;
  1004. struct module_cb_list *list = data;
  1005. for (i = 0; i < q->nr; i++) {
  1006. struct diff_filepair *p = q->queue[i];
  1007. struct module_cb *temp;
  1008. if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode))
  1009. continue;
  1010. temp = (struct module_cb*)malloc(sizeof(struct module_cb));
  1011. temp->mod_src = p->one->mode;
  1012. temp->mod_dst = p->two->mode;
  1013. temp->oid_src = p->one->oid;
  1014. temp->oid_dst = p->two->oid;
  1015. temp->status = p->status;
  1016. temp->sm_path = xstrdup(p->one->path);
  1017. ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
  1018. list->entries[list->nr++] = temp;
  1019. }
  1020. }
  1021. static const char *get_diff_cmd(enum diff_cmd diff_cmd)
  1022. {
  1023. switch (diff_cmd) {
  1024. case DIFF_INDEX: return "diff-index";
  1025. case DIFF_FILES: return "diff-files";
  1026. default: BUG("bad diff_cmd value %d", diff_cmd);
  1027. }
  1028. }
  1029. static int compute_summary_module_list(struct object_id *head_oid,
  1030. struct summary_cb *info,
  1031. enum diff_cmd diff_cmd)
  1032. {
  1033. struct strvec diff_args = STRVEC_INIT;
  1034. struct rev_info rev;
  1035. struct module_cb_list list = MODULE_CB_LIST_INIT;
  1036. strvec_push(&diff_args, get_diff_cmd(diff_cmd));
  1037. if (info->cached)
  1038. strvec_push(&diff_args, "--cached");
  1039. strvec_pushl(&diff_args, "--ignore-submodules=dirty", "--raw", NULL);
  1040. if (head_oid)
  1041. strvec_push(&diff_args, oid_to_hex(head_oid));
  1042. strvec_push(&diff_args, "--");
  1043. if (info->argc)
  1044. strvec_pushv(&diff_args, info->argv);
  1045. git_config(git_diff_basic_config, NULL);
  1046. init_revisions(&rev, info->prefix);
  1047. rev.abbrev = 0;
  1048. precompose_argv(diff_args.nr, diff_args.v);
  1049. setup_revisions(diff_args.nr, diff_args.v, &rev, NULL);
  1050. rev.diffopt.output_format = DIFF_FORMAT_NO_OUTPUT | DIFF_FORMAT_CALLBACK;
  1051. rev.diffopt.format_callback = submodule_summary_callback;
  1052. rev.diffopt.format_callback_data = &list;
  1053. if (!info->cached) {
  1054. if (diff_cmd == DIFF_INDEX)
  1055. setup_work_tree();
  1056. if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
  1057. perror("read_cache_preload");
  1058. return -1;
  1059. }
  1060. } else if (read_cache() < 0) {
  1061. perror("read_cache");
  1062. return -1;
  1063. }
  1064. if (diff_cmd == DIFF_INDEX)
  1065. run_diff_index(&rev, info->cached);
  1066. else
  1067. run_diff_files(&rev, 0);
  1068. prepare_submodule_summary(info, &list);
  1069. strvec_clear(&diff_args);
  1070. return 0;
  1071. }
  1072. static int module_summary(int argc, const char **argv, const char *prefix)
  1073. {
  1074. struct summary_cb info = SUMMARY_CB_INIT;
  1075. int cached = 0;
  1076. int for_status = 0;
  1077. int files = 0;
  1078. int summary_limit = -1;
  1079. enum diff_cmd diff_cmd = DIFF_INDEX;
  1080. struct object_id head_oid;
  1081. int ret;
  1082. struct option module_summary_options[] = {
  1083. OPT_BOOL(0, "cached", &cached,
  1084. N_("use the commit stored in the index instead of the submodule HEAD")),
  1085. OPT_BOOL(0, "files", &files,
  1086. N_("to compare the commit in the index with that in the submodule HEAD")),
  1087. OPT_BOOL(0, "for-status", &for_status,
  1088. N_("skip submodules with 'ignore_config' value set to 'all'")),
  1089. OPT_INTEGER('n', "summary-limit", &summary_limit,
  1090. N_("limit the summary size")),
  1091. OPT_END()
  1092. };
  1093. const char *const git_submodule_helper_usage[] = {
  1094. N_("git submodule--helper summary [<options>] [commit] [--] [<path>]"),
  1095. NULL
  1096. };
  1097. argc = parse_options(argc, argv, prefix, module_summary_options,
  1098. git_submodule_helper_usage, 0);
  1099. if (!summary_limit)
  1100. return 0;
  1101. if (!get_oid(argc ? argv[0] : "HEAD", &head_oid)) {
  1102. if (argc) {
  1103. argv++;
  1104. argc--;
  1105. }
  1106. } else if (!argc || !strcmp(argv[0], "HEAD")) {
  1107. /* before the first commit: compare with an empty tree */
  1108. oidcpy(&head_oid, the_hash_algo->empty_tree);
  1109. if (argc) {
  1110. argv++;
  1111. argc--;
  1112. }
  1113. } else {
  1114. if (get_oid("HEAD", &head_oid))
  1115. die(_("could not fetch a revision for HEAD"));
  1116. }
  1117. if (files) {
  1118. if (cached)
  1119. die(_("--cached and --files are mutually exclusive"));
  1120. diff_cmd = DIFF_FILES;
  1121. }
  1122. info.argc = argc;
  1123. info.argv = argv;
  1124. info.prefix = prefix;
  1125. info.cached = !!cached;
  1126. info.files = !!files;
  1127. info.for_status = !!for_status;
  1128. info.summary_limit = summary_limit;
  1129. ret = compute_summary_module_list((diff_cmd == DIFF_INDEX) ? &head_oid : NULL,
  1130. &info, diff_cmd);
  1131. return ret;
  1132. }
  1133. struct sync_cb {
  1134. const char *prefix;
  1135. unsigned int flags;
  1136. };
  1137. #define SYNC_CB_INIT { NULL, 0 }
  1138. static void sync_submodule(const char *path, const char *prefix,
  1139. unsigned int flags)
  1140. {
  1141. const struct submodule *sub;
  1142. char *remote_key = NULL;
  1143. char *sub_origin_url, *super_config_url, *displaypath;
  1144. struct strbuf sb = STRBUF_INIT;
  1145. struct child_process cp = CHILD_PROCESS_INIT;
  1146. char *sub_config_path = NULL;
  1147. if (!is_submodule_active(the_repository, path))
  1148. return;
  1149. sub = submodule_from_path(the_repository, &null_oid, path);
  1150. if (sub && sub->url) {
  1151. if (starts_with_dot_dot_slash(sub->url) ||
  1152. starts_with_dot_slash(sub->url)) {
  1153. char *remote_url, *up_path;
  1154. char *remote = get_default_remote();
  1155. strbuf_addf(&sb, "remote.%s.url", remote);
  1156. if (git_config_get_string(sb.buf, &remote_url))
  1157. remote_url = xgetcwd();
  1158. up_path = get_up_path(path);
  1159. sub_origin_url = relative_url(remote_url, sub->url, up_path);
  1160. super_config_url = relative_url(remote_url, sub->url, NULL);
  1161. free(remote);
  1162. free(up_path);
  1163. free(remote_url);
  1164. } else {
  1165. sub_origin_url = xstrdup(sub->url);
  1166. super_config_url = xstrdup(sub->url);
  1167. }
  1168. } else {
  1169. sub_origin_url = xstrdup("");
  1170. super_config_url = xstrdup("");
  1171. }
  1172. displaypath = get_submodule_displaypath(path, prefix);
  1173. if (!(flags & OPT_QUIET))
  1174. printf(_("Synchronizing submodule url for '%s'\n"),
  1175. displaypath);
  1176. strbuf_reset(&sb);
  1177. strbuf_addf(&sb, "submodule.%s.url", sub->name);
  1178. if (git_config_set_gently(sb.buf, super_config_url))
  1179. die(_("failed to register url for submodule path '%s'"),
  1180. displaypath);
  1181. if (!is_submodule_populated_gently(path, NULL))
  1182. goto cleanup;
  1183. prepare_submodule_repo_env(&cp.env_array);
  1184. cp.git_cmd = 1;
  1185. cp.dir = path;
  1186. strvec_pushl(&cp.args, "submodule--helper",
  1187. "print-default-remote", NULL);
  1188. strbuf_reset(&sb);
  1189. if (capture_command(&cp, &sb, 0))
  1190. die(_("failed to get the default remote for submodule '%s'"),
  1191. path);
  1192. strbuf_strip_suffix(&sb, "\n");
  1193. remote_key = xstrfmt("remote.%s.url", sb.buf);
  1194. strbuf_reset(&sb);
  1195. submodule_to_gitdir(&sb, path);
  1196. strbuf_addstr(&sb, "/config");
  1197. if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url))
  1198. die(_("failed to update remote for submodule '%s'"),
  1199. path);
  1200. if (flags & OPT_RECURSIVE) {
  1201. struct child_process cpr = CHILD_PROCESS_INIT;
  1202. cpr.git_cmd = 1;
  1203. cpr.dir = path;
  1204. prepare_submodule_repo_env(&cpr.env_array);
  1205. strvec_push(&cpr.args, "--super-prefix");
  1206. strvec_pushf(&cpr.args, "%s/", displaypath);
  1207. strvec_pushl(&cpr.args, "submodule--helper", "sync",
  1208. "--recursive", NULL);
  1209. if (flags & OPT_QUIET)
  1210. strvec_push(&cpr.args, "--quiet");
  1211. if (run_command(&cpr))
  1212. die(_("failed to recurse into submodule '%s'"),
  1213. path);
  1214. }
  1215. cleanup:
  1216. free(super_config_url);
  1217. free(sub_origin_url);
  1218. strbuf_release(&sb);
  1219. free(remote_key);
  1220. free(displaypath);
  1221. free(sub_config_path);
  1222. }
  1223. static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
  1224. {
  1225. struct sync_cb *info = cb_data;
  1226. sync_submodule(list_item->name, info->prefix, info->flags);
  1227. }
  1228. static int module_sync(int argc, const char **argv, const char *prefix)
  1229. {
  1230. struct sync_cb info = SYNC_CB_INIT;
  1231. struct pathspec pathspec;
  1232. struct module_list list = MODULE_LIST_INIT;
  1233. int quiet = 0;
  1234. int recursive = 0;
  1235. struct option module_sync_options[] = {
  1236. OPT__QUIET(&quiet, N_("Suppress output of synchronizing submodule url")),
  1237. OPT_BOOL(0, "recursive", &recursive,
  1238. N_("Recurse into nested submodules")),
  1239. OPT_END()
  1240. };
  1241. const char *const git_submodule_helper_usage[] = {
  1242. N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
  1243. NULL
  1244. };
  1245. argc = parse_options(argc, argv, prefix, module_sync_options,
  1246. git_submodule_helper_usage, 0);
  1247. if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
  1248. return 1;
  1249. info.prefix = prefix;
  1250. if (quiet)
  1251. info.flags |= OPT_QUIET;
  1252. if (recursive)
  1253. info.flags |= OPT_RECURSIVE;
  1254. for_each_listed_submodule(&list, sync_submodule_cb, &info);
  1255. return 0;
  1256. }
  1257. struct deinit_cb {
  1258. const char *prefix;
  1259. unsigned int flags;
  1260. };
  1261. #define DEINIT_CB_INIT { NULL, 0 }
  1262. static void deinit_submodule(const char *path, const char *prefix,
  1263. unsigned int flags)
  1264. {
  1265. const struct submodule *sub;
  1266. char *displaypath = NULL;
  1267. struct child_process cp_config = CHILD_PROCESS_INIT;
  1268. struct strbuf sb_config = STRBUF_INIT;
  1269. char *sub_git_dir = xstrfmt("%s/.git", path);
  1270. sub = submodule_from_path(the_repository, &null_oid, path);
  1271. if (!sub || !sub->name)
  1272. goto cleanup;
  1273. displaypath = get_submodule_displaypath(path, prefix);
  1274. /* remove the submodule work tree (unless the user already did it) */
  1275. if (is_directory(path)) {
  1276. struct strbuf sb_rm = STRBUF_INIT;
  1277. const char *format;
  1278. /*
  1279. * protect submodules containing a .git directory
  1280. * NEEDSWORK: instead of dying, automatically call
  1281. * absorbgitdirs and (possibly) warn.
  1282. */
  1283. if (is_directory(sub_git_dir))
  1284. die(_("Submodule work tree '%s' contains a .git "
  1285. "directory (use 'rm -rf' if you really want "
  1286. "to remove it including all of its history)"),
  1287. displaypath);
  1288. if (!(flags & OPT_FORCE)) {
  1289. struct child_process cp_rm = CHILD_PROCESS_INIT;
  1290. cp_rm.git_cmd = 1;
  1291. strvec_pushl(&cp_rm.args, "rm", "-qn",
  1292. path, NULL);
  1293. if (run_command(&cp_rm))
  1294. die(_("Submodule work tree '%s' contains local "
  1295. "modifications; use '-f' to discard them"),
  1296. displaypath);
  1297. }
  1298. strbuf_addstr(&sb_rm, path);
  1299. if (!remove_dir_recursively(&sb_rm, 0))
  1300. format = _("Cleared directory '%s'\n");
  1301. else
  1302. format = _("Could not remove submodule work tree '%s'\n");
  1303. if (!(flags & OPT_QUIET))
  1304. printf(format, displaypath);
  1305. submodule_unset_core_worktree(sub);
  1306. strbuf_release(&sb_rm);
  1307. }
  1308. if (mkdir(path, 0777))
  1309. printf(_("could not create empty submodule directory %s"),
  1310. displaypath);
  1311. cp_config.git_cmd = 1;
  1312. strvec_pushl(&cp_config.args, "config", "--get-regexp", NULL);
  1313. strvec_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
  1314. /* remove the .git/config entries (unless the user already did it) */
  1315. if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
  1316. char *sub_key = xstrfmt("submodule.%s", sub->name);
  1317. /*
  1318. * remove the whole section so we have a clean state when
  1319. * the user later decides to init this submodule again
  1320. */
  1321. git_config_rename_section_in_file(NULL, sub_key, NULL);
  1322. if (!(flags & OPT_QUIET))
  1323. printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
  1324. sub->name, sub->url, displaypath);
  1325. free(sub_key);
  1326. }
  1327. cleanup:
  1328. free(displaypath);
  1329. free(sub_git_dir);
  1330. strbuf_release(&sb_config);
  1331. }
  1332. static void deinit_submodule_cb(const struct cache_entry *list_item,
  1333. void *cb_data)
  1334. {
  1335. struct deinit_cb *info = cb_data;
  1336. deinit_submodule(list_item->name, info->prefix, info->flags);
  1337. }
  1338. static int module_deinit(int argc, const char **argv, const char *prefix)
  1339. {
  1340. struct deinit_cb info = DEINIT_CB_INIT;
  1341. struct pathspec pathspec;
  1342. struct module_list list = MODULE_LIST_INIT;
  1343. int quiet = 0;
  1344. int force = 0;
  1345. int all = 0;
  1346. struct option module_deinit_options[] = {
  1347. OPT__QUIET(&quiet, N_("Suppress submodule status output")),
  1348. OPT__FORCE(&force, N_("Remove submodule working trees even if they contain local changes"), 0),
  1349. OPT_BOOL(0, "all", &all, N_("Unregister all submodules")),
  1350. OPT_END()
  1351. };
  1352. const char *const git_submodule_helper_usage[] = {
  1353. N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
  1354. NULL
  1355. };
  1356. argc = parse_options(argc, argv, prefix, module_deinit_options,
  1357. git_submodule_helper_usage, 0);
  1358. if (all && argc) {
  1359. error("pathspec and --all are incompatible");
  1360. usage_with_options(git_submodule_helper_usage,
  1361. module_deinit_options);
  1362. }
  1363. if (!argc && !all)
  1364. die(_("Use '--all' if you really want to deinitialize all submodules"));
  1365. if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
  1366. return 1;
  1367. info.prefix = prefix;
  1368. if (quiet)
  1369. info.flags |= OPT_QUIET;
  1370. if (force)
  1371. info.flags |= OPT_FORCE;
  1372. for_each_listed_submodule(&list, deinit_submodule_cb, &info);
  1373. return 0;
  1374. }
  1375. static int clone_submodule(const char *path, const char *gitdir, const char *url,
  1376. const char *depth, struct string_list *reference, int dissociate,
  1377. int quiet, int progress, int single_branch)
  1378. {
  1379. struct child_process cp = CHILD_PROCESS_INIT;
  1380. strvec_push(&cp.args, "clone");
  1381. strvec_push(&cp.args, "--no-checkout");
  1382. if (quiet)
  1383. strvec_push(&cp.args, "--quiet");
  1384. if (progress)
  1385. strvec_push(&cp.args, "--progress");
  1386. if (depth && *depth)
  1387. strvec_pushl(&cp.args, "--depth", depth, NULL);
  1388. if (reference->nr) {
  1389. struct string_list_item *item;
  1390. for_each_string_list_item(item, reference)
  1391. strvec_pushl(&cp.args, "--reference",
  1392. item->string, NULL);
  1393. }
  1394. if (dissociate)
  1395. strvec_push(&cp.args, "--dissociate");
  1396. if (gitdir && *gitdir)
  1397. strvec_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
  1398. if (single_branch >= 0)
  1399. strvec_push(&cp.args, single_branch ?
  1400. "--single-branch" :
  1401. "--no-single-branch");
  1402. strvec_push(&cp.args, "--");
  1403. strvec_push(&cp.args, url);
  1404. strvec_push(&cp.args, path);
  1405. cp.git_cmd = 1;
  1406. prepare_submodule_repo_env(&cp.env_array);
  1407. cp.no_stdin = 1;
  1408. return run_command(&cp);
  1409. }
  1410. struct submodule_alternate_setup {
  1411. const char *submodule_name;
  1412. enum SUBMODULE_ALTERNATE_ERROR_MODE {
  1413. SUBMODULE_ALTERNATE_ERROR_DIE,
  1414. SUBMODULE_ALTERNATE_ERROR_INFO,
  1415. SUBMODULE_ALTERNATE_ERROR_IGNORE
  1416. } error_mode;
  1417. struct string_list *reference;
  1418. };
  1419. #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
  1420. SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
  1421. static const char alternate_error_advice[] = N_(
  1422. "An alternate computed from a superproject's alternate is invalid.\n"
  1423. "To allow Git to clone without an alternate in such a case, set\n"
  1424. "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
  1425. "'--reference-if-able' instead of '--reference'."
  1426. );
  1427. static int add_possible_reference_from_superproject(
  1428. struct object_directory *odb, void *sas_cb)
  1429. {
  1430. struct submodule_alternate_setup *sas = sas_cb;
  1431. size_t len;
  1432. /*
  1433. * If the alternate object store is another repository, try the
  1434. * standard layout with .git/(modules/<name>)+/objects
  1435. */
  1436. if (strip_suffix(odb->path, "/objects", &len)) {
  1437. char *sm_alternate;
  1438. struct strbuf sb = STRBUF_INIT;
  1439. struct strbuf err = STRBUF_INIT;
  1440. strbuf_add(&sb, odb->path, len);
  1441. /*
  1442. * We need to end the new path with '/' to mark it as a dir,
  1443. * otherwise a submodule name containing '/' will be broken
  1444. * as the last part of a missing submodule reference would
  1445. * be taken as a file name.
  1446. */
  1447. strbuf_addf(&sb, "/modules/%s/", sas->submodule_name);
  1448. sm_alternate = compute_alternate_path(sb.buf, &err);
  1449. if (sm_alternate) {
  1450. string_list_append(sas->reference, xstrdup(sb.buf));
  1451. free(sm_alternate);
  1452. } else {
  1453. switch (sas->error_mode) {
  1454. case SUBMODULE_ALTERNATE_ERROR_DIE:
  1455. if (advice_submodule_alternate_error_strategy_die)
  1456. advise(_(alternate_error_advice));
  1457. die(_("submodule '%s' cannot add alternate: %s"),
  1458. sas->submodule_name, err.buf);
  1459. case SUBMODULE_ALTERNATE_ERROR_INFO:
  1460. fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"),
  1461. sas->submodule_name, err.buf);
  1462. case SUBMODULE_ALTERNATE_ERROR_IGNORE:
  1463. ; /* nothing */
  1464. }
  1465. }
  1466. strbuf_release(&sb);
  1467. }
  1468. return 0;
  1469. }
  1470. static void prepare_possible_alternates(const char *sm_name,
  1471. struct string_list *reference)
  1472. {
  1473. char *sm_alternate = NULL, *error_strategy = NULL;
  1474. struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
  1475. git_config_get_string("submodule.alternateLocation", &sm_alternate);
  1476. if (!sm_alternate)
  1477. return;
  1478. git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
  1479. if (!error_strategy)
  1480. error_strategy = xstrdup("die");
  1481. sas.submodule_name = sm_name;
  1482. sas.reference = reference;
  1483. if (!strcmp(error_strategy, "die"))
  1484. sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
  1485. else if (!strcmp(error_strategy, "info"))
  1486. sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
  1487. else if (!strcmp(error_strategy, "ignore"))
  1488. sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
  1489. else
  1490. die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
  1491. if (!strcmp(sm_alternate, "superproject"))
  1492. foreach_alt_odb(add_possible_reference_from_superproject, &sas);
  1493. else if (!strcmp(sm_alternate, "no"))
  1494. ; /* do nothing */
  1495. else
  1496. die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
  1497. free(sm_alternate);
  1498. free(error_strategy);
  1499. }
  1500. static int module_clone(int argc, const char **argv, const char *prefix)
  1501. {
  1502. const char *name = NULL, *url = NULL, *depth = NULL;
  1503. int quiet = 0;
  1504. int progress = 0;
  1505. char *p, *path = NULL, *sm_gitdir;
  1506. struct strbuf sb = STRBUF_INIT;
  1507. struct string_list reference = STRING_LIST_INIT_NODUP;
  1508. int dissociate = 0, require_init = 0;
  1509. char *sm_alternate = NULL, *error_strategy = NULL;
  1510. int single_branch = -1;
  1511. struct option module_clone_options[] = {
  1512. OPT_STRING(0, "prefix", &prefix,
  1513. N_("path"),
  1514. N_("alternative anchor for relative paths")),
  1515. OPT_STRING(0, "path", &path,
  1516. N_("path"),
  1517. N_("where the new submodule will be cloned to")),
  1518. OPT_STRING(0, "name", &name,
  1519. N_("string"),
  1520. N_("name of the new submodule")),
  1521. OPT_STRING(0, "url", &url,
  1522. N_("string"),
  1523. N_("url where to clone the submodule from")),
  1524. OPT_STRING_LIST(0, "reference", &reference,
  1525. N_("repo"),
  1526. N_("reference repository")),
  1527. OPT_BOOL(0, "dissociate", &dissociate,
  1528. N_("use --reference only while cloning")),
  1529. OPT_STRING(0, "depth", &depth,
  1530. N_("string"),
  1531. N_("depth for shallow clones")),
  1532. OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
  1533. OPT_BOOL(0, "progress", &progress,
  1534. N_("force cloning progress")),
  1535. OPT_BOOL(0, "require-init", &require_init,
  1536. N_("disallow cloning into non-empty directory")),
  1537. OPT_BOOL(0, "single-branch", &single_branch,
  1538. N_("clone only one branch, HEAD or --branch")),
  1539. OPT_END()
  1540. };
  1541. const char *const git_submodule_helper_usage[] = {
  1542. N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
  1543. "[--reference <repository>] [--name <name>] [--depth <depth>] "
  1544. "[--single-branch] "
  1545. "--url <url> --path <path>"),
  1546. NULL
  1547. };
  1548. argc = parse_options(argc, argv, prefix, module_clone_options,
  1549. git_submodule_helper_usage, 0);
  1550. if (argc || !url || !path || !*path)
  1551. usage_with_options(git_submodule_helper_usage,
  1552. module_clone_options);
  1553. strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
  1554. sm_gitdir = absolute_pathdup(sb.buf);
  1555. strbuf_reset(&sb);
  1556. if (!is_absolute_path(path)) {
  1557. strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
  1558. path = strbuf_detach(&sb, NULL);
  1559. } else
  1560. path = xstrdup(path);
  1561. if (validate_submodule_git_dir(sm_gitdir, name) < 0)
  1562. die(_("refusing to create/use '%s' in another submodule's "
  1563. "git dir"), sm_gitdir);
  1564. if (!file_exists(sm_gitdir)) {
  1565. if (safe_create_leading_directories_const(sm_gitdir) < 0)
  1566. die(_("could not create directory '%s'"), sm_gitdir);
  1567. prepare_possible_alternates(name, &reference);
  1568. if (clone_submodule(path, sm_gitdir, url, depth, &reference, dissociate,
  1569. quiet, progress, single_branch))
  1570. die(_("clone of '%s' into submodule path '%s' failed"),
  1571. url, path);
  1572. } else {
  1573. if (require_init && !access(path, X_OK) && !is_empty_dir(path))
  1574. die(_("directory not empty: '%s'"), path);
  1575. if (safe_create_leading_directories_const(path) < 0)
  1576. die(_("could not create directory '%s'"), path);
  1577. strbuf_addf(&sb, "%s/index", sm_gitdir);
  1578. unlink_or_warn(sb.buf);
  1579. strbuf_reset(&sb);
  1580. }
  1581. connect_work_tree_and_git_dir(path, sm_gitdir, 0);
  1582. p = git_pathdup_submodule(path, "config");
  1583. if (!p)
  1584. die(_("could not get submodule directory for '%s'"), path);
  1585. /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
  1586. git_config_get_string("submodule.alternateLocation", &sm_alternate);
  1587. if (sm_alternate)
  1588. git_config_set_in_file(p, "submodule.alternateLocation",
  1589. sm_alternate);
  1590. git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
  1591. if (error_strategy)
  1592. git_config_set_in_file(p, "submodule.alternateErrorStrategy",
  1593. error_strategy);
  1594. free(sm_alternate);
  1595. free(error_strategy);
  1596. strbuf_release(&sb);
  1597. free(sm_gitdir);
  1598. free(path);
  1599. free(p);
  1600. return 0;
  1601. }
  1602. static void determine_submodule_update_strategy(struct repository *r,
  1603. int just_cloned,
  1604. const char *path,
  1605. const char *update,
  1606. struct submodule_update_strategy *out)
  1607. {
  1608. const struct submodule *sub = submodule_from_path(r, &null_oid, path);
  1609. char *key;
  1610. const char *val;
  1611. key = xstrfmt("submodule.%s.update", sub->name);
  1612. if (update) {
  1613. if (parse_submodule_update_strategy(update, out) < 0)
  1614. die(_("Invalid update mode '%s' for submodule path '%s'"),
  1615. update, path);
  1616. } else if (!repo_config_get_string_tmp(r, key, &val)) {
  1617. if (parse_submodule_update_strategy(val, out) < 0)
  1618. die(_("Invalid update mode '%s' configured for submodule path '%s'"),
  1619. val, path);
  1620. } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
  1621. if (sub->update_strategy.type == SM_UPDATE_COMMAND)
  1622. BUG("how did we read update = !command from .gitmodules?");
  1623. out->type = sub->update_strategy.type;
  1624. out->command = sub->update_strategy.command;
  1625. } else
  1626. out->type = SM_UPDATE_CHECKOUT;
  1627. if (just_cloned &&
  1628. (out->type == SM_UPDATE_MERGE ||
  1629. out->type == SM_UPDATE_REBASE ||
  1630. out->type == SM_UPDATE_NONE))
  1631. out->type = SM_UPDATE_CHECKOUT;
  1632. free(key);
  1633. }
  1634. static int module_update_module_mode(int argc, const char **argv, const char *prefix)
  1635. {
  1636. const char *path, *update = NULL;
  1637. int just_cloned;
  1638. struct submodule_update_strategy update_strategy = { .type = SM_UPDATE_CHECKOUT };
  1639. if (argc < 3 || argc > 4)
  1640. die("submodule--helper update-module-clone expects <just-cloned> <path> [<update>]");
  1641. just_cloned = git_config_int("just_cloned", argv[1]);
  1642. path = argv[2];
  1643. if (argc == 4)
  1644. update = argv[3];
  1645. determine_submodule_update_strategy(the_repository,
  1646. just_cloned, path, update,
  1647. &update_strategy);
  1648. fputs(submodule_strategy_to_string(&update_strategy), stdout);
  1649. return 0;
  1650. }
  1651. struct update_clone_data {
  1652. const struct submodule *sub;
  1653. struct object_id oid;
  1654. unsigned just_cloned;
  1655. };
  1656. struct submodule_update_clone {
  1657. /* index into 'list', the list of submodules to look into for cloning */
  1658. int current;
  1659. struct module_list list;
  1660. unsigned warn_if_uninitialized : 1;
  1661. /* update parameter passed via commandline */
  1662. struct submodule_update_strategy update;
  1663. /* configuration parameters which are passed on to the children */
  1664. int progress;
  1665. int quiet;
  1666. int recommend_shallow;
  1667. struct string_list references;
  1668. int dissociate;
  1669. unsigned require_init;
  1670. const char *depth;
  1671. const char *recursive_prefix;
  1672. const char *prefix;
  1673. int single_branch;
  1674. /* to be consumed by git-submodule.sh */
  1675. struct update_clone_data *update_clone;
  1676. int update_clone_nr; int update_clone_alloc;
  1677. /* If we want to stop as fast as possible and return an error */
  1678. unsigned quickstop : 1;
  1679. /* failed clones to be retried again */
  1680. const struct cache_entry **failed_clones;
  1681. int failed_clones_nr, failed_clones_alloc;
  1682. int max_jobs;
  1683. };
  1684. #define SUBMODULE_UPDATE_CLONE_INIT { \
  1685. .list = MODULE_LIST_INIT, \
  1686. .update = SUBMODULE_UPDATE_STRATEGY_INIT, \
  1687. .recommend_shallow = -1, \
  1688. .references = STRING_LIST_INIT_DUP, \
  1689. .single_branch = -1, \
  1690. .max_jobs = 1, \
  1691. }
  1692. static void next_submodule_warn_missing(struct submodule_update_clone *suc,
  1693. struct strbuf *out, const char *displaypath)
  1694. {
  1695. /*
  1696. * Only mention uninitialized submodules when their
  1697. * paths have been specified.
  1698. */
  1699. if (suc->warn_if_uninitialized) {
  1700. strbuf_addf(out,
  1701. _("Submodule path '%s' not initialized"),
  1702. displaypath);
  1703. strbuf_addch(out, '\n');
  1704. strbuf_addstr(out,
  1705. _("Maybe you want to use 'update --init'?"));
  1706. strbuf_addch(out, '\n');
  1707. }
  1708. }
  1709. /**
  1710. * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
  1711. * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
  1712. */
  1713. static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
  1714. struct child_process *child,
  1715. struct submodule_update_clone *suc,
  1716. struct strbuf *out)
  1717. {
  1718. const struct submodule *sub = NULL;
  1719. const char *url = NULL;
  1720. const char *update_string;
  1721. enum submodule_update_type update_type;
  1722. char *key;
  1723. struct strbuf displaypath_sb = STRBUF_INIT;
  1724. struct strbuf sb = STRBUF_INIT;
  1725. const char *displaypath = NULL;
  1726. int needs_cloning = 0;
  1727. int need_free_url = 0;
  1728. if (ce_stage(ce)) {
  1729. if (suc->recursive_prefix)
  1730. strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
  1731. else
  1732. strbuf_addstr(&sb, ce->name);
  1733. strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
  1734. strbuf_addch(out, '\n');
  1735. goto cleanup;
  1736. }
  1737. sub = submodule_from_path(the_repository, &null_oid, ce->name);
  1738. if (suc->recursive_prefix)
  1739. displaypath = relative_path(suc->recursive_prefix,
  1740. ce->name, &displaypath_sb);
  1741. else
  1742. displaypath = ce->name;
  1743. if (!sub) {
  1744. next_submodule_warn_missing(suc, out, displaypath);
  1745. goto cleanup;
  1746. }
  1747. key = xstrfmt("submodule.%s.update", sub->name);
  1748. if (!repo_config_get_string_tmp(the_repository, key, &update_string)) {
  1749. update_type = parse_submodule_update_type(update_string);
  1750. } else {
  1751. update_type = sub->update_strategy.type;
  1752. }
  1753. free(key);
  1754. if (suc->update.type == SM_UPDATE_NONE
  1755. || (suc->update.type == SM_UPDATE_UNSPECIFIED
  1756. && update_type == SM_UPDATE_NONE)) {
  1757. strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
  1758. strbuf_addch(out, '\n');
  1759. goto cleanup;
  1760. }
  1761. /* Check if the submodule has been initialized. */
  1762. if (!is_submodule_active(the_repository, ce->name)) {
  1763. next_submodule_warn_missing(suc, out, displaypath);
  1764. goto cleanup;
  1765. }
  1766. strbuf_reset(&sb);
  1767. strbuf_addf(&sb, "submodule.%s.url", sub->name);
  1768. if (repo_config_get_string_tmp(the_repository, sb.buf, &url)) {
  1769. if (starts_with_dot_slash(sub->url) ||
  1770. starts_with_dot_dot_slash(sub->url)) {
  1771. url = compute_submodule_clone_url(sub->url);
  1772. need_free_url = 1;
  1773. } else
  1774. url = sub->url;
  1775. }
  1776. strbuf_reset(&sb);
  1777. strbuf_addf(&sb, "%s/.git", ce->name);
  1778. needs_cloning = !file_exists(sb.buf);
  1779. ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
  1780. suc->update_clone_alloc);
  1781. oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
  1782. suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
  1783. suc->update_clone[suc->update_clone_nr].sub = sub;
  1784. suc->update_clone_nr++;
  1785. if (!needs_cloning)
  1786. goto cleanup;
  1787. child->git_cmd = 1;
  1788. child->no_stdin = 1;
  1789. child->stdout_to_stderr = 1;
  1790. child->err = -1;
  1791. strvec_push(&child->args, "submodule--helper");
  1792. strvec_push(&child->args, "clone");
  1793. if (suc->progress)
  1794. strvec_push(&child->args, "--progress");
  1795. if (suc->quiet)
  1796. strvec_push(&child->args, "--quiet");
  1797. if (suc->prefix)
  1798. strvec_pushl(&child->args, "--prefix", suc->prefix, NULL);
  1799. if (suc->recommend_shallow && sub->recommend_shallow == 1)
  1800. strvec_push(&child->args, "--depth=1");
  1801. if (suc->require_init)
  1802. strvec_push(&child->args, "--require-init");
  1803. strvec_pushl(&child->args, "--path", sub->path, NULL);
  1804. strvec_pushl(&child->args, "--name", sub->name, NULL);
  1805. strvec_pushl(&child->args, "--url", url, NULL);
  1806. if (suc->references.nr) {
  1807. struct string_list_item *item;
  1808. for_each_string_list_item(item, &suc->references)
  1809. strvec_pushl(&child->args, "--reference", item->string, NULL);
  1810. }
  1811. if (suc->dissociate)
  1812. strvec_push(&child->args, "--dissociate");
  1813. if (suc->depth)
  1814. strvec_push(&child->args, suc->depth);
  1815. if (suc->single_branch >= 0)
  1816. strvec_push(&child->args, suc->single_branch ?
  1817. "--single-branch" :
  1818. "--no-single-branch");
  1819. cleanup:
  1820. strbuf_release(&displaypath_sb);
  1821. strbuf_release(&sb);
  1822. if (need_free_url)
  1823. free((void*)url);
  1824. return needs_cloning;
  1825. }
  1826. static int update_clone_get_next_task(struct child_process *child,
  1827. struct strbuf *err,
  1828. void *suc_cb,
  1829. void **idx_task_cb)
  1830. {
  1831. struct submodule_update_clone *suc = suc_cb;
  1832. const struct cache_entry *ce;
  1833. int index;
  1834. for (; suc->current < suc->list.nr; suc->current++) {
  1835. ce = suc->list.entries[suc->current];
  1836. if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
  1837. int *p = xmalloc(sizeof(*p));
  1838. *p = suc->current;
  1839. *idx_task_cb = p;
  1840. suc->current++;
  1841. return 1;
  1842. }
  1843. }
  1844. /*
  1845. * The loop above tried cloning each submodule once, now try the
  1846. * stragglers again, which we can imagine as an extension of the
  1847. * entry list.
  1848. */
  1849. index = suc->current - suc->list.nr;
  1850. if (index < suc->failed_clones_nr) {
  1851. int *p;
  1852. ce = suc->failed_clones[index];
  1853. if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
  1854. suc->current ++;
  1855. strbuf_addstr(err, "BUG: submodule considered for "
  1856. "cloning, doesn't need cloning "
  1857. "any more?\n");
  1858. return 0;
  1859. }
  1860. p = xmalloc(sizeof(*p));
  1861. *p = suc->current;
  1862. *idx_task_cb = p;
  1863. suc->current ++;
  1864. return 1;
  1865. }
  1866. return 0;
  1867. }
  1868. static int update_clone_start_failure(struct strbuf *err,
  1869. void *suc_cb,
  1870. void *idx_task_cb)
  1871. {
  1872. struct submodule_update_clone *suc = suc_cb;
  1873. suc->quickstop = 1;
  1874. return 1;
  1875. }
  1876. static int update_clone_task_finished(int result,
  1877. struct strbuf *err,
  1878. void *suc_cb,
  1879. void *idx_task_cb)
  1880. {
  1881. const struct cache_entry *ce;
  1882. struct submodule_update_clone *suc = suc_cb;
  1883. int *idxP = idx_task_cb;
  1884. int idx = *idxP;
  1885. free(idxP);
  1886. if (!result)
  1887. return 0;
  1888. if (idx < suc->list.nr) {
  1889. ce = suc->list.entries[idx];
  1890. strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
  1891. ce->name);
  1892. strbuf_addch(err, '\n');
  1893. ALLOC_GROW(suc->failed_clones,
  1894. suc->failed_clones_nr + 1,
  1895. suc->failed_clones_alloc);
  1896. suc->failed_clones[suc->failed_clones_nr++] = ce;
  1897. return 0;
  1898. } else {
  1899. idx -= suc->list.nr;
  1900. ce = suc->failed_clones[idx];
  1901. strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
  1902. ce->name);
  1903. strbuf_addch(err, '\n');
  1904. suc->quickstop = 1;
  1905. return 1;
  1906. }
  1907. return 0;
  1908. }
  1909. static int git_update_clone_config(const char *var, const char *value,
  1910. void *cb)
  1911. {
  1912. int *max_jobs = cb;
  1913. if (!strcmp(var, "submodule.fetchjobs"))
  1914. *max_jobs = parse_submodule_fetchjobs(var, value);
  1915. return 0;
  1916. }
  1917. static void update_submodule(struct update_clone_data *ucd)
  1918. {
  1919. fprintf(stdout, "dummy %s %d\t%s\n",
  1920. oid_to_hex(&ucd->oid),
  1921. ucd->just_cloned,
  1922. ucd->sub->path);
  1923. }
  1924. static int update_submodules(struct submodule_update_clone *suc)
  1925. {
  1926. int i;
  1927. run_processes_parallel_tr2(suc->max_jobs, update_clone_get_next_task,
  1928. update_clone_start_failure,
  1929. update_clone_task_finished, suc, "submodule",
  1930. "parallel/update");
  1931. /*
  1932. * We saved the output and put it out all at once now.
  1933. * That means:
  1934. * - the listener does not have to interleave their (checkout)
  1935. * work with our fetching. The writes involved in a
  1936. * checkout involve more straightforward sequential I/O.
  1937. * - the listener can avoid doing any work if fetching failed.
  1938. */
  1939. if (suc->quickstop)
  1940. return 1;
  1941. for (i = 0; i < suc->update_clone_nr; i++)
  1942. update_submodule(&suc->update_clone[i]);
  1943. return 0;
  1944. }
  1945. static int update_clone(int argc, const char **argv, const char *prefix)
  1946. {
  1947. const char *update = NULL;
  1948. struct pathspec pathspec;
  1949. struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
  1950. struct option module_update_clone_options[] = {
  1951. OPT_STRING(0, "prefix", &prefix,
  1952. N_("path"),
  1953. N_("path into the working tree")),
  1954. OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
  1955. N_("path"),
  1956. N_("path into the working tree, across nested "
  1957. "submodule boundaries")),
  1958. OPT_STRING(0, "update", &update,
  1959. N_("string"),
  1960. N_("rebase, merge, checkout or none")),
  1961. OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"),
  1962. N_("reference repository")),
  1963. OPT_BOOL(0, "dissociate", &suc.dissociate,
  1964. N_("use --reference only while cloning")),
  1965. OPT_STRING(0, "depth", &suc.depth, "<depth>",
  1966. N_("Create a shallow clone truncated to the "
  1967. "specified number of revisions")),
  1968. OPT_INTEGER('j', "jobs", &suc.max_jobs,
  1969. N_("parallel jobs")),
  1970. OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
  1971. N_("whether the initial clone should follow the shallow recommendation")),
  1972. OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
  1973. OPT_BOOL(0, "progress", &suc.progress,
  1974. N_("force cloning progress")),
  1975. OPT_BOOL(0, "require-init", &suc.require_init,
  1976. N_("disallow cloning into non-empty directory")),
  1977. OPT_BOOL(0, "single-branch", &suc.single_branch,
  1978. N_("clone only one branch, HEAD or --branch")),
  1979. OPT_END()
  1980. };
  1981. const char *const git_submodule_helper_usage[] = {
  1982. N_("git submodule--helper update-clone [--prefix=<path>] [<path>...]"),
  1983. NULL
  1984. };
  1985. suc.prefix = prefix;
  1986. update_clone_config_from_gitmodules(&suc.max_jobs);
  1987. git_config(git_update_clone_config, &suc.max_jobs);
  1988. argc = parse_options(argc, argv, prefix, module_update_clone_options,
  1989. git_submodule_helper_usage, 0);
  1990. if (update)
  1991. if (parse_submodule_update_strategy(update, &suc.update) < 0)
  1992. die(_("bad value for update parameter"));
  1993. if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
  1994. return 1;
  1995. if (pathspec.nr)
  1996. suc.warn_if_uninitialized = 1;
  1997. return update_submodules(&suc);
  1998. }
  1999. static int resolve_relative_path(int argc, const char **argv, const char *prefix)
  2000. {
  2001. struct strbuf sb = STRBUF_INIT;
  2002. if (argc != 3)
  2003. die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc);
  2004. printf("%s", relative_path(argv[1], argv[2], &sb));
  2005. strbuf_release(&sb);
  2006. return 0;
  2007. }
  2008. static const char *remote_submodule_branch(const char *path)
  2009. {
  2010. const struct submodule *sub;
  2011. const char *branch = NULL;
  2012. char *key;
  2013. sub = submodule_from_path(the_repository, &null_oid, path);
  2014. if (!sub)
  2015. return NULL;
  2016. key = xstrfmt("submodule.%s.branch", sub->name);
  2017. if (repo_config_get_string_tmp(the_repository, key, &branch))
  2018. branch = sub->branch;
  2019. free(key);
  2020. if (!branch)
  2021. return "HEAD";
  2022. if (!strcmp(branch, ".")) {
  2023. const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
  2024. if (!refname)
  2025. die(_("No such ref: %s"), "HEAD");
  2026. /* detached HEAD */
  2027. if (!strcmp(refname, "HEAD"))
  2028. die(_("Submodule (%s) branch configured to inherit "
  2029. "branch from superproject, but the superproject "
  2030. "is not on any branch"), sub->name);
  2031. if (!skip_prefix(refname, "refs/heads/", &refname))
  2032. die(_("Expecting a full ref name, got %s"), refname);
  2033. return refname;
  2034. }
  2035. return branch;
  2036. }
  2037. static int resolve_remote_submodule_branch(int argc, const char **argv,
  2038. const char *prefix)
  2039. {
  2040. const char *ret;
  2041. struct strbuf sb = STRBUF_INIT;
  2042. if (argc != 2)
  2043. die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
  2044. ret = remote_submodule_branch(argv[1]);
  2045. if (!ret)
  2046. die("submodule %s doesn't exist", argv[1]);
  2047. printf("%s", ret);
  2048. strbuf_release(&sb);
  2049. return 0;
  2050. }
  2051. static int push_check(int argc, const char **argv, const char *prefix)
  2052. {
  2053. struct remote *remote;
  2054. const char *superproject_head;
  2055. char *head;
  2056. int detached_head = 0;
  2057. struct object_id head_oid;
  2058. if (argc < 3)
  2059. die("submodule--helper push-check requires at least 2 arguments");
  2060. /*
  2061. * superproject's resolved head ref.
  2062. * if HEAD then the superproject is in a detached head state, otherwise
  2063. * it will be the resolved head ref.
  2064. */
  2065. superproject_head = argv[1];
  2066. argv++;
  2067. argc--;
  2068. /* Get the submodule's head ref and determine if it is detached */
  2069. head = resolve_refdup("HEAD", 0, &head_oid, NULL);
  2070. if (!head)
  2071. die(_("Failed to resolve HEAD as a valid ref."));
  2072. if (!strcmp(head, "HEAD"))
  2073. detached_head = 1;
  2074. /*
  2075. * The remote must be configured.
  2076. * This is to avoid pushing to the exact same URL as the parent.
  2077. */
  2078. remote = pushremote_get(argv[1]);
  2079. if (!remote || remote->origin == REMOTE_UNCONFIGURED)
  2080. die("remote '%s' not configured", argv[1]);
  2081. /* Check the refspec */
  2082. if (argc > 2) {
  2083. int i;
  2084. struct ref *local_refs = get_local_heads();
  2085. struct refspec refspec = REFSPEC_INIT_PUSH;
  2086. refspec_appendn(&refspec, argv + 2, argc - 2);
  2087. for (i = 0; i < refspec.nr; i++) {
  2088. const struct refspec_item *rs = &refspec.items[i];
  2089. if (rs->pattern || rs->matching)
  2090. continue;
  2091. /* LHS must match a single ref */
  2092. switch (count_refspec_match(rs->src, local_refs, NULL)) {
  2093. case 1:
  2094. break;
  2095. case 0:
  2096. /*
  2097. * If LHS matches 'HEAD' then we need to ensure
  2098. * that it matches the same named branch
  2099. * checked out in the superproject.
  2100. */
  2101. if (!strcmp(rs->src, "HEAD")) {
  2102. if (!detached_head &&
  2103. !strcmp(head, superproject_head))
  2104. break;
  2105. die("HEAD does not match the named branch in the superproject");
  2106. }
  2107. /* fallthrough */
  2108. default:
  2109. die("src refspec '%s' must name a ref",
  2110. rs->src);
  2111. }
  2112. }
  2113. refspec_clear(&refspec);
  2114. }
  2115. free(head);
  2116. return 0;
  2117. }
  2118. static int ensure_core_worktree(int argc, const char **argv, const char *prefix)
  2119. {
  2120. const struct submodule *sub;
  2121. const char *path;
  2122. const char *cw;
  2123. struct repository subrepo;
  2124. if (argc != 2)
  2125. BUG("submodule--helper ensure-core-worktree <path>");
  2126. path = argv[1];
  2127. sub = submodule_from_path(the_repository, &null_oid, path);
  2128. if (!sub)
  2129. BUG("We could get the submodule handle before?");
  2130. if (repo_submodule_init(&subrepo, the_repository, sub))
  2131. die(_("could not get a repository handle for submodule '%s'"), path);
  2132. if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) {
  2133. char *cfg_file, *abs_path;
  2134. const char *rel_path;
  2135. struct strbuf sb = STRBUF_INIT;
  2136. cfg_file = repo_git_path(&subrepo, "config");
  2137. abs_path = absolute_pathdup(path);
  2138. rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
  2139. git_config_set_in_file(cfg_file, "core.worktree", rel_path);
  2140. free(cfg_file);
  2141. free(abs_path);
  2142. strbuf_release(&sb);
  2143. }
  2144. return 0;
  2145. }
  2146. static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
  2147. {
  2148. int i;
  2149. struct pathspec pathspec;
  2150. struct module_list list = MODULE_LIST_INIT;
  2151. unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
  2152. struct option embed_gitdir_options[] = {
  2153. OPT_STRING(0, "prefix", &prefix,
  2154. N_("path"),
  2155. N_("path into the working tree")),
  2156. OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
  2157. ABSORB_GITDIR_RECURSE_SUBMODULES),
  2158. OPT_END()
  2159. };
  2160. const char *const git_submodule_helper_usage[] = {
  2161. N_("git submodule--helper absorb-git-dirs [<options>] [<path>...]"),
  2162. NULL
  2163. };
  2164. argc = parse_options(argc, argv, prefix, embed_gitdir_options,
  2165. git_submodule_helper_usage, 0);
  2166. if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
  2167. return 1;
  2168. for (i = 0; i < list.nr; i++)
  2169. absorb_git_dir_into_superproject(list.entries[i]->name, flags);
  2170. return 0;
  2171. }
  2172. static int is_active(int argc, const char **argv, const char *prefix)
  2173. {
  2174. if (argc != 2)
  2175. die("submodule--helper is-active takes exactly 1 argument");
  2176. return !is_submodule_active(the_repository, argv[1]);
  2177. }
  2178. /*
  2179. * Exit non-zero if any of the submodule names given on the command line is
  2180. * invalid. If no names are given, filter stdin to print only valid names
  2181. * (which is primarily intended for testing).
  2182. */
  2183. static int check_name(int argc, const char **argv, const char *prefix)
  2184. {
  2185. if (argc > 1) {
  2186. while (*++argv) {
  2187. if (check_submodule_name(*argv) < 0)
  2188. return 1;
  2189. }
  2190. } else {
  2191. struct strbuf buf = STRBUF_INIT;
  2192. while (strbuf_getline(&buf, stdin) != EOF) {
  2193. if (!check_submodule_name(buf.buf))
  2194. printf("%s\n", buf.buf);
  2195. }
  2196. strbuf_release(&buf);
  2197. }
  2198. return 0;
  2199. }
  2200. static int module_config(int argc, const char **argv, const char *prefix)
  2201. {
  2202. enum {
  2203. CHECK_WRITEABLE = 1,
  2204. DO_UNSET = 2
  2205. } command = 0;
  2206. struct option module_config_options[] = {
  2207. OPT_CMDMODE(0, "check-writeable", &command,
  2208. N_("check if it is safe to write to the .gitmodules file"),
  2209. CHECK_WRITEABLE),
  2210. OPT_CMDMODE(0, "unset", &command,
  2211. N_("unset the config in the .gitmodules file"),
  2212. DO_UNSET),
  2213. OPT_END()
  2214. };
  2215. const char *const git_submodule_helper_usage[] = {
  2216. N_("git submodule--helper config <name> [<value>]"),
  2217. N_("git submodule--helper config --unset <name>"),
  2218. N_("git submodule--helper config --check-writeable"),
  2219. NULL
  2220. };
  2221. argc = parse_options(argc, argv, prefix, module_config_options,
  2222. git_submodule_helper_usage, PARSE_OPT_KEEP_ARGV0);
  2223. if (argc == 1 && command == CHECK_WRITEABLE)
  2224. return is_writing_gitmodules_ok() ? 0 : -1;
  2225. /* Equivalent to ACTION_GET in builtin/config.c */
  2226. if (argc == 2 && command != DO_UNSET)
  2227. return print_config_from_gitmodules(the_repository, argv[1]);
  2228. /* Equivalent to ACTION_SET in builtin/config.c */
  2229. if (argc == 3 || (argc == 2 && command == DO_UNSET)) {
  2230. const char *value = (argc == 3) ? argv[2] : NULL;
  2231. if (!is_writing_gitmodules_ok())
  2232. die(_("please make sure that the .gitmodules file is in the working tree"));
  2233. return config_set_in_gitmodules_file_gently(argv[1], value);
  2234. }
  2235. usage_with_options(git_submodule_helper_usage, module_config_options);
  2236. }
  2237. static int module_set_url(int argc, const char **argv, const char *prefix)
  2238. {
  2239. int quiet = 0;
  2240. const char *newurl;
  2241. const char *path;
  2242. char *config_name;
  2243. struct option options[] = {
  2244. OPT__QUIET(&quiet, N_("Suppress output for setting url of a submodule")),
  2245. OPT_END()
  2246. };
  2247. const char *const usage[] = {
  2248. N_("git submodule--helper set-url [--quiet] <path> <newurl>"),
  2249. NULL
  2250. };
  2251. argc = parse_options(argc, argv, prefix, options, usage, 0);
  2252. if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1]))
  2253. usage_with_options(usage, options);
  2254. config_name = xstrfmt("submodule.%s.url", path);
  2255. config_set_in_gitmodules_file_gently(config_name, newurl);
  2256. sync_submodule(path, prefix, quiet ? OPT_QUIET : 0);
  2257. free(config_name);
  2258. return 0;
  2259. }
  2260. static int module_set_branch(int argc, const char **argv, const char *prefix)
  2261. {
  2262. int opt_default = 0, ret;
  2263. const char *opt_branch = NULL;
  2264. const char *path;
  2265. char *config_name;
  2266. /*
  2267. * We accept the `quiet` option for uniformity across subcommands,
  2268. * though there is nothing to make less verbose in this subcommand.
  2269. */
  2270. struct option options[] = {
  2271. OPT_NOOP_NOARG('q', "quiet"),
  2272. OPT_BOOL('d', "default", &opt_default,
  2273. N_("set the default tracking branch to master")),
  2274. OPT_STRING('b', "branch", &opt_branch, N_("branch"),
  2275. N_("set the default tracking branch")),
  2276. OPT_END()
  2277. };
  2278. const char *const usage[] = {
  2279. N_("git submodule--helper set-branch [-q|--quiet] (-d|--default) <path>"),
  2280. N_("git submodule--helper set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
  2281. NULL
  2282. };
  2283. argc = parse_options(argc, argv, prefix, options, usage, 0);
  2284. if (!opt_branch && !opt_default)
  2285. die(_("--branch or --default required"));
  2286. if (opt_branch && opt_default)
  2287. die(_("--branch and --default are mutually exclusive"));
  2288. if (argc != 1 || !(path = argv[0]))
  2289. usage_with_options(usage, options);
  2290. config_name = xstrfmt("submodule.%s.branch", path);
  2291. ret = config_set_in_gitmodules_file_gently(config_name, opt_branch);
  2292. free(config_name);
  2293. return !!ret;
  2294. }
  2295. #define SUPPORT_SUPER_PREFIX (1<<0)
  2296. struct cmd_struct {
  2297. const char *cmd;
  2298. int (*fn)(int, const char **, const char *);
  2299. unsigned option;
  2300. };
  2301. static struct cmd_struct commands[] = {
  2302. {"list", module_list, 0},
  2303. {"name", module_name, 0},
  2304. {"clone", module_clone, 0},
  2305. {"update-module-mode", module_update_module_mode, 0},
  2306. {"update-clone", update_clone, 0},
  2307. {"ensure-core-worktree", ensure_core_worktree, 0},
  2308. {"relative-path", resolve_relative_path, 0},
  2309. {"resolve-relative-url", resolve_relative_url, 0},
  2310. {"resolve-relative-url-test", resolve_relative_url_test, 0},
  2311. {"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
  2312. {"init", module_init, SUPPORT_SUPER_PREFIX},
  2313. {"status", module_status, SUPPORT_SUPER_PREFIX},
  2314. {"print-default-remote", print_default_remote, 0},
  2315. {"sync", module_sync, SUPPORT_SUPER_PREFIX},
  2316. {"deinit", module_deinit, 0},
  2317. {"summary", module_summary, SUPPORT_SUPER_PREFIX},
  2318. {"remote-branch", resolve_remote_submodule_branch, 0},
  2319. {"push-check", push_check, 0},
  2320. {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
  2321. {"is-active", is_active, 0},
  2322. {"check-name", check_name, 0},
  2323. {"config", module_config, 0},
  2324. {"set-url", module_set_url, 0},
  2325. {"set-branch", module_set_branch, 0},
  2326. };
  2327. int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
  2328. {
  2329. int i;
  2330. if (argc < 2 || !strcmp(argv[1], "-h"))
  2331. usage("git submodule--helper <command>");
  2332. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  2333. if (!strcmp(argv[1], commands[i].cmd)) {
  2334. if (get_super_prefix() &&
  2335. !(commands[i].option & SUPPORT_SUPER_PREFIX))
  2336. die(_("%s doesn't support --super-prefix"),
  2337. commands[i].cmd);
  2338. return commands[i].fn(argc - 1, argv + 1, prefix);
  2339. }
  2340. }
  2341. die(_("'%s' is not a valid submodule--helper "
  2342. "subcommand"), argv[1]);
  2343. }