|
| 1 | +/* |
| 2 | + * libgit2 "tag" example - shows how to list, create and delete tags |
| 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 | + |
| 15 | +#include "common.h" |
| 16 | + |
| 17 | +/** |
| 18 | + * The following example partially reimplements the `git tag` command |
| 19 | + * and some of its options. |
| 20 | + * |
| 21 | + * These commands should work: |
| 22 | +
|
| 23 | + * - Tag name listing (`tag`) |
| 24 | + * - Filtered tag listing with messages (`tag -n3 -l "v0.1*"`) |
| 25 | + * - Lightweight tag creation (`tag test v0.18.0`) |
| 26 | + * - Tag creation (`tag -a -m "Test message" test v0.18.0`) |
| 27 | + * - Tag deletion (`tag -d test`) |
| 28 | + * |
| 29 | + * The command line parsing logic is simplified and doesn't handle |
| 30 | + * all of the use cases. |
| 31 | + */ |
| 32 | + |
| 33 | +/** tag_options represents the parsed command line options */ |
| 34 | +typedef struct { |
| 35 | + const char *message; |
| 36 | + const char *pattern; |
| 37 | + const char *tag_name; |
| 38 | + const char *target; |
| 39 | + int num_lines; |
| 40 | + int force; |
| 41 | +} tag_options; |
| 42 | + |
| 43 | +/** tag_state represents the current program state for dragging around */ |
| 44 | +typedef struct { |
| 45 | + git_repository *repo; |
| 46 | + tag_options *opts; |
| 47 | +} tag_state; |
| 48 | + |
| 49 | +/** An action to execute based on the command line arguments */ |
| 50 | +typedef void (*tag_action)(tag_state *state); |
| 51 | +typedef struct args_info args_info; |
| 52 | + |
| 53 | +static void check(int result, const char *message) |
| 54 | +{ |
| 55 | + if (result) fatal(message, NULL); |
| 56 | +} |
| 57 | + |
| 58 | +/** Tag listing: Print individual message lines */ |
| 59 | +static void print_list_lines(const char *message, const tag_state *state) |
| 60 | +{ |
| 61 | + const char *msg = message; |
| 62 | + int num = state->opts->num_lines - 1; |
| 63 | + |
| 64 | + if (!msg) return; |
| 65 | + |
| 66 | + /** first line - headline */ |
| 67 | + while(*msg && *msg != '\n') printf("%c", *msg++); |
| 68 | + |
| 69 | + /** skip over new lines */ |
| 70 | + while(*msg && *msg == '\n') msg++; |
| 71 | + |
| 72 | + printf("\n"); |
| 73 | + |
| 74 | + /** print just headline? */ |
| 75 | + if (num == 0) return; |
| 76 | + if (*msg && msg[1]) printf("\n"); |
| 77 | + |
| 78 | + /** print individual commit/tag lines */ |
| 79 | + while (*msg && num-- >= 2) { |
| 80 | + printf(" "); |
| 81 | + |
| 82 | + while (*msg && *msg != '\n') printf("%c", *msg++); |
| 83 | + |
| 84 | + /** handle consecutive new lines */ |
| 85 | + if (*msg && *msg == '\n' && msg[1] == '\n') { |
| 86 | + num--; |
| 87 | + printf("\n"); |
| 88 | + } |
| 89 | + while(*msg && *msg == '\n') msg++; |
| 90 | + |
| 91 | + printf("\n"); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/** Tag listing: Print an actual tag object */ |
| 96 | +static void print_tag(git_tag *tag, const tag_state *state) |
| 97 | +{ |
| 98 | + printf("%-16s", git_tag_name(tag)); |
| 99 | + |
| 100 | + if (state->opts->num_lines) { |
| 101 | + const char *msg = git_tag_message(tag); |
| 102 | + print_list_lines(msg, state); |
| 103 | + } else { |
| 104 | + printf("\n"); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/** Tag listing: Print a commit (target of a lightweight tag) */ |
| 109 | +static void print_commit(git_commit *commit, const char *name, |
| 110 | + const tag_state *state) |
| 111 | +{ |
| 112 | + printf("%-16s", name); |
| 113 | + |
| 114 | + if (state->opts->num_lines) { |
| 115 | + const char *msg = git_commit_message(commit); |
| 116 | + print_list_lines(msg, state); |
| 117 | + } else { |
| 118 | + printf("\n"); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/** Tag listing: Fallback, should not happen */ |
| 123 | +static void print_name(const char *name) |
| 124 | +{ |
| 125 | + printf("%s\n", name); |
| 126 | +} |
| 127 | + |
| 128 | +/** Tag listing: Lookup tags based on ref name and dispatch to print */ |
| 129 | +static int each_tag(const char *name, tag_state *state) |
| 130 | +{ |
| 131 | + git_repository *repo = state->repo; |
| 132 | + git_object *obj; |
| 133 | + |
| 134 | + check_lg2(git_revparse_single(&obj, repo, name), |
| 135 | + "Failed to lookup rev", name); |
| 136 | + |
| 137 | + switch (git_object_type(obj)) { |
| 138 | + case GIT_OBJ_TAG: |
| 139 | + print_tag((git_tag *) obj, state); |
| 140 | + break; |
| 141 | + case GIT_OBJ_COMMIT: |
| 142 | + print_commit((git_commit *) obj, name, state); |
| 143 | + break; |
| 144 | + default: |
| 145 | + print_name(name); |
| 146 | + } |
| 147 | + |
| 148 | + git_object_free(obj); |
| 149 | + return 0; |
| 150 | +} |
| 151 | + |
| 152 | +static void action_list_tags(tag_state *state) |
| 153 | +{ |
| 154 | + const char *pattern = state->opts->pattern; |
| 155 | + git_strarray tag_names = {0}; |
| 156 | + size_t i; |
| 157 | + |
| 158 | + check_lg2(git_tag_list_match(&tag_names, pattern ? pattern : "*", state->repo), |
| 159 | + "Unable to get list of tags", NULL); |
| 160 | + |
| 161 | + for(i = 0; i < tag_names.count; i++) { |
| 162 | + each_tag(tag_names.strings[i], state); |
| 163 | + } |
| 164 | + |
| 165 | + git_strarray_free(&tag_names); |
| 166 | +} |
| 167 | + |
| 168 | +static void action_delete_tag(tag_state *state) |
| 169 | +{ |
| 170 | + tag_options *opts = state->opts; |
| 171 | + git_object *obj; |
| 172 | + char oid[GIT_OID_HEXSZ + 1]; |
| 173 | + |
| 174 | + check(!opts->tag_name, "Name required"); |
| 175 | + |
| 176 | + check_lg2(git_revparse_single(&obj, state->repo, opts->tag_name), |
| 177 | + "Failed to lookup rev", opts->tag_name); |
| 178 | + |
| 179 | + check_lg2(git_tag_delete(state->repo, opts->tag_name), |
| 180 | + "Unable to delete tag", opts->tag_name); |
| 181 | + |
| 182 | + git_oid_tostr(oid, sizeof(oid), git_object_id(obj)); |
| 183 | + |
| 184 | + printf("Deleted tag '%s' (was %s)\n", opts->tag_name, oid); |
| 185 | + |
| 186 | + git_object_free(obj); |
| 187 | +} |
| 188 | + |
| 189 | +static void action_create_lighweight_tag(tag_state *state) |
| 190 | +{ |
| 191 | + git_repository *repo = state->repo; |
| 192 | + tag_options *opts = state->opts; |
| 193 | + git_oid oid; |
| 194 | + git_object *target; |
| 195 | + |
| 196 | + check(!opts->tag_name, "Name required"); |
| 197 | + |
| 198 | + if (!opts->target) opts->target = "HEAD"; |
| 199 | + |
| 200 | + check(!opts->target, "Target required"); |
| 201 | + |
| 202 | + check_lg2(git_revparse_single(&target, repo, opts->target), |
| 203 | + "Unable to resolve spec", opts->target); |
| 204 | + |
| 205 | + check_lg2(git_tag_create_lightweight(&oid, repo, opts->tag_name, |
| 206 | + target, opts->force), "Unable to create tag", NULL); |
| 207 | + |
| 208 | + git_object_free(target); |
| 209 | +} |
| 210 | + |
| 211 | +static void action_create_tag(tag_state *state) |
| 212 | +{ |
| 213 | + git_repository *repo = state->repo; |
| 214 | + tag_options *opts = state->opts; |
| 215 | + git_signature *tagger; |
| 216 | + git_oid oid; |
| 217 | + git_object *target; |
| 218 | + |
| 219 | + check(!opts->tag_name, "Name required"); |
| 220 | + check(!opts->message, "Message required"); |
| 221 | + |
| 222 | + if (!opts->target) opts->target = "HEAD"; |
| 223 | + |
| 224 | + check_lg2(git_revparse_single(&target, repo, opts->target), |
| 225 | + "Unable to resolve spec", opts->target); |
| 226 | + |
| 227 | + check_lg2(git_signature_default(&tagger, repo), |
| 228 | + "Unable to create signature", NULL); |
| 229 | + |
| 230 | + check_lg2(git_tag_create(&oid, repo, opts->tag_name, |
| 231 | + target, tagger, opts->message, opts->force), "Unable to create tag", NULL); |
| 232 | + |
| 233 | + git_object_free(target); |
| 234 | + git_signature_free(tagger); |
| 235 | +} |
| 236 | + |
| 237 | +static void print_usage() |
| 238 | +{ |
| 239 | + fprintf(stderr, "usage: see `git help tag`\n"); |
| 240 | + exit(1); |
| 241 | +} |
| 242 | + |
| 243 | +/** Parse command line arguments and choose action to run when done */ |
| 244 | +static void parse_options(tag_action *action, tag_options *opts, int argc, char **argv) |
| 245 | +{ |
| 246 | + args_info args = ARGS_INFO_INIT; |
| 247 | + *action = &action_list_tags; |
| 248 | + |
| 249 | + for (args.pos = 1; args.pos < argc; ++args.pos) { |
| 250 | + const char *curr = argv[args.pos]; |
| 251 | + |
| 252 | + if (curr[0] != '-') { |
| 253 | + if (!opts->tag_name) |
| 254 | + opts->tag_name = curr; |
| 255 | + else if (!opts->target) |
| 256 | + opts->target = curr; |
| 257 | + else |
| 258 | + print_usage(); |
| 259 | + |
| 260 | + if (*action != &action_create_tag) |
| 261 | + *action = &action_create_lighweight_tag; |
| 262 | + } else if (!strcmp(curr, "-n")) { |
| 263 | + opts->num_lines = 1; |
| 264 | + *action = &action_list_tags; |
| 265 | + } else if (!strcmp(curr, "-a")) { |
| 266 | + *action = &action_create_tag; |
| 267 | + } else if (!strcmp(curr, "-f")) { |
| 268 | + opts->force = 1; |
| 269 | + } else if (match_int_arg(&opts->num_lines, &args, "-n", 0)) { |
| 270 | + *action = &action_list_tags; |
| 271 | + } else if (match_str_arg(&opts->pattern, &args, "-l")) { |
| 272 | + *action = &action_list_tags; |
| 273 | + } else if (match_str_arg(&opts->tag_name, &args, "-d")) { |
| 274 | + *action = &action_delete_tag; |
| 275 | + } else if (match_str_arg(&opts->message, &args, "-m")) { |
| 276 | + *action = &action_create_tag; |
| 277 | + } |
| 278 | + } |
| 279 | +} |
| 280 | + |
| 281 | +/** Initialize tag_options struct */ |
| 282 | +static void tag_options_init(tag_options *opts) |
| 283 | +{ |
| 284 | + memset(opts, 0, sizeof(*opts)); |
| 285 | + |
| 286 | + opts->message = NULL; |
| 287 | + opts->pattern = NULL; |
| 288 | + opts->tag_name = NULL; |
| 289 | + opts->target = NULL; |
| 290 | + opts->num_lines = 0; |
| 291 | + opts->force = 0; |
| 292 | +} |
| 293 | + |
| 294 | +int main(int argc, char **argv) |
| 295 | +{ |
| 296 | + git_threads_init(); |
| 297 | + |
| 298 | + git_repository *repo; |
| 299 | + check_lg2(git_repository_open_ext(&repo, ".", 0, NULL), |
| 300 | + "Could not open repository", NULL); |
| 301 | + |
| 302 | + tag_options opts; |
| 303 | + tag_options_init(&opts); |
| 304 | + tag_action action; |
| 305 | + parse_options(&action, &opts, argc, argv); |
| 306 | + |
| 307 | + tag_state state = {repo, &opts}; |
| 308 | + action(&state); |
| 309 | + |
| 310 | + git_repository_free(repo); |
| 311 | + git_threads_shutdown(); |
| 312 | + |
| 313 | + return 0; |
| 314 | +} |
0 commit comments