forked from AndreyG/libgit2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote.cpp
More file actions
234 lines (194 loc) · 5.47 KB
/
Copy pathremote.cpp
File metadata and controls
234 lines (194 loc) · 5.47 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
/*
* libgit2 "remote" example - shows how to modify remotes for a repo
*
* 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 "git2/errors.h"
#include <string.h>
/**
* This is a sample program that is similar to "git remote". See the
* documentation for that (try "git help remote") to understand what this
* program is emulating.
*
* This demonstrates using the libgit2 APIs to modify remotes of a repository.
*/
namespace {
[[noreturn]] void usage(const char *msg, const char *arg = nullptr)
{
fputs("usage: remote add <name> <url>\n", stderr);
fputs(" remote remove <name>\n", stderr);
fputs(" remote rename <old> <new>\n", stderr);
fputs(" remote set-url [--push] <name> <newurl>\n", stderr);
fputs(" remote show [-v|--verbose]\n", stderr);
if (msg && !arg)
fprintf(stderr, "\n%s\n", msg);
else if (msg && arg)
fprintf(stderr, "\n%s: %s\n", msg, arg);
exit(1);
}
[[noreturn]] void report_error(const char *message)
{
const git_error *lg2err = git_error_last();
const char *lg2msg = "", *lg2spacer = "";
if (lg2err && lg2err->message)
{
lg2msg = lg2err->message;
lg2spacer = " - ";
}
fprintf(stderr, "%s %s%s\n", message, lg2spacer, lg2msg);
exit(1);
}
enum class Subcmd {
add,
remove,
rename,
seturl,
show,
};
struct Options {
Subcmd cmd;
/* for command-specific args */
int argc;
char **argv;
Options(int argc, char **argv)
: argc(argc - 2) /* executable and subcommand are removed */
, argv(argv + 2)
{
char *arg = argv[1];
if (argc < 2)
usage("no command specified");
if (!strcmp(arg, "add")) {
cmd = Subcmd::add;
} else if (!strcmp(arg, "remove")) {
cmd = Subcmd::remove;
} else if (!strcmp(arg, "rename")) {
cmd = Subcmd::rename;
} else if (!strcmp(arg, "set-url")) {
cmd = Subcmd::seturl;
} else if (!strcmp(arg, "show")) {
cmd = Subcmd::show;
} else {
usage("command is not valid", arg);
}
}
};
using git::Repository;
void cmd_add(Repository & repo, Options const & o)
{
if (o.argc != 2)
usage("you need to specify a name and URL");
auto name = o.argv[0], url = o.argv[1];
repo.create_remote(name, url);
}
void cmd_remove(Repository & repo, Options const & o)
{
if (o.argc != 1)
usage("you need to specify a name");
auto name = o.argv[0];
repo.delete_remote(name);
}
void cmd_rename(Repository & repo, Options const & o)
{
if (o.argc != 2)
usage("you need to specify old and new remote name");
auto old_name = o.argv[0];
auto new_name = o.argv[1];
if (auto problems = repo.rename_remote(old_name, new_name))
{
for (size_t i = 0, n = problems->count(); i != n; ++i)
puts((*problems)[i]);
}
}
void cmd_seturl(Repository & repo, Options const & o)
{
bool push = false;
char const * name = nullptr, *url = nullptr;
for (auto i = 0; i != o.argc; ++i)
{
char const * arg = o.argv[i];
if (!strcmp(arg, "--push")) {
push = 1;
} else if (arg[0] != '-' && !name) {
name = arg;
} else if (arg[0] != '-' && !url) {
url = arg;
} else {
usage("invalid argument to set-url", arg);
}
}
if (!name || !url)
usage("you need to specify remote and the new URL");
if (push)
repo.set_pushurl(name, url);
else
repo.set_url(name, url);
}
void cmd_show(Repository const & repo, Options const & o)
{
bool verbose = false;
for (int i = 0; i != o.argc; ++i)
{
auto arg = o.argv[i];
if (!strcmp(arg, "-v") ||
!strcmp(arg, "--verbose"))
{
verbose = true;
}
}
auto remotes = repo.remotes();
for (size_t i = 0, n = remotes.count(); i != n; ++i)
{
auto name = remotes[i];
if (!verbose) {
puts(name);
continue;
}
auto remote = repo.remote(name);
auto fetch = remote.url();
if (fetch)
printf("%s\t%s (fetch)\n", name, fetch);
auto push = remote.pushurl();
/* use fetch URL if no distinct push URL has been set */
push = push ? push : fetch;
if (push)
printf("%s\t%s (push)\n", name, push);
}
}
}
int main(int argc, char *argv[])
{
Options opt(argc, argv);
auto_git_initializer;
auto path_to_repo = git::Repository::discover(".");
if (!path_to_repo)
report_error("Could not find repository");
Repository repo(*path_to_repo);
switch (opt.cmd)
{
case Subcmd::add:
cmd_add(repo, opt);
break;
case Subcmd::remove:
cmd_remove(repo, opt);
break;
case Subcmd::rename:
cmd_rename(repo, opt);
break;
case Subcmd::seturl:
cmd_seturl(repo, opt);
break;
case Subcmd::show:
cmd_show(repo, opt);
break;
}
}