MCPcopy Create free account
hub / github.com/git/git / create_default_gitdir_config

Function create_default_gitdir_config

builtin/submodule--helper.c:503–571  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

501}
502
503static void create_default_gitdir_config(const char *submodule_name)
504{
505 struct strbuf gitdir_path = STRBUF_INIT;
506 struct git_hash_ctx ctx;
507 char hex_name_hash[GIT_MAX_HEXSZ + 1], header[128];
508 unsigned char raw_name_hash[GIT_MAX_RAWSZ];
509 int header_len;
510
511 /* Case 1: try the plain module name */
512 repo_git_path_append(the_repository, &gitdir_path, "modules/%s", submodule_name);
513 if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name)) {
514 strbuf_release(&gitdir_path);
515 return;
516 }
517
518 /* Case 2.1: Try URI-safe (RFC3986) encoding first, this fixes nested gitdirs */
519 strbuf_reset(&gitdir_path);
520 repo_git_path_append(the_repository, &gitdir_path, "modules/");
521 strbuf_addstr_urlencode(&gitdir_path, submodule_name, is_rfc3986_unreserved);
522 if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name)) {
523 strbuf_release(&gitdir_path);
524 return;
525 }
526
527 /* Case 2.2: Try extended uppercase URI (RFC3986) encoding, to fix case-folding */
528 strbuf_reset(&gitdir_path);
529 repo_git_path_append(the_repository, &gitdir_path, "modules/");
530 strbuf_addstr_urlencode(&gitdir_path, submodule_name, is_casefolding_rfc3986_unreserved);
531 if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name))
532 return;
533
534 /* Case 2.3: Try some derived gitdir names, see if one sticks */
535 for (char c = '0'; c <= '9'; c++) {
536 strbuf_reset(&gitdir_path);
537 repo_git_path_append(the_repository, &gitdir_path, "modules/");
538 strbuf_addstr_urlencode(&gitdir_path, submodule_name, is_rfc3986_unreserved);
539 strbuf_addch(&gitdir_path, c);
540 if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name))
541 return;
542
543 strbuf_reset(&gitdir_path);
544 repo_git_path_append(the_repository, &gitdir_path, "modules/");
545 strbuf_addstr_urlencode(&gitdir_path, submodule_name, is_casefolding_rfc3986_unreserved);
546 strbuf_addch(&gitdir_path, c);
547 if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name))
548 return;
549 }
550
551 /* Case 2.4: If all the above failed, try a hash of the name as a last resort */
552 header_len = snprintf(header, sizeof(header), "blob %zu", strlen(submodule_name));
553 the_hash_algo->init_fn(&ctx);
554 the_hash_algo->update_fn(&ctx, header, header_len);
555 the_hash_algo->update_fn(&ctx, "\0", 1);
556 the_hash_algo->update_fn(&ctx, submodule_name, strlen(submodule_name));
557 the_hash_algo->final_fn(raw_name_hash, &ctx);
558 hash_to_hex_algop_r(hex_name_hash, raw_name_hash, the_hash_algo);
559 strbuf_reset(&gitdir_path);
560 repo_git_path_append(the_repository, &gitdir_path, "modules/%s", hex_name_hash);

Callers 3

init_submoduleFunction · 0.85
module_migrateFunction · 0.85
module_addFunction · 0.85

Calls 7

repo_git_path_appendFunction · 0.85
strbuf_releaseFunction · 0.85
strbuf_addstr_urlencodeFunction · 0.85
strbuf_addchFunction · 0.85
hash_to_hex_algop_rFunction · 0.85
dieFunction · 0.50

Tested by

no test coverage detected