Merge branch 'rs/ring-buffer-wraparound'
authorJunio C Hamano <gitster@pobox.com>
Thu, 27 Oct 2016 21:58:49 +0000 (14:58 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 27 Oct 2016 21:58:49 +0000 (14:58 -0700)
The code that we have used for the past 10+ years to cycle
4-element ring buffers turns out to be not quite portable in
theoretical world.

* rs/ring-buffer-wraparound:
hex: make wraparound of the index into ring-buffer explicit

1  2 
path.c
diff --combined path.c
index a8e72955f6f28f6e27e7faeee144f4f3ed492c83,9bfaeda2071636ec8a5a5ee32491494f5f83211c..52d889c88ea8483b7a78937804bb1b789d2daa1e
--- 1/path.c
--- 2/path.c
+++ b/path.c
@@@ -6,7 -6,6 +6,7 @@@
  #include "string-list.h"
  #include "dir.h"
  #include "worktree.h"
 +#include "submodule-config.h"
  
  static int get_st_mode_bits(const char *path, int *mode)
  {
@@@ -25,7 -24,8 +25,8 @@@ static struct strbuf *get_pathname(void
                STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
        };
        static int index;
-       struct strbuf *sb = &pathname_array[3 & ++index];
+       struct strbuf *sb = &pathname_array[index];
+       index = (index + 1) % ARRAY_SIZE(pathname_array);
        strbuf_reset(sb);
        return sb;
  }
@@@ -469,16 -469,12 +470,16 @@@ const char *worktree_git_path(const str
        return pathname->buf;
  }
  
 -static void do_submodule_path(struct strbuf *buf, const char *path,
 -                            const char *fmt, va_list args)
 +/* Returns 0 on success, negative on failure. */
 +#define SUBMODULE_PATH_ERR_NOT_CONFIGURED -1
 +static int do_submodule_path(struct strbuf *buf, const char *path,
 +                           const char *fmt, va_list args)
  {
        const char *git_dir;
        struct strbuf git_submodule_common_dir = STRBUF_INIT;
        struct strbuf git_submodule_dir = STRBUF_INIT;
 +      const struct submodule *sub;
 +      int err = 0;
  
        strbuf_addstr(buf, path);
        strbuf_complete(buf, '/');
                strbuf_reset(buf);
                strbuf_addstr(buf, git_dir);
        }
 +      if (!is_git_directory(buf->buf)) {
 +              gitmodules_config();
 +              sub = submodule_from_path(null_sha1, path);
 +              if (!sub) {
 +                      err = SUBMODULE_PATH_ERR_NOT_CONFIGURED;
 +                      goto cleanup;
 +              }
 +              strbuf_reset(buf);
 +              strbuf_git_path(buf, "%s/%s", "modules", sub->name);
 +      }
 +
        strbuf_addch(buf, '/');
        strbuf_addbuf(&git_submodule_dir, buf);
  
  
        strbuf_cleanup_path(buf);
  
 +cleanup:
        strbuf_release(&git_submodule_dir);
        strbuf_release(&git_submodule_common_dir);
 +
 +      return err;
  }
  
  char *git_pathdup_submodule(const char *path, const char *fmt, ...)
  {
 +      int err;
        va_list args;
        struct strbuf buf = STRBUF_INIT;
        va_start(args, fmt);
 -      do_submodule_path(&buf, path, fmt, args);
 +      err = do_submodule_path(&buf, path, fmt, args);
        va_end(args);
 +      if (err) {
 +              strbuf_release(&buf);
 +              return NULL;
 +      }
        return strbuf_detach(&buf, NULL);
  }
  
 -void strbuf_git_path_submodule(struct strbuf *buf, const char *path,
 -                             const char *fmt, ...)
 +int strbuf_git_path_submodule(struct strbuf *buf, const char *path,
 +                            const char *fmt, ...)
  {
 +      int err;
        va_list args;
        va_start(args, fmt);
 -      do_submodule_path(buf, path, fmt, args);
 +      err = do_submodule_path(buf, path, fmt, args);
        va_end(args);
 +
 +      return err;
  }
  
  static void do_git_common_path(struct strbuf *buf,