]>
git.proxmox.com Git - libgit2.git/blob - examples/remote.c
2 * libgit2 "remote" example - shows how to modify remotes for a repo
4 * Written by the libgit2 contributors
6 * To the extent possible under law, the author(s) have dedicated all copyright
7 * and related and neighboring rights to this software to the public domain
8 * worldwide. This software is distributed without any warranty.
10 * You should have received a copy of the CC0 Public Domain Dedication along
11 * with this software. If not, see
12 * <http://creativecommons.org/publicdomain/zero/1.0/>.
18 * This is a sample program that is similar to "git remote". See the
19 * documentation for that (try "git help remote") to understand what this
20 * program is emulating.
22 * This demonstrates using the libgit2 APIs to modify remotes of a repository.
36 /* for command-specific args */
41 static int cmd_add(git_repository
*repo
, struct remote_opts
*o
);
42 static int cmd_remove(git_repository
*repo
, struct remote_opts
*o
);
43 static int cmd_rename(git_repository
*repo
, struct remote_opts
*o
);
44 static int cmd_seturl(git_repository
*repo
, struct remote_opts
*o
);
45 static int cmd_show(git_repository
*repo
, struct remote_opts
*o
);
47 static void parse_subcmd(
48 struct remote_opts
*opt
, int argc
, char **argv
);
49 static void usage(const char *msg
, const char *arg
);
51 int lg2_remote(git_repository
*repo
, int argc
, char *argv
[])
54 struct remote_opts opt
= {0};
56 parse_subcmd(&opt
, argc
, argv
);
61 retval
= cmd_add(repo
, &opt
);
64 retval
= cmd_remove(repo
, &opt
);
67 retval
= cmd_rename(repo
, &opt
);
70 retval
= cmd_seturl(repo
, &opt
);
73 retval
= cmd_show(repo
, &opt
);
80 static int cmd_add(git_repository
*repo
, struct remote_opts
*o
)
83 git_remote
*remote
= {0};
86 usage("you need to specify a name and URL", NULL
);
91 check_lg2(git_remote_create(&remote
, repo
, name
, url
),
92 "could not create remote", NULL
);
97 static int cmd_remove(git_repository
*repo
, struct remote_opts
*o
)
102 usage("you need to specify a name", NULL
);
106 check_lg2(git_remote_delete(repo
, name
),
107 "could not delete remote", name
);
112 static int cmd_rename(git_repository
*repo
, struct remote_opts
*o
)
116 git_strarray problems
= {0};
119 usage("you need to specify old and new remote name", NULL
);
124 retval
= git_remote_rename(&problems
, repo
, old
, new);
128 for (i
= 0; i
< (int) problems
.count
; i
++) {
129 puts(problems
.strings
[0]);
132 git_strarray_free(&problems
);
137 static int cmd_seturl(git_repository
*repo
, struct remote_opts
*o
)
139 int i
, retval
, push
= 0;
140 char *name
= NULL
, *url
= NULL
;
142 for (i
= 0; i
< o
->argc
; i
++) {
143 char *arg
= o
->argv
[i
];
145 if (!strcmp(arg
, "--push")) {
147 } else if (arg
[0] != '-' && name
== NULL
) {
149 } else if (arg
[0] != '-' && url
== NULL
) {
152 usage("invalid argument to set-url", arg
);
156 if (name
== NULL
|| url
== NULL
)
157 usage("you need to specify remote and the new URL", NULL
);
160 retval
= git_remote_set_pushurl(repo
, name
, url
);
162 retval
= git_remote_set_url(repo
, name
, url
);
164 check_lg2(retval
, "could not set URL", url
);
169 static int cmd_show(git_repository
*repo
, struct remote_opts
*o
)
172 const char *arg
, *name
, *fetch
, *push
;
174 git_strarray remotes
= {0};
175 git_remote
*remote
= {0};
177 for (i
= 0; i
< o
->argc
; i
++) {
180 if (!strcmp(arg
, "-v") || !strcmp(arg
, "--verbose")) {
185 check_lg2(git_remote_list(&remotes
, repo
),
186 "could not retrieve remotes", NULL
);
188 for (i
= 0; i
< (int) remotes
.count
; i
++) {
189 name
= remotes
.strings
[i
];
195 check_lg2(git_remote_lookup(&remote
, repo
, name
),
196 "could not look up remote", name
);
198 fetch
= git_remote_url(remote
);
200 printf("%s\t%s (fetch)\n", name
, fetch
);
201 push
= git_remote_pushurl(remote
);
202 /* use fetch URL if no distinct push URL has been set */
203 push
= push
? push
: fetch
;
205 printf("%s\t%s (push)\n", name
, push
);
207 git_remote_free(remote
);
210 git_strarray_free(&remotes
);
215 static void parse_subcmd(
216 struct remote_opts
*opt
, int argc
, char **argv
)
222 usage("no command specified", NULL
);
224 if (!strcmp(arg
, "add")) {
226 } else if (!strcmp(arg
, "remove")) {
228 } else if (!strcmp(arg
, "rename")) {
230 } else if (!strcmp(arg
, "set-url")) {
232 } else if (!strcmp(arg
, "show")) {
235 usage("command is not valid", arg
);
239 opt
->argc
= argc
- 2; /* executable and subcommand are removed */
240 opt
->argv
= argv
+ 2;
243 static void usage(const char *msg
, const char *arg
)
245 fputs("usage: remote add <name> <url>\n", stderr
);
246 fputs(" remote remove <name>\n", stderr
);
247 fputs(" remote rename <old> <new>\n", stderr
);
248 fputs(" remote set-url [--push] <name> <newurl>\n", stderr
);
249 fputs(" remote show [-v|--verbose]\n", stderr
);
252 fprintf(stderr
, "\n%s\n", msg
);
254 fprintf(stderr
, "\n%s: %s\n", msg
, arg
);