Be more flexible with argument order and format
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
diff --git a/examples/blame.c b/examples/blame.c
index a7d0a12..6423b3c 100644
--- a/examples/blame.c
+++ b/examples/blame.c
@@ -17,7 +17,7 @@ static void usage(const char *msg, const char *arg)
fprintf(stderr, "%s: %s\n", msg, arg);
else if (msg)
fprintf(stderr, "%s\n", msg);
- fprintf(stderr, "usage: blame <path> [options] [<commit range>]\n");
+ fprintf(stderr, "usage: blame [options] [<commit range>] <path>\n");
fprintf(stderr, "\n");
fprintf(stderr, " <commit range> example: `HEAD~10..HEAD`, or `1234abcd`\n");
fprintf(stderr, " -L <n,m> process only line range n-m, counting from 1\n");
@@ -30,8 +30,8 @@ static void usage(const char *msg, const char *arg)
int main(int argc, char *argv[])
{
int i, line;
- char *path = NULL, *a;
- const char *rawdata, *commitspec=NULL;
+ const char *path = NULL, *a;
+ const char *rawdata, *commitspec=NULL, *bare_args[3] = {0};
char spec[1024] = {0};
git_repository *repo = NULL;
git_revspec revspec = {0};
@@ -42,16 +42,24 @@ int main(int argc, char *argv[])
git_threads_init();
if (argc < 2) usage(NULL, NULL);
- path = argv[1];
- for (i=2; i<argc; i++) {
+ for (i=1; i<argc; i++) {
a = argv[i];
- if (!strcmp(a, "-M"))
+ if (a[0] != '-') {
+ int i=0;
+ while (bare_args[i] && i < 3) ++i;
+ if (i >= 3)
+ usage("Invalid argument set", NULL);
+ bare_args[i] = a;
+ }
+ else if (!strcmp(a, "--"))
+ continue;
+ else if (!strcasecmp(a, "-M"))
opts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES;
- else if (!strcmp(a, "-C"))
+ else if (!strcasecmp(a, "-C"))
opts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES;
- else if (!strcmp(a, "-L")) {
+ else if (!strcasecmp(a, "-L")) {
i++; a = argv[i];
if (i >= argc) check(-1, "Not enough arguments to -L");
check(sscanf(a, "%d,%d", &opts.min_line, &opts.max_line)-2, "-L format error");
@@ -63,6 +71,21 @@ int main(int argc, char *argv[])
}
}
+ /* Handle the bare arguments */
+ if (!bare_args[0]) usage("Please specify a path", NULL);
+ path = bare_args[0];
+ if (bare_args[1]) {
+ /* <commitspec> <path> */
+ path = bare_args[1];
+ commitspec = bare_args[0];
+ }
+ if (bare_args[2]) {
+ /* <oldcommit> <newcommit> <path> */
+ path = bare_args[2];
+ sprintf(spec, "%s..%s", bare_args[0], bare_args[1]);
+ commitspec = spec;
+ }
+
/* Open the repo */
check(git_repository_open_ext(&repo, ".", 0, NULL), "Couldn't open repository");