From 493c774e58a05bbbac06e4ae1654ca3d24e4e5cf Mon Sep 17 00:00:00 2001 From: Matthias Lederhofer Date: Sun, 3 Jun 2007 16:46:36 +0200 Subject: rev-parse: introduce --is-bare-repository Signed-off-by: Matthias Lederhofer Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 37addb25fa..71d5162595 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -352,6 +352,11 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) : "false"); continue; } + if (!strcmp(arg, "--is-bare-repository")) { + printf("%s\n", is_bare_repository() ? "true" + : "false"); + continue; + } if (!prefixcmp(arg, "--since=")) { show_datestring("--max-age=", arg+8); continue; -- cgit v1.2.1 From 892c41b98ae2e6baf3aa13901cb10db9ac67d2f3 Mon Sep 17 00:00:00 2001 From: Matthias Lederhofer Date: Wed, 6 Jun 2007 09:10:42 +0200 Subject: introduce GIT_WORK_TREE to specify the work tree setup_gdg is used as abbreviation for setup_git_directory_gently. The work tree can be specified using the environment variable GIT_WORK_TREE and the config option core.worktree (the environment variable has precendence over the config option). Additionally there is a command line option --work-tree which sets the environment variable. setup_gdg does the following now: GIT_DIR unspecified repository in .git directory parent directory of the .git directory is used as work tree, GIT_WORK_TREE is ignored GIT_DIR unspecified repository in cwd GIT_DIR is set to cwd see the cases with GIT_DIR specified what happens next and also see the note below GIT_DIR specified GIT_WORK_TREE/core.worktree unspecified cwd is used as work tree GIT_DIR specified GIT_WORK_TREE/core.worktree specified the specified work tree is used Note on the case where GIT_DIR is unspecified and repository is in cwd: GIT_WORK_TREE is used but is_inside_git_dir is always true. I did it this way because setup_gdg might be called multiple times (e.g. when doing alias expansion) and in successive calls setup_gdg should do the same thing every time. Meaning of is_bare/is_inside_work_tree/is_inside_git_dir: (1) is_bare_repository A repository is bare if core.bare is true or core.bare is unspecified and the name suggests it is bare (directory not named .git). The bare option disables a few protective checks which are useful with a working tree. Currently this changes if a repository is bare: updates of HEAD are allowed git gc packs the refs the reflog is disabled by default (2) is_inside_work_tree True if the cwd is inside the associated working tree (if there is one), false otherwise. (3) is_inside_git_dir True if the cwd is inside the git directory, false otherwise. Before this patch is_inside_git_dir was always true for bare repositories. When setup_gdg finds a repository git_config(git_default_config) is always called. This ensure that is_bare_repository makes use of core.bare and does not guess even though core.bare is specified. inside_work_tree and inside_git_dir are set if setup_gdg finds a repository. The is_inside_work_tree and is_inside_git_dir functions will die if they are called before a successful call to setup_gdg. Signed-off-by: Matthias Lederhofer Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 71d5162595..497903a85a 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -352,6 +352,11 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) : "false"); continue; } + if (!strcmp(arg, "--is-inside-work-tree")) { + printf("%s\n", is_inside_work_tree() ? "true" + : "false"); + continue; + } if (!strcmp(arg, "--is-bare-repository")) { printf("%s\n", is_bare_repository() ? "true" : "false"); -- cgit v1.2.1 From e90fdc39b6903502192b2dd11e5503cea721a1ad Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 1 Aug 2007 01:30:14 +0100 Subject: Clean up work-tree handling The old version of work-tree support was an unholy mess, barely readable, and not to the point. For example, why do you have to provide a worktree, when it is not used? As in "git status". Now it works. Another riddle was: if you can have work trees inside the git dir, why are some programs complaining that they need a work tree? IOW it is allowed to call $ git --git-dir=../ --work-tree=. bla when you really want to. In this case, you are both in the git directory and in the working tree. So, programs have to actually test for the right thing, namely if they are inside a working tree, and not if they are inside a git directory. Also, GIT_DIR=../.git should behave the same as if no GIT_DIR was specified, unless there is a repository in the current working directory. It does now. The logic to determine if a repository is bare, or has a work tree (tertium non datur), is this: --work-tree=bla overrides GIT_WORK_TREE, which overrides core.bare = true, which overrides core.worktree, which overrides GIT_DIR/.. when GIT_DIR ends in /.git, which overrides the directory in which .git/ was found. In related news, a long standing bug was fixed: when in .git/bla/x.git/, which is a bare repository, git formerly assumed ../.. to be the appropriate git dir. This problem was reported by Shawn Pearce to have caused much pain, where a colleague mistakenly ran "git init" in "/" a long time ago, and bare repositories just would not work. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 497903a85a..8d78b69c90 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -321,6 +321,13 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) } if (!strcmp(arg, "--show-cdup")) { const char *pfx = prefix; + if (!is_inside_work_tree()) { + const char *work_tree = + get_git_work_tree(); + if (work_tree) + printf("%s\n", work_tree); + continue; + } while (pfx) { pfx = strchr(pfx, '/'); if (pfx) { -- cgit v1.2.1 From 21d4783538662143ef52ed6967c948ab27586232 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sun, 4 Nov 2007 11:30:53 +0100 Subject: Add a parseopt mode to git-rev-parse to bring parse-options to shell scripts. Signed-off-by: Pierre Habouzit Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 8d78b69c90..054519bf28 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -8,6 +8,7 @@ #include "refs.h" #include "quote.h" #include "builtin.h" +#include "parse-options.h" #define DO_REVS 1 #define DO_NOREV 2 @@ -209,6 +210,128 @@ static int try_difference(const char *arg) return 0; } +static int parseopt_dump(const struct option *o, const char *arg, int unset) +{ + struct strbuf *parsed = o->value; + if (unset) + strbuf_addf(parsed, " --no-%s", o->long_name); + else if (o->short_name) + strbuf_addf(parsed, " -%c", o->short_name); + else + strbuf_addf(parsed, " --%s", o->long_name); + if (arg) { + strbuf_addch(parsed, ' '); + sq_quote_buf(parsed, arg); + } + return 0; +} + +static const char *skipspaces(const char *s) +{ + while (isspace(*s)) + s++; + return s; +} + +static int cmd_parseopt(int argc, const char **argv, const char *prefix) +{ + static int keep_dashdash = 0; + static char const * const parseopt_usage[] = { + "git-rev-parse --parseopt [options] -- [...]", + NULL + }; + static struct option parseopt_opts[] = { + OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash, + "keep the `--` passed as an arg"), + OPT_END(), + }; + + struct strbuf sb, parsed; + const char **usage = NULL; + struct option *opts = NULL; + int onb = 0, osz = 0, unb = 0, usz = 0; + + strbuf_init(&parsed, 0); + strbuf_addstr(&parsed, "set --"); + argc = parse_options(argc, argv, parseopt_opts, parseopt_usage, + PARSE_OPT_KEEP_DASHDASH); + if (argc < 1 || strcmp(argv[0], "--")) + usage_with_options(parseopt_usage, parseopt_opts); + + strbuf_init(&sb, 0); + /* get the usage up to the first line with a -- on it */ + for (;;) { + if (strbuf_getline(&sb, stdin, '\n') == EOF) + die("premature end of input"); + ALLOC_GROW(usage, unb + 1, usz); + if (!strcmp("--", sb.buf)) { + if (unb < 1) + die("no usage string given before the `--' separator"); + usage[unb] = NULL; + break; + } + usage[unb++] = strbuf_detach(&sb, NULL); + } + + /* parse: (|,|)[=?]? SP+ */ + while (strbuf_getline(&sb, stdin, '\n') != EOF) { + const char *s; + struct option *o; + + if (!sb.len) + continue; + + ALLOC_GROW(opts, onb + 1, osz); + memset(opts + onb, 0, sizeof(opts[onb])); + + o = &opts[onb++]; + s = strchr(sb.buf, ' '); + if (!s || *sb.buf == ' ') { + o->type = OPTION_GROUP; + o->help = xstrdup(skipspaces(s)); + continue; + } + + o->type = OPTION_CALLBACK; + o->help = xstrdup(skipspaces(s)); + o->value = &parsed; + o->callback = &parseopt_dump; + switch (s[-1]) { + case '=': + s--; + break; + case '?': + o->flags = PARSE_OPT_OPTARG; + s--; + break; + default: + o->flags = PARSE_OPT_NOARG; + break; + } + + if (s - sb.buf == 1) /* short option only */ + o->short_name = *sb.buf; + else if (sb.buf[1] != ',') /* long option only */ + o->long_name = xmemdupz(sb.buf, s - sb.buf); + else { + o->short_name = *sb.buf; + o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2); + } + } + strbuf_release(&sb); + + /* put an OPT_END() */ + ALLOC_GROW(opts, onb + 1, osz); + memset(opts + onb, 0, sizeof(opts[onb])); + argc = parse_options(argc, argv, opts, usage, + keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0); + + strbuf_addf(&parsed, " --"); + sq_quote_argv(&parsed, argv, argc, 0); + puts(parsed.buf); + return 0; +} + int cmd_rev_parse(int argc, const char **argv, const char *prefix) { int i, as_is = 0, verify = 0; @@ -216,6 +339,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) git_config(git_default_config); + if (argc > 1 && !strcmp("--parseopt", argv[1])) + return cmd_parseopt(argc - 1, argv + 1, prefix); + for (i = 1; i < argc; i++) { const char *arg = argv[i]; -- cgit v1.2.1 From 5410a02ab9e6a1987147724f8ea65e6a077b3832 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 6 Nov 2007 12:23:14 -0800 Subject: git-rev-parse --parseopt The "parseopt mode" of git-rev-parse does not need to be run inside a git repository, although the normal mode does. Most notabily, lack of this fix breaks git-clone script, as noticed by Nico. Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 054519bf28..d1038a0e66 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -337,11 +337,11 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) int i, as_is = 0, verify = 0; unsigned char sha1[20]; - git_config(git_default_config); - if (argc > 1 && !strcmp("--parseopt", argv[1])) return cmd_parseopt(argc - 1, argv + 1, prefix); + prefix = setup_git_directory(); + git_config(git_default_config); for (i = 1; i < argc; i++) { const char *arg = argv[i]; -- cgit v1.2.1 From b319ce4c14f7fe0ee469a3f9def1098d84177849 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Mon, 3 Dec 2007 05:51:50 +0100 Subject: Trace and quote with argv: get rid of unneeded count argument. Now that str_buf takes care of all the allocations, there is no more gain to pass an argument count. So this patch removes the "count" argument from: - "sq_quote_argv" - "trace_argv_printf" and all the callers. Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index d1038a0e66..20d1789e01 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -327,7 +327,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix) keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0); strbuf_addf(&parsed, " --"); - sq_quote_argv(&parsed, argv, argc, 0); + sq_quote_argv(&parsed, argv, 0); puts(parsed.buf); return 0; } -- cgit v1.2.1 From a6d97d49e23382027efff8a8e90e69e0572620c6 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sat, 5 Jan 2008 12:09:55 -0800 Subject: git-rev-parse --symbolic-full-name The plumbing level can understand that the user meant "refs/heads/master" when the user says "master" or "heads/master", but there is no easy way for the scripts to figure it out without duplicating the dwim_ref() logic. Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 20d1789e01..b9af1a5a55 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -21,6 +21,9 @@ static const char *def; #define NORMAL 0 #define REVERSED 1 static int show_type = NORMAL; + +#define SHOW_SYMBOLIC_ASIS 1 +#define SHOW_SYMBOLIC_FULL 2 static int symbolic; static int abbrev; static int output_sq; @@ -103,8 +106,32 @@ static void show_rev(int type, const unsigned char *sha1, const char *name) if (type != show_type) putchar('^'); - if (symbolic && name) - show(name); + if (symbolic && name) { + if (symbolic == SHOW_SYMBOLIC_FULL) { + unsigned char discard[20]; + char *full; + + switch (dwim_ref(name, strlen(name), discard, &full)) { + case 0: + /* + * Not found -- not a ref. We could + * emit "name" here, but symbolic-full + * users are interested in finding the + * refs spelled in full, and they would + * need to filter non-refs if we did so. + */ + break; + case 1: /* happy */ + show(full); + break; + default: /* ambiguous */ + error("refname '%s' is ambiguous", name); + break; + } + } else { + show(name); + } + } else if (abbrev) show(find_unique_abbrev(sha1, abbrev)); else @@ -421,7 +448,11 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) continue; } if (!strcmp(arg, "--symbolic")) { - symbolic = 1; + symbolic = SHOW_SYMBOLIC_ASIS; + continue; + } + if (!strcmp(arg, "--symbolic-full-name")) { + symbolic = SHOW_SYMBOLIC_FULL; continue; } if (!strcmp(arg, "--all")) { -- cgit v1.2.1 From e103343644188ddddb3678f0568d150ca56f59e3 Mon Sep 17 00:00:00 2001 From: Jay Soffian Date: Mon, 25 Feb 2008 23:07:39 -0500 Subject: rev-parse: fix potential bus error with --parseopt option spec handling A non-empty line containing no spaces should be treated by --parseopt as an option group header, but was causing a bus error. Also added a test script for rev-parse --parseopt. Signed-off-by: Jay Soffian Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index b9af1a5a55..90dbb9d7c1 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -315,7 +315,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix) s = strchr(sb.buf, ' '); if (!s || *sb.buf == ' ') { o->type = OPTION_GROUP; - o->help = xstrdup(skipspaces(s)); + o->help = xstrdup(skipspaces(sb.buf)); continue; } -- cgit v1.2.1 From ff962a3f1900966d008d6f9eaf32095c42322b9d Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sun, 2 Mar 2008 09:21:38 +0100 Subject: parse-opt: bring PARSE_OPT_HIDDEN and NONEG to git-rev-parse --parseopt Signed-off-by: Pierre Habouzit Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 90dbb9d7c1..0351d54435 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -322,18 +322,24 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix) o->type = OPTION_CALLBACK; o->help = xstrdup(skipspaces(s)); o->value = &parsed; + o->flags = PARSE_OPT_NOARG; o->callback = &parseopt_dump; - switch (s[-1]) { - case '=': - s--; - break; - case '?': - o->flags = PARSE_OPT_OPTARG; - s--; - break; - default: - o->flags = PARSE_OPT_NOARG; - break; + while (s > sb.buf && strchr("*=?!", s[-1])) { + switch (*--s) { + case '=': + o->flags &= ~PARSE_OPT_NOARG; + break; + case '?': + o->flags &= ~PARSE_OPT_NOARG; + o->flags |= PARSE_OPT_OPTARG; + break; + case '!': + o->flags |= PARSE_OPT_NONEG; + break; + case '*': + o->flags |= PARSE_OPT_HIDDEN; + break; + } } if (s - sb.buf == 1) /* short option only */ -- cgit v1.2.1 From b1b359699a24a89f773cccdf35801bc6fc15ade8 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sat, 26 Apr 2008 13:57:23 +0200 Subject: rev-parse: teach "--verify" to be quiet when using "-q" or "--quiet" Currently "git rev-parse --verify " is often used with its error output redirected to /dev/null. This patch makes it easier to do that. The -q|--quiet option is designed to work the same way as it does for "git symbolic-ref". Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 0351d54435..9384a991ee 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -365,9 +365,17 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix) return 0; } +static void die_no_single_rev(int quiet) +{ + if (quiet) + exit(1); + else + die("Needed a single revision"); +} + int cmd_rev_parse(int argc, const char **argv, const char *prefix) { - int i, as_is = 0, verify = 0; + int i, as_is = 0, verify = 0, quiet = 0; unsigned char sha1[20]; if (argc > 1 && !strcmp("--parseopt", argv[1])) @@ -432,6 +440,10 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) verify = 1; continue; } + if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) { + quiet = 1; + continue; + } if (!strcmp(arg, "--short") || !prefixcmp(arg, "--short=")) { filter &= ~(DO_FLAGS|DO_NOREV); @@ -549,7 +561,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) continue; } if (show_flag(arg) && verify) - die("Needed a single revision"); + die_no_single_rev(quiet); continue; } @@ -568,11 +580,11 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) if (!show_file(arg)) continue; if (verify) - die("Needed a single revision"); + die_no_single_rev(quiet); verify_filename(prefix, arg); } show_default(); if (verify && revs_count != 1) - die("Needed a single revision"); + die_no_single_rev(quiet); return 0; } -- cgit v1.2.1 From 75ecfce39703e15a9f3d812faa4eb039935c8962 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sat, 26 Apr 2008 15:19:29 +0200 Subject: rev-parse: fix --verify to error out when passed junk after a good rev Before this patch something like: $ git rev-parse --verify worked whatever junk was as long as could be parsed correctly. This patch makes "git rev-parse --verify" error out when passed any junk after a good rev. Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 9384a991ee..0e59707323 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -576,11 +576,11 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) show_rev(REVERSED, sha1, arg+1); continue; } + if (verify) + die_no_single_rev(quiet); as_is = 1; if (!show_file(arg)) continue; - if (verify) - die_no_single_rev(quiet); verify_filename(prefix, arg); } show_default(); -- cgit v1.2.1 From 28bfa145e4dedad9b2c81857b77ca13871c77853 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sun, 11 May 2008 18:27:36 +0200 Subject: rev-parse: fix using "--default" with "--verify" Before this patch, something like: $ git rev-parse --verify HEAD --default master did not work, while: $ git rev-parse --default master --verify HEAD worked. This patch fixes that, so that they both work (assuming HEAD and master can be parsed). Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 0e59707323..7dbf282f5e 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -583,6 +583,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) continue; verify_filename(prefix, arg); } + if (verify && revs_count == 1) + return 0; show_default(); if (verify && revs_count != 1) die_no_single_rev(quiet); -- cgit v1.2.1 From dfd1b749befcb581d84722caa6a3af56ce6526a5 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sun, 11 May 2008 18:28:25 +0200 Subject: rev-parse --verify: do not output anything on error Before this patch, when "git rev-parse --verify" was passed at least one good rev and then anything, it would output something for the good rev even if it would latter exit on error. With this patch, we only output something if everything is ok. Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 7dbf282f5e..f8d8548e9c 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -28,8 +28,6 @@ static int symbolic; static int abbrev; static int output_sq; -static int revs_count; - /* * Some arguments are relevant "revision" arguments, * others are about output format or other details. @@ -102,7 +100,6 @@ static void show_rev(int type, const unsigned char *sha1, const char *name) if (!(filter & DO_REVS)) return; def = NULL; - revs_count++; if (type != show_type) putchar('^'); @@ -150,7 +147,7 @@ static int show_flag(const char *arg) return 0; } -static void show_default(void) +static int show_default(void) { const char *s = def; @@ -160,9 +157,10 @@ static void show_default(void) def = NULL; if (!get_sha1(s, sha1)) { show_rev(NORMAL, sha1, s); - return; + return 1; } } + return 0; } static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data) @@ -375,8 +373,9 @@ static void die_no_single_rev(int quiet) int cmd_rev_parse(int argc, const char **argv, const char *prefix) { - int i, as_is = 0, verify = 0, quiet = 0; + int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0; unsigned char sha1[20]; + const char *name = NULL; if (argc > 1 && !strcmp("--parseopt", argv[1])) return cmd_parseopt(argc - 1, argv + 1, prefix); @@ -568,12 +567,17 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) /* Not a flag argument */ if (try_difference(arg)) continue; - if (!get_sha1(arg, sha1)) { - show_rev(NORMAL, sha1, arg); - continue; + name = arg; + type = NORMAL; + if (*arg == '^') { + name++; + type = REVERSED; } - if (*arg == '^' && !get_sha1(arg+1, sha1)) { - show_rev(REVERSED, sha1, arg+1); + if (!get_sha1(name, sha1)) { + if (verify) + revs_count++; + else + show_rev(type, sha1, name); continue; } if (verify) @@ -583,10 +587,14 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) continue; verify_filename(prefix, arg); } - if (verify && revs_count == 1) - return 0; - show_default(); - if (verify && revs_count != 1) + if (verify) { + if (revs_count == 1) { + show_rev(type, sha1, name); + return 0; + } else if (revs_count == 0 && show_default()) + return 0; die_no_single_rev(quiet); + } else + show_default(); return 0; } -- cgit v1.2.1 From ef90d6d4208a5130185b04f06e5f90a5f9959fe3 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 14 May 2008 18:46:53 +0100 Subject: Provide git_config with a callback-data parameter git_config() only had a function parameter, but no callback data parameter. This assumes that all callback functions only modify global variables. With this patch, every callback gets a void * parameter, and it is hoped that this will help the libification effort. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index f8d8548e9c..1d019b3c16 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -381,7 +381,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) return cmd_parseopt(argc - 1, argv + 1, prefix); prefix = setup_git_directory(); - git_config(git_default_config); + git_config(git_default_config, NULL); for (i = 1; i < argc; i++) { const char *arg = argv[i]; -- cgit v1.2.1 From e00f3790b88ce61f1bdc863011a122b98b43197e Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Fri, 23 May 2008 16:13:05 +0200 Subject: rev-parse --symbolic-full-name: don't print '^' if SHA1 is not a ref The intention of --symbolic-full-name is to not print anything if a revision is not an exact ref. But this command: $ git-rev-parse --symbolic-full-name --not master~1 still emitted a sole '^' to stdout (provided that there's no other ref at master~1). This fixes it. Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 90dbb9d7c1..1ae086ad17 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -96,6 +96,14 @@ static void show(const char *arg) puts(arg); } +/* Like show(), but with a negation prefix according to type */ +static void show_with_type(int type, const char *arg) +{ + if (type != show_type) + putchar('^'); + show(arg); +} + /* Output a revision, only if filter allows it */ static void show_rev(int type, const unsigned char *sha1, const char *name) { @@ -104,8 +112,6 @@ static void show_rev(int type, const unsigned char *sha1, const char *name) def = NULL; revs_count++; - if (type != show_type) - putchar('^'); if (symbolic && name) { if (symbolic == SHOW_SYMBOLIC_FULL) { unsigned char discard[20]; @@ -122,20 +128,20 @@ static void show_rev(int type, const unsigned char *sha1, const char *name) */ break; case 1: /* happy */ - show(full); + show_with_type(type, full); break; default: /* ambiguous */ error("refname '%s' is ambiguous", name); break; } } else { - show(name); + show_with_type(type, name); } } else if (abbrev) - show(find_unique_abbrev(sha1, abbrev)); + show_with_type(type, find_unique_abbrev(sha1, abbrev)); else - show(sha1_to_hex(sha1)); + show_with_type(type, sha1_to_hex(sha1)); } /* Output a flag, only if filter allows it. */ -- cgit v1.2.1 From 1b1dd23f2d6a707b7077cdf6bc6d4055bd0bfb7d Mon Sep 17 00:00:00 2001 From: Stephan Beyer Date: Sun, 13 Jul 2008 15:36:15 +0200 Subject: Make usage strings dash-less When you misuse a git command, you are shown the usage string. But this is currently shown in the dashed form. So if you just copy what you see, it will not work, when the dashed form is no longer supported. This patch makes git commands show the dash-less version. For shell scripts that do not specify OPTIONS_SPEC, git-sh-setup.sh generates a dash-less usage string now. Signed-off-by: Stephan Beyer Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index a7860ed75a..aa71f4a4fa 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -268,7 +268,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix) { static int keep_dashdash = 0; static char const * const parseopt_usage[] = { - "git-rev-parse --parseopt [options] -- [...]", + "git rev-parse --parseopt [options] -- [...]", NULL }; static struct option parseopt_opts[] = { -- cgit v1.2.1 From 2122f8b963d49a59762e121c2da571c2348dcce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Steinbrink?= Date: Sat, 26 Jul 2008 18:37:56 +0200 Subject: rev-parse: Add support for the ^! and ^@ syntax MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Those shorthands are explained in the rev-parse documentation but were not actually supported by rev-parse itself. gitk internally uses rev-parse to interpret its command line arguments, and being able to use these "limit with parents" syntax is handy there. Signed-off-by: Björn Steinbrink Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index aa71f4a4fa..9aa049ec17 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -241,6 +241,36 @@ static int try_difference(const char *arg) return 0; } +static int try_parent_shorthands(const char *arg) +{ + char *dotdot; + unsigned char sha1[20]; + struct commit *commit; + struct commit_list *parents; + int parents_only; + + if ((dotdot = strstr(arg, "^!"))) + parents_only = 0; + else if ((dotdot = strstr(arg, "^@"))) + parents_only = 1; + + if (!dotdot || dotdot[2]) + return 0; + + *dotdot = 0; + if (get_sha1(arg, sha1)) + return 0; + + if (!parents_only) + show_rev(NORMAL, sha1, arg); + commit = lookup_commit_reference(sha1); + for (parents = commit->parents; parents; parents = parents->next) + show_rev(parents_only ? NORMAL : REVERSED, + parents->item->object.sha1, arg); + + return 1; +} + static int parseopt_dump(const struct option *o, const char *arg, int unset) { struct strbuf *parsed = o->value; @@ -573,6 +603,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) /* Not a flag argument */ if (try_difference(arg)) continue; + if (try_parent_shorthands(arg)) + continue; name = arg; type = NORMAL; if (*arg == '^') { -- cgit v1.2.1 From f285a2d7ed6548666989406de8f0e7233eb84368 Mon Sep 17 00:00:00 2001 From: Brandon Casey Date: Thu, 9 Oct 2008 14:12:12 -0500 Subject: Replace calls to strbuf_init(&foo, 0) with STRBUF_INIT initializer Many call sites use strbuf_init(&foo, 0) to initialize local strbuf variable "foo" which has not been accessed since its declaration. These can be replaced with a static initialization using the STRBUF_INIT macro which is just as readable, saves a function call, and takes up fewer lines. Signed-off-by: Brandon Casey Signed-off-by: Shawn O. Pearce --- builtin-rev-parse.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 9aa049ec17..81d5a6ffc9 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -307,19 +307,17 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix) OPT_END(), }; - struct strbuf sb, parsed; + struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT; const char **usage = NULL; struct option *opts = NULL; int onb = 0, osz = 0, unb = 0, usz = 0; - strbuf_init(&parsed, 0); strbuf_addstr(&parsed, "set --"); argc = parse_options(argc, argv, parseopt_opts, parseopt_usage, PARSE_OPT_KEEP_DASHDASH); if (argc < 1 || strcmp(argv[0], "--")) usage_with_options(parseopt_usage, parseopt_opts); - strbuf_init(&sb, 0); /* get the usage up to the first line with a -- on it */ for (;;) { if (strbuf_getline(&sb, stdin, '\n') == EOF) -- cgit v1.2.1 From a45d34691ea624e93863e95706eeb1b1909304f3 Mon Sep 17 00:00:00 2001 From: Bert Wesarg Date: Mon, 13 Apr 2009 13:20:26 +0200 Subject: rev-parse: --abbrev-ref option to shorten ref name This applies the shorten_unambiguous_ref function to the object name. Default mode is controlled by core.warnAmbiguousRefs. Else it is given as optional argument to --abbrev-ref={strict|loose}. This should be faster than 'git for-each-ref --format="%(refname:short)" ' for single refs. Signed-off-by: Bert Wesarg Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 81d5a6ffc9..22c6d6ad16 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -26,6 +26,8 @@ static int show_type = NORMAL; #define SHOW_SYMBOLIC_FULL 2 static int symbolic; static int abbrev; +static int abbrev_ref; +static int abbrev_ref_strict; static int output_sq; /* @@ -109,8 +111,8 @@ static void show_rev(int type, const unsigned char *sha1, const char *name) return; def = NULL; - if (symbolic && name) { - if (symbolic == SHOW_SYMBOLIC_FULL) { + if ((symbolic || abbrev_ref) && name) { + if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) { unsigned char discard[20]; char *full; @@ -125,6 +127,9 @@ static void show_rev(int type, const unsigned char *sha1, const char *name) */ break; case 1: /* happy */ + if (abbrev_ref) + full = shorten_unambiguous_ref(full, + abbrev_ref_strict); show_with_type(type, full); break; default: /* ambiguous */ @@ -506,6 +511,20 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) symbolic = SHOW_SYMBOLIC_FULL; continue; } + if (!prefixcmp(arg, "--abbrev-ref") && + (!arg[12] || arg[12] == '=')) { + abbrev_ref = 1; + abbrev_ref_strict = warn_ambiguous_refs; + if (arg[12] == '=') { + if (!strcmp(arg + 13, "strict")) + abbrev_ref_strict = 1; + else if (!strcmp(arg + 13, "loose")) + abbrev_ref_strict = 0; + else + die("unknown mode for %s", arg); + } + continue; + } if (!strcmp(arg, "--all")) { for_each_ref(show_reference, NULL); continue; -- cgit v1.2.1 From 503253771e93fd9f29c9f0773223d456677452c4 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sat, 25 Apr 2009 06:55:26 +0200 Subject: rev-parse: add --sq-quote to shell quote arguments Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 22c6d6ad16..c5b3d6e31b 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -402,6 +402,18 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix) return 0; } +static int cmd_sq_quote(int argc, const char **argv) +{ + struct strbuf buf = STRBUF_INIT; + + if (argc) + sq_quote_argv(&buf, argv, 0); + printf("%s\n", buf.buf); + strbuf_release(&buf); + + return 0; +} + static void die_no_single_rev(int quiet) { if (quiet) @@ -419,6 +431,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) if (argc > 1 && !strcmp("--parseopt", argv[1])) return cmd_parseopt(argc - 1, argv + 1, prefix); + if (argc > 1 && !strcmp("--sq-quote", argv[1])) + return cmd_sq_quote(argc - 2, argv + 2); + prefix = setup_git_directory(); git_config(git_default_config, NULL); for (i = 1; i < argc; i++) { -- cgit v1.2.1 From 377829201783b8a648e07af6ce7d747e0f45dc19 Mon Sep 17 00:00:00 2001 From: Stephen Boyd Date: Sat, 23 May 2009 11:53:12 -0700 Subject: parse-opts: prepare for OPT_FILENAME To give OPT_FILENAME the prefix, we pass the prefix to parse_options() which passes the prefix to parse_options_start() which sets the prefix member of parse_opts_ctx accordingly. If there isn't a prefix in the calling context, passing NULL will suffice. Signed-off-by: Stephen Boyd Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index c5b3d6e31b..112d622cda 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -318,7 +318,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix) int onb = 0, osz = 0, unb = 0, usz = 0; strbuf_addstr(&parsed, "set --"); - argc = parse_options(argc, argv, parseopt_opts, parseopt_usage, + argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage, PARSE_OPT_KEEP_DASHDASH); if (argc < 1 || strcmp(argv[0], "--")) usage_with_options(parseopt_usage, parseopt_opts); @@ -393,7 +393,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix) /* put an OPT_END() */ ALLOC_GROW(opts, onb + 1, osz); memset(opts + onb, 0, sizeof(opts[onb])); - argc = parse_options(argc, argv, opts, usage, + argc = parse_options(argc, argv, prefix, opts, usage, keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0); strbuf_addf(&parsed, " --"); -- cgit v1.2.1 From 6e0800ef2575751f2e20d11e2cfe305304e5e9b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= Date: Sun, 14 Jun 2009 01:58:43 +0200 Subject: parse-opt: make PARSE_OPT_STOP_AT_NON_OPTION available to git rev-parse MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Uwe Kleine-König Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 112d622cda..5ea7518b0f 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -301,7 +301,7 @@ static const char *skipspaces(const char *s) static int cmd_parseopt(int argc, const char **argv, const char *prefix) { - static int keep_dashdash = 0; + static int keep_dashdash = 0, stop_at_non_option = 0; static char const * const parseopt_usage[] = { "git rev-parse --parseopt [options] -- [...]", NULL @@ -309,6 +309,9 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix) static struct option parseopt_opts[] = { OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash, "keep the `--` passed as an arg"), + OPT_BOOLEAN(0, "stop-at-non-option", &stop_at_non_option, + "stop parsing after the " + "first non-option argument"), OPT_END(), }; @@ -394,7 +397,8 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix) ALLOC_GROW(opts, onb + 1, osz); memset(opts + onb, 0, sizeof(opts[onb])); argc = parse_options(argc, argv, prefix, opts, usage, - keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0); + keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0 | + stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0); strbuf_addf(&parsed, " --"); sq_quote_argv(&parsed, argv, 0); -- cgit v1.2.1 From 0721c314a5c8fddc877140ab5a333c42c62f780d Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Sat, 27 Jun 2009 17:58:47 +0200 Subject: Use die_errno() instead of die() when checking syscalls Lots of die() calls did not actually report the kind of error, which can leave the user confused as to the real problem. Use die_errno() where we check a system/library call that sets errno on failure, or one of the following that wrap such calls: Function Passes on error from -------- -------------------- odb_pack_keep open read_ancestry fopen read_in_full xread strbuf_read xread strbuf_read_file open or strbuf_read_file strbuf_readlink readlink write_in_full xwrite Signed-off-by: Thomas Rast Signed-off-by: Junio C Hamano --- builtin-rev-parse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'builtin-rev-parse.c') diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 112d622cda..da26dbc020 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -592,7 +592,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) continue; } if (!getcwd(cwd, PATH_MAX)) - die("unable to get current working directory"); + die_errno("unable to get current working directory"); printf("%s/.git\n", cwd); continue; } -- cgit v1.2.1