diff mbox series

[v2,4/8] packfile: pass down repository to `odb_pack_name`

Message ID 73ba9945a7b7ec69e4ea29116c473b88e5c2a916.1730122499.git.karthik.188@gmail.com (mailing list archive)
State Superseded
Headers show
Series packfile: avoid using the 'the_repository' global variable | expand

Commit Message

karthik nayak Oct. 28, 2024, 1:43 p.m. UTC
The function `odb_pack_name` currently relies on the global variable
`the_repository`. To eliminate global variable usage in `packfile.c`, we
should progressively shift the dependency on the_repository to higher
layers.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 builtin/fast-import.c    | 8 ++++----
 builtin/index-pack.c     | 4 ++--
 builtin/pack-redundant.c | 4 ++--
 http.c                   | 2 +-
 packfile.c               | 9 ++++-----
 packfile.h               | 3 ++-
 6 files changed, 15 insertions(+), 15 deletions(-)

Comments

Taylor Blau Oct. 28, 2024, 4:14 p.m. UTC | #1
On Mon, Oct 28, 2024 at 02:43:42PM +0100, Karthik Nayak wrote:
> ---
>  builtin/fast-import.c    | 8 ++++----
>  builtin/index-pack.c     | 4 ++--
>  builtin/pack-redundant.c | 4 ++--
>  http.c                   | 2 +-
>  packfile.c               | 9 ++++-----
>  packfile.h               | 3 ++-
>  6 files changed, 15 insertions(+), 15 deletions(-)

All looking very sensible, nicely done.

Thanks,
Taylor
Jeff King Oct. 29, 2024, 5:50 a.m. UTC | #2
On Mon, Oct 28, 2024 at 02:43:42PM +0100, Karthik Nayak wrote:

> diff --git a/builtin/fast-import.c b/builtin/fast-import.c
> index ffee7d3abd..f4892d7f37 100644
> --- a/builtin/fast-import.c
> +++ b/builtin/fast-import.c
> @@ -806,7 +806,7 @@ static char *keep_pack(const char *curr_index_name)
>  	struct strbuf name = STRBUF_INIT;
>  	int keep_fd;
>  
> -	odb_pack_name(&name, pack_data->hash, "keep");
> +	odb_pack_name(the_repository, &name, pack_data->hash, "keep");

Why not pack_data->repo here? It's always going to be set to
the_repository in this program, but I think minimizing the number of
references to it still has value.

> @@ -814,11 +814,11 @@ static char *keep_pack(const char *curr_index_name)
>  	if (close(keep_fd))
>  		die_errno("failed to write keep file");
>  
> -	odb_pack_name(&name, pack_data->hash, "pack");
> +	odb_pack_name(the_repository, &name, pack_data->hash, "pack");
>  	if (finalize_object_file(pack_data->pack_name, name.buf))
>  		die("cannot store pack file");
>  
> -	odb_pack_name(&name, pack_data->hash, "idx");
> +	odb_pack_name(the_repository, &name, pack_data->hash, "idx");

Likewise in both of these spots.

>  	if (finalize_object_file(curr_index_name, name.buf))
>  		die("cannot store index file");
>  	free((void *)curr_index_name);
> @@ -832,7 +832,7 @@ static void unkeep_all_packs(void)
>  
>  	for (k = 0; k < pack_id; k++) {
>  		struct packed_git *p = all_packs[k];
> -		odb_pack_name(&name, p->hash, "keep");
> +		odb_pack_name(p->repo, &name, p->hash, "keep");

This one does use p->repo. Good.

> diff --git a/builtin/index-pack.c b/builtin/index-pack.c
> index be2f99625e..eaefb41761 100644
> --- a/builtin/index-pack.c
> +++ b/builtin/index-pack.c
> @@ -1479,7 +1479,7 @@ static void write_special_file(const char *suffix, const char *msg,
>  	if (pack_name)
>  		filename = derive_filename(pack_name, "pack", suffix, &name_buf);
>  	else
> -		filename = odb_pack_name(&name_buf, hash, suffix);
> +		filename = odb_pack_name(the_repository, &name_buf, hash, suffix);
>  
>  	fd = odb_pack_keep(filename);
>  	if (fd < 0) {
> @@ -1507,7 +1507,7 @@ static void rename_tmp_packfile(const char **final_name,
>  {
>  	if (!*final_name || strcmp(*final_name, curr_name)) {
>  		if (!*final_name)
> -			*final_name = odb_pack_name(name, hash, ext);
> +			*final_name = odb_pack_name(the_repository, name, hash, ext);

These two don't have a packed_git, so they use their own repo. Makes
sense.

> -int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, struct repository *repo UNUSED) {
> +int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, struct repository *repo) {
>  	int i; int i_still_use_this = 0; struct pack_list *min = NULL, *red, *pl;
>  	struct llist *ignore;
>  	struct strbuf idx_name = STRBUF_INIT;
> @@ -690,7 +690,7 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, s
>  	pl = red = pack_list_difference(local_packs, min);
>  	while (pl) {
>  		printf("%s\n%s\n",
> -		       odb_pack_name(&idx_name, pl->pack->hash, "idx"),
> +		       odb_pack_name(repo, &idx_name, pl->pack->hash, "idx"),
>  		       pl->pack->pack_name);
>  		pl = pl->next;
>  	}

This one is using the "repo" variable passed to the main function. That
seems a little sketchy to me philosophically, though, because these
packs all came from a call to get_all_packs(the_repository). I think
the two will always be the same, but it feels like we should be using
pl->pack->repo here for consistency.

> diff --git a/http.c b/http.c
> index 7e5be05207..50d8811cea 100644
> --- a/http.c
> +++ b/http.c
> @@ -2579,7 +2579,7 @@ struct http_pack_request *new_direct_http_pack_request(
>  
>  	preq->url = url;
>  
> -	odb_pack_name(&preq->tmpfile, packed_git_hash, "pack");
> +	odb_pack_name(the_repository, &preq->tmpfile, packed_git_hash, "pack");
>  	strbuf_addstr(&preq->tmpfile, ".temp");
>  	preq->packfile = fopen(preq->tmpfile.buf, "a");
>  	if (!preq->packfile) {

This one really ought to be using the packed_git we set up for the
matching idx file, but we won't have passed it through. And it's not
worth heavy refactoring just to get access to it, IMHO.


Earlier I mentioned that another helper could simplify many of these
sites a little. What I meant was this (on top of what's in your series):

diff --git a/builtin/fast-import.c b/builtin/fast-import.c
index 9056447bd0..976cb1d77b 100644
--- a/builtin/fast-import.c
+++ b/builtin/fast-import.c
@@ -806,19 +806,19 @@ static char *keep_pack(const char *curr_index_name)
 	struct strbuf name = STRBUF_INIT;
 	int keep_fd;
 
-	odb_pack_name(the_repository, &name, pack_data->hash, "keep");
+	pack_hashfile(pack_data, &name, "keep");
 	keep_fd = odb_pack_keep(name.buf);
 	if (keep_fd < 0)
 		die_errno("cannot create keep file");
 	write_or_die(keep_fd, keep_msg, strlen(keep_msg));
 	if (close(keep_fd))
 		die_errno("failed to write keep file");
 
-	odb_pack_name(the_repository, &name, pack_data->hash, "pack");
+	pack_hashfile(pack_data, &name, "pack");
 	if (finalize_object_file(pack_data->pack_name, name.buf))
 		die("cannot store pack file");
 
-	odb_pack_name(the_repository, &name, pack_data->hash, "idx");
+	pack_hashfile(pack_data, &name, "idx");
 	if (finalize_object_file(curr_index_name, name.buf))
 		die("cannot store index file");
 	free((void *)curr_index_name);
@@ -832,7 +832,7 @@ static void unkeep_all_packs(void)
 
 	for (k = 0; k < pack_id; k++) {
 		struct packed_git *p = all_packs[k];
-		odb_pack_name(p->repo, &name, p->hash, "keep");
+		pack_hashfile(p, &name, "keep");
 		unlink_or_warn(name.buf);
 	}
 	strbuf_release(&name);
diff --git a/builtin/pack-redundant.c b/builtin/pack-redundant.c
index 7d6c47ffd9..d3b5e7e112 100644
--- a/builtin/pack-redundant.c
+++ b/builtin/pack-redundant.c
@@ -690,7 +690,7 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, s
 	pl = red = pack_list_difference(local_packs, min);
 	while (pl) {
 		printf("%s\n%s\n",
-		       odb_pack_name(repo, &idx_name, pl->pack->hash, "idx"),
+		       pack_hashfile(pl->pack, &idx_name, "idx"),
 		       pl->pack->pack_name);
 		pl = pl->next;
G 	}
diff --git a/packfile.c b/packfile.c
index cfbfcdc2b8..d81a62eb84 100644
--- a/packfile.c
+++ b/packfile.c
@@ -46,6 +46,11 @@ char *odb_pack_name(struct repository *repo, struct strbuf *buf,
 	return buf->buf;
 }
 
+char *pack_hashfile(struct packed_git *p, struct strbuf *out, const char *ext)
+{
+	return odb_pack_name(p->repo, out, p->hash, ext);
+}
+
 static unsigned int pack_used_ctr;
 static unsigned int pack_mmap_calls;
 static unsigned int peak_pack_open_windows;
diff --git a/packfile.h b/packfile.h
index 3409aef35d..43c19d7bba 100644
--- a/packfile.h
+++ b/packfile.h
@@ -32,6 +32,9 @@ struct pack_entry {
 char *odb_pack_name(struct repository *repo, struct strbuf *buf,
 		    const unsigned char *hash, const char *ext);
 
+/* Like odb_pack_name(), but pull repo and hash from existing packed_git. */
+char *pack_hashfile(struct packed_git *p, struct strbuf *out, const char *ext);
+
 /*
  * Return the basename of the packfile, omitting any containing directory
  * (e.g., "pack-1234abcd[...].pack").


While coming up with the name, though, I had some second thoughts. The
interface implies that its the way you should derive a pack-related
filename from a packed_git. But it really is mis-designed for that
purpose! The packed_git struct has "foo.pack" or similar in its
pack_name field, and the correct way to derive the .idx, .bitmap, .keep,
etc, is by string substitution. While we do tend to name packs
pack-$hash.pack, most of the code will happily work on
"some-arbitrary-name.pack". And that's why we have so few
odb_pack_name() calls in the first place.

IMHO the ones in fast-import should probably be doing that suffix
replacement instead (and probably we should have a decent helper to
facilitate that; you can grep for strip_suffix.*pack to see places that
could potentially use it).

All that said, I don't think it's worth derailing your series to deal
with that cleanup. That can come later if we want. And if we do that,
then the pack_hashfile() I suggested above would have no callers,
because it's the wrong approach.

I do think it's probably worth changing your series to use the
packed_git repo pointers we already have available, though (i.e., the
cases I pointed out inline above).

-Peff
karthik nayak Oct. 29, 2024, 12:45 p.m. UTC | #3
Jeff King <peff@peff.net> writes:

> On Mon, Oct 28, 2024 at 02:43:42PM +0100, Karthik Nayak wrote:
>
>> diff --git a/builtin/fast-import.c b/builtin/fast-import.c
>> index ffee7d3abd..f4892d7f37 100644
>> --- a/builtin/fast-import.c
>> +++ b/builtin/fast-import.c
>> @@ -806,7 +806,7 @@ static char *keep_pack(const char *curr_index_name)
>>  	struct strbuf name = STRBUF_INIT;
>>  	int keep_fd;
>>
>> -	odb_pack_name(&name, pack_data->hash, "keep");
>> +	odb_pack_name(the_repository, &name, pack_data->hash, "keep");
>
> Why not pack_data->repo here? It's always going to be set to
> the_repository in this program, but I think minimizing the number of
> references to it still has value.
>

I tried to swap out 'the_repository' with local variables in most places
I could. Here, I totally missed 'pack_data', although it was right
there. Thanks will swap out.

>> @@ -814,11 +814,11 @@ static char *keep_pack(const char *curr_index_name)
>>  	if (close(keep_fd))
>>  		die_errno("failed to write keep file");
>>
>> -	odb_pack_name(&name, pack_data->hash, "pack");
>> +	odb_pack_name(the_repository, &name, pack_data->hash, "pack");
>>  	if (finalize_object_file(pack_data->pack_name, name.buf))
>>  		die("cannot store pack file");
>>
>> -	odb_pack_name(&name, pack_data->hash, "idx");
>> +	odb_pack_name(the_repository, &name, pack_data->hash, "idx");
>
> Likewise in both of these spots.
>
>>  	if (finalize_object_file(curr_index_name, name.buf))
>>  		die("cannot store index file");
>>  	free((void *)curr_index_name);
>> @@ -832,7 +832,7 @@ static void unkeep_all_packs(void)
>>
>>  	for (k = 0; k < pack_id; k++) {
>>  		struct packed_git *p = all_packs[k];
>> -		odb_pack_name(&name, p->hash, "keep");
>> +		odb_pack_name(p->repo, &name, p->hash, "keep");
>
> This one does use p->repo. Good.
>
>> diff --git a/builtin/index-pack.c b/builtin/index-pack.c
>> index be2f99625e..eaefb41761 100644
>> --- a/builtin/index-pack.c
>> +++ b/builtin/index-pack.c
>> @@ -1479,7 +1479,7 @@ static void write_special_file(const char *suffix, const char *msg,
>>  	if (pack_name)
>>  		filename = derive_filename(pack_name, "pack", suffix, &name_buf);
>>  	else
>> -		filename = odb_pack_name(&name_buf, hash, suffix);
>> +		filename = odb_pack_name(the_repository, &name_buf, hash, suffix);
>>
>>  	fd = odb_pack_keep(filename);
>>  	if (fd < 0) {
>> @@ -1507,7 +1507,7 @@ static void rename_tmp_packfile(const char **final_name,
>>  {
>>  	if (!*final_name || strcmp(*final_name, curr_name)) {
>>  		if (!*final_name)
>> -			*final_name = odb_pack_name(name, hash, ext);
>> +			*final_name = odb_pack_name(the_repository, name, hash, ext);
>
> These two don't have a packed_git, so they use their own repo. Makes
> sense.
>
>> -int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, struct repository *repo UNUSED) {
>> +int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, struct repository *repo) {
>>  	int i; int i_still_use_this = 0; struct pack_list *min = NULL, *red, *pl;
>>  	struct llist *ignore;
>>  	struct strbuf idx_name = STRBUF_INIT;
>> @@ -690,7 +690,7 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, s
>>  	pl = red = pack_list_difference(local_packs, min);
>>  	while (pl) {
>>  		printf("%s\n%s\n",
>> -		       odb_pack_name(&idx_name, pl->pack->hash, "idx"),
>> +		       odb_pack_name(repo, &idx_name, pl->pack->hash, "idx"),
>>  		       pl->pack->pack_name);
>>  		pl = pl->next;
>>  	}
>
> This one is using the "repo" variable passed to the main function. That
> seems a little sketchy to me philosophically, though, because these
> packs all came from a call to get_all_packs(the_repository). I think
> the two will always be the same, but it feels like we should be using
> pl->pack->repo here for consistency.
>

Yeah, this seems more appropriate indeed. I will swap out.

>> diff --git a/http.c b/http.c
>> index 7e5be05207..50d8811cea 100644
>> --- a/http.c
>> +++ b/http.c
>> @@ -2579,7 +2579,7 @@ struct http_pack_request *new_direct_http_pack_request(
>>
>>  	preq->url = url;
>>
>> -	odb_pack_name(&preq->tmpfile, packed_git_hash, "pack");
>> +	odb_pack_name(the_repository, &preq->tmpfile, packed_git_hash, "pack");
>>  	strbuf_addstr(&preq->tmpfile, ".temp");
>>  	preq->packfile = fopen(preq->tmpfile.buf, "a");
>>  	if (!preq->packfile) {
>
> This one really ought to be using the packed_git we set up for the
> matching idx file, but we won't have passed it through. And it's not
> worth heavy refactoring just to get access to it, IMHO.
>
>
> Earlier I mentioned that another helper could simplify many of these
> sites a little. What I meant was this (on top of what's in your series):
>
> diff --git a/builtin/fast-import.c b/builtin/fast-import.c
> index 9056447bd0..976cb1d77b 100644
> --- a/builtin/fast-import.c
> +++ b/builtin/fast-import.c
> @@ -806,19 +806,19 @@ static char *keep_pack(const char *curr_index_name)
>  	struct strbuf name = STRBUF_INIT;
>  	int keep_fd;
>
> -	odb_pack_name(the_repository, &name, pack_data->hash, "keep");
> +	pack_hashfile(pack_data, &name, "keep");
>  	keep_fd = odb_pack_keep(name.buf);
>  	if (keep_fd < 0)
>  		die_errno("cannot create keep file");
>  	write_or_die(keep_fd, keep_msg, strlen(keep_msg));
>  	if (close(keep_fd))
>  		die_errno("failed to write keep file");
>
> -	odb_pack_name(the_repository, &name, pack_data->hash, "pack");
> +	pack_hashfile(pack_data, &name, "pack");
>  	if (finalize_object_file(pack_data->pack_name, name.buf))
>  		die("cannot store pack file");
>
> -	odb_pack_name(the_repository, &name, pack_data->hash, "idx");
> +	pack_hashfile(pack_data, &name, "idx");
>  	if (finalize_object_file(curr_index_name, name.buf))
>  		die("cannot store index file");
>  	free((void *)curr_index_name);
> @@ -832,7 +832,7 @@ static void unkeep_all_packs(void)
>
>  	for (k = 0; k < pack_id; k++) {
>  		struct packed_git *p = all_packs[k];
> -		odb_pack_name(p->repo, &name, p->hash, "keep");
> +		pack_hashfile(p, &name, "keep");
>  		unlink_or_warn(name.buf);
>  	}
>  	strbuf_release(&name);
> diff --git a/builtin/pack-redundant.c b/builtin/pack-redundant.c
> index 7d6c47ffd9..d3b5e7e112 100644
> --- a/builtin/pack-redundant.c
> +++ b/builtin/pack-redundant.c
> @@ -690,7 +690,7 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, s
>  	pl = red = pack_list_difference(local_packs, min);
>  	while (pl) {
>  		printf("%s\n%s\n",
> -		       odb_pack_name(repo, &idx_name, pl->pack->hash, "idx"),
> +		       pack_hashfile(pl->pack, &idx_name, "idx"),
>  		       pl->pack->pack_name);
>  		pl = pl->next;
> G 	}
> diff --git a/packfile.c b/packfile.c
> index cfbfcdc2b8..d81a62eb84 100644
> --- a/packfile.c
> +++ b/packfile.c
> @@ -46,6 +46,11 @@ char *odb_pack_name(struct repository *repo, struct strbuf *buf,
>  	return buf->buf;
>  }
>
> +char *pack_hashfile(struct packed_git *p, struct strbuf *out, const char *ext)
> +{
> +	return odb_pack_name(p->repo, out, p->hash, ext);
> +}
> +
>  static unsigned int pack_used_ctr;
>  static unsigned int pack_mmap_calls;
>  static unsigned int peak_pack_open_windows;
> diff --git a/packfile.h b/packfile.h
> index 3409aef35d..43c19d7bba 100644
> --- a/packfile.h
> +++ b/packfile.h
> @@ -32,6 +32,9 @@ struct pack_entry {
>  char *odb_pack_name(struct repository *repo, struct strbuf *buf,
>  		    const unsigned char *hash, const char *ext);
>
> +/* Like odb_pack_name(), but pull repo and hash from existing packed_git. */
> +char *pack_hashfile(struct packed_git *p, struct strbuf *out, const char *ext);
> +
>  /*
>   * Return the basename of the packfile, omitting any containing directory
>   * (e.g., "pack-1234abcd[...].pack").
>
>
> While coming up with the name, though, I had some second thoughts. The
> interface implies that its the way you should derive a pack-related
> filename from a packed_git. But it really is mis-designed for that
> purpose! The packed_git struct has "foo.pack" or similar in its
> pack_name field, and the correct way to derive the .idx, .bitmap, .keep,
> etc, is by string substitution. While we do tend to name packs
> pack-$hash.pack, most of the code will happily work on
> "some-arbitrary-name.pack". And that's why we have so few
> odb_pack_name() calls in the first place.
>
> IMHO the ones in fast-import should probably be doing that suffix
> replacement instead (and probably we should have a decent helper to
> facilitate that; you can grep for strip_suffix.*pack to see places that
> could potentially use it).
>
> All that said, I don't think it's worth derailing your series to deal
> with that cleanup. That can come later if we want. And if we do that,
> then the pack_hashfile() I suggested above would have no callers,
> because it's the wrong approach.

Thanks for the detailed explanation. Maybe we should mark this as
#leftoverbits for a future cleanup.

>
> I do think it's probably worth changing your series to use the
> packed_git repo pointers we already have available, though (i.e., the
> cases I pointed out inline above).
>

Yes, this I've incorporated into the next version. Thanks.

> -Peff
Taylor Blau Oct. 29, 2024, 5:33 p.m. UTC | #4
On Tue, Oct 29, 2024 at 01:50:39AM -0400, Jeff King wrote:
> On Mon, Oct 28, 2024 at 02:43:42PM +0100, Karthik Nayak wrote:
>
> > diff --git a/builtin/fast-import.c b/builtin/fast-import.c
> > index ffee7d3abd..f4892d7f37 100644
> > --- a/builtin/fast-import.c
> > +++ b/builtin/fast-import.c
> > @@ -806,7 +806,7 @@ static char *keep_pack(const char *curr_index_name)
> >  	struct strbuf name = STRBUF_INIT;
> >  	int keep_fd;
> >
> > -	odb_pack_name(&name, pack_data->hash, "keep");
> > +	odb_pack_name(the_repository, &name, pack_data->hash, "keep");
>
> Why not pack_data->repo here? It's always going to be set to
> the_repository in this program, but I think minimizing the number of
> references to it still has value.

Yeah, I had pointed out a similar thing when I looked at this patch in
the message above yours in this thread.

I think we reached the same conclusion that this isn't strictly
incorrect, because in all of the instances that I looked at, p->repo is
equal to the_repository, so from an external behavior perspective, the
two are equivalent choices.

But I agree that the point is to *use* p->repo and not rely directly on
'the_repository', so that your suggestion here is a good one.

> Earlier I mentioned that another helper could simplify many of these
> sites a little. What I meant was this (on top of what's in your series):
>
> diff --git a/builtin/fast-import.c b/builtin/fast-import.c
> index 9056447bd0..976cb1d77b 100644
> --- a/builtin/fast-import.c
> +++ b/builtin/fast-import.c
> @@ -806,19 +806,19 @@ static char *keep_pack(const char *curr_index_name)
>  	struct strbuf name = STRBUF_INIT;
>  	int keep_fd;
>
> -	odb_pack_name(the_repository, &name, pack_data->hash, "keep");
> +	pack_hashfile(pack_data, &name, "keep");
>  	keep_fd = odb_pack_keep(name.buf);
>  	if (keep_fd < 0)
>  		die_errno("cannot create keep file");
>  	write_or_die(keep_fd, keep_msg, strlen(keep_msg));
>  	if (close(keep_fd))
>  		die_errno("failed to write keep file");
>
> -	odb_pack_name(the_repository, &name, pack_data->hash, "pack");
> +	pack_hashfile(pack_data, &name, "pack");
>  	if (finalize_object_file(pack_data->pack_name, name.buf))
>  		die("cannot store pack file");
>
> -	odb_pack_name(the_repository, &name, pack_data->hash, "idx");
> +	pack_hashfile(pack_data, &name, "idx");
>  	if (finalize_object_file(curr_index_name, name.buf))
>  		die("cannot store index file");
>  	free((void *)curr_index_name);
> @@ -832,7 +832,7 @@ static void unkeep_all_packs(void)
>
>  	for (k = 0; k < pack_id; k++) {
>  		struct packed_git *p = all_packs[k];
> -		odb_pack_name(p->repo, &name, p->hash, "keep");
> +		pack_hashfile(p, &name, "keep");
>  		unlink_or_warn(name.buf);
>  	}
>  	strbuf_release(&name);
> diff --git a/builtin/pack-redundant.c b/builtin/pack-redundant.c
> index 7d6c47ffd9..d3b5e7e112 100644
> --- a/builtin/pack-redundant.c
> +++ b/builtin/pack-redundant.c
> @@ -690,7 +690,7 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, s
>  	pl = red = pack_list_difference(local_packs, min);
>  	while (pl) {
>  		printf("%s\n%s\n",
> -		       odb_pack_name(repo, &idx_name, pl->pack->hash, "idx"),
> +		       pack_hashfile(pl->pack, &idx_name, "idx"),
>  		       pl->pack->pack_name);
>  		pl = pl->next;
> G 	}
> diff --git a/packfile.c b/packfile.c
> index cfbfcdc2b8..d81a62eb84 100644
> --- a/packfile.c
> +++ b/packfile.c
> @@ -46,6 +46,11 @@ char *odb_pack_name(struct repository *repo, struct strbuf *buf,
>  	return buf->buf;
>  }
>
> +char *pack_hashfile(struct packed_git *p, struct strbuf *out, const char *ext)
> +{
> +	return odb_pack_name(p->repo, out, p->hash, ext);
> +}
> +
>  static unsigned int pack_used_ctr;
>  static unsigned int pack_mmap_calls;
>  static unsigned int peak_pack_open_windows;
> diff --git a/packfile.h b/packfile.h
> index 3409aef35d..43c19d7bba 100644
> --- a/packfile.h
> +++ b/packfile.h
> @@ -32,6 +32,9 @@ struct pack_entry {
>  char *odb_pack_name(struct repository *repo, struct strbuf *buf,
>  		    const unsigned char *hash, const char *ext);
>
> +/* Like odb_pack_name(), but pull repo and hash from existing packed_git. */
> +char *pack_hashfile(struct packed_git *p, struct strbuf *out, const char *ext);
> +
>  /*
>   * Return the basename of the packfile, omitting any containing directory
>   * (e.g., "pack-1234abcd[...].pack").
>
>
> While coming up with the name, though, I had some second thoughts. The
> interface implies that its the way you should derive a pack-related
> filename from a packed_git. But it really is mis-designed for that
> purpose! The packed_git struct has "foo.pack" or similar in its
> pack_name field, and the correct way to derive the .idx, .bitmap, .keep,
> etc, is by string substitution. While we do tend to name packs
> pack-$hash.pack, most of the code will happily work on
> "some-arbitrary-name.pack". And that's why we have so few
> odb_pack_name() calls in the first place.
>
> IMHO the ones in fast-import should probably be doing that suffix
> replacement instead (and probably we should have a decent helper to
> facilitate that; you can grep for strip_suffix.*pack to see places that
> could potentially use it).
>
> All that said, I don't think it's worth derailing your series to deal
> with that cleanup. That can come later if we want. And if we do that,
> then the pack_hashfile() I suggested above would have no callers,
> because it's the wrong approach.

Heh. I feel like you and I just discussed this on the list together a
couple of days ago. Indeed, there are quite a few that would benefit
from such a cleanup (there are even more if you search for
'strip_suffix.*idx', which would work similarly).

> I do think it's probably worth changing your series to use the
> packed_git repo pointers we already have available, though (i.e., the
> cases I pointed out inline above).

But yeah, we can take that up as a secondary step on top of this series
if we wend up wanting to do that in the future.

Thanks,
Taylor
diff mbox series

Patch

diff --git a/builtin/fast-import.c b/builtin/fast-import.c
index ffee7d3abd..f4892d7f37 100644
--- a/builtin/fast-import.c
+++ b/builtin/fast-import.c
@@ -806,7 +806,7 @@  static char *keep_pack(const char *curr_index_name)
 	struct strbuf name = STRBUF_INIT;
 	int keep_fd;
 
-	odb_pack_name(&name, pack_data->hash, "keep");
+	odb_pack_name(the_repository, &name, pack_data->hash, "keep");
 	keep_fd = odb_pack_keep(name.buf);
 	if (keep_fd < 0)
 		die_errno("cannot create keep file");
@@ -814,11 +814,11 @@  static char *keep_pack(const char *curr_index_name)
 	if (close(keep_fd))
 		die_errno("failed to write keep file");
 
-	odb_pack_name(&name, pack_data->hash, "pack");
+	odb_pack_name(the_repository, &name, pack_data->hash, "pack");
 	if (finalize_object_file(pack_data->pack_name, name.buf))
 		die("cannot store pack file");
 
-	odb_pack_name(&name, pack_data->hash, "idx");
+	odb_pack_name(the_repository, &name, pack_data->hash, "idx");
 	if (finalize_object_file(curr_index_name, name.buf))
 		die("cannot store index file");
 	free((void *)curr_index_name);
@@ -832,7 +832,7 @@  static void unkeep_all_packs(void)
 
 	for (k = 0; k < pack_id; k++) {
 		struct packed_git *p = all_packs[k];
-		odb_pack_name(&name, p->hash, "keep");
+		odb_pack_name(p->repo, &name, p->hash, "keep");
 		unlink_or_warn(name.buf);
 	}
 	strbuf_release(&name);
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index be2f99625e..eaefb41761 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -1479,7 +1479,7 @@  static void write_special_file(const char *suffix, const char *msg,
 	if (pack_name)
 		filename = derive_filename(pack_name, "pack", suffix, &name_buf);
 	else
-		filename = odb_pack_name(&name_buf, hash, suffix);
+		filename = odb_pack_name(the_repository, &name_buf, hash, suffix);
 
 	fd = odb_pack_keep(filename);
 	if (fd < 0) {
@@ -1507,7 +1507,7 @@  static void rename_tmp_packfile(const char **final_name,
 {
 	if (!*final_name || strcmp(*final_name, curr_name)) {
 		if (!*final_name)
-			*final_name = odb_pack_name(name, hash, ext);
+			*final_name = odb_pack_name(the_repository, name, hash, ext);
 		if (finalize_object_file(curr_name, *final_name))
 			die(_("unable to rename temporary '*.%s' file to '%s'"),
 			    ext, *final_name);
diff --git a/builtin/pack-redundant.c b/builtin/pack-redundant.c
index d2c1c4e5ec..7d6c47ffd9 100644
--- a/builtin/pack-redundant.c
+++ b/builtin/pack-redundant.c
@@ -589,7 +589,7 @@  static void load_all(void)
 	}
 }
 
-int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, struct repository *repo UNUSED) {
+int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, struct repository *repo) {
 	int i; int i_still_use_this = 0; struct pack_list *min = NULL, *red, *pl;
 	struct llist *ignore;
 	struct strbuf idx_name = STRBUF_INIT;
@@ -690,7 +690,7 @@  int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, s
 	pl = red = pack_list_difference(local_packs, min);
 	while (pl) {
 		printf("%s\n%s\n",
-		       odb_pack_name(&idx_name, pl->pack->hash, "idx"),
+		       odb_pack_name(repo, &idx_name, pl->pack->hash, "idx"),
 		       pl->pack->pack_name);
 		pl = pl->next;
 	}
diff --git a/http.c b/http.c
index 7e5be05207..50d8811cea 100644
--- a/http.c
+++ b/http.c
@@ -2579,7 +2579,7 @@  struct http_pack_request *new_direct_http_pack_request(
 
 	preq->url = url;
 
-	odb_pack_name(&preq->tmpfile, packed_git_hash, "pack");
+	odb_pack_name(the_repository, &preq->tmpfile, packed_git_hash, "pack");
 	strbuf_addstr(&preq->tmpfile, ".temp");
 	preq->packfile = fopen(preq->tmpfile.buf, "a");
 	if (!preq->packfile) {
diff --git a/packfile.c b/packfile.c
index cc558f06cc..096a0cd6ba 100644
--- a/packfile.c
+++ b/packfile.c
@@ -25,13 +25,12 @@ 
 #include "pack-revindex.h"
 #include "promisor-remote.h"
 
-char *odb_pack_name(struct strbuf *buf,
-		    const unsigned char *hash,
-		    const char *ext)
+char *odb_pack_name(struct repository *repo, struct strbuf *buf,
+		    const unsigned char *hash, const char *ext)
 {
 	strbuf_reset(buf);
-	strbuf_addf(buf, "%s/pack/pack-%s.%s", repo_get_object_directory(the_repository),
-		    hash_to_hex(hash), ext);
+	strbuf_addf(buf, "%s/pack/pack-%s.%s", repo_get_object_directory(repo),
+		    hash_to_hex_algop(hash, repo->hash_algo), ext);
 	return buf->buf;
 }
 
diff --git a/packfile.h b/packfile.h
index 344da905c2..48d058699d 100644
--- a/packfile.h
+++ b/packfile.h
@@ -29,7 +29,8 @@  struct pack_entry {
  *
  * Example: odb_pack_name(out, sha1, "idx") => ".git/objects/pack/pack-1234..idx"
  */
-char *odb_pack_name(struct strbuf *buf, const unsigned char *sha1, const char *ext);
+char *odb_pack_name(struct repository *repo, struct strbuf *buf,
+		    const unsigned char *hash, const char *ext);
 
 /*
  * Return the basename of the packfile, omitting any containing directory