]>
git.proxmox.com Git - libgit2.git/blob - examples/rev-list.c
2 * libgit2 "rev-list" example - shows how to transform a rev-spec into a list
5 * Written by the libgit2 contributors
7 * To the extent possible under law, the author(s) have dedicated all copyright
8 * and related and neighboring rights to this software to the public domain
9 * worldwide. This software is distributed without any warranty.
11 * You should have received a copy of the CC0 Public Domain Dedication along
12 * with this software. If not, see
13 * <http://creativecommons.org/publicdomain/zero/1.0/>.
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
);
23 int lg2_rev_list(git_repository
*repo
, int argc
, char **argv
)
25 struct args_info args
= ARGS_INFO_INIT
;
29 char buf
[GIT_OID_HEXSZ
+1];
31 check_lg2(revwalk_parse_options(&sort
, &args
), "parsing options", NULL
);
33 check_lg2(git_revwalk_new(&walk
, repo
), "allocating revwalk", NULL
);
34 git_revwalk_sorting(walk
, sort
);
35 check_lg2(revwalk_parse_revs(repo
, walk
, &args
), "parsing revs", NULL
);
37 while (!git_revwalk_next(&oid
, walk
)) {
38 git_oid_fmt(buf
, &oid
);
39 buf
[GIT_OID_HEXSZ
] = '\0';
43 git_revwalk_free(walk
);
47 static int push_commit(git_revwalk
*walk
, const git_oid
*oid
, int hide
)
50 return git_revwalk_hide(walk
, oid
);
52 return git_revwalk_push(walk
, oid
);
55 static int push_spec(git_repository
*repo
, git_revwalk
*walk
, const char *spec
, int hide
)
60 if ((error
= git_revparse_single(&obj
, repo
, spec
)) < 0)
63 error
= push_commit(walk
, git_object_id(obj
), hide
);
68 static int push_range(git_repository
*repo
, git_revwalk
*walk
, const char *range
, int hide
)
73 if ((error
= git_revparse(&revspec
, repo
, range
)))
76 if (revspec
.flags
& GIT_REVPARSE_MERGE_BASE
) {
77 /* TODO: support "<commit>...<commit>" */
78 return GIT_EINVALIDSPEC
;
81 if ((error
= push_commit(walk
, git_object_id(revspec
.from
), !hide
)))
84 error
= push_commit(walk
, git_object_id(revspec
.to
), hide
);
87 git_object_free(revspec
.from
);
88 git_object_free(revspec
.to
);
92 static void print_usage(void)
94 fprintf(stderr
, "rev-list [--git-dir=dir] [--topo-order|--date-order] [--reverse] <revspec>\n");
98 static int revwalk_parse_options(git_sort_t
*sort
, struct args_info
*args
)
100 assert(sort
&& args
);
101 *sort
= GIT_SORT_NONE
;
106 for (args
->pos
= 1; args
->pos
< args
->argc
; ++args
->pos
) {
107 const char *curr
= args
->argv
[args
->pos
];
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
;
122 static int revwalk_parse_revs(git_repository
*repo
, git_revwalk
*walk
, struct args_info
*args
)
128 for (; args
->pos
< args
->argc
; ++args
->pos
) {
129 const char *curr
= args
->argv
[args
->pos
];
131 if (!strcmp(curr
, "--not")) {
133 } else if (curr
[0] == '^') {
134 if ((error
= push_spec(repo
, walk
, curr
+ 1, !hide
)))
136 } else if (strstr(curr
, "..")) {
137 if ((error
= push_range(repo
, walk
, curr
, hide
)))
140 if (push_spec(repo
, walk
, curr
, hide
) == 0)
143 if ((error
= git_oid_fromstr(&oid
, curr
)))
145 if ((error
= push_commit(walk
, &oid
, hide
)))