[PATCH 3/5] refspec: output a refspec item
- From
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>
- Date
- Apr 5, 2021, 13:04 UTC
- Message-ID
- <e10007e1cf8ff0005295f648b9489c11a9427122.1617627856.git.gitgitgadget@gmail.com>
- In-Reply-To
- <pull.924.git.1617627856.gitgitgadget@gmail.com>
From: Derrick Stolee <dstolee@microsoft.com>
Add a new method, refspec_item_format(), that takes a 'struct refspec_item' pointer as input and returns a string for how that refspec item should be written to Git's config or a subcommand, such as 'git fetch'.
There are several subtleties regarding special-case refspecs that can occur and are represented in t5511-refspec.sh. These cases will be explored in new tests in the following change. It requires adding a new test helper in order to test this format directly, so that is saved for a separate change to keep this one focused on the logic of the format method.
A future change will consume this method when translating refspecs in the 'prefetch' task of the 'git maintenance' builtin.
Signed-off-by: Derrick Stolee <dstolee@microsoft.com> --- refspec.c | 25 +++++++++++++++++++++++++ refspec.h | 5 +++++ 2 files changed, 30 insertions(+)
diff --git a/refspec.c b/refspec.c index e3d852c0bfec..ca65ba01bfe6 100644 --- a/refspec.c +++ b/refspec.c @@ -180,6 +180,31 @@ void refspec_item_clear(struct refspec_item *item) item->exact_sha1 = 0; } +const char *refspec_item_format(const struct refspec_item *rsi) +{ + static struct strbuf buf = STRBUF_INIT; + + strbuf_reset(&buf); + + if (rsi->matching) + return ":"; + + if (rsi->negative) + strbuf_addch(&buf, '^'); + else if (rsi->force) + strbuf_addch(&buf, '+'); + + if (rsi->src) + strbuf_addstr(&buf, rsi->src); + + if (rsi->dst) { + strbuf_addch(&buf, ':'); + strbuf_addstr(&buf, rsi->dst); + } + + return buf.buf; +} + void refspec_init(struct refspec *rs, int fetch) { memset(rs, 0, sizeof(*rs)); diff --git a/refspec.h b/refspec.h index 8b79891d3218..92a312f5b4e6 100644 --- a/refspec.h +++ b/refspec.h @@ -56,6 +56,11 @@ int refspec_item_init(struct refspec_item *item, const char *refspec, void refspec_item_init_or_die(struct refspec_item *item, const char *refspec, int fetch); void refspec_item_clear(struct refspec_item *item); +/* + * Output a given refspec item to a string. + */ +const char *refspec_item_format(const struct refspec_item *rsi); + void refspec_init(struct refspec *rs, int fetch); void refspec_append(struct refspec *rs, const char *refspec); __attribute__((format (printf,2,3)))
-- gitgitgadget