]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refactor find_ref_by_name() to accept const list
authorJeff King <peff@peff.net>
Wed, 25 Feb 2009 08:32:10 +0000 (03:32 -0500)
committerJunio C Hamano <gitster@pobox.com>
Thu, 26 Feb 2009 08:49:44 +0000 (00:49 -0800)
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>
cache.h
refs.c

diff --git a/cache.h b/cache.h
index 189151de25ffd1a6671b7a70f359fa9b94b82173..609380d93500fc8de451ea62b56e9fdc53900752 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -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);
diff --git a/refs.c b/refs.c
index 6eb5f5384611bb5d159d892a1bfd120d72e54b9b..b2a37e1185704733faa4d8395b996dc80d3f19ef 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -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;
 }