2005-06-14 01:06:50 +08:00
|
|
|
/*
|
|
|
|
* rev-parse.c
|
|
|
|
*
|
|
|
|
* Copyright (C) Linus Torvalds, 2005
|
|
|
|
*/
|
|
|
|
#include "cache.h"
|
2005-06-21 11:28:09 +08:00
|
|
|
#include "commit.h"
|
2005-07-04 04:07:52 +08:00
|
|
|
#include "refs.h"
|
2005-06-21 11:28:09 +08:00
|
|
|
|
2005-06-25 01:12:55 +08:00
|
|
|
static char *def = NULL;
|
|
|
|
static int no_revs = 0;
|
|
|
|
static int single_rev = 0;
|
|
|
|
static int revs_only = 0;
|
|
|
|
static int do_rev_argument = 1;
|
|
|
|
static int output_revs = 0;
|
2005-07-07 01:08:08 +08:00
|
|
|
static int flags_only = 0;
|
|
|
|
static int no_flags = 0;
|
[PATCH] Help scripts that use git-rev-parse to grok args with SP/TAB/LF
The git-rev-parse command uses LF to separate each argument it
parses, so its users at least need to set IFS to LF to be able
to handle filenames with embedded SPs and TABs. Some commands,
however, can take and do expect arguments with embedded LF,
notably, "-S" (pickaxe) of diff family, so even this workaround
does not work for them.
When --sq flag to git-rev-parse is given, instead of showing one
argument per line, it outputs arguments quoted for consumption
with "eval" by the caller, to remedy this situation.
As an example, this patch converts git-whatchanged to use this
new feature.
Signed-off-by: Junio C Hamano <junkio@cox.net>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-07-23 10:08:32 +08:00
|
|
|
static int output_sq = 0;
|
2005-06-25 01:12:55 +08:00
|
|
|
|
2005-06-27 02:34:30 +08:00
|
|
|
#define NORMAL 0
|
|
|
|
#define REVERSED 1
|
|
|
|
static int show_type = NORMAL;
|
|
|
|
|
2005-06-14 02:14:20 +08:00
|
|
|
/*
|
|
|
|
* Some arguments are relevant "revision" arguments,
|
|
|
|
* others are about output format or other details.
|
|
|
|
* This sorts it all out.
|
|
|
|
*/
|
|
|
|
static int is_rev_argument(const char *arg)
|
|
|
|
{
|
|
|
|
static const char *rev_args[] = {
|
|
|
|
"--max-count=",
|
|
|
|
"--max-age=",
|
|
|
|
"--min-age=",
|
|
|
|
"--merge-order",
|
2005-08-09 10:31:37 +08:00
|
|
|
"--topo-order",
|
|
|
|
"--bisect",
|
|
|
|
"--no-merges",
|
2005-06-14 02:14:20 +08:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
const char **p = rev_args;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
const char *str = *p++;
|
|
|
|
int len;
|
|
|
|
if (!str)
|
|
|
|
return 0;
|
|
|
|
len = strlen(str);
|
|
|
|
if (!strncmp(arg, str, len))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[PATCH] Help scripts that use git-rev-parse to grok args with SP/TAB/LF
The git-rev-parse command uses LF to separate each argument it
parses, so its users at least need to set IFS to LF to be able
to handle filenames with embedded SPs and TABs. Some commands,
however, can take and do expect arguments with embedded LF,
notably, "-S" (pickaxe) of diff family, so even this workaround
does not work for them.
When --sq flag to git-rev-parse is given, instead of showing one
argument per line, it outputs arguments quoted for consumption
with "eval" by the caller, to remedy this situation.
As an example, this patch converts git-whatchanged to use this
new feature.
Signed-off-by: Junio C Hamano <junkio@cox.net>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-07-23 10:08:32 +08:00
|
|
|
static void show(const char *arg)
|
|
|
|
{
|
|
|
|
if (output_sq) {
|
|
|
|
int sq = '\'', ch;
|
|
|
|
|
|
|
|
putchar(sq);
|
|
|
|
while ((ch = *arg++)) {
|
|
|
|
if (ch == sq)
|
|
|
|
fputs("'\\'", stdout);
|
|
|
|
putchar(ch);
|
|
|
|
}
|
|
|
|
putchar(sq);
|
|
|
|
putchar(' ');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
puts(arg);
|
|
|
|
}
|
|
|
|
|
2005-07-04 04:07:52 +08:00
|
|
|
static void show_rev(int type, const unsigned char *sha1)
|
2005-06-25 01:12:55 +08:00
|
|
|
{
|
|
|
|
if (no_revs)
|
|
|
|
return;
|
|
|
|
output_revs++;
|
[PATCH] Help scripts that use git-rev-parse to grok args with SP/TAB/LF
The git-rev-parse command uses LF to separate each argument it
parses, so its users at least need to set IFS to LF to be able
to handle filenames with embedded SPs and TABs. Some commands,
however, can take and do expect arguments with embedded LF,
notably, "-S" (pickaxe) of diff family, so even this workaround
does not work for them.
When --sq flag to git-rev-parse is given, instead of showing one
argument per line, it outputs arguments quoted for consumption
with "eval" by the caller, to remedy this situation.
As an example, this patch converts git-whatchanged to use this
new feature.
Signed-off-by: Junio C Hamano <junkio@cox.net>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-07-23 10:08:32 +08:00
|
|
|
|
|
|
|
/* Hexadecimal string plus possibly a carret;
|
|
|
|
* this does not have to be quoted even under output_sq.
|
|
|
|
*/
|
|
|
|
printf("%s%s%c", type == show_type ? "" : "^", sha1_to_hex(sha1),
|
|
|
|
output_sq ? ' ' : '\n');
|
2005-06-25 01:12:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void show_rev_arg(char *rev)
|
|
|
|
{
|
|
|
|
if (no_revs)
|
|
|
|
return;
|
[PATCH] Help scripts that use git-rev-parse to grok args with SP/TAB/LF
The git-rev-parse command uses LF to separate each argument it
parses, so its users at least need to set IFS to LF to be able
to handle filenames with embedded SPs and TABs. Some commands,
however, can take and do expect arguments with embedded LF,
notably, "-S" (pickaxe) of diff family, so even this workaround
does not work for them.
When --sq flag to git-rev-parse is given, instead of showing one
argument per line, it outputs arguments quoted for consumption
with "eval" by the caller, to remedy this situation.
As an example, this patch converts git-whatchanged to use this
new feature.
Signed-off-by: Junio C Hamano <junkio@cox.net>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-07-23 10:08:32 +08:00
|
|
|
show(rev);
|
2005-06-25 01:12:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void show_norev(char *norev)
|
|
|
|
{
|
2005-07-07 01:08:08 +08:00
|
|
|
if (flags_only)
|
|
|
|
return;
|
2005-06-25 01:12:55 +08:00
|
|
|
if (revs_only)
|
|
|
|
return;
|
[PATCH] Help scripts that use git-rev-parse to grok args with SP/TAB/LF
The git-rev-parse command uses LF to separate each argument it
parses, so its users at least need to set IFS to LF to be able
to handle filenames with embedded SPs and TABs. Some commands,
however, can take and do expect arguments with embedded LF,
notably, "-S" (pickaxe) of diff family, so even this workaround
does not work for them.
When --sq flag to git-rev-parse is given, instead of showing one
argument per line, it outputs arguments quoted for consumption
with "eval" by the caller, to remedy this situation.
As an example, this patch converts git-whatchanged to use this
new feature.
Signed-off-by: Junio C Hamano <junkio@cox.net>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-07-23 10:08:32 +08:00
|
|
|
show(norev);
|
2005-06-25 01:12:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void show_arg(char *arg)
|
|
|
|
{
|
2005-07-07 01:08:08 +08:00
|
|
|
if (no_flags)
|
|
|
|
return;
|
2005-06-25 01:12:55 +08:00
|
|
|
if (do_rev_argument && is_rev_argument(arg))
|
|
|
|
show_rev_arg(arg);
|
|
|
|
else
|
|
|
|
show_norev(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show_default(void)
|
|
|
|
{
|
|
|
|
char *s = def;
|
|
|
|
|
|
|
|
if (s) {
|
|
|
|
unsigned char sha1[20];
|
|
|
|
|
|
|
|
def = NULL;
|
2005-08-04 13:15:49 +08:00
|
|
|
if (!get_sha1(s, sha1)) {
|
2005-06-27 02:34:30 +08:00
|
|
|
show_rev(NORMAL, sha1);
|
2005-06-25 01:12:55 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
show_arg(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-04 04:07:52 +08:00
|
|
|
static int show_reference(const char *refname, const unsigned char *sha1)
|
|
|
|
{
|
|
|
|
show_rev(NORMAL, sha1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-14 01:06:50 +08:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2005-06-25 01:12:55 +08:00
|
|
|
int i, as_is = 0;
|
2005-06-14 01:06:50 +08:00
|
|
|
unsigned char sha1[20];
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
char *arg = argv[i];
|
|
|
|
char *dotdot;
|
|
|
|
|
|
|
|
if (as_is) {
|
2005-06-25 01:12:55 +08:00
|
|
|
show_norev(arg);
|
2005-06-14 01:06:50 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*arg == '-') {
|
|
|
|
if (!strcmp(arg, "--")) {
|
2005-06-25 01:12:55 +08:00
|
|
|
show_default();
|
2005-06-14 01:21:11 +08:00
|
|
|
if (revs_only)
|
|
|
|
break;
|
2005-06-14 01:06:50 +08:00
|
|
|
as_is = 1;
|
|
|
|
}
|
|
|
|
if (!strcmp(arg, "--default")) {
|
|
|
|
def = argv[i+1];
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
2005-06-14 01:21:11 +08:00
|
|
|
if (!strcmp(arg, "--revs-only")) {
|
|
|
|
revs_only = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(arg, "--no-revs")) {
|
|
|
|
no_revs = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2005-07-07 01:08:08 +08:00
|
|
|
if (!strcmp(arg, "--flags")) {
|
|
|
|
flags_only = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(arg, "--no-flags")) {
|
|
|
|
no_flags = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2005-06-25 01:12:55 +08:00
|
|
|
if (!strcmp(arg, "--verify")) {
|
|
|
|
revs_only = 1;
|
|
|
|
do_rev_argument = 0;
|
|
|
|
single_rev = 1;
|
|
|
|
continue;
|
2005-06-14 02:14:20 +08:00
|
|
|
}
|
[PATCH] Help scripts that use git-rev-parse to grok args with SP/TAB/LF
The git-rev-parse command uses LF to separate each argument it
parses, so its users at least need to set IFS to LF to be able
to handle filenames with embedded SPs and TABs. Some commands,
however, can take and do expect arguments with embedded LF,
notably, "-S" (pickaxe) of diff family, so even this workaround
does not work for them.
When --sq flag to git-rev-parse is given, instead of showing one
argument per line, it outputs arguments quoted for consumption
with "eval" by the caller, to remedy this situation.
As an example, this patch converts git-whatchanged to use this
new feature.
Signed-off-by: Junio C Hamano <junkio@cox.net>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-07-23 10:08:32 +08:00
|
|
|
if (!strcmp(arg, "--sq")) {
|
|
|
|
output_sq = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2005-06-27 02:34:30 +08:00
|
|
|
if (!strcmp(arg, "--not")) {
|
|
|
|
show_type ^= REVERSED;
|
|
|
|
continue;
|
|
|
|
}
|
2005-07-04 04:07:52 +08:00
|
|
|
if (!strcmp(arg, "--all")) {
|
|
|
|
for_each_ref(show_reference);
|
|
|
|
continue;
|
|
|
|
}
|
2005-06-25 01:12:55 +08:00
|
|
|
show_arg(arg);
|
2005-06-14 01:06:50 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dotdot = strstr(arg, "..");
|
|
|
|
if (dotdot) {
|
|
|
|
unsigned char end[20];
|
|
|
|
char *n = dotdot+2;
|
|
|
|
*dotdot = 0;
|
2005-08-04 13:15:49 +08:00
|
|
|
if (!get_sha1(arg, sha1)) {
|
2005-06-14 01:06:50 +08:00
|
|
|
if (!*n)
|
|
|
|
n = "HEAD";
|
2005-08-04 13:15:49 +08:00
|
|
|
if (!get_sha1(n, end)) {
|
2005-06-14 01:21:11 +08:00
|
|
|
if (no_revs)
|
|
|
|
continue;
|
|
|
|
def = NULL;
|
2005-06-27 02:34:30 +08:00
|
|
|
show_rev(NORMAL, end);
|
|
|
|
show_rev(REVERSED, sha1);
|
2005-06-14 01:06:50 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*dotdot = '.';
|
|
|
|
}
|
2005-08-04 13:15:49 +08:00
|
|
|
if (!get_sha1(arg, sha1)) {
|
2005-06-20 23:29:13 +08:00
|
|
|
if (no_revs)
|
|
|
|
continue;
|
|
|
|
def = NULL;
|
2005-06-27 02:34:30 +08:00
|
|
|
show_rev(NORMAL, sha1);
|
2005-06-20 23:29:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
2005-08-04 13:15:49 +08:00
|
|
|
if (*arg == '^' && !get_sha1(arg+1, sha1)) {
|
2005-06-20 23:29:13 +08:00
|
|
|
if (no_revs)
|
|
|
|
continue;
|
|
|
|
def = NULL;
|
2005-06-27 02:34:30 +08:00
|
|
|
show_rev(REVERSED, sha1);
|
2005-06-20 23:29:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
2005-06-25 01:12:55 +08:00
|
|
|
show_default();
|
|
|
|
show_norev(arg);
|
|
|
|
}
|
|
|
|
show_default();
|
|
|
|
if (single_rev && output_revs != 1) {
|
|
|
|
fprintf(stderr, "Needed a single revision\n");
|
|
|
|
exit(1);
|
2005-06-14 01:06:50 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|