Unverified Commit 68ba2e8d by Patrick Steinhardt Committed by GitHub

Merge pull request #4956 from pks-t/pks/examples-cgit2-standalone

examples: produce single cgit2 binary
parents 9eb098d8 6cf47726
INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
INCLUDE_DIRECTORIES(SYSTEM ${LIBGIT2_SYSTEM_INCLUDES})
FILE(GLOB_RECURSE SRC_EXAMPLE_GIT2 network/*.c network/*.h common.?)
ADD_EXECUTABLE(cgit2 ${SRC_EXAMPLE_GIT2})
SET_TARGET_PROPERTIES(cgit2 PROPERTIES C_STANDARD 90)
FILE(GLOB LG2_SOURCES *.c)
ADD_EXECUTABLE(lg2 ${LG2_SOURCES})
SET_TARGET_PROPERTIES(lg2 PROPERTIES C_STANDARD 90)
# Ensure that we do not use deprecated functions internally
ADD_DEFINITIONS(-DGIT_DEPRECATE_HARD)
IF(WIN32 OR ANDROID)
TARGET_LINK_LIBRARIES(cgit2 git2)
TARGET_LINK_LIBRARIES(lg2 git2)
ELSE()
TARGET_LINK_LIBRARIES(cgit2 git2 pthread)
TARGET_LINK_LIBRARIES(lg2 git2 pthread)
ENDIF()
FILE(GLOB SRC_EXAMPLE_APPS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c)
FOREACH(src_app ${SRC_EXAMPLE_APPS})
STRING(REPLACE ".c" "" app_name ${src_app})
IF(NOT ${app_name} STREQUAL "common")
ADD_EXECUTABLE(${app_name} ${src_app} "common.c")
TARGET_LINK_LIBRARIES(${app_name} git2)
SET_TARGET_PROPERTIES(${app_name} PROPERTIES C_STANDARD 90)
ENDIF()
ENDFOREACH()
......@@ -15,6 +15,18 @@
#include "common.h"
#include <assert.h>
/**
* The following example demonstrates how to add files with libgit2.
*
* It will use the repository in the current working directory, and act
* on files passed as its parameters.
*
* Recognized options are:
* -v/--verbose: show the file's status after acting on it.
* -n/--dry-run: do not actually change the index.
* -u/--update: update the index instead of adding to it.
*/
enum print_options {
SKIP = 1,
VERBOSE = 2,
......@@ -31,46 +43,46 @@ static void parse_opts(int *options, int *count, int argc, char *argv[]);
void init_array(git_strarray *array, int argc, char **argv);
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload);
int main (int argc, char** argv)
int lg2_add(git_repository *repo, int argc, char** argv)
{
git_index_matched_path_cb matched_cb = NULL;
git_repository *repo = NULL;
git_index *index;
git_strarray array = {0};
int options = 0, count = 0;
struct print_payload payload = {0};
git_libgit2_init();
parse_opts(&options, &count, argc, argv);
init_array(&array, argc-count, argv+count);
check_lg2(git_repository_open(&repo, "."), "No git repository", NULL);
check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL);
if (options&VERBOSE || options&SKIP) {
/* Setup a callback if the requested options need it */
if ((options & VERBOSE) || (options & SKIP)) {
matched_cb = &print_matched_cb;
}
/* Perform the requested action with the index and files */
payload.options = options;
payload.repo = repo;
if (options&UPDATE) {
if (options & UPDATE) {
git_index_update_all(index, &array, matched_cb, &payload);
} else {
git_index_add_all(index, &array, 0, matched_cb, &payload);
}
/* Cleanup memory */
git_index_write(index);
git_index_free(index);
git_repository_free(repo);
git_libgit2_shutdown();
return 0;
}
/*
* This callback is called for each file under consideration by
* git_index_(update|add)_all above.
* It makes uses of the callback's ability to abort the action.
*/
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload)
{
struct print_payload p = *(struct print_payload*)(payload);
......@@ -78,18 +90,19 @@ int print_matched_cb(const char *path, const char *matched_pathspec, void *paylo
unsigned status;
(void)matched_pathspec;
/* Get the file status */
if (git_status_file(&status, p.repo, path)) {
return -1;
}
if (status & GIT_STATUS_WT_MODIFIED || status & GIT_STATUS_WT_NEW) {
if ((status & GIT_STATUS_WT_MODIFIED) || (status & GIT_STATUS_WT_NEW)) {
printf("add '%s'\n", path);
ret = 0;
} else {
ret = 1;
}
if(p.options & SKIP) {
if ((p.options & SKIP)) {
ret = 1;
}
......@@ -101,11 +114,11 @@ void init_array(git_strarray *array, int argc, char **argv)
unsigned int i;
array->count = argc;
array->strings = malloc(sizeof(char*) * array->count);
assert(array->strings!=NULL);
array->strings = calloc(array->count, sizeof(char *));
assert(array->strings != NULL);
for(i=0; i<array->count; i++) {
array->strings[i]=argv[i];
for (i = 0; i < array->count; i++) {
array->strings[i] = argv[i];
}
return;
......@@ -125,33 +138,27 @@ static void parse_opts(int *options, int *count, int argc, char *argv[])
int i;
for (i = 1; i < argc; ++i) {
if (argv[i][0] != '-') {
if (argv[i][0] != '-')
break;
}
else if(!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v")) {
else if (!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v"))
*options |= VERBOSE;
}
else if(!strcmp(argv[i], "--dry-run") || !strcmp(argv[i], "-n")) {
else if (!strcmp(argv[i], "--dry-run") || !strcmp(argv[i], "-n"))
*options |= SKIP;
}
else if(!strcmp(argv[i], "--update") || !strcmp(argv[i], "-u")) {
else if (!strcmp(argv[i], "--update") || !strcmp(argv[i], "-u"))
*options |= UPDATE;
}
else if(!strcmp(argv[i], "-h")) {
else if (!strcmp(argv[i], "-h")) {
print_usage();
break;
}
else if(!strcmp(argv[i], "--")) {
} else if (!strcmp(argv[i], "--")) {
i++;
break;
}
else {
} else {
fprintf(stderr, "Unsupported option %s.\n", argv[i]);
print_usage();
}
}
if (argc<=i)
if (argc <= i)
print_usage();
*count = i;
......
......@@ -35,30 +35,24 @@ struct opts {
};
static void parse_opts(struct opts *o, int argc, char *argv[]);
int main(int argc, char *argv[])
int lg2_blame(git_repository *repo, int argc, char *argv[])
{
int line, break_on_null_hunk;
size_t i, rawsize;
char spec[1024] = {0};
struct opts o = {0};
const char *rawdata;
git_repository *repo = NULL;
git_revspec revspec = {0};
git_blame_options blameopts = GIT_BLAME_OPTIONS_INIT;
git_blame *blame = NULL;
git_blob *blob;
git_object *obj;
git_libgit2_init();
parse_opts(&o, argc, argv);
if (o.M) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES;
if (o.C) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES;
if (o.F) blameopts.flags |= GIT_BLAME_FIRST_PARENT;
/** Open the repository. */
check_lg2(git_repository_open_ext(&repo, ".", 0, NULL), "Couldn't open repository", NULL);
/**
* The commit range comes in "commitish" form. Use the rev-parse API to
* nail down the end points.
......@@ -131,9 +125,6 @@ int main(int argc, char *argv[])
/** Cleanup. */
git_blob_free(blob);
git_blame_free(blame);
git_repository_free(repo);
git_libgit2_shutdown();
return 0;
}
......
......@@ -120,19 +120,14 @@ static void parse_opts(struct opts *o, int argc, char *argv[]);
/** Entry point for this command */
int main(int argc, char *argv[])
int lg2_cat_file(git_repository *repo, int argc, char *argv[])
{
git_repository *repo;
struct opts o = { ".", NULL, 0, 0 };
git_object *obj = NULL;
char oidstr[GIT_OID_HEXSZ + 1];
git_libgit2_init();
parse_opts(&o, argc, argv);
check_lg2(git_repository_open_ext(&repo, o.dir, 0, NULL),
"Could not open repository", NULL);
check_lg2(git_revparse_single(&obj, repo, o.rev),
"Could not resolve", o.rev);
......@@ -188,9 +183,6 @@ int main(int argc, char *argv[])
}
git_object_free(obj);
git_repository_free(repo);
git_libgit2_shutdown();
return 0;
}
......
......@@ -172,9 +172,8 @@ cleanup:
}
/** That example's entry point */
int main(int argc, char **argv)
int lg2_checkout(git_repository *repo, int argc, char **argv)
{
git_repository *repo = NULL;
struct args_info args = ARGS_INFO_INIT;
checkout_options opts;
git_repository_state_t state;
......@@ -185,15 +184,6 @@ int main(int argc, char **argv)
/** Parse our command line options */
parse_options(&path, &opts, &args);
/** Initialize the library */
err = git_libgit2_init();
if (!err)
check_lg2(err, "Failed to initialize libgit2", NULL);
/** Open the repository corresponding to the options */
check_lg2(git_repository_open_ext(&repo, path, 0, NULL),
"Could not open repository", NULL);
/** Make sure we're not about to checkout while something else is going on */
state = git_repository_state(repo);
if (state != GIT_REPOSITORY_STATE_NONE) {
......@@ -228,8 +218,5 @@ int main(int argc, char **argv)
cleanup:
git_annotated_commit_free(checkout_target);
git_repository_free(repo);
git_libgit2_shutdown();
return err;
}
#include "common.h"
#include <git2.h>
#include <git2/clone.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
# include <pthread.h>
# include <unistd.h>
#endif
typedef struct progress_data {
git_transfer_progress fetch_progress;
......@@ -72,7 +63,7 @@ static void checkout_progress(const char *path, size_t cur, size_t tot, void *pa
}
int do_clone(git_repository *repo, int argc, char **argv)
int lg2_clone(git_repository *repo, int argc, char **argv)
{
progress_data pd = {{0}};
git_repository *cloned_repo = NULL;
......
......@@ -13,6 +13,9 @@
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "common.h"
......@@ -289,3 +292,76 @@ int resolve_refish(git_annotated_commit **commit, git_repository *repo, const ch
return err;
}
static int readline(char **out)
{
int c, error = 0, length = 0, allocated = 0;
char *line = NULL;
errno = 0;
while ((c = getchar()) != EOF) {
if (length == allocated) {
allocated += 16;
if ((line = realloc(line, allocated)) == NULL) {
error = -1;
goto error;
}
}
if (c == '\n')
break;
line[length++] = c;
}
if (errno != 0) {
error = -1;
goto error;
}
line[length] = '\0';
*out = line;
line = NULL;
error = length;
error:
free(line);
return error;
}
int cred_acquire_cb(git_cred **out,
const char *url,
const char *username_from_url,
unsigned int allowed_types,
void *payload)
{
char *username = NULL, *password = NULL;
int error;
UNUSED(url);
UNUSED(username_from_url);
UNUSED(allowed_types);
UNUSED(payload);
printf("Username: ");
if (readline(&username) < 0) {
fprintf(stderr, "Unable to read username: %s", strerror(errno));
return -1;
}
/* Yup. Right there on your terminal. Careful where you copy/paste output. */
printf("Password: ");
if (readline(&password) < 0) {
fprintf(stderr, "Unable to read password: %s", strerror(errno));
free(username);
return -1;
}
error = git_cred_userpass_plaintext_new(out, username, password);
free(username);
free(password);
return error;
}
......@@ -17,6 +17,41 @@
#include <stdlib.h>
#include <git2.h>
#ifndef PRIuZ
/* Define the printf format specifer to use for size_t output */
#if defined(_MSC_VER) || defined(__MINGW32__)
# define PRIuZ "Iu"
#else
# define PRIuZ "zu"
#endif
#endif
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
#define UNUSED(x) (void)(x)
extern int lg2_add(git_repository *repo, int argc, char **argv);
extern int lg2_blame(git_repository *repo, int argc, char **argv);
extern int lg2_cat_file(git_repository *repo, int argc, char **argv);
extern int lg2_checkout(git_repository *repo, int argc, char **argv);
extern int lg2_clone(git_repository *repo, int argc, char **argv);
extern int lg2_describe(git_repository *repo, int argc, char **argv);
extern int lg2_diff(git_repository *repo, int argc, char **argv);
extern int lg2_fetch(git_repository *repo, int argc, char **argv);
extern int lg2_for_each_ref(git_repository *repo, int argc, char **argv);
extern int lg2_general(git_repository *repo, int argc, char **argv);
extern int lg2_index_pack(git_repository *repo, int argc, char **argv);
extern int lg2_init(git_repository *repo, int argc, char **argv);
extern int lg2_log(git_repository *repo, int argc, char **argv);
extern int lg2_ls_files(git_repository *repo, int argc, char **argv);
extern int lg2_ls_remote(git_repository *repo, int argc, char **argv);
extern int lg2_merge(git_repository *repo, int argc, char **argv);
extern int lg2_remote(git_repository *repo, int argc, char **argv);
extern int lg2_rev_list(git_repository *repo, int argc, char **argv);
extern int lg2_rev_parse(git_repository *repo, int argc, char **argv);
extern int lg2_show_index(git_repository *repo, int argc, char **argv);
extern int lg2_status(git_repository *repo, int argc, char **argv);
extern int lg2_tag(git_repository *repo, int argc, char **argv);
/**
* Check libgit2 error code, printing error to stderr on failure and
* exiting the program.
......@@ -122,3 +157,12 @@ extern void *xrealloc(void *oldp, size_t newsz);
* Convert a refish to an annotated commit.
*/
extern int resolve_refish(git_annotated_commit **commit, git_repository *repo, const char *refish);
/**
* Acquire credentials via command line
*/
extern int cred_acquire_cb(git_cred **out,
const char *url,
const char *username_from_url,
unsigned int allowed_types,
void *payload);
......@@ -152,23 +152,14 @@ static void describe_options_init(describe_options *opts)
git_describe_init_format_options(&opts->format_options, GIT_DESCRIBE_FORMAT_OPTIONS_VERSION);
}
int main(int argc, char **argv)
int lg2_describe(git_repository *repo, int argc, char **argv)
{
git_repository *repo;
describe_options opts;
git_libgit2_init();
check_lg2(git_repository_open_ext(&repo, ".", 0, NULL),
"Could not open repository", NULL);
describe_options_init(&opts);
parse_options(&opts, argc, argv);
do_describe(repo, &opts);
git_repository_free(repo);
git_libgit2_shutdown();
return 0;
}
......@@ -67,9 +67,8 @@ static int color_printer(
const git_diff_delta*, const git_diff_hunk*, const git_diff_line*, void*);
static void diff_print_stats(git_diff *diff, struct opts *o);
int main(int argc, char *argv[])
int lg2_diff(git_repository *repo, int argc, char *argv[])
{
git_repository *repo = NULL;
git_tree *t1 = NULL, *t2 = NULL;
git_diff *diff;
struct opts o = {
......@@ -77,13 +76,8 @@ int main(int argc, char *argv[])
-1, 0, 0, GIT_DIFF_FORMAT_PATCH, NULL, NULL, "."
};
git_libgit2_init();
parse_opts(&o, argc, argv);
check_lg2(git_repository_open_ext(&repo, o.dir, 0, NULL),
"Could not open repository", o.dir);
/**
* Possible argument patterns:
*
......@@ -157,13 +151,9 @@ int main(int argc, char *argv[])
}
/** Cleanup before exiting. */
git_diff_free(diff);
git_tree_free(t1);
git_tree_free(t2);
git_repository_free(repo);
git_libgit2_shutdown();
return 0;
}
......
#include "common.h"
#include <git2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
# include <pthread.h>
# include <unistd.h>
#endif
static int progress_cb(const char *str, int len, void *data)
{
......@@ -62,7 +54,7 @@ static int transfer_progress_cb(const git_transfer_progress *stats, void *payloa
}
/** Entry point for this command */
int fetch(git_repository *repo, int argc, char **argv)
int lg2_fetch(git_repository *repo, int argc, char **argv)
{
git_remote *remote = NULL;
const git_transfer_progress *stats;
......
......@@ -31,19 +31,15 @@ static int show_ref(git_reference *ref, void *data)
return 0;
}
int main(int argc, char **argv)
int lg2_for_each_ref(git_repository *repo, int argc, char **argv)
{
git_repository *repo;
git_libgit2_init();
UNUSED(argv);
if (argc != 1 || argv[1] /* silence -Wunused-parameter */)
if (argc != 1)
fatal("Sorry, no for-each-ref options supported yet", NULL);
check_lg2(git_repository_open(&repo, "."),
"Could not open repository", NULL);
check_lg2(git_reference_foreach(repo, show_ref, repo),
"Could not iterate over references", NULL);
git_libgit2_shutdown();
return 0;
}
......@@ -76,12 +76,11 @@ static void check_error(int error_code, const char *action)
exit(1);
}
int main (int argc, char** argv)
int lg2_general(git_repository *repo, int argc, char** argv)
{
int error;
git_oid oid;
char *repo_path;
git_repository *repo;
/**
* Initialize the library, this will set up any global state which libgit2 needs
......
#include <git2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "common.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
......@@ -17,7 +15,6 @@
#else
# include <unistd.h>
#endif
#include "common.h"
/*
* This could be run in the main loop whilst the application waits for
......@@ -31,7 +28,7 @@ static int index_cb(const git_transfer_progress *stats, void *data)
return 0;
}
int index_pack(git_repository *repo, int argc, char **argv)
int lg2_index_pack(git_repository *repo, int argc, char **argv)
{
git_indexer *idx;
git_transfer_progress stats = {0, 0};
......
......@@ -40,14 +40,10 @@ struct opts {
static void create_initial_commit(git_repository *repo);
static void parse_opts(struct opts *o, int argc, char *argv[]);
int main(int argc, char *argv[])
int lg2_init(git_repository *repo, int argc, char *argv[])
{
git_repository *repo = NULL;
struct opts o = { 1, 0, 0, 0, GIT_REPOSITORY_INIT_SHARED_UMASK, 0, 0, 0 };
git_libgit2_init();
parse_opts(&o, argc, argv);
/* Initialize repository. */
......@@ -116,7 +112,6 @@ int main(int argc, char *argv[])
}
git_repository_free(repo);
git_libgit2_shutdown();
return 0;
}
......
#include <stdlib.h>
#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 */
typedef int (*git_command_fn)(git_repository *, int , char **);
struct {
char *name;
git_cb fn;
git_command_fn fn;
char requires_repo;
} commands[] = {
{"ls-remote", ls_remote},
{"fetch", fetch},
{"clone", do_clone},
{"index-pack", index_pack},
{ NULL, NULL}
{ "add", lg2_add, 1 },
{ "blame", lg2_blame, 1 },
{ "cat-file", lg2_cat_file, 1 },
{ "checkout", lg2_checkout, 1 },
{ "clone", lg2_clone, 0 },
{ "describe", lg2_describe, 1 },
{ "diff", lg2_diff, 1 },
{ "fetch", lg2_fetch, 1 },
{ "for-each-ref", lg2_for_each_ref, 1 },
{ "general", lg2_general, 0 },
{ "index-pack", lg2_index_pack, 1 },
{ "init", lg2_init, 0 },
{ "log", lg2_log, 1 },
{ "ls-files", lg2_ls_files, 1 },
{ "ls-remote", lg2_ls_remote, 1 },
{ "merge", lg2_merge, 1 },
{ "remote", lg2_remote, 1 },
{ "rev-list", lg2_rev_list, 1 },
{ "rev-parse", lg2_rev_parse, 1 },
{ "show-index", lg2_show_index, 0 },
{ "status", lg2_status, 1 },
{ "tag", lg2_tag, 1 },
};
static int run_command(git_cb fn, git_repository *repo, struct args_info args)
static int run_command(git_command_fn fn, git_repository *repo, struct args_info args)
{
int error;
......@@ -35,19 +50,27 @@ static int run_command(git_cb fn, git_repository *repo, struct args_info args)
return !!error;
}
static int usage(const char *prog)
{
size_t i;
fprintf(stderr, "usage: %s <cmd>...\n\nAvailable commands:\n\n", prog);
for (i = 0; i < ARRAY_SIZE(commands); i++)
fprintf(stderr, "\t%s\n", commands[i].name);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
int i;
int return_code = 1;
int error;
git_repository *repo;
struct args_info args = ARGS_INFO_INIT;
git_repository *repo = NULL;
const char *git_dir = NULL;
int return_code = 1;
size_t i;
if (argc < 2) {
fprintf(stderr, "usage: %s <cmd> [repo]\n", argv[0]);
exit(EXIT_FAILURE);
}
if (argc < 2)
usage(argv[0]);
git_libgit2_init();
......@@ -65,25 +88,33 @@ int main(int argc, char **argv)
}
}
/* Before running the actual command, create an instance of the local
* repository and pass it to the function. */
if (args.pos == args.argc)
usage(argv[0]);
error = git_repository_open(&repo, git_dir);
if (error < 0)
repo = NULL;
if (!git_dir)
git_dir = ".";
for (i = 0; i < ARRAY_SIZE(commands); ++i) {
if (strcmp(args.argv[args.pos], commands[i].name))
continue;
/*
* Before running the actual command, create an instance
* of the local repository and pass it to the function.
* */
if (commands[i].requires_repo) {
check_lg2(git_repository_open_ext(&repo, git_dir, 0, NULL),
"Unable to open repository '%s'", git_dir);
}
for (i = 0; commands[i].name != NULL; ++i) {
if (!strcmp(args.argv[args.pos], commands[i].name)) {
return_code = run_command(commands[i].fn, repo, args);
goto shutdown;
}
}
fprintf(stderr, "Command not found: %s\n", argv[1]);
shutdown:
git_repository_free(repo);
git_libgit2_shutdown();
return return_code;
......
......@@ -71,7 +71,7 @@ static int match_with_parent(git_commit *commit, int i, git_diff_options *);
static int signature_matches(const git_signature *sig, const char *filter);
static int log_message_matches(const git_commit *commit, const char *filter);
int main(int argc, char *argv[])
int lg2_log(git_repository *repo, int argc, char *argv[])
{
int i, count = 0, printed = 0, parents, last_arg;
struct log_state s;
......@@ -81,11 +81,9 @@ int main(int argc, char *argv[])
git_commit *commit = NULL;
git_pathspec *ps = NULL;
git_libgit2_init();
/** Parse arguments and set up revwalker. */
last_arg = parse_options(&s, &opt, argc, argv);
s.repo = repo;
diffopts.pathspec.strings = &argv[last_arg];
diffopts.pathspec.count = argc - last_arg;
......@@ -180,8 +178,6 @@ int main(int argc, char *argv[])
git_pathspec_free(ps);
git_revwalk_free(s.walker);
git_repository_free(s.repo);
git_libgit2_shutdown();
return 0;
}
......@@ -243,13 +239,6 @@ static int add_revision(struct log_state *s, const char *revstr)
git_revspec revs;
int hide = 0;
/** Open repo on demand if it isn't already open. */
if (!s->repo) {
if (!s->repodir) s->repodir = ".";
check_lg2(git_repository_open_ext(&s->repo, s->repodir, 0, NULL),
"Could not open repository", s->repodir);
}
if (!revstr) {
push_rev(s, NULL, hide);
return 0;
......
......@@ -13,7 +13,6 @@
*/
#include "common.h"
#include "array.h"
/**
* This example demonstrates the libgit2 index APIs to roughly
......@@ -111,21 +110,15 @@ static int print_paths(ls_options *opts, git_index *index)
return 0;
}
int main(int argc, char *argv[])
int lg2_ls_files(git_repository *repo, int argc, char *argv[])
{
ls_options opts;
git_repository *repo = NULL;
git_index *index = NULL;
ls_options opts;
int error;
if ((error = parse_options(&opts, argc, argv)) < 0)
return error;
git_libgit2_init();
if ((error = git_repository_open_ext(&repo, ".", 0, NULL)) < 0)
goto cleanup;
if ((error = git_repository_index(&index, repo)) < 0)
goto cleanup;
......@@ -133,8 +126,6 @@ int main(int argc, char *argv[])
cleanup:
git_index_free(index);
git_repository_free(repo);
git_libgit2_shutdown();
return error;
}
#include <git2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "common.h"
static int use_remote(git_repository *repo, char *name)
......@@ -49,7 +45,7 @@ cleanup:
}
/** Entry point for this command */
int ls_remote(git_repository *repo, int argc, char **argv)
int lg2_ls_remote(git_repository *repo, int argc, char **argv)
{
int error;
......
......@@ -278,9 +278,8 @@ cleanup:
return err;
}
int main(int argc, char **argv)
int lg2_merge(git_repository *repo, int argc, char **argv)
{
git_repository *repo = NULL;
merge_options opts;
git_index *index;
git_repository_state_t state;
......@@ -292,11 +291,6 @@ int main(int argc, char **argv)
merge_options_init(&opts);
parse_options(&path, &opts, argc, argv);
git_libgit2_init();
check_lg2(git_repository_open_ext(&repo, path, 0, NULL),
"Could not open repository", NULL);
state = git_repository_state(repo);
if (state != GIT_REPOSITORY_STATE_NONE) {
fprintf(stderr, "repository is in unexpected state %d\n", state);
......@@ -366,8 +360,6 @@ int main(int argc, char **argv)
cleanup:
free(opts.heads);
free(opts.annotated);
git_repository_free(repo);
git_libgit2_shutdown();
return 0;
}
default: all
CC = gcc
CFLAGS += -g
CFLAGS += -I../../include
LDFLAGS += -L../../build -L../..
LIBRARIES += -lgit2 -lpthread
OBJECTS = \
git2.o \
ls-remote.o \
fetch.o \
clone.o \
index-pack.o \
common.o
all: $(OBJECTS)
$(CC) $(CFLAGS) $(LDFLAGS) -o git2 $(OBJECTS) $(LIBRARIES)
clean:
$(RM) $(OBJECTS)
$(RM) git2
#include "common.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
/* Shamelessly borrowed from http://stackoverflow.com/questions/3417837/
* with permission of the original author, Martin Pool.
* http://sourcefrog.net/weblog/software/languages/C/unused.html
*/
#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#else
# define UNUSED(x) x
#endif
static int readline(char **out)
{
int c, error = 0, length = 0, allocated = 0;
char *line = NULL;
errno = 0;
while ((c = getchar()) != EOF) {
if (length == allocated) {
allocated += 16;
if ((line = realloc(line, allocated)) == NULL) {
error = -1;
goto error;
}
}
if (c == '\n')
break;
line[length++] = c;
}
if (errno != 0) {
error = -1;
goto error;
}
line[length] = '\0';
*out = line;
line = NULL;
error = length;
error:
free(line);
return error;
}
int cred_acquire_cb(git_cred **out,
const char * UNUSED(url),
const char * UNUSED(username_from_url),
unsigned int UNUSED(allowed_types),
void * UNUSED(payload))
{
char *username = NULL, *password = NULL;
int error;
printf("Username: ");
if (readline(&username) < 0) {
fprintf(stderr, "Unable to read username: %s", strerror(errno));
return -1;
}
/* Yup. Right there on your terminal. Careful where you copy/paste output. */
printf("Password: ");
if (readline(&password) < 0) {
fprintf(stderr, "Unable to read password: %s", strerror(errno));
free(username);
return -1;
}
error = git_cred_userpass_plaintext_new(out, username, password);
free(username);
free(password);
return error;
}
#ifndef __COMMON_H__
#define __COMMON_H__
#include <git2.h>
typedef int (*git_cb)(git_repository *, int , char **);
int ls_remote(git_repository *repo, int argc, char **argv);
int parse_pkt_line(git_repository *repo, int argc, char **argv);
int show_remote(git_repository *repo, int argc, char **argv);
int fetch(git_repository *repo, int argc, char **argv);
int index_pack(git_repository *repo, int argc, char **argv);
int do_clone(git_repository *repo, int argc, char **argv);
int cred_acquire_cb(git_cred **out,
const char * url,
const char * username_from_url,
unsigned int allowed_types,
void *payload);
#ifndef PRIuZ
/* Define the printf format specifer to use for size_t output */
#if defined(_MSC_VER) || defined(__MINGW32__)
# define PRIuZ "Iu"
#else
# define PRIuZ "zu"
#endif
#endif
#endif /* __COMMON_H__ */
......@@ -48,24 +48,13 @@ static void parse_subcmd(
struct opts *opt, int argc, char **argv);
static void usage(const char *msg, const char *arg);
int main(int argc, char *argv[])
int lg2_remote(git_repository *repo, int argc, char *argv[])
{
int retval = 0;
struct opts opt = {0};
git_buf buf = GIT_BUF_INIT_CONST(NULL, 0);
git_repository *repo = NULL;
parse_subcmd(&opt, argc, argv);
git_libgit2_init();
check_lg2(git_repository_discover(&buf, ".", 0, NULL),
"Could not find repository", NULL);
check_lg2(git_repository_open(&repo, buf.ptr),
"Could not open repository", NULL);
git_buf_dispose(&buf);
switch (opt.cmd)
{
case subcmd_add:
......@@ -85,8 +74,6 @@ int main(int argc, char *argv[])
break;
}
git_libgit2_shutdown();
return retval;
}
......
......@@ -17,16 +17,12 @@
static int revwalk_parseopts(git_repository *repo, git_revwalk *walk, int nopts, char **opts);
int main (int argc, char **argv)
int lg2_rev_list(git_repository *repo, int argc, char **argv)
{
git_repository *repo;
git_revwalk *walk;
git_oid oid;
char buf[GIT_OID_HEXSZ+1];
git_libgit2_init();
check_lg2(git_repository_open_ext(&repo, ".", 0, NULL), "opening repository", NULL);
check_lg2(git_revwalk_new(&walk, repo), "allocating revwalk", NULL);
check_lg2(revwalk_parseopts(repo, walk, argc-1, argv+1), "parsing options", NULL);
......@@ -36,7 +32,6 @@ int main (int argc, char **argv)
printf("%s\n", buf);
}
git_libgit2_shutdown();
return 0;
}
......
......@@ -16,26 +16,20 @@
/** Forward declarations for helpers. */
struct parse_state {
git_repository *repo;
const char *repodir;
const char *spec;
int not;
};
static void parse_opts(struct parse_state *ps, int argc, char *argv[]);
static int parse_revision(struct parse_state *ps);
static int parse_revision(git_repository *repo, struct parse_state *ps);
int main(int argc, char *argv[])
int lg2_rev_parse(git_repository *repo, int argc, char *argv[])
{
struct parse_state ps = {0};
git_libgit2_init();
parse_opts(&ps, argc, argv);
check_lg2(parse_revision(&ps), "Parsing", NULL);
git_repository_free(ps.repo);
git_libgit2_shutdown();
check_lg2(parse_revision(repo, &ps), "Parsing", NULL);
return 0;
}
......@@ -68,19 +62,12 @@ static void parse_opts(struct parse_state *ps, int argc, char *argv[])
}
}
static int parse_revision(struct parse_state *ps)
static int parse_revision(git_repository *repo, struct parse_state *ps)
{
git_revspec rs;
char str[GIT_OID_HEXSZ + 1];
if (!ps->repo) {
if (!ps->repodir)
ps->repodir = ".";
check_lg2(git_repository_open_ext(&ps->repo, ps->repodir, 0, NULL),
"Could not open repository from", ps->repodir);
}
check_lg2(git_revparse(&rs, ps->repo, ps->spec), "Could not parse", ps->spec);
check_lg2(git_revparse(&rs, repo, ps->spec), "Could not parse", ps->spec);
if ((rs.flags & GIT_REVPARSE_SINGLE) != 0) {
git_oid_tostr(str, sizeof(str), git_object_id(rs.from));
......@@ -94,7 +81,7 @@ static int parse_revision(struct parse_state *ps)
if ((rs.flags & GIT_REVPARSE_MERGE_BASE) != 0) {
git_oid base;
check_lg2(git_merge_base(&base, ps->repo,
check_lg2(git_merge_base(&base, repo,
git_object_id(rs.from), git_object_id(rs.to)),
"Could not find merge base", ps->spec);
......
......@@ -14,7 +14,7 @@
#include "common.h"
int main (int argc, char** argv)
int lg2_show_index(git_repository *repo, int argc, char** argv)
{
git_index *index;
unsigned int i, ecount;
......@@ -23,8 +23,6 @@ int main (int argc, char** argv)
char out[GIT_OID_HEXSZ+1];
out[GIT_OID_HEXSZ] = '\0';
git_libgit2_init();
if (argc > 2)
fatal("usage: showindex [<repo-dir>]", NULL);
if (argc > 1)
......@@ -34,7 +32,6 @@ int main (int argc, char** argv)
if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) {
check_lg2(git_index_open(&index, dir), "could not open index", dir);
} else {
git_repository *repo;
check_lg2(git_repository_open_ext(&repo, dir, 0, NULL), "could not open repository", dir);
check_lg2(git_repository_index(&index, repo), "could not open repository index", NULL);
git_repository_free(repo);
......@@ -64,7 +61,6 @@ int main (int argc, char** argv)
}
git_index_free(index);
git_libgit2_shutdown();
return 0;
}
......@@ -67,14 +67,11 @@ static void print_long(git_status_list *status);
static void print_short(git_repository *repo, git_status_list *status);
static int print_submod(git_submodule *sm, const char *name, void *payload);
int main(int argc, char *argv[])
int lg2_status(git_repository *repo, int argc, char *argv[])
{
git_repository *repo = NULL;
git_status_list *status;
struct opts o = { GIT_STATUS_OPTIONS_INIT, "." };
git_libgit2_init();
o.statusopt.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
o.statusopt.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
......@@ -82,13 +79,6 @@ int main(int argc, char *argv[])
parse_opts(&o, argc, argv);
/**
* Try to open the repository at the given path (or at the current
* directory if none was given).
*/
check_lg2(git_repository_open_ext(&repo, o.repodir, 0, NULL),
"Could not open repository", o.repodir);
if (git_repository_is_bare(repo))
fatal("Cannot report status on bare repository",
git_repository_path(repo));
......@@ -134,9 +124,6 @@ show_status:
goto show_status;
}
git_repository_free(repo);
git_libgit2_shutdown();
return 0;
}
......
......@@ -293,18 +293,12 @@ static void tag_options_init(tag_options *opts)
opts->force = 0;
}
int main(int argc, char **argv)
int lg2_tag(git_repository *repo, int argc, char **argv)
{
git_repository *repo;
tag_options opts;
tag_action action;
tag_state state;
git_libgit2_init();
check_lg2(git_repository_open_ext(&repo, ".", 0, NULL),
"Could not open repository", NULL);
tag_options_init(&opts);
parse_options(&action, &opts, argc, argv);
......@@ -312,8 +306,5 @@ int main(int argc, char **argv)
state.opts = &opts;
action(&state);
git_repository_free(repo);
git_libgit2_shutdown();
return 0;
}
#!/bin/bash
THIS_FILE="$(readlink -f "$0")"
ROOT="$(dirname "$(dirname "$(dirname "$THIS_FILE")")")"
PROGRAM="$ROOT"/examples/rev-list
LIBDIR="$ROOT"/build
REPO="$ROOT"/tests/resources/testrepo.git
cd "$REPO"
run () {
LD_LIBRARY_PATH="$LIBDIR" "$PROGRAM" "$@"
}
diff -u - <(run --date-order a4a7dce) <<EOF
a4a7dce85cf63874e984719f4fdd239f5145052f
c47800c7266a2be04c571c04d5a6614691ea99bd
9fd738e8f7967c078dceed8190330fc8648ee56a
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
5b5b025afb0b4c913b4c338a42934a3863bf3644
8496071c1b46c854b31185ea97743be6a8774479
EOF
out="$(run --topo-order a4a7dce)"
diff -q - <(echo -n "$out") <<EOF >/dev/null ||
a4a7dce85cf63874e984719f4fdd239f5145052f
c47800c7266a2be04c571c04d5a6614691ea99bd
9fd738e8f7967c078dceed8190330fc8648ee56a
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
5b5b025afb0b4c913b4c338a42934a3863bf3644
8496071c1b46c854b31185ea97743be6a8774479
EOF
diff -u - <(echo "$out") <<EOF
a4a7dce85cf63874e984719f4fdd239f5145052f
9fd738e8f7967c078dceed8190330fc8648ee56a
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
c47800c7266a2be04c571c04d5a6614691ea99bd
5b5b025afb0b4c913b4c338a42934a3863bf3644
8496071c1b46c854b31185ea97743be6a8774479
EOF
diff -u - <(run --date-order --reverse a4a7dce) <<EOF
8496071c1b46c854b31185ea97743be6a8774479
5b5b025afb0b4c913b4c338a42934a3863bf3644
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
9fd738e8f7967c078dceed8190330fc8648ee56a
c47800c7266a2be04c571c04d5a6614691ea99bd
a4a7dce85cf63874e984719f4fdd239f5145052f
EOF
out=$(run --topo-order --reverse a4a7dce)
diff -q - <(echo -n "$out") <<EOF >/dev/null ||
8496071c1b46c854b31185ea97743be6a8774479
5b5b025afb0b4c913b4c338a42934a3863bf3644
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
9fd738e8f7967c078dceed8190330fc8648ee56a
c47800c7266a2be04c571c04d5a6614691ea99bd
a4a7dce85cf63874e984719f4fdd239f5145052f
EOF
diff -u - <(echo "$out") <<EOF
8496071c1b46c854b31185ea97743be6a8774479
5b5b025afb0b4c913b4c338a42934a3863bf3644
c47800c7266a2be04c571c04d5a6614691ea99bd
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
9fd738e8f7967c078dceed8190330fc8648ee56a
a4a7dce85cf63874e984719f4fdd239f5145052f
EOF
out="$(run --date-order --topo-order --reverse --reverse a4a7dce)"
diff -q - <(echo -n "$out") <<EOF >/dev/null ||
a4a7dce85cf63874e984719f4fdd239f5145052f
c47800c7266a2be04c571c04d5a6614691ea99bd
9fd738e8f7967c078dceed8190330fc8648ee56a
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
5b5b025afb0b4c913b4c338a42934a3863bf3644
8496071c1b46c854b31185ea97743be6a8774479
EOF
diff -u - <(echo "$out") <<EOF
a4a7dce85cf63874e984719f4fdd239f5145052f
9fd738e8f7967c078dceed8190330fc8648ee56a
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
c47800c7266a2be04c571c04d5a6614691ea99bd
5b5b025afb0b4c913b4c338a42934a3863bf3644
8496071c1b46c854b31185ea97743be6a8774479
EOF
diff -u - <(run ^9fd738e~2 9fd738e) <<EOF
9fd738e8f7967c078dceed8190330fc8648ee56a
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
EOF
diff -u - <(run --not 9fd738e..9fd738e~2) <<EOF
9fd738e8f7967c078dceed8190330fc8648ee56a
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
EOF
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