git/refspec.h
Jeff King fe17a25905 refspec: store raw refspecs inside refspec_item
The refspec struct keeps two matched arrays: one for the refspec_item
structs and one for the original raw refspec strings. The main reason
for this is that there are other users of refspec_item that do not care
about the raw strings. But it does make managing the refspec struct
awkward, as we must keep the two arrays in sync. This has led to bugs in
the past (both leaks and double-frees).

Let's just store a copy of the raw refspec string directly in each
refspec_item struct. This simplifies the handling at a small cost:

  1. Direct callers of refspec_item_init() will now get an extra copy of
     the refspec string, even if they don't need it. This should be
     negligible, as the struct is already allocating two strings for the
     parsed src/dst values (and we tend to only do it sparingly anyway
     for things like the TAG_REFSPEC literal).

  2. Users of refspec_appendf() will now generate a temporary string,
     copy it, and then free the result (versus handing off ownership of
     the temporary string). We could get around this by having a "nodup"
     variant of refspec_item_init(), but it doesn't seem worth the extra
     complexity for something that is not remotely a hot code path.

Code which accesses refspec->raw now needs to look at refspec->item.raw.
Other callers which just use refspec_item directly can remain the same.
We'll free the allocated string in refspec_item_clear(), which they
should be calling anyway to free src/dst.

One subtle note: refspec_item_init() can return an error, in which case
we'll still have set its "raw" field. But that is also true of the "src"
and "dst" fields, so any caller which does not _clear() the failed item
is already potentially leaking. In practice most code just calls die()
on an error anyway, but you can see the exception in valid_fetch_refspec(),
which does correctly call _clear() even on error.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-11-12 18:16:48 +09:00

75 lines
2.3 KiB
C

#ifndef REFSPEC_H
#define REFSPEC_H
#define TAG_REFSPEC "refs/tags/*:refs/tags/*"
/**
* A struct refspec_item holds the parsed interpretation of a refspec. If it
* will force updates (starts with a '+'), force is true. If it is a pattern
* (sides end with '*') pattern is true. If it is a negative refspec, (starts
* with '^'), negative is true. src and dest are the two sides (including '*'
* characters if present); if there is only one side, it is src, and dst is
* NULL; if sides exist but are empty (i.e., the refspec either starts or ends
* with ':'), the corresponding side is "".
*
* remote_find_tracking(), given a remote and a struct refspec_item with either src
* or dst filled out, will fill out the other such that the result is in the
* "fetch" specification for the remote (note that this evaluates patterns and
* returns a single result).
*/
struct refspec_item {
unsigned force : 1;
unsigned pattern : 1;
unsigned matching : 1;
unsigned exact_sha1 : 1;
unsigned negative : 1;
char *src;
char *dst;
char *raw;
};
#define REFSPEC_FETCH 1
#define REFSPEC_PUSH 0
#define REFSPEC_INIT_FETCH { .fetch = REFSPEC_FETCH }
#define REFSPEC_INIT_PUSH { .fetch = REFSPEC_PUSH }
/**
* An array of strings can be parsed into a struct refspec using
* parse_fetch_refspec() or parse_push_refspec().
*/
struct refspec {
struct refspec_item *items;
int alloc;
int nr;
int fetch;
};
int refspec_item_init(struct refspec_item *item, const char *refspec,
int fetch);
void refspec_item_init_or_die(struct refspec_item *item, const char *refspec,
int fetch);
void refspec_item_clear(struct refspec_item *item);
void refspec_init(struct refspec *rs, int fetch);
void refspec_append(struct refspec *rs, const char *refspec);
__attribute__((format (printf,2,3)))
void refspec_appendf(struct refspec *rs, const char *fmt, ...);
void refspec_appendn(struct refspec *rs, const char **refspecs, int nr);
void refspec_clear(struct refspec *rs);
int valid_fetch_refspec(const char *refspec);
int valid_remote_name(const char *name);
struct strvec;
/*
* Determine what <prefix> values to pass to the peer in ref-prefix lines
* (see linkgit:gitprotocol-v2[5]).
*/
void refspec_ref_prefixes(const struct refspec *rs,
struct strvec *ref_prefixes);
#endif /* REFSPEC_H */