Skip to content

Commit f085eb4

Browse files
committed
shallow: offer to prune only non-existing entries
The `prune_shallow()` function wants a full reachability check to be completed before it goes to work, to ensure that all unreachable entries are removed from the shallow file. However, in the upcoming patch we do not even want to go that far. We really only need to remove entries corresponding to pruned commits, i.e. to commits that no longer exist. Let's support that use case. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent ed8559b commit f085eb4

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

builtin/prune.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
161161
free(s);
162162

163163
if (is_repository_shallow(the_repository))
164-
prune_shallow(show_only);
164+
prune_shallow(show_only, 0);
165165

166166
return 0;
167167
}

commit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ extern void assign_shallow_commits_to_refs(struct shallow_info *info,
249249
uint32_t **used,
250250
int *ref_status);
251251
extern int delayed_reachability_test(struct shallow_info *si, int c);
252-
extern void prune_shallow(int show_only);
252+
extern void prune_shallow(int show_only, int quick_prune);
253253
extern struct trace_key trace_shallow;
254254

255255
extern int interactive_add(int argc, const char **argv, const char *prefix, int patch);

shallow.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ static void check_shallow_file_for_update(struct repository *r)
247247

248248
#define SEEN_ONLY 1
249249
#define VERBOSE 2
250+
#define QUICK_PRUNE 4
250251

251252
struct write_shallow_data {
252253
struct strbuf *out;
@@ -261,7 +262,11 @@ static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
261262
const char *hex = oid_to_hex(&graft->oid);
262263
if (graft->nr_parent != -1)
263264
return 0;
264-
if (data->flags & SEEN_ONLY) {
265+
if (data->flags & QUICK_PRUNE) {
266+
struct commit *c = lookup_commit(the_repository, &graft->oid);
267+
if (!c || parse_commit(c))
268+
return 0;
269+
} else if (data->flags & SEEN_ONLY) {
265270
struct commit *c = lookup_commit(the_repository, &graft->oid);
266271
if (!c || !(c->object.flags & SEEN)) {
267272
if (data->flags & VERBOSE)
@@ -371,24 +376,31 @@ void advertise_shallow_grafts(int fd)
371376

372377
/*
373378
* mark_reachable_objects() should have been run prior to this and all
374-
* reachable commits marked as "SEEN".
379+
* reachable commits marked as "SEEN", except when quick_prune is non-zero,
380+
* in which case lines are excised from the shallow file if they refer to
381+
* commits that do not exist (any longer).
375382
*/
376-
void prune_shallow(int show_only)
383+
void prune_shallow(int show_only, int quick_prune)
377384
{
378385
struct lock_file shallow_lock = LOCK_INIT;
379386
struct strbuf sb = STRBUF_INIT;
387+
unsigned flags = SEEN_ONLY;
380388
int fd;
381389

390+
if (quick_prune)
391+
flags |= QUICK_PRUNE;
392+
382393
if (show_only) {
383-
write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY | VERBOSE);
394+
flags |= VERBOSE;
395+
write_shallow_commits_1(&sb, 0, NULL, flags);
384396
strbuf_release(&sb);
385397
return;
386398
}
387399
fd = hold_lock_file_for_update(&shallow_lock,
388400
git_path_shallow(the_repository),
389401
LOCK_DIE_ON_ERROR);
390402
check_shallow_file_for_update(the_repository);
391-
if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
403+
if (write_shallow_commits_1(&sb, 0, NULL, flags)) {
392404
if (write_in_full(fd, sb.buf, sb.len) < 0)
393405
die_errno("failed to write to %s",
394406
get_lock_file_path(&shallow_lock));

0 commit comments

Comments
 (0)