Skip to content

Commit 3115ee4

Browse files
peffgitster
authored andcommitted
cat-file: sort and de-dup output of --batch-all-objects
The sorting we could probably live without, but printing duplicates is just a hassle for the user, who must then de-dup themselves (or risk a wrong answer if they are doing something like counting objects with a particular property). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 6a95193 commit 3115ee4

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

Documentation/git-cat-file.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ OPTIONS
7474
requested batch operation on all objects in the repository and
7575
any alternate object stores (not just reachable objects).
7676
Requires `--batch` or `--batch-check` be specified. Note that
77-
the order of the objects is unspecified, and there may be
78-
duplicate entries.
77+
the objects are visited in order sorted by their hashes.
7978

8079
--buffer::
8180
Normally batch output is flushed after each object is output, so

builtin/cat-file.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "userdiff.h"
1010
#include "streaming.h"
1111
#include "tree-walk.h"
12+
#include "sha1-array.h"
1213

1314
struct batch_options {
1415
int enabled;
@@ -324,27 +325,28 @@ struct object_cb_data {
324325
struct expand_data *expand;
325326
};
326327

327-
static int batch_object_cb(const unsigned char *sha1,
328-
struct object_cb_data *data)
328+
static void batch_object_cb(const unsigned char sha1[20], void *vdata)
329329
{
330+
struct object_cb_data *data = vdata;
330331
hashcpy(data->expand->sha1, sha1);
331332
batch_object_write(NULL, data->opt, data->expand);
332-
return 0;
333333
}
334334

335335
static int batch_loose_object(const unsigned char *sha1,
336336
const char *path,
337337
void *data)
338338
{
339-
return batch_object_cb(sha1, data);
339+
sha1_array_append(data, sha1);
340+
return 0;
340341
}
341342

342343
static int batch_packed_object(const unsigned char *sha1,
343344
struct packed_git *pack,
344345
uint32_t pos,
345346
void *data)
346347
{
347-
return batch_object_cb(sha1, data);
348+
sha1_array_append(data, sha1);
349+
return 0;
348350
}
349351

350352
static int batch_objects(struct batch_options *opt)
@@ -375,11 +377,17 @@ static int batch_objects(struct batch_options *opt)
375377
data.info.typep = &data.type;
376378

377379
if (opt->all_objects) {
380+
struct sha1_array sa = SHA1_ARRAY_INIT;
378381
struct object_cb_data cb;
382+
383+
for_each_loose_object(batch_loose_object, &sa, 0);
384+
for_each_packed_object(batch_packed_object, &sa, 0);
385+
379386
cb.opt = opt;
380387
cb.expand = &data;
381-
for_each_loose_object(batch_loose_object, &cb, 0);
382-
for_each_packed_object(batch_packed_object, &cb, 0);
388+
sha1_array_for_each_unique(&sa, batch_object_cb, &cb);
389+
390+
sha1_array_clear(&sa);
383391
return 0;
384392
}
385393

t/t1006-cat-file.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ test_expect_success 'git cat-file --batch --follow-symlink returns correct sha a
548548
'
549549

550550
test_expect_success 'cat-file --batch-all-objects shows all objects' '
551-
# make new repos so we now the full set of objects; we will
551+
# make new repos so we know the full set of objects; we will
552552
# also make sure that there are some packed and some loose
553553
# objects, some referenced and some not, and that there are
554554
# some available only via alternates.
@@ -569,8 +569,7 @@ test_expect_success 'cat-file --batch-all-objects shows all objects' '
569569
) >>expect.unsorted &&
570570
sort <expect.unsorted >expect &&
571571
git -C all-two cat-file --batch-all-objects \
572-
--batch-check="%(objectname)" >actual.unsorted &&
573-
sort <actual.unsorted >actual &&
572+
--batch-check="%(objectname)" >actual &&
574573
test_cmp expect actual
575574
'
576575

0 commit comments

Comments
 (0)