Merge tag 'v2.12.4' into maint
authorJunio C Hamano <gitster@pobox.com>
Tue, 1 Aug 2017 19:27:31 +0000 (12:27 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 1 Aug 2017 19:27:31 +0000 (12:27 -0700)
1  2 
cache.h
connect.c
path.c
diff --combined cache.h
index 52b91f5b64146e1b6c7c93aa5054a7aca6303b42,1c4686a7aca07bb497e9fc455670b5d5141bee01..c1041cc02b79ea49f265d89be31bf95ff9eff543
+++ b/cache.h
@@@ -10,8 -10,8 +10,8 @@@
  #include "trace.h"
  #include "string-list.h"
  #include "pack-revindex.h"
 +#include "hash.h"
  
 -#include SHA1_HEADER
  #ifndef platform_SHA_CTX
  /*
   * platform's underlying implementation of SHA-1; could be OpenSSL,
@@@ -66,12 -66,8 +66,12 @@@ unsigned long git_deflate_bound(git_zst
  #define GIT_SHA1_RAWSZ 20
  #define GIT_SHA1_HEXSZ (2 * GIT_SHA1_RAWSZ)
  
 +/* The length in byte and in hex digits of the largest possible hash value. */
 +#define GIT_MAX_RAWSZ GIT_SHA1_RAWSZ
 +#define GIT_MAX_HEXSZ GIT_SHA1_HEXSZ
 +
  struct object_id {
 -      unsigned char hash[GIT_SHA1_RAWSZ];
 +      unsigned char hash[GIT_MAX_RAWSZ];
  };
  
  #if defined(DT_UNKNOWN) && !defined(NO_D_TYPE_IN_DIRENT)
@@@ -347,7 -343,6 +347,7 @@@ struct index_state 
  extern struct index_state the_index;
  
  /* Name hashing */
 +extern int test_lazy_init_name_hash(struct index_state *istate, int try_threaded);
  extern void add_name_hash(struct index_state *istate, struct cache_entry *ce);
  extern void remove_name_hash(struct index_state *istate, struct cache_entry *ce);
  extern void free_name_hash(struct index_state *istate);
@@@ -415,7 -410,6 +415,7 @@@ static inline enum object_type object_t
  #define GIT_WORK_TREE_ENVIRONMENT "GIT_WORK_TREE"
  #define GIT_PREFIX_ENVIRONMENT "GIT_PREFIX"
  #define GIT_SUPER_PREFIX_ENVIRONMENT "GIT_INTERNAL_SUPER_PREFIX"
 +#define GIT_TOPLEVEL_PREFIX_ENVIRONMENT "GIT_INTERNAL_TOPLEVEL_PREFIX"
  #define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
  #define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
  #define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
@@@ -524,30 -518,11 +524,30 @@@ extern void set_git_work_tree(const cha
  #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
  
  extern void setup_work_tree(void);
 +/*
 + * Find GIT_DIR of the repository that contains the current working directory,
 + * without changing the working directory or other global state. The result is
 + * appended to gitdir. The return value is either NULL if no repository was
 + * found, or pointing to the path inside gitdir's buffer.
 + */
 +extern const char *discover_git_directory(struct strbuf *gitdir);
  extern const char *setup_git_directory_gently(int *);
  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,
@@@ -599,7 -574,6 +599,7 @@@ extern int write_locked_index(struct in
  extern int discard_index(struct index_state *);
  extern int unmerged_index(const struct index_state *);
  extern int verify_path(const char *path);
 +extern int strcmp_offset(const char *s1, const char *s2, size_t *first_change);
  extern int index_dir_exists(struct index_state *istate, const char *name, int namelen);
  extern void adjust_dirname_case(struct index_state *istate, char *name);
  extern struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int igncase);
@@@ -711,8 -685,6 +711,8 @@@ extern void update_index_if_able(struc
  extern int hold_locked_index(struct lock_file *, int);
  extern void set_alternate_index_output(const char *);
  
 +extern int verify_index_checksum;
 +
  /* Environment bits from configuration mechanism */
  extern int trust_executable_bit;
  extern int trust_ctime;
@@@ -985,7 -957,7 +985,7 @@@ extern char *sha1_pack_index_name(cons
  extern const char *find_unique_abbrev(const unsigned char *sha1, int len);
  extern int find_unique_abbrev_r(char *hex, const unsigned char *sha1, int len);
  
 -extern const unsigned char null_sha1[GIT_SHA1_RAWSZ];
 +extern const unsigned char null_sha1[GIT_MAX_RAWSZ];
  extern const struct object_id null_oid;
  
  static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
@@@ -1097,9 -1069,8 +1097,9 @@@ int adjust_shared_perm(const char *path
  
  /*
   * Create the directory containing the named path, using care to be
 - * somewhat safe against races.  Return one of the scld_error values
 - * to indicate success/failure.
 + * somewhat safe against races. Return one of the scld_error values to
 + * indicate success/failure. On error, set errno to describe the
 + * problem.
   *
   * SCLD_VANISHED indicates that one of the ancestor directories of the
   * path existed at one point during the function call and then
@@@ -1123,51 -1094,8 +1123,51 @@@ enum scld_error 
  enum scld_error safe_create_leading_directories(char *path);
  enum scld_error safe_create_leading_directories_const(const char *path);
  
 +/*
 + * Callback function for raceproof_create_file(). This function is
 + * expected to do something that makes dirname(path) permanent despite
 + * the fact that other processes might be cleaning up empty
 + * directories at the same time. Usually it will create a file named
 + * path, but alternatively it could create another file in that
 + * directory, or even chdir() into that directory. The function should
 + * return 0 if the action was completed successfully. On error, it
 + * should return a nonzero result and set errno.
 + * raceproof_create_file() treats two errno values specially:
 + *
 + * - ENOENT -- dirname(path) does not exist. In this case,
 + *             raceproof_create_file() tries creating dirname(path)
 + *             (and any parent directories, if necessary) and calls
 + *             the function again.
 + *
 + * - EISDIR -- the file already exists and is a directory. In this
 + *             case, raceproof_create_file() removes the directory if
 + *             it is empty (and recursively any empty directories that
 + *             it contains) and calls the function again.
 + *
 + * Any other errno causes raceproof_create_file() to fail with the
 + * callback's return value and errno.
 + *
 + * Obviously, this function should be OK with being called again if it
 + * fails with ENOENT or EISDIR. In other scenarios it will not be
 + * called again.
 + */
 +typedef int create_file_fn(const char *path, void *cb);
 +
 +/*
 + * Create a file in dirname(path) by calling fn, creating leading
 + * directories if necessary. Retry a few times in case we are racing
 + * with another process that is trying to clean up the directory that
 + * contains path. See the documentation for create_file_fn for more
 + * details.
 + *
 + * Return the value and set the errno that resulted from the most
 + * recent call of fn. fn is always called at least once, and will be
 + * called more than once if it returns ENOENT or EISDIR.
 + */
 +int raceproof_create_file(const char *path, create_file_fn fn, void *cb);
 +
  int mkdir_in_gitdir(const char *path);
 -extern char *expand_user_path(const char *path);
 +extern char *expand_user_path(const char *path, int real_home);
  const char *enter_repo(const char *path, int strict);
  static inline int is_absolute_path(const char *path)
  {
@@@ -1190,6 -1118,14 +1190,14 @@@ char *strip_path_suffix(const char *pat
  int daemon_avoid_alias(const char *path);
  extern int is_ntfs_dotgit(const char *name);
  
+ /*
+  * Returns true iff "str" could be confused as a command-line option when
+  * passed to a sub-program like "ssh". Note that this has nothing to do with
+  * shell-quoting, which should be handled separately; we're assuming here that
+  * the string makes it verbatim to the sub-program.
+  */
+ int looks_like_command_line_option(const char *str);
  /**
   * Return a newly allocated string with the evaluation of
   * "$XDG_CONFIG_HOME/git/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise
   */
  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
@@@ -1305,9 -1234,6 +1313,9 @@@ extern int has_pack_index(const unsigne
  
  extern void assert_sha1_type(const unsigned char *sha1, enum object_type expect);
  
 +/* Helper to check and "touch" a file */
 +extern int check_and_freshen_file(const char *fn, int freshen);
 +
  extern const signed char hexval_table[256];
  static inline unsigned int hexval(unsigned char c)
  {
@@@ -1333,18 -1259,13 +1341,18 @@@ static inline int hex2chr(const char *s
  
  struct object_context {
        unsigned char tree[20];
 -      char path[PATH_MAX];
        unsigned mode;
        /*
         * symlink_path is only used by get_tree_entry_follow_symlinks,
         * and only for symlinks that point outside the repository.
         */
        struct strbuf symlink_path;
 +      /*
 +       * If GET_SHA1_RECORD_PATH is set, this will record path (if any)
 +       * found when resolving the name. The caller is responsible for
 +       * releasing the memory.
 +       */
 +      char *path;
  };
  
  #define GET_SHA1_QUIETLY           01
  #define GET_SHA1_TREEISH          020
  #define GET_SHA1_BLOB             040
  #define GET_SHA1_FOLLOW_SYMLINKS 0100
 +#define GET_SHA1_RECORD_PATH     0200
  #define GET_SHA1_ONLY_TO_DIE    04000
  
  #define GET_SHA1_DISAMBIGUATORS \
@@@ -1369,11 -1289,11 +1377,11 @@@ extern int get_sha1_tree(const char *st
  extern int get_sha1_treeish(const char *str, unsigned char *sha1);
  extern int get_sha1_blob(const char *str, unsigned char *sha1);
  extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix);
 -extern int get_sha1_with_context(const char *str, unsigned flags, unsigned char *sha1, struct object_context *orc);
 +extern int get_sha1_with_context(const char *str, unsigned flags, unsigned char *sha1, struct object_context *oc);
  
  extern int get_oid(const char *str, struct object_id *oid);
  
 -typedef int each_abbrev_fn(const unsigned char *sha1, void *);
 +typedef int each_abbrev_fn(const struct object_id *oid, void *);
  extern int for_each_abbrev(const char *prefix, each_abbrev_fn, void *);
  
  extern int set_disambiguate_hint_config(const char *var, const char *value);
@@@ -1404,15 -1324,6 +1412,15 @@@ extern char *oid_to_hex_r(char *out, co
  extern char *sha1_to_hex(const unsigned char *sha1);  /* static buffer result! */
  extern char *oid_to_hex(const struct object_id *oid); /* same static buffer as sha1_to_hex */
  
 +/*
 + * Parse a 40-character hexadecimal object ID starting from hex, updating the
 + * pointer specified by end when parsing stops.  The resulting object ID is
 + * stored in oid.  Returns 0 on success.  Parsing will stop on the first NUL or
 + * other invalid character.  end is only updated on success; otherwise, it is
 + * unmodified.
 + */
 +extern int parse_oid_hex(const char *hex, struct object_id *oid, const char **end);
 +
  /*
   * This reads short-hand syntax that not only evaluates to a commit
   * object name, but also can act as if the end user spelled the name
@@@ -1688,12 -1599,9 +1696,12 @@@ extern struct packed_git *find_sha1_pac
  extern void pack_report(void);
  
  /*
 - * Create a temporary file rooted in the object database directory.
 + * Create a temporary file rooted in the object database directory, or
 + * die on failure. The filename is taken from "pattern", which should have the
 + * usual "XXXXXX" trailer, and the resulting filename is written into the
 + * "template" buffer. Returns the open descriptor.
   */
 -extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
 +extern int odb_mkstemp(struct strbuf *template, const char *pattern);
  
  /*
   * Generate the filename to be used for a pack file with checksum "sha1" and
@@@ -1747,12 -1655,6 +1755,12 @@@ extern void check_pack_index_ptr(const 
   * error.
   */
  extern const unsigned char *nth_packed_object_sha1(struct packed_git *, uint32_t n);
 +/*
 + * Like nth_packed_object_sha1, but write the data into the object specified by
 + * the the first argument.  Returns the first argument on success, and NULL on
 + * error.
 + */
 +extern const struct object_id *nth_packed_object_oid(struct object_id *, struct packed_git *, uint32_t n);
  
  /*
   * Return the offset of the nth object within the specified packfile.
@@@ -1794,7 -1696,7 +1802,7 @@@ extern int unpack_object_header(struct 
   * scratch buffer, but restored to its original contents before
   * the function returns.
   */
 -typedef int each_loose_object_fn(const unsigned char *sha1,
 +typedef int each_loose_object_fn(const struct object_id *oid,
                                 const char *path,
                                 void *data);
  typedef int each_loose_cruft_fn(const char *basename,
@@@ -1820,7 -1722,7 +1828,7 @@@ int for_each_loose_file_in_objdir_buf(s
   * LOCAL_ONLY flag is set).
   */
  #define FOR_EACH_OBJECT_LOCAL_ONLY 0x1
 -typedef int each_packed_object_fn(const unsigned char *sha1,
 +typedef int each_packed_object_fn(const struct object_id *oid,
                                  struct packed_git *pack,
                                  uint32_t pos,
                                  void *data);
@@@ -1898,11 -1800,6 +1906,11 @@@ enum config_origin_type 
        CONFIG_ORIGIN_CMDLINE
  };
  
 +struct config_options {
 +      unsigned int respect_includes : 1;
 +      const char *git_dir;
 +};
 +
  typedef int (*config_fn_t)(const char *, const char *, void *);
  extern int git_default_config(const char *, const char *, void *);
  extern int git_config_from_file(config_fn_t fn, const char *, void *);
@@@ -1912,17 -1809,15 +1920,17 @@@ extern int git_config_from_blob_sha1(co
                                     const unsigned char *sha1, void *data);
  extern void git_config_push_parameter(const char *text);
  extern int git_config_from_parameters(config_fn_t fn, void *data);
 +extern void read_early_config(config_fn_t cb, void *data);
  extern void git_config(config_fn_t fn, void *);
  extern int git_config_with_options(config_fn_t fn, void *,
                                   struct git_config_source *config_source,
 -                                 int respect_includes);
 +                                 const struct config_options *opts);
  extern int git_parse_ulong(const char *, unsigned long *);
  extern int git_parse_maybe_bool(const char *);
  extern int git_config_int(const char *, const char *);
  extern int64_t git_config_int64(const char *, const char *);
  extern unsigned long git_config_ulong(const char *, const char *);
 +extern ssize_t git_config_ssize_t(const char *, const char *);
  extern int git_config_bool_or_int(const char *, const char *, int *);
  extern int git_config_bool(const char *, const char *);
  extern int git_config_maybe_bool(const char *, const char *);
@@@ -1969,7 -1864,6 +1977,7 @@@ struct config_include_data 
        int depth;
        config_fn_t fn;
        void *data;
 +      const struct config_options *opts;
  };
  #define CONFIG_INCLUDE_INIT { 0 }
  extern int git_config_include(const char *name, const char *value, void *data);
@@@ -2047,11 -1941,6 +2055,11 @@@ extern int git_config_get_bool_or_int(c
  extern int git_config_get_maybe_bool(const char *key, int *dest);
  extern int git_config_get_pathname(const char *key, const char **dest);
  extern int git_config_get_untracked_cache(void);
 +extern int git_config_get_split_index(void);
 +extern int git_config_get_max_percent_split_change(void);
 +
 +/* This dies if the configured or default date is in the future */
 +extern int git_config_get_expiry(const char *key, const char **output);
  
  /*
   * This is a hack for test programs like test-dump-untracked-cache to
diff --combined connect.c
index c72b1d1151744c5e7c7b82f453892a1bc9b26021,ea316c485ab3d0329caf6711b1f422ee298fb0b0..e43df44b0cfb6b8eaf09c662c8e631c05568a214
+++ b/connect.c
@@@ -71,7 -71,7 +71,7 @@@ static void parse_one_symref_info(struc
            check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
                /* "symref=bogus:pair */
                goto reject;
 -      item = string_list_append(symref, sym);
 +      item = string_list_append_nodup(symref, sym);
        item->util = target;
        return;
  reject:
@@@ -111,8 -111,8 +111,8 @@@ static void annotate_refs_with_symref_i
   */
  struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
                              struct ref **list, unsigned int flags,
 -                            struct sha1_array *extra_have,
 -                            struct sha1_array *shallow_points)
 +                            struct oid_array *extra_have,
 +                            struct oid_array *shallow_points)
  {
        struct ref **orig_list = list;
  
                                die("protocol error: expected shallow sha-1, got '%s'", arg);
                        if (!shallow_points)
                                die("repository on the other end cannot be shallow");
 -                      sha1_array_append(shallow_points, old_oid.hash);
 +                      oid_array_append(shallow_points, &old_oid);
                        continue;
                }
  
                }
  
                if (extra_have && !strcmp(name, ".have")) {
 -                      sha1_array_append(extra_have, old_oid.hash);
 +                      oid_array_append(extra_have, &old_oid);
                        continue;
                }
  
@@@ -577,6 -577,11 +577,11 @@@ static struct child_process *git_proxy_
  
        get_host_and_port(&host, &port);
  
+       if (looks_like_command_line_option(host))
+               die("strange hostname '%s' blocked", host);
+       if (looks_like_command_line_option(port))
+               die("strange port '%s' blocked", port);
        proxy = xmalloc(sizeof(*proxy));
        child_process_init(proxy);
        argv_array_push(&proxy->args, git_proxy_command);
@@@ -691,70 -696,6 +696,70 @@@ static const char *get_ssh_command(void
        return NULL;
  }
  
 +static int override_ssh_variant(int *port_option, int *needs_batch)
 +{
 +      char *variant;
 +
 +      variant = xstrdup_or_null(getenv("GIT_SSH_VARIANT"));
 +      if (!variant &&
 +          git_config_get_string("ssh.variant", &variant))
 +              return 0;
 +
 +      if (!strcmp(variant, "plink") || !strcmp(variant, "putty")) {
 +              *port_option = 'P';
 +              *needs_batch = 0;
 +      } else if (!strcmp(variant, "tortoiseplink")) {
 +              *port_option = 'P';
 +              *needs_batch = 1;
 +      } else {
 +              *port_option = 'p';
 +              *needs_batch = 0;
 +      }
 +      free(variant);
 +      return 1;
 +}
 +
 +static void handle_ssh_variant(const char *ssh_command, int is_cmdline,
 +                             int *port_option, int *needs_batch)
 +{
 +      const char *variant;
 +      char *p = NULL;
 +
 +      if (override_ssh_variant(port_option, needs_batch))
 +              return;
 +
 +      if (!is_cmdline) {
 +              p = xstrdup(ssh_command);
 +              variant = basename(p);
 +      } else {
 +              const char **ssh_argv;
 +
 +              p = xstrdup(ssh_command);
 +              if (split_cmdline(p, &ssh_argv) > 0) {
 +                      variant = basename((char *)ssh_argv[0]);
 +                      /*
 +                       * At this point, variant points into the buffer
 +                       * referenced by p, hence we do not need ssh_argv
 +                       * any longer.
 +                       */
 +                      free(ssh_argv);
 +              } else {
 +                      free(p);
 +                      return;
 +              }
 +      }
 +
 +      if (!strcasecmp(variant, "plink") ||
 +          !strcasecmp(variant, "plink.exe"))
 +              *port_option = 'P';
 +      else if (!strcasecmp(variant, "tortoiseplink") ||
 +               !strcasecmp(variant, "tortoiseplink.exe")) {
 +              *port_option = 'P';
 +              *needs_batch = 1;
 +      }
 +      free(p);
 +}
 +
  /*
   * This returns a dummy child_process if the transport protocol does not
   * need fork(2), or a struct child_process object if it does.  Once done,
@@@ -823,6 -764,9 +828,9 @@@ struct child_process *git_connect(int f
                conn = xmalloc(sizeof(*conn));
                child_process_init(conn);
  
+               if (looks_like_command_line_option(path))
+                       die("strange pathname '%s' blocked", path);
                strbuf_addstr(&cmd, prog);
                strbuf_addch(&cmd, ' ');
                sq_quote_buf(&cmd, path);
                conn->in = conn->out = -1;
                if (protocol == PROTO_SSH) {
                        const char *ssh;
 -                      int putty = 0, tortoiseplink = 0;
 +                      int needs_batch = 0;
 +                      int port_option = 'p';
                        char *ssh_host = hostandport;
                        const char *port = NULL;
                        transport_check_allowed("ssh");
                                return NULL;
                        }
  
+                       if (looks_like_command_line_option(ssh_host))
+                               die("strange hostname '%s' blocked", ssh_host);
                        ssh = get_ssh_command();
 -                      if (!ssh) {
 -                              const char *base;
 -                              char *ssh_dup;
 -
 +                      if (ssh)
 +                              handle_ssh_variant(ssh, 1, &port_option,
 +                                                 &needs_batch);
 +                      else {
                                /*
                                 * GIT_SSH is the no-shell version of
                                 * GIT_SSH_COMMAND (and must remain so for
                                ssh = getenv("GIT_SSH");
                                if (!ssh)
                                        ssh = "ssh";
 -
 -                              ssh_dup = xstrdup(ssh);
 -                              base = basename(ssh_dup);
 -
 -                              tortoiseplink = !strcasecmp(base, "tortoiseplink") ||
 -                                      !strcasecmp(base, "tortoiseplink.exe");
 -                              putty = tortoiseplink ||
 -                                      !strcasecmp(base, "plink") ||
 -                                      !strcasecmp(base, "plink.exe");
 -
 -                              free(ssh_dup);
 +                              else
 +                                      handle_ssh_variant(ssh, 0,
 +                                                         &port_option,
 +                                                         &needs_batch);
                        }
  
                        argv_array_push(&conn->args, ssh);
                                argv_array_push(&conn->args, "-4");
                        else if (flags & CONNECT_IPV6)
                                argv_array_push(&conn->args, "-6");
 -                      if (tortoiseplink)
 +                      if (needs_batch)
                                argv_array_push(&conn->args, "-batch");
                        if (port) {
 -                              /* P is for PuTTY, p is for OpenSSH */
 -                              argv_array_push(&conn->args, putty ? "-P" : "-p");
 +                              argv_array_pushf(&conn->args,
 +                                               "-%c", port_option);
                                argv_array_push(&conn->args, port);
                        }
                        argv_array_push(&conn->args, ssh_host);
diff --combined path.c
index c1cb1cf6273f4a5a7ed91d495c55a7d283497460,897b3417d40f7b5bd01914b8923f2f3f89350c0b..0349a0ab7b8656130df89ef007f5921797cdab3e
--- 1/path.c
--- 2/path.c
+++ b/path.c
@@@ -471,19 -471,39 +471,19 @@@ const char *worktree_git_path(const str
  }
  
  /* 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;
 +      int ret;
  
 -      strbuf_addstr(buf, path);
 -      strbuf_complete(buf, '/');
 -      strbuf_addstr(buf, ".git");
 -
 -      git_dir = read_gitfile(buf->buf);
 -      if (git_dir) {
 -              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);
 +      ret = submodule_to_gitdir(&git_submodule_dir, path);
 +      if (ret)
 +              goto cleanup;
  
 +      strbuf_complete(&git_submodule_dir, '/');
 +      strbuf_addbuf(buf, &git_submodule_dir);
        strbuf_vaddf(buf, fmt, args);
  
        if (get_common_dir_noenv(&git_submodule_common_dir, git_submodule_dir.buf))
  cleanup:
        strbuf_release(&git_submodule_dir);
        strbuf_release(&git_submodule_common_dir);
 -
 -      return err;
 +      return ret;
  }
  
  char *git_pathdup_submodule(const char *path, const char *fmt, ...)
@@@ -617,10 -638,8 +617,10 @@@ static struct passwd *getpw_str(const c
   * Return a string with ~ and ~user expanded via getpw*.  If buf != NULL,
   * then it is a newly allocated string. Returns NULL on getpw failure or
   * if path is NULL.
 + *
 + * If real_home is true, real_path($HOME) is used in the expansion.
   */
 -char *expand_user_path(const char *path)
 +char *expand_user_path(const char *path, int real_home)
  {
        struct strbuf user_path = STRBUF_INIT;
        const char *to_copy = path;
                        const char *home = getenv("HOME");
                        if (!home)
                                goto return_null;
 -                      strbuf_addstr(&user_path, home);
 +                      if (real_home)
 +                              strbuf_addstr(&user_path, real_path(home));
 +                      else
 +                              strbuf_addstr(&user_path, home);
  #ifdef GIT_WINDOWS_NATIVE
                        convert_slashes(user_path.buf);
  #endif
@@@ -707,7 -723,7 +707,7 @@@ const char *enter_repo(const char *path
                strbuf_add(&validated_path, path, len);
  
                if (used_path.buf[0] == '~') {
 -                      char *newpath = expand_user_path(used_path.buf);
 +                      char *newpath = expand_user_path(used_path.buf, 0);
                        if (!newpath)
                                return NULL;
                        strbuf_attach(&used_path, newpath, strlen(newpath),
@@@ -1241,6 -1257,11 +1241,11 @@@ int is_ntfs_dotgit(const char *name
                }
  }
  
+ int looks_like_command_line_option(const char *str)
+ {
+       return str && str[0] == '-';
+ }
  char *xdg_config_home(const char *filename)
  {
        const char *home, *config_home;
        return NULL;
  }
  
 +char *xdg_cache_home(const char *filename)
 +{
 +      const char *home, *cache_home;
 +
 +      assert(filename);
 +      cache_home = getenv("XDG_CACHE_HOME");
 +      if (cache_home && *cache_home)
 +              return mkpathdup("%s/git/%s", cache_home, filename);
 +
 +      home = getenv("HOME");
 +      if (home)
 +              return mkpathdup("%s/.cache/git/%s", home, filename);
 +      return NULL;
 +}
 +
  GIT_PATH_FUNC(git_path_cherry_pick_head, "CHERRY_PICK_HEAD")
  GIT_PATH_FUNC(git_path_revert_head, "REVERT_HEAD")
  GIT_PATH_FUNC(git_path_squash_msg, "SQUASH_MSG")