|
15 | 15 |
|
16 | 16 | #include "common.h" |
17 | 17 |
|
18 | | -static int revwalk_parseopts(git_repository *repo, git_revwalk *walk, int nopts, char **opts); |
| 18 | +#include <assert.h> |
| 19 | + |
| 20 | +static int revwalk_parse_options(git_sort_t *sort, struct args_info *args); |
| 21 | +static int revwalk_parse_revs(git_repository *repo, git_revwalk *walk, struct args_info *args); |
19 | 22 |
|
20 | 23 | int lg2_rev_list(git_repository *repo, int argc, char **argv) |
21 | 24 | { |
| 25 | + struct args_info args = ARGS_INFO_INIT; |
22 | 26 | git_revwalk *walk; |
23 | 27 | git_oid oid; |
| 28 | + git_sort_t sort; |
24 | 29 | char buf[GIT_OID_HEXSZ+1]; |
25 | 30 |
|
| 31 | + check_lg2(revwalk_parse_options(&sort, &args), "parsing options", NULL); |
| 32 | + |
26 | 33 | check_lg2(git_revwalk_new(&walk, repo), "allocating revwalk", NULL); |
27 | | - check_lg2(revwalk_parseopts(repo, walk, argc-1, argv+1), "parsing options", NULL); |
| 34 | + git_revwalk_sorting(walk, sort); |
| 35 | + check_lg2(revwalk_parse_revs(repo, walk, &args), "parsing revs", NULL); |
28 | 36 |
|
29 | 37 | while (!git_revwalk_next(&oid, walk)) { |
30 | 38 | git_oid_fmt(buf, &oid); |
31 | 39 | buf[GIT_OID_HEXSZ] = '\0'; |
32 | 40 | printf("%s\n", buf); |
33 | 41 | } |
34 | 42 |
|
| 43 | + git_revwalk_free(walk); |
35 | 44 | return 0; |
36 | 45 | } |
37 | 46 |
|
@@ -80,33 +89,60 @@ static int push_range(git_repository *repo, git_revwalk *walk, const char *range |
80 | 89 | return error; |
81 | 90 | } |
82 | 91 |
|
83 | | -static int revwalk_parseopts(git_repository *repo, git_revwalk *walk, int nopts, char **opts) |
| 92 | +static void print_usage(void) |
| 93 | +{ |
| 94 | + fprintf(stderr, "rev-list [--git-dir=dir] [--topo-order|--date-order] [--reverse] <revspec>\n"); |
| 95 | + exit(-1); |
| 96 | +} |
| 97 | + |
| 98 | +static int revwalk_parse_options(git_sort_t *sort, struct args_info *args) |
84 | 99 | { |
85 | | - int hide, i, error; |
86 | | - unsigned int sorting = GIT_SORT_NONE; |
| 100 | + assert(sort && args); |
| 101 | + *sort = GIT_SORT_NONE; |
| 102 | + |
| 103 | + if (args->argc < 1) |
| 104 | + print_usage(); |
| 105 | + |
| 106 | + for (args->pos = 1; args->pos < args->argc; ++args->pos) { |
| 107 | + const char *curr = args->argv[args->pos]; |
| 108 | + |
| 109 | + if (!strcmp(curr, "--topo-order")) { |
| 110 | + *sort |= GIT_SORT_TOPOLOGICAL; |
| 111 | + } else if (!strcmp(curr, "--date-order")) { |
| 112 | + *sort |= GIT_SORT_TIME; |
| 113 | + } else if (!strcmp(curr, "--reverse")) { |
| 114 | + *sort |= (*sort & ~GIT_SORT_REVERSE) ^ GIT_SORT_REVERSE; |
| 115 | + } else { |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + return 0; |
| 120 | +} |
| 121 | + |
| 122 | +static int revwalk_parse_revs(git_repository *repo, git_revwalk *walk, struct args_info *args) |
| 123 | +{ |
| 124 | + int hide, error; |
| 125 | + git_oid oid; |
87 | 126 |
|
88 | 127 | hide = 0; |
89 | | - for (i = 0; i < nopts; i++) { |
90 | | - if (!strcmp(opts[i], "--topo-order")) { |
91 | | - sorting = GIT_SORT_TOPOLOGICAL | (sorting & GIT_SORT_REVERSE); |
92 | | - git_revwalk_sorting(walk, sorting); |
93 | | - } else if (!strcmp(opts[i], "--date-order")) { |
94 | | - sorting = GIT_SORT_TIME | (sorting & GIT_SORT_REVERSE); |
95 | | - git_revwalk_sorting(walk, sorting); |
96 | | - } else if (!strcmp(opts[i], "--reverse")) { |
97 | | - sorting = (sorting & ~GIT_SORT_REVERSE) |
98 | | - | ((sorting & GIT_SORT_REVERSE) ? 0 : GIT_SORT_REVERSE); |
99 | | - git_revwalk_sorting(walk, sorting); |
100 | | - } else if (!strcmp(opts[i], "--not")) { |
| 128 | + for (; args->pos < args->argc; ++args->pos) { |
| 129 | + const char *curr = args->argv[args->pos]; |
| 130 | + |
| 131 | + if (!strcmp(curr, "--not")) { |
101 | 132 | hide = !hide; |
102 | | - } else if (opts[i][0] == '^') { |
103 | | - if ((error = push_spec(repo, walk, opts[i] + 1, !hide))) |
| 133 | + } else if (curr[0] == '^') { |
| 134 | + if ((error = push_spec(repo, walk, curr + 1, !hide))) |
104 | 135 | return error; |
105 | | - } else if (strstr(opts[i], "..")) { |
106 | | - if ((error = push_range(repo, walk, opts[i], hide))) |
| 136 | + } else if (strstr(curr, "..")) { |
| 137 | + if ((error = push_range(repo, walk, curr, hide))) |
107 | 138 | return error; |
108 | 139 | } else { |
109 | | - if ((error = push_spec(repo, walk, opts[i], hide))) |
| 140 | + if (push_spec(repo, walk, curr, hide) == 0) |
| 141 | + continue; |
| 142 | + |
| 143 | + if ((error = git_oid_fromstr(&oid, curr))) |
| 144 | + return error; |
| 145 | + if ((error = push_commit(walk, &oid, hide))) |
110 | 146 | return error; |
111 | 147 | } |
112 | 148 | } |
|
0 commit comments