refs: add ref_type function
authorDavid Turner <dturner@twopensource.com>
Fri, 31 Jul 2015 06:06:18 +0000 (02:06 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 31 Jul 2015 17:39:04 +0000 (10:39 -0700)
Add a function ref_type, which categorizes refs as per-worktree,
pseudoref, or normal ref.

Later, we will use this in refs.c to treat pseudorefs specially.
Alternate ref backends may use it to treat both pseudorefs and
per-worktree refs differently.

Signed-off-by: David Turner <dturner@twopensource.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs.c
refs.h
diff --git a/refs.c b/refs.c
index 97a75f3c3125d6a73e020ee2da73fcb6cce3d13c..58be2595e590b7feff438d18365f8d0cb4bbcae3 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -2821,6 +2821,32 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
        return 0;
 }
 
+static int is_per_worktree_ref(const char *refname)
+{
+       return !strcmp(refname, "HEAD");
+}
+
+static int is_pseudoref_syntax(const char *refname)
+{
+       const char *c;
+
+       for (c = refname; *c; c++) {
+               if (!isupper(*c) && *c != '-' && *c != '_')
+                       return 0;
+       }
+
+       return 1;
+}
+
+enum ref_type ref_type(const char *refname)
+{
+       if (is_per_worktree_ref(refname))
+               return REF_TYPE_PER_WORKTREE;
+       if (is_pseudoref_syntax(refname))
+               return REF_TYPE_PSEUDOREF;
+       return REF_TYPE_NORMAL;
+}
+
 int delete_ref(const char *refname, const unsigned char *sha1, unsigned int flags)
 {
        struct ref_transaction *transaction;
diff --git a/refs.h b/refs.h
index 4f1b23656892ab0cdd3ea459138df0c6e843dc4c..1927bda993da81d0d5a7011ff7b15fb8d4f62d92 100644 (file)
--- a/refs.h
+++ b/refs.h
@@ -380,6 +380,14 @@ int update_ref(const char *msg, const char *refname,
 extern int parse_hide_refs_config(const char *var, const char *value, const char *);
 extern int ref_is_hidden(const char *);
 
+enum ref_type {
+       REF_TYPE_PER_WORKTREE,
+       REF_TYPE_PSEUDOREF,
+       REF_TYPE_NORMAL,
+};
+
+enum ref_type ref_type(const char *refname);
+
 enum expire_reflog_flags {
        EXPIRE_REFLOGS_DRY_RUN = 1 << 0,
        EXPIRE_REFLOGS_UPDATE_REF = 1 << 1,