]> asedeno.scripts.mit.edu Git - git.git/blobdiff - refs.c
receive-pack: check object type of sha1 before using them as commits
[git.git] / refs.c
diff --git a/refs.c b/refs.c
index 0dc5678079b6e6d40a06f48d14a9f73465cb9243..7484a46d6834b4c85dd81f1f38596bc3be087426 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -580,18 +580,6 @@ int for_each_remote_ref(each_ref_fn fn, void *cb_data)
        return do_for_each_ref("refs/remotes/", fn, 13, cb_data);
 }
 
-/* NEEDSWORK: This is only used by ssh-upload and it should go; the
- * caller should do resolve_ref or read_ref like everybody else.  Or
- * maybe everybody else should use get_ref_sha1() instead of doing
- * read_ref().
- */
-int get_ref_sha1(const char *ref, unsigned char *sha1)
-{
-       if (check_ref_format(ref))
-               return -1;
-       return read_ref(mkpath("refs/%s", ref), sha1);
-}
-
 /*
  * Make sure "ref" is something reasonable to have under ".git/refs/";
  * We do not like it if:
@@ -625,36 +613,72 @@ int check_ref_format(const char *ref)
                while ((ch = *cp++) == '/')
                        ; /* tolerate duplicated slashes */
                if (!ch)
-                       return -1; /* should not end with slashes */
+                       /* should not end with slashes */
+                       return CHECK_REF_FORMAT_ERROR;
 
                /* we are at the beginning of the path component */
                if (ch == '.')
-                       return -1;
+                       return CHECK_REF_FORMAT_ERROR;
                bad_type = bad_ref_char(ch);
                if (bad_type) {
-                       return (bad_type == 2 && !*cp) ? -3 : -1;
+                       return (bad_type == 2 && !*cp)
+                               ? CHECK_REF_FORMAT_WILDCARD
+                               : CHECK_REF_FORMAT_ERROR;
                }
 
                /* scan the rest of the path component */
                while ((ch = *cp++) != 0) {
                        bad_type = bad_ref_char(ch);
                        if (bad_type) {
-                               return (bad_type == 2 && !*cp) ? -3 : -1;
+                               return (bad_type == 2 && !*cp)
+                                       ? CHECK_REF_FORMAT_WILDCARD
+                                       : CHECK_REF_FORMAT_ERROR;
                        }
                        if (ch == '/')
                                break;
                        if (ch == '.' && *cp == '.')
-                               return -1;
+                               return CHECK_REF_FORMAT_ERROR;
                }
                level++;
                if (!ch) {
                        if (level < 2)
-                               return -2; /* at least of form "heads/blah" */
-                       return 0;
+                               return CHECK_REF_FORMAT_ONELEVEL;
+                       return CHECK_REF_FORMAT_OK;
                }
        }
 }
 
+const char *ref_rev_parse_rules[] = {
+       "%.*s",
+       "refs/%.*s",
+       "refs/tags/%.*s",
+       "refs/heads/%.*s",
+       "refs/remotes/%.*s",
+       "refs/remotes/%.*s/HEAD",
+       NULL
+};
+
+const char *ref_fetch_rules[] = {
+       "%.*s",
+       "refs/%.*s",
+       "refs/heads/%.*s",
+       NULL
+};
+
+int refname_match(const char *abbrev_name, const char *full_name, const char **rules)
+{
+       const char **p;
+       const int abbrev_name_len = strlen(abbrev_name);
+
+       for (p = rules; *p; p++) {
+               if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) {
+                       return 1;
+               }
+       }
+
+       return 0;
+}
+
 static struct ref_lock *verify_lock(struct ref_lock *lock,
        const unsigned char *old_sha1, int mustexist)
 {
@@ -797,9 +821,13 @@ struct ref_lock *lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
 
 struct ref_lock *lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1, int flags)
 {
-       if (check_ref_format(ref) == -1)
+       switch (check_ref_format(ref)) {
+       case CHECK_REF_FORMAT_ERROR:
+       case CHECK_REF_FORMAT_WILDCARD:
                return NULL;
-       return lock_ref_sha1_basic(ref, old_sha1, flags, NULL);
+       default:
+               return lock_ref_sha1_basic(ref, old_sha1, flags, NULL);
+       }
 }
 
 static struct lock_file packlock;
@@ -1075,7 +1103,7 @@ static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
        adjust_shared_perm(log_file);
 
        msglen = msg ? strlen(msg) : 0;
-       committer = git_committer_info(-1);
+       committer = git_committer_info(0);
        maxlen = strlen(committer) + msglen + 100;
        logrec = xmalloc(maxlen);
        len = sprintf(logrec, "%s %s %s\n",
@@ -1213,15 +1241,11 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
 static char *ref_msg(const char *line, const char *endp)
 {
        const char *ep;
-       char *msg;
-
        line += 82;
-       for (ep = line; ep < endp && *ep != '\n'; ep++)
-               ;
-       msg = xmalloc(ep - line + 1);
-       memcpy(msg, line, ep - line);
-       msg[ep - line] = 0;
-       return msg;
+       ep = memchr(line, '\n', endp - line);
+       if (!ep)
+               ep = endp;
+       return xmemdupz(line, ep - line);
 }
 
 int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *sha1, char **msg, unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
@@ -1449,3 +1473,11 @@ int update_ref(const char *action, const char *refname,
        }
        return 0;
 }
+
+struct ref *find_ref_by_name(struct ref *list, const char *name)
+{
+       for ( ; list; list = list->next)
+               if (!strcmp(list->name, name))
+                       return list;
+       return NULL;
+}