Skip to content

Commit 611c778

Browse files
matheustavaresgitster
authored andcommitted
checkout: fix two bugs on the final count of updated entries
At the end of `git checkout <pathspec>`, we get a message informing how many entries were updated in the working tree. However, this number can be inaccurate for two reasons: 1) Delayed entries currently get counted twice. 2) Failed entries are included in the count. The first problem happens because the counter is first incremented before inserting the entry in the delayed checkout queue, and once again when finish_delayed_checkout() calls checkout_entry(). And the second happens because the counter is incremented too early in checkout_entry(), before the entry was in fact checked out. Fix that by moving the count increment further down in the call stack and removing the duplicate increment on delayed entries. Note that we have to keep a per-entry reference for the counter (both on parallel checkout and delayed checkout) because not all entries are always accumulated at the same counter. See checkout_worktree(), at builtin/checkout.c for an example. Signed-off-by: Matheus Tavares <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 11d14de commit 611c778

File tree

9 files changed

+40
-25
lines changed

9 files changed

+40
-25
lines changed

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
417417
mem_pool_discard(&ce_mem_pool, should_validate_cache_entries());
418418
remove_marked_cache_entries(&the_index, 1);
419419
remove_scheduled_dirs();
420-
errs |= finish_delayed_checkout(&state, &nr_checkouts, opts->show_progress);
420+
errs |= finish_delayed_checkout(&state, opts->show_progress);
421421

422422
if (opts->count_checkout_paths) {
423423
if (nr_unmerged)

convert.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ struct delayed_checkout {
5353
enum ce_delay_state state;
5454
/* List of filter drivers that signaled delayed blobs. */
5555
struct string_list filters;
56-
/* List of delayed blobs identified by their path. */
56+
/*
57+
* List of delayed blobs identified by their path. The `util` member
58+
* holds a counter pointer which must be incremented when/if the
59+
* associated blob gets checked out.
60+
*/
5761
struct string_list paths;
5862
};
5963

entry.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,11 @@ static int remove_available_paths(struct string_list_item *item, void *cb_data)
157157

158158
available = string_list_lookup(available_paths, item->string);
159159
if (available)
160-
available->util = (void *)item->string;
160+
available->util = item->util;
161161
return !available;
162162
}
163163

164-
int finish_delayed_checkout(struct checkout *state, int *nr_checkouts,
165-
int show_progress)
164+
int finish_delayed_checkout(struct checkout *state, int show_progress)
166165
{
167166
int errs = 0;
168167
unsigned processed_paths = 0;
@@ -227,7 +226,7 @@ int finish_delayed_checkout(struct checkout *state, int *nr_checkouts,
227226
strlen(path->string), 0);
228227
if (ce) {
229228
display_progress(progress, ++processed_paths);
230-
errs |= checkout_entry(ce, state, NULL, nr_checkouts);
229+
errs |= checkout_entry(ce, state, NULL, path->util);
231230
filtered_bytes += ce->ce_stat_data.sd_size;
232231
display_throughput(progress, filtered_bytes);
233232
} else
@@ -266,7 +265,8 @@ void update_ce_after_write(const struct checkout *state, struct cache_entry *ce,
266265

267266
/* Note: ca is used (and required) iff the entry refers to a regular file. */
268267
static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca,
269-
const struct checkout *state, int to_tempfile)
268+
const struct checkout *state, int to_tempfile,
269+
int *nr_checkouts)
270270
{
271271
unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
272272
struct delayed_checkout *dco = state->delayed_checkout;
@@ -279,6 +279,7 @@ static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca
279279
struct stat st;
280280
const struct submodule *sub;
281281
struct checkout_metadata meta;
282+
static int scratch_nr_checkouts;
282283

283284
clone_checkout_metadata(&meta, &state->meta, &ce->oid);
284285

@@ -333,9 +334,15 @@ static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca
333334
ret = async_convert_to_working_tree_ca(ca, ce->name,
334335
new_blob, size,
335336
&buf, &meta, dco);
336-
if (ret && string_list_has_string(&dco->paths, ce->name)) {
337-
free(new_blob);
338-
goto delayed;
337+
if (ret) {
338+
struct string_list_item *item =
339+
string_list_lookup(&dco->paths, ce->name);
340+
if (item) {
341+
item->util = nr_checkouts ? nr_checkouts
342+
: &scratch_nr_checkouts;
343+
free(new_blob);
344+
goto delayed;
345+
}
339346
}
340347
} else {
341348
ret = convert_to_working_tree_ca(ca, ce->name, new_blob,
@@ -392,6 +399,8 @@ static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca
392399
ce->name);
393400
update_ce_after_write(state, ce , &st);
394401
}
402+
if (nr_checkouts)
403+
(*nr_checkouts)++;
395404
delayed:
396405
return 0;
397406
}
@@ -476,7 +485,7 @@ int checkout_entry_ca(struct cache_entry *ce, struct conv_attrs *ca,
476485
convert_attrs(state->istate, &ca_buf, ce->name);
477486
ca = &ca_buf;
478487
}
479-
return write_entry(ce, topath, ca, state, 1);
488+
return write_entry(ce, topath, ca, state, 1, nr_checkouts);
480489
}
481490

482491
strbuf_reset(&path);
@@ -540,18 +549,15 @@ int checkout_entry_ca(struct cache_entry *ce, struct conv_attrs *ca,
540549

541550
create_directories(path.buf, path.len, state);
542551

543-
if (nr_checkouts)
544-
(*nr_checkouts)++;
545-
546552
if (S_ISREG(ce->ce_mode) && !ca) {
547553
convert_attrs(state->istate, &ca_buf, ce->name);
548554
ca = &ca_buf;
549555
}
550556

551-
if (!enqueue_checkout(ce, ca))
557+
if (!enqueue_checkout(ce, ca, nr_checkouts))
552558
return 0;
553559

554-
return write_entry(ce, path.buf, ca, state, 0);
560+
return write_entry(ce, path.buf, ca, state, 0, nr_checkouts);
555561
}
556562

557563
void unlink_entry(const struct cache_entry *ce)

entry.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ static inline int checkout_entry(struct cache_entry *ce,
4343
}
4444

4545
void enable_delayed_checkout(struct checkout *state);
46-
int finish_delayed_checkout(struct checkout *state, int *nr_checkouts,
47-
int show_progress);
46+
int finish_delayed_checkout(struct checkout *state, int show_progress);
4847

4948
/*
5049
* Unlink the last component and schedule the leading directories for

parallel-checkout.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ static int is_eligible_for_parallel_checkout(const struct cache_entry *ce,
143143
}
144144
}
145145

146-
int enqueue_checkout(struct cache_entry *ce, struct conv_attrs *ca)
146+
int enqueue_checkout(struct cache_entry *ce, struct conv_attrs *ca,
147+
int *checkout_counter)
147148
{
148149
struct parallel_checkout_item *pc_item;
149150

@@ -159,6 +160,7 @@ int enqueue_checkout(struct cache_entry *ce, struct conv_attrs *ca)
159160
memcpy(&pc_item->ca, ca, sizeof(pc_item->ca));
160161
pc_item->status = PC_ITEM_PENDING;
161162
pc_item->id = parallel_checkout.nr;
163+
pc_item->checkout_counter = checkout_counter;
162164
parallel_checkout.nr++;
163165

164166
return 0;
@@ -200,7 +202,8 @@ static int handle_results(struct checkout *state)
200202

201203
switch(pc_item->status) {
202204
case PC_ITEM_WRITTEN:
203-
/* Already handled */
205+
if (pc_item->checkout_counter)
206+
(*pc_item->checkout_counter)++;
204207
break;
205208
case PC_ITEM_COLLIDED:
206209
/*
@@ -225,7 +228,8 @@ static int handle_results(struct checkout *state)
225228
* add any extra overhead.
226229
*/
227230
ret |= checkout_entry_ca(pc_item->ce, &pc_item->ca,
228-
state, NULL, NULL);
231+
state, NULL,
232+
pc_item->checkout_counter);
229233
advance_progress_meter();
230234
break;
231235
case PC_ITEM_PENDING:

parallel-checkout.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ void init_parallel_checkout(void);
3131
* entry is not eligible for parallel checkout. Otherwise, enqueue the entry
3232
* for later write and return 0.
3333
*/
34-
int enqueue_checkout(struct cache_entry *ce, struct conv_attrs *ca);
34+
int enqueue_checkout(struct cache_entry *ce, struct conv_attrs *ca,
35+
int *checkout_counter);
3536
size_t pc_queue_size(void);
3637

3738
/*
@@ -68,6 +69,7 @@ struct parallel_checkout_item {
6869
struct cache_entry *ce;
6970
struct conv_attrs ca;
7071
size_t id; /* position in parallel_checkout.items[] of main process */
72+
int *checkout_counter;
7173

7274
/* Output fields, sent from workers. */
7375
enum pc_item_status status;

t/t0021-conversion.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ do
11321132
'
11331133
done
11341134

1135-
test_expect_failure PERL 'delayed checkout correctly reports the number of updated entries' '
1135+
test_expect_success PERL 'delayed checkout correctly reports the number of updated entries' '
11361136
rm -rf repo &&
11371137
git init repo &&
11381138
(

t/t2080-parallel-checkout-basics.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ test_expect_success SYMLINKS 'parallel checkout checks for symlinks in leading d
230230
# check the final report including sequential, parallel, and delayed entries
231231
# all at the same time. So we must have finer control of the parallel checkout
232232
# variables.
233-
test_expect_failure PERL '"git checkout ." report should not include failed entries' '
233+
test_expect_success PERL '"git checkout ." report should not include failed entries' '
234234
write_script rot13-filter.pl "$PERL_PATH" \
235235
<"$TEST_DIRECTORY"/t0021/rot13-filter.pl &&
236236

unpack-trees.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ static int check_updates(struct unpack_trees_options *o,
487487
errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
488488
progress, &cnt);
489489
stop_progress(&progress);
490-
errs |= finish_delayed_checkout(&state, NULL, o->verbose_update);
490+
errs |= finish_delayed_checkout(&state, o->verbose_update);
491491
git_attr_set_direction(GIT_ATTR_CHECKIN);
492492

493493
if (o->clone)

0 commit comments

Comments
 (0)