-
Notifications
You must be signed in to change notification settings - Fork 1
/
ls-remote.c
127 lines (100 loc) · 2.33 KB
/
ls-remote.c
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
#include <git2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "common.h"
static void show_refs(git_headarray *refs)
{
int i;
git_remote_head *head;
for(i = 0; i < refs->len; ++i){
char oid[GIT_OID_HEXSZ + 1] = {0};
head = refs->heads[i];
git_oid_fmt(oid, &head->oid);
printf("%s\t%s\n", oid, head->name);
}
}
#if 0
int use_transport(char *url)
{
int error;
git_transport *t;
git_headarray refs;
error = git_transport_new(&t, url);
if (error < GIT_SUCCESS) {
fprintf(stderr, "Failed to create transport: %s", git_lasterror());
return error;
}
error = git_transport_connect(t, GIT_DIR_FETCH);
if (error < GIT_SUCCESS) {
goto cleanup;
}
error = git_transport_ls(t, &refs);
if (error < GIT_SUCCESS) {
return error;
goto cleanup;
}
show_refs(&refs);
cleanup:
git_transport_close(t);
git_transport_free(t);
return error;
}
#endif
int use_unnamed(git_repository *repo, const char *url)
{
git_remote *remote = NULL;
git_headarray refs;
int error;
error = git_remote_new(&remote, repo, url);
if (error < GIT_SUCCESS)
goto cleanup;
puts("Connecting...");
error = git_remote_connect(remote, GIT_DIR_FETCH);
if (error < GIT_SUCCESS)
goto cleanup;
error = git_remote_ls(remote, &refs);
if (error < GIT_SUCCESS)
goto cleanup;
show_refs(&refs);
cleanup:
git_remote_free(remote);
return error;
}
int use_remote(git_repository *repo, char *name)
{
git_remote *remote = NULL;
git_config *cfg = NULL;
git_headarray refs;
int error;
error = git_repository_config(&cfg, repo, NULL, NULL);
if (error < GIT_SUCCESS)
return error;
error = git_remote_get(&remote, cfg, name);
if (error < GIT_SUCCESS)
goto cleanup;
error = git_remote_connect(remote, GIT_DIR_FETCH);
if (error < GIT_SUCCESS)
goto cleanup;
error = git_remote_ls(remote, &refs);
if (error < GIT_SUCCESS)
goto cleanup;
show_refs(&refs);
cleanup:
git_config_free(cfg);
git_remote_free(remote);
return error;
}
int ls_remote(git_repository *repo, int argc, char **argv)
{
git_headarray heads;
git_remote_head *head;
int error, i;
/* If there's a ':' in the name, assume it's an URL */
if (strchr(argv[1], ':') != NULL) {
error = use_unnamed(repo, argv[1]);
} else {
error = use_remote(repo, argv[1]);
}
return error;
}