Skip to content

Commit 0d44d3d

Browse files
arrbeeVicent Marti
authored andcommitted
Extending log example code
This adds more command line processing to the example version of log. In particular, this adds the funky command line processing that allows an arbitrary series of revisions followed by an arbitrary number of paths and/or glob patterns. The actual logging part still isn't implemented.
1 parent d2ce27d commit 0d44d3d

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

examples/log.c

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,71 @@ static void usage(const char *message, const char *arg)
3232

3333
int main(int argc, char *argv[])
3434
{
35-
int i;
35+
int i, j, last_nonoption, force_files = -1;
3636
char *a;
3737
const char *dir = ".";
3838
git_repository *repo;
39+
git_revwalk *walker;
40+
git_revspec revs;
3941

4042
git_threads_init();
4143

42-
for (i = 1; i < argc; ++i) {
44+
for (i = 1, last_nonoption = 1; i < argc; ++i) {
4345
a = argv[i];
4446

45-
if (a[0] != '-') {
47+
if (a[0] != '-' || force_files > 0) {
48+
/* condense args not prefixed with '-' to start of argv */
49+
if (last_nonoption != i)
50+
argv[last_nonoption] = a;
51+
last_nonoption++;
4652
}
53+
else if (!strcmp(a, "--"))
54+
force_files = last_nonoption; /* copy all args as filenames */
4755
else if (!check_str_param(a, "--git-dir=", &dir))
4856
usage("Unknown argument", a);
4957
}
5058

5159
check(git_repository_open_ext(&repo, dir, 0, NULL),
5260
"Could not open repository");
61+
check(git_revwalk_new(&walker, repo),
62+
"Could not create revision walker");
63+
64+
if (force_files < 0)
65+
force_files = last_nonoption;
66+
67+
for (i = 1; i < force_files; ) {
68+
printf("option '%s'\n", argv[i]);
69+
70+
if (!git_revparse(&revs, repo, argv[i])) {
71+
char str[GIT_OID_HEXSZ+1];
72+
73+
if (revs.from) {
74+
git_oid_tostr(str, sizeof(str), git_object_id(revs.from));
75+
printf("revwalk from %s\n", str);
76+
}
77+
if (revs.to) {
78+
git_oid_tostr(str, sizeof(str), git_object_id(revs.to));
79+
printf("revwalk to %s\n", str);
80+
}
81+
82+
/* push / hide / merge-base in revwalker */
83+
84+
++i;
85+
} else {
86+
/* shift array down */
87+
for (a = argv[i], j = i + 1; j < force_files; ++j)
88+
argv[j - 1] = argv[j];
89+
argv[--force_files] = a;
90+
}
91+
}
92+
93+
if (i == 1) {
94+
/* no revs pushed so push HEAD */
95+
printf("revwalk HEAD\n");
96+
}
97+
98+
for (i = force_files; i < last_nonoption; ++i)
99+
printf("file %s\n", argv[i]);
53100

54101
git_repository_free(repo);
55102
git_threads_shutdown();

0 commit comments

Comments
 (0)