Skip to content

Commit 38b3c0d

Browse files
committed
Add snippets for checkout callbacks
1 parent d228210 commit 38b3c0d

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

docs/guides/101-samples/index.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,4 +1281,61 @@ int error = git_checkout_head(repo, &opts);
12811281
12821282
<h3 id="checkout_progress">Progress</h3>
12831283
1284+
```c
1285+
typedef struct { /* … */ } progress_data;
1286+
void checkout_progress(
1287+
const char *path,
1288+
size_t completed_steps,
1289+
size_t total_steps,
1290+
void *payload)
1291+
{
1292+
progress_data *pd = (progress_data*)payload;
1293+
int checkout_percent = total_steps > 0
1294+
? (100 * completed_steps) / total_steps
1295+
: 0;
1296+
/* Do something with checkout progress */
1297+
}
1298+
1299+
/* … */
1300+
progress_data d = {0};
1301+
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
1302+
opts.progress_cb = checkout_progress;
1303+
opts.progress_payload = &d;
1304+
1305+
int error = git_checkout_head(repo, &opts);
1306+
```
1307+
1308+
(
1309+
[`git_checkout_opts`](http://libgit2.github.com/libgit2/#HEAD/type/git_checkout_opts),
1310+
[`git_checkout_progress_cb`](http://libgit2.github.com/libgit2/#HEAD/type/git_checkout_progress_cb)
1311+
)
1312+
12841313
<h3 id="checkout_notify">Notify</h3>
1314+
1315+
```c
1316+
typedef struct { /**/ } notify_data;
1317+
static int checkout_notify(
1318+
git_checkout_notify_t why,
1319+
const char *path,
1320+
const git_diff_file *baseline,
1321+
const git_diff_file *target,
1322+
const git_diff_file *workdir,
1323+
void *payload)
1324+
{
1325+
notify_data *d = (notify_data*)payload;
1326+
/**/
1327+
}
1328+
1329+
/**/
1330+
notify_data d = {0};
1331+
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
1332+
opts.notify_cb = checkout_notify;
1333+
opts.notify_payload = &d;
1334+
1335+
int error = git_checkout_head(repo, &opts);
1336+
```
1337+
1338+
(
1339+
[`git_checkout_opts`](http://libgit2.github.com/libgit2/#HEAD/type/git_checkout_opts),
1340+
[`git_checkout_notify_t`](http://libgit2.github.com/libgit2/#HEAD/type/git_checkout_notify_t)
1341+
)

0 commit comments

Comments
 (0)