On Mon, Apr 05, 2021 at 01:04:13PM +0000, Derrick Stolee via GitGitGadget wrote: > From: Derrick Stolee <dstolee@xxxxxxxxxxxxx> > > 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@xxxxxxxxxxxxx> > --- > 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); is this even needed? > + > + 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; should this be strbuf_detach? > +} > + > 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 >