Skip to contentSkip to conversation

Reference iterators

PATCH Superseded Michael Haggerty 6 participants 27 messages Jun 16, 2016 Kbuild

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Reference iterators

This patch series implements a new iteration paradigm for iterating over references using iterators. This approach was proposed earlier as an RFC [1].

The justification for this change is laid out in the RFC [1] and in the commit message for patch 09/13 [2]. Please refer to those, as I won't repeat them here.

There are several obvious followup steps that are not included in this patch series (because they don't help with the initial transition to pluggable reference backends). I've written prototypes of several of these:

* The iterator interface could be made public and callers could start using it directly.

* A filter_ref_iterator could let references be filtered based on a callback function (this would be useful, for example, for for_each_glob_ref()).

* A single_ref_iterator could "iterate" over a single reference (e.g., HEAD).

* A series_ref_iterator could iterate over multiple iterators, for callers that want to operate on, say, HEAD plus all branches.

* The ref_cache could be fed from an iterator, to better decouple caching from reading packed and loose references, and to make it easy to use caching with other reference storage backends.

* Per-worktree refs could be overlaid on top of shared references using merge_ref_iterator rather than mixing them up in the same ref_cache.

* The dir_iterator could be used in more places; for example, when reading loose references from disk.

Table of contents of changes:

* The first eight patches are cleanups.

* Patch 05/13 fixes a code path that unlinks symrefs directly instead of using the refs API.

* Patch 09/13 is the most important part of this series. It introduces not only reference iterators, but also (1) a pattern that other iterator interfaces can follow (along with some useful constants in iterator.h), and (2) a pattern for building OO code with inheritance.

* Patch 10/13 actually uses the new reference iteration mechanism.

* Patch 11/13 avoids aborting reflog iterations if an unexpected file is found under $GIT_DIR/logs. This fixes a bug that could cause objects needed by reflogs to be pruned, breaking the reflogs.

* Patch 12/13 adds an iterator interface for iterating over directories (using the same model as patch 09/13).

* Patch 13/13 implements for_each_reflog() on top of an iterator interface (essentially the analogue of patch 10/13, but for reflogs).

Note that it is not necessary to rebuild for_each_reflog_ent() on top of iterators at this time, because that function deals with only a single reference at a time. Therefore, composability is not important here (for example, it won't have to deal with multiple refs backends at the same time).

This patch series applies on top of mh/split-under-lock. It is also available from my GitHub repository [3] as branch "ref-iterators".

I haven't yet completely rebased the ref-store changes (virtualization of the refs API) on top of all of these changes, but I will work on that next.

Michael

[1] http://thread.gmane.org/gmane.comp.version-control.git/290409
[2] http://mid.gmane.org/89634d216544d1102dafd5d18247bff2581d48a8.1464537050.git.mhagger@alum.mit.edu
[3] https://github.com/mhagger/git

Michael Haggerty (13):
refs: remove unnecessary "extern" keywords do_for_each_ref(): move docstring to the header file
refs: use name "prefix" consistently
delete_refs(): add a flags argument
remote rm: handle symbolic refs correctly get_ref_cache(): only create an instance if there is a submodule entry_resolves_to_object(): rename function from
ref_resolves_to_object()
ref_resolves_to_object(): new function
refs: introduce an iterator interface do_for_each_ref(): reimplement using reference iteration for_each_reflog(): don't abort for bad references
dir_iterator: new API for iterating over a directory tree for_each_reflog(): reimplement using iterators

 Makefile             |   2 +
 builtin/fetch.c      |   2 +-
 builtin/remote.c     |   8 +-
 dir-iterator.c       | 180 +++++++++++++++
 dir-iterator.h       |  86 +++++++
 iterator.h           |  81 +++++++
 refs.c               |  20 ++
 refs.h               | 139 +++++++-----
 refs/files-backend.c | 630 +++++++++++++++++++++++++++++++--------------------
 refs/iterator.c      | 376 ++++++++++++++++++++++++++++++
 refs/refs-internal.h | 225 +++++++++++++++++-
 11 files changed, 1427 insertions(+), 322 deletions(-)
 create mode 100644 dir-iterator.c
 create mode 100644 dir-iterator.h
 create mode 100644 iterator.h
 create mode 100644 refs/iterator.c
Signature · 1 line
2.8.1
#1 lore raw

PATCH 1/13 refs: remove unnecessary "extern" keywords

There's continuing work in this area, so clean up unneeded "extern" keywords rather than schlepping them around. Also split up some overlong lines and add parameter names in a couple of places.

---
 refs.h | 132 +++++++++++++++++++++++++++++++++++------------------------------
 1 file changed, 72 insertions(+), 60 deletions(-)
refs.h +72 -60
index 9230d47..21874f0 100644
@@ -52,19 +52,19 @@
 #define RESOLVE_REF_NO_RECURSE 0x02
 #define RESOLVE_REF_ALLOW_BAD_NAME 0x04
 
-extern const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
-				      unsigned char *sha1, int *flags);
+const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
+			       unsigned char *sha1, int *flags);
 
-extern char *resolve_refdup(const char *refname, int resolve_flags,
-			    unsigned char *sha1, int *flags);
+char *resolve_refdup(const char *refname, int resolve_flags,
+		     unsigned char *sha1, int *flags);
 
-extern int read_ref_full(const char *refname, int resolve_flags,
-			 unsigned char *sha1, int *flags);
-extern int read_ref(const char *refname, unsigned char *sha1);
+int read_ref_full(const char *refname, int resolve_flags,
+		  unsigned char *sha1, int *flags);
+int read_ref(const char *refname, unsigned char *sha1);
 
-extern int ref_exists(const char *refname);
+int ref_exists(const char *refname);
 
-extern int is_branch(const char *refname);
+int is_branch(const char *refname);
 
 /*
  * If refname is a non-symbolic reference that refers to a tag object,
@@ -74,24 +74,25 @@ extern int is_branch(const char *refname);
  * Symbolic references are considered unpeelable, even if they
  * ultimately resolve to a peelable tag.
  */
-extern int peel_ref(const char *refname, unsigned char *sha1);
+int peel_ref(const char *refname, unsigned char *sha1);
 
 /**
  * Resolve refname in the nested "gitlink" repository that is located
  * at path.  If the resolution is successful, return 0 and set sha1 to
  * the name of the object; otherwise, return a non-zero value.
  */
-extern int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1);
+int resolve_gitlink_ref(const char *path, const char *refname,
+			unsigned char *sha1);
 
 /*
  * Return true iff abbrev_name is a possible abbreviation for
  * full_name according to the rules defined by ref_rev_parse_rules in
  * refs.c.
  */
-extern int refname_match(const char *abbrev_name, const char *full_name);
+int refname_match(const char *abbrev_name, const char *full_name);
 
-extern int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);
-extern int dwim_log(const char *str, int len, unsigned char *sha1, char **ref);
+int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);
+int dwim_log(const char *str, int len, unsigned char *sha1, char **ref);
 
 /*
  * A ref_transaction represents a collection of ref updates
@@ -182,38 +183,45 @@ typedef int each_ref_fn(const char *refname,
  * modifies the reference also returns a nonzero value to immediately
  * stop the iteration.
  */
-extern int head_ref(each_ref_fn fn, void *cb_data);
-extern int for_each_ref(each_ref_fn fn, void *cb_data);
-extern int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data);
-extern int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken);
-extern int for_each_tag_ref(each_ref_fn fn, void *cb_data);
-extern int for_each_branch_ref(each_ref_fn fn, void *cb_data);
-extern int for_each_remote_ref(each_ref_fn fn, void *cb_data);
-extern int for_each_replace_ref(each_ref_fn fn, void *cb_data);
-extern int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data);
-extern int for_each_glob_ref_in(each_ref_fn fn, const char *pattern, const char *prefix, void *cb_data);
+int head_ref(each_ref_fn fn, void *cb_data);
+int for_each_ref(each_ref_fn fn, void *cb_data);
+int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data);
+int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data,
+			unsigned int broken);
+int for_each_tag_ref(each_ref_fn fn, void *cb_data);
+int for_each_branch_ref(each_ref_fn fn, void *cb_data);
+int for_each_remote_ref(each_ref_fn fn, void *cb_data);
+int for_each_replace_ref(each_ref_fn fn, void *cb_data);
+int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data);
+int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
+			 const char *prefix, void *cb_data);
 
-extern int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
-extern int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
-extern int for_each_ref_in_submodule(const char *submodule, const char *prefix,
+int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
+int for_each_ref_submodule(const char *submodule,
+			   each_ref_fn fn, void *cb_data);
+int for_each_ref_in_submodule(const char *submodule, const char *prefix,
 		each_ref_fn fn, void *cb_data);
-extern int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
-extern int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
-extern int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
+int for_each_tag_ref_submodule(const char *submodule,
+			       each_ref_fn fn, void *cb_data);
+int for_each_branch_ref_submodule(const char *submodule,
+				  each_ref_fn fn, void *cb_data);
+int for_each_remote_ref_submodule(const char *submodule,
+				  each_ref_fn fn, void *cb_data);
 
-extern int head_ref_namespaced(each_ref_fn fn, void *cb_data);
-extern int for_each_namespaced_ref(each_ref_fn fn, void *cb_data);
+int head_ref_namespaced(each_ref_fn fn, void *cb_data);
+int for_each_namespaced_ref(each_ref_fn fn, void *cb_data);
 
 /* can be used to learn about broken ref and symref */
-extern int for_each_rawref(each_ref_fn fn, void *cb_data);
+int for_each_rawref(each_ref_fn fn, void *cb_data);
 
 static inline const char *has_glob_specials(const char *pattern)
 {
 	return strpbrk(pattern, "?*[");
 }
 
-extern void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname);
-extern void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames);
+void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname);
+void warn_dangling_symrefs(FILE *fp, const char *msg_fmt,
+			   const struct string_list *refnames);
 
 /*
  * Flags for controlling behaviour of pack_refs()
@@ -245,13 +253,13 @@ int pack_refs(unsigned int flags);
 int safe_create_reflog(const char *refname, int force_create, struct strbuf *err);
 
 /** Reads log for the value of ref during at_time. **/
-extern int read_ref_at(const char *refname, unsigned int flags,
-		       unsigned long at_time, int cnt,
-		       unsigned char *sha1, char **msg,
-		       unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt);
+int read_ref_at(const char *refname, unsigned int flags,
+		unsigned long at_time, int cnt,
+		unsigned char *sha1, char **msg,
+		unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt);
 
 /** Check if a particular reflog exists */
-extern int reflog_exists(const char *refname);
+int reflog_exists(const char *refname);
 
 /*
  * Delete the specified reference. If old_sha1 is non-NULL, then
@@ -260,21 +268,25 @@ extern int reflog_exists(const char *refname);
  * exists, regardless of its old value. It is an error for old_sha1 to
  * be NULL_SHA1. flags is passed through to ref_transaction_delete().
  */
-extern int delete_ref(const char *refname, const unsigned char *old_sha1,
-		      unsigned int flags);
+int delete_ref(const char *refname, const unsigned char *old_sha1,
+	       unsigned int flags);
 
 /*
  * Delete the specified references. If there are any problems, emit
  * errors but attempt to keep going (i.e., the deletes are not done in
  * an all-or-nothing transaction).
  */
-extern int delete_refs(struct string_list *refnames);
+int delete_refs(struct string_list *refnames);
 
 /** Delete a reflog */
-extern int delete_reflog(const char *refname);
+int delete_reflog(const char *refname);
 
 /* iterate over reflog entries */
-typedef int each_reflog_ent_fn(unsigned char *osha1, unsigned char *nsha1, const char *, unsigned long, int, const char *, void *);
+typedef int each_reflog_ent_fn(
+		unsigned char *old_sha1, unsigned char *new_sha1,
+		const char *committer, unsigned long timestamp,
+		int tz, const char *msg, void *cb_data);
+
 int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data);
 int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void *cb_data);
 
@@ -282,7 +294,7 @@ int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void
  * Calls the specified function for each reflog file until it returns nonzero,
  * and returns the value
  */
-extern int for_each_reflog(each_ref_fn, void *);
+int for_each_reflog(each_ref_fn fn, void *cb_data);
 
 #define REFNAME_ALLOW_ONELEVEL 1
 #define REFNAME_REFSPEC_PATTERN 2
@@ -295,16 +307,16 @@ extern int for_each_reflog(each_ref_fn, void *);
  * allow a single "*" wildcard character in the refspec. No leading or
  * repeated slashes are accepted.
  */
-extern int check_refname_format(const char *refname, int flags);
+int check_refname_format(const char *refname, int flags);
 
-extern const char *prettify_refname(const char *refname);
+const char *prettify_refname(const char *refname);
 
-extern char *shorten_unambiguous_ref(const char *refname, int strict);
+char *shorten_unambiguous_ref(const char *refname, int strict);
 
 /** rename ref, return 0 on success **/
-extern int rename_ref(const char *oldref, const char *newref, const char *logmsg);
+int rename_ref(const char *oldref, const char *newref, const char *logmsg);
 
-extern int create_symref(const char *refname, const char *target, const char *logmsg);
+int create_symref(const char *refname, const char *target, const char *logmsg);
 
 /*
  * Update HEAD of the specified gitdir.
@@ -313,7 +325,7 @@ extern int create_symref(const char *refname, const char *target, const char *lo
  * $GIT_DIR points to.
  * Return 0 if successful, non-zero otherwise.
  * */
-extern int set_worktree_head_symref(const char *gitdir, const char *target);
+int set_worktree_head_symref(const char *gitdir, const char *target);
 
 enum action_on_err {
 	UPDATE_REFS_MSG_ON_ERR,
@@ -463,7 +475,7 @@ int update_ref(const char *msg, const char *refname,
 	       const unsigned char *new_sha1, const unsigned char *old_sha1,
 	       unsigned int flags, enum action_on_err onerr);
 
-extern int parse_hide_refs_config(const char *var, const char *value, const char *);
+int parse_hide_refs_config(const char *var, const char *value, const char *);
 
 /*
  * Check whether a ref is hidden. If no namespace is set, both the first and
@@ -473,7 +485,7 @@ extern int parse_hide_refs_config(const char *var, const char *value, const char
  * the ref is outside that namespace, the first parameter is NULL. The second
  * parameter always points to the full ref name.
  */
-extern int ref_is_hidden(const char *, const char *);
+int ref_is_hidden(const char *, const char *);
 
 enum ref_type {
 	REF_TYPE_PER_WORKTREE,
@@ -522,11 +534,11 @@ typedef void reflog_expiry_cleanup_fn(void *cb_data);
  * enum expire_reflog_flags. The three function pointers are described
  * above. On success, return zero.
  */
-extern int reflog_expire(const char *refname, const unsigned char *sha1,
-			 unsigned int flags,
-			 reflog_expiry_prepare_fn prepare_fn,
-			 reflog_expiry_should_prune_fn should_prune_fn,
-			 reflog_expiry_cleanup_fn cleanup_fn,
-			 void *policy_cb_data);
+int reflog_expire(const char *refname, const unsigned char *sha1,
+		  unsigned int flags,
+		  reflog_expiry_prepare_fn prepare_fn,
+		  reflog_expiry_should_prune_fn should_prune_fn,
+		  reflog_expiry_cleanup_fn cleanup_fn,
+		  void *policy_cb_data);
 
 #endif /* REFS_H */
Signature · 1 line
2.8.1
#2 lore raw

PATCH 2/13 do_for_each_ref(): move docstring to the header file

In reply to Michael Haggerty
---
 refs/files-backend.c |  9 ---------
 refs/refs-internal.h | 10 +++++++++-
 2 files changed, 9 insertions(+), 10 deletions(-)
refs/files-backend.c +0 -9
index 1230dfb..68db3e8 100644
@@ -1878,15 +1878,6 @@ static int do_for_each_entry(struct ref_cache *refs, const char *base,
 	return retval;
 }
 
-/*
- * Call fn for each reference in the specified ref_cache for which the
- * refname begins with base.  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.
- */
 int do_for_each_ref(const char *submodule, const char *base,
 		    each_ref_fn fn, int trim, int flags, void *cb_data)
 {
refs/refs-internal.h +9 -1
index 1bb3d87..b4dd545 100644
@@ -249,7 +249,15 @@ int rename_ref_available(const char *oldname, const char *newname);
 #define DO_FOR_EACH_INCLUDE_BROKEN 0x01
 
 /*
- * The common backend for the for_each_*ref* functions
+ * Call fn for each reference in the specified submodule for which the
+ * refname begins with base. 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.
+ *
+ * This is the common backend for the for_each_*ref* functions.
  */
 int do_for_each_ref(const char *submodule, const char *base,
 		    each_ref_fn fn, int trim, int flags, void *cb_data);
Signature · 1 line
2.8.1
#3 lore raw

PATCH 3/13 refs: use name "prefix" consistently

In reply to Michael Haggerty

In the context of the for_each_ref() functions, call the prefix that references must start with "prefix". (In some places it was called "base".) This is clearer, and also prevents confusion with another planned use of the word "base".

---
 refs/files-backend.c | 24 ++++++++++++------------
 refs/refs-internal.h | 14 +++++++-------
 2 files changed, 19 insertions(+), 19 deletions(-)
refs/files-backend.c +12 -12
index 68db3e8..5af7441 100644
@@ -542,7 +542,7 @@ static struct ref_entry *current_ref;
 typedef int each_ref_entry_fn(struct ref_entry *entry, void *cb_data);
 
 struct ref_entry_cb {
-	const char *base;
+	const char *prefix;
 	int trim;
 	int flags;
 	each_ref_fn *fn;
@@ -559,7 +559,7 @@ static int do_one_ref(struct ref_entry *entry, void *cb_data)
 	struct ref_entry *old_current_ref;
 	int retval;
 
-	if (!starts_with(entry->name, data->base))
+	if (!starts_with(entry->name, data->prefix))
 		return 0;
 
 	if (!(data->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
@@ -1824,12 +1824,12 @@ int peel_ref(const char *refname, unsigned char *sha1)
 
 /*
  * Call fn for each reference in the specified ref_cache, omitting
- * references not in the containing_dir of base.  fn is called for all
- * references, including broken ones.  If fn ever returns a non-zero
+ * references not in the containing_dir of prefix. Call fn for all
+ * references, including broken ones. If fn ever returns a non-zero
  * value, stop the iteration and return that value; otherwise, return
  * 0.
  */
-static int do_for_each_entry(struct ref_cache *refs, const char *base,
+static int do_for_each_entry(struct ref_cache *refs, const char *prefix,
 			     each_ref_entry_fn fn, void *cb_data)
 {
 	struct packed_ref_cache *packed_ref_cache;
@@ -1846,8 +1846,8 @@ static int do_for_each_entry(struct ref_cache *refs, const char *base,
 	 * disk.
 	 */
 	loose_dir = get_loose_refs(refs);
-	if (base && *base) {
-		loose_dir = find_containing_dir(loose_dir, base, 0);
+	if (prefix && *prefix) {
+		loose_dir = find_containing_dir(loose_dir, prefix, 0);
 	}
 	if (loose_dir)
 		prime_ref_dir(loose_dir);
@@ -1855,8 +1855,8 @@ static int do_for_each_entry(struct ref_cache *refs, const char *base,
 	packed_ref_cache = get_packed_ref_cache(refs);
 	acquire_packed_ref_cache(packed_ref_cache);
 	packed_dir = get_packed_ref_dir(packed_ref_cache);
-	if (base && *base) {
-		packed_dir = find_containing_dir(packed_dir, base, 0);
+	if (prefix && *prefix) {
+		packed_dir = find_containing_dir(packed_dir, prefix, 0);
 	}
 
 	if (packed_dir && loose_dir) {
@@ -1878,14 +1878,14 @@ static int do_for_each_entry(struct ref_cache *refs, const char *base,
 	return retval;
 }
 
-int do_for_each_ref(const char *submodule, const char *base,
+int do_for_each_ref(const char *submodule, const char *prefix,
 		    each_ref_fn fn, int trim, int flags, void *cb_data)
 {
 	struct ref_entry_cb data;
 	struct ref_cache *refs;
 
 	refs = get_ref_cache(submodule);
-	data.base = base;
+	data.prefix = prefix;
 	data.trim = trim;
 	data.flags = flags;
 	data.fn = fn;
@@ -1896,7 +1896,7 @@ int do_for_each_ref(const char *submodule, const char *base,
 	if (ref_paranoia)
 		data.flags |= DO_FOR_EACH_INCLUDE_BROKEN;
 
-	return do_for_each_entry(refs, base, do_one_ref, &data);
+	return do_for_each_entry(refs, prefix, do_one_ref, &data);
 }
 
 /*
refs/refs-internal.h +7 -7
index b4dd545..8ad02d8 100644
@@ -250,16 +250,16 @@ int rename_ref_available(const char *oldname, const char *newname);
 
 /*
  * Call fn for each reference in the specified submodule for which the
- * refname begins with base. 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.
+ * 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.
  *
  * This is the common backend for the for_each_*ref* functions.
  */
-int do_for_each_ref(const char *submodule, const char *base,
+int do_for_each_ref(const char *submodule, const char *prefix,
 		    each_ref_fn fn, int trim, int flags, void *cb_data);
 
 /*
Signature · 1 line
2.8.1
#4 lore raw

PATCH 4/13 delete_refs(): add a flags argument

In reply to Michael Haggerty

This will be useful for passing REF_NODEREF through.

---
 builtin/fetch.c      | 2 +-
 builtin/remote.c     | 4 ++--
 refs.h               | 5 +++--
 refs/files-backend.c | 4 ++--
 4 files changed, 8 insertions(+), 7 deletions(-)
builtin/fetch.c +1 -1
index f8455bd..b55c83c 100644
@@ -806,7 +806,7 @@ static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
 		for (ref = stale_refs; ref; ref = ref->next)
 			string_list_append(&refnames, ref->name);
 
-		result = delete_refs(&refnames);
+		result = delete_refs(&refnames, 0);
 		string_list_clear(&refnames, 0);
 	}
 
builtin/remote.c +2 -2
index fda5c2e..1bbf9b4 100644
@@ -788,7 +788,7 @@ static int rm(int argc, const char **argv)
 	strbuf_release(&buf);
 
 	if (!result)
-		result = delete_refs(&branches);
+		result = delete_refs(&branches, 0);
 	string_list_clear(&branches, 0);
 
 	if (skipped.nr) {
@@ -1303,7 +1303,7 @@ static int prune_remote(const char *remote, int dry_run)
 	string_list_sort(&refs_to_prune);
 
 	if (!dry_run)
-		result |= delete_refs(&refs_to_prune);
+		result |= delete_refs(&refs_to_prune, 0);
 
 	for_each_string_list_item(item, &states.stale) {
 		const char *refname = item->util;
refs.h +3 -2
index 21874f0..6d515a4 100644
@@ -274,9 +274,10 @@ int delete_ref(const char *refname, const unsigned char *old_sha1,
 /*
  * Delete the specified references. If there are any problems, emit
  * errors but attempt to keep going (i.e., the deletes are not done in
- * an all-or-nothing transaction).
+ * an all-or-nothing transaction). flags is passed through to
+ * ref_transaction_delete().
  */
-int delete_refs(struct string_list *refnames);
+int delete_refs(struct string_list *refnames, unsigned int flags);
 
 /** Delete a reflog */
 int delete_reflog(const char *refname);
refs/files-backend.c +2 -2
index 5af7441..a0d09f4 100644
@@ -2403,7 +2403,7 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
 	return 0;
 }
 
-int delete_refs(struct string_list *refnames)
+int delete_refs(struct string_list *refnames, unsigned int flags)
 {
 	struct strbuf err = STRBUF_INIT;
 	int i, result = 0;
@@ -2432,7 +2432,7 @@ int delete_refs(struct string_list *refnames)
 	for (i = 0; i < refnames->nr; i++) {
 		const char *refname = refnames->items[i].string;
 
-		if (delete_ref(refname, NULL, 0))
+		if (delete_ref(refname, NULL, flags))
 			result |= error(_("could not remove reference %s"), refname);
 	}
 
Signature · 1 line
2.8.1
#5 lore raw

PATCH 5/13 remote rm: handle symbolic refs correctly

In reply to Michael Haggerty

In the modern world of reference backends, it is not OK to delete a symref by unlink()ing the file directly. This must be done via the refs API.

We do so by adding the symref to the list of references to delete along with the non-symbolic references, then calling delete_refs() with the new flags option set to REF_NODEREF.

---
 builtin/remote.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
builtin/remote.c +1 -5
index 1bbf9b4..c4b4d67 100644
@@ -539,10 +539,6 @@ static int add_branch_for_removal(const char *refname,
 		return 0;
 	}
 
-	/* make sure that symrefs are deleted */
-	if (flags & REF_ISSYMREF)
-		return unlink(git_path("%s", refname));
-
 	string_list_append(branches->branches, refname);
 
 	return 0;
@@ -788,7 +784,7 @@ static int rm(int argc, const char **argv)
 	strbuf_release(&buf);
 
 	if (!result)
-		result = delete_refs(&branches, 0);
+		result = delete_refs(&branches, REF_NODEREF);
 	string_list_clear(&branches, 0);
 
 	if (skipped.nr) {
Signature · 1 line
2.8.1
#6 lore raw

PATCH 6/13 get_ref_cache(): only create an instance if there is a submodule

In reply to Michael Haggerty

If there is not a nonbare repository where a submodule is supposedly located, then don't instantiate a ref_cache for it.

The analogous check can be removed from resolve_gitlink_ref().

---
This doesn't actually reduce the number of ref_cache instances generated by out test suite, but it is a more logical place for the check that was added in

a2d5156c resolve_gitlink_ref: ignore non-repository paths

 refs/files-backend.c | 33 ++++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 11 deletions(-)
refs/files-backend.c +22 -11
index a0d09f4..142c977 100644
@@ -954,15 +954,26 @@ static struct ref_cache *lookup_ref_cache(const char *submodule)
 
 /*
  * Return a pointer to a ref_cache for the specified submodule. For
- * the main repository, use submodule==NULL. The returned structure
- * will be allocated and initialized but not necessarily populated; it
- * should not be freed.
+ * the main repository, use submodule==NULL; such a call cannot fail.
+ * For a submodule, the submodule must exist and be a nonbare
+ * repository, otherwise return NULL.
+ *
+ * The returned structure will be allocated and initialized but not
+ * necessarily populated; it should not be freed.
  */
 static struct ref_cache *get_ref_cache(const char *submodule)
 {
 	struct ref_cache *refs = lookup_ref_cache(submodule);
-	if (!refs)
-		refs = create_ref_cache(submodule);
+
+	if (!refs) {
+		struct strbuf submodule_sb = STRBUF_INIT;
+
+		strbuf_addstr(&submodule_sb, submodule);
+		if (is_nonbare_repository_dir(&submodule_sb))
+			refs = create_ref_cache(submodule);
+		strbuf_release(&submodule_sb);
+	}
+
 	return refs;
 }
 
@@ -1341,13 +1352,10 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh
 		return -1;
 
 	strbuf_add(&submodule, path, len);
-	refs = lookup_ref_cache(submodule.buf);
+	refs = get_ref_cache(submodule.buf);
 	if (!refs) {
-		if (!is_nonbare_repository_dir(&submodule)) {
-			strbuf_release(&submodule);
-			return -1;
-		}
-		refs = create_ref_cache(submodule.buf);
+		strbuf_release(&submodule);
+		return -1;
 	}
 	strbuf_release(&submodule);
 
@@ -1885,6 +1893,9 @@ int do_for_each_ref(const char *submodule, const char *prefix,
 	struct ref_cache *refs;
 
 	refs = get_ref_cache(submodule);
+	if (!refs)
+		return 0;
+
 	data.prefix = prefix;
 	data.trim = trim;
 	data.flags = flags;
Signature · 1 line
2.8.1
#7 lore raw

PATCH 7/13 entry_resolves_to_object(): rename function from ref_resolves_to_object()

In reply to Michael Haggerty

Free up the old name for a more general purpose.

---
 refs/files-backend.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
refs/files-backend.c +3 -3
index 142c977..1a46f32 100644
@@ -517,7 +517,7 @@ static void sort_ref_dir(struct ref_dir *dir)
  * an object in the database.  Emit a warning if the referred-to
  * object does not exist.
  */
-static int ref_resolves_to_object(struct ref_entry *entry)
+static int entry_resolves_to_object(struct ref_entry *entry)
 {
 	if (entry->flag & REF_ISBROKEN)
 		return 0;
@@ -563,7 +563,7 @@ static int do_one_ref(struct ref_entry *entry, void *cb_data)
 		return 0;
 
 	if (!(data->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
-	      !ref_resolves_to_object(entry))
+	      !entry_resolves_to_object(entry))
 		return 0;
 
 	/* Store the old value, in case this is a recursive call: */
@@ -2228,7 +2228,7 @@ static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)
 		return 0;
 
 	/* Do not pack symbolic or broken refs: */
-	if ((entry->flag & REF_ISSYMREF) || !ref_resolves_to_object(entry))
+	if ((entry->flag & REF_ISSYMREF) || !entry_resolves_to_object(entry))
 		return 0;
 
 	/* Add a packed ref cache entry equivalent to the loose entry. */
Signature · 1 line
2.8.1
#8 lore raw

PATCH 8/13 ref_resolves_to_object(): new function

In reply to Michael Haggerty

Extract new function ref_resolves_to_object() from entry_resolves_to_object(). It can be used even if there is no ref_entry at hand.

---
 refs/files-backend.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)
refs/files-backend.c +23 -10
index 1a46f32..8ab4d5f 100644
@@ -513,19 +513,32 @@ static void sort_ref_dir(struct ref_dir *dir)
 }
 
 /*
- * Return true iff the reference described by entry can be resolved to
- * an object in the database.  Emit a warning if the referred-to
- * object does not exist.
+ * Return true if refname, which has the specified oid and flags, can
+ * be resolved to an object in the database. If the referred-to object
+ * does not exist, emit a warning and return false.
+ */
+static int ref_resolves_to_object(const char *refname,
+				  const struct object_id *oid,
+				  unsigned int flags)
+{
+	if (flags & REF_ISBROKEN)
+		return 0;
+	if (!has_sha1_file(oid->hash)) {
+		error("%s does not point to a valid object!", refname);
+		return 0;
+	}
+	return 1;
+}
+
+/*
+ * Return true if the reference described by entry can be resolved to
+ * an object in the database; otherwise, emit a warning and return
+ * false.
  */
 static int entry_resolves_to_object(struct ref_entry *entry)
 {
-	if (entry->flag & REF_ISBROKEN)
-		return 0;
-	if (!has_sha1_file(entry->u.value.oid.hash)) {
-		error("%s does not point to a valid object!", entry->name);
-		return 0;
-	}
-	return 1;
+	return ref_resolves_to_object(entry->name,
+				      &entry->u.value.oid, entry->flag);
 }
 
 /*
Signature · 1 line
2.8.1
#9 lore raw

PATCH 9/13 refs: introduce an iterator interface

In reply to Michael Haggerty

Currently, the API for iterating over references is via a family of for_each_ref()-type functions that invoke a callback function for each selected reference. All of these eventually call do_for_each_ref(), which knows how to do one thing: iterate in parallel through two ref_caches, one for loose and one for packed refs, giving loose references precedence over packed refs. This is rather complicated code, and is quite specialized to the files backend. It also requires callers to encapsulate their work into a callback function, which often means that they have to define and use a "cb_data" struct to manage their context.

The current design is already bursting at the seams, and will become even more awkward in the upcoming world of multiple reference storage backends:

* Per-worktree vs. shared references are currently handled via a kludge in git_path() rather than iterating over each part of the reference namespace separately and merging the results. This kludge will cease to work when we have multiple reference storage backends.

* The current scheme is inflexible. What if we sometimes want to bypass the ref_cache, or use it only for packed or only for loose refs? What if we want to store symbolic refs in one type of storage backend and non-symbolic ones in another?

In the future, each reference backend will need to define its own way of iterating over references. The crux of the problem with the current design is that it is impossible to compose for_each_ref()-style iterations, because the flow of control is owned by the for_each_ref() function. There is nothing that a caller can do but iterate through all references in a single burst, so there is no way for it to interleave references from multiple backends and present the result to the rest of the world as a single compound backend.

This commit introduces a new iteration primitive for references: a ref_iterator. A ref_iterator is a polymorphic object that a reference storage backend can be asked to instantiate. There are three functions that can be applied to a ref_iterator:

* ref_iterator_advance(): move to the next reference in the iteration
* ref_iterator_abort(): end the iteration before it is exhausted
* ref_iterator_peel(): peel the reference currently being looked at

Iterating using a ref_iterator leaves the flow of control in the hands of the caller, which means that ref_iterators from multiple sources (e.g., loose and packed refs) can be composed and presented to the world as a single compound ref_iterator.

It also means that the backend code for implementing reference iteration will sometimes be more complicated. For example, the cache_ref_iterator (which iterates over a ref_cache) can't use the C stack to recurse; instead, it must manage its own stack internally as explicit data structures. There is also a lot of boilerplate connected with object-oriented programming in C.

Eventually, end-user callers will be able to be written in a more natural way—managing their own flow of control rather than having to work via callbacks. Since there will only be a few reference backends but there are many consumers of this API, this is a good tradeoff.

More importantly, we gain composability, and especially the possibility of writing interchangeable parts that can work with any ref_iterator.

For example, merge_ref_iterator implements a generic way of merging the contents of any two ref_iterators. It is used to merge loose + packed refs as part of the implementation of the files_ref_iterator. But it will also be possible to use it to merge other pairs of reference sources (e.g., per-worktree vs. shared refs).

Another example is prefix_ref_iterator, which can be used to trim a prefix off the front of reference names before presenting them to the caller (e.g., "refs/heads/master" -> "master").

In this patch, we introduce the iterator abstraction and many utilities, and implement a reference iterator for the files ref storage backend. (I've written several other obvious utilities, for example a generic way to filter references being iterated over. These will probably be useful in the future. But they are not needed for this patch series, so I am not including them at this time.)

In a moment we will rewrite do_for_each_ref() to work via reference iterators (allowing some special-purpose code to be discarded), and do something similar for reflogs. In future patch series, we will expose the ref_iterator abstraction in the public refs API so that callers can use it directly.

Implementation note: I tried abstracting this a layer further to allow generic iterators (over arbitrary types of objects) and generic utilities like a generic merge_iterator. But the implementation in C was very cumbersome, involving (in my opinion) too much boilerplate and too much unsafe casting, some of which would have had to be done on the caller side. However, I did put a few iterator-related constants in a top-level header file, iterator.h, as they will be useful in a moment to implement iteration over directory trees and possibly other types of iterators in the future.

---
 Makefile             |   1 +
 iterator.h           |  81 ++++++++++++
 refs.h               |   4 +-
 refs/files-backend.c | 282 +++++++++++++++++++++++++++++++++++++++++
 refs/iterator.c      | 347 +++++++++++++++++++++++++++++++++++++++++++++++++++
 refs/refs-internal.h | 193 ++++++++++++++++++++++++++++
 6 files changed, 907 insertions(+), 1 deletion(-)
 create mode 100644 iterator.h
 create mode 100644 refs/iterator.c
Makefile +1 -0
index a83e322..ac8f365 100644
@@ -786,6 +786,7 @@ LIB_OBJS += read-cache.o
 LIB_OBJS += reflog-walk.o
 LIB_OBJS += refs.o
 LIB_OBJS += refs/files-backend.o
+LIB_OBJS += refs/iterator.o
 LIB_OBJS += ref-filter.o
 LIB_OBJS += remote.o
 LIB_OBJS += replace_object.o
iterator.h new file +81 -0

81 lines not on this page · raw message

refs.h +3 -1
index 6d515a4..442c1a5 100644
@@ -141,7 +141,9 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **ref);
 struct ref_transaction;
 
 /*
- * Bit values set in the flags argument passed to each_ref_fn():
+ * Bit values set in the flags argument passed to each_ref_fn() and
+ * stored in ref_iterator::flags. Other bits are for internal use
+ * only:
  */
 
 /* Reference is a symbolic reference. */
refs/files-backend.c +282 -0

300 lines not on this page · raw message

refs/iterator.c new file +347 -0

347 lines not on this page · raw message

refs/refs-internal.h +193 -0

199 lines not on this page · raw message

Signature · 1 line
2.8.1
#10 lore raw

Re: [PATCH 09/13] refs: introduce an iterator interface

On 30/05/16 08:55, Michael Haggerty wrote:
[snip]

Quoted 68 lines

/* Reference is a symbolic reference. */

refs/files-backend.c +52 -0
index 8ab4d5f..dbf1587 100644
@@ -1,6 +1,7 @@
 #include "../cache.h"
 #include "../refs.h"
 #include "refs-internal.h"
+#include "../iterator.h"
 #include "../lockfile.h"
 #include "../object.h"
 #include "../dir.h"
@@ -704,6 +705,154 @@ static void prime_ref_dir(struct ref_dir *dir)
 	}
 }
 
+/*
+ * A level in the reference hierarchy that is currently being iterated
+ * through.
+ */
+struct cache_ref_iterator_level {
+	/*
+	 * The ref_dir being iterated over at this level. The ref_dir
+         * is sorted before being stored here.
+	 */
+	struct ref_dir *dir;
+
+	/*
+	 * The index of the current entry within dir (which might
+	 * itself be a directory). If index == -1, then the iteration
+	 * hasn't yet begun. If index == dir->nr, then the iteration
+	 * through this level is over.
+	 */
+	int index;
+};
+
+/*
+ * Represent an iteration through a ref_dir in the memory cache. The
+ * iteration recurses through subdirectories.
+ */
+struct cache_ref_iterator {
+	struct ref_iterator base;
+
+	/*
+	 * The number of levels currently on the stack. This is always
+	 * at least 1, because when it becomes zero the iteration is
+	 * ended and this struct is freed.
+	 */
+	size_t levels_nr;
+
+	/* The number of levels that have been allocated on the stack */
+	size_t levels_alloc;
+
+	/*
+	 * A stack of levels. levels[0] is the uppermost level that is
+	 * being iterated over in this iteration. (This is not
+	 * necessary the top level in the references hierarchy. If we
+	 * are iterating through a subtree, then levels[0] will hold
+	 * the ref_dir for that subtree, and subsequent levels will go
+	 * on from there.)
+	 */
+	struct cache_ref_iterator_level *levels;
+};
+
+static int cache_ref_iterator_advance(struct ref_iterator *ref_iterator)
+{
+	struct cache_ref_iterator *iter =

refs/files-backend.c static void prime_ref_dir(struct ref_dir *dir) · line 768

Quoted 10 lines
refs/files-backend.c +10 -0
+		(struct cache_ref_iterator *)ref_iterator;
+
+	while (1) {
+		struct cache_ref_iterator_level *level =
+			&iter->levels[iter->levels_nr - 1];
+		struct ref_dir *dir = level->dir;
+		struct ref_entry *entry;
+
+		if (level->index == -1)
+			sort_ref_dir(dir);

do you need to sort here ...

Quoted 9 lines
refs/files-backend.c +9 -0
+
+		if (++level->index == level->dir->nr) {
+			/* This level is exhausted; pop up a level */
+			if (--iter->levels_nr == 0)
+				return ref_iterator_abort(ref_iterator);
+
+			continue;
+		}
+

refs/files-backend.c static void prime_ref_dir(struct ref_dir *dir) · line 787

Quoted 10 lines
refs/files-backend.c +10 -0
+		entry = dir->entries[level->index];
+
+		if (entry->flag & REF_DIR) {
+			/* push down a level */
+			ALLOC_GROW(iter->levels, iter->levels_nr + 1,
+				   iter->levels_alloc);
+
+			level = &iter->levels[iter->levels_nr++];
+			level->dir = get_ref_dir(entry);
+			sort_ref_dir(level->dir);

... given that you sort here?

refs/files-backend.c static void prime_ref_dir(struct ref_dir *dir) · line 797

Quoted 10 lines
refs/files-backend.c +10 -0
+			level->index = -1;
+		} else {
+			iter->base.refname = entry->name;
+			iter->base.oid = &entry->u.value.oid;
+			iter->base.flags = entry->flag;
+			return ITER_OK;
+		}
+	}
+}
+

ATB,
Ramsay Jones

#11 lore raw

Re: [PATCH 09/13] refs: introduce an iterator interface

Quoted 98 lines · Ramsay Jones

On 30/05/16 08:55, Michael Haggerty wrote:
[snip]

Quoted 68 lines

/* Reference is a symbolic reference. */

refs/files-backend.c +52 -0
index 8ab4d5f..dbf1587 100644
@@ -1,6 +1,7 @@
 #include "../cache.h"
 #include "../refs.h"
 #include "refs-internal.h"
+#include "../iterator.h"
 #include "../lockfile.h"
 #include "../object.h"
 #include "../dir.h"
@@ -704,6 +705,154 @@ static void prime_ref_dir(struct ref_dir *dir)
 	}
 }
 
+/*
+ * A level in the reference hierarchy that is currently being iterated
+ * through.
+ */
+struct cache_ref_iterator_level {
+	/*
+	 * The ref_dir being iterated over at this level. The ref_dir
+         * is sorted before being stored here.
+	 */
+	struct ref_dir *dir;
+
+	/*
+	 * The index of the current entry within dir (which might
+	 * itself be a directory). If index == -1, then the iteration
+	 * hasn't yet begun. If index == dir->nr, then the iteration
+	 * through this level is over.
+	 */
+	int index;
+};
+
+/*
+ * Represent an iteration through a ref_dir in the memory cache. The
+ * iteration recurses through subdirectories.
+ */
+struct cache_ref_iterator {
+	struct ref_iterator base;
+
+	/*
+	 * The number of levels currently on the stack. This is always
+	 * at least 1, because when it becomes zero the iteration is
+	 * ended and this struct is freed.
+	 */
+	size_t levels_nr;
+
+	/* The number of levels that have been allocated on the stack */
+	size_t levels_alloc;
+
+	/*
+	 * A stack of levels. levels[0] is the uppermost level that is
+	 * being iterated over in this iteration. (This is not
+	 * necessary the top level in the references hierarchy. If we
+	 * are iterating through a subtree, then levels[0] will hold
+	 * the ref_dir for that subtree, and subsequent levels will go
+	 * on from there.)
+	 */
+	struct cache_ref_iterator_level *levels;
+};
+
+static int cache_ref_iterator_advance(struct ref_iterator *ref_iterator)
+{
+	struct cache_ref_iterator *iter =

refs/files-backend.c static void prime_ref_dir(struct ref_dir *dir) · line 768

Quoted 10 lines
refs/files-backend.c +10 -0
+		(struct cache_ref_iterator *)ref_iterator;
+
+	while (1) {
+		struct cache_ref_iterator_level *level =
+			&iter->levels[iter->levels_nr - 1];
+		struct ref_dir *dir = level->dir;
+		struct ref_entry *entry;
+
+		if (level->index == -1)
+			sort_ref_dir(dir);

do you need to sort here ...

Quoted 13 lines
refs/files-backend.c +13 -0
+
+		if (++level->index == level->dir->nr) {
+			/* This level is exhausted; pop up a level */
+			if (--iter->levels_nr == 0)
+				return ref_iterator_abort(ref_iterator);
+
+			continue;
+		}
+
+		entry = dir->entries[level->index];
+
+		if (entry->flag & REF_DIR) {
+			/* push down a level */
Quoted 8 lines

refs/files-backend.c static void prime_ref_dir(struct ref_dir *dir) · line 787

Quoted 6 lines
refs/files-backend.c +6 -0
+			ALLOC_GROW(iter->levels, iter->levels_nr + 1,
+				   iter->levels_alloc);
+
+			level = &iter->levels[iter->levels_nr++];
+			level->dir = get_ref_dir(entry);
+			sort_ref_dir(level->dir);

... given that you sort here?

I had intended to say 'or vice versa' here. When I wrote this, I had not finished reading this patch (let alone the series). Now, I suspect that you can simply drop this 'sort_ref_dir()' call site. Unless I've misread the code, of course! ;-)

ATB,
Ramsay Jones

#12 lore raw

Re: [PATCH 09/13] refs: introduce an iterator interface

Quoted 22 lines · Ramsay Jones
Quoted 14 lines · Ramsay Jones

On 30/05/16 08:55, Michael Haggerty wrote:
[snip]

refs/files-backend.c static void prime_ref_dir(struct ref_dir *dir) · line 768

Quoted 7 lines

/* Reference is a symbolic reference. */

refs/files-backend.c +1 -0
index 8ab4d5f..dbf1587 100644
[...]
+			sort_ref_dir(dir);

do you need to sort here ...

Quoted 4 lines

refs/files-backend.c static void prime_ref_dir(struct ref_dir *dir) · line 787

Quoted 2 lines

[...]

refs/files-backend.c +1 -0
+			sort_ref_dir(level->dir);

... given that you sort here?

Quoted 4 lines

I had intended to say 'or vice versa' here. When I wrote this, I had not finished reading this patch (let alone the series). Now, I suspect that you can simply drop this 'sort_ref_dir()' call site. Unless I've misread the code, of course! ;-)

Yes, you are right. Thanks for catching this! I'll fix it in v2.

Michael

#13 lore raw

Re: [PATCH 09/13] refs: introduce an iterator interface

In reply to Michael Haggerty
Quoted 14 lines · Michael Haggerty

[...]
This commit introduces a new iteration primitive for references: a ref_iterator. A ref_iterator is a polymorphic object that a reference storage backend can be asked to instantiate. There are three functions that can be applied to a ref_iterator:

* ref_iterator_advance(): move to the next reference in the iteration
* ref_iterator_abort(): end the iteration before it is exhausted
* ref_iterator_peel(): peel the reference currently being looked at

---

refs/refs-internal.h int rename_ref_available(const char *oldname, const char *newname); · line 255

Quoted 7 lines
refs/refs-internal.h +7 -0
@@ -249,6 +249,199 @@ int rename_ref_available(const char *oldname, const char *newname);
+/*
+ * Advance the iterator to the first or next item and return ITER_OK.
+ * If the iteration is exhausted, free the resources associated with
+ * the ref_iterator and return ITER_DONE. On errors, free the iterator
+ * resources and return ITER_ERROR. It is a bug to use ref_iterator or
+ * call this function again after it has returned false.
+ */

Either:

s/false/something other than ITER_OK/

or:

s/false/ITER_DONE or ITER_ERROR/

refs/refs-internal.h int rename_ref_available(const char *oldname, const char *newname); · line 332

Quoted 6 lines
refs/refs-internal.h +6 -0
+int ref_iterator_advance(struct ref_iterator *ref_iterator);
+
+/*
+ * An iterator over nothing (its first ref_iterator_advance() call
+ * returns 0).
+ */

s/0/ITER_DONE/

refs/refs-internal.h int rename_ref_available(const char *oldname, const char *newname); · line 338

Quoted 6 lines
refs/refs-internal.h +6 -0
+struct ref_iterator *empty_ref_iterator_begin(void);
+
+/*
+ * Return true iff ref_iterator is an empty_ref_iterator.
+ */
+int is_empty_ref_iterator(struct ref_iterator *ref_iterator);

I can see that you used this function as an optimization or convenience in overlay_ref_iterator_begin(), but do you expect it to be generally useful otherwise? Is it worth publishing? Do you have other use-cases in mind?

Also, can you explain why the merge iterator doesn't also perform the optimization/convenience of checking if one iterator is an empty iterator?

refs/refs-internal.h int rename_ref_available(const char *oldname, const char *newname); · line 269

Quoted 2 lines
refs/refs-internal.h +2 -0
+/*
+ * Iterate over the intries from iter0 and iter1, with the values

s/intries/entries/

Quoted 11 lines
refs/refs-internal.h +11 -0
+ * interleaved as directed by the select function. The iterator takes
+ * ownership of iter0 and iter1 and frees them when the iteration is
+ * over.
+ */
+struct ref_iterator *merge_ref_iterator_begin(
+               struct ref_iterator *iter0, struct ref_iterator *iter1,
+               ref_iterator_select_fn *select, void *cb_data);
+
+/*
+ * An iterator consisting of the union of the entries from iter0 and
+ * iter1. If there are entries common to the two sub-iterators, use

refs/refs-internal.h int rename_ref_available(const char *oldname, const char *newname); · line 378

Quoted 10 lines
refs/refs-internal.h +10 -0
+ * the one from iter1. Each iterator must iterate over its entries in
+ * strcmp() order by refname for this to work.
+ *
+ * The new iterator takes ownership of its arguments and frees them
+ * when the iteration is over. As a convenience to callers, if iter0
+ * or iter1 is_empty_ref_iterator(), then abort that one immediately
+ * and return the other iterator directly, without wrapping it.
+ */
+struct ref_iterator *overlay_ref_iterator_begin(struct ref_iterator *iter0,
+                                               struct ref_iterator *iter1);

When reading about the overlay iterator (both code and documentation), my expectation was that iter0 would shadow iter1, not the other way around as implemented here. Of course, that's entirely subjective, but the generic names don't provide any useful clues as to which shadows which. Perhaps giving them more meaningful names would help.

Quoted 5 lines
refs/refs-internal.h +5 -0
+/*
+ * Wrap iter0, only letting through the references whose names start
+ * with prefix. If trim is set, set iter->refname to the name of the
+ * reference with that many characters trimmed off the front;
+ * otherwise set it to the full refname. The new iterator takes over

refs/refs-internal.h int rename_ref_available(const char *oldname, const char *newname); · line 394

Quoted 10 lines
refs/refs-internal.h +10 -0
+ * ownership of iter0 and frees it when iteration is over. It makes
+ * its own copy of prefix.
+ *
+ * As an convenience to callers, if prefix is the empty string and
+ * trim is zero, this function returns iter0 directly, without
+ * wrapping it.
+ */
+struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
+                                              const char *prefix,
+                                              int trim);

Minor: Similarly, when reading the code and documentation, I wondered why this was named 'iter0' when no 'iter1' was in sight. Perhaps name it simply 'iter'.

#14 lore raw

Re: [PATCH 09/13] refs: introduce an iterator interface

Quoted 10 lines · Eric Sunshine
Quoted 1 line · Michael Haggerty

[...]

[...]
Either:

s/false/something other than ITER_OK/

or:

s/false/ITER_DONE or ITER_ERROR/

Thanks.

Quoted 8 lines

refs/refs-internal.h int rename_ref_available(const char *oldname, const char *newname); · line 332

Quoted 6 lines
refs/refs-internal.h +6 -0
+int ref_iterator_advance(struct ref_iterator *ref_iterator);
+
+/*
+ * An iterator over nothing (its first ref_iterator_advance() call
+ * returns 0).
+ */

s/0/ITER_DONE/

Thanks. I guess you can guess what an earlier draft of this interface looked like :-)

Quoted 11 lines

refs/refs-internal.h int rename_ref_available(const char *oldname, const char *newname); · line 338

Quoted 6 lines
refs/refs-internal.h +6 -0
+struct ref_iterator *empty_ref_iterator_begin(void);
+
+/*
+ * Return true iff ref_iterator is an empty_ref_iterator.
+ */
+int is_empty_ref_iterator(struct ref_iterator *ref_iterator);

I can see that you used this function as an optimization or convenience in overlay_ref_iterator_begin(), but do you expect it to be generally useful otherwise? Is it worth publishing? Do you have other use-cases in mind?

It is only "published" within the refs module, in refs/refs-internal.h. This header file is not meant to be used by code outside of the refs module.

My thinking was that it might be useful to other reference backends. The function is pretty safe for anybody to call, though I admit that it is not very general.

I don't have a strong feeling either way. If nobody else chimes in, I'll remove it from the header file as you suggested. We can always add it back if somebody needs it.

Quoted 3 lines

Also, can you explain why the merge iterator doesn't also perform the optimization/convenience of checking if one iterator is an empty iterator?

That's because the merge iterator doesn't know what its select function will do. For example, you could imagine an "intersect" select function that only lets through references that were in *both* sub-iterators. In that case, your suggested "optimization" would be incorrect.

Incidentally, that's also why I decided to leave the select function in charge even after one or both of the sub-iterators is exhausted—because it lets merge_ref_iterator implement more diverse behavior.

Quoted 4 lines
Quoted 2 lines

+/*
+ * Iterate over the intries from iter0 and iter1, with the values

s/intries/entries/

Thanks.

Quoted 19 lines
Quoted 19 lines

+ * interleaved as directed by the select function. The iterator takes
+ * ownership of iter0 and iter1 and frees them when the iteration is
+ * over.
+ */

refs/refs-internal.h +15 -0
+struct ref_iterator *merge_ref_iterator_begin(
+               struct ref_iterator *iter0, struct ref_iterator *iter1,
+               ref_iterator_select_fn *select, void *cb_data);
+
+/*
+ * An iterator consisting of the union of the entries from iter0 and
+ * iter1. If there are entries common to the two sub-iterators, use
+ * the one from iter1. Each iterator must iterate over its entries in
+ * strcmp() order by refname for this to work.
+ *
+ * The new iterator takes ownership of its arguments and frees them
+ * when the iteration is over. As a convenience to callers, if iter0
+ * or iter1 is_empty_ref_iterator(), then abort that one immediately
+ * and return the other iterator directly, without wrapping it.
+ */
Quoted 8 lines

refs/refs-internal.h int rename_ref_available(const char *oldname, const char *newname); · line 378

Quoted 2 lines
refs/refs-internal.h +2 -0
+struct ref_iterator *overlay_ref_iterator_begin(struct ref_iterator *iter0,
+                                               struct ref_iterator *iter1);

When reading about the overlay iterator (both code and documentation), my expectation was that iter0 would shadow iter1, not the other way around as implemented here. Of course, that's entirely subjective, but the generic names don't provide any useful clues as to which shadows which. Perhaps giving them more meaningful names would help.

That's a good idea. I also found myself having to refer back to the documentation to remind myself which was which.

How about I rename them "back" and "front"? I will also reverse the order of the arguments.

(But I will leave the names "iter0" and "iter1" in merge_ref_iterator, and also the constants like ITER_SELECT_0, because these don't necessarily have the interpretation of "back" and "front".)

Quoted 11 lines
Quoted 11 lines

+/*
+ * Wrap iter0, only letting through the references whose names start
+ * with prefix. If trim is set, set iter->refname to the name of the
+ * reference with that many characters trimmed off the front;
+ * otherwise set it to the full refname. The new iterator takes over
+ * ownership of iter0 and frees it when iteration is over. It makes
+ * its own copy of prefix.
+ *
+ * As an convenience to callers, if prefix is the empty string and
+ * trim is zero, this function returns iter0 directly, without
+ * wrapping it.

Quoted 8 lines

refs/refs-internal.h int rename_ref_available(const char *oldname, const char *newname); · line 394

Quoted 4 lines

+ */

refs/refs-internal.h +3 -0
+struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
+                                              const char *prefix,
+                                              int trim);

Minor: Similarly, when reading the code and documentation, I wondered why this was named 'iter0' when no 'iter1' was in sight. Perhaps name it simply 'iter'.

I found that it got a little bit confusing, because the constructor and method implementations all use iter as a local variable. In particular in the constructor there would want to be an argument "iter" and also the local variable "iter" for the iterator being constructed, so a new name would otherwise have to be invented for one or the other. Between all the "iter" and "iter" and "iter->iter", I found that naming the sub-iterator "iter0" made things a little bit less bewildering.

If you don't like that, we could name the embedded iterators something like "subiter", "subiter0", and "subiter1". But the current convention is a bit more succinct so I slightly prefer it.

Thanks for all your comments!

Michael

#15 lore raw

Re: [PATCH 09/13] refs: introduce an iterator interface

Quoted 14 lines · Michael Haggerty
Quoted 12 lines · Eric Sunshine

refs/refs-internal.h int rename_ref_available(const char *oldname, const char *newname); · line 338

Quoted 6 lines · Michael Haggerty
refs/refs-internal.h +6 -0
+struct ref_iterator *empty_ref_iterator_begin(void);
+
+/*
+ * Return true iff ref_iterator is an empty_ref_iterator.
+ */
+int is_empty_ref_iterator(struct ref_iterator *ref_iterator);

I can see that you used this function as an optimization or convenience in overlay_ref_iterator_begin(), but do you expect it to be generally useful otherwise? Is it worth publishing? Do you have other use-cases in mind?

Quoted 2 lines

It is only "published" within the refs module, in refs/refs-internal.h. This header file is not meant to be used by code outside of the refs module.

Ah, I forgot about that. In that case, it's probably less of an issue.

Quoted 7 lines

My thinking was that it might be useful to other reference backends. The function is pretty safe for anybody to call, though I admit that it is not very general.

I don't have a strong feeling either way. If nobody else chimes in, I'll remove it from the header file as you suggested. We can always add it back if somebody needs it.

I don't feel strongly about it either.

Quoted 8 lines
Quoted 3 lines

Also, can you explain why the merge iterator doesn't also perform the optimization/convenience of checking if one iterator is an empty iterator?

That's because the merge iterator doesn't know what its select function will do. For example, you could imagine an "intersect" select function that only lets through references that were in *both* sub-iterators. In that case, your suggested "optimization" would be incorrect.

Makes sense. Thanks for explaining. I wonder if this deserves a comment somewhere in code or commit message to make the situation clear to a future developer who might think it a good idea to promote the "optimization" to the merge iterator.

Quoted 20 lines
Quoted 11 lines
Quoted 11 lines

+/*
+ * An iterator consisting of the union of the entries from iter0 and
+ * iter1. If there are entries common to the two sub-iterators, use
+ * the one from iter1. Each iterator must iterate over its entries in
+ * strcmp() order by refname for this to work.
+ *
+ * The new iterator takes ownership of its arguments and frees them
+ * when the iteration is over. As a convenience to callers, if iter0
+ * or iter1 is_empty_ref_iterator(), then abort that one immediately
+ * and return the other iterator directly, without wrapping it.
+ */

Quoted 8 lines

refs/refs-internal.h int rename_ref_available(const char *oldname, const char *newname); · line 378

Quoted 2 lines
refs/refs-internal.h +2 -0
+struct ref_iterator *overlay_ref_iterator_begin(struct ref_iterator *iter0,
+                                               struct ref_iterator *iter1);

When reading about the overlay iterator (both code and documentation), my expectation was that iter0 would shadow iter1, not the other way around as implemented here. Of course, that's entirely subjective, but the generic names don't provide any useful clues as to which shadows which. Perhaps giving them more meaningful names would help.

Quoted 5 lines

That's a good idea. I also found myself having to refer back to the documentation to remind myself which was which.

How about I rename them "back" and "front"? I will also reverse the order of the arguments.

I had a hard time coming up with better names, which is why I didn't suggest any in my review. The best I had was "shadower" and "shadowee", but they are far too similar (and long) for my tastes. "back" and "front" feel a bit off, but are better than anything I thought of.

As for the argument order, I can't explain why my expectation was that it would be the other way around, so I certainly don't insist that they be swapped.

Quoted 3 lines

(But I will leave the names "iter0" and "iter1" in merge_ref_iterator, and also the constants like ITER_SELECT_0, because these don't necessarily have the interpretation of "back" and "front".)

Yes, those names are fine in the merge iterator.

Quoted 28 lines
Quoted 11 lines
Quoted 11 lines

+/*
+ * Wrap iter0, only letting through the references whose names start
+ * with prefix. If trim is set, set iter->refname to the name of the
+ * reference with that many characters trimmed off the front;
+ * otherwise set it to the full refname. The new iterator takes over
+ * ownership of iter0 and frees it when iteration is over. It makes
+ * its own copy of prefix.
+ *
+ * As an convenience to callers, if prefix is the empty string and
+ * trim is zero, this function returns iter0 directly, without
+ * wrapping it.

Quoted 8 lines

refs/refs-internal.h int rename_ref_available(const char *oldname, const char *newname); · line 394

Quoted 4 lines

+ */

refs/refs-internal.h +3 -0
+struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
+                                              const char *prefix,
+                                              int trim);

Minor: Similarly, when reading the code and documentation, I wondered why this was named 'iter0' when no 'iter1' was in sight. Perhaps name it simply 'iter'.

I found that it got a little bit confusing, because the constructor and method implementations all use iter as a local variable. In particular in the constructor there would want to be an argument "iter" and also the local variable "iter" for the iterator being constructed, so a new name would otherwise have to be invented for one or the other. Between all the "iter" and "iter" and "iter->iter", I found that naming the sub-iterator "iter0" made things a little bit less bewildering.

Quoted 3 lines

If you don't like that, we could name the embedded iterators something like "subiter", "subiter0", and "subiter1". But the current convention is a bit more succinct so I slightly prefer it.

I'd probably have called the sub-iterator "child" (which is the same length as "iter0") or "wrapped". If you're not interested changing the name in the code, perhaps in the header alone you could call it simply "iter" or omit the name altogether, but this a very minor issue and probably not worth much time or effort to address.

#16 lore raw

Re: [PATCH 09/13] refs: introduce an iterator interface

Quoted 24 lines · Eric Sunshine
Quoted 14 lines · Michael Haggerty
Quoted 12 lines · Eric Sunshine

refs/refs-internal.h int rename_ref_available(const char *oldname, const char *newname); · line 338

Quoted 6 lines · Michael Haggerty
refs/refs-internal.h +6 -0
+struct ref_iterator *empty_ref_iterator_begin(void);
+
+/*
+ * Return true iff ref_iterator is an empty_ref_iterator.
+ */
+int is_empty_ref_iterator(struct ref_iterator *ref_iterator);

I can see that you used this function as an optimization or convenience in overlay_ref_iterator_begin(), but do you expect it to be generally useful otherwise? Is it worth publishing? Do you have other use-cases in mind?

Quoted 2 lines

It is only "published" within the refs module, in refs/refs-internal.h. This header file is not meant to be used by code outside of the refs module.

Ah, I forgot about that. In that case, it's probably less of an issue.

Quoted 4 lines

My thinking was that it might be useful to other reference backends. The function is pretty safe for anybody to call, though I admit that it is not very general.

Quoted 5 lines
Quoted 3 lines

I don't have a strong feeling either way. If nobody else chimes in, I'll remove it from the header file as you suggested. We can always add it back if somebody needs it.

I don't feel strongly about it either.

OK then, I'll leave it as-is.

Quoted 9 lines
Quoted 8 lines
Quoted 3 lines

Also, can you explain why the merge iterator doesn't also perform the optimization/convenience of checking if one iterator is an empty iterator?

That's because the merge iterator doesn't know what its select function will do. For example, you could imagine an "intersect" select function that only lets through references that were in *both* sub-iterators. In that case, your suggested "optimization" would be incorrect.

Quoted 4 lines

Makes sense. Thanks for explaining. I wonder if this deserves a comment somewhere in code or commit message to make the situation clear to a future developer who might think it a good idea to promote the "optimization" to the merge iterator.

Good idea. I'll add a comment.

Quoted 1 line

[...]

Thanks,
Michael

#17 lore raw

Re: [PATCH 09/13] refs: introduce an iterator interface

In reply to Michael Haggerty
Quoted 8 lines · Michael Haggerty

This commit introduces a new iteration primitive for references: a ref_iterator. A ref_iterator is a polymorphic object that a reference storage backend can be asked to instantiate. There are three functions that can be applied to a ref_iterator:

* ref_iterator_advance(): move to the next reference in the iteration
* ref_iterator_abort(): end the iteration before it is exhausted
* ref_iterator_peel(): peel the reference currently being looked at

This part looked somewhat strange in that it makes "peel" sound something very special. Even though I understand why, it made me feel uneasy. I do not think of another operation like peel that may want to have such a specialized helper, so I'll let it pass, but the primary uneasiness I felt comes from the fact that "iterator-peel" is not an essential service of the API that needs for correctness, but is a pure optimization (i.e. you could grab a ref from the normal iterator call, and then ask "please peel this ref" to the usual ref API that does not know anything about iteration).

Quoted 4 lines

Iterating using a ref_iterator leaves the flow of control in the hands of the caller, which means that ref_iterators from multiple sources (e.g., loose and packed refs) can be composed and presented to the world as a single compound ref_iterator.

Yes, this is a very good move. I am happy to see us going in this direction.

#18 lore raw

Re: [PATCH 09/13] refs: introduce an iterator interface

Quoted 11 lines · Junio C Hamano
Quoted 8 lines · Michael Haggerty

This commit introduces a new iteration primitive for references: a ref_iterator. A ref_iterator is a polymorphic object that a reference storage backend can be asked to instantiate. There are three functions that can be applied to a ref_iterator:

* ref_iterator_advance(): move to the next reference in the iteration
* ref_iterator_abort(): end the iteration before it is exhausted
* ref_iterator_peel(): peel the reference currently being looked at

Quoted 9 lines

This part looked somewhat strange in that it makes "peel" sound something very special. Even though I understand why, it made me feel uneasy. I do not think of another operation like peel that may want to have such a specialized helper, so I'll let it pass, but the primary uneasiness I felt comes from the fact that "iterator-peel" is not an essential service of the API that needs for correctness, but is a pure optimization (i.e. you could grab a ref from the normal iterator call, and then ask "please peel this ref" to the usual ref API that does not know anything about iteration).

I agree that this is inelegant. The underlying reason is that the iterator embodies both the iteration and also the reference currently being iterated over. So ref_iterator_advance() and ref_iterator_abort() are really iterator methods, whereas ref_iterator_peel() is really a method on the current reference. For that matter, the refname etc fields are conceptually parts of the current reference, too, not of the iteration.

One of my many coding spikes tried to separate the two concepts by having the iterator give back a separate object representing the reference. That object would have had a peel() method. But I wasn't happy with that approach:

* Anything that made it look like the reference object had a lifetime independent of that of the iterator seemed like asking for trouble.

* The peel() method would probably have to be a real method that knows where the reference came from (as opposed to a simple function) because of things like prefix_ref_iterator, where the refname stored in the ref_iterator might have had its prefix stripped off.

The complexity level was getting beyond what I felt was tenable in C, so I made the compromise that you see.

If I had more time I'd do some benchmarking to see whether the "peeling" optimization is really still needed. It comes from the days when references were stored in giant lists. Now that we store refs in tries, and each level gets sorted as part of the iteration anyway, the lookup might be fast enough that nobody would care.

Quoted 1 line

[...]

Michael

#19 lore raw

Re: [PATCH 09/13] refs: introduce an iterator interface

In reply to Michael Haggerty
Quoted 11 lines · Michael Haggerty

Currently, the API for iterating over references is via a family of for_each_ref()-type functions that invoke a callback function for each selected reference. All of these eventually call do_for_each_ref(), which knows how to do one thing: iterate in parallel through two ref_caches, one for loose and one for packed refs, giving loose references precedence over packed refs. This is rather complicated code, and is quite specialized to the files backend. It also requires callers to encapsulate their work into a callback function, which often means that they have to define and use a "cb_data" struct to manage their context.

Quoted 8 lines

The current design is already bursting at the seams, and will become even more awkward in the upcoming world of multiple reference storage backends:

* Per-worktree vs. shared references are currently handled via a kludge in git_path() rather than iterating over each part of the reference namespace separately and merging the results. This kludge will cease to work when we have multiple reference storage backends.

Question from a refs user. Right now worktree.c:get_worktrees() peeks directly to "$GIT_DIR/worktrees/xxx/HEAD" and parses the content itself, something that I promised to fix but never got around to do it. Will we have an iterator to go through all worktrees' HEAD, or will there be an API to say "resolve ref HEAD from worktree XYZ"?

Signature · 1 line
Duy
#20 lore raw

Re: [PATCH 09/13] refs: introduce an iterator interface

Quoted 21 lines · Duy Nguyen
Quoted 11 lines · Michael Haggerty

Currently, the API for iterating over references is via a family of for_each_ref()-type functions that invoke a callback function for each selected reference. All of these eventually call do_for_each_ref(), which knows how to do one thing: iterate in parallel through two ref_caches, one for loose and one for packed refs, giving loose references precedence over packed refs. This is rather complicated code, and is quite specialized to the files backend. It also requires callers to encapsulate their work into a callback function, which often means that they have to define and use a "cb_data" struct to manage their context.

Quoted 8 lines

The current design is already bursting at the seams, and will become even more awkward in the upcoming world of multiple reference storage backends:

* Per-worktree vs. shared references are currently handled via a kludge in git_path() rather than iterating over each part of the reference namespace separately and merging the results. This kludge will cease to work when we have multiple reference storage backends.

Quoted 5 lines

Question from a refs user. Right now worktree.c:get_worktrees() peeks directly to "$GIT_DIR/worktrees/xxx/HEAD" and parses the content itself, something that I promised to fix but never got around to do it. Will we have an iterator to go through all worktrees' HEAD, or will there be an API to say "resolve ref HEAD from worktree XYZ"?

My preference is that there is a way to say "create a ref_store object representing the loose references stored physically under "$GIT_DIR/worktrees/xxx". Then that ref_store could be asked to read its HEAD (or iterate over all of the refs under that path or whatever). You could even write HEAD through the same ref_store.

Michael

#21 lore raw

PATCH 10/13 do_for_each_ref(): reimplement using reference iteration

In reply to Michael Haggerty

Use the reference iterator interface to implement do_for_each_ref(). Delete a bunch of code supporting the old for_each_ref() implementation. And now that do_for_each_ref() is generic code (it is no longer tied to the files backend), move it to refs.c.

The implementation is via a new function, do_for_each_ref_iterator(), which takes a reference iterator as argument and calls a callback function for each of the references in the iterator.

This change requires the current_ref performance hack for peel_ref() to be implemented via ref_iterator_peel() rather than peel_entry() because we don't have a ref_entry handy (it is hidden under three layers:
file_ref_iterator, merge_ref_iterator, and cache_ref_iterator). So:

* do_for_each_ref_iterator() records the active iterator in current_ref_iter while it is running.

* peel_ref() checks whether current_ref_iter is pointing at the requested reference. If so, it asks the iterator to peel the reference (which it can do efficiently via its "peel" virtual function). For extra safety, we do the optimization only if the refname *addresses* are the same, not only if the refname *strings* are the same, to forestall possible mixups between refnames that come from different ref_iterators.

Please note that this optimization of peel_ref() is only available when iterating via do_for_each_ref_iterator() (including all of the for_each_ref() functions, which call it indirectly). It would be complicated to implement a similar optimization when iterating directly using a reference iterator, because multiple reference iterators can be in use at the same time, with interleaved calls to ref_iterator_advance(). (In fact we do exactly that in merge_ref_iterator.)

But that is not necessary. peel_ref() is only called while iterating over references. Callers who iterate using the for_each_ref() functions benefit from the optimization described above. Callers who iterate using reference iterators directly have access to the ref_iterator, so they can call ref_iterator_peel() themselves to get an analogous optimization in a more straightforward manner.

If we rewrite all callers to use the reference iteration API, then we can remove the current_ref_iter hack permanently.

---
 refs.c               |  20 +++++
 refs/files-backend.c | 206 ++-------------------------------------------------
 refs/iterator.c      |  29 ++++++++
 refs/refs-internal.h |  33 ++++++---
 4 files changed, 76 insertions(+), 212 deletions(-)
refs.c +20 -0
index 842c5c7..814cad3 100644
@@ -1120,6 +1120,26 @@ int head_ref(each_ref_fn fn, void *cb_data)
 	return head_ref_submodule(NULL, fn, cb_data);
 }
 
+/*
+ * 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_ref(const char *submodule, const char *prefix,
+			   each_ref_fn fn, int trim, int flags, void *cb_data)
+{
+	struct ref_iterator *iter;
+
+	iter = files_ref_iterator_begin(submodule, prefix, flags);
+	iter = prefix_ref_iterator_begin(iter, prefix, trim);
+
+	return do_for_each_ref_iterator(iter, fn, cb_data);
+}
+
 int for_each_ref(each_ref_fn fn, void *cb_data)
 {
 	return do_for_each_ref(NULL, "", fn, 0, 0, cb_data);
refs/files-backend.c +5 -201

233 lines not on this page · raw message

refs/iterator.c +29 -0
index 82a6948..c1670ab 100644
@@ -345,3 +345,32 @@ struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
 
 	return ref_iterator;
 }
+
+struct ref_iterator *current_ref_iter = NULL;
+
+int do_for_each_ref_iterator(struct ref_iterator *iter,
+			     each_ref_fn fn, void *cb_data)
+{
+	int retval = 0, ok;
+	struct ref_iterator *old_ref_iter = current_ref_iter;
+
+	current_ref_iter = iter;
+	while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
+		retval = fn(iter->refname, iter->oid, iter->flags, cb_data);
+		if (retval) {
+			/*
+			 * If ref_iterator_abort() returns ITER_ERROR,
+			 * we ignore that error in deference to the
+			 * callback function's return value.
+			 */
+			ref_iterator_abort(iter);
+			goto out;
+		}
+	}
+
+out:
+	current_ref_iter = old_ref_iter;
+	if (ok == ITER_ERROR)
+		return -1;
+	return retval;
+}
refs/refs-internal.h +22 -11
index 11182b4..f16a38e 100644
@@ -442,18 +442,29 @@ struct ref_iterator_vtable {
 };
 
 /*
- * 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.
- *
- * This is the common backend for the for_each_*ref* functions.
+ * current_ref_iter is a performance hack: when iterating over
+ * references using the for_each_ref*() functions, current_ref_iter is
+ * set to the reference iterator before calling the callback function.
+ * If the callback function calls peel_ref(), then peel_ref() first
+ * checks whether the reference to be peeled is the one referred to by
+ * the iterator (it usually is) and if so, asks the iterator for the
+ * peeled version of the reference if it is available. This avoids a
+ * refname lookup in a common case. current_ref_iter is set to NULL
+ * when the iteration is over.
  */
-int do_for_each_ref(const char *submodule, const char *prefix,
-		    each_ref_fn fn, int trim, int flags, void *cb_data);
+extern struct ref_iterator *current_ref_iter;
+
+/*
+ * The common backend for the for_each_*ref* functions. Call fn for
+ * each reference in iter. If the iterator itself ever returns
+ * ITER_ERROR, return -1. If fn ever returns a non-zero value, stop
+ * the iteration and return that value. Otherwise, return 0. In any
+ * case, free the iterator when done. This function is basically an
+ * adapter between the callback style of reference iteration and the
+ * iterator style.
+ */
+int do_for_each_ref_iterator(struct ref_iterator *iter,
+			     each_ref_fn fn, void *cb_data);
 
 /*
  * Read the specified reference from the filesystem or packed refs
Signature · 1 line
2.8.1
#22 lore raw

PATCH 11/13 for_each_reflog(): don't abort for bad references

In reply to Michael Haggerty

If there is a file under "$GIT_DIR/logs" with no corresponding reference, the old code was emitting an error message, aborting the reflog iteration, and returning -1. But

* None of the callers was checking the exit value

* The callers all want to find all legitimate reflogs (sometimes for the purpose of determining object reachability!) and wouldn't benefit from a truncated iteration anyway.

So instead, emit an error message and skip the "broken" reflog, but continue with the iteration.

---
 refs/files-backend.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
refs/files-backend.c +1 -1
index c48a006..a7cc0e2 100644
@@ -3325,7 +3325,7 @@ static int do_for_each_reflog(struct strbuf *name, each_ref_fn fn, void *cb_data
 				struct object_id oid;
 
 				if (read_ref_full(name->buf, 0, oid.hash, NULL))
-					retval = error("bad ref for %s", name->buf);
+					error("bad ref for %s", name->buf);
 				else
 					retval = fn(name->buf, &oid, 0, cb_data);
 			}
Signature · 1 line
2.8.1
#23 lore raw

PATCH 12/13 dir_iterator: new API for iterating over a directory tree

In reply to Michael Haggerty

The iterator interface is modeled on that for references, though no vtable is necessary because there is (so far?) only one type of dir_iterator.

There are obviously a lot of features that could easily be added to this class:

* Skip/include directory paths in the iteration
* Shallow/deep iteration
* Letting the caller decide which subdirectories to recurse into (e.g., via a dir_iterator_advance_into() function)
* Option to iterate in sorted order
* Option to iterate over directory paths before vs. after their contents

But these are not needed for the current patch series, so I refrain.

---
 Makefile       |   1 +
 dir-iterator.c | 180 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 dir-iterator.h |  86 +++++++++++++++++++++++++++
 3 files changed, 267 insertions(+)
 create mode 100644 dir-iterator.c
 create mode 100644 dir-iterator.h
Makefile +1 -0
index ac8f365..b4ffc11 100644
@@ -722,6 +722,7 @@ LIB_OBJS += diff-lib.o
 LIB_OBJS += diff-no-index.o
 LIB_OBJS += diff.o
 LIB_OBJS += dir.o
+LIB_OBJS += dir-iterator.o
 LIB_OBJS += editor.o
 LIB_OBJS += entry.o
 LIB_OBJS += environment.o
dir-iterator.c new file +180 -0

180 lines not on this page · raw message

dir-iterator.h new file +86 -0

86 lines not on this page · raw message

Signature · 1 line
2.8.1
#24 lore raw

Re: [PATCH 12/13] dir_iterator: new API for iterating over a directory tree

dir-iterator.c line 12

Quoted 7 lines · Michael Haggerty
dir-iterator.c new file +7 -0
+struct dir_iterator_level {
+	int initialized;
+
+	DIR *dir;
+
+	/*
+	 * The length of the directory part of refname at this level

"refname"? Isn't this was for directories?

Review

Quoted 1 line
(quoted diff) +1 -0
+			if (lstat(iter->base.path.buf, &iter

->base.st) < 0)

dir-iterator.c line 127

Quoted 1 line
dir-iterator.c new file +1 -0
+				continue; /* silently skip */

Should we warn on non-ENOENT errors here?

Quoted 3 lines

+/*
+ * End the iteration before it has been exhausted. Free the reference

s/reference/directory/

#25 lore raw

Re: [PATCH 12/13] dir_iterator: new API for iterating over a directory tree

Quoted 11 lines · David Turner

dir-iterator.c line 12

Quoted 7 lines · Michael Haggerty
dir-iterator.c new file +7 -0
+struct dir_iterator_level {
+	int initialized;
+
+	DIR *dir;
+
+	/*
+	 * The length of the directory part of refname at this level

"refname"? Isn't this was for directories?

Thanks.

Quoted 5 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+			if (lstat(iter->base.path.buf, &iter

->base.st) < 0)

dir-iterator.c line 127

Quoted 1 line
dir-iterator.c new file +1 -0
+				continue; /* silently skip */

Should we warn on non-ENOENT errors here?

Good idea. I'll add that.

Quoted 5 lines
Quoted 3 lines

+/*
+ * End the iteration before it has been exhausted. Free the reference

s/reference/directory/

Thanks. Will fix.

Michael

#26 lore raw

PATCH 13/13 for_each_reflog(): reimplement using iterators

In reply to Michael Haggerty

Allow references with reflogs to be iterated over using a ref_iterator. The latter is implemented as a files_reflog_iterator, which in turn uses dir_iterator to read the "logs" directory.

Note that reflog iteration doesn't correctly handle per-worktree reflogs (either before or after this patch).

---
 refs/files-backend.c | 113 ++++++++++++++++++++++++++++++++-------------------
 refs/refs-internal.h |   7 ++++
 2 files changed, 78 insertions(+), 42 deletions(-)
refs/files-backend.c +71 -42
index a7cc0e2..2e0d006 100644
@@ -2,6 +2,7 @@
 #include "../refs.h"
 #include "refs-internal.h"
 #include "../iterator.h"
+#include "../dir-iterator.h"
 #include "../lockfile.h"
 #include "../object.h"
 #include "../dir.h"
@@ -3292,60 +3293,88 @@ int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_dat
 	strbuf_release(&sb);
 	return ret;
 }
-/*
- * Call fn for each reflog in the namespace indicated by name.  name
- * must be empty or end with '/'.  Name will be used as a scratch
- * space, but its contents will be restored before return.
- */
-static int do_for_each_reflog(struct strbuf *name, each_ref_fn fn, void *cb_data)
+
+struct files_reflog_iterator {
+	struct ref_iterator base;
+
+	struct dir_iterator *dir_iterator;
+	struct object_id oid;
+};
+
+static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
 {
-	DIR *d = opendir(git_path("logs/%s", name->buf));
-	int retval = 0;
-	struct dirent *de;
-	int oldlen = name->len;
+	struct files_reflog_iterator *iter =
+		(struct files_reflog_iterator *)ref_iterator;
+	struct dir_iterator *diter = iter->dir_iterator;
+	int ok;
 
-	if (!d)
-		return name->len ? errno : 0;
+	while ((ok = dir_iterator_advance(diter)) == ITER_OK) {
+		int flags;
 
-	while ((de = readdir(d)) != NULL) {
-		struct stat st;
-
-		if (de->d_name[0] == '.')
+		if (!S_ISREG(diter->st.st_mode))
+			continue;
+		if (diter->basename[0] == '.')
 			continue;
-		if (ends_with(de->d_name, ".lock"))
+		if (ends_with(diter->basename, ".lock"))
 			continue;
-		strbuf_addstr(name, de->d_name);
-		if (stat(git_path("logs/%s", name->buf), &st) < 0) {
-			; /* silently ignore */
-		} else {
-			if (S_ISDIR(st.st_mode)) {
-				strbuf_addch(name, '/');
-				retval = do_for_each_reflog(name, fn, cb_data);
-			} else {
-				struct object_id oid;
 
-				if (read_ref_full(name->buf, 0, oid.hash, NULL))
-					error("bad ref for %s", name->buf);
-				else
-					retval = fn(name->buf, &oid, 0, cb_data);
-			}
-			if (retval)
-				break;
+		if (read_ref_full(diter->relative_path, 0,
+				  iter->oid.hash, &flags)) {
+			error("bad ref for %s", diter->path.buf);
+			continue;
 		}
-		strbuf_setlen(name, oldlen);
+
+		iter->base.refname = diter->relative_path;
+		iter->base.oid = &iter->oid;
+		iter->base.flags = flags;
+		return ITER_OK;
 	}
-	closedir(d);
-	return retval;
+
+	iter->dir_iterator = NULL;
+	if (ref_iterator_abort(ref_iterator) == ITER_ERROR)
+		ok = ITER_ERROR;
+	return ok;
+}
+
+static int files_reflog_iterator_peel(struct ref_iterator *ref_iterator,
+				   struct object_id *peeled)
+{
+	die("BUG: ref_iterator_peel() called for reflog_iterator");
+}
+
+static int files_reflog_iterator_abort(struct ref_iterator *ref_iterator)
+{
+	struct files_reflog_iterator *iter =
+		(struct files_reflog_iterator *)ref_iterator;
+	int ok = ITER_DONE;
+
+	if (iter->dir_iterator)
+		ok = dir_iterator_abort(iter->dir_iterator);
+
+	base_ref_iterator_free(ref_iterator);
+	return ok;
+}
+
+struct ref_iterator_vtable files_reflog_iterator_vtable = {
+	files_reflog_iterator_advance,
+	files_reflog_iterator_peel,
+	files_reflog_iterator_abort
+};
+
+struct ref_iterator *files_reflog_iterator_begin(void)
+{
+	struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));
+	struct ref_iterator *ref_iterator = &iter->base;
+
+	base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
+	iter->dir_iterator = dir_iterator_begin(git_path("logs"));
+	return ref_iterator;
 }
 
 int for_each_reflog(each_ref_fn fn, void *cb_data)
 {
-	int retval;
-	struct strbuf name;
-	strbuf_init(&name, PATH_MAX);
-	retval = do_for_each_reflog(&name, fn, cb_data);
-	strbuf_release(&name);
-	return retval;
+	return do_for_each_ref_iterator(files_reflog_iterator_begin(),
+					fn, cb_data);
 }
 
 static int ref_update_reject_duplicates(struct string_list *refnames,
refs/refs-internal.h +7 -0
index f16a38e..87ad8b1 100644
@@ -403,6 +403,13 @@ struct ref_iterator *files_ref_iterator_begin(const char *submodule,
 					      const char *prefix,
 					      unsigned int flags);
 
+/*
+ * Iterate over the references in the main ref_store that have a
+ * reflog. The paths within a directory are iterated over in arbitrary
+ * order.
+ */
+struct ref_iterator *files_reflog_iterator_begin(void);
+
 /* Internal implementation of reference iteration: */
 
 /*
Signature · 1 line
2.8.1