Files
git/shallow.h
Patrick Steinhardt d43b9acb05 setup: unify setup of shallow file
It is possible to configure an arbitrary "shallow" file via two
mechanisms, and the respective logic to handle these is split across two
locations:

  - Via the "GIT_SHALLOW_FILE" environment variable, which is handled in
    `setup_git_env_internal()`.

  - Via the global "--shallow-file=" command line option, which is
    handled in `handle_options()`.

We can rather easily unify this logic by not configuring the shallow
file in `handle_options()`, but instead overwriting the environment
variable. The environment variable itself is then handled inside of
`apply_repository_format()`, which is responsible for configuring a
discovered Git directory.

This new logic is similar in nature to how we handle the other global
options already, all of which end up setting an environment variable.
So for one this gives us more consistency. But more importantly, this
change means that `the_repository` will not contain any relevant state
anymore before we hit `apply_repository_format()` once we're at the end
of this patch series. Consequently, it will become possible for us to
completely discard `the_repository` and populate it anew.

Note that on first sight, this change looks like it might change the
precedence order. Before this change, we used to configure the shallow
file in the arguments handler first, and then it looks like we override
it via the environment variable. What's important to note though is the
last parameter to `set_alternate_shallow_file()`, which tells us whether
we want to overwrite a preexisting value, and when applying the value
from the environment we tell it not to overwrite preexisting values. So
in effect, the command line has precedence over the environment. After
this change, we now overwrite preexisting environment variables when we
see the argument, and consequently we keep the precedence order in tact.

With this change though we don't need the final parameter anymore that
tells `set_alternate_shallow_file()` whether or not to overwrite. We
only have a single callsite for this function now, and that function is
itself only ever called exactly once. Remove that parameter.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2026-07-07 10:49:40 -07:00

89 lines
2.7 KiB
C

#ifndef SHALLOW_H
#define SHALLOW_H
#include "commit.h"
#include "lockfile.h"
#include "object.h"
#include "repository.h"
#include "strbuf.h"
struct oid_array;
struct strvec;
void set_alternate_shallow_file(struct repository *r, const char *path);
int register_shallow(struct repository *r, const struct object_id *oid);
int unregister_shallow(const struct object_id *oid);
int is_repository_shallow(struct repository *r);
/*
* Lock for updating the $GIT_DIR/shallow file.
*
* Use `commit_shallow_file()` to commit an update, or
* `rollback_shallow_file()` to roll it back. In either case, any
* in-memory cached information about which commits are shallow will be
* appropriately invalidated so that future operations reflect the new
* state.
*/
struct shallow_lock {
struct lock_file lock;
};
#define SHALLOW_LOCK_INIT { \
.lock = LOCK_INIT, \
}
/* commit $GIT_DIR/shallow and reset stat-validity checks */
int commit_shallow_file(struct repository *r, struct shallow_lock *lk);
/* rollback $GIT_DIR/shallow and reset stat-validity checks */
void rollback_shallow_file(struct repository *r, struct shallow_lock *lk);
int get_shallows_depth(struct object_array *heads, struct object_array *shallows);
struct commit_list *get_shallow_commits(struct object_array *heads,
struct object_array *shallows, int deepen_relative,
int depth, int shallow_flag, int not_shallow_flag);
struct commit_list *get_shallow_commits_by_rev_list(struct strvec *argv,
int shallow_flag, int not_shallow_flag);
int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
const struct oid_array *extra);
void setup_alternate_shallow(struct shallow_lock *shallow_lock,
const char **alternate_shallow_file,
const struct oid_array *extra);
const char *setup_temporary_shallow(const struct oid_array *extra);
void advertise_shallow_grafts(int);
#define PRUNE_SHOW_ONLY 1
#define PRUNE_QUICK 2
void prune_shallow(unsigned options);
/*
* Initialize with prepare_shallow_info() or zero-initialize (equivalent to
* prepare_shallow_info with a NULL oid_array).
*/
struct shallow_info {
struct oid_array *shallow;
size_t *ours, nr_ours;
size_t *theirs, nr_theirs;
struct oid_array *ref;
/* for receive-pack */
uint32_t **used_shallow;
int *need_reachability_test;
int *reachable;
int *shallow_ref;
struct commit_stack commits;
};
void prepare_shallow_info(struct shallow_info *, struct oid_array *);
void clear_shallow_info(struct shallow_info *);
void remove_nonexistent_theirs_shallow(struct shallow_info *);
void assign_shallow_commits_to_refs(struct shallow_info *info,
uint32_t **used,
int *ref_status);
int delayed_reachability_test(struct shallow_info *si, int c);
extern struct trace_key trace_shallow;
#endif /* SHALLOW_H */