builtin/for-each-ref: factor out core logic into a helper
The implementation of `git for-each-ref` is monolithic within `cmd_for_each_ref()`, making it impossible to share its logic with other commands. To enable code reuse for the upcoming `git refs list` subcommand, refactor the core logic into a shared helper function. Introduce a new `for-each-ref.h` header to define the public interface for this shared logic. It contains the declaration for a new helper function, `for_each_ref_core()`, and a macro for the common usage options. Move the option parsing, filtering, and formatting logic from `cmd_for_each_ref()` into a new helper function named `for_each_ref_core()`. This helper is made generic by accepting the command's usage string as a parameter. The original `cmd_for_each_ref()` is simplified to a thin wrapper that is only responsible for defining its specific usage array and calling the shared helper. Mentored-by: Patrick Steinhardt <ps@pks.im> Mentored-by: shejialuo <shejialuo@gmail.com> Mentored-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Meet Soni <meetsoni3017@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
69c207dc45
commit
6eeb1c070a
26
for-each-ref.h
Normal file
26
for-each-ref.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef FOR_EACH_REF_H
|
||||
#define FOR_EACH_REF_H
|
||||
|
||||
struct repository;
|
||||
|
||||
/*
|
||||
* Shared usage string for options common to git-for-each-ref(1)
|
||||
* and git-refs-list(1). The command-specific part (e.g., "git refs list ")
|
||||
* must be prepended by the caller.
|
||||
*/
|
||||
#define COMMON_USAGE_FOR_EACH_REF \
|
||||
"[--count=<count>] [--shell|--perl|--python|--tcl]\n" \
|
||||
" [(--sort=<key>)...] [--format=<format>]\n" \
|
||||
" [--include-root-refs] [--points-at=<object>]\n" \
|
||||
" [--merged[=<object>]] [--no-merged[=<object>]]\n" \
|
||||
" [--contains[=<object>]] [--no-contains[=<object>]]\n" \
|
||||
" [(--exclude=<pattern>)...] [--start-after=<marker>]\n" \
|
||||
" [ --stdin | <pattern>... ]"
|
||||
|
||||
/*
|
||||
* The core logic for for-each-ref and its clones.
|
||||
*/
|
||||
int for_each_ref_core(int argc, const char **argv, const char *prefix,
|
||||
struct repository *repo, const char *const *usage);
|
||||
|
||||
#endif /* FOR_EACH_REF_H */
|
||||
Reference in New Issue
Block a user