Skip to content

Commit

Permalink
reachable: add options to add_unseen_recent_objects_to_traversal
Browse files Browse the repository at this point in the history
This function behaves very similarly to what we will need in
pack-objects in order to implement cruft packs with expiration. But it
is lacking a couple of things. Namely, it needs:

  - a mechanism to communicate the timestamps of individual recent
    objects to some external caller

  - and, in the case of packed objects, our future caller will also want
    to know the originating pack, as well as the offset within that pack
    at which the object can be found

  - finally, it needs a way to skip over packs which are marked as kept
    in-core.

To address the first two, add a callback interface in this patch which
reports the time of each recent object, as well as a (packed_git,
off_t) pair for packed objects.

Likewise, add a new option to the packed object iterators to skip over
packs which are marked as kept in core. This option will become
implicitly tested in a future patch.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
ttaylorr authored and gitster committed May 26, 2022
1 parent b757353 commit 2fb9040
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
2 changes: 1 addition & 1 deletion builtin/pack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -3957,7 +3957,7 @@ static void get_object_list(struct rev_info *revs, int ac, const char **av)
if (unpack_unreachable_expiration) {
revs->ignore_missing_links = 1;
if (add_unseen_recent_objects_to_traversal(revs,
unpack_unreachable_expiration))
unpack_unreachable_expiration, NULL, 0))
die(_("unable to add recent objects"));
if (prepare_revision_walk(revs))
die(_("revision walk setup failed"));
Expand Down
51 changes: 43 additions & 8 deletions reachable.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ static void mark_commit(struct commit *c, void *data)
struct recent_data {
struct rev_info *revs;
timestamp_t timestamp;
report_recent_object_fn *cb;
int ignore_in_core_kept_packs;
};

static void add_recent_object(const struct object_id *oid,
struct packed_git *pack,
off_t offset,
timestamp_t mtime,
struct recent_data *data)
{
Expand Down Expand Up @@ -103,13 +107,29 @@ static void add_recent_object(const struct object_id *oid,
die("unable to lookup %s", oid_to_hex(oid));

add_pending_object(data->revs, obj, "");
if (data->cb)
data->cb(obj, pack, offset, mtime);
}

static int want_recent_object(struct recent_data *data,
const struct object_id *oid)
{
if (data->ignore_in_core_kept_packs &&
has_object_kept_pack(oid, IN_CORE_KEEP_PACKS))
return 0;
return 1;
}

static int add_recent_loose(const struct object_id *oid,
const char *path, void *data)
{
struct stat st;
struct object *obj = lookup_object(the_repository, oid);
struct object *obj;

if (!want_recent_object(data, oid))
return 0;

obj = lookup_object(the_repository, oid);

if (obj && obj->flags & SEEN)
return 0;
Expand All @@ -126,37 +146,51 @@ static int add_recent_loose(const struct object_id *oid,
return error_errno("unable to stat %s", oid_to_hex(oid));
}

add_recent_object(oid, st.st_mtime, data);
add_recent_object(oid, NULL, 0, st.st_mtime, data);
return 0;
}

static int add_recent_packed(const struct object_id *oid,
struct packed_git *p, uint32_t pos,
void *data)
{
struct object *obj = lookup_object(the_repository, oid);
struct object *obj;

if (!want_recent_object(data, oid))
return 0;

obj = lookup_object(the_repository, oid);

if (obj && obj->flags & SEEN)
return 0;
add_recent_object(oid, p->mtime, data);
add_recent_object(oid, p, nth_packed_object_offset(p, pos), p->mtime, data);
return 0;
}

int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
timestamp_t timestamp)
timestamp_t timestamp,
report_recent_object_fn *cb,
int ignore_in_core_kept_packs)
{
struct recent_data data;
enum for_each_object_flags flags;
int r;

data.revs = revs;
data.timestamp = timestamp;
data.cb = cb;
data.ignore_in_core_kept_packs = ignore_in_core_kept_packs;

r = for_each_loose_object(add_recent_loose, &data,
FOR_EACH_OBJECT_LOCAL_ONLY);
if (r)
return r;
return for_each_packed_object(add_recent_packed, &data,
FOR_EACH_OBJECT_LOCAL_ONLY);

flags = FOR_EACH_OBJECT_LOCAL_ONLY | FOR_EACH_OBJECT_PACK_ORDER;
if (ignore_in_core_kept_packs)
flags |= FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS;

return for_each_packed_object(add_recent_packed, &data, flags);
}

static int mark_object_seen(const struct object_id *oid,
Expand Down Expand Up @@ -217,7 +251,8 @@ void mark_reachable_objects(struct rev_info *revs, int mark_reflog,

if (mark_recent) {
revs->ignore_missing_links = 1;
if (add_unseen_recent_objects_to_traversal(revs, mark_recent))
if (add_unseen_recent_objects_to_traversal(revs, mark_recent,
NULL, 0))
die("unable to mark recent objects");
if (prepare_revision_walk(revs))
die("revision walk setup failed");
Expand Down
9 changes: 8 additions & 1 deletion reachable.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#ifndef REACHEABLE_H
#define REACHEABLE_H

#include "object.h"

struct progress;
struct rev_info;

typedef void report_recent_object_fn(const struct object *, struct packed_git *,
off_t, time_t);

int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
timestamp_t timestamp);
timestamp_t timestamp,
report_recent_object_fn cb,
int ignore_in_core_kept_packs);
void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
timestamp_t mark_recent, struct progress *);

Expand Down

0 comments on commit 2fb9040

Please sign in to comment.