Merge branch 'jk/git-path'
authorJunio C Hamano <gitster@pobox.com>
Wed, 19 Aug 2015 21:48:56 +0000 (14:48 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 19 Aug 2015 21:48:56 +0000 (14:48 -0700)
git_path() and mkpath() are handy helper functions but it is easy
to misuse, as the callers need to be careful to keep the number of
active results below 4. Their uses have been reduced.

* jk/git-path:
memoize common git-path "constant" files
get_repo_path: refactor path-allocation
find_hook: keep our own static buffer
refs.c: remove_empty_directories can take a strbuf
refs.c: avoid git_path assignment in lock_ref_sha1_basic
refs.c: avoid repeated git_path calls in rename_tmp_log
refs.c: simplify strbufs in reflog setup and writing
path.c: drop git_path_submodule
refs.c: remove extra git_path calls from read_loose_refs
remote.c: drop extraneous local variable from migrate_file
prefer mkpathdup to mkpath in assignments
prefer git_pathdup to git_path in some possibly-dangerous cases
add_to_alternates_file: don't add duplicate entries
t5700: modernize style
cache.h: complete set of git_path_submodule helpers
cache.h: clarify documentation for git_path, et al

1  2 
bisect.c
branch.c
builtin/clone.c
builtin/fetch.c
cache.h
dir.c
fast-import.c
refs.c
sha1_file.c
unpack-trees.c
diff --cc bisect.c
Simple merge
diff --cc branch.c
index c85be0785b066380241aa66ff3d57155824fb226,e283683d9d2c5a587f6c3fc54256d67cc9c6c36b..3364adf1821b9a8b9bb0d81e8f1987f611eecdbc
+++ b/branch.c
@@@ -302,78 -302,11 +302,78 @@@ void create_branch(const char *head
  
  void remove_branch_state(void)
  {
-       unlink(git_path("CHERRY_PICK_HEAD"));
-       unlink(git_path("REVERT_HEAD"));
-       unlink(git_path("MERGE_HEAD"));
-       unlink(git_path("MERGE_RR"));
-       unlink(git_path("MERGE_MSG"));
-       unlink(git_path("MERGE_MODE"));
-       unlink(git_path("SQUASH_MSG"));
+       unlink(git_path_cherry_pick_head());
+       unlink(git_path_revert_head());
+       unlink(git_path_merge_head());
+       unlink(git_path_merge_rr());
+       unlink(git_path_merge_msg());
+       unlink(git_path_merge_mode());
+       unlink(git_path_squash_msg());
  }
 +
 +static void check_linked_checkout(const char *branch, const char *id)
 +{
 +      struct strbuf sb = STRBUF_INIT;
 +      struct strbuf path = STRBUF_INIT;
 +      struct strbuf gitdir = STRBUF_INIT;
 +
 +      /*
 +       * $GIT_COMMON_DIR/HEAD is practically outside
 +       * $GIT_DIR so resolve_ref_unsafe() won't work (it
 +       * uses git_path). Parse the ref ourselves.
 +       */
 +      if (id)
 +              strbuf_addf(&path, "%s/worktrees/%s/HEAD", get_git_common_dir(), id);
 +      else
 +              strbuf_addf(&path, "%s/HEAD", get_git_common_dir());
 +
 +      if (!strbuf_readlink(&sb, path.buf, 0)) {
 +              if (!starts_with(sb.buf, "refs/") ||
 +                  check_refname_format(sb.buf, 0))
 +                      goto done;
 +      } else if (strbuf_read_file(&sb, path.buf, 0) >= 0 &&
 +          starts_with(sb.buf, "ref:")) {
 +              strbuf_remove(&sb, 0, strlen("ref:"));
 +              strbuf_trim(&sb);
 +      } else
 +              goto done;
 +      if (strcmp(sb.buf, branch))
 +              goto done;
 +      if (id) {
 +              strbuf_reset(&path);
 +              strbuf_addf(&path, "%s/worktrees/%s/gitdir", get_git_common_dir(), id);
 +              if (strbuf_read_file(&gitdir, path.buf, 0) <= 0)
 +                      goto done;
 +              strbuf_rtrim(&gitdir);
 +      } else
 +              strbuf_addstr(&gitdir, get_git_common_dir());
 +      skip_prefix(branch, "refs/heads/", &branch);
 +      strbuf_strip_suffix(&gitdir, ".git");
 +      die(_("'%s' is already checked out at '%s'"), branch, gitdir.buf);
 +done:
 +      strbuf_release(&path);
 +      strbuf_release(&sb);
 +      strbuf_release(&gitdir);
 +}
 +
 +void die_if_checked_out(const char *branch)
 +{
 +      struct strbuf path = STRBUF_INIT;
 +      DIR *dir;
 +      struct dirent *d;
 +
 +      check_linked_checkout(branch, NULL);
 +
 +      strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
 +      dir = opendir(path.buf);
 +      strbuf_release(&path);
 +      if (!dir)
 +              return;
 +
 +      while ((d = readdir(dir)) != NULL) {
 +              if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
 +                      continue;
 +              check_linked_checkout(branch, d->d_name);
 +      }
 +      closedir(dir);
 +}
diff --cc builtin/clone.c
index 5169746b64888b3f8cf9d4fabd6ea52a45fff4d3,5864ad15e192ceb3d0375d19399356b0256fd46f..578da85254418a620d3c7c847af8eed6a3a29d58
@@@ -144,9 -146,22 +146,22 @@@ static const char *get_repo_path_1(stru
        return NULL;
  }
  
+ static char *get_repo_path(const char *repo, int *is_bundle)
+ {
+       struct strbuf path = STRBUF_INIT;
+       const char *raw;
+       char *canon;
+       strbuf_addstr(&path, repo);
+       raw = get_repo_path_1(&path, is_bundle);
+       canon = raw ? xstrdup(absolute_path(raw)) : NULL;
+       strbuf_release(&path);
+       return canon;
+ }
  static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
  {
 -      const char *end = repo + strlen(repo), *start;
 +      const char *end = repo + strlen(repo), *start, *ptr;
        size_t len;
        char *dir;
  
diff --cc builtin/fetch.c
Simple merge
diff --cc cache.h
Simple merge
diff --cc dir.c
Simple merge
diff --cc fast-import.c
Simple merge
diff --cc refs.c
Simple merge
diff --cc sha1_file.c
Simple merge
diff --cc unpack-trees.c
Simple merge