Skip to content

Commit ced634a

Browse files
committed
More remotes content
1 parent 06968cd commit ced634a

File tree

1 file changed

+109
-15
lines changed

1 file changed

+109
-15
lines changed

docs/guides/101-samples/index.md

+109-15
Original file line numberDiff line numberDiff line change
@@ -1401,49 +1401,143 @@ int error = git_remote_create_inmemory(&remote, repo,)
14011401
<h3 id="remotes_rename">Renaming</h3>
14021402

14031403
```c
1404+
typedef struct { /**/ } rename_data;
1405+
int problem_cb(const char *problem, void *payload)
1406+
{
1407+
rename_data *d = (rename_data*)payload;
1408+
/* Called when there's a problem renaming the remote. */
1409+
}
1410+
1411+
rename_data d = {0};
1412+
int error = git_remote_rename(remote, "old_origin", problem_cb, &d);
14041413
```
14051414
(
1406-
[``](),
1415+
[`git_remote_rename`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_rename)
14071416
)
14081417
14091418
<h3 id="remotes_properties">Properties</h3>
14101419
14111420
```c
1421+
const char *name = git_remote_name(remote);
1422+
const char *url = git_remote_url(remote);
1423+
const char *pushurl = git_remote_pushurl(remote);
1424+
1425+
/* URLs are mutable, but make sure you save */
1426+
int error = git_remote_set_url(remote, "https://…");
1427+
error = git_remote_set_pushurl(remote, "https://…");
14121428
```
14131429
(
1414-
[``](),
1430+
[`git_remote_name`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_name),
1431+
[`git_remote_url`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_url),
1432+
[`git_remote_pushurl`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_pushurl),
1433+
[`git_remote_set_url`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_set_url),
1434+
[`git_remote_set_pushurl`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_set_pushurl),
14151435
)
14161436

14171437
<h3 id="remotes_refspecs">Refspecs</h3>
14181438

14191439
```c
1420-
```
1421-
(
1422-
[``](),
1423-
)
1440+
/* refspecs are available en masse */
1441+
git_strarray fetch_refspecs = {0};
1442+
int error = git_remote_get_fetch_refspecs(&fetch_refspecs, remote);
1443+
git_strarray push_refspecs = {0};
1444+
error = git_remote_get_push_refspecs(&fetch_refspecs, remote);
14241445

1425-
<h3 id="remotes_modify">Modifying</h3>
1446+
/* … or individually */
1447+
size_t count = git_remote_refspec_count(remote);
1448+
const git_refspec *rs = git_remote_get_refspec(remote, 0);
14261449

1427-
```c
1450+
/* You can add one spec at a time */
1451+
error = git_remote_add_fetch(remote, "…");
1452+
error = git_remote_add_push(remote, "…");
1453+
1454+
/* … or swap out the entire set */
1455+
error = git_remote_set_fetch_refspecs(remote, fetch_refspecs);
1456+
error = git_remote_set_push_refspecs(remote, push_refspecs);
14281457
```
14291458
(
1430-
[``](),
1459+
[`git_remote_get_fetch_refspecs`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_get_fetch_refspecs),
1460+
[`git_remote_get_push_refspecs`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_get_push_refspecs),
1461+
[`git_remote_refspec_count`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_refspec_count),
1462+
[`git_remote_get_refspec`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_get_refspec),
1463+
[`git_remote_add_fetch`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_add_fetch),
1464+
[`git_remote_add_push`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_add_push),
1465+
[`git_remote_set_fetch_refspecs`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_set_fetch_refspecs),
1466+
[`git_remote_set_push_refspecs`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_set_push_refspecs),
14311467
)
14321468
1433-
<h3 id="remotes_network">Network</h3>
1469+
<h3 id="remotes_fetching">Fetching</h3>
14341470
14351471
```c
1472+
/* Open a connection for reading. */
1473+
int error = git_remote_connect(remote, GIT_DIRECTION_FETCH);
1474+
int connected = git_remote_connected(remote);
1475+
1476+
/* List the heads on the remote */
1477+
const git_remote_head **remote_heads = NULL;
1478+
size_t count = 0;
1479+
error = git_remote_ls(&remote_heads, &count, remote);
1480+
for (size_t i=0; i<count; ++i) {
1481+
git_remote_head *head = remote_heads[i];
1482+
/* … */
1483+
}
1484+
1485+
/* Negotiate and download objects */
1486+
error = git_remote_download(remote);
1487+
1488+
/* Update remote refs */
1489+
error = git_remote_update_tips(remote);
1490+
1491+
/* All of the above in one step */
1492+
error = git_remote_fetch(remote);
14361493
```
14371494
(
1438-
[``](),
1495+
[`git_remote_connect`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_connect),
1496+
[`git_remote_connected`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_connected),
1497+
[`git_remote_ls`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_ls),
1498+
[`git_remote_download`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_download),
1499+
[`git_remote_update_tips`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_update_tips),
1500+
[`git_remote_fetch`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_fetch)
14391501
)
14401502

1441-
<h3 id="remotes_fetch">Fetch</h3>
1503+
<h3 id="remotes_callbacks">Callbacks</h3>
1504+
1505+
The network code uses callbacks for reporting progress and getting credentials (when necessary).
1506+
Note that inside a callback is the only place where `git_remote_stop` has any effect.
14421507

14431508
```c
1509+
/* Progress callback */
1510+
typedef struct { /**/ } remote_data;
1511+
int progress_cb(const git_transfer_progress *stats, void *payload)
1512+
{
1513+
remote_data *d = (remote_data*)payload;
1514+
/**/
1515+
}
1516+
1517+
/* Credential callback */
1518+
int credential_cb(git_cred **out,
1519+
const char *url,
1520+
const char *username_from_url,
1521+
unsigned int allowed_types,
1522+
void *payload)
1523+
{
1524+
remote_data *d = (remote_data*)payload;
1525+
/**/
1526+
}
1527+
1528+
remote_data d = {0};
1529+
git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
1530+
callbacks.progress = progress_cb;
1531+
callbacks.credentials = credential_cb;
1532+
callbacks.payload = &d;
1533+
int error = git_remote_set_callbacks(remote, &callbacks);
14441534
```
1535+
1536+
For an example of the credentials callback in action, check out [the network example](https://github.com/libgit2/libgit2/blob/development/examples/network/common.c),
1537+
or the built-in [credential helpers](https://github.com/libgit2/libgit2/blob/development/src/transports/cred_helpers.c).
1538+
14451539
(
1446-
[``](),
1540+
[`git_remote_stop`](http://libgit2.github.com/libgit2/#HEAD/type/git_remote_stop),
1541+
[`git_remote_callbacks`](http://libgit2.github.com/libgit2/#HEAD/type/git_remote_callbacks),
1542+
[`git_remote_set_callbacks`](http://libgit2.github.com/libgit2/#HEAD/group/remote/git_remote_set_callbacks)
14471543
)
1448-
1449-
<h3 id="remotes_callbacks">Callbacks</h3>

0 commit comments

Comments
 (0)