From 0f64bfa9567f697d6152ef9ea937d4b573029a08 Mon Sep 17 00:00:00 2001 From: Clemens Buchacher Date: Mon, 1 Aug 2011 23:19:58 +0200 Subject: [PATCH 01/13] ls-files: fix pathspec display on error The following sequence of commands reveals an issue with error reporting of relative paths: $ mkdir sub $ cd sub $ git ls-files --error-unmatch ../bbbbb error: pathspec 'b' did not match any file(s) known to git. $ git commit --error-unmatch ../bbbbb error: pathspec 'b' did not match any file(s) known to git. This bug is visible only if the normalized path (i.e., the relative path from the repository root) is longer than the prefix. Otherwise, the code skips over the normalized path and reads from an unused memory location which still contains a leftover of the original command line argument. So instead, use the existing facilities to deal with relative paths correctly. Also fix inconsistency between "checkout" and "commit", e.g. $ cd Documentation $ git checkout nosuch.txt error: pathspec 'Documentation/nosuch.txt' did not match... $ git commit nosuch.txt error: pathspec 'nosuch.txt' did not match... by propagating the prefix down the codepath that reports the error. Signed-off-by: Clemens Buchacher Signed-off-by: Junio C Hamano --- builtin/checkout.c | 6 ++-- builtin/commit.c | 2 +- builtin/ls-files.c | 10 ++++-- cache.h | 2 +- quote.c | 8 +++-- t/t3005-ls-files-relative.sh | 70 ++++++++++++++++++++++++++++++++++++ 6 files changed, 88 insertions(+), 10 deletions(-) create mode 100755 t/t3005-ls-files-relative.sh diff --git a/builtin/checkout.c b/builtin/checkout.c index 28cdc51b85e7d4..a5717f1637d9a0 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -201,7 +201,7 @@ static int checkout_merged(int pos, struct checkout *state) } static int checkout_paths(struct tree *source_tree, const char **pathspec, - struct checkout_opts *opts) + const char *prefix, struct checkout_opts *opts) { int pos; struct checkout state; @@ -231,7 +231,7 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec, match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, ps_matched); } - if (report_path_error(ps_matched, pathspec, 0)) + if (report_path_error(ps_matched, pathspec, prefix)) return 1; /* "checkout -m path" to recreate conflicted state */ @@ -1060,7 +1060,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix) if (1 < !!opts.writeout_stage + !!opts.force + !!opts.merge) die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\nchecking out of the index.")); - return checkout_paths(source_tree, pathspec, &opts); + return checkout_paths(source_tree, pathspec, prefix, &opts); } if (patch_mode) diff --git a/builtin/commit.c b/builtin/commit.c index e1af9b19f0be71..9679a99f990258 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -272,7 +272,7 @@ static int list_paths(struct string_list *list, const char *with_tree, item->util = item; /* better a valid pointer than a fake one */ } - return report_path_error(m, pattern, prefix ? strlen(prefix) : 0); + return report_path_error(m, pattern, prefix); } static void add_remove_files(struct string_list *list) diff --git a/builtin/ls-files.c b/builtin/ls-files.c index 15701233e29b24..468bb13c81b960 100644 --- a/builtin/ls-files.c +++ b/builtin/ls-files.c @@ -388,11 +388,13 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix) } } -int report_path_error(const char *ps_matched, const char **pathspec, int prefix_len) +int report_path_error(const char *ps_matched, const char **pathspec, const char *prefix) { /* * Make sure all pathspec matched; otherwise it is an error. */ + struct strbuf sb = STRBUF_INIT; + const char *name; int num, errors = 0; for (num = 0; pathspec[num]; num++) { int other, found_dup; @@ -417,10 +419,12 @@ int report_path_error(const char *ps_matched, const char **pathspec, int prefix_ if (found_dup) continue; + name = quote_path_relative(pathspec[num], -1, &sb, prefix); error("pathspec '%s' did not match any file(s) known to git.", - pathspec[num] + prefix_len); + name); errors++; } + strbuf_release(&sb); return errors; } @@ -611,7 +615,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix) if (ps_matched) { int bad; - bad = report_path_error(ps_matched, pathspec, prefix_len); + bad = report_path_error(ps_matched, pathspec, prefix); if (bad) fprintf(stderr, "Did you forget to 'git add'?\n"); diff --git a/cache.h b/cache.h index e11cf6ab1c73ac..d55a6bbe37a1c2 100644 --- a/cache.h +++ b/cache.h @@ -1175,7 +1175,7 @@ extern int ws_blank_line(const char *line, int len, unsigned ws_rule); #define ws_tab_width(rule) ((rule) & WS_TAB_WIDTH_MASK) /* ls-files */ -int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset); +int report_path_error(const char *ps_matched, const char **pathspec, const char *prefix); void overlay_tree_on_cache(const char *tree_name, const char *prefix); char *alias_lookup(const char *alias); diff --git a/quote.c b/quote.c index 63d3b018183abc..532fd3b7b7a0c7 100644 --- a/quote.c +++ b/quote.c @@ -325,8 +325,12 @@ static const char *path_relative(const char *in, int len, if (len < 0) len = strlen(in); - if (prefix && prefix_len < 0) - prefix_len = strlen(prefix); + if (prefix_len < 0) { + if (prefix) + prefix_len = strlen(prefix); + else + prefix_len = 0; + } off = 0; i = 0; diff --git a/t/t3005-ls-files-relative.sh b/t/t3005-ls-files-relative.sh new file mode 100755 index 00000000000000..a2b63e2c1066f1 --- /dev/null +++ b/t/t3005-ls-files-relative.sh @@ -0,0 +1,70 @@ +#!/bin/sh + +test_description='ls-files tests with relative paths + +This test runs git ls-files with various relative path arguments. +' + +. ./test-lib.sh + +new_line=' +' +sq=\' + +test_expect_success 'prepare' ' + : >never-mind-me && + git add never-mind-me && + mkdir top && + ( + cd top && + mkdir sub && + x="x xa xbc xdef xghij xklmno" && + y=$(echo "$x" | tr x y) && + touch $x && + touch $y && + cd sub && + git add ../x* + ) +' + +test_expect_success 'ls-files with mixed levels' ' + ( + cd top/sub && + cat >expect <<-EOF && + ../../never-mind-me + ../x + EOF + git ls-files $(cat expect) >actual && + test_cmp expect actual + ) +' + +test_expect_success 'ls-files -c' ' + ( + cd top/sub && + for f in ../y* + do + echo "error: pathspec $sq$f$sq did not match any file(s) known to git." + done >expect && + echo "Did you forget to ${sq}git add${sq}?" >>expect && + ls ../x* >>expect && + test_must_fail git ls-files -c --error-unmatch ../[xy]* >actual 2>&1 && + test_cmp expect actual + ) +' + +test_expect_success 'ls-files -o' ' + ( + cd top/sub && + for f in ../x* + do + echo "error: pathspec $sq$f$sq did not match any file(s) known to git." + done >expect && + echo "Did you forget to ${sq}git add${sq}?" >>expect && + ls ../y* >>expect && + test_must_fail git ls-files -o --error-unmatch ../[xy]* >actual 2>&1 && + test_cmp expect actual + ) +' + +test_done From e9d4f7405b6ab399ef0ea62a796c646de676555d Mon Sep 17 00:00:00 2001 From: Michael J Gruber Date: Fri, 19 Aug 2011 13:45:43 +0200 Subject: [PATCH 02/13] branch.c: use the parsed branch name When setting up tracking info, branch.c uses the given branch specifier ("name"). Use the parsed name ("ref.buf") instead so that git branch --set-upstream @{-1} foo sets up tracking info for the previous branch rather than for a branch named "@{-1}". Signed-off-by: Michael J Gruber Signed-off-by: Junio C Hamano --- branch.c | 2 +- t/t6040-tracking-info.sh | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/branch.c b/branch.c index c0c865a4b1b0fc..d62cc0132cbff6 100644 --- a/branch.c +++ b/branch.c @@ -210,7 +210,7 @@ void create_branch(const char *head, start_name); if (real_ref && track) - setup_tracking(name, real_ref, track); + setup_tracking(ref.buf+11, real_ref, track); if (!dont_change_ref) if (write_ref_sha1(lock, sha1, msg) < 0) diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh index a9b0ac1efc0eac..19de5b16eb530d 100755 --- a/t/t6040-tracking-info.sh +++ b/t/t6040-tracking-info.sh @@ -110,4 +110,18 @@ test_expect_success '--set-upstream does not change branch' ' grep -q "^refs/heads/master$" actual && cmp expect2 actual2 ' + +test_expect_success '--set-upstream @{-1}' ' + git checkout from-master && + git checkout from-master2 && + git config branch.from-master2.merge > expect2 && + git branch --set-upstream @{-1} follower && + git config branch.from-master.merge > actual && + git config branch.from-master2.merge > actual2 && + git branch --set-upstream from-master follower && + git config branch.from-master.merge > expect && + test_cmp expect2 actual2 && + test_cmp expect actual +' + test_done From 13d6ec913330ba90fb1a67ec5789e437f8ac4193 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 22 Aug 2011 14:04:56 -0700 Subject: [PATCH 03/13] read_gitfile_gently(): rename misnamed function to read_gitfile() The function was not gentle at all to the callers and died without giving them a chance to deal with possible errors. Rename it to read_gitfile(), and update all the callers. As no existing caller needs a true "gently" variant, we do not bother adding one at this point. Signed-off-by: Junio C Hamano --- builtin/init-db.c | 2 +- cache.h | 2 +- environment.c | 2 +- path.c | 2 +- refs.c | 2 +- setup.c | 6 +++--- submodule.c | 6 +++--- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/builtin/init-db.c b/builtin/init-db.c index 025aa47c804e44..d07554c8844a9b 100644 --- a/builtin/init-db.c +++ b/builtin/init-db.c @@ -347,7 +347,7 @@ static void separate_git_dir(const char *git_dir) const char *src; if (S_ISREG(st.st_mode)) - src = read_gitfile_gently(git_link); + src = read_gitfile(git_link); else if (S_ISDIR(st.st_mode)) src = git_link; else diff --git a/cache.h b/cache.h index e11cf6ab1c73ac..1abf71505007a9 100644 --- a/cache.h +++ b/cache.h @@ -420,7 +420,7 @@ extern char *get_index_file(void); extern char *get_graft_file(void); extern int set_git_dir(const char *path); extern const char *get_git_work_tree(void); -extern const char *read_gitfile_gently(const char *path); +extern const char *read_gitfile(const char *path); extern void set_git_work_tree(const char *tree); #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES" diff --git a/environment.c b/environment.c index 94d58fd24413ec..2228c4e9a14ed6 100644 --- a/environment.c +++ b/environment.c @@ -91,7 +91,7 @@ static void setup_git_env(void) git_dir = getenv(GIT_DIR_ENVIRONMENT); git_dir = git_dir ? xstrdup(git_dir) : NULL; if (!git_dir) { - git_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT); + git_dir = read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT); git_dir = git_dir ? xstrdup(git_dir) : NULL; } if (!git_dir) diff --git a/path.c b/path.c index 4d73cc9cd26708..6f3f5d56c0ed76 100644 --- a/path.c +++ b/path.c @@ -139,7 +139,7 @@ char *git_path_submodule(const char *path, const char *fmt, ...) strbuf_addch(&buf, '/'); strbuf_addstr(&buf, ".git"); - git_dir = read_gitfile_gently(buf.buf); + git_dir = read_gitfile(buf.buf); if (git_dir) { strbuf_reset(&buf); strbuf_addstr(&buf, git_dir); diff --git a/refs.c b/refs.c index b10419a69815ef..c98c006e7a0270 100644 --- a/refs.c +++ b/refs.c @@ -451,7 +451,7 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *re memcpy(gitdir + len, "/.git", 6); len += 5; - tmp = read_gitfile_gently(gitdir); + tmp = read_gitfile(gitdir); if (tmp) { free(gitdir); len = strlen(tmp); diff --git a/setup.c b/setup.c index ce87900ce3c68a..1b06db53fc24f1 100644 --- a/setup.c +++ b/setup.c @@ -375,7 +375,7 @@ static int check_repository_format_gently(const char *gitdir, int *nongit_ok) * Try to read the location of the git directory from the .git file, * return path to git directory if found. */ -const char *read_gitfile_gently(const char *path) +const char *read_gitfile(const char *path) { char *buf; char *dir; @@ -437,7 +437,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv, if (PATH_MAX - 40 < strlen(gitdirenv)) die("'$%s' too big", GIT_DIR_ENVIRONMENT); - gitfile = (char*)read_gitfile_gently(gitdirenv); + gitfile = (char*)read_gitfile(gitdirenv); if (gitfile) { gitfile = xstrdup(gitfile); gitdirenv = gitfile; @@ -661,7 +661,7 @@ static const char *setup_git_directory_gently_1(int *nongit_ok) if (one_filesystem) current_device = get_device_or_die(".", NULL); for (;;) { - gitfile = (char*)read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT); + gitfile = (char*)read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT); if (gitfile) gitdirenv = gitfile = xstrdup(gitfile); else { diff --git a/submodule.c b/submodule.c index b6dec70bd1a6b3..b8b0326c5090f5 100644 --- a/submodule.c +++ b/submodule.c @@ -32,7 +32,7 @@ static int add_submodule_odb(const char *path) const char *git_dir; strbuf_addf(&objects_directory, "%s/.git", path); - git_dir = read_gitfile_gently(objects_directory.buf); + git_dir = read_gitfile(objects_directory.buf); if (git_dir) { strbuf_reset(&objects_directory); strbuf_addstr(&objects_directory, git_dir); @@ -478,7 +478,7 @@ int fetch_populated_submodules(int num_options, const char **options, strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name); strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf); strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name); - git_dir = read_gitfile_gently(submodule_git_dir.buf); + git_dir = read_gitfile(submodule_git_dir.buf); if (!git_dir) git_dir = submodule_git_dir.buf; if (is_directory(git_dir)) { @@ -516,7 +516,7 @@ unsigned is_submodule_modified(const char *path, int ignore_untracked) const char *git_dir; strbuf_addf(&buf, "%s/.git", path); - git_dir = read_gitfile_gently(buf.buf); + git_dir = read_gitfile(buf.buf); if (!git_dir) git_dir = buf.buf; if (!is_directory(git_dir)) { From 9b0ebc722cfc12bd14934aab5cf77ebe654e36e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 21 Aug 2011 18:58:09 +0700 Subject: [PATCH 04/13] clone: allow to clone from .git file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- builtin/clone.c | 19 ++++++++++++++++++- t/t5601-clone.sh | 4 ++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/builtin/clone.c b/builtin/clone.c index f579794d9a93a0..ec57f3dbe4a629 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -101,9 +101,26 @@ static char *get_repo_path(const char *repo, int *is_bundle) for (i = 0; i < ARRAY_SIZE(suffix); i++) { const char *path; path = mkpath("%s%s", repo, suffix[i]); - if (is_directory(path)) { + if (stat(path, &st)) + continue; + if (S_ISDIR(st.st_mode)) { *is_bundle = 0; return xstrdup(absolute_path(path)); + } else if (S_ISREG(st.st_mode) && st.st_size > 8) { + /* Is it a "gitfile"? */ + char signature[8]; + int len, fd = open(path, O_RDONLY); + if (fd < 0) + continue; + len = read_in_full(fd, signature, 8); + close(fd); + if (len != 8 || strncmp(signature, "gitdir: ", 8)) + continue; + path = read_gitfile(path); + if (path) { + *is_bundle = 0; + return xstrdup(absolute_path(path)); + } } } diff --git a/t/t5601-clone.sh b/t/t5601-clone.sh index 151ea531bdfeb5..501bd3fb6cc9c8 100755 --- a/t/t5601-clone.sh +++ b/t/t5601-clone.sh @@ -202,6 +202,10 @@ test_expect_success 'clone separate gitdir: output' ' test_cmp expected dst/.git ' +test_expect_success 'clone from .git file' ' + git clone dst/.git dst2 +' + test_expect_success 'clone separate gitdir where target already exists' ' rm -rf dst && test_must_fail git clone --separate-git-dir realgitdir src dst From dbc92b072dd7023f8ba1f682a8060022fc72504a Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 22 Aug 2011 18:05:15 -0700 Subject: [PATCH 05/13] clone: allow more than one --reference Also add a test to expose a long-standing bug that is triggered when cloning with --reference option from a local repository that has its own alternates. The alternate object stores specified on the command line are lost, and only alternates copied from the source repository remain. The bug will be fixed in the next patch. Signed-off-by: Junio C Hamano --- builtin/clone.c | 32 ++++++++++++++++++++++++-------- t/t5601-clone.sh | 15 +++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/builtin/clone.c b/builtin/clone.c index f579794d9a93a0..ee5d6518c8f8ad 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -39,13 +39,23 @@ static const char * const builtin_clone_usage[] = { static int option_no_checkout, option_bare, option_mirror; static int option_local, option_no_hardlinks, option_shared, option_recursive; -static char *option_template, *option_reference, *option_depth; +static char *option_template, *option_depth; static char *option_origin = NULL; static char *option_branch = NULL; static const char *real_git_dir; static char *option_upload_pack = "git-upload-pack"; static int option_verbosity; static int option_progress; +static struct string_list option_reference; + +static int opt_parse_reference(const struct option *opt, const char *arg, int unset) +{ + struct string_list *option_reference = opt->value; + if (!arg) + return -1; + string_list_append(option_reference, arg); + return 0; +} static struct option builtin_clone_options[] = { OPT__VERBOSITY(&option_verbosity), @@ -71,8 +81,8 @@ static struct option builtin_clone_options[] = { "initialize submodules in the clone"), OPT_STRING(0, "template", &option_template, "template-directory", "directory from which templates will be used"), - OPT_STRING(0, "reference", &option_reference, "repo", - "reference repository"), + OPT_CALLBACK(0 , "reference", &option_reference, "repo", + "reference repository", &opt_parse_reference), OPT_STRING('o', "origin", &option_origin, "branch", "use instead of 'origin' to track upstream"), OPT_STRING('b', "branch", &option_branch, "branch", @@ -197,7 +207,7 @@ static void strip_trailing_slashes(char *dir) *end = '\0'; } -static void setup_reference(const char *repo) +static int add_one_reference(struct string_list_item *item, void *cb_data) { const char *ref_git; char *ref_git_copy; @@ -206,13 +216,13 @@ static void setup_reference(const char *repo) struct transport *transport; const struct ref *extra; - ref_git = real_path(option_reference); + ref_git = real_path(item->string); if (is_directory(mkpath("%s/.git/objects", ref_git))) ref_git = mkpath("%s/.git", ref_git); else if (!is_directory(mkpath("%s/objects", ref_git))) die(_("reference repository '%s' is not a local directory."), - option_reference); + item->string); ref_git_copy = xstrdup(ref_git); @@ -227,6 +237,12 @@ static void setup_reference(const char *repo) transport_disconnect(transport); free(ref_git_copy); + return 0; +} + +static void setup_reference(void) +{ + for_each_string_list(&option_reference, add_one_reference, NULL); } static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest) @@ -521,8 +537,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix) git_config_set(key.buf, repo); strbuf_reset(&key); - if (option_reference) - setup_reference(git_dir); + if (option_reference.nr) + setup_reference(); fetch_pattern = value.buf; refspec = parse_fetch_refspec(1, &fetch_pattern); diff --git a/t/t5601-clone.sh b/t/t5601-clone.sh index 151ea531bdfeb5..0163ad1e211af8 100755 --- a/t/t5601-clone.sh +++ b/t/t5601-clone.sh @@ -207,4 +207,19 @@ test_expect_success 'clone separate gitdir where target already exists' ' test_must_fail git clone --separate-git-dir realgitdir src dst ' +test_expect_failure 'clone --reference from original' ' + git clone --shared --bare src src-1 && + git clone --bare src src-2 && + git clone --reference=src-2 --bare src-1 target-8 && + grep /src-2/ target-8/objects/info/alternates +' + +test_expect_success 'clone with more than one --reference' ' + git clone --bare src src-3 && + git clone --bare src src-4 && + git clone --reference=src-3 --reference=src-4 src target-9 && + grep /src-3/ target-9/.git/objects/info/alternates && + grep /src-4/ target-9/.git/objects/info/alternates +' + test_done From e6baf4a1ae1bb75d59967066ade1290cf105dcd8 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 22 Aug 2011 18:05:16 -0700 Subject: [PATCH 06/13] clone: clone from a repository with relative alternates Cloning from a local repository blindly copies or hardlinks all the files under objects/ hierarchy. This results in two issues: - If the repository cloned has an "objects/info/alternates" file, and the command line of clone specifies --reference, the ones specified on the command line get overwritten by the copy from the original repository. - An entry in a "objects/info/alternates" file can specify the object stores it borrows objects from as a path relative to the "objects/" directory. When cloning a repository with such an alternates file, if the new repository is not sitting next to the original repository, such relative paths needs to be adjusted so that they can be used in the new repository. This updates add_to_alternates_file() to take the path to the alternate object store, including the "/objects" part at the end (earlier, it was taking the path to $GIT_DIR and was adding "/objects" itself), as it is technically possible to specify in objects/info/alternates file the path of a directory whose name does not end with "/objects". Signed-off-by: Junio C Hamano --- builtin/clone.c | 91 ++++++++++++++++++++++++++++++++++++------------ sha1_file.c | 2 +- t/t5601-clone.sh | 10 +++++- 3 files changed, 78 insertions(+), 25 deletions(-) diff --git a/builtin/clone.c b/builtin/clone.c index ee5d6518c8f8ad..16b4fba71f8fed 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -209,34 +209,34 @@ static void strip_trailing_slashes(char *dir) static int add_one_reference(struct string_list_item *item, void *cb_data) { - const char *ref_git; - char *ref_git_copy; - + char *ref_git; + struct strbuf alternate = STRBUF_INIT; struct remote *remote; struct transport *transport; const struct ref *extra; - ref_git = real_path(item->string); - - if (is_directory(mkpath("%s/.git/objects", ref_git))) - ref_git = mkpath("%s/.git", ref_git); - else if (!is_directory(mkpath("%s/objects", ref_git))) + /* Beware: real_path() and mkpath() return static buffer */ + ref_git = xstrdup(real_path(item->string)); + if (is_directory(mkpath("%s/.git/objects", ref_git))) { + char *ref_git_git = xstrdup(mkpath("%s/.git", ref_git)); + free(ref_git); + ref_git = ref_git_git; + } else if (!is_directory(mkpath("%s/objects", ref_git))) die(_("reference repository '%s' is not a local directory."), item->string); - ref_git_copy = xstrdup(ref_git); - - add_to_alternates_file(ref_git_copy); + strbuf_addf(&alternate, "%s/objects", ref_git); + add_to_alternates_file(alternate.buf); + strbuf_release(&alternate); - remote = remote_get(ref_git_copy); - transport = transport_get(remote, ref_git_copy); + remote = remote_get(ref_git); + transport = transport_get(remote, ref_git); for (extra = transport_get_remote_refs(transport); extra; extra = extra->next) add_extra_ref(extra->name, extra->old_sha1, 0); transport_disconnect(transport); - - free(ref_git_copy); + free(ref_git); return 0; } @@ -245,7 +245,42 @@ static void setup_reference(void) for_each_string_list(&option_reference, add_one_reference, NULL); } -static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest) +static void copy_alternates(struct strbuf *src, struct strbuf *dst, + const char *src_repo) +{ + /* + * Read from the source objects/info/alternates file + * and copy the entries to corresponding file in the + * destination repository with add_to_alternates_file(). + * Both src and dst have "$path/objects/info/alternates". + * + * Instead of copying bit-for-bit from the original, + * we need to append to existing one so that the already + * created entry via "clone -s" is not lost, and also + * to turn entries with paths relative to the original + * absolute, so that they can be used in the new repository. + */ + FILE *in = fopen(src->buf, "r"); + struct strbuf line = STRBUF_INIT; + + while (strbuf_getline(&line, in, '\n') != EOF) { + char *abs_path, abs_buf[PATH_MAX]; + if (!line.len || line.buf[0] == '#') + continue; + if (is_absolute_path(line.buf)) { + add_to_alternates_file(line.buf); + continue; + } + abs_path = mkpath("%s/objects/%s", src_repo, line.buf); + normalize_path_copy(abs_buf, abs_path); + add_to_alternates_file(abs_buf); + } + strbuf_release(&line); + fclose(in); +} + +static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest, + const char *src_repo, int src_baselen) { struct dirent *de; struct stat buf; @@ -281,7 +316,14 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest) } if (S_ISDIR(buf.st_mode)) { if (de->d_name[0] != '.') - copy_or_link_directory(src, dest); + copy_or_link_directory(src, dest, + src_repo, src_baselen); + continue; + } + + /* Files that cannot be copied bit-for-bit... */ + if (!strcmp(src->buf + src_baselen, "/info/alternates")) { + copy_alternates(src, dest, src_repo); continue; } @@ -304,17 +346,20 @@ static const struct ref *clone_local(const char *src_repo, const char *dest_repo) { const struct ref *ret; - struct strbuf src = STRBUF_INIT; - struct strbuf dest = STRBUF_INIT; struct remote *remote; struct transport *transport; - if (option_shared) - add_to_alternates_file(src_repo); - else { + if (option_shared) { + struct strbuf alt = STRBUF_INIT; + strbuf_addf(&alt, "%s/objects", src_repo); + add_to_alternates_file(alt.buf); + strbuf_release(&alt); + } else { + struct strbuf src = STRBUF_INIT; + struct strbuf dest = STRBUF_INIT; strbuf_addf(&src, "%s/objects", src_repo); strbuf_addf(&dest, "%s/objects", dest_repo); - copy_or_link_directory(&src, &dest); + copy_or_link_directory(&src, &dest, src_repo, src.len); strbuf_release(&src); strbuf_release(&dest); } diff --git a/sha1_file.c b/sha1_file.c index 064a33040812ba..f7c3408de61ce4 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -380,7 +380,7 @@ void add_to_alternates_file(const char *reference) { struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); int fd = hold_lock_file_for_append(lock, git_path("objects/info/alternates"), LOCK_DIE_ON_ERROR); - char *alt = mkpath("%s/objects\n", reference); + char *alt = mkpath("%s\n", reference); write_or_die(fd, alt, strlen(alt)); if (commit_lock_file(lock)) die("could not close alternates file"); diff --git a/t/t5601-clone.sh b/t/t5601-clone.sh index 0163ad1e211af8..d87214cfbf95d4 100755 --- a/t/t5601-clone.sh +++ b/t/t5601-clone.sh @@ -207,7 +207,7 @@ test_expect_success 'clone separate gitdir where target already exists' ' test_must_fail git clone --separate-git-dir realgitdir src dst ' -test_expect_failure 'clone --reference from original' ' +test_expect_success 'clone --reference from original' ' git clone --shared --bare src src-1 && git clone --bare src src-2 && git clone --reference=src-2 --bare src-1 target-8 && @@ -222,4 +222,12 @@ test_expect_success 'clone with more than one --reference' ' grep /src-4/ target-9/.git/objects/info/alternates ' +test_expect_success 'clone from original with relative alternate' ' + mkdir nest && + git clone --bare src nest/src-5 && + echo ../../../src/.git/objects >nest/src-5/objects/info/alternates && + git clone --bare nest/src-5 target-10 && + grep /src/\\.git/objects target-10/objects/info/alternates +' + test_done From 2f633f41d69527cdd9ff5b8e04a752f1774fc3df Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Thu, 25 Aug 2011 21:19:24 +0200 Subject: [PATCH 07/13] check-ref-format --print: Normalize refnames that start with slashes When asked if "refs///heads/master" is valid, check-ref-format says "Yes, it is well formed", and when asked to print canonical form, it shows "refs/heads/master". This is so that it can be tucked after "$GIT_DIR/" to form a valid pathname for a loose ref, and we normalize a pathname like "$GIT_DIR/refs///heads/master" to de-dup the slashes in it. Similarly, when asked if "/refs/heads/master" is valid, check-ref-format says "Yes, it is Ok", but the leading slash is not removed when printing, leading to "$GIT_DIR//refs/heads/master". Fix it to make sure such leading slashes are removed. Add tests that such refnames are accepted and normalized correctly. Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- builtin/check-ref-format.c | 6 +++--- t/t1402-check-ref-format.sh | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/builtin/check-ref-format.c b/builtin/check-ref-format.c index ae3f28115a7a4d..0723cf245e52e6 100644 --- a/builtin/check-ref-format.c +++ b/builtin/check-ref-format.c @@ -12,8 +12,8 @@ static const char builtin_check_ref_format_usage[] = " or: git check-ref-format --branch "; /* - * Replace each run of adjacent slashes in src with a single slash, - * and write the result to dst. + * Remove leading slashes and replace each run of adjacent slashes in + * src with a single slash, and write the result to dst. * * This function is similar to normalize_path_copy(), but stripped down * to meet check_ref_format's simpler needs. @@ -21,7 +21,7 @@ static const char builtin_check_ref_format_usage[] = static void collapse_slashes(char *dst, const char *src) { char ch; - char prev = '\0'; + char prev = '/'; while ((ch = *src++) != '\0') { if (prev == '/' && ch == prev) diff --git a/t/t1402-check-ref-format.sh b/t/t1402-check-ref-format.sh index 1b0f82fa4c7928..7563043c53f09c 100755 --- a/t/t1402-check-ref-format.sh +++ b/t/t1402-check-ref-format.sh @@ -18,6 +18,9 @@ invalid_ref 'foo' valid_ref 'foo/bar/baz' valid_ref 'refs///heads/foo' invalid_ref 'heads/foo/' +valid_ref '/heads/foo' +valid_ref '///heads/foo' +invalid_ref '/foo' invalid_ref './foo' invalid_ref '.refs/foo' invalid_ref 'heads/foo..bar' @@ -70,7 +73,10 @@ invalid_ref_normalized() { valid_ref_normalized 'heads/foo' 'heads/foo' valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo' +valid_ref_normalized '/heads/foo' 'heads/foo' +valid_ref_normalized '///heads/foo' 'heads/foo' invalid_ref_normalized 'foo' +invalid_ref_normalized '/foo' invalid_ref_normalized 'heads/foo/../bar' invalid_ref_normalized 'heads/./foo' invalid_ref_normalized 'heads\foo' From f3738c1ce9193a4bf45ba1a3ea67d0cf32da0257 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Sat, 27 Aug 2011 06:12:44 +0200 Subject: [PATCH 08/13] Forbid DEL characters in reference names DEL is an ASCII control character and therefore should not be permitted in reference names. Add tests for this and other unusual characters. Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- refs.c | 2 +- t/t1402-check-ref-format.sh | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/refs.c b/refs.c index e3c05110e58ca5..6f471d42c37ab4 100644 --- a/refs.c +++ b/refs.c @@ -837,7 +837,7 @@ int for_each_rawref(each_ref_fn fn, void *cb_data) static inline int bad_ref_char(int ch) { - if (((unsigned) ch) <= ' ' || + if (((unsigned) ch) <= ' ' || ch == 0x7f || ch == '~' || ch == '^' || ch == ':' || ch == '\\') return 1; /* 2.13 Pattern Matching Notation */ diff --git a/t/t1402-check-ref-format.sh b/t/t1402-check-ref-format.sh index 7563043c53f09c..ed4275afe31004 100755 --- a/t/t1402-check-ref-format.sh +++ b/t/t1402-check-ref-format.sh @@ -30,6 +30,9 @@ invalid_ref 'heads/foo.lock' valid_ref 'heads/foo@bar' invalid_ref 'heads/v@{ation' invalid_ref 'heads/foo\bar' +invalid_ref "$(printf 'heads/foo\t')" +invalid_ref "$(printf 'heads/foo\177')" +valid_ref "$(printf 'heads/fu\303\237')" test_expect_success "check-ref-format --branch @{-1}" ' T=$(git write-tree) && From 385ceec1cb46f8a476fa11ffc853dedba512fd52 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Sun, 28 Aug 2011 09:34:56 +0200 Subject: [PATCH 09/13] t3005: do not assume a particular order of stdout and stderr of git-ls-files There is no guarantee that stderr is flushed before stdout when both channels are redirected to a file. Check the channels using independent files. Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- t/t3005-ls-files-relative.sh | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/t/t3005-ls-files-relative.sh b/t/t3005-ls-files-relative.sh index a2b63e2c1066f1..377869432e9d02 100755 --- a/t/t3005-ls-files-relative.sh +++ b/t/t3005-ls-files-relative.sh @@ -45,11 +45,12 @@ test_expect_success 'ls-files -c' ' for f in ../y* do echo "error: pathspec $sq$f$sq did not match any file(s) known to git." - done >expect && - echo "Did you forget to ${sq}git add${sq}?" >>expect && - ls ../x* >>expect && - test_must_fail git ls-files -c --error-unmatch ../[xy]* >actual 2>&1 && - test_cmp expect actual + done >expect.err && + echo "Did you forget to ${sq}git add${sq}?" >>expect.err && + ls ../x* >expect.out && + test_must_fail git ls-files -c --error-unmatch ../[xy]* >actual.out 2>actual.err && + test_cmp expect.out actual.out && + test_cmp expect.err actual.err ) ' @@ -59,11 +60,12 @@ test_expect_success 'ls-files -o' ' for f in ../x* do echo "error: pathspec $sq$f$sq did not match any file(s) known to git." - done >expect && - echo "Did you forget to ${sq}git add${sq}?" >>expect && - ls ../y* >>expect && - test_must_fail git ls-files -o --error-unmatch ../[xy]* >actual 2>&1 && - test_cmp expect actual + done >expect.err && + echo "Did you forget to ${sq}git add${sq}?" >>expect.err && + ls ../y* >expect.out && + test_must_fail git ls-files -o --error-unmatch ../[xy]* >actual.out 2>actual.err && + test_cmp expect.out actual.out && + test_cmp expect.err actual.err ) ' From dff4b0ef30cd3f06e1f2383ce09f81b8da94d5b3 Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Mon, 29 Aug 2011 17:22:06 +0200 Subject: [PATCH 10/13] am: format is in $patch_format, not parse_patch The error message given when the patch format was not recognized was wrong, since the variable checked was $parse_patch rather than $patch_format. Fix by checking the non-emptyness of the correct variable. Signed-off-by: Giuseppe Bilotta Signed-off-by: Junio C Hamano --- git-am.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/git-am.sh b/git-am.sh index f719f6e654fdb7..78e3ee039f921a 100755 --- a/git-am.sh +++ b/git-am.sh @@ -268,7 +268,8 @@ split_patches () { msgnum= ;; *) - if test -n "$parse_patch" ; then + if test -n "$patch_format" + then clean_abort "Patch format $patch_format is not supported." else clean_abort "Patch format detection failed." From e622f41dcd97861796c7529712170f9eae07dc56 Mon Sep 17 00:00:00 2001 From: Jay Soffian Date: Mon, 19 Sep 2011 19:40:52 -0400 Subject: [PATCH 11/13] git-mergetool: check return value from read Mostly fixed already by 6b44577 (mergetool: check return value from read, 2011-07-01). Catch two uses it missed. Signed-off-by: Jay Soffian Signed-off-by: Junio C Hamano --- git-mergetool--lib.sh | 2 +- git-mergetool.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/git-mergetool--lib.sh b/git-mergetool--lib.sh index 4db92123312596..a79a2ecd49411f 100644 --- a/git-mergetool--lib.sh +++ b/git-mergetool--lib.sh @@ -38,7 +38,7 @@ check_unchanged () { while true; do echo "$MERGED seems unchanged." printf "Was the merge successful? [y/n] " - read answer + read answer || return 1 case "$answer" in y*|Y*) status=0; break ;; n*|N*) status=1; break ;; diff --git a/git-mergetool.sh b/git-mergetool.sh index 3c157bcd26232c..b6d463f0d05736 100755 --- a/git-mergetool.sh +++ b/git-mergetool.sh @@ -72,7 +72,7 @@ describe_file () { resolve_symlink_merge () { while true; do printf "Use (l)ocal or (r)emote, or (a)bort? " - read ans + read ans || return 1 case "$ans" in [lL]*) git checkout-index -f --stage=2 -- "$MERGED" From 85b3c75f4fd3aa4da976bac702827dc8d7d1bf15 Mon Sep 17 00:00:00 2001 From: Allan Caffee Date: Sun, 31 Jul 2011 21:52:41 -0400 Subject: [PATCH 12/13] describe: Refresh the index when run with --dirty When running git describe --dirty the index should be refreshed. Previously the cached index would cause describe to think that the index was dirty when, in reality, it was just stale. The issue was exposed by python setuptools which hardlinks files into another directory when building a distribution. Signed-off-by: Junio C Hamano --- builtin/describe.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/builtin/describe.c b/builtin/describe.c index 66fc291c8a81de..9f63067f50a6f4 100644 --- a/builtin/describe.c +++ b/builtin/describe.c @@ -462,8 +462,21 @@ int cmd_describe(int argc, const char **argv, const char *prefix) die(_("No names found, cannot describe anything.")); if (argc == 0) { - if (dirty && !cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1, diff_index_args, prefix)) - dirty = NULL; + if (dirty) { + static struct lock_file index_lock; + int fd; + + read_cache_preload(NULL); + refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, + NULL, NULL, NULL); + fd = hold_locked_index(&index_lock, 0); + if (0 <= fd) + update_index_if_able(&the_index, &index_lock); + + if (!cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1, + diff_index_args, prefix)) + dirty = NULL; + } describe("HEAD", 1); } else if (dirty) { die(_("--dirty is incompatible with committishes")); From 632052641517de1a965c1f045b97d2eaa541b2e9 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 23 Sep 2011 14:38:39 -0700 Subject: [PATCH 13/13] Git 1.7.6.4 Signed-off-by: Junio C Hamano --- Documentation/RelNotes/1.7.6.4.txt | 32 ++++++++++++++++++++++++++++++ Documentation/git.txt | 3 ++- GIT-VERSION-GEN | 2 +- RelNotes | 2 +- 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 Documentation/RelNotes/1.7.6.4.txt diff --git a/Documentation/RelNotes/1.7.6.4.txt b/Documentation/RelNotes/1.7.6.4.txt new file mode 100644 index 00000000000000..e19acac2da5f17 --- /dev/null +++ b/Documentation/RelNotes/1.7.6.4.txt @@ -0,0 +1,32 @@ +Git v1.7.6.4 Release Notes +========================== + +Fixes since v1.7.6.3 +-------------------- + + * The error reporting logic of "git am" when the command is fed a file + whose mail-storage format is unknown was fixed. + + * "git branch --set-upstream @{-1} foo" did not expand @{-1} correctly. + + * "git check-ref-format --print" used to parrot a candidate string that + began with a slash (e.g. /refs/heads/master) without stripping it, to make + the result a suitably normalized string the caller can append to "$GIT_DIR/". + + * "git clone" failed to clone locally from a ".git" file that itself + is not a directory but is a pointer to one. + + * "git clone" from a local repository that borrows from another + object store using a relative path in its objects/info/alternates + file did not adjust the alternates in the resulting repository. + + * "git describe --dirty" did not refresh the index before checking the + state of the working tree files. + + * "git ls-files ../$path" that is run from a subdirectory reported errors + incorrectly when there is no such path that matches the given pathspec. + + * "git mergetool" could loop forever prompting when nothing can be read + from the standard input. + +Also contains minor fixes and documentation updates. diff --git a/Documentation/git.txt b/Documentation/git.txt index 3562da85e9edf0..d1481354a89b39 100644 --- a/Documentation/git.txt +++ b/Documentation/git.txt @@ -44,9 +44,10 @@ unreleased) version of git, that is available from 'master' branch of the `git.git` repository. Documentation for older releases are available here: -* link:v1.7.6.3/git.html[documentation for release 1.7.6.3] +* link:v1.7.6.4/git.html[documentation for release 1.7.6.4] * release notes for + link:RelNotes/1.7.6.4.txt[1.7.6.4], link:RelNotes/1.7.6.3.txt[1.7.6.3], link:RelNotes/1.7.6.2.txt[1.7.6.2], link:RelNotes/1.7.6.1.txt[1.7.6.1], diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN index b74b21f7dd66a0..b42c77c392e5bb 100755 --- a/GIT-VERSION-GEN +++ b/GIT-VERSION-GEN @@ -1,7 +1,7 @@ #!/bin/sh GVF=GIT-VERSION-FILE -DEF_VER=v1.7.6.3 +DEF_VER=v1.7.6.4 LF=' ' diff --git a/RelNotes b/RelNotes index 695f126692a55f..2b3ea813c80a43 120000 --- a/RelNotes +++ b/RelNotes @@ -1 +1 @@ -Documentation/RelNotes/1.7.6.3.txt \ No newline at end of file +Documentation/RelNotes/1.7.6.4.txt \ No newline at end of file