Skip to content

Commit 18308c1

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. 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]>
1 parent d9720bd commit 18308c1

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
@@ -249,7 +249,9 @@ 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+
#define PRUNE_SHOW_ONLY 1
253+
#define PRUNE_QUICK 2
254+
extern void prune_shallow(unsigned options);
253255
extern struct trace_key trace_shallow;
254256

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

shallow.c

Lines changed: 17 additions & 6 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 4
250251

251252
struct write_shallow_data {
252253
struct strbuf *out;
@@ -261,7 +262,10 @@ 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) {
266+
if (!has_object_file(&graft->oid))
267+
return 0;
268+
} else if (data->flags & SEEN_ONLY) {
265269
struct commit *c = lookup_commit(the_repository, &graft->oid);
266270
if (!c || !(c->object.flags & SEEN)) {
267271
if (data->flags & VERBOSE)
@@ -371,24 +375,31 @@ void advertise_shallow_grafts(int fd)
371375

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

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

0 commit comments

Comments
 (0)