mirror of
https://github.com/git/git.git
synced 2026-01-10 10:13:33 +00:00
refs: convert iteration over replace refs to accept ref store
The function `for_each_replace_ref()` is a bit of an oddball across the refs interfaces as it accepts a pointer to the repository instead of a pointer to the ref store. The only reason for us to accept a repository is so that we can eventually pass it back to the callback function that the caller has provided. This is somewhat arbitrary though, as callers that need the repository can instead make it accessible via the callback payload. Refactor the function to instead accept the ref store and adjust callers accordingly. This allows us to get rid of some of the boilerplate that we had to carry to pass along the repository and brings us in line with the other functions that iterate through refs. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
dc7fb4f72c
commit
8378c9d27b
@@ -43,11 +43,12 @@ enum replace_format {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct show_data {
|
struct show_data {
|
||||||
|
struct repository *repo;
|
||||||
const char *pattern;
|
const char *pattern;
|
||||||
enum replace_format format;
|
enum replace_format format;
|
||||||
};
|
};
|
||||||
|
|
||||||
static int show_reference(struct repository *r, const char *refname,
|
static int show_reference(const char *refname,
|
||||||
const struct object_id *oid,
|
const struct object_id *oid,
|
||||||
int flag UNUSED, void *cb_data)
|
int flag UNUSED, void *cb_data)
|
||||||
{
|
{
|
||||||
@@ -62,11 +63,11 @@ static int show_reference(struct repository *r, const char *refname,
|
|||||||
struct object_id object;
|
struct object_id object;
|
||||||
enum object_type obj_type, repl_type;
|
enum object_type obj_type, repl_type;
|
||||||
|
|
||||||
if (repo_get_oid(r, refname, &object))
|
if (repo_get_oid(data->repo, refname, &object))
|
||||||
return error(_("failed to resolve '%s' as a valid ref"), refname);
|
return error(_("failed to resolve '%s' as a valid ref"), refname);
|
||||||
|
|
||||||
obj_type = oid_object_info(r, &object, NULL);
|
obj_type = oid_object_info(data->repo, &object, NULL);
|
||||||
repl_type = oid_object_info(r, oid, NULL);
|
repl_type = oid_object_info(data->repo, oid, NULL);
|
||||||
|
|
||||||
printf("%s (%s) -> %s (%s)\n", refname, type_name(obj_type),
|
printf("%s (%s) -> %s (%s)\n", refname, type_name(obj_type),
|
||||||
oid_to_hex(oid), type_name(repl_type));
|
oid_to_hex(oid), type_name(repl_type));
|
||||||
@@ -80,6 +81,7 @@ static int list_replace_refs(const char *pattern, const char *format)
|
|||||||
{
|
{
|
||||||
struct show_data data;
|
struct show_data data;
|
||||||
|
|
||||||
|
data.repo = the_repository;
|
||||||
if (!pattern)
|
if (!pattern)
|
||||||
pattern = "*";
|
pattern = "*";
|
||||||
data.pattern = pattern;
|
data.pattern = pattern;
|
||||||
@@ -99,7 +101,8 @@ static int list_replace_refs(const char *pattern, const char *format)
|
|||||||
"valid formats are 'short', 'medium' and 'long'"),
|
"valid formats are 'short', 'medium' and 'long'"),
|
||||||
format);
|
format);
|
||||||
|
|
||||||
for_each_replace_ref(the_repository, show_reference, (void *)&data);
|
refs_for_each_replace_ref(get_main_ref_store(the_repository),
|
||||||
|
show_reference, (void *)&data);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
58
refs.c
58
refs.c
@@ -1576,53 +1576,12 @@ struct ref_iterator *refs_ref_iterator_begin(
|
|||||||
return iter;
|
return iter;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Call fn for each reference in the specified submodule for which the
|
|
||||||
* refname begins with prefix. If trim is non-zero, then trim that
|
|
||||||
* many characters off the beginning of each refname before passing
|
|
||||||
* the refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to
|
|
||||||
* include broken references in the iteration. If fn ever returns a
|
|
||||||
* non-zero value, stop the iteration and return that value;
|
|
||||||
* otherwise, return 0.
|
|
||||||
*/
|
|
||||||
static int do_for_each_repo_ref(struct repository *r, const char *prefix,
|
|
||||||
each_repo_ref_fn fn, int trim, int flags,
|
|
||||||
void *cb_data)
|
|
||||||
{
|
|
||||||
struct ref_iterator *iter;
|
|
||||||
struct ref_store *refs = get_main_ref_store(r);
|
|
||||||
|
|
||||||
if (!refs)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
iter = refs_ref_iterator_begin(refs, prefix, NULL, trim, flags);
|
|
||||||
|
|
||||||
return do_for_each_repo_ref_iterator(r, iter, fn, cb_data);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct do_for_each_ref_help {
|
|
||||||
each_ref_fn *fn;
|
|
||||||
void *cb_data;
|
|
||||||
};
|
|
||||||
|
|
||||||
static int do_for_each_ref_helper(struct repository *r UNUSED,
|
|
||||||
const char *refname,
|
|
||||||
const struct object_id *oid,
|
|
||||||
int flags,
|
|
||||||
void *cb_data)
|
|
||||||
{
|
|
||||||
struct do_for_each_ref_help *hp = cb_data;
|
|
||||||
|
|
||||||
return hp->fn(refname, oid, flags, hp->cb_data);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int do_for_each_ref(struct ref_store *refs, const char *prefix,
|
static int do_for_each_ref(struct ref_store *refs, const char *prefix,
|
||||||
const char **exclude_patterns,
|
const char **exclude_patterns,
|
||||||
each_ref_fn fn, int trim,
|
each_ref_fn fn, int trim,
|
||||||
enum do_for_each_ref_flags flags, void *cb_data)
|
enum do_for_each_ref_flags flags, void *cb_data)
|
||||||
{
|
{
|
||||||
struct ref_iterator *iter;
|
struct ref_iterator *iter;
|
||||||
struct do_for_each_ref_help hp = { fn, cb_data };
|
|
||||||
|
|
||||||
if (!refs)
|
if (!refs)
|
||||||
return 0;
|
return 0;
|
||||||
@@ -1630,8 +1589,7 @@ static int do_for_each_ref(struct ref_store *refs, const char *prefix,
|
|||||||
iter = refs_ref_iterator_begin(refs, prefix, exclude_patterns, trim,
|
iter = refs_ref_iterator_begin(refs, prefix, exclude_patterns, trim,
|
||||||
flags);
|
flags);
|
||||||
|
|
||||||
return do_for_each_repo_ref_iterator(the_repository, iter,
|
return do_for_each_ref_iterator(iter, fn, cb_data);
|
||||||
do_for_each_ref_helper, &hp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int refs_for_each_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
|
int refs_for_each_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
|
||||||
@@ -1652,12 +1610,12 @@ int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix,
|
|||||||
return do_for_each_ref(refs, prefix, exclude_patterns, fn, 0, 0, cb_data);
|
return do_for_each_ref(refs, prefix, exclude_patterns, fn, 0, 0, cb_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
int for_each_replace_ref(struct repository *r, each_repo_ref_fn fn, void *cb_data)
|
int refs_for_each_replace_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
|
||||||
{
|
{
|
||||||
const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
|
const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
|
||||||
return do_for_each_repo_ref(r, git_replace_ref_base, fn,
|
return do_for_each_ref(refs, git_replace_ref_base, NULL, fn,
|
||||||
strlen(git_replace_ref_base),
|
strlen(git_replace_ref_base),
|
||||||
DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
|
DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
int refs_for_each_namespaced_ref(struct ref_store *refs,
|
int refs_for_each_namespaced_ref(struct ref_store *refs,
|
||||||
@@ -2425,8 +2383,7 @@ struct do_for_each_reflog_help {
|
|||||||
void *cb_data;
|
void *cb_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
static int do_for_each_reflog_helper(struct repository *r UNUSED,
|
static int do_for_each_reflog_helper(const char *refname,
|
||||||
const char *refname,
|
|
||||||
const struct object_id *oid UNUSED,
|
const struct object_id *oid UNUSED,
|
||||||
int flags,
|
int flags,
|
||||||
void *cb_data)
|
void *cb_data)
|
||||||
@@ -2442,8 +2399,7 @@ int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_dat
|
|||||||
|
|
||||||
iter = refs->be->reflog_iterator_begin(refs);
|
iter = refs->be->reflog_iterator_begin(refs);
|
||||||
|
|
||||||
return do_for_each_repo_ref_iterator(the_repository, iter,
|
return do_for_each_ref_iterator(iter, do_for_each_reflog_helper, &hp);
|
||||||
do_for_each_reflog_helper, &hp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int refs_for_each_reflog_ent_reverse(struct ref_store *refs,
|
int refs_for_each_reflog_ent_reverse(struct ref_store *refs,
|
||||||
|
|||||||
17
refs.h
17
refs.h
@@ -298,16 +298,6 @@ struct ref_transaction;
|
|||||||
typedef int each_ref_fn(const char *refname,
|
typedef int each_ref_fn(const char *refname,
|
||||||
const struct object_id *oid, int flags, void *cb_data);
|
const struct object_id *oid, int flags, void *cb_data);
|
||||||
|
|
||||||
/*
|
|
||||||
* The same as each_ref_fn, but also with a repository argument that
|
|
||||||
* contains the repository associated with the callback.
|
|
||||||
*/
|
|
||||||
typedef int each_repo_ref_fn(struct repository *r,
|
|
||||||
const char *refname,
|
|
||||||
const struct object_id *oid,
|
|
||||||
int flags,
|
|
||||||
void *cb_data);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The following functions invoke the specified callback function for
|
* The following functions invoke the specified callback function for
|
||||||
* each reference indicated. If the function ever returns a nonzero
|
* each reference indicated. If the function ever returns a nonzero
|
||||||
@@ -329,6 +319,8 @@ int refs_for_each_branch_ref(struct ref_store *refs,
|
|||||||
each_ref_fn fn, void *cb_data);
|
each_ref_fn fn, void *cb_data);
|
||||||
int refs_for_each_remote_ref(struct ref_store *refs,
|
int refs_for_each_remote_ref(struct ref_store *refs,
|
||||||
each_ref_fn fn, void *cb_data);
|
each_ref_fn fn, void *cb_data);
|
||||||
|
int refs_for_each_replace_ref(struct ref_store *refs,
|
||||||
|
each_ref_fn fn, void *cb_data);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* references matching any pattern in "exclude_patterns" are omitted from the
|
* references matching any pattern in "exclude_patterns" are omitted from the
|
||||||
@@ -353,11 +345,6 @@ int refs_for_each_fullref_in_prefixes(struct ref_store *refs,
|
|||||||
const char **exclude_patterns,
|
const char **exclude_patterns,
|
||||||
each_ref_fn fn, void *cb_data);
|
each_ref_fn fn, void *cb_data);
|
||||||
|
|
||||||
/**
|
|
||||||
* iterate refs from the respective area.
|
|
||||||
*/
|
|
||||||
int for_each_replace_ref(struct repository *r, each_repo_ref_fn fn, void *cb_data);
|
|
||||||
|
|
||||||
/* iterates all refs that match the specified glob pattern. */
|
/* iterates all refs that match the specified glob pattern. */
|
||||||
int refs_for_each_glob_ref(struct ref_store *refs, each_ref_fn fn,
|
int refs_for_each_glob_ref(struct ref_store *refs, each_ref_fn fn,
|
||||||
const char *pattern, void *cb_data);
|
const char *pattern, void *cb_data);
|
||||||
|
|||||||
@@ -440,15 +440,15 @@ struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
|
|||||||
|
|
||||||
struct ref_iterator *current_ref_iter = NULL;
|
struct ref_iterator *current_ref_iter = NULL;
|
||||||
|
|
||||||
int do_for_each_repo_ref_iterator(struct repository *r, struct ref_iterator *iter,
|
int do_for_each_ref_iterator(struct ref_iterator *iter,
|
||||||
each_repo_ref_fn fn, void *cb_data)
|
each_ref_fn fn, void *cb_data)
|
||||||
{
|
{
|
||||||
int retval = 0, ok;
|
int retval = 0, ok;
|
||||||
struct ref_iterator *old_ref_iter = current_ref_iter;
|
struct ref_iterator *old_ref_iter = current_ref_iter;
|
||||||
|
|
||||||
current_ref_iter = iter;
|
current_ref_iter = iter;
|
||||||
while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
|
while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
|
||||||
retval = fn(r, iter->refname, iter->oid, iter->flags, cb_data);
|
retval = fn(iter->refname, iter->oid, iter->flags, cb_data);
|
||||||
if (retval) {
|
if (retval) {
|
||||||
/*
|
/*
|
||||||
* If ref_iterator_abort() returns ITER_ERROR,
|
* If ref_iterator_abort() returns ITER_ERROR,
|
||||||
|
|||||||
@@ -503,9 +503,8 @@ extern struct ref_iterator *current_ref_iter;
|
|||||||
* adapter between the callback style of reference iteration and the
|
* adapter between the callback style of reference iteration and the
|
||||||
* iterator style.
|
* iterator style.
|
||||||
*/
|
*/
|
||||||
int do_for_each_repo_ref_iterator(struct repository *r,
|
int do_for_each_ref_iterator(struct ref_iterator *iter,
|
||||||
struct ref_iterator *iter,
|
each_ref_fn fn, void *cb_data);
|
||||||
each_repo_ref_fn fn, void *cb_data);
|
|
||||||
|
|
||||||
struct ref_store;
|
struct ref_store;
|
||||||
|
|
||||||
|
|||||||
@@ -8,12 +8,13 @@
|
|||||||
#include "repository.h"
|
#include "repository.h"
|
||||||
#include "commit.h"
|
#include "commit.h"
|
||||||
|
|
||||||
static int register_replace_ref(struct repository *r,
|
static int register_replace_ref(const char *refname,
|
||||||
const char *refname,
|
|
||||||
const struct object_id *oid,
|
const struct object_id *oid,
|
||||||
int flag UNUSED,
|
int flag UNUSED,
|
||||||
void *cb_data UNUSED)
|
void *cb_data)
|
||||||
{
|
{
|
||||||
|
struct repository *r = cb_data;
|
||||||
|
|
||||||
/* Get sha1 from refname */
|
/* Get sha1 from refname */
|
||||||
const char *slash = strrchr(refname, '/');
|
const char *slash = strrchr(refname, '/');
|
||||||
const char *hash = slash ? slash + 1 : refname;
|
const char *hash = slash ? slash + 1 : refname;
|
||||||
@@ -50,7 +51,8 @@ void prepare_replace_object(struct repository *r)
|
|||||||
xmalloc(sizeof(*r->objects->replace_map));
|
xmalloc(sizeof(*r->objects->replace_map));
|
||||||
oidmap_init(r->objects->replace_map, 0);
|
oidmap_init(r->objects->replace_map, 0);
|
||||||
|
|
||||||
for_each_replace_ref(r, register_replace_ref, NULL);
|
refs_for_each_replace_ref(get_main_ref_store(r),
|
||||||
|
register_replace_ref, r);
|
||||||
r->objects->replace_map_initialized = 1;
|
r->objects->replace_map_initialized = 1;
|
||||||
|
|
||||||
pthread_mutex_unlock(&r->objects->replace_mutex);
|
pthread_mutex_unlock(&r->objects->replace_mutex);
|
||||||
|
|||||||
Reference in New Issue
Block a user