Unverified Commit f55accce by Patrick Steinhardt Committed by GitHub

Merge pull request #4040 from tiennou/examples/merge

Merge example
parents 341608dc 33f44db9
LINK_DIRECTORIES(${LIBGIT2_LIBDIRS})
INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
FILE(GLOB_RECURSE SRC_EXAMPLE_GIT2 network/*.c network/*.h)
FILE(GLOB_RECURSE SRC_EXAMPLE_GIT2 network/*.c network/*.h common.?)
ADD_EXECUTABLE(cgit2 ${SRC_EXAMPLE_GIT2})
IF(WIN32 OR ANDROID)
TARGET_LINK_LIBRARIES(cgit2 git2)
......
......@@ -235,3 +235,13 @@ void treeish_to_tree(
git_object_free(obj);
}
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;
}
......@@ -103,3 +103,8 @@ extern int diff_output(
*/
extern void treeish_to_tree(
git_tree **out, git_repository *repo, const char *treeish);
/**
* A realloc that exits on failure
*/
extern void *xrealloc(void *oldp, size_t newsz);
......@@ -47,16 +47,6 @@ 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;
......
......@@ -8,13 +8,6 @@
# include <unistd.h>
#endif
struct dl_data {
git_remote *remote;
git_fetch_options *fetch_opts;
int ret;
int finished;
};
static int progress_cb(const char *str, int len, void *data)
{
(void)data;
......
......@@ -2,10 +2,11 @@
#include <stdio.h>
#include <string.h>
#include "../common.h"
#include "common.h"
// This part is not strictly libgit2-dependent, but you can use this
// as a starting point for a git-like tool
/* This part is not strictly libgit2-dependent, but you can use this
* as a starting point for a git-like tool */
struct {
char *name;
......@@ -18,20 +19,12 @@ struct {
{ NULL, NULL}
};
static int run_command(git_cb fn, int argc, char **argv)
static int run_command(git_cb fn, git_repository *repo, struct args_info args)
{
int error;
git_repository *repo;
// Before running the actual command, create an instance of the local
// repository and pass it to the function.
error = git_repository_open(&repo, ".git");
if (error < 0)
repo = NULL;
// Run the command. If something goes wrong, print the error message to stderr
error = fn(repo, argc, argv);
/* Run the command. If something goes wrong, print the error message to stderr */
error = fn(repo, args.argc - args.pos, &args.argv[args.pos]);
if (error < 0) {
if (giterr_last() == NULL)
fprintf(stderr, "Error without message");
......@@ -39,9 +32,6 @@ static int run_command(git_cb fn, int argc, char **argv)
fprintf(stderr, "Bad news:\n %s\n", giterr_last()->message);
}
if(repo)
git_repository_free(repo);
return !!error;
}
......@@ -49,6 +39,10 @@ int main(int argc, char **argv)
{
int i;
int return_code = 1;
int error;
git_repository *repo;
struct args_info args = ARGS_INFO_INIT;
const char *git_dir = NULL;
if (argc < 2) {
fprintf(stderr, "usage: %s <cmd> [repo]\n", argv[0]);
......@@ -57,9 +51,30 @@ int main(int argc, char **argv)
git_libgit2_init();
for (args.pos = 1; args.pos < args.argc; ++args.pos) {
char *a = args.argv[args.pos];
if (a[0] != '-') {
/* non-arg */
break;
} else if (optional_str_arg(&git_dir, &args, "--git-dir", ".git")) {
continue;
} else if (!strcmp(a, "--")) {
/* arg separator */
break;
}
}
/* Before running the actual command, create an instance of the local
* repository and pass it to the function. */
error = git_repository_open(&repo, git_dir);
if (error < 0)
repo = NULL;
for (i = 0; commands[i].name != NULL; ++i) {
if (!strcmp(argv[1], commands[i].name)) {
return_code = run_command(commands[i].fn, --argc, ++argv);
if (!strcmp(args.argv[args.pos], commands[i].name)) {
return_code = run_command(commands[i].fn, repo, args);
goto shutdown;
}
}
......@@ -67,6 +82,8 @@ int main(int argc, char **argv)
fprintf(stderr, "Command not found: %s\n", argv[1]);
shutdown:
git_repository_free(repo);
git_libgit2_shutdown();
return return_code;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment