Fix buffer overflow in prepare_attr_stack
[gitweb.git] / refs.c
diff --git a/refs.c b/refs.c
index 89876bff871d007a6675f5790ce8cb34fe21fb39..c9bcf148b0e1cd659e645e0f793fc07ebc133426 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -2,6 +2,7 @@
 #include "refs.h"
 #include "object.h"
 #include "tag.h"
+#include "dir.h"
 
 /* ISSYMREF=01 and ISPACKED=02 are public interfaces */
 #define REF_KNOWS_PEELED 04
@@ -150,12 +151,15 @@ static struct ref_list *sort_ref_list(struct ref_list *list)
  * Future: need to be in "struct repository"
  * when doing a full libification.
  */
-struct cached_refs {
+static struct cached_refs {
        char did_loose;
        char did_packed;
        struct ref_list *loose;
        struct ref_list *packed;
 } cached_refs;
+static struct ref_list *current_ref;
+
+static struct ref_list *extra_refs;
 
 static void free_ref_list(struct ref_list *list)
 {
@@ -213,6 +217,17 @@ static void read_packed_refs(FILE *f, struct cached_refs *cached_refs)
        cached_refs->packed = sort_ref_list(list);
 }
 
+void add_extra_ref(const char *name, const unsigned char *sha1, int flag)
+{
+       extra_refs = add_ref(name, sha1, flag, extra_refs, NULL);
+}
+
+void clear_extra_refs(void)
+{
+       free_ref_list(extra_refs);
+       extra_refs = NULL;
+}
+
 static struct ref_list *get_packed_refs(void)
 {
        if (!cached_refs.did_packed) {
@@ -350,6 +365,7 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *re
 {
        int len = strlen(path), retval;
        char *gitdir;
+       const char *tmp;
 
        while (len && path[len-1] == '/')
                len--;
@@ -357,16 +373,27 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *re
                return -1;
        gitdir = xmalloc(len + MAXREFLEN + 8);
        memcpy(gitdir, path, len);
-       memcpy(gitdir + len, "/.git/", 7);
-
-       retval = resolve_gitlink_ref_recursive(gitdir, len+6, refname, result, 0);
+       memcpy(gitdir + len, "/.git", 6);
+       len += 5;
+
+       tmp = read_gitfile_gently(gitdir);
+       if (tmp) {
+               free(gitdir);
+               len = strlen(tmp);
+               gitdir = xmalloc(len + MAXREFLEN + 3);
+               memcpy(gitdir, tmp, len);
+       }
+       gitdir[len] = '/';
+       gitdir[++len] = '\0';
+       retval = resolve_gitlink_ref_recursive(gitdir, len, refname, result, 0);
        free(gitdir);
        return retval;
 }
 
 const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *flag)
 {
-       int depth = MAXDEPTH, len;
+       int depth = MAXDEPTH;
+       ssize_t len;
        char buffer[256];
        static char ref_buffer[256];
 
@@ -475,6 +502,7 @@ static int do_one_ref(const char *base, each_ref_fn fn, int trim,
                error("%s does not point to a valid object!", entry->name);
                return 0;
        }
+       current_ref = entry;
        return fn(entry->name + trim, entry->sha1, entry->flag, cb_data);
 }
 
@@ -484,6 +512,16 @@ int peel_ref(const char *ref, unsigned char *sha1)
        unsigned char base[20];
        struct object *o;
 
+       if (current_ref && (current_ref->name == ref
+               || !strcmp(current_ref->name, ref))) {
+               if (current_ref->flag & REF_KNOWS_PEELED) {
+                       hashcpy(sha1, current_ref->peeled);
+                       return 0;
+               }
+               hashcpy(base, current_ref->sha1);
+               goto fallback;
+       }
+
        if (!resolve_ref(ref, base, 1, &flag))
                return -1;
 
@@ -503,9 +541,9 @@ int peel_ref(const char *ref, unsigned char *sha1)
                }
        }
 
-       /* fallback - callers should not call this for unpacked refs */
+fallback:
        o = parse_object(base);
-       if (o->type == OBJ_TAG) {
+       if (o && o->type == OBJ_TAG) {
                o = deref_tag(o, ref, 0);
                if (o) {
                        hashcpy(sha1, o->sha1);
@@ -518,10 +556,15 @@ int peel_ref(const char *ref, unsigned char *sha1)
 static int do_for_each_ref(const char *base, each_ref_fn fn, int trim,
                           void *cb_data)
 {
-       int retval;
+       int retval = 0;
        struct ref_list *packed = get_packed_refs();
        struct ref_list *loose = get_loose_refs();
 
+       struct ref_list *extra;
+
+       for (extra = extra_refs; extra; extra = extra->next)
+               retval = do_one_ref(base, fn, trim, cb_data, extra);
+
        while (packed && loose) {
                struct ref_list *entry;
                int cmp = strcmp(packed->name, loose->name);
@@ -538,15 +581,18 @@ static int do_for_each_ref(const char *base, each_ref_fn fn, int trim,
                }
                retval = do_one_ref(base, fn, trim, cb_data, entry);
                if (retval)
-                       return retval;
+                       goto end_each;
        }
 
        for (packed = packed ? packed : loose; packed; packed = packed->next) {
                retval = do_one_ref(base, fn, trim, cb_data, packed);
                if (retval)
-                       return retval;
+                       goto end_each;
        }
-       return 0;
+
+end_each:
+       current_ref = NULL;
+       return retval;
 }
 
 int head_ref(each_ref_fn fn, void *cb_data)
@@ -579,18 +625,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:
@@ -603,15 +637,20 @@ int get_ref_sha1(const char *ref, unsigned char *sha1)
 
 static inline int bad_ref_char(int ch)
 {
-       return (((unsigned) ch) <= ' ' ||
-               ch == '~' || ch == '^' || ch == ':' ||
-               /* 2.13 Pattern Matching Notation */
-               ch == '?' || ch == '*' || ch == '[');
+       if (((unsigned) ch) <= ' ' ||
+           ch == '~' || ch == '^' || ch == ':')
+               return 1;
+       /* 2.13 Pattern Matching Notation */
+       if (ch == '?' || ch == '[') /* Unsupported */
+               return 1;
+       if (ch == '*') /* Supported at the end */
+               return 2;
+       return 0;
 }
 
 int check_ref_format(const char *ref)
 {
-       int ch, level;
+       int ch, level, bad_type;
        const char *cp = ref;
 
        level = 0;
@@ -619,28 +658,70 @@ 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 == '.' || bad_ref_char(ch))
-                       return -1;
+               if (ch == '.')
+                       return CHECK_REF_FORMAT_ERROR;
+               bad_type = bad_ref_char(ch);
+               if (bad_type) {
+                       return (bad_type == 2 && !*cp)
+                               ? CHECK_REF_FORMAT_WILDCARD
+                               : CHECK_REF_FORMAT_ERROR;
+               }
 
                /* scan the rest of the path component */
                while ((ch = *cp++) != 0) {
-                       if (bad_ref_char(ch))
-                               return -1;
+                       bad_type = bad_ref_char(ch);
+                       if (bad_type) {
+                               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,
@@ -660,57 +741,23 @@ static struct ref_lock *verify_lock(struct ref_lock *lock,
        return lock;
 }
 
-static int remove_empty_dir_recursive(char *path, int len)
-{
-       DIR *dir = opendir(path);
-       struct dirent *e;
-       int ret = 0;
-
-       if (!dir)
-               return -1;
-       if (path[len-1] != '/')
-               path[len++] = '/';
-       while ((e = readdir(dir)) != NULL) {
-               struct stat st;
-               int namlen;
-               if ((e->d_name[0] == '.') &&
-                   ((e->d_name[1] == 0) ||
-                    ((e->d_name[1] == '.') && e->d_name[2] == 0)))
-                       continue; /* "." and ".." */
-
-               namlen = strlen(e->d_name);
-               if ((len + namlen < PATH_MAX) &&
-                   strcpy(path + len, e->d_name) &&
-                   !lstat(path, &st) &&
-                   S_ISDIR(st.st_mode) &&
-                   !remove_empty_dir_recursive(path, len + namlen))
-                       continue; /* happy */
-
-               /* path too long, stat fails, or non-directory still exists */
-               ret = -1;
-               break;
-       }
-       closedir(dir);
-       if (!ret) {
-               path[len] = 0;
-               ret = rmdir(path);
-       }
-       return ret;
-}
-
-static int remove_empty_directories(char *file)
+static int remove_empty_directories(const char *file)
 {
        /* we want to create a file but there is a directory there;
         * if that is an empty directory (or a directory that contains
         * only empty directories), remove them.
         */
-       char path[PATH_MAX];
-       int len = strlen(file);
+       struct strbuf path;
+       int result;
 
-       if (len >= PATH_MAX) /* path too long ;-) */
-               return -1;
-       strcpy(path, file);
-       return remove_empty_dir_recursive(path, len);
+       strbuf_init(&path, 20);
+       strbuf_addstr(&path, file);
+
+       result = remove_dir_recursively(&path, 1);
+
+       strbuf_release(&path);
+
+       return result;
 }
 
 static int is_refname_available(const char *ref, const char *oldref,
@@ -736,19 +783,20 @@ static int is_refname_available(const char *ref, const char *oldref,
        return 1;
 }
 
-static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1, int *flag)
+static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1, int flags, int *type_p)
 {
        char *ref_file;
        const char *orig_ref = ref;
        struct ref_lock *lock;
        struct stat st;
        int last_errno = 0;
+       int type;
        int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
 
        lock = xcalloc(1, sizeof(struct ref_lock));
        lock->lock_fd = -1;
 
-       ref = resolve_ref(ref, lock->old_sha1, mustexist, flag);
+       ref = resolve_ref(ref, lock->old_sha1, mustexist, &type);
        if (!ref && errno == EISDIR) {
                /* we are trying to lock foo but we used to
                 * have foo/bar which now does not exist;
@@ -761,8 +809,10 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
                        error("there are still refs under '%s'", orig_ref);
                        goto error_return;
                }
-               ref = resolve_ref(orig_ref, lock->old_sha1, mustexist, flag);
+               ref = resolve_ref(orig_ref, lock->old_sha1, mustexist, &type);
        }
+       if (type_p)
+           *type_p = type;
        if (!ref) {
                last_errno = errno;
                error("unable to resolve reference %s: %s",
@@ -780,10 +830,15 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
 
        lock->lk = xcalloc(1, sizeof(struct lock_file));
 
+       if (flags & REF_NODEREF)
+               ref = orig_ref;
        lock->ref_name = xstrdup(ref);
        lock->orig_ref_name = xstrdup(orig_ref);
        ref_file = git_path("%s", ref);
-       lock->force_write = lstat(ref_file, &st) && errno == ENOENT;
+       if (lstat(ref_file, &st) && errno == ENOENT)
+               lock->force_write = 1;
+       if ((flags & REF_NODEREF) && (type & REF_ISSYMREF))
+               lock->force_write = 1;
 
        if (safe_create_leading_directories(ref_file)) {
                last_errno = errno;
@@ -806,14 +861,18 @@ struct ref_lock *lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
        if (check_ref_format(ref))
                return NULL;
        strcpy(refpath, mkpath("refs/%s", ref));
-       return lock_ref_sha1_basic(refpath, old_sha1, NULL);
+       return lock_ref_sha1_basic(refpath, old_sha1, 0, NULL);
 }
 
-struct ref_lock *lock_any_ref_for_update(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)) {
+       default:
                return NULL;
-       return lock_ref_sha1_basic(ref, old_sha1, NULL);
+       case 0:
+       case CHECK_REF_FORMAT_ONELEVEL:
+               return lock_ref_sha1_basic(ref, old_sha1, flags, NULL);
+       }
 }
 
 static struct lock_file packlock;
@@ -858,7 +917,7 @@ int delete_ref(const char *refname, const unsigned char *sha1)
        struct ref_lock *lock;
        int err, i, ret = 0, flag = 0;
 
-       lock = lock_ref_sha1_basic(refname, sha1, &flag);
+       lock = lock_ref_sha1_basic(refname, sha1, 0, &flag);
        if (!lock)
                return 1;
        if (!(flag & REF_ISPACKED)) {
@@ -866,7 +925,7 @@ int delete_ref(const char *refname, const unsigned char *sha1)
                i = strlen(lock->lk->filename) - 5; /* .lock */
                lock->lk->filename[i] = 0;
                err = unlink(lock->lk->filename);
-               if (err) {
+               if (err && errno != ENOENT) {
                        ret = 1;
                        error("unlink(%s) failed: %s",
                              lock->lk->filename, strerror(errno));
@@ -909,7 +968,7 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
        if (!is_refname_available(newref, oldref, get_loose_refs(), 0))
                return 1;
 
-       lock = lock_ref_sha1_basic(renamed_ref, NULL, NULL);
+       lock = lock_ref_sha1_basic(renamed_ref, NULL, 0, NULL);
        if (!lock)
                return error("unable to lock %s", renamed_ref);
        lock->force_write = 1;
@@ -963,7 +1022,7 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
        }
        logmoved = log;
 
-       lock = lock_ref_sha1_basic(newref, NULL, NULL);
+       lock = lock_ref_sha1_basic(newref, NULL, 0, NULL);
        if (!lock) {
                error("unable to lock %s for update", newref);
                goto rollback;
@@ -979,7 +1038,7 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
        return 0;
 
  rollback:
-       lock = lock_ref_sha1_basic(oldref, NULL, NULL);
+       lock = lock_ref_sha1_basic(oldref, NULL, 0, NULL);
        if (!lock) {
                error("unable to lock %s for rollback", oldref);
                goto rollbacklog;
@@ -1004,19 +1063,58 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
        return 1;
 }
 
+int close_ref(struct ref_lock *lock)
+{
+       if (close_lock_file(lock->lk))
+               return -1;
+       lock->lock_fd = -1;
+       return 0;
+}
+
+int commit_ref(struct ref_lock *lock)
+{
+       if (commit_lock_file(lock->lk))
+               return -1;
+       lock->lock_fd = -1;
+       return 0;
+}
+
 void unlock_ref(struct ref_lock *lock)
 {
-       if (lock->lock_fd >= 0) {
-               close(lock->lock_fd);
-               /* Do not free lock->lk -- atexit() still looks at them */
-               if (lock->lk)
-                       rollback_lock_file(lock->lk);
-       }
+       /* Do not free lock->lk -- atexit() still looks at them */
+       if (lock->lk)
+               rollback_lock_file(lock->lk);
        free(lock->ref_name);
        free(lock->orig_ref_name);
        free(lock);
 }
 
+/*
+ * copy the reflog message msg to buf, which has been allocated sufficiently
+ * large, while cleaning up the whitespaces.  Especially, convert LF to space,
+ * because reflog file is one line per entry.
+ */
+static int copy_msg(char *buf, const char *msg)
+{
+       char *cp = buf;
+       char c;
+       int wasspace = 1;
+
+       *cp++ = '\t';
+       while ((c = *msg++)) {
+               if (wasspace && isspace(c))
+                       continue;
+               wasspace = isspace(c);
+               if (wasspace)
+                       c = ' ';
+               *cp++ = c;
+       }
+       while (buf < cp && isspace(cp[-1]))
+               cp--;
+       *cp++ = '\n';
+       return cp - buf;
+}
+
 static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
                         const unsigned char *new_sha1, const char *msg)
 {
@@ -1061,22 +1159,8 @@ static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
 
        adjust_shared_perm(log_file);
 
-       msglen = 0;
-       if (msg) {
-               /* clean up the message and make sure it is a single line */
-               for ( ; *msg; msg++)
-                       if (!isspace(*msg))
-                               break;
-               if (*msg) {
-                       const char *ep = strchr(msg, '\n');
-                       if (ep)
-                               msglen = ep - msg;
-                       else
-                               msglen = strlen(msg);
-               }
-       }
-
-       committer = git_committer_info(-1);
+       msglen = msg ? strlen(msg) : 0;
+       committer = git_committer_info(0);
        maxlen = strlen(committer) + msglen + 100;
        logrec = xmalloc(maxlen);
        len = sprintf(logrec, "%s %s %s\n",
@@ -1084,19 +1168,24 @@ static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
                      sha1_to_hex(new_sha1),
                      committer);
        if (msglen)
-               len += sprintf(logrec + len - 1, "\t%.*s\n", msglen, msg) - 1;
+               len += copy_msg(logrec + len - 1, msg) - 1;
        written = len <= maxlen ? write_in_full(logfd, logrec, len) : -1;
        free(logrec);
-       close(logfd);
-       if (written != len)
+       if (close(logfd) != 0 || written != len)
                return error("Unable to append to %s", log_file);
        return 0;
 }
 
+static int is_branch(const char *refname)
+{
+       return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
+}
+
 int write_ref_sha1(struct ref_lock *lock,
        const unsigned char *sha1, const char *logmsg)
 {
        static char term = '\n';
+       struct object *o;
 
        if (!lock)
                return -1;
@@ -1104,9 +1193,22 @@ int write_ref_sha1(struct ref_lock *lock,
                unlock_ref(lock);
                return 0;
        }
+       o = parse_object(sha1);
+       if (!o) {
+               error("Trying to write ref %s with nonexistant object %s",
+                       lock->ref_name, sha1_to_hex(sha1));
+               unlock_ref(lock);
+               return -1;
+       }
+       if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
+               error("Trying to write non-commit object %s to branch %s",
+                       sha1_to_hex(sha1), lock->ref_name);
+               unlock_ref(lock);
+               return -1;
+       }
        if (write_in_full(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
            write_in_full(lock->lock_fd, &term, 1) != 1
-               || close(lock->lock_fd) < 0) {
+               || close_ref(lock) < 0) {
                error("Couldn't write %s", lock->lk->filename);
                unlock_ref(lock);
                return -1;
@@ -1139,12 +1241,11 @@ int write_ref_sha1(struct ref_lock *lock,
                    !strcmp(head_ref, lock->ref_name))
                        log_ref_write("HEAD", lock->old_sha1, sha1, logmsg);
        }
-       if (commit_lock_file(lock->lk)) {
+       if (commit_ref(lock)) {
                error("Couldn't set %s", lock->ref_name);
                unlock_ref(lock);
                return -1;
        }
-       lock->lock_fd = -1;
        unlock_ref(lock);
        return 0;
 }
@@ -1185,8 +1286,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
                goto error_free_return;
        }
        written = write_in_full(fd, ref, len);
-       close(fd);
-       if (written != len) {
+       if (close(fd) != 0 || written != len) {
                error("Unable to write to %s", lockpath);
                goto error_unlink_return;
        }
@@ -1216,15 +1316,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)
@@ -1283,7 +1379,7 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *
                                if (hashcmp(logged_sha1, sha1)) {
                                        fprintf(stderr,
                                                "warning: Log %s has gap after %s.\n",
-                                               logfile, show_rfc2822_date(date, tz));
+                                               logfile, show_date(date, tz, DATE_RFC2822));
                                }
                        }
                        else if (date == at_time) {
@@ -1296,7 +1392,7 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *
                                if (hashcmp(logged_sha1, sha1)) {
                                        fprintf(stderr,
                                                "warning: Log %s unexpectedly ended on %s.\n",
-                                               logfile, show_rfc2822_date(date, tz));
+                                               logfile, show_date(date, tz, DATE_RFC2822));
                                }
                        }
                        munmap(log_mapped, mapsz);
@@ -1425,3 +1521,38 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)
 {
        return do_for_each_reflog("", fn, cb_data);
 }
+
+int update_ref(const char *action, const char *refname,
+               const unsigned char *sha1, const unsigned char *oldval,
+               int flags, enum action_on_err onerr)
+{
+       static struct ref_lock *lock;
+       lock = lock_any_ref_for_update(refname, oldval, flags);
+       if (!lock) {
+               const char *str = "Cannot lock the ref '%s'.";
+               switch (onerr) {
+               case MSG_ON_ERR: error(str, refname); break;
+               case DIE_ON_ERR: die(str, refname); break;
+               case QUIET_ON_ERR: break;
+               }
+               return 1;
+       }
+       if (write_ref_sha1(lock, sha1, action) < 0) {
+               const char *str = "Cannot update the ref '%s'.";
+               switch (onerr) {
+               case MSG_ON_ERR: error(str, refname); break;
+               case DIE_ON_ERR: die(str, refname); break;
+               case QUIET_ON_ERR: break;
+               }
+               return 1;
+       }
+       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;
+}