forked from AndreyG/libgit2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckout.cpp
More file actions
288 lines (242 loc) · 7.06 KB
/
Copy pathcheckout.cpp
File metadata and controls
288 lines (242 loc) · 7.06 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* libgit2 "checkout" example - shows how to perform checkouts
*
* Written by the libgit2 contributors
*
* To the extent possible under law, the author(s) have dedicated all copyright
* and related and neighboring rights to this software to the public domain
* worldwide. This software is distributed without any warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication along
* with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include "git2cpp/repo.h"
#include "git2cpp/initializer.h"
#include "git2cpp/annotated_commit.h"
#include <git2/errors.h>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
/* Define the printf format specifer to use for size_t output */
#if defined(_MSC_VER) || defined(__MINGW32__)
# define PRIuZ "Iu"
# define PRIxZ "Ix"
# define PRIdZ "Id"
#else
# define PRIuZ "zu"
# define PRIxZ "zx"
# define PRIdZ "zd"
#endif
/**
* The following example demonstrates how to do checkouts with libgit2.
*
* Recognized options are :
* --force: force the checkout to happen.
* --[no-]progress: show checkout progress, on by default.
* --perf: show performance data.
*/
namespace {
struct args_info {
int argc;
char **argv;
int pos;
};
typedef struct {
int force : 1;
int progress : 1;
int perf : 1;
} checkout_options;
[[noreturn]] void print_usage()
{
fprintf(stderr, "usage: checkout [options] <branch>\n"
"Options are :\n"
" --git-dir: use the following git repository.\n"
" --force: force the checkout.\n"
" --[no-]progress: show checkout progress.\n"
" --perf: show performance data.\n");
exit(1);
}
bool match_bool_arg(int *out, args_info *args, const char *opt)
{
const char *found = args->argv[args->pos];
if (!strcmp(found, opt)) {
*out = 1;
return true;
}
if (!strncmp(found, "--no-", strlen("--no-")) &&
!strcmp(found + strlen("--no-"), opt + 2)) {
*out = 0;
return true;
}
*out = -1;
return false;
}
[[noreturn]] void fatal(const char *message, const char *extra)
{
if (extra)
fprintf(stderr, "%s %s\n", message, extra);
else
fprintf(stderr, "%s\n", message);
exit(1);
}
size_t is_prefixed(const char *str, const char *pfx)
{
size_t len = strlen(pfx);
return strncmp(str, pfx, len) ? 0 : len;
}
bool match_str_arg(const char **out, struct args_info *args, const char *opt)
{
const char *found = args->argv[args->pos];
size_t len = is_prefixed(found, opt);
if (!len)
return false;
if (!found[len]) {
if (args->pos + 1 == args->argc)
fatal("expected value following argument", opt);
args->pos += 1;
*out = args->argv[args->pos];
return true;
}
if (found[len] == '=') {
*out = found + len + 1;
return true;
}
return false;
}
void parse_options(const char **repo_path, checkout_options *opts, struct args_info *args)
{
if (args->argc <= 1)
print_usage();
memset(opts, 0, sizeof(*opts));
/* Default values */
opts->progress = 1;
for (args->pos = 1; args->pos < args->argc; ++args->pos) {
const char *curr = args->argv[args->pos];
int bool_arg;
if (strcmp(curr, "--") == 0) {
break;
} else if (!strcmp(curr, "--force")) {
opts->force = 1;
} else if (match_bool_arg(&bool_arg, args, "--progress")) {
opts->progress = bool_arg;
} else if (match_bool_arg(&bool_arg, args, "--perf")) {
opts->perf = bool_arg;
} else if (match_str_arg(repo_path, args, "--git-dir")) {
continue;
} else {
break;
}
}
}
/**
* This function is called to report progression, ie. it's called once with
* a NULL path and the number of total steps, then for each subsequent path,
* the current completed_step value.
*/
void print_checkout_progress(const char *path, size_t completed_steps, size_t total_steps, void */*payload*/)
{
if (path == NULL) {
printf("checkout started: %" PRIuZ " steps\n", total_steps);
} else {
printf("checkout: %s %" PRIuZ "/%" PRIuZ "\n", path, completed_steps, total_steps);
}
}
/**
* This function is called when the checkout completes, and is used to report the
* number of syscalls performed.
*/
void print_perf_data(const git_checkout_perfdata *perfdata, void* /*payload*/)
{
printf("perf: stat: %" PRIuZ " mkdir: %" PRIuZ " chmod: %" PRIuZ "\n",
perfdata->stat_calls, perfdata->mkdir_calls, perfdata->chmod_calls);
}
/**
* This is the main "checkout <branch>" function, responsible for performing
* a branch-based checkout.
*/
void perform_checkout_ref(git::Repository & repo, git::AnnotatedCommit const & target, checkout_options const & opts)
{
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
/** Setup our checkout options from the parsed options */
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
if (opts.force)
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
if (opts.progress)
checkout_opts.progress_cb = print_checkout_progress;
if (opts.perf)
checkout_opts.perfdata_cb = print_perf_data;
/** Grab the commit we're interested to move to */
auto target_commit = repo.commit_lookup(target.commit_id());
/**
* Perform the checkout so the workdir corresponds to what target_commit
* contains.
*
* Note that it's okay to pass a git_commit here, because it will be
* peeled to a tree.
*/
if (repo.checkout_tree(target_commit, checkout_opts))
{
fprintf(stderr, "failed to checkout tree: %s\n", git_error_last()->message);
return;
}
/**
* Now that the checkout has completed, we have to update HEAD.
*
* Depending on the "origin" of target (ie. it's an OID or a branch name),
* we might need to detach HEAD.
*/
int err;
if (auto ref = target.commit_ref()) {
err = repo.set_head(ref);
} else {
err = repo.set_head_detached(target);
}
if (err != 0) {
fprintf(stderr, "failed to update HEAD reference: %s\n", git_error_last()->message);
}
}
git::AnnotatedCommit resolve_refish(git::Repository const & repo, const char *refish)
{
if (auto ref = repo.dwim(refish))
return repo.annotated_commit_from_ref(ref);
auto obj = repo.revparse_single(refish);
return repo.annotated_commit_lookup(obj.single()->id());
}
}
/** That example's entry point */
int main(int argc, char **argv)
{
args_info args { argc, argv };
checkout_options opts;
const char *path = ".";
/** Parse our command line options */
parse_options(&path, &opts, &args);
/** Initialize the library */
auto_git_initializer;
git::Repository repo(path);
/** Make sure we're not about to checkout while something else is going on */
auto const state = repo.state();
if (state != GIT_REPOSITORY_STATE_NONE) {
fprintf(stderr, "repository is in unexpected state %d\n", state);
return EXIT_FAILURE;
}
if (args.pos >= args.argc) {
fprintf(stderr, "unhandled\n");
return EXIT_FAILURE;
}
if (strcmp("--", args.argv[args.pos]))
{
/**
* Try to checkout the given path
*/
fprintf(stderr, "unhandled path-based checkout\n");
return EXIT_FAILURE;
}
/**
* Try to resolve a "refish" argument to a target libgit2 can use
*/
auto checkout_target = resolve_refish(repo, args.argv[args.pos]);
perform_checkout_ref(repo, checkout_target, opts);
}