Nilorea Library n_git module test.
#ifdef HAVE_LIBGIT2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
fprintf(stderr,
" -v version\n"
" -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
" -h help\n");
}
while ((
getoptret = getopt(argc, argv,
"hvV:")) != EOF) {
case 'v':
fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
exit(1);
case 'V':
if (!strcmp("LOG_NULL", optarg))
else if (!strcmp("LOG_NOTICE", optarg))
else if (!strcmp("LOG_INFO", optarg))
else if (!strcmp("LOG_ERR", optarg))
else if (!strcmp("LOG_DEBUG", optarg))
else {
fprintf(stderr, "%s is not a valid log level.\n", optarg);
exit(-1);
}
break;
default:
case '?': {
if (optopt == 'V') {
fprintf(stderr, "\n Missing log level\n");
}
exit(1);
}
case 'h': {
exit(1);
}
}
}
}
static int write_file(
const char* dir,
const char* filename,
const char* content) {
char path[1024];
snprintf(path, sizeof(path), "%s/%s", dir, filename);
FILE* fp = fopen(path, "w");
if (!fp) return -1;
fprintf(fp, "%s", content);
fclose(fp);
return 0;
}
char cmd[1088];
snprintf(cmd, sizeof(cmd), "rm -rf %s", dir);
int ret = system(cmd);
(void)ret;
}
int main(
int argc,
char** argv) {
int errors = 0;
char tmpdir[] = "/tmp/nilorea_git_test_XXXXXX";
if (!mkdtemp(tmpdir)) {
return 1;
}
if (!repo) {
return 1;
}
if (
write_file(tmpdir,
"test.txt",
"hello") != 0) {
errors++;
}
errors++;
}
if (
n_git_commit(repo,
"initial commit",
"Test User",
"test@example.org") != 0) {
errors++;
}
{
if (!st) {
errors++;
} else {
errors++;
} else {
}
}
}
if (
write_file(tmpdir,
"test.txt",
"hello world modified") != 0) {
errors++;
}
{
n_log(
LOG_ERR,
"FAIL: diff should be non-empty after modification");
errors++;
} else {
}
}
errors++;
}
{
n_log(
LOG_ERR,
"FAIL: diff should be empty after restore, got: %s", diff->
data);
errors++;
} else {
}
}
{
if (!log_list) {
errors++;
} else {
errors++;
} else {
}
}
}
}
{
char branch[256];
} else {
errors++;
}
}
{
} else {
errors++;
}
if (branches) {
} else {
errors++;
}
} else {
errors++;
}
}
if (errors > 0) {
return 1;
}
return 0;
}
#else
#include <stdio.h>
fprintf(stderr, "ex_git: built without HAVE_LIBGIT2, skipping\n");
return 0;
}
#endif
void process_args(int argc, char **argv)
static int write_file(const char *dir, const char *filename, const char *content)
Write a file inside the repo with given content.
static void cleanup_dir(const char *dir)
Remove a directory tree recursively (simple rm -rf via system).
void n_git_close(N_GIT_REPO **repo)
Close a Git repository and free resources.
int n_git_checkout_path(N_GIT_REPO *repo, const char *filepath)
Restore a single file from HEAD (discard working directory changes).
LIST * n_git_list_branches(N_GIT_REPO *repo)
List all local branch names.
LIST * n_git_status(N_GIT_REPO *repo)
Get the status of all files in the working directory.
LIST * n_git_log(N_GIT_REPO *repo, size_t max_entries)
Retrieve commit log entries starting from HEAD.
int n_git_stage(N_GIT_REPO *repo, const char *filepath)
Stage a single file by path.
int n_git_create_branch(N_GIT_REPO *repo, const char *branch_name)
Create a new branch from HEAD.
int n_git_current_branch(N_GIT_REPO *repo, char *out, size_t out_size)
Get the name of the current branch.
int n_git_commit(N_GIT_REPO *repo, const char *message, const char *author_name, const char *author_email)
Create a commit from the current index.
N_STR * n_git_diff_workdir(N_GIT_REPO *repo)
Get a diff of the working directory against the index.
N_GIT_REPO * n_git_init(const char *path)
Initialize a new Git repository.
int n_git_delete_branch(N_GIT_REPO *repo, const char *branch_name)
Delete a local branch.
Wrapper around a git_repository handle.
void * ptr
void pointer to store
LIST_NODE * start
pointer to the start of the list
size_t nb_items
number of item currently in the list
int list_destroy(LIST **list)
Empty and Free a list container.
Structure of a generic LIST container.
Structure of a generic list node.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_NOTICE
normal but significant condition
#define LOG_NULL
no log output
#define LOG_INFO
informational
size_t written
size of the written data inside the string
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
A box including a string and his lenght.
Common headers and low-level functions & define.
libgit2 wrapper for Git repository operations
List structures and definitions.
N_STR and string function declaration.