Merge branch 'mh/ref-api'
authorJunio C Hamano <gitster@pobox.com>
Fri, 21 Oct 2011 23:04:36 +0000 (16:04 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 21 Oct 2011 23:04:36 +0000 (16:04 -0700)
* mh/ref-api:
clear_ref_cache(): inline function
write_ref_sha1(): only invalidate the loose ref cache
clear_ref_cache(): extract two new functions
clear_ref_cache(): rename parameter
invalidate_ref_cache(): expose this function in the refs API
invalidate_ref_cache(): take the submodule as parameter
invalidate_ref_cache(): rename function from invalidate_cached_refs()

1  2 
refs.c
diff --combined refs.c
index 2496d4093ed231cc448b7d50be64e443aff34e92,d8a4fa33e35cdf73a8d6efce593a857f395f96f8..ee7e38182a5ded3aa13e513880be7edf52a5be2b
--- 1/refs.c
--- 2/refs.c
+++ b/refs.c
@@@ -134,15 -134,15 +134,15 @@@ static struct ref_entry *search_ref_arr
   * Future: need to be in "struct repository"
   * when doing a full libification.
   */
- static struct cached_refs {
-       struct cached_refs *next;
+ static struct ref_cache {
+       struct ref_cache *next;
        char did_loose;
        char did_packed;
        struct ref_array loose;
        struct ref_array packed;
        /* The submodule name, or "" for the main repo. */
        char name[FLEX_ARRAY];
- } *cached_refs;
+ } *ref_cache;
  
  static struct ref_entry *current_ref;
  
@@@ -158,36 -158,41 +158,41 @@@ static void free_ref_array(struct ref_a
        array->refs = NULL;
  }
  
- static void clear_cached_refs(struct cached_refs *ca)
+ static void clear_packed_ref_cache(struct ref_cache *refs)
  {
-       if (ca->did_loose)
-               free_ref_array(&ca->loose);
-       if (ca->did_packed)
-               free_ref_array(&ca->packed);
-       ca->did_loose = ca->did_packed = 0;
+       if (refs->did_packed)
+               free_ref_array(&refs->packed);
+       refs->did_packed = 0;
  }
  
- static struct cached_refs *create_cached_refs(const char *submodule)
+ static void clear_loose_ref_cache(struct ref_cache *refs)
+ {
+       if (refs->did_loose)
+               free_ref_array(&refs->loose);
+       refs->did_loose = 0;
+ }
+ static struct ref_cache *create_ref_cache(const char *submodule)
  {
        int len;
-       struct cached_refs *refs;
+       struct ref_cache *refs;
        if (!submodule)
                submodule = "";
        len = strlen(submodule) + 1;
-       refs = xcalloc(1, sizeof(struct cached_refs) + len);
+       refs = xcalloc(1, sizeof(struct ref_cache) + len);
        memcpy(refs->name, submodule, len);
        return refs;
  }
  
  /*
-  * Return a pointer to a cached_refs for the specified submodule. For
+  * Return a pointer to a ref_cache for the specified submodule. For
   * the main repository, use submodule==NULL. The returned structure
   * will be allocated and initialized but not necessarily populated; it
   * should not be freed.
   */
- static struct cached_refs *get_cached_refs(const char *submodule)
+ static struct ref_cache *get_ref_cache(const char *submodule)
  {
-       struct cached_refs *refs = cached_refs;
+       struct ref_cache *refs = ref_cache;
        if (!submodule)
                submodule = "";
        while (refs) {
                refs = refs->next;
        }
  
-       refs = create_cached_refs(submodule);
-       refs->next = cached_refs;
-       cached_refs = refs;
+       refs = create_ref_cache(submodule);
+       refs->next = ref_cache;
+       ref_cache = refs;
        return refs;
  }
  
static void invalidate_cached_refs(void)
void invalidate_ref_cache(const char *submodule)
  {
-       struct cached_refs *refs = cached_refs;
-       while (refs) {
-               clear_cached_refs(refs);
-               refs = refs->next;
-       }
+       struct ref_cache *refs = get_ref_cache(submodule);
+       clear_packed_ref_cache(refs);
+       clear_loose_ref_cache(refs);
  }
  
  static void read_packed_refs(FILE *f, struct ref_array *array)
@@@ -257,7 -260,7 +260,7 @@@ void clear_extra_refs(void
  
  static struct ref_array *get_packed_refs(const char *submodule)
  {
-       struct cached_refs *refs = get_cached_refs(submodule);
+       struct ref_cache *refs = get_ref_cache(submodule);
  
        if (!refs->did_packed) {
                const char *packed_refs_file;
@@@ -379,7 -382,7 +382,7 @@@ void warn_dangling_symref(FILE *fp, con
  
  static struct ref_array *get_loose_refs(const char *submodule)
  {
-       struct cached_refs *refs = get_cached_refs(submodule);
+       struct ref_cache *refs = get_ref_cache(submodule);
  
        if (!refs->did_loose) {
                get_ref_dir(submodule, "refs", &refs->loose);
  #define MAXDEPTH 5
  #define MAXREFLEN (1024)
  
 +/*
 + * Called by resolve_gitlink_ref_recursive() after it failed to read
 + * from "name", which is "module/.git/<refname>". Find <refname> in
 + * the packed-refs file for the submodule.
 + */
  static int resolve_gitlink_packed_ref(char *name, int pathlen, const char *refname, unsigned char *result)
  {
        int retval = -1;
        struct ref_entry *ref;
 -      struct ref_array *array = get_packed_refs(name);
 +      struct ref_array *array;
  
 +      /* being defensive: resolve_gitlink_ref() did this for us */
 +      if (pathlen < 6 || memcmp(name + pathlen - 6, "/.git/", 6))
 +              die("Oops");
 +      name[pathlen - 6] = '\0'; /* make it path to the submodule */
 +      array = get_packed_refs(name);
        ref = search_ref_array(array, refname);
        if (ref != NULL) {
                memcpy(result, ref->sha1, 20);
@@@ -1238,7 -1231,7 +1241,7 @@@ int delete_ref(const char *refname, con
        ret |= repack_without_ref(refname);
  
        unlink_or_warn(git_path("logs/%s", lock->ref_name));
-       invalidate_cached_refs();
+       invalidate_ref_cache(NULL);
        unlock_ref(lock);
        return ret;
  }
  
  int rename_ref(const char *oldref, const char *newref, const char *logmsg)
  {
 -      static const char renamed_ref[] = "RENAMED-REF";
        unsigned char sha1[20], orig_sha1[20];
        int flag = 0, logmoved = 0;
        struct ref_lock *lock;
        if (!is_refname_available(newref, oldref, get_loose_refs(NULL), 0))
                return 1;
  
 -      lock = lock_ref_sha1_basic(renamed_ref, NULL, 0, NULL);
 -      if (!lock)
 -              return error("unable to lock %s", renamed_ref);
 -      lock->force_write = 1;
 -      if (write_ref_sha1(lock, orig_sha1, logmsg))
 -              return error("unable to save current sha1 in %s", renamed_ref);
 -
        if (log && rename(git_path("logs/%s", oldref), git_path(TMP_RENAMED_LOG)))
                return error("unable to move logfile logs/%s to "TMP_RENAMED_LOG": %s",
                        oldref, strerror(errno));
@@@ -1529,7 -1530,7 +1532,7 @@@ int write_ref_sha1(struct ref_lock *loc
                unlock_ref(lock);
                return -1;
        }
-       invalidate_cached_refs();
+       clear_loose_ref_cache(get_ref_cache(NULL));
        if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 ||
            (strcmp(lock->ref_name, lock->orig_ref_name) &&
             log_ref_write(lock->orig_ref_name, lock->old_sha1, sha1, logmsg) < 0)) {