Merge branch 'jk/prefix-filename'
authorJunio C Hamano <gitster@pobox.com>
Mon, 27 Mar 2017 17:59:26 +0000 (10:59 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 27 Mar 2017 17:59:26 +0000 (10:59 -0700)
Code clean-up with minor bugfixes.

* jk/prefix-filename:
bundle: use prefix_filename with bundle path
prefix_filename: simplify windows #ifdef
prefix_filename: return newly allocated string
prefix_filename: drop length parameter
prefix_filename: move docstring to header file
hash-object: fix buffer reuse with --path in a subdirectory

1  2 
builtin/rev-parse.c
cache.h
diff --combined builtin/rev-parse.c
index 1e5bdea0d5ba598b4191238fee761295bd8523ab,7cd01c28190560ed3a3e81aa025b5f9f35417858..9e53a1a7ca4a4ffa0bd810673d432c6f2449f5a5
@@@ -228,9 -228,9 +228,9 @@@ static int show_file(const char *arg, i
        if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
                if (output_prefix) {
                        const char *prefix = startup_info->prefix;
-                       show(prefix_filename(prefix,
-                                            prefix ? strlen(prefix) : 0,
-                                            arg));
+                       char *fname = prefix_filename(prefix, arg);
+                       show(fname);
+                       free(fname);
                } else
                        show(arg);
                return 1;
@@@ -536,34 -536,6 +536,34 @@@ N_("git rev-parse --parseopt [<options>
     "\n"
     "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
  
 +/*
 + * Parse "opt" or "opt=<value>", setting value respectively to either
 + * NULL or the string after "=".
 + */
 +static int opt_with_value(const char *arg, const char *opt, const char **value)
 +{
 +      if (skip_prefix(arg, opt, &arg)) {
 +              if (!*arg) {
 +                      *value = NULL;
 +                      return 1;
 +              }
 +              if (*arg++ == '=') {
 +                      *value = arg;
 +                      return 1;
 +              }
 +      }
 +      return 0;
 +}
 +
 +static void handle_ref_opt(const char *pattern, const char *prefix)
 +{
 +      if (pattern)
 +              for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
 +      else
 +              for_each_ref_in(prefix, show_reference, NULL);
 +      clear_ref_exclusion(&ref_excludes);
 +}
 +
  int cmd_rev_parse(int argc, const char **argv, const char *prefix)
  {
        int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
                                flags |= GET_SHA1_QUIETLY;
                                continue;
                        }
 -                      if (!strcmp(arg, "--short") ||
 -                          starts_with(arg, "--short=")) {
 +                      if (opt_with_value(arg, "--short", &arg)) {
                                filter &= ~(DO_FLAGS|DO_NOREV);
                                verify = 1;
                                abbrev = DEFAULT_ABBREV;
 -                              if (!arg[7])
 +                              if (!arg)
                                        continue;
 -                              abbrev = strtoul(arg + 8, NULL, 10);
 +                              abbrev = strtoul(arg, NULL, 10);
                                if (abbrev < MINIMUM_ABBREV)
                                        abbrev = MINIMUM_ABBREV;
                                else if (40 <= abbrev)
                                symbolic = SHOW_SYMBOLIC_FULL;
                                continue;
                        }
 -                      if (starts_with(arg, "--abbrev-ref") &&
 -                          (!arg[12] || arg[12] == '=')) {
 +                      if (opt_with_value(arg, "--abbrev-ref", &arg)) {
                                abbrev_ref = 1;
                                abbrev_ref_strict = warn_ambiguous_refs;
 -                              if (arg[12] == '=') {
 -                                      if (!strcmp(arg + 13, "strict"))
 +                              if (arg) {
 +                                      if (!strcmp(arg, "strict"))
                                                abbrev_ref_strict = 1;
 -                                      else if (!strcmp(arg + 13, "loose"))
 +                                      else if (!strcmp(arg, "loose"))
                                                abbrev_ref_strict = 0;
                                        else
 -                                              die("unknown mode for %s", arg);
 +                                              die("unknown mode for --abbrev-ref: %s",
 +                                                  arg);
                                }
                                continue;
                        }
                                for_each_ref(show_reference, NULL);
                                continue;
                        }
 -                      if (starts_with(arg, "--disambiguate=")) {
 -                              for_each_abbrev(arg + 15, show_abbrev, NULL);
 +                      if (skip_prefix(arg, "--disambiguate=", &arg)) {
 +                              for_each_abbrev(arg, show_abbrev, NULL);
                                continue;
                        }
                        if (!strcmp(arg, "--bisect")) {
                                for_each_ref_in("refs/bisect/good", anti_reference, NULL);
                                continue;
                        }
 -                      if (starts_with(arg, "--branches=")) {
 -                              for_each_glob_ref_in(show_reference, arg + 11,
 -                                      "refs/heads/", NULL);
 -                              clear_ref_exclusion(&ref_excludes);
 -                              continue;
 -                      }
 -                      if (!strcmp(arg, "--branches")) {
 -                              for_each_branch_ref(show_reference, NULL);
 -                              clear_ref_exclusion(&ref_excludes);
 -                              continue;
 -                      }
 -                      if (starts_with(arg, "--tags=")) {
 -                              for_each_glob_ref_in(show_reference, arg + 7,
 -                                      "refs/tags/", NULL);
 -                              clear_ref_exclusion(&ref_excludes);
 -                              continue;
 -                      }
 -                      if (!strcmp(arg, "--tags")) {
 -                              for_each_tag_ref(show_reference, NULL);
 -                              clear_ref_exclusion(&ref_excludes);
 +                      if (opt_with_value(arg, "--branches", &arg)) {
 +                              handle_ref_opt(arg, "refs/heads/");
                                continue;
                        }
 -                      if (starts_with(arg, "--glob=")) {
 -                              for_each_glob_ref(show_reference, arg + 7, NULL);
 -                              clear_ref_exclusion(&ref_excludes);
 +                      if (opt_with_value(arg, "--tags", &arg)) {
 +                              handle_ref_opt(arg, "refs/tags/");
                                continue;
                        }
 -                      if (starts_with(arg, "--remotes=")) {
 -                              for_each_glob_ref_in(show_reference, arg + 10,
 -                                      "refs/remotes/", NULL);
 -                              clear_ref_exclusion(&ref_excludes);
 +                      if (skip_prefix(arg, "--glob=", &arg)) {
 +                              handle_ref_opt(arg, NULL);
                                continue;
                        }
 -                      if (!strcmp(arg, "--remotes")) {
 -                              for_each_remote_ref(show_reference, NULL);
 -                              clear_ref_exclusion(&ref_excludes);
 +                      if (opt_with_value(arg, "--remotes", &arg)) {
 +                              handle_ref_opt(arg, "refs/remotes/");
                                continue;
                        }
 -                      if (starts_with(arg, "--exclude=")) {
 -                              add_ref_exclusion(&ref_excludes, arg + 10);
 +                      if (skip_prefix(arg, "--exclude=", &arg)) {
 +                              add_ref_exclusion(&ref_excludes, arg);
                                continue;
                        }
                        if (!strcmp(arg, "--show-toplevel")) {
                                }
                                continue;
                        }
 -                      if (starts_with(arg, "--since=")) {
 -                              show_datestring("--max-age=", arg+8);
 +                      if (skip_prefix(arg, "--since=", &arg)) {
 +                              show_datestring("--max-age=", arg);
                                continue;
                        }
 -                      if (starts_with(arg, "--after=")) {
 -                              show_datestring("--max-age=", arg+8);
 +                      if (skip_prefix(arg, "--after=", &arg)) {
 +                              show_datestring("--max-age=", arg);
                                continue;
                        }
 -                      if (starts_with(arg, "--before=")) {
 -                              show_datestring("--min-age=", arg+9);
 +                      if (skip_prefix(arg, "--before=", &arg)) {
 +                              show_datestring("--min-age=", arg);
                                continue;
                        }
 -                      if (starts_with(arg, "--until=")) {
 -                              show_datestring("--min-age=", arg+8);
 +                      if (skip_prefix(arg, "--until=", &arg)) {
 +                              show_datestring("--min-age=", arg);
                                continue;
                        }
                        if (show_flag(arg) && verify)
diff --combined cache.h
index 2214d52f6127f20ad9788402b0e448977db615c5,aa6a0fb91a9ccf8ba8c2415405d2f4f1a650033a..db4120c233535e363cb9c368056ee83bf0b113db
+++ b/cache.h
@@@ -529,7 -529,19 +529,19 @@@ extern const char *setup_git_directory_
  extern const char *setup_git_directory(void);
  extern char *prefix_path(const char *prefix, int len, const char *path);
  extern char *prefix_path_gently(const char *prefix, int len, int *remaining, const char *path);
- extern const char *prefix_filename(const char *prefix, int len, const char *path);
+ /*
+  * Concatenate "prefix" (if len is non-zero) and "path", with no
+  * connecting characters (so "prefix" should end with a "/").
+  * Unlike prefix_path, this should be used if the named file does
+  * not have to interact with index entry; i.e. name of a random file
+  * on the filesystem.
+  *
+  * The return value is always a newly allocated string (even if the
+  * prefix was empty).
+  */
+ extern char *prefix_filename(const char *prefix, const char *path);
  extern int check_filename(const char *prefix, const char *name);
  extern void verify_filename(const char *prefix,
                            const char *name,
@@@ -1176,13 -1188,6 +1188,13 @@@ extern int is_ntfs_dotgit(const char *n
   */
  extern char *xdg_config_home(const char *filename);
  
 +/**
 + * Return a newly allocated string with the evaluation of
 + * "$XDG_CACHE_HOME/git/$filename" if $XDG_CACHE_HOME is non-empty, otherwise
 + * "$HOME/.cache/git/$filename". Return NULL upon error.
 + */
 +extern char *xdg_cache_home(const char *filename);
 +
  /* object replacement */
  #define LOOKUP_REPLACE_OBJECT 1
  #define LOOKUP_UNKNOWN_OBJECT 2
@@@ -1660,27 -1665,6 +1672,27 @@@ extern struct packed_git *find_sha1_pac
  
  extern void pack_report(void);
  
 +/*
 + * Create a temporary file rooted in the object database directory.
 + */
 +extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
 +
 +/*
 + * Generate the filename to be used for a pack file with checksum "sha1" and
 + * extension "ext". The result is written into the strbuf "buf", overwriting
 + * any existing contents. A pointer to buf->buf is returned as a convenience.
 + *
 + * Example: odb_pack_name(out, sha1, "idx") => ".git/objects/pack/pack-1234..idx"
 + */
 +extern char *odb_pack_name(struct strbuf *buf, const unsigned char *sha1, const char *ext);
 +
 +/*
 + * Create a pack .keep file named "name" (which should generally be the output
 + * of odb_pack_name). Returns a file descriptor opened for writing, or -1 on
 + * error.
 + */
 +extern int odb_pack_keep(const char *name);
 +
  /*
   * mmap the index file for the specified packfile (if it is not
   * already mmapped).  Return 0 on success.