summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2015-03-03 14:35:10 +0100
committerCarlos Martín Nieto <cmn@dwim.me>2015-03-03 14:35:10 +0100
commitfe477951be315e6fe28d6df9baf1557cef6fe539 (patch)
treeef31affdc679b59f17e597148f5883022cc03af7 /examples
parent99b68a2aecfaa24f252f265d61b230b8e2576dd2 (diff)
parent23d1dbe91963b70fbf05ce504d8d143c5e5ec568 (diff)
downloadlibgit2-fe477951be315e6fe28d6df9baf1557cef6fe539.tar.gz
Merge pull request #2885 from JIghtuse/master
describe example: function to add commits to opts
Diffstat (limited to 'examples')
-rw-r--r--examples/describe.c30
1 files changed, 24 insertions, 6 deletions
diff --git a/examples/describe.c b/examples/describe.c
index 72d981153..f7b4b1c20 100644
--- a/examples/describe.c
+++ b/examples/describe.c
@@ -13,6 +13,7 @@
*/
#include "common.h"
+#include <assert.h>
/**
* The following example partially reimplements the `git describe` command
@@ -46,6 +47,27 @@ typedef struct {
typedef struct args_info args_info;
+static void *xrealloc(void *oldp, size_t newsz)
+{
+ void *p = realloc(oldp, newsz);
+ if (p == NULL) {
+ fprintf(stderr, "Cannot allocate memory, exiting.\n");
+ exit(1);
+ }
+ return p;
+}
+
+static void opts_add_commit(describe_options *opts, const char *commit)
+{
+ size_t sz;
+
+ assert(opts != NULL);
+
+ sz = ++opts->commit_count * sizeof(opts->commits[0]);
+ opts->commits = xrealloc(opts->commits, sz);
+ opts->commits[opts->commit_count - 1] = commit;
+}
+
static void do_describe_single(git_repository *repo, describe_options *opts, const char *rev)
{
git_object *commit;
@@ -96,9 +118,7 @@ static void parse_options(describe_options *opts, int argc, char **argv)
const char *curr = argv[args.pos];
if (curr[0] != '-') {
- size_t newsz = ++opts->commit_count * sizeof(opts->commits[0]);
- opts->commits = (const char **)realloc((void *)opts->commits, newsz);
- opts->commits[opts->commit_count - 1] = curr;
+ opts_add_commit(opts, curr);
} else if (!strcmp(curr, "--all")) {
opts->describe_options.describe_strategy = GIT_DESCRIBE_ALL;
} else if (!strcmp(curr, "--tags")) {
@@ -124,9 +144,7 @@ static void parse_options(describe_options *opts, int argc, char **argv)
}
else {
if (!opts->format_options.dirty_suffix || !opts->format_options.dirty_suffix[0]) {
- size_t sz = ++opts->commit_count * sizeof(opts->commits[0]);
- opts->commits = (const char **)malloc(sz);
- opts->commits[0] = "HEAD";
+ opts_add_commit(opts, "HEAD");
}
}
}