
The previous change implemented the ahead_behind() method, including an algorithm to compute the ahead/behind values for a number of commit tips relative to a number of commit bases. Now, integrate that algorithm as part of 'git for-each-ref' hidden behind a new format atom, ahead-behind. This naturally extends to 'git branch' and 'git tag' builtins, as well. This format allows specifying multiple bases, if so desired, and all matching references are compared against all of those bases. For this reason, failing to read a reference provided from these atoms results in an error. In order to translate the ahead_behind() method information to the format output code in ref-filter.c, we must populate arrays of ahead_behind_count structs. In struct ref_array, we store the full array that will be passed to ahead_behind(). In struct ref_array_item, we store an array of pointers that point to the relvant items within the full array. In this way, we can pull all relevant ahead/behind values directly when formatting output for a specific item. It also ensures the lifetime of the ahead_behind_count structs matches the time that the array is being used. Add specific tests of the ahead/behind counts in t6600-test-reach.sh, as it has an interesting repository shape. In particular, its merging strategy and its use of different commit-graphs would demonstrate over- counting if the ahead_behind() method did not already account for that possibility. Also add tests for the specific for-each-ref, branch, and tag builtins. In the case of 'git tag', there are intersting cases that happen when some of the selected tips are not commits. This requires careful logic around commits_nr in the second loop of filter_ahead_behind(). Also, the test in t7004 is carefully located to avoid being dependent on the GPG prereq. It also avoids using the test_commit helper, as that will add ticks to the time and disrupt the expected timestamps in later tag tests. Also add performance tests in a new p1300-graph-walks.sh script. This will be useful for more uses in the future, but for now compare the ahead-behind counting algorithm in 'git for-each-ref' to the naive implementation by running 'git rev-list --count' processes for each input. For the Git source code repository, the improvement is already obvious: Test this tree --------------------------------------------------------------- 1500.2: ahead-behind counts: git for-each-ref 0.07(0.07+0.00) 1500.3: ahead-behind counts: git branch 0.07(0.06+0.00) 1500.4: ahead-behind counts: git tag 0.07(0.06+0.00) 1500.5: ahead-behind counts: git rev-list 1.32(1.04+0.27) But the standard performance benchmark is the Linux kernel repository, which demosntrates a significant improvement: Test this tree --------------------------------------------------------------- 1500.2: ahead-behind counts: git for-each-ref 0.27(0.24+0.02) 1500.3: ahead-behind counts: git branch 0.27(0.24+0.03) 1500.4: ahead-behind counts: git tag 0.28(0.27+0.01) 1500.5: ahead-behind counts: git rev-list 4.57(4.03+0.54) The 'git rev-list' test exists in this change as a demonstration, but it will be removed in the next change to avoid wasting time on this comparison. Signed-off-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
171 lines
5.1 KiB
C
171 lines
5.1 KiB
C
#ifndef REF_FILTER_H
|
|
#define REF_FILTER_H
|
|
|
|
#include "oid-array.h"
|
|
#include "refs.h"
|
|
#include "commit.h"
|
|
#include "parse-options.h"
|
|
#include "string-list.h"
|
|
|
|
/* Quoting styles */
|
|
#define QUOTE_NONE 0
|
|
#define QUOTE_SHELL 1
|
|
#define QUOTE_PERL 2
|
|
#define QUOTE_PYTHON 4
|
|
#define QUOTE_TCL 8
|
|
|
|
#define FILTER_REFS_TAGS 0x0002
|
|
#define FILTER_REFS_BRANCHES 0x0004
|
|
#define FILTER_REFS_REMOTES 0x0008
|
|
#define FILTER_REFS_OTHERS 0x0010
|
|
#define FILTER_REFS_ALL (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
|
|
FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
|
|
#define FILTER_REFS_DETACHED_HEAD 0x0020
|
|
#define FILTER_REFS_KIND_MASK (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD)
|
|
|
|
struct atom_value;
|
|
struct ref_sorting;
|
|
struct ahead_behind_count;
|
|
|
|
enum ref_sorting_order {
|
|
REF_SORTING_REVERSE = 1<<0,
|
|
REF_SORTING_ICASE = 1<<1,
|
|
REF_SORTING_VERSION = 1<<2,
|
|
REF_SORTING_DETACHED_HEAD_FIRST = 1<<3,
|
|
};
|
|
|
|
struct ref_array_item {
|
|
struct object_id objectname;
|
|
const char *rest;
|
|
int flag;
|
|
unsigned int kind;
|
|
const char *symref;
|
|
struct commit *commit;
|
|
struct atom_value *value;
|
|
struct ahead_behind_count **counts;
|
|
|
|
char refname[FLEX_ARRAY];
|
|
};
|
|
|
|
struct ref_array {
|
|
int nr, alloc;
|
|
struct ref_array_item **items;
|
|
struct rev_info *revs;
|
|
|
|
struct ahead_behind_count *counts;
|
|
size_t counts_nr;
|
|
};
|
|
|
|
struct ref_filter {
|
|
const char **name_patterns;
|
|
struct oid_array points_at;
|
|
struct commit_list *with_commit;
|
|
struct commit_list *no_commit;
|
|
struct commit_list *reachable_from;
|
|
struct commit_list *unreachable_from;
|
|
|
|
unsigned int with_commit_tag_algo : 1,
|
|
match_as_path : 1,
|
|
ignore_case : 1,
|
|
detached : 1;
|
|
unsigned int kind,
|
|
lines;
|
|
int abbrev,
|
|
verbose;
|
|
};
|
|
|
|
struct ref_format {
|
|
/*
|
|
* Set these to define the format; make sure you call
|
|
* verify_ref_format() afterwards to finalize.
|
|
*/
|
|
const char *format;
|
|
const char *rest;
|
|
int quote_style;
|
|
int use_rest;
|
|
int use_color;
|
|
|
|
/* Internal state to ref-filter */
|
|
int need_color_reset_at_eol;
|
|
|
|
/* List of bases for ahead-behind counts. */
|
|
struct string_list bases;
|
|
};
|
|
|
|
#define REF_FORMAT_INIT { \
|
|
.use_color = -1, \
|
|
.bases = STRING_LIST_INIT_DUP, \
|
|
}
|
|
|
|
/* Macros for checking --merged and --no-merged options */
|
|
#define _OPT_MERGED_NO_MERGED(option, filter, h) \
|
|
{ OPTION_CALLBACK, 0, option, (filter), N_("commit"), (h), \
|
|
PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG, \
|
|
parse_opt_merge_filter, (intptr_t) "HEAD" \
|
|
}
|
|
#define OPT_MERGED(f, h) _OPT_MERGED_NO_MERGED("merged", f, h)
|
|
#define OPT_NO_MERGED(f, h) _OPT_MERGED_NO_MERGED("no-merged", f, h)
|
|
|
|
#define OPT_REF_SORT(var) \
|
|
OPT_STRING_LIST(0, "sort", (var), \
|
|
N_("key"), N_("field name to sort on"))
|
|
|
|
/*
|
|
* API for filtering a set of refs. Based on the type of refs the user
|
|
* has requested, we iterate through those refs and apply filters
|
|
* as per the given ref_filter structure and finally store the
|
|
* filtered refs in the ref_array structure.
|
|
*/
|
|
int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type);
|
|
/* Clear all memory allocated to ref_array */
|
|
void ref_array_clear(struct ref_array *array);
|
|
/* Used to verify if the given format is correct and to parse out the used atoms */
|
|
int verify_ref_format(struct ref_format *format);
|
|
/* Sort the given ref_array as per the ref_sorting provided */
|
|
void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
|
|
/* Set REF_SORTING_* sort_flags for all elements of a sorting list */
|
|
void ref_sorting_set_sort_flags_all(struct ref_sorting *sorting, unsigned int mask, int on);
|
|
/* Based on the given format and quote_style, fill the strbuf */
|
|
int format_ref_array_item(struct ref_array_item *info,
|
|
struct ref_format *format,
|
|
struct strbuf *final_buf,
|
|
struct strbuf *error_buf);
|
|
/* Release a "struct ref_sorting" */
|
|
void ref_sorting_release(struct ref_sorting *);
|
|
/* Convert list of sort options into ref_sorting */
|
|
struct ref_sorting *ref_sorting_options(struct string_list *);
|
|
/* Function to parse --merged and --no-merged options */
|
|
int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
|
|
/* Get the current HEAD's description */
|
|
char *get_head_description(void);
|
|
/* Set up translated strings in the output. */
|
|
void setup_ref_filter_porcelain_msg(void);
|
|
|
|
/*
|
|
* Print a single ref, outside of any ref-filter. Note that the
|
|
* name must be a fully qualified refname.
|
|
*/
|
|
void pretty_print_ref(const char *name, const struct object_id *oid,
|
|
struct ref_format *format);
|
|
|
|
/*
|
|
* Push a single ref onto the array; this can be used to construct your own
|
|
* ref_array without using filter_refs().
|
|
*/
|
|
struct ref_array_item *ref_array_push(struct ref_array *array,
|
|
const char *refname,
|
|
const struct object_id *oid);
|
|
|
|
/*
|
|
* If the provided format includes ahead-behind atoms, then compute the
|
|
* ahead-behind values for the array of filtered references. Must be
|
|
* called after filter_refs() but before outputting the formatted refs.
|
|
*
|
|
* If this is not called, then any ahead-behind atoms will be blank.
|
|
*/
|
|
void filter_ahead_behind(struct repository *r,
|
|
struct ref_format *format,
|
|
struct ref_array *array);
|
|
|
|
#endif /* REF_FILTER_H */
|