Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_git.c File Reference

libgit2 wrapper for Git repository operations More...

#include "nilorea/n_git.h"
#include "nilorea/n_log.h"
#include "nilorea/n_common.h"
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
+ Include dependency graph for n_git.c:

Go to the source code of this file.

Macros

#define GIT_OID_SHA1_HEXSIZE   GIT_OID_HEXSZ
 

Functions

static void _n_git_commit_info_destroy (void *ptr)
 Destructor for N_GIT_COMMIT_INFO used by LIST.
 
static int _n_git_cred_cb (git_credential **out, const char *url, const char *username_from_url, unsigned int allowed_types, void *payload)
 libgit2 credential callback for remote operations.
 
static int _n_git_diff_print_cb (const git_diff_delta *delta, const git_diff_hunk *hunk, const git_diff_line *line, void *payload)
 Callback for git_diff_print, appends each line to an N_STR.
 
static void _n_git_ensure_init (void)
 Ensure libgit2 is initialized (idempotent).
 
static void _n_git_log_error (const char *func_name)
 Log the last libgit2 error.
 
static void _n_git_set_status (N_GIT_REPO *repo, const char *fmt,...)
 Set the last_status message on a repo handle.
 
static void _n_git_status_entry_destroy (void *ptr)
 Destructor for N_GIT_STATUS_ENTRY used by LIST.
 
static void _n_git_string_destroy (void *ptr)
 Destructor for char* branch name strings used by LIST.
 
int n_git_ahead_behind (N_GIT_REPO *repo, size_t *ahead, size_t *behind)
 Count commits ahead and behind relative to the remote tracking branch.
 
int n_git_checkout_commit (N_GIT_REPO *repo, const char *hash)
 Checkout a specific commit (detached HEAD).
 
int n_git_checkout_path (N_GIT_REPO *repo, const char *filepath)
 Restore a single file from HEAD (discard working directory changes).
 
void n_git_close (N_GIT_REPO **repo)
 Close a Git repository and free resources.
 
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.
 
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_delete_branch (N_GIT_REPO *repo, const char *branch_name)
 Delete a local branch.
 
N_STRn_git_diff_commits (N_GIT_REPO *repo, const char *hash_a, const char *hash_b)
 Get a diff between two commits identified by hash.
 
N_STRn_git_diff_workdir (N_GIT_REPO *repo)
 Get a diff of the working directory against the index.
 
int n_git_fetch (N_GIT_REPO *repo)
 Fetch from the "origin" remote without merging.
 
N_GIT_REPOn_git_init (const char *path)
 Initialize a new Git repository.
 
N_GIT_REPOn_git_init_ex (const char *path, const char *initial_branch)
 Initialize a new Git repository with a custom initial branch name.
 
LISTn_git_list_branches (N_GIT_REPO *repo)
 List all local branch names.
 
LISTn_git_log (N_GIT_REPO *repo, size_t max_entries)
 Retrieve commit log entries starting from HEAD.
 
N_GIT_REPOn_git_open (const char *path)
 Open an existing Git repository.
 
int n_git_pull (N_GIT_REPO *repo)
 Fetch from "origin" and fast-forward merge the current branch.
 
int n_git_push (N_GIT_REPO *repo)
 Push the current branch to the "origin" remote.
 
int n_git_remote_set_url (N_GIT_REPO *repo, const char *url)
 Ensure a remote named "origin" exists with the given URL.
 
int n_git_set_remote_auth (N_GIT_REPO *repo, const N_GIT_REMOTE_AUTH *auth)
 Set remote authentication credentials on a repo handle.
 
int n_git_stage (N_GIT_REPO *repo, const char *filepath)
 Stage a file or directory by path.
 
int n_git_stage_all (N_GIT_REPO *repo)
 Stage all modified and untracked files.
 
LISTn_git_status (N_GIT_REPO *repo)
 Get the status of all files in the working directory.
 
int n_git_switch_branch (N_GIT_REPO *repo, const char *branch_name)
 Switch to an existing local branch.
 
int n_git_unstage (N_GIT_REPO *repo, const char *filepath)
 Unstage a single file (reset from HEAD).
 

Variables

static int _n_git_initialized = 0
 static flag ensuring git_libgit2_init is called only once
 

Detailed Description

libgit2 wrapper for Git repository operations

Author
Castagnier Mickael
Version
1.0
Date
26/03/2026

Definition in file n_git.c.

Macro Definition Documentation

◆ GIT_OID_SHA1_HEXSIZE

#define GIT_OID_SHA1_HEXSIZE   GIT_OID_HEXSZ

Definition at line 47 of file n_git.c.

Function Documentation

◆ _n_git_commit_info_destroy()

static void _n_git_commit_info_destroy ( void *  ptr)
static

Destructor for N_GIT_COMMIT_INFO used by LIST.

Parameters
ptrPointer to the entry to free.

Definition at line 119 of file n_git.c.

References FreeNoLog.

Referenced by n_git_log().

+ Here is the caller graph for this function:

◆ _n_git_cred_cb()

static int _n_git_cred_cb ( git_credential **  out,
const char *  url,
const char *  username_from_url,
unsigned int  allowed_types,
void *  payload 
)
static

libgit2 credential callback for remote operations.

Definition at line 1108 of file n_git.c.

References N_GIT_AUTH_BASIC, N_GIT_AUTH_SSH, N_GIT_AUTH_TOKEN, N_GIT_REMOTE_AUTH::password, N_GIT_REMOTE_AUTH::ssh_passphrase, N_GIT_REMOTE_AUTH::ssh_privkey_path, N_GIT_REMOTE_AUTH::ssh_pubkey_path, N_GIT_REMOTE_AUTH::type, and N_GIT_REMOTE_AUTH::username.

Referenced by n_git_fetch(), n_git_pull(), and n_git_push().

+ Here is the caller graph for this function:

◆ _n_git_diff_print_cb()

static int _n_git_diff_print_cb ( const git_diff_delta *  delta,
const git_diff_hunk *  hunk,
const git_diff_line *  line,
void *  payload 
)
static

Callback for git_diff_print, appends each line to an N_STR.

Parameters
deltaUnused.
hunkUnused.
lineThe diff line to append.
payloadPointer to the N_STR* accumulator.
Returns
0 on success.

Definition at line 141 of file n_git.c.

References FreeNoLog, Malloc, and nstrprintf_cat.

Referenced by n_git_diff_commits(), and n_git_diff_workdir().

+ Here is the caller graph for this function:

◆ _n_git_ensure_init()

static void _n_git_ensure_init ( void  )
static

Ensure libgit2 is initialized (idempotent).

Definition at line 56 of file n_git.c.

References _n_git_initialized.

Referenced by n_git_ahead_behind(), n_git_fetch(), n_git_init_ex(), n_git_open(), n_git_pull(), n_git_push(), and n_git_remote_set_url().

+ Here is the caller graph for this function:

◆ _n_git_log_error()

static void _n_git_log_error ( const char *  func_name)
static

◆ _n_git_set_status()

static void _n_git_set_status ( N_GIT_REPO repo,
const char *  fmt,
  ... 
)
static

Set the last_status message on a repo handle.

Parameters
repoThe repo handle (may be NULL).
fmtprintf-style format string.

Definition at line 93 of file n_git.c.

References N_GIT_REPO::last_status.

Referenced by n_git_ahead_behind(), n_git_checkout_path(), n_git_commit(), n_git_create_branch(), n_git_fetch(), n_git_pull(), n_git_push(), n_git_stage(), n_git_stage_all(), n_git_switch_branch(), and n_git_unstage().

+ Here is the caller graph for this function:

◆ _n_git_status_entry_destroy()

static void _n_git_status_entry_destroy ( void *  ptr)
static

Destructor for N_GIT_STATUS_ENTRY used by LIST.

Parameters
ptrPointer to the entry to free.

Definition at line 110 of file n_git.c.

References FreeNoLog.

Referenced by n_git_status().

+ Here is the caller graph for this function:

◆ _n_git_string_destroy()

static void _n_git_string_destroy ( void *  ptr)
static

Destructor for char* branch name strings used by LIST.

Parameters
ptrPointer to the string to free.

Definition at line 128 of file n_git.c.

References FreeNoLog.

Referenced by n_git_list_branches().

+ Here is the caller graph for this function:

Variable Documentation

◆ _n_git_initialized

int _n_git_initialized = 0
static

static flag ensuring git_libgit2_init is called only once

Definition at line 51 of file n_git.c.

Referenced by _n_git_ensure_init().