43 " -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
51 while ((
getoptret = getopt(argc, argv,
"hvV:")) != EOF) {
54 fprintf(stderr,
"Date de compilation : %s a %s.\n", __DATE__, __TIME__);
57 if (!strcmp(
"LOG_NULL", optarg))
59 else if (!strcmp(
"LOG_NOTICE", optarg))
61 else if (!strcmp(
"LOG_INFO", optarg))
63 else if (!strcmp(
"LOG_ERR", optarg))
65 else if (!strcmp(
"LOG_DEBUG", optarg))
68 fprintf(stderr,
"%s is not a valid log level.\n", optarg);
75 fprintf(stderr,
"\n Missing log level\n");
96static int write_file(
const char* dir,
const char* filename,
const char* content) {
98 snprintf(path,
sizeof(path),
"%s/%s", dir, filename);
99 FILE* fp = fopen(path,
"w");
101 fprintf(fp,
"%s", content);
112 snprintf(cmd,
sizeof(cmd),
"rm -rf %s", dir);
113 int ret = system(cmd);
117int main(
int argc,
char** argv) {
123 char tmpdir[] =
"/tmp/nilorea_git_test_XXXXXX";
124 if (!mkdtemp(tmpdir)) {
140 if (
write_file(tmpdir,
"test.txt",
"hello") != 0) {
155 if (
n_git_commit(repo,
"initial commit",
"Test User",
"test@example.org") != 0) {
180 if (
write_file(tmpdir,
"test.txt",
"hello world modified") != 0) {
190 n_log(
LOG_ERR,
"FAIL: diff should be non-empty after modification");
193 n_log(
LOG_INFO,
"PASS: diff is non-empty after modification");
211 n_log(
LOG_ERR,
"FAIL: diff should be empty after restore, got: %s", diff->
data);
232 if (node && node->
ptr) {
298 fprintf(stderr,
"ex_git: built without HAVE_LIBGIT2, skipping\n");
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).
char short_hash[10]
short (abbreviated) hex hash
char author[256]
author name and email
char message[512]
first line of commit message
char hash[42]
full hex hash
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.