Skip to content

Commit cbb388f

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1efe0ae commit cbb388f

File tree

11 files changed

+32
-25
lines changed

11 files changed

+32
-25
lines changed

builtin/fast-import.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,11 +821,11 @@ static char *keep_pack(const char *curr_index_name)
821821
die_errno("failed to write keep file");
822822

823823
odb_pack_name(pack_data->repo, &name, pack_data->hash, "pack");
824-
if (finalize_object_file(pack_data->pack_name, name.buf))
824+
if (finalize_object_file(pack_data->repo, pack_data->pack_name, name.buf))
825825
die("cannot store pack file");
826826

827827
odb_pack_name(pack_data->repo, &name, pack_data->hash, "idx");
828-
if (finalize_object_file(curr_index_name, name.buf))
828+
if (finalize_object_file(pack_data->repo, curr_index_name, name.buf))
829829
die("cannot store index file");
830830
free((void *)curr_index_name);
831831
return strbuf_detach(&name, NULL);

builtin/index-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,7 @@ static void rename_tmp_packfile(const char **final_name,
15981598
if (!*final_name || strcmp(*final_name, curr_name)) {
15991599
if (!*final_name)
16001600
*final_name = odb_pack_name(the_repository, name, hash, ext);
1601-
if (finalize_object_file(curr_name, *final_name))
1601+
if (finalize_object_file(the_repository, curr_name, *final_name))
16021602
die(_("unable to rename temporary '*.%s' file to '%s'"),
16031603
ext, *final_name);
16041604
} else if (make_read_only_if_same) {

builtin/pack-objects.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1449,7 +1449,7 @@ static void write_pack_file(void)
14491449
strbuf_setlen(&tmpname, tmpname_len);
14501450
}
14511451

1452-
rename_tmp_packfile_idx(&tmpname, &idx_tmp_name);
1452+
rename_tmp_packfile_idx(the_repository, &tmpname, &idx_tmp_name);
14531453

14541454
free(idx_tmp_name);
14551455
strbuf_release(&tmpname);

bulk-checkin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static void finish_tmp_packfile(struct strbuf *basename,
4646
stage_tmp_packfiles(the_repository, basename, pack_tmp_name,
4747
written_list, nr_written, NULL, pack_idx_opts, hash,
4848
&idx_tmp_name);
49-
rename_tmp_packfile_idx(basename, &idx_tmp_name);
49+
rename_tmp_packfile_idx(the_repository, basename, &idx_tmp_name);
5050

5151
free(idx_tmp_name);
5252
}

http.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,7 +2331,7 @@ int http_get_file(const char *url, const char *filename,
23312331
ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options);
23322332
fclose(result);
23332333

2334-
if (ret == HTTP_OK && finalize_object_file(tmpfile.buf, filename))
2334+
if (ret == HTTP_OK && finalize_object_file(the_repository, tmpfile.buf, filename))
23352335
ret = HTTP_ERROR;
23362336
cleanup:
23372337
strbuf_release(&tmpfile);
@@ -2815,7 +2815,7 @@ int finish_http_object_request(struct http_object_request *freq)
28152815
return -1;
28162816
}
28172817
odb_loose_path(the_repository->objects->sources, &filename, &freq->oid);
2818-
freq->rename = finalize_object_file(freq->tmpfile.buf, filename.buf);
2818+
freq->rename = finalize_object_file(the_repository, freq->tmpfile.buf, filename.buf);
28192819
strbuf_release(&filename);
28202820

28212821
return freq->rename;

midx-write.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ static void write_midx_reverse_index(struct write_midx_context *ctx,
667667
tmp_file = write_rev_file_order(ctx->repo, NULL, ctx->pack_order,
668668
ctx->entries_nr, midx_hash, WRITE_REV);
669669

670-
if (finalize_object_file(tmp_file, buf.buf))
670+
if (finalize_object_file(ctx->repo, tmp_file, buf.buf))
671671
die(_("cannot store reverse index file"));
672672

673673
strbuf_release(&buf);

object-file.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,14 @@ static int check_collision(const char *source, const char *dest)
584584
/*
585585
* Move the just written object into its final resting place.
586586
*/
587-
int finalize_object_file(const char *tmpfile, const char *filename)
587+
int finalize_object_file(struct repository *repo,
588+
const char *tmpfile, const char *filename)
588589
{
589-
return finalize_object_file_flags(tmpfile, filename, 0);
590+
return finalize_object_file_flags(repo, tmpfile, filename, 0);
590591
}
591592

592-
int finalize_object_file_flags(const char *tmpfile, const char *filename,
593+
int finalize_object_file_flags(struct repository *repo,
594+
const char *tmpfile, const char *filename,
593595
enum finalize_object_file_flags flags)
594596
{
595597
unsigned retries = 0;
@@ -649,7 +651,7 @@ int finalize_object_file_flags(const char *tmpfile, const char *filename,
649651
}
650652

651653
out:
652-
if (adjust_shared_perm(the_repository, filename))
654+
if (adjust_shared_perm(repo, filename))
653655
return error(_("unable to set permission to '%s'"), filename);
654656
return 0;
655657
}
@@ -889,7 +891,7 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
889891
warning_errno(_("failed utime() on %s"), tmp_file.buf);
890892
}
891893

892-
return finalize_object_file_flags(tmp_file.buf, filename.buf,
894+
return finalize_object_file_flags(the_repository, tmp_file.buf, filename.buf,
893895
FOF_SKIP_COLLISION_CHECK);
894896
}
895897

@@ -1020,7 +1022,7 @@ int stream_loose_object(struct input_stream *in_stream, size_t len,
10201022
strbuf_release(&dir);
10211023
}
10221024

1023-
err = finalize_object_file_flags(tmp_file.buf, filename.buf,
1025+
err = finalize_object_file_flags(the_repository, tmp_file.buf, filename.buf,
10241026
FOF_SKIP_COLLISION_CHECK);
10251027
if (!err && compat)
10261028
err = repo_add_loose_object_map(the_repository, oid, &compat_oid);

object-file.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,10 @@ enum finalize_object_file_flags {
218218
FOF_SKIP_COLLISION_CHECK = 1,
219219
};
220220

221-
int finalize_object_file(const char *tmpfile, const char *filename);
222-
int finalize_object_file_flags(const char *tmpfile, const char *filename,
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,
223225
enum finalize_object_file_flags flags);
224226

225227
void hash_object_file(const struct git_hash_algo *algo, const void *buf,

pack-write.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -538,22 +538,24 @@ struct hashfile *create_tmp_packfile(struct repository *repo,
538538
return hashfd(repo->hash_algo, fd, *pack_tmp_name);
539539
}
540540

541-
static void rename_tmp_packfile(struct strbuf *name_prefix, const char *source,
541+
static void rename_tmp_packfile(struct repository *repo,
542+
struct strbuf *name_prefix, const char *source,
542543
const char *ext)
543544
{
544545
size_t name_prefix_len = name_prefix->len;
545546

546547
strbuf_addstr(name_prefix, ext);
547-
if (finalize_object_file(source, name_prefix->buf))
548+
if (finalize_object_file(repo, source, name_prefix->buf))
548549
die("unable to rename temporary file to '%s'",
549550
name_prefix->buf);
550551
strbuf_setlen(name_prefix, name_prefix_len);
551552
}
552553

553-
void rename_tmp_packfile_idx(struct strbuf *name_buffer,
554+
void rename_tmp_packfile_idx(struct repository *repo,
555+
struct strbuf *name_buffer,
554556
char **idx_tmp_name)
555557
{
556-
rename_tmp_packfile(name_buffer, *idx_tmp_name, "idx");
558+
rename_tmp_packfile(repo, name_buffer, *idx_tmp_name, "idx");
557559
}
558560

559561
void stage_tmp_packfiles(struct repository *repo,
@@ -586,11 +588,11 @@ void stage_tmp_packfiles(struct repository *repo,
586588
hash);
587589
}
588590

589-
rename_tmp_packfile(name_buffer, pack_tmp_name, "pack");
591+
rename_tmp_packfile(repo, name_buffer, pack_tmp_name, "pack");
590592
if (rev_tmp_name)
591-
rename_tmp_packfile(name_buffer, rev_tmp_name, "rev");
593+
rename_tmp_packfile(repo, name_buffer, rev_tmp_name, "rev");
592594
if (mtimes_tmp_name)
593-
rename_tmp_packfile(name_buffer, mtimes_tmp_name, "mtimes");
595+
rename_tmp_packfile(repo, name_buffer, mtimes_tmp_name, "mtimes");
594596

595597
free(rev_tmp_name);
596598
free(mtimes_tmp_name);

pack.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ void stage_tmp_packfiles(struct repository *repo,
145145
struct pack_idx_option *pack_idx_opts,
146146
unsigned char hash[],
147147
char **idx_tmp_name);
148-
void rename_tmp_packfile_idx(struct strbuf *basename,
148+
void rename_tmp_packfile_idx(struct repository *repo,
149+
struct strbuf *basename,
149150
char **idx_tmp_name);
150151

151152
#endif

0 commit comments

Comments
 (0)