mirror of
https://github.com/git/git.git
synced 2024-11-27 12:03:55 +08:00
98e7ab6d42
The for-each-ref family of commands invoke parsers immediately when
it sees each --sort=<atom> option, and die before even seeing the
other options on the command line when the <atom> is unrecognised.
Instead, accumulate them in a string list, and have them parsed into
a ref_sorting structure after the command line parsing is done. As
a consequence, "git branch --sort=bogus -h" used to fail to give the
brief help, which arguably may have been a feature, now does so,
which is more consistent with how other options work.
The patch is smaller than the actual extent of the "damage" to the
codebase, thanks to the fact that the original code consistently
used OPT_REF_SORT() macro to handle command line options. We only
needed to replace the variable used for the list, and implementation
of the callback function used in the macro.
The old rule was for the users of the API to:
- Declare ref_sorting and ref_sorting_tail variables;
- OPT_REF_SORT() macro will instantiate ref_sorting instance (which
may barf and die) and append it to the tail;
- Append to the tail each ref_sorting read from the configuration
by parsing in the config callback (which may barf and die);
- See if ref_sorting is null and use ref_sorting_default() instead.
Now the rule is not all that different but is simpler:
- Declare ref_sorting_options string list.
- OPT_REF_SORT() macro will append it to the string list;
- Append to the string list the sort key read from the
configuration;
- call ref_sorting_options() to turn the string list to ref_sorting
structure (which also deals with the default value).
As side effects, this change also cleans up a few issues:
- 95be717c
(parse_opt_ref_sorting: always use with NONEG flag,
2019-03-20) muses that "git for-each-ref --no-sort" should simply
clear the sort keys accumulated so far; it now does.
- The implementation detail of "struct ref_sorting" and the helper
function parse_ref_sorting() can now be private to the ref-filter
API implementation.
- If you set branch.sort to a bogus value, the any "git branch"
invocation, not only the listing mode, would abort with the
original code; now it doesn't
Signed-off-by: Junio C Hamano <gitster@pobox.com>
147 lines
4.4 KiB
C
147 lines
4.4 KiB
C
#ifndef REF_FILTER_H
|
|
#define REF_FILTER_H
|
|
|
|
#include "oid-array.h"
|
|
#include "refs.h"
|
|
#include "commit.h"
|
|
#include "parse-options.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;
|
|
|
|
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;
|
|
char refname[FLEX_ARRAY];
|
|
};
|
|
|
|
struct ref_array {
|
|
int nr, alloc;
|
|
struct ref_array_item **items;
|
|
struct rev_info *revs;
|
|
};
|
|
|
|
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;
|
|
};
|
|
|
|
#define REF_FORMAT_INIT { .use_color = -1 }
|
|
|
|
/* 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);
|
|
|
|
#endif /* REF_FILTER_H */
|