1 /* 2 * libgit2 "stash" example - shows how to use the stash API 3 * 4 * Written by the libgit2 contributors 5 * 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. 9 * 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/>. 13 */ 14 module libgit2_d.example.stash; 15 16 17 private static import core.stdc.stdio; 18 private static import core.stdc.stdlib; 19 private static import core.stdc.string; 20 private static import libgit2_d.commit; 21 private static import libgit2_d.example.common; 22 private static import libgit2_d.oid; 23 private static import libgit2_d.signature; 24 private static import libgit2_d.stash; 25 private static import libgit2_d.types; 26 27 package: 28 29 enum subcmd 30 { 31 SUBCMD_APPLY, 32 SUBCMD_LIST, 33 SUBCMD_POP, 34 SUBCMD_PUSH, 35 } 36 37 struct opts 38 { 39 .subcmd cmd; 40 int argc; 41 char** argv; 42 } 43 44 nothrow @nogc 45 private void usage(const (char)* fmt, const char* msg = null) 46 47 in 48 { 49 } 50 51 do 52 { 53 core.stdc.stdio.fputs("usage: git stash list\n", core.stdc.stdio.stderr); 54 core.stdc.stdio.fputs(" or: git stash ( pop | apply )\n", core.stdc.stdio.stderr); 55 core.stdc.stdio.fputs(" or: git stash [push]\n", core.stdc.stdio.stderr); 56 core.stdc.stdio.fputs("\n", core.stdc.stdio.stderr); 57 58 if (msg == null) { 59 core.stdc.stdio.fprintf(core.stdc.stdio.stderr, fmt); 60 } else { 61 core.stdc.stdio.fprintf(core.stdc.stdio.stderr, fmt, msg); 62 } 63 64 core.stdc.stdlib.exit(1); 65 } 66 67 nothrow @nogc 68 private void parse_subcommand(.opts* opts, int argc, char** argv) 69 70 in 71 { 72 } 73 74 do 75 { 76 const char* arg = (argc < 2) ? ("push") : (argv[1]); 77 .subcmd cmd; 78 79 if (!core.stdc..string.strcmp(arg, "apply")) { 80 cmd = .subcmd.SUBCMD_APPLY; 81 } else if (!core.stdc..string.strcmp(arg, "list")) { 82 cmd = .subcmd.SUBCMD_LIST; 83 } else if (!core.stdc..string.strcmp(arg, "pop")) { 84 cmd = .subcmd.SUBCMD_POP; 85 } else if (!core.stdc..string.strcmp(arg, "push")) { 86 cmd = .subcmd.SUBCMD_PUSH; 87 } else { 88 .usage("invalid command %s", arg); 89 90 return; 91 } 92 93 opts.cmd = cmd; 94 opts.argc = (argc < 2) ? (argc - 1) : (argc - 2); 95 opts.argv = argv; 96 } 97 98 nothrow @nogc 99 private int cmd_apply(libgit2_d.types.git_repository* repo, .opts* opts) 100 101 in 102 { 103 } 104 105 do 106 { 107 if (opts.argc) { 108 .usage("apply does not accept any parameters"); 109 } 110 111 libgit2_d.example.common.check_lg2(libgit2_d.stash.git_stash_apply(repo, 0, null), "Unable to apply stash", null); 112 113 return 0; 114 } 115 116 extern (C) 117 nothrow @nogc 118 private int list_stash_cb(size_t index, const (char)* message, const (libgit2_d.oid.git_oid)* stash_id, void* payload) 119 120 in 121 { 122 } 123 124 do 125 { 126 //cast(void)(stash_id); 127 //cast(void)(payload); 128 core.stdc.stdio.printf("stash@{%" ~ libgit2_d.example.common.PRIuZ ~ "}: %s\n", index, message); 129 130 return 0; 131 } 132 133 nothrow @nogc 134 private int cmd_list(libgit2_d.types.git_repository* repo, .opts* opts) 135 136 in 137 { 138 } 139 140 do 141 { 142 if (opts.argc) { 143 .usage("list does not accept any parameters"); 144 } 145 146 libgit2_d.example.common.check_lg2(libgit2_d.stash.git_stash_foreach(repo, &.list_stash_cb, null), "Unable to list stashes", null); 147 148 return 0; 149 } 150 151 nothrow @nogc 152 private int cmd_push(libgit2_d.types.git_repository* repo, .opts* opts) 153 154 in 155 { 156 } 157 158 do 159 { 160 if (opts.argc) { 161 .usage("push does not accept any parameters"); 162 } 163 164 libgit2_d.types.git_signature* signature; 165 libgit2_d.example.common.check_lg2(libgit2_d.signature.git_signature_default(&signature, repo), "Unable to get signature", null); 166 167 libgit2_d.oid.git_oid stashid; 168 libgit2_d.example.common.check_lg2(libgit2_d.stash.git_stash_save(&stashid, repo, signature, null, libgit2_d.stash.git_stash_flags.GIT_STASH_DEFAULT), "Unable to save stash", null); 169 170 libgit2_d.types.git_commit* stash; 171 libgit2_d.example.common.check_lg2(libgit2_d.commit.git_commit_lookup(&stash, repo, &stashid), "Unable to lookup stash commit", null); 172 173 core.stdc.stdio.printf("Saved working directory %s\n", libgit2_d.commit.git_commit_summary(stash)); 174 175 libgit2_d.signature.git_signature_free(signature); 176 libgit2_d.commit.git_commit_free(stash); 177 178 return 0; 179 } 180 181 nothrow @nogc 182 private int cmd_pop(libgit2_d.types.git_repository* repo, .opts* opts) 183 184 in 185 { 186 } 187 188 do 189 { 190 if (opts.argc) { 191 .usage("pop does not accept any parameters"); 192 } 193 194 libgit2_d.example.common.check_lg2(libgit2_d.stash.git_stash_pop(repo, 0, null), "Unable to pop stash", null); 195 196 core.stdc.stdio.printf("Dropped refs/stash@{0}\n"); 197 198 return 0; 199 } 200 201 extern (C) 202 nothrow @nogc 203 //int lg2_stash(libgit2_d.types.git_repository* repo, int argc, char*[] argv) 204 public int lg2_stash(libgit2_d.types.git_repository* repo, int argc, char** argv) 205 206 in 207 { 208 } 209 210 do 211 { 212 .opts opts = .opts.init; 213 214 .parse_subcommand(&opts, argc, argv); 215 216 switch (opts.cmd) { 217 case .subcmd.SUBCMD_APPLY: 218 return .cmd_apply(repo, &opts); 219 220 case .subcmd.SUBCMD_LIST: 221 return .cmd_list(repo, &opts); 222 223 case .subcmd.SUBCMD_PUSH: 224 return .cmd_push(repo, &opts); 225 226 case .subcmd.SUBCMD_POP: 227 return .cmd_pop(repo, &opts); 228 229 default: 230 break; 231 } 232 233 return -1; 234 }