]>
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 opts
*o
);
42 static int cmd_remove(git_repository
*repo
, struct opts
*o
);
43 static int cmd_rename(git_repository
*repo
, struct opts
*o
);
44 static int cmd_seturl(git_repository
*repo
, struct opts
*o
);
45 static int cmd_show(git_repository
*repo
, struct opts
*o
);
47 static void parse_subcmd(
48 struct opts
*opt
, int argc
, char **argv
);
49 static void usage(const char *msg
, const char *arg
);
51 int main(int argc
, char *argv
[])
54 struct opts opt
= {0};
55 git_buf buf
= GIT_BUF_INIT_CONST(NULL
, 0);
56 git_repository
*repo
= NULL
;
58 parse_subcmd(&opt
, argc
, argv
);
62 check_lg2(git_repository_discover(&buf
, ".", 0, NULL
),
63 "Could not find repository", NULL
);
65 check_lg2(git_repository_open(&repo
, buf
.ptr
),
66 "Could not open repository", NULL
);
72 retval
= cmd_add(repo
, &opt
);
75 retval
= cmd_remove(repo
, &opt
);
78 retval
= cmd_rename(repo
, &opt
);
81 retval
= cmd_seturl(repo
, &opt
);
84 retval
= cmd_show(repo
, &opt
);
88 git_libgit2_shutdown();
93 static int cmd_add(git_repository
*repo
, struct opts
*o
)
96 git_remote
*remote
= {0};
99 usage("you need to specify a name and URL", NULL
);
104 check_lg2(git_remote_create(&remote
, repo
, name
, url
),
105 "could not create remote", NULL
);
110 static int cmd_remove(git_repository
*repo
, struct opts
*o
)
115 usage("you need to specify a name", NULL
);
119 check_lg2(git_remote_delete(repo
, name
),
120 "could not delete remote", name
);
125 static int cmd_rename(git_repository
*repo
, struct opts
*o
)
129 git_strarray problems
= {0};
132 usage("you need to specify old and new remote name", NULL
);
137 retval
= git_remote_rename(&problems
, repo
, old
, new);
141 for (i
= 0; i
< (int) problems
.count
; i
++) {
142 puts(problems
.strings
[0]);
145 git_strarray_free(&problems
);
150 static int cmd_seturl(git_repository
*repo
, struct opts
*o
)
152 int i
, retval
, push
= 0;
153 char *name
= NULL
, *url
= NULL
;
155 for (i
= 0; i
< o
->argc
; i
++) {
156 char *arg
= o
->argv
[i
];
158 if (!strcmp(arg
, "--push")) {
160 } else if (arg
[0] != '-' && name
== NULL
) {
162 } else if (arg
[0] != '-' && url
== NULL
) {
165 usage("invalid argument to set-url", arg
);
169 if (name
== NULL
|| url
== NULL
)
170 usage("you need to specify remote and the new URL", NULL
);
173 retval
= git_remote_set_pushurl(repo
, name
, url
);
175 retval
= git_remote_set_url(repo
, name
, url
);
177 check_lg2(retval
, "could not set URL", url
);
182 static int cmd_show(git_repository
*repo
, struct opts
*o
)
185 const char *arg
, *name
, *fetch
, *push
;
187 git_strarray remotes
= {0};
188 git_remote
*remote
= {0};
190 for (i
= 0; i
< o
->argc
; i
++) {
193 if (!strcmp(arg
, "-v") || !strcmp(arg
, "--verbose")) {
198 check_lg2(git_remote_list(&remotes
, repo
),
199 "could not retrieve remotes", NULL
);
201 for (i
= 0; i
< (int) remotes
.count
; i
++) {
202 name
= remotes
.strings
[i
];
208 check_lg2(git_remote_lookup(&remote
, repo
, name
),
209 "could not look up remote", name
);
211 fetch
= git_remote_url(remote
);
213 printf("%s\t%s (fetch)\n", name
, fetch
);
214 push
= git_remote_pushurl(remote
);
215 /* use fetch URL if no distinct push URL has been set */
216 push
= push
? push
: fetch
;
218 printf("%s\t%s (push)\n", name
, push
);
220 git_remote_free(remote
);
223 git_strarray_free(&remotes
);
228 static void parse_subcmd(
229 struct opts
*opt
, int argc
, char **argv
)
235 usage("no command specified", NULL
);
237 if (!strcmp(arg
, "add")) {
239 } else if (!strcmp(arg
, "remove")) {
241 } else if (!strcmp(arg
, "rename")) {
243 } else if (!strcmp(arg
, "set-url")) {
245 } else if (!strcmp(arg
, "show")) {
248 usage("command is not valid", arg
);
252 opt
->argc
= argc
- 2; /* executable and subcommand are removed */
253 opt
->argv
= argv
+ 2;
256 static void usage(const char *msg
, const char *arg
)
258 fputs("usage: remote add <name> <url>\n", stderr
);
259 fputs(" remote remove <name>\n", stderr
);
260 fputs(" remote rename <old> <new>\n", stderr
);
261 fputs(" remote set-url [--push] <name> <newurl>\n", stderr
);
262 fputs(" remote show [-v|--verbose]\n", stderr
);
265 fprintf(stderr
, "\n%s\n", msg
);
267 fprintf(stderr
, "\n%s: %s\n", msg
, arg
);