1 module ls_remote; 2 3 import git.c; 4 5 import std.algorithm; 6 import std.stdio; 7 import std.string; 8 9 extern(C) int show_ref__cb(git_remote_head* head, void* payload) 10 { 11 char[GIT_OID_HEXSZ + 1] oid = '\0'; 12 git_oid_fmt(oid.ptr, &head.oid); 13 printf("%s\t%s\n", oid.ptr, head.name); 14 return 0; 15 } 16 17 int use_unnamed(git_repository* repo, const char* url) 18 { 19 git_remote* remote = null; 20 int error; 21 22 // Create an instance of a remote from the URL. The transport to use 23 // is detected from the URL 24 error = git_remote_create_inmemory(&remote, repo, null, url); 25 26 if (error < 0) 27 goto cleanup; 28 29 // When connecting, the underlying code needs to know wether we 30 // want to push or fetch 31 error = git_remote_connect(remote, git_direction.GIT_DIRECTION_FETCH); 32 33 if (error < 0) 34 goto cleanup; 35 36 // With git_remote_ls we can retrieve the advertised heads 37 error = git_remote_ls(remote, &show_ref__cb, null); 38 39 cleanup: 40 git_remote_free(remote); 41 return error; 42 } 43 44 int use_remote(git_repository* repo, const(char)* name) 45 { 46 git_remote* remote = null; 47 int error; 48 49 // Find the remote by name 50 error = git_remote_load(&remote, repo, name); 51 52 if (error < 0) 53 goto cleanup; 54 55 error = git_remote_connect(remote, git_direction.GIT_DIRECTION_FETCH); 56 57 if (error < 0) 58 goto cleanup; 59 60 error = git_remote_ls(remote, &show_ref__cb, null); 61 62 cleanup: 63 git_remote_free(remote); 64 return error; 65 } 66 67 // This gets called to do the work. The remote can be given either as 68 // the name of a configured remote or an URL. 69 int run_ls_remote(git_repository* repo, int argc, string[] argv) 70 { 71 if (argc < 2) 72 { 73 writeln("I need a remote.\n"); 74 return -1; 75 } 76 77 int error; 78 int i; 79 80 /* If there's a ':' in the name, assume it's a URL */ 81 if (argv[1].canFind(":")) 82 { 83 error = use_unnamed(repo, argv[1].toStringz); 84 } 85 else 86 { 87 error = use_remote(repo, argv[1].toStringz); 88 } 89 90 return error; 91 }