Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fe42557

Browse files
committed
examples: buff up rev-list by adding OID support
This allows the example to be used as a quick revwalk test harness.
1 parent 313908f commit fe42557

File tree

1 file changed

+58
-22
lines changed

1 file changed

+58
-22
lines changed

examples/rev-list.c

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,32 @@
1515

1616
#include "common.h"
1717

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);
1922

2023
int lg2_rev_list(git_repository *repo, int argc, char **argv)
2124
{
25+
struct args_info args = ARGS_INFO_INIT;
2226
git_revwalk *walk;
2327
git_oid oid;
28+
git_sort_t sort;
2429
char buf[GIT_OID_HEXSZ+1];
2530

31+
check_lg2(revwalk_parse_options(&sort, &args), "parsing options", NULL);
32+
2633
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);
2836

2937
while (!git_revwalk_next(&oid, walk)) {
3038
git_oid_fmt(buf, &oid);
3139
buf[GIT_OID_HEXSZ] = '\0';
3240
printf("%s\n", buf);
3341
}
3442

43+
git_revwalk_free(walk);
3544
return 0;
3645
}
3746

@@ -80,33 +89,60 @@ static int push_range(git_repository *repo, git_revwalk *walk, const char *range
8089
return error;
8190
}
8291

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)
8499
{
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;
87126

88127
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")) {
101132
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)))
104135
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)))
107138
return error;
108139
} 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)))
110146
return error;
111147
}
112148
}

0 commit comments

Comments
 (0)