Skip to content

Commit 2588f6e

Browse files
dschogitster
authored andcommitted
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. Rather than extending the signature of `prune_shallow()` to accept another Boolean, let's turn it into a bit field and declare constants, for readability. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 328a435 commit 2588f6e

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
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 ? PRUNE_SHOW_ONLY : 0);
165165

166166
return 0;
167167
}

commit.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ extern void assign_shallow_commits_to_refs(struct shallow_info *info,
255255
uint32_t **used,
256256
int *ref_status);
257257
extern int delayed_reachability_test(struct shallow_info *si, int c);
258-
extern void prune_shallow(int show_only);
258+
#define PRUNE_SHOW_ONLY 1
259+
#define PRUNE_QUICK 2
260+
extern void prune_shallow(unsigned options);
259261
extern struct trace_key trace_shallow;
260262

261263
int is_descendant_of(struct commit *, struct commit_list *);

shallow.c

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

247247
#define SEEN_ONLY 1
248248
#define VERBOSE 2
249+
#define QUICK 4
249250

250251
struct write_shallow_data {
251252
struct strbuf *out;
@@ -260,7 +261,10 @@ static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
260261
const char *hex = oid_to_hex(&graft->oid);
261262
if (graft->nr_parent != -1)
262263
return 0;
263-
if (data->flags & SEEN_ONLY) {
264+
if (data->flags & QUICK) {
265+
if (!has_object_file(&graft->oid))
266+
return 0;
267+
} else if (data->flags & SEEN_ONLY) {
264268
struct commit *c = lookup_commit(the_repository, &graft->oid);
265269
if (!c || !(c->object.flags & SEEN)) {
266270
if (data->flags & VERBOSE)
@@ -370,24 +374,31 @@ void advertise_shallow_grafts(int fd)
370374

371375
/*
372376
* mark_reachable_objects() should have been run prior to this and all
373-
* reachable commits marked as "SEEN".
377+
* reachable commits marked as "SEEN", except when quick_prune is non-zero,
378+
* in which case lines are excised from the shallow file if they refer to
379+
* commits that do not exist (any longer).
374380
*/
375-
void prune_shallow(int show_only)
381+
void prune_shallow(unsigned options)
376382
{
377383
struct lock_file shallow_lock = LOCK_INIT;
378384
struct strbuf sb = STRBUF_INIT;
385+
unsigned flags = SEEN_ONLY;
379386
int fd;
380387

381-
if (show_only) {
382-
write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY | VERBOSE);
388+
if (options & PRUNE_QUICK)
389+
flags |= QUICK;
390+
391+
if (options & PRUNE_SHOW_ONLY) {
392+
flags |= VERBOSE;
393+
write_shallow_commits_1(&sb, 0, NULL, flags);
383394
strbuf_release(&sb);
384395
return;
385396
}
386397
fd = hold_lock_file_for_update(&shallow_lock,
387398
git_path_shallow(the_repository),
388399
LOCK_DIE_ON_ERROR);
389400
check_shallow_file_for_update(the_repository);
390-
if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
401+
if (write_shallow_commits_1(&sb, 0, NULL, flags)) {
391402
if (write_in_full(fd, sb.buf, sb.len) < 0)
392403
die_errno("failed to write to %s",
393404
get_lock_file_path(&shallow_lock));

0 commit comments

Comments
 (0)