From 9919f41c91e525fd813fd2cd006f8fdcf976a661 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 17 Jul 2006 00:34:44 -0700 Subject: [PATCH 01/27] git-diff A...B to (usually) mean "git-diff `git-merge-base A B` B" This tweaks the argument parser of "git diff" to allow "git-diff A...B" to show diffs leading to B since their merge-base, when there is only one sensible merge base between A and B. Currently nonsense cases are thrown at combined-diff to produce nonsense results, which would eventually need to be fixed. Signed-off-by: Junio C Hamano --- builtin-diff.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/builtin-diff.c b/builtin-diff.c index cb38f44561..efd315240f 100644 --- a/builtin-diff.c +++ b/builtin-diff.c @@ -346,7 +346,15 @@ int cmd_diff(int argc, const char **argv, char **envp) return builtin_diff_index(&rev, argc, argv); else if (ents == 2) return builtin_diff_tree(&rev, argc, argv, ent); + else if ((ents == 3) && (ent[0].item->flags & UNINTERESTING)) { + /* diff A...B where there is one sane merge base between + * A and B. We have ent[0] == merge-base, ent[1] == A, + * and ent[2] == B. Show diff between the base and B. + */ + return builtin_diff_tree(&rev, argc, argv, ent); + } else - return builtin_diff_combined(&rev, argc, argv, ent, ents); + return builtin_diff_combined(&rev, argc, argv, + ent, ents); usage(builtin_diff_usage); } From c6e1d9ed509265a66fe9d155b1a66aa954cce675 Mon Sep 17 00:00:00 2001 From: Luben Tuikov Date: Sun, 23 Jul 2006 13:26:30 -0700 Subject: [PATCH 02/27] gitweb.cgi: Teach git_history() to read hash from $hash_base Teach git_history() to take its hash argument from the hb parameter, i.e. from $hash_base. Also change all "a=history" actions to pass "hb=" instead of "h=". Signed-off-by: Luben Tuikov Signed-off-by: Junio C Hamano --- gitweb/gitweb.cgi | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/gitweb/gitweb.cgi b/gitweb/gitweb.cgi index 2fd1e5f78e..d976d4adca 100755 --- a/gitweb/gitweb.cgi +++ b/gitweb/gitweb.cgi @@ -1687,7 +1687,7 @@ sub git_tree { "" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name")}, "blob") . # " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$t_hash$base_key;f=$base$t_name")}, "blame") . - " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash_base;f=$base$t_name")}, "history") . + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash_base;f=$base$t_name")}, "history") . " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$t_hash;f=$base$t_name")}, "raw") . "\n"; } elsif ($t_type eq "tree") { @@ -1696,7 +1696,7 @@ sub git_tree { "\n" . "" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, "tree") . - " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash_base;f=$base$t_name")}, "history") . + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash_base;f=$base$t_name")}, "history") . "\n"; } print "\n"; @@ -2041,7 +2041,7 @@ sub git_commit { "[deleted " . file_type($from_mode). "]\n" . "" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, "blob") . - " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash;f=$file")}, "history") . + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash;f=$file")}, "history") . "\n" } elsif ($status eq "M" || $status eq "T") { my $mode_chnge = ""; @@ -2072,7 +2072,7 @@ sub git_commit { if ($to_id ne $from_id) { print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file")}, "diff"); } - print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash;f=$file")}, "history") . "\n"; + print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash;f=$file")}, "history") . "\n"; print "\n"; } elsif ($status eq "R") { my ($from_file, $to_file) = split "\t", $file; @@ -2293,10 +2293,10 @@ sub git_commitdiff_plain { } sub git_history { - if (!defined $hash) { - $hash = git_read_head($project); + if (!defined $hash_base) { + $hash_base = git_read_head($project); } - my %co = git_read_commit($hash); + my %co = git_read_commit($hash_base); if (!%co) { die_error(undef, "Unknown commit object."); } @@ -2306,18 +2306,18 @@ sub git_history { $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") . " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") . " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") . - " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") . - " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") . - " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") . + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") . + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") . + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") . "

\n" . "\n"; print "
\n" . - $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" . + $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) . "\n" . "
\n"; print "
/" . esc_html($file_name) . "
\n"; open my $fd, "-|", - "$GIT rev-list --full-history $hash -- \'$file_name\'"; + "$GIT rev-list --full-history $hash_base -- \'$file_name\'"; print "\n"; my $alternate = 0; while (my $line = <$fd>) { @@ -2345,7 +2345,7 @@ sub git_history { $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") . " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") . " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=$commit;f=$file_name")}, "blob"); - my $blob = git_get_hash_by_path($hash, $file_name); + my $blob = git_get_hash_by_path($hash_base, $file_name); my $blob_parent = git_get_hash_by_path($commit, $file_name); if (defined $blob && defined $blob_parent && $blob ne $blob_parent) { print " | " . From cff0771bfbc8a8c1a432a99bc297a52a5ba94304 Mon Sep 17 00:00:00 2001 From: Luben Tuikov Date: Sun, 23 Jul 2006 13:28:55 -0700 Subject: [PATCH 03/27] gitweb.cgi: Include direct link to "raw" files from "history" In "history" view, the "page_path" is now also a URL link to the "raw" format of the file, which will always give you the latest version in the repository. This is helpful for externally linking files, such that the latest version is always referenced and in "raw" format. Signed-off-by: Luben Tuikov Signed-off-by: Junio C Hamano --- gitweb/gitweb.cgi | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/gitweb/gitweb.cgi b/gitweb/gitweb.cgi index d976d4adca..7a61de4a77 100755 --- a/gitweb/gitweb.cgi +++ b/gitweb/gitweb.cgi @@ -1531,6 +1531,14 @@ sub git_blob_plain_mimetype { } sub git_blob_plain { + if (!defined $hash) { + if (defined $file_name) { + my $base = $hash_base || git_read_head($project); + $hash = git_get_hash_by_path($base, $file_name, "blob") || die_error(undef, "Error lookup file."); + } else { + die_error(undef, "No file name defined."); + } + } my $type = shift; open my $fd, "-|", "$GIT cat-file blob $hash" or die_error("Couldn't cat $file_name, $hash"); @@ -1554,10 +1562,14 @@ sub git_blob_plain { } sub git_blob { - if (!defined $hash && defined $file_name) { - my $base = $hash_base || git_read_head($project); - $hash = git_get_hash_by_path($base, $file_name, "blob") || die_error(undef, "Error lookup file."); - } + if (!defined $hash) { + if (defined $file_name) { + my $base = $hash_base || git_read_head($project); + $hash = git_get_hash_by_path($base, $file_name, "blob") || die_error(undef, "Error lookup file."); + } else { + die_error(undef, "No file name defined."); + } + } my $have_blame = git_get_project_config_bool ('blame'); open my $fd, "-|", "$GIT cat-file blob $hash" or die_error(undef, "Open failed."); my $mimetype = git_blob_plain_mimetype($fd, $file_name); @@ -1687,7 +1699,7 @@ sub git_tree { "\n"; } elsif ($t_type eq "tree") { @@ -2314,7 +2326,18 @@ sub git_history { print "
\n" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) . "\n" . "
\n"; - print "
/" . esc_html($file_name) . "
\n"; + if (defined $hash) { + my $ftype = git_get_type($hash); + + if ($ftype =~ "blob") { + print "
/" . + $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;f=$file_name")}, esc_html($file_name)) . "
\n"; + } else { + print "
/" . esc_html($file_name) . "
\n"; + } + } else { + print "
/" . esc_html($file_name) . "
\n"; + } open my $fd, "-|", "$GIT rev-list --full-history $hash_base -- \'$file_name\'"; From 93d5f0619ccf10d7a16834b9e3be38871d2aae6c Mon Sep 17 00:00:00 2001 From: Luben Tuikov Date: Sun, 23 Jul 2006 13:30:08 -0700 Subject: [PATCH 04/27] gitweb.cgi: Show "raw" head of project link even when $hash is not defined Some callers of git_history() do not set $hash of $file_name. Add code to find it, if it is not defined. Signed-off-by: Luben Tuikov Signed-off-by: Junio C Hamano --- gitweb/gitweb.cgi | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gitweb/gitweb.cgi b/gitweb/gitweb.cgi index 7a61de4a77..c04283ba58 100755 --- a/gitweb/gitweb.cgi +++ b/gitweb/gitweb.cgi @@ -2326,6 +2326,9 @@ sub git_history { print "
\n" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) . "\n" . "
\n"; + if (!defined $hash && defined $file_name) { + $hash = git_get_hash_by_path($hash_base, $file_name); + } if (defined $hash) { my $ftype = git_get_type($hash); From 634331061599a82968daddae2d2c0896b6137d4c Mon Sep 17 00:00:00 2001 From: Luben Tuikov Date: Sun, 23 Jul 2006 13:31:15 -0700 Subject: [PATCH 05/27] gitweb.cgi: Centralize printing of the page path Centralize printing of the page path so that if the entity is a blob, we can set the page path to be the link to the HEAD revision of the "raw" blob. Signed-off-by: Luben Tuikov Signed-off-by: Junio C Hamano --- gitweb/gitweb.cgi | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/gitweb/gitweb.cgi b/gitweb/gitweb.cgi index c04283ba58..4106cb8eb6 100755 --- a/gitweb/gitweb.cgi +++ b/gitweb/gitweb.cgi @@ -1199,6 +1199,20 @@ sub git_summary { git_footer_html(); } +sub git_print_page_path { + my $name = shift; + my $type = shift; + + if (!defined $name) { + print "
/
\n"; + } elsif ($type =~ "blob") { + print "
" . + $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;f=$file_name")}, esc_html($name)) . "
\n"; + } else { + print "
" . esc_html($name) . "
\n"; + } +} + sub git_tag { my $head = git_read_head($project); git_header_html(); @@ -1266,7 +1280,7 @@ sub git_blame { "
" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) . "
\n"; - print "
" . esc_html($file_name) . "
\n"; + git_print_page_path($file_name); print "
\n"; print < @@ -1604,9 +1618,7 @@ sub git_blob { "

\n" . "
$hash
\n"; } - if (defined $file_name) { - print "
" . esc_html($file_name) . "
\n"; - } + git_print_page_path($file_name, "blob"); print "
\n"; my $nr; while (my $line = <$fd>) { @@ -1671,10 +1683,8 @@ sub git_tree { } if (defined $file_name) { $base = esc_html("$file_name/"); - print "
/" . esc_html($file_name) . "
\n"; - } else { - print "
/
\n"; } + git_print_page_path($file_name); print "
\n"; print "
" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name")}, "blob") . # " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$t_hash$base_key;f=$base$t_name")}, "blame") . - " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash_base;f=$base$t_name")}, "history") . + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$t_hash;hb=$hash_base;f=$base$t_name")}, "history") . " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$t_hash;f=$base$t_name")}, "raw") . "
\n"; my $alternate = 0; @@ -2132,9 +2142,7 @@ sub git_blobdiff { "

\n" . "
$hash vs $hash_parent
\n"; } - if (defined $file_name) { - print "
/" . esc_html($file_name) . "
\n"; - } + git_print_page_path($file_name, "blob"); print "
\n" . "
blob:" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name")}, $hash_parent) . @@ -2308,6 +2316,7 @@ sub git_history { if (!defined $hash_base) { $hash_base = git_read_head($project); } + my $ftype; my %co = git_read_commit($hash_base); if (!%co) { die_error(undef, "Unknown commit object."); @@ -2330,17 +2339,9 @@ sub git_history { $hash = git_get_hash_by_path($hash_base, $file_name); } if (defined $hash) { - my $ftype = git_get_type($hash); - - if ($ftype =~ "blob") { - print "
/" . - $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;f=$file_name")}, esc_html($file_name)) . "
\n"; - } else { - print "
/" . esc_html($file_name) . "
\n"; - } - } else { - print "
/" . esc_html($file_name) . "
\n"; + $ftype = git_get_type($hash); } + git_print_page_path($file_name, $ftype); open my $fd, "-|", "$GIT rev-list --full-history $hash_base -- \'$file_name\'"; From 4ab243a944a6013d9e862adadd6fe6152c842401 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 24 Jul 2006 14:10:45 +0200 Subject: [PATCH 06/27] Allow an alias to start with "-p" Now, something like [alias] pd = -p diff works as expected. [jc: a follow-up fix from Jeff King folded in.] Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- git.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/git.c b/git.c index ee5a0e86a7..c0bd19d0ef 100644 --- a/git.c +++ b/git.c @@ -35,6 +35,27 @@ static void prepend_to_path(const char *dir, int len) setenv("PATH", path, 1); } +static int handle_options(const char*** argv, int* argc) +{ + int handled = 0; + + while (*argc > 0) { + const char *cmd = (*argv)[0]; + if (cmd[0] != '-') + break; + + if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) { + setup_pager(); + } else + die ("Unknown option: %s", cmd); + + (*argv)++; + (*argc)--; + handled++; + } + return handled; +} + static const char *alias_command; static char *alias_string = NULL; @@ -106,7 +127,7 @@ static int handle_alias(int *argcp, const char ***argv) subdir = setup_git_directory_gently(&nongit); if (!nongit) { - int count; + int count, option_count; const char** new_argv; alias_command = (*argv)[0]; @@ -114,6 +135,10 @@ static int handle_alias(int *argcp, const char ***argv) if (alias_string) { count = split_cmdline(alias_string, &new_argv); + option_count = handle_options(&new_argv, &count); + memmove(new_argv - option_count, new_argv, + count * sizeof(char *)); + new_argv -= option_count; if (count < 1) die("empty alias for %s", alias_command); @@ -273,13 +298,12 @@ int main(int argc, const char **argv, char **envp) /* Look for flags.. */ while (argc > 1) { - cmd = *++argv; + argv++; argc--; - if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) { - setup_pager(); - continue; - } + handle_options(&argv, &argc); + + cmd = *argv; if (strncmp(cmd, "--", 2)) break; From 6acbcb927f8e34857fa8f68fcb4f9076941b24ff Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 25 Jul 2006 20:24:22 +0200 Subject: [PATCH 07/27] git wrapper: add --git-dir= and --bare options With this, you can say git --bare repack -a -d inside a bare repository, and it will actually work. While at it, also move the --version, --help and --exec-path options to the handle_options() function. While at documenting the new options, also document the --paginate option. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- Documentation/git.txt | 12 +++++- builtin-help.c | 2 +- git.c | 91 ++++++++++++++++++++++--------------------- 3 files changed, 58 insertions(+), 47 deletions(-) diff --git a/Documentation/git.txt b/Documentation/git.txt index ce3058182f..7310a2b8b8 100644 --- a/Documentation/git.txt +++ b/Documentation/git.txt @@ -8,7 +8,8 @@ git - the stupid content tracker SYNOPSIS -------- -'git' [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ARGS] +'git' [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate] + [--bare] [--git-dir=GIT_DIR] [--help] COMMAND [ARGS] DESCRIPTION ----------- @@ -41,6 +42,15 @@ OPTIONS environment variable. If no path is given 'git' will print the current setting and then exit. +-p|--paginate:: + Pipe all output into 'less' (or if set, $PAGER). + +--git-dir=:: + Set the path to the repository. This can also be controlled by + setting the GIT_DIR environment variable. + +--bare:: + Same as --git-dir=`pwd`. FURTHER DOCUMENTATION --------------------- diff --git a/builtin-help.c b/builtin-help.c index 335fe5fedc..bc1b4da3bc 100644 --- a/builtin-help.c +++ b/builtin-help.c @@ -229,7 +229,7 @@ int cmd_version(int argc, const char **argv, char **envp) int cmd_help(int argc, const char **argv, char **envp) { - const char *help_cmd = argv[1]; + const char *help_cmd = argc > 1 ? argv[1] : NULL; if (!help_cmd) cmd_usage(0, git_exec_path(), NULL); else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) diff --git a/git.c b/git.c index c0bd19d0ef..885e1ce75b 100644 --- a/git.c +++ b/git.c @@ -44,10 +44,42 @@ static int handle_options(const char*** argv, int* argc) if (cmd[0] != '-') break; - if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) { + /* + * For legacy reasons, the "version" and "help" + * commands can be written with "--" prepended + * to make them look like flags. + */ + if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version")) + break; + + /* + * Check remaining flags. + */ + if (!strncmp(cmd, "--exec-path", 11)) { + cmd += 11; + if (*cmd == '=') + git_set_exec_path(cmd + 1); + else { + puts(git_exec_path()); + exit(0); + } + } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) { setup_pager(); - } else - die ("Unknown option: %s", cmd); + } else if (!strcmp(cmd, "--git-dir")) { + if (*argc < 1) + return -1; + setenv("GIT_DIR", (*argv)[1], 1); + (*argv)++; + (*argc)--; + } else if (!strncmp(cmd, "--git-dir=", 10)) { + setenv("GIT_DIR", cmd + 10, 1); + } else if (!strcmp(cmd, "--bare")) { + static char git_dir[1024]; + setenv("GIT_DIR", getcwd(git_dir, 1024), 1); + } else { + fprintf(stderr, "Unknown option: %s\n", cmd); + cmd_usage(0, NULL, NULL); + } (*argv)++; (*argc)--; @@ -293,50 +325,19 @@ int main(int argc, const char **argv, char **envp) die("cannot handle %s internally", cmd); } - /* Default command: "help" */ - cmd = "help"; - /* Look for flags.. */ - while (argc > 1) { - argv++; - argc--; - - handle_options(&argv, &argc); - - cmd = *argv; - - if (strncmp(cmd, "--", 2)) - break; - - cmd += 2; - - /* - * For legacy reasons, the "version" and "help" - * commands can be written with "--" prepended - * to make them look like flags. - */ - if (!strcmp(cmd, "help")) - break; - if (!strcmp(cmd, "version")) - break; - - /* - * Check remaining flags (which by now must be - * "--exec-path", but maybe we will accept - * other arguments some day) - */ - if (!strncmp(cmd, "exec-path", 9)) { - cmd += 9; - if (*cmd == '=') { - git_set_exec_path(cmd + 1); - continue; - } - puts(git_exec_path()); - exit(0); - } - cmd_usage(0, NULL, NULL); + argv++; + argc--; + handle_options(&argv, &argc); + if (argc > 0) { + if (!strncmp(argv[0], "--", 2)) + argv[0] += 2; + } else { + /* Default command: "help" */ + argv[0] = "help"; + argc = 1; } - argv[0] = cmd; + cmd = argv[0]; /* * We search for git commands in the following order: From 5209eda86363a3ba2e000903ad8de29589b18b58 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 26 Jul 2006 23:11:46 +0200 Subject: [PATCH 08/27] instaweb: Be more clear if httpd or the browser fail Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- git-instaweb.sh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/git-instaweb.sh b/git-instaweb.sh index 9829c59154..16cd351f7f 100755 --- a/git-instaweb.sh +++ b/git-instaweb.sh @@ -54,6 +54,10 @@ start_httpd () { fi done fi + if test $? != 0; then + echo "Could not execute http daemon $httpd." + exit 1 + fi } stop_httpd () { @@ -184,7 +188,7 @@ EOF else # plain-old CGI list_mods=`echo "$httpd" | sed "s/-f$/-l/"` - $list_mods | grep 'mod_cgi\.c' >/dev/null || \ + $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \ echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf" cat >> "$conf" < Date: Wed, 26 Jul 2006 22:51:52 +0200 Subject: [PATCH 09/27] cvsserver: imitate git-update-ref when committing git-update-ref writes into the lockfile, and renames it afterwards. Like commit v1.3.0-rc3~22, it is not only cleaner, but also helps with shared setups: every developer can have a different primary group; what matters is that $GIT_DIR/refs/heads has to be writable by a group you are in. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- git-cvsserver.perl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/git-cvsserver.perl b/git-cvsserver.perl index 5b73837bb1..2130d57020 100755 --- a/git-cvsserver.perl +++ b/git-cvsserver.perl @@ -1142,9 +1142,7 @@ sub req_ci exit; } - open FILE, ">", "$ENV{GIT_DIR}refs/heads/$state->{module}"; - print FILE $commithash; - close FILE; + print LOCKFILE $commithash; $updater->update(); @@ -1171,7 +1169,9 @@ sub req_ci } close LOCKFILE; - unlink($lockfile); + my $reffile = "$ENV{GIT_DIR}refs/heads/$state->{module}"; + unlink($reffile); + rename($lockfile, $reffile); chdir "/"; print "ok\n"; From 6c35119ac711c69c69874d7d52f5cfa12c07ccce Mon Sep 17 00:00:00 2001 From: Matthias Lederhofer Date: Fri, 14 Jul 2006 17:53:29 +0200 Subject: [PATCH 10/27] daemon: documentation for --reuseaddr, --detach and --pid-file Signed-off-by: Matthias Lederhofer Signed-off-by: Junio C Hamano --- Documentation/git-daemon.txt | 13 ++++++++++++- daemon.c | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Documentation/git-daemon.txt b/Documentation/git-daemon.txt index 4c357daf6a..0f7d274eab 100644 --- a/Documentation/git-daemon.txt +++ b/Documentation/git-daemon.txt @@ -11,7 +11,7 @@ SYNOPSIS 'git-daemon' [--verbose] [--syslog] [--inetd | --port=n] [--export-all] [--timeout=n] [--init-timeout=n] [--strict-paths] [--base-path=path] [--user-path | --user-path=path] - [directory...] + [--reuseaddr] [--detach] [--pid-file=file] [directory...] DESCRIPTION ----------- @@ -82,6 +82,17 @@ OPTIONS --verbose:: Log details about the incoming connections and requested files. +--reuseaddr:: + Use SO_REUSEADDR when binding the listening socket. + This allows the server to restart without waiting for + old connections to time out. + +--detach:: + Detach from the shell. Implies --syslog. + +--pid-file=file:: + Save the process id in 'file'. + :: A directory to add to the whitelist of allowed directories. Unless --strict-paths is specified this will also include subdirectories diff --git a/daemon.c b/daemon.c index e4ec676301..810837f0c4 100644 --- a/daemon.c +++ b/daemon.c @@ -19,7 +19,7 @@ static const char daemon_usage[] = "git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all]\n" " [--timeout=n] [--init-timeout=n] [--strict-paths]\n" " [--base-path=path] [--user-path | --user-path=path]\n" -" [--reuseaddr] [directory...]"; +" [--reuseaddr] [--detach] [--pid-file=file] [directory...]"; /* List of acceptable pathname prefixes */ static char **ok_paths = NULL; From cc41cd2e60da8224dcd04f23d837853cff5d4eda Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Thu, 27 Jul 2006 20:58:53 +0200 Subject: [PATCH 11/27] Remove -d from *-fetch usage strings This is a really ancient remnant of the short era of delta objects stored directly in the object database. Signed-off-by: Petr Baudis Signed-off-by: Junio C Hamano --- http-fetch.c | 2 +- local-fetch.c | 4 ++-- ssh-fetch.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/http-fetch.c b/http-fetch.c index 12493fbed2..dc286b79f6 100644 --- a/http-fetch.c +++ b/http-fetch.c @@ -1245,7 +1245,7 @@ int main(int argc, char **argv) arg++; } if (argc < arg + 2) { - usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url"); + usage("git-http-fetch [-c] [-t] [-a] [-v] [--recover] [-w ref] commit-id url"); return 1; } commit_id = argv[arg]; diff --git a/local-fetch.c b/local-fetch.c index ffa4887570..a05ac16cd0 100644 --- a/local-fetch.c +++ b/local-fetch.c @@ -194,9 +194,9 @@ int fetch_ref(char *ref, unsigned char *sha1) } static const char local_pull_usage[] = -"git-local-fetch [-c] [-t] [-a] [-d] [-v] [-w filename] [--recover] [-l] [-s] [-n] commit-id path"; +"git-local-fetch [-c] [-t] [-a] [-v] [-w filename] [--recover] [-l] [-s] [-n] commit-id path"; -/* +/* * By default we only use file copy. * If -l is specified, a hard link is attempted. * If -s is specified, then a symlink is attempted. diff --git a/ssh-fetch.c b/ssh-fetch.c index 28f7fd9174..a8a6cfbb30 100644 --- a/ssh-fetch.c +++ b/ssh-fetch.c @@ -120,7 +120,7 @@ int fetch_ref(char *ref, unsigned char *sha1) static const char ssh_fetch_usage[] = MY_PROGRAM_NAME - " [-c] [-t] [-a] [-v] [-d] [--recover] [-w ref] commit-id url"; + " [-c] [-t] [-a] [-v] [--recover] [-w ref] commit-id url"; int main(int argc, char **argv) { char *commit_id; From c2c487cf3a6c37bc9b4e3f0e10ad08d9ce048404 Mon Sep 17 00:00:00 2001 From: "Michael S. Tsirkin" Date: Thu, 27 Jul 2006 17:03:43 +0300 Subject: [PATCH 12/27] mailinfo: accept >From in message header Mail I get sometimes has multiple From lines, like this: From Majordomo@vger.kernel.org Thu Jul 27 16:39:36 2006 >From mtsirkin Thu Jul 27 16:39:36 2006 Received: from yok.mtl.com [10.0.8.11] ... which confuses git-mailinfo since that does not recognize >From as a valid header line. This patch makes it recognize >From XXX as a valid header line. Signed-off-by: Michael S. Tsirkin Signed-off-by: Junio C Hamano --- builtin-mailinfo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c index ac53f76f68..05dc1bfe71 100644 --- a/builtin-mailinfo.c +++ b/builtin-mailinfo.c @@ -446,7 +446,7 @@ static int read_one_header_line(char *line, int sz, FILE *in) break; } /* Count mbox From headers as headers */ - if (!ofs && !memcmp(line, "From ", 5)) + if (!ofs && (!memcmp(line, "From ", 5) || !memcmp(line, ">From ", 6))) ofs = 1; return ofs; } From c6b69bdbc1b0b914aa0d1e59a29305fce82d6f06 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Thu, 27 Jul 2006 23:56:14 +0200 Subject: [PATCH 13/27] Make pull() take some implicit data as explicit arguments Currently it's a bit weird that pull() takes a single argument describing the commit but takes the write_ref from a global variable. This makes it take that as a parameter as well, which might be nicer for the libification in the future, but especially it will make for nicer code when we implement pull()ing multiple commits at once. Signed-off-by: Petr Baudis Signed-off-by: Junio C Hamano --- fetch.c | 6 ++---- fetch.h | 11 ++++------- http-fetch.c | 4 ++-- local-fetch.c | 4 ++-- ssh-fetch.c | 4 ++-- 5 files changed, 12 insertions(+), 17 deletions(-) diff --git a/fetch.c b/fetch.c index 989d7a4788..3255cc6250 100644 --- a/fetch.c +++ b/fetch.c @@ -8,9 +8,6 @@ #include "blob.h" #include "refs.h" -const char *write_ref = NULL; -const char *write_ref_log_details = NULL; - int get_tree = 0; int get_history = 0; int get_all = 0; @@ -213,7 +210,8 @@ static int mark_complete(const char *path, const unsigned char *sha1) return 0; } -int pull(char *target) +int pull(char *target, const char *write_ref, + const char *write_ref_log_details) { struct ref_lock *lock = NULL; unsigned char sha1[20]; diff --git a/fetch.h b/fetch.h index 841bb1af9c..7bda355bc1 100644 --- a/fetch.h +++ b/fetch.h @@ -22,12 +22,6 @@ extern void prefetch(unsigned char *sha1); */ extern int fetch_ref(char *ref, unsigned char *sha1); -/* If set, the ref filename to write the target value to. */ -extern const char *write_ref; - -/* If set additional text will appear in the ref log. */ -extern const char *write_ref_log_details; - /* Set to fetch the target tree. */ extern int get_tree; @@ -46,6 +40,9 @@ extern int get_recover; /* Report what we got under get_verbosely */ extern void pull_say(const char *, const char *); -extern int pull(char *target); +/* If write_ref is set, the ref filename to write the target value to. */ +/* If write_ref_log_details is set, additional text will appear in the ref log. */ +extern int pull(char *target, const char *write_ref, + const char *write_ref_log_details); #endif /* PULL_H */ diff --git a/http-fetch.c b/http-fetch.c index dc286b79f6..963d439f82 100644 --- a/http-fetch.c +++ b/http-fetch.c @@ -1216,6 +1216,7 @@ int fetch_ref(char *ref, unsigned char *sha1) int main(int argc, char **argv) { + const char *write_ref = NULL; char *commit_id; char *url; char *path; @@ -1250,7 +1251,6 @@ int main(int argc, char **argv) } commit_id = argv[arg]; url = argv[arg + 1]; - write_ref_log_details = url; http_init(); @@ -1268,7 +1268,7 @@ int main(int argc, char **argv) alt->path_len = strlen(path); } - if (pull(commit_id)) + if (pull(commit_id, write_ref, url)) rc = 1; http_cleanup(); diff --git a/local-fetch.c b/local-fetch.c index a05ac16cd0..452b83396c 100644 --- a/local-fetch.c +++ b/local-fetch.c @@ -204,6 +204,7 @@ static const char local_pull_usage[] = */ int main(int argc, char **argv) { + const char *write_ref = NULL; char *commit_id; int arg = 1; @@ -240,9 +241,8 @@ int main(int argc, char **argv) usage(local_pull_usage); commit_id = argv[arg]; path = argv[arg + 1]; - write_ref_log_details = path; - if (pull(commit_id)) + if (pull(commit_id, write_ref, path)) return 1; return 0; diff --git a/ssh-fetch.c b/ssh-fetch.c index a8a6cfbb30..aef3aa4a05 100644 --- a/ssh-fetch.c +++ b/ssh-fetch.c @@ -123,6 +123,7 @@ static const char ssh_fetch_usage[] = " [-c] [-t] [-a] [-v] [--recover] [-w ref] commit-id url"; int main(int argc, char **argv) { + const char *write_ref = NULL; char *commit_id; char *url; int arg = 1; @@ -159,7 +160,6 @@ int main(int argc, char **argv) } commit_id = argv[arg]; url = argv[arg + 1]; - write_ref_log_details = url; if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1)) return 1; @@ -167,7 +167,7 @@ int main(int argc, char **argv) if (get_version()) return 1; - if (pull(commit_id)) + if (pull(commit_id, write_ref, url)) return 1; return 0; From 4211e4d10cd98b1aeed97bdb6cdebb9411956bb5 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Thu, 27 Jul 2006 23:56:17 +0200 Subject: [PATCH 14/27] Make pull() support fetching multiple targets at once pull() now takes an array of arguments instead of just one of each kind. Currently, no users use the new capability, but that'll change. Signed-off-by: Petr Baudis Signed-off-by: Junio C Hamano --- fetch.c | 80 +++++++++++++++++++++++++++++---------------------- fetch.h | 2 +- http-fetch.c | 2 +- local-fetch.c | 2 +- ssh-fetch.c | 2 +- 5 files changed, 50 insertions(+), 38 deletions(-) diff --git a/fetch.c b/fetch.c index 3255cc6250..281df61e7b 100644 --- a/fetch.c +++ b/fetch.c @@ -210,55 +210,67 @@ static int mark_complete(const char *path, const unsigned char *sha1) return 0; } -int pull(char *target, const char *write_ref, +int pull(int targets, char **target, const char **write_ref, const char *write_ref_log_details) { - struct ref_lock *lock = NULL; - unsigned char sha1[20]; + struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *)); + unsigned char *sha1 = xmalloc(targets * 20); char *msg; int ret; + int i; save_commit_buffer = 0; track_object_refs = 0; - if (write_ref) { - lock = lock_ref_sha1(write_ref, NULL, 0); - if (!lock) { - error("Can't lock ref %s", write_ref); - return -1; + + for (i = 0; i < targets; i++) { + if (!write_ref[i]) + continue; + + lock[i] = lock_ref_sha1(write_ref[i], NULL, 0); + if (!lock[i]) { + error("Can't lock ref %s", write_ref[i]); + goto unlock_and_fail; } } if (!get_recover) for_each_ref(mark_complete); - if (interpret_target(target, sha1)) { - error("Could not interpret %s as something to pull", target); - if (lock) - unlock_ref(lock); - return -1; - } - if (process(lookup_unknown_object(sha1))) { - if (lock) - unlock_ref(lock); - return -1; - } - if (loop()) { - if (lock) - unlock_ref(lock); - return -1; + for (i = 0; i < targets; i++) { + if (interpret_target(target[i], &sha1[20 * i])) { + error("Could not interpret %s as something to pull", target[i]); + goto unlock_and_fail; + } + if (process(lookup_unknown_object(&sha1[20 * i]))) + goto unlock_and_fail; } - if (write_ref) { - if (write_ref_log_details) { - msg = xmalloc(strlen(write_ref_log_details) + 12); - sprintf(msg, "fetch from %s", write_ref_log_details); - } - else - msg = NULL; - ret = write_ref_sha1(lock, sha1, msg ? msg : "fetch (unknown)"); - if (msg) - free(msg); - return ret; + if (loop()) + goto unlock_and_fail; + + if (write_ref_log_details) { + msg = xmalloc(strlen(write_ref_log_details) + 12); + sprintf(msg, "fetch from %s", write_ref_log_details); + } else { + msg = NULL; } + for (i = 0; i < targets; i++) { + if (!write_ref[i]) + continue; + ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch (unknown)"); + lock[i] = NULL; + if (ret) + goto unlock_and_fail; + } + if (msg) + free(msg); + return 0; + + +unlock_and_fail: + for (i = 0; i < targets; i++) + if (lock[i]) + unlock_ref(lock[i]); + return -1; } diff --git a/fetch.h b/fetch.h index 7bda355bc1..75e48af780 100644 --- a/fetch.h +++ b/fetch.h @@ -42,7 +42,7 @@ extern void pull_say(const char *, const char *); /* If write_ref is set, the ref filename to write the target value to. */ /* If write_ref_log_details is set, additional text will appear in the ref log. */ -extern int pull(char *target, const char *write_ref, +extern int pull(int targets, char **target, const char **write_ref, const char *write_ref_log_details); #endif /* PULL_H */ diff --git a/http-fetch.c b/http-fetch.c index 963d439f82..bc67db1add 100644 --- a/http-fetch.c +++ b/http-fetch.c @@ -1268,7 +1268,7 @@ int main(int argc, char **argv) alt->path_len = strlen(path); } - if (pull(commit_id, write_ref, url)) + if (pull(1, &commit_id, &write_ref, url)) rc = 1; http_cleanup(); diff --git a/local-fetch.c b/local-fetch.c index 452b83396c..1be73904f1 100644 --- a/local-fetch.c +++ b/local-fetch.c @@ -242,7 +242,7 @@ int main(int argc, char **argv) commit_id = argv[arg]; path = argv[arg + 1]; - if (pull(commit_id, write_ref, path)) + if (pull(1, &commit_id, &write_ref, path)) return 1; return 0; diff --git a/ssh-fetch.c b/ssh-fetch.c index aef3aa4a05..6e16568f88 100644 --- a/ssh-fetch.c +++ b/ssh-fetch.c @@ -167,7 +167,7 @@ int main(int argc, char **argv) if (get_version()) return 1; - if (pull(commit_id, write_ref, url)) + if (pull(1, &commit_id, &write_ref, url)) return 1; return 0; From 8e87ca661513d9f4b737295b234e93767cd2ee0c Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Thu, 27 Jul 2006 23:56:19 +0200 Subject: [PATCH 15/27] Teach git-local-fetch the --stdin switch This makes it possible to fetch many commits (refs) at once, greatly speeding up cg-clone. Signed-off-by: Petr Baudis Signed-off-by: Junio C Hamano --- Documentation/git-local-fetch.txt | 6 +++++ fetch.c | 40 +++++++++++++++++++++++++++++++ fetch.h | 6 +++++ local-fetch.c | 32 +++++++++++++++++-------- 4 files changed, 74 insertions(+), 10 deletions(-) diff --git a/Documentation/git-local-fetch.txt b/Documentation/git-local-fetch.txt index 87abec1c4e..2fbdfe086a 100644 --- a/Documentation/git-local-fetch.txt +++ b/Documentation/git-local-fetch.txt @@ -29,6 +29,12 @@ OPTIONS Writes the commit-id into the filename under $GIT_DIR/refs/ on the local end after the transfer is complete. +--stdin:: + Instead of a commit id on the commandline (which is not expected in this + case), 'git-local-fetch' expects lines on stdin in the format + + ['\t'] + Author ------ Written by Junio C Hamano diff --git a/fetch.c b/fetch.c index 281df61e7b..2151c7b78c 100644 --- a/fetch.c +++ b/fetch.c @@ -7,6 +7,7 @@ #include "tag.h" #include "blob.h" #include "refs.h" +#include "strbuf.h" int get_tree = 0; int get_history = 0; @@ -210,6 +211,45 @@ static int mark_complete(const char *path, const unsigned char *sha1) return 0; } +int pull_targets_stdin(char ***target, const char ***write_ref) +{ + int targets = 0, targets_alloc = 0; + struct strbuf buf; + *target = NULL; *write_ref = NULL; + strbuf_init(&buf); + while (1) { + char *rf_one = NULL; + char *tg_one; + + read_line(&buf, stdin, '\n'); + if (buf.eof) + break; + tg_one = buf.buf; + rf_one = strchr(tg_one, '\t'); + if (rf_one) + *rf_one++ = 0; + + if (targets >= targets_alloc) { + targets_alloc = targets_alloc ? targets_alloc * 2 : 64; + *target = xrealloc(*target, targets_alloc * sizeof(**target)); + *write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref)); + } + (*target)[targets] = strdup(tg_one); + (*write_ref)[targets] = rf_one ? strdup(rf_one) : NULL; + targets++; + } + return targets; +} + +void pull_targets_free(int targets, char **target, const char **write_ref) +{ + while (targets--) { + free(target[targets]); + if (write_ref[targets]) + free((char *) write_ref[targets]); + } +} + int pull(int targets, char **target, const char **write_ref, const char *write_ref_log_details) { diff --git a/fetch.h b/fetch.h index 75e48af780..be48c6f190 100644 --- a/fetch.h +++ b/fetch.h @@ -40,6 +40,12 @@ extern int get_recover; /* Report what we got under get_verbosely */ extern void pull_say(const char *, const char *); +/* Load pull targets from stdin */ +extern int pull_targets_stdin(char ***target, const char ***write_ref); + +/* Free up loaded targets */ +extern void pull_targets_free(int targets, char **target, const char **write_ref); + /* If write_ref is set, the ref filename to write the target value to. */ /* If write_ref_log_details is set, additional text will appear in the ref log. */ extern int pull(int targets, char **target, const char **write_ref, diff --git a/local-fetch.c b/local-fetch.c index 1be73904f1..b216bdd557 100644 --- a/local-fetch.c +++ b/local-fetch.c @@ -8,8 +8,9 @@ static int use_link = 0; static int use_symlink = 0; static int use_filecopy = 1; +static int commits_on_stdin = 0; -static char *path; /* "Remote" git repository */ +static const char *path; /* "Remote" git repository */ void prefetch(unsigned char *sha1) { @@ -194,7 +195,7 @@ int fetch_ref(char *ref, unsigned char *sha1) } static const char local_pull_usage[] = -"git-local-fetch [-c] [-t] [-a] [-v] [-w filename] [--recover] [-l] [-s] [-n] commit-id path"; +"git-local-fetch [-c] [-t] [-a] [-v] [-w filename] [--recover] [-l] [-s] [-n] [--stdin] commit-id path"; /* * By default we only use file copy. @@ -202,10 +203,11 @@ static const char local_pull_usage[] = * If -s is specified, then a symlink is attempted. * If -n is _not_ specified, then a regular file-to-file copy is done. */ -int main(int argc, char **argv) +int main(int argc, const char **argv) { - const char *write_ref = NULL; - char *commit_id; + int commits; + const char **write_ref = NULL; + char **commit_id; int arg = 1; setup_git_directory(); @@ -230,20 +232,30 @@ int main(int argc, char **argv) else if (argv[arg][1] == 'v') get_verbosely = 1; else if (argv[arg][1] == 'w') - write_ref = argv[++arg]; + write_ref = &argv[++arg]; else if (!strcmp(argv[arg], "--recover")) get_recover = 1; + else if (!strcmp(argv[arg], "--stdin")) + commits_on_stdin = 1; else usage(local_pull_usage); arg++; } - if (argc < arg + 2) + if (argc < arg + 2 - commits_on_stdin) usage(local_pull_usage); - commit_id = argv[arg]; - path = argv[arg + 1]; + if (commits_on_stdin) { + commits = pull_targets_stdin(&commit_id, &write_ref); + } else { + commit_id = (char **) &argv[arg++]; + commits = 1; + } + path = argv[arg]; - if (pull(1, &commit_id, &write_ref, path)) + if (pull(commits, commit_id, write_ref, path)) return 1; + if (commits_on_stdin) + pull_targets_free(commits, commit_id, write_ref); + return 0; } From 8e29f6a07e321a24f1d4f579817f5a8a43cdac05 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Thu, 27 Jul 2006 23:56:22 +0200 Subject: [PATCH 16/27] Teach git-http-fetch the --stdin switch Speeds up things quite a lot when fetching tags with Cogito. Signed-off-by: Petr Baudis Signed-off-by: Junio C Hamano --- Documentation/git-http-fetch.txt | 8 +++++- http-fetch.c | 45 ++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/Documentation/git-http-fetch.txt b/Documentation/git-http-fetch.txt index bc1a132891..3d508094af 100644 --- a/Documentation/git-http-fetch.txt +++ b/Documentation/git-http-fetch.txt @@ -8,7 +8,7 @@ git-http-fetch - downloads a remote git repository via HTTP SYNOPSIS -------- -'git-http-fetch' [-c] [-t] [-a] [-d] [-v] [-w filename] [--recover] +'git-http-fetch' [-c] [-t] [-a] [-d] [-v] [-w filename] [--recover] [--stdin] DESCRIPTION ----------- @@ -33,6 +33,12 @@ commit-id:: Writes the commit-id into the filename under $GIT_DIR/refs/ on the local end after the transfer is complete. +--stdin:: + Instead of a commit id on the commandline (which is not expected in this + case), 'git-http-fetch' expects lines on stdin in the format + + ['\t'] + Author ------ Written by Linus Torvalds diff --git a/http-fetch.c b/http-fetch.c index bc67db1add..1aad39b4d8 100644 --- a/http-fetch.c +++ b/http-fetch.c @@ -36,6 +36,8 @@ enum XML_Status { #define PREV_BUF_SIZE 4096 #define RANGE_HEADER_SIZE 30 +static int commits_on_stdin = 0; + static int got_alternates = -1; static int corrupt_object_found = 0; @@ -43,7 +45,7 @@ static struct curl_slist *no_pragma_header; struct alt_base { - char *base; + const char *base; int path_len; int got_indices; struct packed_git *packs; @@ -81,7 +83,7 @@ struct object_request }; struct alternates_request { - char *base; + const char *base; char *url; struct buffer *buffer; struct active_request_slot *slot; @@ -142,7 +144,7 @@ static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb, return size; } -static void fetch_alternates(char *base); +static void fetch_alternates(const char *base); static void process_object_response(void *callback_data); @@ -507,7 +509,7 @@ static void process_alternates_response(void *callback_data) (struct alternates_request *)callback_data; struct active_request_slot *slot = alt_req->slot; struct alt_base *tail = alt; - char *base = alt_req->base; + const char *base = alt_req->base; static const char null_byte = '\0'; char *data; int i = 0; @@ -612,7 +614,7 @@ static void process_alternates_response(void *callback_data) got_alternates = 1; } -static void fetch_alternates(char *base) +static void fetch_alternates(const char *base) { struct buffer buffer; char *url; @@ -1185,7 +1187,7 @@ int fetch_ref(char *ref, unsigned char *sha1) char *url; char hex[42]; struct buffer buffer; - char *base = alt->base; + const char *base = alt->base; struct active_request_slot *slot; struct slot_results results; buffer.size = 41; @@ -1214,11 +1216,12 @@ int fetch_ref(char *ref, unsigned char *sha1) return 0; } -int main(int argc, char **argv) +int main(int argc, const char **argv) { - const char *write_ref = NULL; - char *commit_id; - char *url; + int commits; + const char **write_ref = NULL; + char **commit_id; + const char *url; char *path; int arg = 1; int rc = 0; @@ -1238,19 +1241,26 @@ int main(int argc, char **argv) } else if (argv[arg][1] == 'v') { get_verbosely = 1; } else if (argv[arg][1] == 'w') { - write_ref = argv[arg + 1]; + write_ref = &argv[arg + 1]; arg++; } else if (!strcmp(argv[arg], "--recover")) { get_recover = 1; + } else if (!strcmp(argv[arg], "--stdin")) { + commits_on_stdin = 1; } arg++; } - if (argc < arg + 2) { - usage("git-http-fetch [-c] [-t] [-a] [-v] [--recover] [-w ref] commit-id url"); + if (argc < arg + 2 - commits_on_stdin) { + usage("git-http-fetch [-c] [-t] [-a] [-v] [--recover] [-w ref] [--stdin] commit-id url"); return 1; } - commit_id = argv[arg]; - url = argv[arg + 1]; + if (commits_on_stdin) { + commits = pull_targets_stdin(&commit_id, &write_ref); + } else { + commit_id = (char **) &argv[arg++]; + commits = 1; + } + url = argv[arg]; http_init(); @@ -1268,13 +1278,16 @@ int main(int argc, char **argv) alt->path_len = strlen(path); } - if (pull(1, &commit_id, &write_ref, url)) + if (pull(commits, commit_id, write_ref, url)) rc = 1; http_cleanup(); curl_slist_free_all(no_pragma_header); + if (commits_on_stdin) + pull_targets_free(commits, commit_id, write_ref); + if (corrupt_object_found) { fprintf(stderr, "Some loose object were found to be corrupt, but they might be just\n" From 5f468c4805c785115cd9c5f6a8f299f23a9034f5 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 27 Jul 2006 22:15:01 -0700 Subject: [PATCH 17/27] lost-found: use fsck-objects --full Signed-off-by: Junio C Hamano --- git-lost-found.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-lost-found.sh b/git-lost-found.sh index ba6d587f31..b928f2ca52 100755 --- a/git-lost-found.sh +++ b/git-lost-found.sh @@ -12,7 +12,7 @@ fi laf="$GIT_DIR/lost-found" rm -fr "$laf" && mkdir -p "$laf/commit" "$laf/other" || exit -git fsck-objects | +git fsck-objects --full | while read dangling type sha1 do case "$dangling" in From dee4e384f376020e08cb78f6dfaf00ae84e97a9e Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 27 Jul 2006 22:27:44 -0700 Subject: [PATCH 18/27] git-reset: detect update-ref error and report it. Signed-off-by: Junio C Hamano --- git-reset.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/git-reset.sh b/git-reset.sh index 5c0224090a..36fc8ce25b 100755 --- a/git-reset.sh +++ b/git-reset.sh @@ -52,7 +52,8 @@ then else rm -f "$GIT_DIR/ORIG_HEAD" fi -git-update-ref -m "reset $reset_type $@" HEAD "$rev" +git-update-ref -m "reset $reset_type $*" HEAD "$rev" +update_ref_status=$? case "$reset_type" in --hard ) @@ -66,3 +67,5 @@ case "$reset_type" in esac rm -f "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/rr-cache/MERGE_RR" "$GIT_DIR/SQUASH_MSG" + +exit $update_ref_status From ef1d9c5aa4c8fd57b2a8043c0cd9fea1c507db6a Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 27 Jul 2006 22:55:44 -0700 Subject: [PATCH 19/27] log and diff family: honor config even from subdirectories There currently is an unfortunate circular dependency between what init_revisions (the command line revision specification parser) does and setting up the log and diff options. The function uses setup_git_directory() to find the root of the project relative to the current directory and calls diff_setup() to prepare diff generation. However, some of the things that diff_setup() does needs to depend on the configuration variable, which needs to be read after setup_git_directory() is called. This patch is a low impact workaround. It first lets init_revisions() to run and do its thing, then uses git_config() and diff_setup() after it returns, so that configuration variables that affects the diff operation can be used from subdirectories. Signed-off-by: Junio C Hamano --- builtin-diff-files.c | 2 +- builtin-diff-index.c | 2 +- builtin-diff-tree.c | 2 +- builtin-diff.c | 3 ++- builtin-log.c | 9 ++++++--- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/builtin-diff-files.c b/builtin-diff-files.c index 81ac2fe64a..2e10118623 100644 --- a/builtin-diff-files.c +++ b/builtin-diff-files.c @@ -18,8 +18,8 @@ int cmd_diff_files(int argc, const char **argv, char **envp) struct rev_info rev; int silent = 0; - git_config(git_default_config); /* no "diff" UI options */ init_revisions(&rev); + git_config(git_default_config); /* no "diff" UI options */ rev.abbrev = 0; argc = setup_revisions(argc, argv, &rev, NULL); diff --git a/builtin-diff-index.c b/builtin-diff-index.c index a1fa1b85cf..dc52c054ee 100644 --- a/builtin-diff-index.c +++ b/builtin-diff-index.c @@ -15,8 +15,8 @@ int cmd_diff_index(int argc, const char **argv, char **envp) int cached = 0; int i; - git_config(git_default_config); /* no "diff" UI options */ init_revisions(&rev); + git_config(git_default_config); /* no "diff" UI options */ rev.abbrev = 0; argc = setup_revisions(argc, argv, &rev, NULL); diff --git a/builtin-diff-tree.c b/builtin-diff-tree.c index b610668594..8957b459de 100644 --- a/builtin-diff-tree.c +++ b/builtin-diff-tree.c @@ -67,9 +67,9 @@ int cmd_diff_tree(int argc, const char **argv, char **envp) static struct rev_info *opt = &log_tree_opt; int read_stdin = 0; + init_revisions(opt); git_config(git_default_config); /* no "diff" UI options */ nr_sha1 = 0; - init_revisions(opt); opt->abbrev = 0; opt->diff = 1; argc = setup_revisions(argc, argv, opt, NULL); diff --git a/builtin-diff.c b/builtin-diff.c index cb38f44561..7d5ad6271e 100644 --- a/builtin-diff.c +++ b/builtin-diff.c @@ -250,8 +250,9 @@ int cmd_diff(int argc, const char **argv, char **envp) * Other cases are errors. */ - git_config(git_diff_ui_config); init_revisions(&rev); + git_config(git_diff_ui_config); + diff_setup(&rev.diffopt); argc = setup_revisions(argc, argv, &rev, NULL); if (!rev.diffopt.output_format) { diff --git a/builtin-log.c b/builtin-log.c index 4052cc75bd..88c835acba 100644 --- a/builtin-log.c +++ b/builtin-log.c @@ -49,8 +49,9 @@ int cmd_whatchanged(int argc, const char **argv, char **envp) { struct rev_info rev; - git_config(git_diff_ui_config); init_revisions(&rev); + git_config(git_diff_ui_config); + diff_setup(&rev.diffopt); rev.diff = 1; rev.diffopt.recursive = 1; rev.simplify_history = 0; @@ -64,8 +65,9 @@ int cmd_show(int argc, const char **argv, char **envp) { struct rev_info rev; - git_config(git_diff_ui_config); init_revisions(&rev); + git_config(git_diff_ui_config); + diff_setup(&rev.diffopt); rev.diff = 1; rev.diffopt.recursive = 1; rev.combine_merges = 1; @@ -81,8 +83,9 @@ int cmd_log(int argc, const char **argv, char **envp) { struct rev_info rev; - git_config(git_diff_ui_config); init_revisions(&rev); + git_config(git_diff_ui_config); + diff_setup(&rev.diffopt); rev.always_show_header = 1; cmd_log_init(argc, argv, envp, &rev); return cmd_log_walk(&rev); From 2941cab99ca9dec3356f0126be41bc3995bb1fef Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 28 Jul 2006 11:17:46 +0200 Subject: [PATCH 20/27] Makefile: ssh-pull.o depends on ssh-fetch.c Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index 49eaa10b3b..8349e3defa 100644 --- a/Makefile +++ b/Makefile @@ -614,6 +614,8 @@ $(SIMPLE_PROGRAMS) : git-%$X : %.o $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \ $(LIB_FILE) $(SIMPLE_LIB) +ssh-pull.o: ssh-fetch.c +ssh-push.o: ssh-upload.c git-local-fetch$X: fetch.o git-ssh-fetch$X: rsh.o fetch.o git-ssh-upload$X: rsh.o From e5a94313c06b83e034179ca6092ea23343e48dec Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 28 Jul 2006 17:46:11 +0200 Subject: [PATCH 21/27] Teach git-apply about '-R' Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- builtin-apply.c | 65 ++++++++++++++++++++++++++++++++++------- t/t4102-apply-rename.sh | 24 +++++++++++++-- 2 files changed, 77 insertions(+), 12 deletions(-) diff --git a/builtin-apply.c b/builtin-apply.c index d924ac3d0a..6b38a8ad0d 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -120,7 +120,7 @@ struct fragment { struct patch { char *new_name, *old_name, *def_name; unsigned int old_mode, new_mode; - int is_rename, is_copy, is_new, is_delete, is_binary; + int is_rename, is_copy, is_new, is_delete, is_binary, is_reverse; #define BINARY_DELTA_DEFLATED 1 #define BINARY_LITERAL_DEFLATED 2 unsigned long deflate_origlen; @@ -1119,6 +1119,34 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch) return offset + hdrsize + patchsize; } +#define swap(a,b) myswap((a),(b),sizeof(a)) + +#define myswap(a, b, size) do { \ + unsigned char mytmp[size]; \ + memcpy(mytmp, &a, size); \ + memcpy(&a, &b, size); \ + memcpy(&b, mytmp, size); \ +} while (0) + +static void reverse_patches(struct patch *p) +{ + for (; p; p = p->next) { + struct fragment *frag = p->fragments; + + swap(p->new_name, p->old_name); + swap(p->new_mode, p->old_mode); + swap(p->is_new, p->is_delete); + swap(p->lines_added, p->lines_deleted); + swap(p->old_sha1_prefix, p->new_sha1_prefix); + + for (; frag; frag = frag->next) { + swap(frag->newpos, frag->oldpos); + swap(frag->newlines, frag->oldlines); + } + p->is_reverse = !p->is_reverse; + } +} + static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"; static const char minuses[]= "----------------------------------------------------------------------"; @@ -1336,7 +1364,7 @@ static int apply_line(char *output, const char *patch, int plen) } static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, - int inaccurate_eof) + int reverse, int inaccurate_eof) { int match_beginning, match_end; char *buf = desc->buffer; @@ -1350,6 +1378,7 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, int pos, lines; while (size > 0) { + char first; int len = linelen(patch, size); int plen; @@ -1366,16 +1395,23 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, plen = len-1; if (len < size && patch[len] == '\\') plen--; - switch (*patch) { + first = *patch; + if (reverse) { + if (first == '-') + first = '+'; + else if (first == '+') + first = '-'; + } + switch (first) { case ' ': case '-': memcpy(old + oldsize, patch + 1, plen); oldsize += plen; - if (*patch == '-') + if (first == '-') break; /* Fall-through for ' ' */ case '+': - if (*patch != '+' || !no_add) + if (first != '+' || !no_add) newsize += apply_line(new + newsize, patch, plen); break; @@ -1615,7 +1651,8 @@ static int apply_fragments(struct buffer_desc *desc, struct patch *patch) return apply_binary(desc, patch); while (frag) { - if (apply_one_fragment(desc, frag, patch->inaccurate_eof) < 0) + if (apply_one_fragment(desc, frag, patch->is_reverse, + patch->inaccurate_eof) < 0) return error("patch failed: %s:%ld", name, frag->oldpos); frag = frag->next; @@ -2142,7 +2179,8 @@ static int use_patch(struct patch *p) return 1; } -static int apply_patch(int fd, const char *filename, int inaccurate_eof) +static int apply_patch(int fd, const char *filename, + int reverse, int inaccurate_eof) { unsigned long offset, size; char *buffer = read_patch_file(fd, &size); @@ -2162,6 +2200,8 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof) nr = parse_chunk(buffer + offset, size, patch); if (nr < 0) break; + if (reverse) + reverse_patches(patch); if (use_patch(patch)) { patch_stats(patch); *listp = patch; @@ -2226,6 +2266,7 @@ int cmd_apply(int argc, const char **argv, char **envp) { int i; int read_stdin = 1; + int reverse = 0; int inaccurate_eof = 0; const char *whitespace_option = NULL; @@ -2236,7 +2277,7 @@ int cmd_apply(int argc, const char **argv, char **envp) int fd; if (!strcmp(arg, "-")) { - apply_patch(0, "", inaccurate_eof); + apply_patch(0, "", reverse, inaccurate_eof); read_stdin = 0; continue; } @@ -2313,6 +2354,10 @@ int cmd_apply(int argc, const char **argv, char **envp) parse_whitespace_option(arg + 13); continue; } + if (!strcmp(arg, "-R") || !strcmp(arg, "--reverse")) { + reverse = 1; + continue; + } if (!strcmp(arg, "--inaccurate-eof")) { inaccurate_eof = 1; continue; @@ -2333,12 +2378,12 @@ int cmd_apply(int argc, const char **argv, char **envp) usage(apply_usage); read_stdin = 0; set_default_whitespace_mode(whitespace_option); - apply_patch(fd, arg, inaccurate_eof); + apply_patch(fd, arg, reverse, inaccurate_eof); close(fd); } set_default_whitespace_mode(whitespace_option); if (read_stdin) - apply_patch(0, "", inaccurate_eof); + apply_patch(0, "", reverse, inaccurate_eof); if (whitespace_error) { if (squelch_whitespace_errors && squelch_whitespace_errors < whitespace_error) { diff --git a/t/t4102-apply-rename.sh b/t/t4102-apply-rename.sh index fbb508d389..22da6a00cc 100755 --- a/t/t4102-apply-rename.sh +++ b/t/t4102-apply-rename.sh @@ -13,8 +13,8 @@ test_description='git-apply handling copy/rename patch. cat >test-patch <<\EOF diff --git a/foo b/bar similarity index 47% -copy from foo -copy to bar +rename from foo +rename to bar --- a/foo +++ b/bar @@ -1 +1 @@ @@ -39,4 +39,24 @@ else 'test -f bar && ls -l bar | grep "^-..x......"' fi +test_expect_success 'apply reverse' \ + 'git-apply -R --index --stat --summary --apply test-patch && + test "$(cat foo)" = "This is foo"' + +cat >test-patch <<\EOF +diff --git a/foo b/bar +similarity index 47% +copy from foo +copy to bar +--- a/foo ++++ b/bar +@@ -1 +1 @@ +-This is foo ++This is bar +EOF + +test_expect_success 'apply copy' \ + 'git-apply --index --stat --summary --apply test-patch && + test "$(cat bar)" = "This is bar" -a "$(cat foo)" = "This is foo"' + test_done From ab9f30fd7538ec5385bf5a3d11117f23f4f320ee Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 28 Jul 2006 12:21:17 -0700 Subject: [PATCH 22/27] git-apply -R: binary patches are irreversible for now. Signed-off-by: Junio C Hamano --- builtin-apply.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/builtin-apply.c b/builtin-apply.c index 6b38a8ad0d..d4381d9a8f 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -1535,6 +1535,12 @@ static int apply_binary_fragment(struct buffer_desc *desc, struct patch *patch) void *data; void *result; + /* Binary patch is irreversible */ + if (patch->is_reverse) + return error("cannot reverse-apply a binary patch to '%s'", + patch->new_name + ? patch->new_name : patch->old_name); + data = inflate_it(fragment->patch, fragment->size, patch->deflate_origlen); if (!data) From 5afa69b415fd020d6dd36751bdcf45fdbf07dd4a Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 28 Jul 2006 12:23:28 -0700 Subject: [PATCH 23/27] t4103: fix binary patch application test. The generated binary patch was _not_ binary -- earlier I made the --full-index flag to imply binary patch generation to the diff machinery, but later we made it independent from --binary (although the latter implies the former). Signed-off-by: Junio C Hamano --- t/t4103-apply-binary.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/t4103-apply-binary.sh b/t/t4103-apply-binary.sh index 00bd8b15c6..ff052699a2 100755 --- a/t/t4103-apply-binary.sh +++ b/t/t4103-apply-binary.sh @@ -35,8 +35,8 @@ git-commit -m 'Second Version' git-diff-tree -p master binary >B.diff git-diff-tree -p -C master binary >C.diff -git-diff-tree -p --full-index master binary >BF.diff -git-diff-tree -p --full-index -C master binary >CF.diff +git-diff-tree -p --binary master binary >BF.diff +git-diff-tree -p --binary -C master binary >CF.diff test_expect_success 'stat binary diff -- should not fail.' \ 'git-checkout master From 1b03dfed182a1dc47cc0eb1047a34cd914440ce6 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sat, 29 Jul 2006 02:10:07 +0200 Subject: [PATCH 24/27] Fix http-fetch With the latest changes in fetch.c, http-fetch crashed accessing write_ref[i], where write_ref was NULL. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- fetch.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fetch.c b/fetch.c index 2151c7b78c..aeb6bf2639 100644 --- a/fetch.c +++ b/fetch.c @@ -245,7 +245,7 @@ void pull_targets_free(int targets, char **target, const char **write_ref) { while (targets--) { free(target[targets]); - if (write_ref[targets]) + if (write_ref && write_ref[targets]) free((char *) write_ref[targets]); } } @@ -263,7 +263,7 @@ int pull(int targets, char **target, const char **write_ref, track_object_refs = 0; for (i = 0; i < targets; i++) { - if (!write_ref[i]) + if (!write_ref || !write_ref[i]) continue; lock[i] = lock_ref_sha1(write_ref[i], NULL, 0); @@ -295,7 +295,7 @@ int pull(int targets, char **target, const char **write_ref, msg = NULL; } for (i = 0; i < targets; i++) { - if (!write_ref[i]) + if (!write_ref || !write_ref[i]) continue; ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch (unknown)"); lock[i] = NULL; From 818f477c40fee62ab8ea5b493c51bb357f38957c Mon Sep 17 00:00:00 2001 From: Shawn Pearce Date: Fri, 28 Jul 2006 23:44:51 -0400 Subject: [PATCH 25/27] Display an error from update-ref if target ref name is invalid. Alex Riesen (raa.lkml@gmail.com) recently observed that git branch would fail with no error message due to unexpected situations with regards to refs. For example, if .git/refs/heads/gu is a file but "git branch -b refs/heads/gu/fixa HEAD" was invoked by the user it would fail silently due to refs/heads/gu being a file and not a directory. This change adds a test for trying to create a ref within a directory that is actually currently a file, and adds error printing within the ref locking routine should the resolve operation fail. The error printing code probably belongs at this level of the library as other failures within the ref locking, writing and logging code are also currently at this level of the code. Signed-off-by: Shawn O. Pearce Signed-off-by: Junio C Hamano --- refs.c | 5 +++++ t/t1400-update-ref.sh | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/refs.c b/refs.c index 56db394459..02850b6908 100644 --- a/refs.c +++ b/refs.c @@ -294,6 +294,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *path, int plen, const unsigned char *old_sha1, int mustexist) { + const char *orig_path = path; struct ref_lock *lock; struct stat st; @@ -303,7 +304,11 @@ static struct ref_lock *lock_ref_sha1_basic(const char *path, plen = strlen(path) - plen; path = resolve_ref(path, lock->old_sha1, mustexist); if (!path) { + int last_errno = errno; + error("unable to resolve reference %s: %s", + orig_path, strerror(errno)); unlock_ref(lock); + errno = last_errno; return NULL; } lock->lk = xcalloc(1, sizeof(struct lock_file)); diff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh index 04fab26621..ddc80bbeae 100755 --- a/t/t1400-update-ref.sh +++ b/t/t1400-update-ref.sh @@ -14,6 +14,8 @@ D=4444444444444444444444444444444444444444 E=5555555555555555555555555555555555555555 F=6666666666666666666666666666666666666666 m=refs/heads/master +n_dir=refs/heads/gu +n=$n_dir/fixes test_expect_success \ "create $m" \ @@ -25,6 +27,16 @@ test_expect_success \ test $B = $(cat .git/$m)' rm -f .git/$m +test_expect_success \ + "fail to create $n" \ + 'touch .git/$n_dir + git-update-ref $n $A >out 2>err + test $? = 1 && + test "" = "$(cat out)" && + grep "error: unable to resolve reference" err && + grep $n err' +rm -f .git/$n_dir out err + test_expect_success \ "create $m (by HEAD)" \ 'git-update-ref HEAD $A && From db6296a566eb1a8007a84330a911b38055720743 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Fri, 28 Jul 2006 21:21:48 -0700 Subject: [PATCH 26/27] Call setup_git_directory() early Any git command that expects to work in a subdirectory of a project, and that reads the git config files (which is just about all of them) needs to make sure that it does the "setup_git_directory()" call before it tries to read the config file. This means, among other things, that we need to move the call out of "init_revisions()", and into the caller. This does the mostly trivial conversion to do that. Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- blame.c | 2 +- builtin-diff-files.c | 2 +- builtin-diff-index.c | 2 +- builtin-diff-tree.c | 2 +- builtin-diff.c | 6 +++--- builtin-fmt-merge-msg.c | 3 ++- builtin-log.c | 18 ++++++++++-------- builtin-prune.c | 2 +- builtin-rev-list.c | 2 +- http-push.c | 2 +- revision.c | 4 ++-- revision.h | 2 +- 12 files changed, 25 insertions(+), 22 deletions(-) diff --git a/blame.c b/blame.c index b04b8f58aa..76712b5962 100644 --- a/blame.c +++ b/blame.c @@ -834,7 +834,7 @@ int main(int argc, const char **argv) } - init_revisions(&rev); + init_revisions(&rev, setup_git_directory()); rev.remove_empty_trees = 1; rev.topo_order = 1; rev.prune_fn = simplify_commit; diff --git a/builtin-diff-files.c b/builtin-diff-files.c index 2e10118623..ea2936a5ec 100644 --- a/builtin-diff-files.c +++ b/builtin-diff-files.c @@ -18,7 +18,7 @@ int cmd_diff_files(int argc, const char **argv, char **envp) struct rev_info rev; int silent = 0; - init_revisions(&rev); + init_revisions(&rev, setup_git_directory()); git_config(git_default_config); /* no "diff" UI options */ rev.abbrev = 0; diff --git a/builtin-diff-index.c b/builtin-diff-index.c index dc52c054ee..eeeee93fb7 100644 --- a/builtin-diff-index.c +++ b/builtin-diff-index.c @@ -15,7 +15,7 @@ int cmd_diff_index(int argc, const char **argv, char **envp) int cached = 0; int i; - init_revisions(&rev); + init_revisions(&rev, setup_git_directory()); git_config(git_default_config); /* no "diff" UI options */ rev.abbrev = 0; diff --git a/builtin-diff-tree.c b/builtin-diff-tree.c index 8957b459de..f8215ea707 100644 --- a/builtin-diff-tree.c +++ b/builtin-diff-tree.c @@ -67,7 +67,7 @@ int cmd_diff_tree(int argc, const char **argv, char **envp) static struct rev_info *opt = &log_tree_opt; int read_stdin = 0; - init_revisions(opt); + init_revisions(opt, setup_git_directory()); git_config(git_default_config); /* no "diff" UI options */ nr_sha1 = 0; opt->abbrev = 0; diff --git a/builtin-diff.c b/builtin-diff.c index dca223235d..45b27cfca3 100644 --- a/builtin-diff.c +++ b/builtin-diff.c @@ -227,7 +227,7 @@ int cmd_diff(int argc, const char **argv, char **envp) struct rev_info rev; struct object_array_entry ent[100]; int ents = 0, blobs = 0, paths = 0; - const char *path = NULL; + const char *path = NULL, *prefix; struct blobinfo blob[2]; /* @@ -250,9 +250,9 @@ int cmd_diff(int argc, const char **argv, char **envp) * Other cases are errors. */ - init_revisions(&rev); + prefix = setup_git_directory(); git_config(git_diff_ui_config); - diff_setup(&rev.diffopt); + init_revisions(&rev, prefix); argc = setup_revisions(argc, argv, &rev, NULL); if (!rev.diffopt.output_format) { diff --git a/builtin-fmt-merge-msg.c b/builtin-fmt-merge-msg.c index f20b27b8a9..338f2094f3 100644 --- a/builtin-fmt-merge-msg.c +++ b/builtin-fmt-merge-msg.c @@ -250,6 +250,7 @@ int cmd_fmt_merge_msg(int argc, char **argv, char **envp) const char *sep = ""; unsigned char head_sha1[20]; const char *head, *current_branch; + const char *prefix = setup_git_directory(); git_config(fmt_merge_msg_config); @@ -342,7 +343,7 @@ int cmd_fmt_merge_msg(int argc, char **argv, char **envp) struct rev_info rev; head = lookup_commit(head_sha1); - init_revisions(&rev); + init_revisions(&rev, prefix); rev.commit_format = CMIT_FMT_ONELINE; rev.ignore_merges = 1; rev.limited = 1; diff --git a/builtin-log.c b/builtin-log.c index 88c835acba..52064cd178 100644 --- a/builtin-log.c +++ b/builtin-log.c @@ -48,10 +48,10 @@ static int cmd_log_walk(struct rev_info *rev) int cmd_whatchanged(int argc, const char **argv, char **envp) { struct rev_info rev; + const char *prefix = setup_git_directory(); - init_revisions(&rev); git_config(git_diff_ui_config); - diff_setup(&rev.diffopt); + init_revisions(&rev, prefix); rev.diff = 1; rev.diffopt.recursive = 1; rev.simplify_history = 0; @@ -64,10 +64,10 @@ int cmd_whatchanged(int argc, const char **argv, char **envp) int cmd_show(int argc, const char **argv, char **envp) { struct rev_info rev; + const char *prefix = setup_git_directory(); - init_revisions(&rev); git_config(git_diff_ui_config); - diff_setup(&rev.diffopt); + init_revisions(&rev, prefix); rev.diff = 1; rev.diffopt.recursive = 1; rev.combine_merges = 1; @@ -82,10 +82,10 @@ int cmd_show(int argc, const char **argv, char **envp) int cmd_log(int argc, const char **argv, char **envp) { struct rev_info rev; + const char *prefix = setup_git_directory(); - init_revisions(&rev); git_config(git_diff_ui_config); - diff_setup(&rev.diffopt); + init_revisions(&rev, prefix); rev.always_show_header = 1; cmd_log_init(argc, argv, envp, &rev); return cmd_log_walk(&rev); @@ -188,6 +188,7 @@ static void get_patch_ids(struct rev_info *rev, struct diff_options *options) struct object *o1, *o2; unsigned flags1, flags2; unsigned char sha1[20]; + const char *prefix = setup_git_directory(); if (rev->pending.nr != 2) die("Need exactly one range."); @@ -206,7 +207,7 @@ static void get_patch_ids(struct rev_info *rev, struct diff_options *options) die("diff_setup_done failed"); /* given a range a..b get all patch ids for b..a */ - init_revisions(&check_rev); + init_revisions(&check_rev, prefix); o1->flags ^= UNINTERESTING; o2->flags ^= UNINTERESTING; add_pending_object(&check_rev, o1, "o1"); @@ -260,9 +261,10 @@ int cmd_format_patch(int argc, const char **argv, char **envp) char *add_signoff = NULL; char message_id[1024]; char ref_message_id[1024]; + const char *prefix = setup_git_directory(); git_config(git_format_config); - init_revisions(&rev); + init_revisions(&rev, prefix); rev.commit_format = CMIT_FMT_EMAIL; rev.verbose_header = 1; rev.diff = 1; diff --git a/builtin-prune.c b/builtin-prune.c index d196c41f13..4ed1e1b43c 100644 --- a/builtin-prune.c +++ b/builtin-prune.c @@ -234,7 +234,7 @@ int cmd_prune(int argc, const char **argv, char **envp) * Set up revision parsing, and mark us as being interested * in all object types, not just commits. */ - init_revisions(&revs); + init_revisions(&revs, setup_git_directory()); revs.tag_objects = 1; revs.blob_objects = 1; revs.tree_objects = 1; diff --git a/builtin-rev-list.c b/builtin-rev-list.c index 8f32871337..2b6691c8e4 100644 --- a/builtin-rev-list.c +++ b/builtin-rev-list.c @@ -311,7 +311,7 @@ int cmd_rev_list(int argc, const char **argv, char **envp) struct commit_list *list; int i; - init_revisions(&revs); + init_revisions(&revs, setup_git_directory()); revs.abbrev = 0; revs.commit_format = CMIT_FMT_UNSPECIFIED; argc = setup_revisions(argc, argv, &revs, NULL); diff --git a/http-push.c b/http-push.c index 47686195cd..4021e7d927 100644 --- a/http-push.c +++ b/http-push.c @@ -2521,7 +2521,7 @@ int main(int argc, char **argv) commit_argv[3] = old_sha1_hex; commit_argc++; } - init_revisions(&revs); + init_revisions(&revs, setup_git_directory()); setup_revisions(commit_argc, commit_argv, &revs, NULL); free(new_sha1_hex); if (old_sha1_hex) { diff --git a/revision.c b/revision.c index 874e349db8..a58257ad80 100644 --- a/revision.c +++ b/revision.c @@ -509,7 +509,7 @@ static int add_parents_only(struct rev_info *revs, const char *arg, int flags) return 1; } -void init_revisions(struct rev_info *revs) +void init_revisions(struct rev_info *revs, const char *prefix) { memset(revs, 0, sizeof(*revs)); @@ -521,7 +521,7 @@ void init_revisions(struct rev_info *revs) revs->pruning.change = file_change; revs->lifo = 1; revs->dense = 1; - revs->prefix = setup_git_directory(); + revs->prefix = prefix; revs->max_age = -1; revs->min_age = -1; revs->max_count = -1; diff --git a/revision.h b/revision.h index e23ec8f45a..0c3b8d9905 100644 --- a/revision.h +++ b/revision.h @@ -87,7 +87,7 @@ struct rev_info { extern int rev_same_tree_as_empty(struct rev_info *, struct tree *t1); extern int rev_compare_tree(struct rev_info *, struct tree *t1, struct tree *t2); -extern void init_revisions(struct rev_info *revs); +extern void init_revisions(struct rev_info *revs, const char *prefix); extern int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def); extern void prepare_revision_walk(struct rev_info *revs); extern struct commit *get_revision(struct rev_info *revs); From a633fca0c056aa221d23493c276d3713191621b3 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Fri, 28 Jul 2006 22:44:25 -0700 Subject: [PATCH 27/27] Call setup_git_directory() much earlier This changes the calling convention of built-in commands and passes the "prefix" (i.e. pathname of $PWD relative to the project root level) down to them. Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- builtin-add.c | 3 +- builtin-apply.c | 2 +- builtin-cat-file.c | 3 +- builtin-check-ref-format.c | 2 +- builtin-commit-tree.c | 4 +- builtin-count.c | 2 +- builtin-diff-files.c | 4 +- builtin-diff-index.c | 4 +- builtin-diff-stages.c | 3 +- builtin-diff-tree.c | 4 +- builtin-diff.c | 5 +-- builtin-fmt-merge-msg.c | 3 +- builtin-grep.c | 3 +- builtin-help.c | 4 +- builtin-init-db.c | 2 +- builtin-log.c | 25 +++++-------- builtin-ls-files.c | 3 +- builtin-ls-tree.c | 8 ++-- builtin-mailinfo.c | 2 +- builtin-mailsplit.c | 2 +- builtin-prune.c | 4 +- builtin-push.c | 2 +- builtin-read-tree.c | 3 +- builtin-rev-list.c | 4 +- builtin-rev-parse.c | 3 +- builtin-rm.c | 3 +- builtin-show-branch.c | 3 +- builtin-stripspace.c | 2 +- builtin-tar-tree.c | 9 ++--- builtin-update-index.c | 3 +- builtin-update-ref.c | 3 +- builtin-upload-tar.c | 2 +- builtin-write-tree.c | 4 +- builtin.h | 76 +++++++++++++++++++------------------- git.c | 63 +++++++++++++++++-------------- 35 files changed, 128 insertions(+), 144 deletions(-) diff --git a/builtin-add.c b/builtin-add.c index 3a73a173f7..0fa7dc1f11 100644 --- a/builtin-add.c +++ b/builtin-add.c @@ -123,11 +123,10 @@ static int add_file_to_index(const char *path, int verbose) static struct lock_file lock_file; -int cmd_add(int argc, const char **argv, char **envp) +int cmd_add(int argc, const char **argv, const char *prefix) { int i, newfd; int verbose = 0, show_only = 0; - const char *prefix = setup_git_directory(); const char **pathspec; struct dir_struct dir; diff --git a/builtin-apply.c b/builtin-apply.c index d4381d9a8f..f8c6763c74 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -2268,7 +2268,7 @@ static int git_apply_config(const char *var, const char *value) } -int cmd_apply(int argc, const char **argv, char **envp) +int cmd_apply(int argc, const char **argv, const char *prefix) { int i; int read_stdin = 1; diff --git a/builtin-cat-file.c b/builtin-cat-file.c index 4d36817e5f..814fb0743f 100644 --- a/builtin-cat-file.c +++ b/builtin-cat-file.c @@ -94,7 +94,7 @@ static int pprint_tag(const unsigned char *sha1, const char *buf, unsigned long return 0; } -int cmd_cat_file(int argc, const char **argv, char **envp) +int cmd_cat_file(int argc, const char **argv, const char *prefix) { unsigned char sha1[20]; char type[20]; @@ -102,7 +102,6 @@ int cmd_cat_file(int argc, const char **argv, char **envp) unsigned long size; int opt; - setup_git_directory(); git_config(git_default_config); if (argc != 3) usage("git-cat-file [-t|-s|-e|-p|] "); diff --git a/builtin-check-ref-format.c b/builtin-check-ref-format.c index 4a23936aff..701de439ae 100644 --- a/builtin-check-ref-format.c +++ b/builtin-check-ref-format.c @@ -6,7 +6,7 @@ #include "refs.h" #include "builtin.h" -int cmd_check_ref_format(int argc, const char **argv, char **envp) +int cmd_check_ref_format(int argc, const char **argv, const char *prefix) { if (argc != 2) usage("git check-ref-format refname"); diff --git a/builtin-commit-tree.c b/builtin-commit-tree.c index ec082bf754..9c98796671 100644 --- a/builtin-commit-tree.c +++ b/builtin-commit-tree.c @@ -77,7 +77,7 @@ static int new_parent(int idx) return 1; } -int cmd_commit_tree(int argc, const char **argv, char **envp) +int cmd_commit_tree(int argc, const char **argv, const char *prefix) { int i; int parents = 0; @@ -88,8 +88,6 @@ int cmd_commit_tree(int argc, const char **argv, char **envp) unsigned int size; setup_ident(); - setup_git_directory(); - git_config(git_default_config); if (argc < 2) diff --git a/builtin-count.c b/builtin-count.c index 5ee72df247..1d3729aa99 100644 --- a/builtin-count.c +++ b/builtin-count.c @@ -67,7 +67,7 @@ static void count_objects(DIR *d, char *path, int len, int verbose, } } -int cmd_count_objects(int ac, const char **av, char **ep) +int cmd_count_objects(int ac, const char **av, const char *prefix) { int i; int verbose = 0; diff --git a/builtin-diff-files.c b/builtin-diff-files.c index ea2936a5ec..ac13db70ff 100644 --- a/builtin-diff-files.c +++ b/builtin-diff-files.c @@ -13,12 +13,12 @@ static const char diff_files_usage[] = "git-diff-files [-q] [-0/-1/2/3 |-c|--cc] [] [...]" COMMON_DIFF_OPTIONS_HELP; -int cmd_diff_files(int argc, const char **argv, char **envp) +int cmd_diff_files(int argc, const char **argv, const char *prefix) { struct rev_info rev; int silent = 0; - init_revisions(&rev, setup_git_directory()); + init_revisions(&rev, prefix); git_config(git_default_config); /* no "diff" UI options */ rev.abbrev = 0; diff --git a/builtin-diff-index.c b/builtin-diff-index.c index eeeee93fb7..95a3db156b 100644 --- a/builtin-diff-index.c +++ b/builtin-diff-index.c @@ -9,13 +9,13 @@ static const char diff_cache_usage[] = "[] [...]" COMMON_DIFF_OPTIONS_HELP; -int cmd_diff_index(int argc, const char **argv, char **envp) +int cmd_diff_index(int argc, const char **argv, const char *prefix) { struct rev_info rev; int cached = 0; int i; - init_revisions(&rev, setup_git_directory()); + init_revisions(&rev, prefix); git_config(git_default_config); /* no "diff" UI options */ rev.abbrev = 0; diff --git a/builtin-diff-stages.c b/builtin-diff-stages.c index 9c62702941..5960e08997 100644 --- a/builtin-diff-stages.c +++ b/builtin-diff-stages.c @@ -55,10 +55,9 @@ static void diff_stages(int stage1, int stage2, const char **pathspec) } } -int cmd_diff_stages(int ac, const char **av, char **envp) +int cmd_diff_stages(int ac, const char **av, const char *prefix) { int stage1, stage2; - const char *prefix = setup_git_directory(); const char **pathspec = NULL; git_config(git_default_config); /* no "diff" UI options */ diff --git a/builtin-diff-tree.c b/builtin-diff-tree.c index f8215ea707..24cb2d7f84 100644 --- a/builtin-diff-tree.c +++ b/builtin-diff-tree.c @@ -59,7 +59,7 @@ static const char diff_tree_usage[] = " --root include the initial commit as diff against /dev/null\n" COMMON_DIFF_OPTIONS_HELP; -int cmd_diff_tree(int argc, const char **argv, char **envp) +int cmd_diff_tree(int argc, const char **argv, const char *prefix) { int nr_sha1; char line[1000]; @@ -67,7 +67,7 @@ int cmd_diff_tree(int argc, const char **argv, char **envp) static struct rev_info *opt = &log_tree_opt; int read_stdin = 0; - init_revisions(opt, setup_git_directory()); + init_revisions(opt, prefix); git_config(git_default_config); /* no "diff" UI options */ nr_sha1 = 0; opt->abbrev = 0; diff --git a/builtin-diff.c b/builtin-diff.c index 45b27cfca3..48d2fd03b7 100644 --- a/builtin-diff.c +++ b/builtin-diff.c @@ -221,13 +221,13 @@ void add_head(struct rev_info *revs) add_pending_object(revs, obj, "HEAD"); } -int cmd_diff(int argc, const char **argv, char **envp) +int cmd_diff(int argc, const char **argv, const char *prefix) { int i; struct rev_info rev; struct object_array_entry ent[100]; int ents = 0, blobs = 0, paths = 0; - const char *path = NULL, *prefix; + const char *path = NULL; struct blobinfo blob[2]; /* @@ -250,7 +250,6 @@ int cmd_diff(int argc, const char **argv, char **envp) * Other cases are errors. */ - prefix = setup_git_directory(); git_config(git_diff_ui_config); init_revisions(&rev, prefix); diff --git a/builtin-fmt-merge-msg.c b/builtin-fmt-merge-msg.c index 338f2094f3..c84224ee84 100644 --- a/builtin-fmt-merge-msg.c +++ b/builtin-fmt-merge-msg.c @@ -242,7 +242,7 @@ static void shortlog(const char *name, unsigned char *sha1, free_list(&subjects); } -int cmd_fmt_merge_msg(int argc, char **argv, char **envp) +int cmd_fmt_merge_msg(int argc, char **argv, const char *prefix) { int limit = 20, i = 0; char line[1024]; @@ -250,7 +250,6 @@ int cmd_fmt_merge_msg(int argc, char **argv, char **envp) const char *sep = ""; unsigned char head_sha1[20]; const char *head, *current_branch; - const char *prefix = setup_git_directory(); git_config(fmt_merge_msg_config); diff --git a/builtin-grep.c b/builtin-grep.c index a79bac305a..69b7c4862a 100644 --- a/builtin-grep.c +++ b/builtin-grep.c @@ -919,14 +919,13 @@ static const char emsg_missing_context_len[] = static const char emsg_missing_argument[] = "option requires an argument -%s"; -int cmd_grep(int argc, const char **argv, char **envp) +int cmd_grep(int argc, const char **argv, const char *prefix) { int hit = 0; int cached = 0; int seen_dashdash = 0; struct grep_opt opt; struct object_array list = { 0, 0, NULL }; - const char *prefix = setup_git_directory(); const char **paths = NULL; int i; diff --git a/builtin-help.c b/builtin-help.c index bc1b4da3bc..bb0b03f1ae 100644 --- a/builtin-help.c +++ b/builtin-help.c @@ -221,13 +221,13 @@ static void show_man_page(const char *git_cmd) execlp("man", "man", page, NULL); } -int cmd_version(int argc, const char **argv, char **envp) +int cmd_version(int argc, const char **argv, const char *prefix) { printf("git version %s\n", git_version_string); return 0; } -int cmd_help(int argc, const char **argv, char **envp) +int cmd_help(int argc, const char **argv, const char *prefix) { const char *help_cmd = argc > 1 ? argv[1] : NULL; if (!help_cmd) diff --git a/builtin-init-db.c b/builtin-init-db.c index 7fdd2fa9f9..52473edf56 100644 --- a/builtin-init-db.c +++ b/builtin-init-db.c @@ -250,7 +250,7 @@ static const char init_db_usage[] = * On the other hand, it might just make lookup slower and messier. You * be the judge. The default case is to have one DB per managed directory. */ -int cmd_init_db(int argc, const char **argv, char **envp) +int cmd_init_db(int argc, const char **argv, const char *prefix) { const char *git_dir; const char *sha1_dir; diff --git a/builtin-log.c b/builtin-log.c index 52064cd178..82c69d1d05 100644 --- a/builtin-log.c +++ b/builtin-log.c @@ -16,7 +16,7 @@ /* this is in builtin-diff.c */ void add_head(struct rev_info *revs); -static void cmd_log_init(int argc, const char **argv, char **envp, +static void cmd_log_init(int argc, const char **argv, const char *prefix, struct rev_info *rev) { rev->abbrev = DEFAULT_ABBREV; @@ -45,26 +45,24 @@ static int cmd_log_walk(struct rev_info *rev) return 0; } -int cmd_whatchanged(int argc, const char **argv, char **envp) +int cmd_whatchanged(int argc, const char **argv, const char *prefix) { struct rev_info rev; - const char *prefix = setup_git_directory(); git_config(git_diff_ui_config); init_revisions(&rev, prefix); rev.diff = 1; rev.diffopt.recursive = 1; rev.simplify_history = 0; - cmd_log_init(argc, argv, envp, &rev); + cmd_log_init(argc, argv, prefix, &rev); if (!rev.diffopt.output_format) rev.diffopt.output_format = DIFF_FORMAT_RAW; return cmd_log_walk(&rev); } -int cmd_show(int argc, const char **argv, char **envp) +int cmd_show(int argc, const char **argv, const char *prefix) { struct rev_info rev; - const char *prefix = setup_git_directory(); git_config(git_diff_ui_config); init_revisions(&rev, prefix); @@ -75,19 +73,18 @@ int cmd_show(int argc, const char **argv, char **envp) rev.always_show_header = 1; rev.ignore_merges = 0; rev.no_walk = 1; - cmd_log_init(argc, argv, envp, &rev); + cmd_log_init(argc, argv, prefix, &rev); return cmd_log_walk(&rev); } -int cmd_log(int argc, const char **argv, char **envp) +int cmd_log(int argc, const char **argv, const char *prefix) { struct rev_info rev; - const char *prefix = setup_git_directory(); git_config(git_diff_ui_config); init_revisions(&rev, prefix); rev.always_show_header = 1; - cmd_log_init(argc, argv, envp, &rev); + cmd_log_init(argc, argv, prefix, &rev); return cmd_log_walk(&rev); } @@ -181,14 +178,13 @@ static int get_patch_id(struct commit *commit, struct diff_options *options, return diff_flush_patch_id(options, sha1); } -static void get_patch_ids(struct rev_info *rev, struct diff_options *options) +static void get_patch_ids(struct rev_info *rev, struct diff_options *options, const char *prefix) { struct rev_info check_rev; struct commit *commit; struct object *o1, *o2; unsigned flags1, flags2; unsigned char sha1[20]; - const char *prefix = setup_git_directory(); if (rev->pending.nr != 2) die("Need exactly one range."); @@ -244,7 +240,7 @@ static void gen_message_id(char *dest, unsigned int length, char *base) (int)(email_end - email_start - 1), email_start + 1); } -int cmd_format_patch(int argc, const char **argv, char **envp) +int cmd_format_patch(int argc, const char **argv, const char *prefix) { struct commit *commit; struct commit **list = NULL; @@ -261,7 +257,6 @@ int cmd_format_patch(int argc, const char **argv, char **envp) char *add_signoff = NULL; char message_id[1024]; char ref_message_id[1024]; - const char *prefix = setup_git_directory(); git_config(git_format_config); init_revisions(&rev, prefix); @@ -368,7 +363,7 @@ int cmd_format_patch(int argc, const char **argv, char **envp) } if (ignore_if_in_upstream) - get_patch_ids(&rev, &patch_id_opts); + get_patch_ids(&rev, &patch_id_opts, prefix); if (!use_stdout) realstdout = fdopen(dup(1), "w"); diff --git a/builtin-ls-files.c b/builtin-ls-files.c index 8dae9f70e2..79ffe8f423 100644 --- a/builtin-ls-files.c +++ b/builtin-ls-files.c @@ -322,14 +322,13 @@ static const char ls_files_usage[] = "[ --exclude-per-directory= ] [--full-name] [--abbrev] " "[--] []*"; -int cmd_ls_files(int argc, const char **argv, char** envp) +int cmd_ls_files(int argc, const char **argv, const char *prefix) { int i; int exc_given = 0; struct dir_struct dir; memset(&dir, 0, sizeof(dir)); - prefix = setup_git_directory(); if (prefix) prefix_offset = strlen(prefix); git_config(git_default_config); diff --git a/builtin-ls-tree.c b/builtin-ls-tree.c index b8d0d88ba8..261147fdbe 100644 --- a/builtin-ls-tree.c +++ b/builtin-ls-tree.c @@ -18,7 +18,7 @@ static int abbrev = 0; static int ls_options = 0; static const char **pathspec; static int chomp_prefix = 0; -static const char *prefix; +static const char *ls_tree_prefix; static const char ls_tree_usage[] = "git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] [--full-name] [--abbrev[=]] [path...]"; @@ -71,7 +71,7 @@ static int show_tree(const unsigned char *sha1, const char *base, int baselen, return 0; if (chomp_prefix && - (baselen < chomp_prefix || memcmp(prefix, base, chomp_prefix))) + (baselen < chomp_prefix || memcmp(ls_tree_prefix, base, chomp_prefix))) return 0; if (!(ls_options & LS_NAME_ONLY)) @@ -85,13 +85,13 @@ static int show_tree(const unsigned char *sha1, const char *base, int baselen, return retval; } -int cmd_ls_tree(int argc, const char **argv, char **envp) +int cmd_ls_tree(int argc, const char **argv, const char *prefix) { unsigned char sha1[20]; struct tree *tree; - prefix = setup_git_directory(); git_config(git_default_config); + ls_tree_prefix = prefix; if (prefix && *prefix) chomp_prefix = strlen(prefix); while (1 < argc && argv[1][0] == '-') { diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c index 05dc1bfe71..24a4fc63b3 100644 --- a/builtin-mailinfo.c +++ b/builtin-mailinfo.c @@ -836,7 +836,7 @@ int mailinfo(FILE *in, FILE *out, int ks, const char *encoding, static const char mailinfo_usage[] = "git-mailinfo [-k] [-u | --encoding=] msg patch info"; -int cmd_mailinfo(int argc, const char **argv, char **envp) +int cmd_mailinfo(int argc, const char **argv, const char *prefix) { /* NEEDSWORK: might want to do the optional .git/ directory * discovery diff --git a/builtin-mailsplit.c b/builtin-mailsplit.c index e2a0058435..91a699d34d 100644 --- a/builtin-mailsplit.c +++ b/builtin-mailsplit.c @@ -138,7 +138,7 @@ out: free(name); return ret; } -int cmd_mailsplit(int argc, const char **argv, char **envp) +int cmd_mailsplit(int argc, const char **argv, const char *prefix) { int nr = 0, nr_prec = 4, ret; int allow_bare = 0; diff --git a/builtin-prune.c b/builtin-prune.c index 4ed1e1b43c..6a86eb52ae 100644 --- a/builtin-prune.c +++ b/builtin-prune.c @@ -217,7 +217,7 @@ static void add_cache_refs(void) add_cache_tree(active_cache_tree); } -int cmd_prune(int argc, const char **argv, char **envp) +int cmd_prune(int argc, const char **argv, const char *prefix) { int i; @@ -234,7 +234,7 @@ int cmd_prune(int argc, const char **argv, char **envp) * Set up revision parsing, and mark us as being interested * in all object types, not just commits. */ - init_revisions(&revs, setup_git_directory()); + init_revisions(&revs, prefix); revs.tag_objects = 1; revs.blob_objects = 1; revs.tree_objects = 1; diff --git a/builtin-push.c b/builtin-push.c index 31cbfd7386..a824171066 100644 --- a/builtin-push.c +++ b/builtin-push.c @@ -270,7 +270,7 @@ static int do_push(const char *repo) return 0; } -int cmd_push(int argc, const char **argv, char **envp) +int cmd_push(int argc, const char **argv, const char *prefix) { int i; const char *repo = "origin"; /* default repository */ diff --git a/builtin-read-tree.c b/builtin-read-tree.c index 122b6f130b..49c10bf221 100644 --- a/builtin-read-tree.c +++ b/builtin-read-tree.c @@ -870,7 +870,7 @@ static const char read_tree_usage[] = "git-read-tree ( | [[-m [--aggressive static struct lock_file lock_file; -int cmd_read_tree(int argc, const char **argv, char **envp) +int cmd_read_tree(int argc, const char **argv, const char *prefix) { int i, newfd, stage = 0; unsigned char sha1[20]; @@ -882,7 +882,6 @@ int cmd_read_tree(int argc, const char **argv, char **envp) state.quiet = 1; state.refresh_cache = 1; - setup_git_directory(); git_config(git_default_config); newfd = hold_lock_file_for_update(&lock_file, get_index_file()); diff --git a/builtin-rev-list.c b/builtin-rev-list.c index 2b6691c8e4..0dee1734a3 100644 --- a/builtin-rev-list.c +++ b/builtin-rev-list.c @@ -306,12 +306,12 @@ static void mark_edges_uninteresting(struct commit_list *list) } } -int cmd_rev_list(int argc, const char **argv, char **envp) +int cmd_rev_list(int argc, const char **argv, const char *prefix) { struct commit_list *list; int i; - init_revisions(&revs, setup_git_directory()); + init_revisions(&revs, prefix); revs.abbrev = 0; revs.commit_format = CMIT_FMT_UNSPECIFIED; argc = setup_revisions(argc, argv, &revs, NULL); diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index b3e4386c1b..aca4a36032 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -209,11 +209,10 @@ static int try_difference(const char *arg) return 0; } -int cmd_rev_parse(int argc, const char **argv, char **envp) +int cmd_rev_parse(int argc, const char **argv, const char *prefix) { int i, as_is = 0, verify = 0; unsigned char sha1[20]; - const char *prefix = setup_git_directory(); git_config(git_default_config); diff --git a/builtin-rm.c b/builtin-rm.c index bb810ba41a..92d205a715 100644 --- a/builtin-rm.c +++ b/builtin-rm.c @@ -43,11 +43,10 @@ static int remove_file(const char *name) static struct lock_file lock_file; -int cmd_rm(int argc, const char **argv, char **envp) +int cmd_rm(int argc, const char **argv, const char *prefix) { int i, newfd; int verbose = 0, show_only = 0, force = 0; - const char *prefix = setup_git_directory(); const char **pathspec; char *seen; diff --git a/builtin-show-branch.c b/builtin-show-branch.c index 82f75b72de..2a1b848f6c 100644 --- a/builtin-show-branch.c +++ b/builtin-show-branch.c @@ -550,7 +550,7 @@ static int omit_in_dense(struct commit *commit, struct commit **rev, int n) return 0; } -int cmd_show_branch(int ac, const char **av, char **envp) +int cmd_show_branch(int ac, const char **av, const char *prefix) { struct commit *rev[MAX_REVS], *commit; struct commit_list *list = NULL, *seen = NULL; @@ -573,7 +573,6 @@ int cmd_show_branch(int ac, const char **av, char **envp) int topics = 0; int dense = 1; - setup_git_directory(); git_config(git_show_branch_config); /* If nothing is specified, try the default first */ diff --git a/builtin-stripspace.c b/builtin-stripspace.c index 2ce1264f7b..09cc9108cd 100644 --- a/builtin-stripspace.c +++ b/builtin-stripspace.c @@ -54,7 +54,7 @@ void stripspace(FILE *in, FILE *out) fputc('\n', out); } -int cmd_stripspace(int argc, const char **argv, char **envp) +int cmd_stripspace(int argc, const char **argv, const char *prefix) { stripspace(stdin, stdout); return 0; diff --git a/builtin-tar-tree.c b/builtin-tar-tree.c index e5aaded820..7c48db9ec8 100644 --- a/builtin-tar-tree.c +++ b/builtin-tar-tree.c @@ -308,7 +308,7 @@ int git_tar_config(const char *var, const char *value) return git_default_config(var, value); } -static int generate_tar(int argc, const char **argv, char** envp) +static int generate_tar(int argc, const char **argv, const char *prefix) { unsigned char sha1[20], tree_sha1[20]; struct commit *commit; @@ -319,7 +319,6 @@ static int generate_tar(int argc, const char **argv, char** envp) current_path.alloc = PATH_MAX; current_path.len = current_path.eof = 0; - setup_git_directory(); git_config(git_tar_config); switch (argc) { @@ -402,19 +401,19 @@ static int remote_tar(int argc, const char **argv) return !!ret; } -int cmd_tar_tree(int argc, const char **argv, char **envp) +int cmd_tar_tree(int argc, const char **argv, const char *prefix) { if (argc < 2) usage(tar_tree_usage); if (!strncmp("--remote=", argv[1], 9)) return remote_tar(argc, argv); - return generate_tar(argc, argv, envp); + return generate_tar(argc, argv, prefix); } /* ustar header + extended global header content */ #define HEADERSIZE (2 * RECORDSIZE) -int cmd_get_tar_commit_id(int argc, const char **argv, char **envp) +int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix) { char buffer[HEADERSIZE]; struct ustar_header *header = (struct ustar_header *)buffer; diff --git a/builtin-update-index.c b/builtin-update-index.c index 1a4200d151..24dca47d8d 100644 --- a/builtin-update-index.c +++ b/builtin-update-index.c @@ -476,12 +476,11 @@ static int do_reupdate(int ac, const char **av, return 0; } -int cmd_update_index(int argc, const char **argv, char **envp) +int cmd_update_index(int argc, const char **argv, const char *prefix) { int i, newfd, entries, has_errors = 0, line_termination = '\n'; int allow_options = 1; int read_from_stdin = 0; - const char *prefix = setup_git_directory(); int prefix_length = prefix ? strlen(prefix) : 0; char set_executable_bit = 0; unsigned int refresh_flags = 0; diff --git a/builtin-update-ref.c b/builtin-update-ref.c index 83094abe0f..5bd71825fd 100644 --- a/builtin-update-ref.c +++ b/builtin-update-ref.c @@ -5,7 +5,7 @@ static const char git_update_ref_usage[] = "git-update-ref [] [-m ]"; -int cmd_update_ref(int argc, const char **argv, char **envp) +int cmd_update_ref(int argc, const char **argv, const char *prefix) { const char *refname=NULL, *value=NULL, *oldval=NULL, *msg=NULL; struct ref_lock *lock; @@ -13,7 +13,6 @@ int cmd_update_ref(int argc, const char **argv, char **envp) int i; setup_ident(); - setup_git_directory(); git_config(git_default_config); for (i = 1; i < argc; i++) { diff --git a/builtin-upload-tar.c b/builtin-upload-tar.c index d4fa7b56c3..7b401bbb77 100644 --- a/builtin-upload-tar.c +++ b/builtin-upload-tar.c @@ -15,7 +15,7 @@ static int nak(const char *reason) return 1; } -int cmd_upload_tar(int argc, const char **argv, char **envp) +int cmd_upload_tar(int argc, const char **argv, const char *prefix) { int len; const char *dir = argv[1]; diff --git a/builtin-write-tree.c b/builtin-write-tree.c index 449a4d1b57..0289f59936 100644 --- a/builtin-write-tree.c +++ b/builtin-write-tree.c @@ -60,14 +60,12 @@ int write_tree(unsigned char *sha1, int missing_ok, const char *prefix) return 0; } -int cmd_write_tree(int argc, const char **argv, char **envp) +int cmd_write_tree(int argc, const char **argv, const char *unused_prefix) { int missing_ok = 0, ret; const char *prefix = NULL; unsigned char sha1[20]; - setup_git_directory(); - while (1 < argc) { const char *arg = argv[1]; if (!strcmp(arg, "--missing-ok")) diff --git a/builtin.h b/builtin.h index 5339d8627f..de244cdf65 100644 --- a/builtin.h +++ b/builtin.h @@ -15,53 +15,53 @@ void cmd_usage(int show_all, const char *exec_path, const char *fmt, ...) #endif ; -extern int cmd_help(int argc, const char **argv, char **envp); -extern int cmd_version(int argc, const char **argv, char **envp); +extern int cmd_help(int argc, const char **argv, const char *prefix); +extern int cmd_version(int argc, const char **argv, const char *prefix); -extern int cmd_whatchanged(int argc, const char **argv, char **envp); -extern int cmd_show(int argc, const char **argv, char **envp); -extern int cmd_log(int argc, const char **argv, char **envp); -extern int cmd_diff(int argc, const char **argv, char **envp); -extern int cmd_format_patch(int argc, const char **argv, char **envp); -extern int cmd_count_objects(int argc, const char **argv, char **envp); +extern int cmd_whatchanged(int argc, const char **argv, const char *prefix); +extern int cmd_show(int argc, const char **argv, const char *prefix); +extern int cmd_log(int argc, const char **argv, const char *prefix); +extern int cmd_diff(int argc, const char **argv, const char *prefix); +extern int cmd_format_patch(int argc, const char **argv, const char *prefix); +extern int cmd_count_objects(int argc, const char **argv, const char *prefix); -extern int cmd_prune(int argc, const char **argv, char **envp); +extern int cmd_prune(int argc, const char **argv, const char *prefix); -extern int cmd_push(int argc, const char **argv, char **envp); -extern int cmd_grep(int argc, const char **argv, char **envp); -extern int cmd_rm(int argc, const char **argv, char **envp); -extern int cmd_add(int argc, const char **argv, char **envp); -extern int cmd_rev_list(int argc, const char **argv, char **envp); -extern int cmd_check_ref_format(int argc, const char **argv, char **envp); -extern int cmd_init_db(int argc, const char **argv, char **envp); -extern int cmd_tar_tree(int argc, const char **argv, char **envp); -extern int cmd_upload_tar(int argc, const char **argv, char **envp); -extern int cmd_get_tar_commit_id(int argc, const char **argv, char **envp); -extern int cmd_ls_files(int argc, const char **argv, char **envp); -extern int cmd_ls_tree(int argc, const char **argv, char **envp); -extern int cmd_read_tree(int argc, const char **argv, char **envp); -extern int cmd_commit_tree(int argc, const char **argv, char **envp); -extern int cmd_apply(int argc, const char **argv, char **envp); -extern int cmd_show_branch(int argc, const char **argv, char **envp); -extern int cmd_diff_files(int argc, const char **argv, char **envp); -extern int cmd_diff_index(int argc, const char **argv, char **envp); -extern int cmd_diff_stages(int argc, const char **argv, char **envp); -extern int cmd_diff_tree(int argc, const char **argv, char **envp); -extern int cmd_cat_file(int argc, const char **argv, char **envp); -extern int cmd_rev_parse(int argc, const char **argv, char **envp); -extern int cmd_update_index(int argc, const char **argv, char **envp); -extern int cmd_update_ref(int argc, const char **argv, char **envp); -extern int cmd_fmt_merge_msg(int argc, const char **argv, char **envp); +extern int cmd_push(int argc, const char **argv, const char *prefix); +extern int cmd_grep(int argc, const char **argv, const char *prefix); +extern int cmd_rm(int argc, const char **argv, const char *prefix); +extern int cmd_add(int argc, const char **argv, const char *prefix); +extern int cmd_rev_list(int argc, const char **argv, const char *prefix); +extern int cmd_check_ref_format(int argc, const char **argv, const char *prefix); +extern int cmd_init_db(int argc, const char **argv, const char *prefix); +extern int cmd_tar_tree(int argc, const char **argv, const char *prefix); +extern int cmd_upload_tar(int argc, const char **argv, const char *prefix); +extern int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix); +extern int cmd_ls_files(int argc, const char **argv, const char *prefix); +extern int cmd_ls_tree(int argc, const char **argv, const char *prefix); +extern int cmd_read_tree(int argc, const char **argv, const char *prefix); +extern int cmd_commit_tree(int argc, const char **argv, const char *prefix); +extern int cmd_apply(int argc, const char **argv, const char *prefix); +extern int cmd_show_branch(int argc, const char **argv, const char *prefix); +extern int cmd_diff_files(int argc, const char **argv, const char *prefix); +extern int cmd_diff_index(int argc, const char **argv, const char *prefix); +extern int cmd_diff_stages(int argc, const char **argv, const char *prefix); +extern int cmd_diff_tree(int argc, const char **argv, const char *prefix); +extern int cmd_cat_file(int argc, const char **argv, const char *prefix); +extern int cmd_rev_parse(int argc, const char **argv, const char *prefix); +extern int cmd_update_index(int argc, const char **argv, const char *prefix); +extern int cmd_update_ref(int argc, const char **argv, const char *prefix); +extern int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix); -extern int cmd_write_tree(int argc, const char **argv, char **envp); +extern int cmd_write_tree(int argc, const char **argv, const char *prefix); extern int write_tree(unsigned char *sha1, int missing_ok, const char *prefix); -extern int cmd_mailsplit(int argc, const char **argv, char **envp); +extern int cmd_mailsplit(int argc, const char **argv, const char *prefix); extern int split_mbox(const char **mbox, const char *dir, int allow_bare, int nr_prec, int skip); -extern int cmd_mailinfo(int argc, const char **argv, char **envp); +extern int cmd_mailinfo(int argc, const char **argv, const char *prefix); extern int mailinfo(FILE *in, FILE *out, int ks, const char *encoding, const char *msg, const char *patch); -extern int cmd_stripspace(int argc, const char **argv, char **envp); +extern int cmd_stripspace(int argc, const char **argv, const char *prefix); extern void stripspace(FILE *in, FILE *out); #endif diff --git a/git.c b/git.c index 885e1ce75b..79db43e143 100644 --- a/git.c +++ b/git.c @@ -214,51 +214,54 @@ static int handle_alias(int *argcp, const char ***argv) const char git_version_string[] = GIT_VERSION; +#define NEEDS_PREFIX 1 + static void handle_internal_command(int argc, const char **argv, char **envp) { const char *cmd = argv[0]; static struct cmd_struct { const char *cmd; - int (*fn)(int, const char **, char **); + int (*fn)(int, const char **, const char *); + int prefix; } commands[] = { { "version", cmd_version }, { "help", cmd_help }, - { "log", cmd_log }, - { "whatchanged", cmd_whatchanged }, - { "show", cmd_show }, + { "log", cmd_log, NEEDS_PREFIX }, + { "whatchanged", cmd_whatchanged, NEEDS_PREFIX }, + { "show", cmd_show, NEEDS_PREFIX }, { "push", cmd_push }, - { "format-patch", cmd_format_patch }, + { "format-patch", cmd_format_patch, NEEDS_PREFIX }, { "count-objects", cmd_count_objects }, - { "diff", cmd_diff }, - { "grep", cmd_grep }, - { "rm", cmd_rm }, - { "add", cmd_add }, - { "rev-list", cmd_rev_list }, + { "diff", cmd_diff, NEEDS_PREFIX }, + { "grep", cmd_grep, NEEDS_PREFIX }, + { "rm", cmd_rm, NEEDS_PREFIX }, + { "add", cmd_add, NEEDS_PREFIX }, + { "rev-list", cmd_rev_list, NEEDS_PREFIX }, { "init-db", cmd_init_db }, { "get-tar-commit-id", cmd_get_tar_commit_id }, { "upload-tar", cmd_upload_tar }, { "check-ref-format", cmd_check_ref_format }, - { "ls-files", cmd_ls_files }, - { "ls-tree", cmd_ls_tree }, - { "tar-tree", cmd_tar_tree }, - { "read-tree", cmd_read_tree }, - { "commit-tree", cmd_commit_tree }, + { "ls-files", cmd_ls_files, NEEDS_PREFIX }, + { "ls-tree", cmd_ls_tree, NEEDS_PREFIX }, + { "tar-tree", cmd_tar_tree, NEEDS_PREFIX }, + { "read-tree", cmd_read_tree, NEEDS_PREFIX }, + { "commit-tree", cmd_commit_tree, NEEDS_PREFIX }, { "apply", cmd_apply }, - { "show-branch", cmd_show_branch }, - { "diff-files", cmd_diff_files }, - { "diff-index", cmd_diff_index }, - { "diff-stages", cmd_diff_stages }, - { "diff-tree", cmd_diff_tree }, - { "cat-file", cmd_cat_file }, - { "rev-parse", cmd_rev_parse }, - { "write-tree", cmd_write_tree }, + { "show-branch", cmd_show_branch, NEEDS_PREFIX }, + { "diff-files", cmd_diff_files, NEEDS_PREFIX }, + { "diff-index", cmd_diff_index, NEEDS_PREFIX }, + { "diff-stages", cmd_diff_stages, NEEDS_PREFIX }, + { "diff-tree", cmd_diff_tree, NEEDS_PREFIX }, + { "cat-file", cmd_cat_file, NEEDS_PREFIX }, + { "rev-parse", cmd_rev_parse, NEEDS_PREFIX }, + { "write-tree", cmd_write_tree, NEEDS_PREFIX }, { "mailsplit", cmd_mailsplit }, { "mailinfo", cmd_mailinfo }, { "stripspace", cmd_stripspace }, - { "update-index", cmd_update_index }, - { "update-ref", cmd_update_ref }, - { "fmt-merge-msg", cmd_fmt_merge_msg }, - { "prune", cmd_prune }, + { "update-index", cmd_update_index, NEEDS_PREFIX }, + { "update-ref", cmd_update_ref, NEEDS_PREFIX }, + { "fmt-merge-msg", cmd_fmt_merge_msg, NEEDS_PREFIX }, + { "prune", cmd_prune, NEEDS_PREFIX }, }; int i; @@ -270,9 +273,13 @@ static void handle_internal_command(int argc, const char **argv, char **envp) for (i = 0; i < ARRAY_SIZE(commands); i++) { struct cmd_struct *p = commands+i; + const char *prefix; if (strcmp(p->cmd, cmd)) continue; + prefix = NULL; + if (p->prefix) + prefix = setup_git_directory(); if (getenv("GIT_TRACE")) { int i; fprintf(stderr, "trace: built-in: git"); @@ -284,7 +291,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp) fflush(stderr); } - exit(p->fn(argc, argv, envp)); + exit(p->fn(argc, argv, prefix)); } }