Skip to content

Commit

Permalink
refactor find_ref_by_name() to accept const list
Browse files Browse the repository at this point in the history
Since it doesn't actually touch its argument, this makes
sense.

However, we still want to return a non-const version (which
requires a cast) so that this:

  struct ref *a, *b;
  a = find_ref_by_name(b);

works. Unfortunately, you can also silently strip the const
from a variable:

  struct ref *a;
  const struct ref *b;
  a = find_ref_by_name(b);

This is a classic C const problem because there is no way to
say "return the type with the same constness that was passed
to us"; we provide the same semantics as standard library
functions like strchr.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Jay Soffian <jaysoffian@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Feb 26, 2009
1 parent 119c8ee commit 5483f79
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ struct ref {
#define REF_HEADS (1u << 1)
#define REF_TAGS (1u << 2)

extern struct ref *find_ref_by_name(struct ref *list, const char *name);
extern struct ref *find_ref_by_name(const struct ref *list, const char *name);

#define CONNECT_VERBOSE (1u << 0)
extern struct child_process *git_connect(int fd[2], const char *url, const char *prog, int flags);
Expand Down
4 changes: 2 additions & 2 deletions refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1628,10 +1628,10 @@ int update_ref(const char *action, const char *refname,
return 0;
}

struct ref *find_ref_by_name(struct ref *list, const char *name)
struct ref *find_ref_by_name(const struct ref *list, const char *name)
{
for ( ; list; list = list->next)
if (!strcmp(list->name, name))
return list;
return (struct ref *)list;
return NULL;
}

0 comments on commit 5483f79

Please sign in to comment.