@matheusbsbs / matheus-bagliano / commits / 5483f79998
Fork 0

refactor find_ref_by_name() to accept const list

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>

Jeff King committed Feb 25, 2009 at 03:32 UTC 5483f79998c5a9705d453a713d11fb7591329ed4
2 files changed +3 -3
cache.h
+1 -1
@@ -801,7 +801,7 @@ struct ref {
801 #define REF_HEADS (1u << 1)
802 #define REF_TAGS (1u << 2)
803
804 -extern struct ref *find_ref_by_name(struct ref *list, const char *name);
804 +extern struct ref *find_ref_by_name(const struct ref *list, const char *name);
805
806 #define CONNECT_VERBOSE (1u << 0)
807 extern struct child_process *git_connect(int fd[2], const char *url, const char *prog, int flags);
refs.c
+2 -2
@@ -1628,10 +1628,10 @@ int update_ref(const char *action, const char *refname,
1628 return 0;
1629 }
1630
1631 -struct ref *find_ref_by_name(struct ref *list, const char *name)
1631 +struct ref *find_ref_by_name(const struct ref *list, const char *name)
1632 {
1633 for ( ; list; list = list->next)
1634 if (!strcmp(list->name, name))
1635 - return list;
1635 + return (struct ref *)list;
1636 return NULL;
1637 }