Skip to content

Commit 63f7fbe

Browse files
committed
Merge pull request libgit2#36 from libgit2/checkout
Checkout
2 parents c762299 + 38b3c0d commit 63f7fbe

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

docs/guides/101-samples/index.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,3 +1224,118 @@ git_revwalk_simplify_first_parent(walker);
12241224
[`git_revwalk_sorting`](http://libgit2.github.com/libgit2/#HEAD/group/revwalk/git_revwalk_sorting),
12251225
[`git_revwalk_simplify_first_parent`](http://libgit2.github.com/libgit2/#HEAD/group/revwalk/git_revwalk_simplify_first_parent)
12261226
)
1227+
1228+
1229+
<h2 id="checkout">Checkout</h2>
1230+
1231+
<h3 id="checkout_strategies">Strategies</h3>
1232+
1233+
`git_checkout_options` isn't actually very optional.
1234+
The defaults won't be useful outside of a small number of cases.
1235+
The best example of this is `checkout_strategy`; the default value does nothing to the work tree.
1236+
So if you want your checkout to check files out, choose an appropriate strategy.
1237+
1238+
* `NONE` is the equivalent of a dry run; no files will be checked out.
1239+
* `SAFE` is similar to `git checkout`; unmodified files are updated, and modified files are left alone.
1240+
If a file was present in the old HEAD but is missing, it's considered deleted, and won't be created.
1241+
* `SAFE_CREATE` is similar to `git checkout-index`, or what happens after a clone.
1242+
Unmodified files are updated, and missing files are created, but files with modifications are left alone.
1243+
* `FORCE` is similar to `git checkout --force`; all modifications are overwritten, and all missing files are created.
1244+
1245+
Take a look at the [checkout header](https://github.com/libgit2/libgit2/blob/HEAD/include/git2/checkout.h#files) for extensive explanation of the checkout flags.
1246+
1247+
<h3 id="checkout_simple">Simple</h3>
1248+
1249+
```c
1250+
/* Checkout from HEAD, something like `git checkout HEAD` */
1251+
int error = git_checkout_head(repo, &opts);
1252+
1253+
/* Checkout from the index */
1254+
error = git_checkout_index(repo, &opts);
1255+
1256+
/* Checkout a different tree */
1257+
git_object *treeish = NULL;
1258+
error = git_revparse_single(&treeish, repo, "feature_branch1");
1259+
error = git_checkout_tree(repo, treeish, &opts);
1260+
```
1261+
1262+
(
1263+
[`git_checkout_head`](http://libgit2.github.com/libgit2/#HEAD/group/checkout/git_checkout_head),
1264+
[`git_checkout_index`](http://libgit2.github.com/libgit2/#HEAD/group/checkout/git_checkout_index),
1265+
[`git_checkout_tree`](http://libgit2.github.com/libgit2/#HEAD/group/checkout/git_checkout_tree),
1266+
[`git_revparse_single`](http://libgit2.github.com/libgit2/#HEAD/group/revparse/git_revparse_single)
1267+
)
1268+
1269+
<h3 id="checkout_paths">Paths</h3>
1270+
1271+
This limits the checkout operation to only certain paths, kind of like `git checkout … -- path/to/a path/to/b`.
1272+
1273+
```c
1274+
char *paths[] = { "path/to/a.txt", "path/to/b.txt" };
1275+
opts.paths.strings = paths;
1276+
opts.paths.count = 2;
1277+
int error = git_checkout_head(repo, &opts);
1278+
```
1279+
1280+
([`git_strarray`](http://libgit2.github.com/libgit2/#HEAD/type/git_strarray))
1281+
1282+
<h3 id="checkout_progress">Progress</h3>
1283+
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+
1313+
<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)