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