Git fork

object-file: get rid of `the_repository` in `finalize_object_file()`

We implicitly depend on `the_repository` when moving an object file into
place in `finalize_object_file()`. Get rid of this global dependency by
passing in a repository.

Note that one might be pressed to inject an object database instead of a
repository. But the function doesn't really care about the ODB at all.
All it does is to move a file into place while checking whether there is
any collision. As such, the functionality it provides is independent of
the object database and only needs the repository as parameter so that
it can adjust permissions of the file we are about to finalize.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Patrick Steinhardt and committed by
Junio C Hamano
cbb388f3 1efe0aea

+32 -25
+2 -2
builtin/fast-import.c
··· 821 die_errno("failed to write keep file"); 822 823 odb_pack_name(pack_data->repo, &name, pack_data->hash, "pack"); 824 - if (finalize_object_file(pack_data->pack_name, name.buf)) 825 die("cannot store pack file"); 826 827 odb_pack_name(pack_data->repo, &name, pack_data->hash, "idx"); 828 - if (finalize_object_file(curr_index_name, name.buf)) 829 die("cannot store index file"); 830 free((void *)curr_index_name); 831 return strbuf_detach(&name, NULL);
··· 821 die_errno("failed to write keep file"); 822 823 odb_pack_name(pack_data->repo, &name, pack_data->hash, "pack"); 824 + if (finalize_object_file(pack_data->repo, pack_data->pack_name, name.buf)) 825 die("cannot store pack file"); 826 827 odb_pack_name(pack_data->repo, &name, pack_data->hash, "idx"); 828 + if (finalize_object_file(pack_data->repo, curr_index_name, name.buf)) 829 die("cannot store index file"); 830 free((void *)curr_index_name); 831 return strbuf_detach(&name, NULL);
+1 -1
builtin/index-pack.c
··· 1598 if (!*final_name || strcmp(*final_name, curr_name)) { 1599 if (!*final_name) 1600 *final_name = odb_pack_name(the_repository, name, hash, ext); 1601 - if (finalize_object_file(curr_name, *final_name)) 1602 die(_("unable to rename temporary '*.%s' file to '%s'"), 1603 ext, *final_name); 1604 } else if (make_read_only_if_same) {
··· 1598 if (!*final_name || strcmp(*final_name, curr_name)) { 1599 if (!*final_name) 1600 *final_name = odb_pack_name(the_repository, name, hash, ext); 1601 + if (finalize_object_file(the_repository, curr_name, *final_name)) 1602 die(_("unable to rename temporary '*.%s' file to '%s'"), 1603 ext, *final_name); 1604 } else if (make_read_only_if_same) {
+1 -1
builtin/pack-objects.c
··· 1449 strbuf_setlen(&tmpname, tmpname_len); 1450 } 1451 1452 - rename_tmp_packfile_idx(&tmpname, &idx_tmp_name); 1453 1454 free(idx_tmp_name); 1455 strbuf_release(&tmpname);
··· 1449 strbuf_setlen(&tmpname, tmpname_len); 1450 } 1451 1452 + rename_tmp_packfile_idx(the_repository, &tmpname, &idx_tmp_name); 1453 1454 free(idx_tmp_name); 1455 strbuf_release(&tmpname);
+1 -1
bulk-checkin.c
··· 46 stage_tmp_packfiles(the_repository, basename, pack_tmp_name, 47 written_list, nr_written, NULL, pack_idx_opts, hash, 48 &idx_tmp_name); 49 - rename_tmp_packfile_idx(basename, &idx_tmp_name); 50 51 free(idx_tmp_name); 52 }
··· 46 stage_tmp_packfiles(the_repository, basename, pack_tmp_name, 47 written_list, nr_written, NULL, pack_idx_opts, hash, 48 &idx_tmp_name); 49 + rename_tmp_packfile_idx(the_repository, basename, &idx_tmp_name); 50 51 free(idx_tmp_name); 52 }
+2 -2
http.c
··· 2331 ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options); 2332 fclose(result); 2333 2334 - if (ret == HTTP_OK && finalize_object_file(tmpfile.buf, filename)) 2335 ret = HTTP_ERROR; 2336 cleanup: 2337 strbuf_release(&tmpfile); ··· 2815 return -1; 2816 } 2817 odb_loose_path(the_repository->objects->sources, &filename, &freq->oid); 2818 - freq->rename = finalize_object_file(freq->tmpfile.buf, filename.buf); 2819 strbuf_release(&filename); 2820 2821 return freq->rename;
··· 2331 ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options); 2332 fclose(result); 2333 2334 + if (ret == HTTP_OK && finalize_object_file(the_repository, tmpfile.buf, filename)) 2335 ret = HTTP_ERROR; 2336 cleanup: 2337 strbuf_release(&tmpfile); ··· 2815 return -1; 2816 } 2817 odb_loose_path(the_repository->objects->sources, &filename, &freq->oid); 2818 + freq->rename = finalize_object_file(the_repository, freq->tmpfile.buf, filename.buf); 2819 strbuf_release(&filename); 2820 2821 return freq->rename;
+1 -1
midx-write.c
··· 667 tmp_file = write_rev_file_order(ctx->repo, NULL, ctx->pack_order, 668 ctx->entries_nr, midx_hash, WRITE_REV); 669 670 - if (finalize_object_file(tmp_file, buf.buf)) 671 die(_("cannot store reverse index file")); 672 673 strbuf_release(&buf);
··· 667 tmp_file = write_rev_file_order(ctx->repo, NULL, ctx->pack_order, 668 ctx->entries_nr, midx_hash, WRITE_REV); 669 670 + if (finalize_object_file(ctx->repo, tmp_file, buf.buf)) 671 die(_("cannot store reverse index file")); 672 673 strbuf_release(&buf);
+8 -6
object-file.c
··· 584 /* 585 * Move the just written object into its final resting place. 586 */ 587 - int finalize_object_file(const char *tmpfile, const char *filename) 588 { 589 - return finalize_object_file_flags(tmpfile, filename, 0); 590 } 591 592 - int finalize_object_file_flags(const char *tmpfile, const char *filename, 593 enum finalize_object_file_flags flags) 594 { 595 unsigned retries = 0; ··· 649 } 650 651 out: 652 - if (adjust_shared_perm(the_repository, filename)) 653 return error(_("unable to set permission to '%s'"), filename); 654 return 0; 655 } ··· 889 warning_errno(_("failed utime() on %s"), tmp_file.buf); 890 } 891 892 - return finalize_object_file_flags(tmp_file.buf, filename.buf, 893 FOF_SKIP_COLLISION_CHECK); 894 } 895 ··· 1020 strbuf_release(&dir); 1021 } 1022 1023 - err = finalize_object_file_flags(tmp_file.buf, filename.buf, 1024 FOF_SKIP_COLLISION_CHECK); 1025 if (!err && compat) 1026 err = repo_add_loose_object_map(the_repository, oid, &compat_oid);
··· 584 /* 585 * Move the just written object into its final resting place. 586 */ 587 + int finalize_object_file(struct repository *repo, 588 + const char *tmpfile, const char *filename) 589 { 590 + return finalize_object_file_flags(repo, tmpfile, filename, 0); 591 } 592 593 + int finalize_object_file_flags(struct repository *repo, 594 + const char *tmpfile, const char *filename, 595 enum finalize_object_file_flags flags) 596 { 597 unsigned retries = 0; ··· 651 } 652 653 out: 654 + if (adjust_shared_perm(repo, filename)) 655 return error(_("unable to set permission to '%s'"), filename); 656 return 0; 657 } ··· 891 warning_errno(_("failed utime() on %s"), tmp_file.buf); 892 } 893 894 + return finalize_object_file_flags(the_repository, tmp_file.buf, filename.buf, 895 FOF_SKIP_COLLISION_CHECK); 896 } 897 ··· 1022 strbuf_release(&dir); 1023 } 1024 1025 + err = finalize_object_file_flags(the_repository, tmp_file.buf, filename.buf, 1026 FOF_SKIP_COLLISION_CHECK); 1027 if (!err && compat) 1028 err = repo_add_loose_object_map(the_repository, oid, &compat_oid);
+4 -2
object-file.h
··· 218 FOF_SKIP_COLLISION_CHECK = 1, 219 }; 220 221 - int finalize_object_file(const char *tmpfile, const char *filename); 222 - int finalize_object_file_flags(const char *tmpfile, const char *filename, 223 enum finalize_object_file_flags flags); 224 225 void hash_object_file(const struct git_hash_algo *algo, const void *buf,
··· 218 FOF_SKIP_COLLISION_CHECK = 1, 219 }; 220 221 + int finalize_object_file(struct repository *repo, 222 + const char *tmpfile, const char *filename); 223 + int finalize_object_file_flags(struct repository *repo, 224 + const char *tmpfile, const char *filename, 225 enum finalize_object_file_flags flags); 226 227 void hash_object_file(const struct git_hash_algo *algo, const void *buf,
+9 -7
pack-write.c
··· 538 return hashfd(repo->hash_algo, fd, *pack_tmp_name); 539 } 540 541 - static void rename_tmp_packfile(struct strbuf *name_prefix, const char *source, 542 const char *ext) 543 { 544 size_t name_prefix_len = name_prefix->len; 545 546 strbuf_addstr(name_prefix, ext); 547 - if (finalize_object_file(source, name_prefix->buf)) 548 die("unable to rename temporary file to '%s'", 549 name_prefix->buf); 550 strbuf_setlen(name_prefix, name_prefix_len); 551 } 552 553 - void rename_tmp_packfile_idx(struct strbuf *name_buffer, 554 char **idx_tmp_name) 555 { 556 - rename_tmp_packfile(name_buffer, *idx_tmp_name, "idx"); 557 } 558 559 void stage_tmp_packfiles(struct repository *repo, ··· 586 hash); 587 } 588 589 - rename_tmp_packfile(name_buffer, pack_tmp_name, "pack"); 590 if (rev_tmp_name) 591 - rename_tmp_packfile(name_buffer, rev_tmp_name, "rev"); 592 if (mtimes_tmp_name) 593 - rename_tmp_packfile(name_buffer, mtimes_tmp_name, "mtimes"); 594 595 free(rev_tmp_name); 596 free(mtimes_tmp_name);
··· 538 return hashfd(repo->hash_algo, fd, *pack_tmp_name); 539 } 540 541 + static void rename_tmp_packfile(struct repository *repo, 542 + struct strbuf *name_prefix, const char *source, 543 const char *ext) 544 { 545 size_t name_prefix_len = name_prefix->len; 546 547 strbuf_addstr(name_prefix, ext); 548 + if (finalize_object_file(repo, source, name_prefix->buf)) 549 die("unable to rename temporary file to '%s'", 550 name_prefix->buf); 551 strbuf_setlen(name_prefix, name_prefix_len); 552 } 553 554 + void rename_tmp_packfile_idx(struct repository *repo, 555 + struct strbuf *name_buffer, 556 char **idx_tmp_name) 557 { 558 + rename_tmp_packfile(repo, name_buffer, *idx_tmp_name, "idx"); 559 } 560 561 void stage_tmp_packfiles(struct repository *repo, ··· 588 hash); 589 } 590 591 + rename_tmp_packfile(repo, name_buffer, pack_tmp_name, "pack"); 592 if (rev_tmp_name) 593 + rename_tmp_packfile(repo, name_buffer, rev_tmp_name, "rev"); 594 if (mtimes_tmp_name) 595 + rename_tmp_packfile(repo, name_buffer, mtimes_tmp_name, "mtimes"); 596 597 free(rev_tmp_name); 598 free(mtimes_tmp_name);
+2 -1
pack.h
··· 145 struct pack_idx_option *pack_idx_opts, 146 unsigned char hash[], 147 char **idx_tmp_name); 148 - void rename_tmp_packfile_idx(struct strbuf *basename, 149 char **idx_tmp_name); 150 151 #endif
··· 145 struct pack_idx_option *pack_idx_opts, 146 unsigned char hash[], 147 char **idx_tmp_name); 148 + void rename_tmp_packfile_idx(struct repository *repo, 149 + struct strbuf *basename, 150 char **idx_tmp_name); 151 152 #endif
+1 -1
tmp-objdir.c
··· 227 return -1; 228 return migrate_paths(t, src, dst, flags); 229 } 230 - return finalize_object_file_flags(src->buf, dst->buf, flags); 231 } 232 233 static int is_loose_object_shard(const char *name)
··· 227 return -1; 228 return migrate_paths(t, src, dst, flags); 229 } 230 + return finalize_object_file_flags(t->repo, src->buf, dst->buf, flags); 231 } 232 233 static int is_loose_object_shard(const char *name)