1 module libgit2.example.ls_remote;
2 
3 
4 private static import core.stdc.stdio;
5 private static import core.stdc.stdlib;
6 private static import libgit2.example.common;
7 private static import libgit2.net;
8 private static import libgit2.oid;
9 private static import libgit2.remote;
10 private static import libgit2.types;
11 
12 nothrow @nogc
13 private int use_remote(libgit2.types.git_repository* repo, char* name)
14 
15 	in
16 	{
17 	}
18 
19 	do
20 	{
21 		libgit2.remote.git_remote_callbacks callbacks = libgit2.remote.GIT_REMOTE_CALLBACKS_INIT();
22 
23 		/* Find the remote by name */
24 		libgit2.types.git_remote* remote = null;
25 
26 		scope (exit) {
27 			libgit2.remote.git_remote_free(remote);
28 		}
29 
30 		int error = libgit2.remote.git_remote_lookup(&remote, repo, name);
31 
32 		if (error < 0) {
33 			error = libgit2.remote.git_remote_create_anonymous(&remote, repo, name);
34 
35 			if (error < 0) {
36 				return error;
37 			}
38 		}
39 
40 		/**
41 		 * Connect to the remote and call the printing function for
42 		 * each of the remote references.
43 		 */
44 		callbacks.credentials = &libgit2.example.common.cred_acquire_cb;
45 
46 		error = libgit2.remote.git_remote_connect(remote, libgit2.net.git_direction.GIT_DIRECTION_FETCH, &callbacks, null, null);
47 
48 		if (error < 0) {
49 			return error;
50 		}
51 
52 		/**
53 		 * Get the list of references on the remote and print out
54 		 * their name next to what they point to.
55 		 */
56 		size_t refs_len;
57 
58 		const (libgit2.net.git_remote_head)** refs;
59 
60 		if (libgit2.remote.git_remote_ls(&refs, &refs_len, remote) < 0) {
61 			return error;
62 		}
63 
64 		for (size_t i = 0; i < refs_len; i++) {
65 			char[libgit2.oid.GIT_OID_SHA1_HEXSIZE + 1] oid  = '\0';
66 			libgit2.oid.git_oid_fmt((&oid[0]), &refs[i].oid);
67 			core.stdc.stdio.printf("%s\t%s\n", (&oid[0]), refs[i].name);
68 		}
69 
70 		return error;
71 	}
72 
73 /**
74  * Entry point for this command
75  */
76 extern (C)
77 nothrow @nogc
78 public int lg2_ls_remote(libgit2.types.git_repository* repo, int argc, char** argv)
79 
80 	in
81 	{
82 	}
83 
84 	do
85 	{
86 		if (argc < 2) {
87 			core.stdc.stdio.fprintf(core.stdc.stdio.stderr, "usage: %s ls-remote <remote>\n", argv[-1]);
88 
89 			return core.stdc.stdlib.EXIT_FAILURE;
90 		}
91 
92 		int error = .use_remote(repo, argv[1]);
93 
94 		return error;
95 	}