5 Module GIT
gullradriel edited this page 2026-10-09 00:55:18 +02:00

GIT: libgit2 wrapper for repository operations

Structures

Name
struct N_GIT_STATUS_ENTRY
Single file status entry.
struct N_GIT_REPO
Wrapper around a git_repository handle.
struct N_GIT_REMOTE_AUTH
Credentials for remote operations.
struct N_GIT_COMMIT_INFO
Commit metadata.

Functions

Name
int n_git_unstage(N_GIT_REPO * repo, const char * filepath)
Unstage a single file (reset from HEAD).
int n_git_switch_branch(N_GIT_REPO * repo, const char * branch_name)
Switch to an existing local branch.
LIST * n_git_status(N_GIT_REPO * repo)
Get the status of all files in the working directory.
int n_git_stage_all(N_GIT_REPO * repo)
Stage all modified and untracked files.
int n_git_stage(N_GIT_REPO * repo, const char * filepath)
Stage a file or directory by path.
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_remote_set_url(N_GIT_REPO * repo, const char * url)
Ensure a remote named "origin" exists with the given URL.
int n_git_push(N_GIT_REPO * repo)
Push the current branch to the "origin" remote.
int n_git_pull(N_GIT_REPO * repo)
Fetch from "origin" and fast-forward merge the current branch.
N_GIT_REPO * n_git_open(const char * path)
Open an existing Git repository.
LIST * n_git_log(N_GIT_REPO * repo, size_t max_entries)
Retrieve commit log entries starting from HEAD.
LIST * n_git_list_branches(N_GIT_REPO * repo)
List all local branch names.
N_GIT_REPO * n_git_init_ex(const char * path, const char * initial_branch)
Initialize a new Git repository with a custom initial branch name.
N_GIT_REPO * n_git_init(const char * path)
Initialize a new Git repository.
int n_git_fetch(N_GIT_REPO * repo)
Fetch from the "origin" remote without merging.
N_STR * n_git_diff_workdir(N_GIT_REPO * repo)
Get a diff of the working directory against the index.
N_STR * n_git_diff_commits(N_GIT_REPO * repo, const char * hash_a, const char * hash_b)
Get a diff between two commits identified by hash.
int n_git_delete_branch(N_GIT_REPO * repo, const char * branch_name)
Delete a local branch.
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_create_branch(N_GIT_REPO * repo, const char * branch_name)
Create a new branch from HEAD.
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.
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).
int n_git_checkout_commit(N_GIT_REPO * repo, const char * hash)
Checkout a specific commit (detached HEAD).
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.

Defines

Name
N_GIT_STATUS_STAGED
File is staged in the index.
N_GIT_STATUS_NEW
File is new / untracked.
N_GIT_STATUS_MODIFIED
File has been modified.
N_GIT_STATUS_DELETED
File has been deleted.
N_GIT_AUTH_TOKEN
Remote auth: personal access token (used as password with empty or "x-access-token" user).
N_GIT_AUTH_SSH
Remote auth: SSH key file.
N_GIT_AUTH_NONE
Remote auth: no authentication.
N_GIT_AUTH_BASIC
Remote auth: username + password.

Functions Documentation

function n_git_unstage

int n_git_unstage(
    N_GIT_REPO * repo,
    const char * filepath
)

Unstage a single file (reset from HEAD).

Parameters:

  • repo An opened N_GIT_REPO handle.
  • filepath Relative path of the file to unstage.

Return: 0 on success, -1 on failure.

function n_git_switch_branch

int n_git_switch_branch(
    N_GIT_REPO * repo,
    const char * branch_name
)

Switch to an existing local branch.

Parameters:

  • repo An opened N_GIT_REPO handle.
  • branch_name Name of the branch to switch to.

Return: 0 on success, -1 on failure.

function n_git_status

LIST * n_git_status(
    N_GIT_REPO * repo
)

Get the status of all files in the working directory.

Parameters:

Return: A LIST of N_GIT_STATUS_ENTRY pointers, or NULL on failure.

function n_git_stage_all

int n_git_stage_all(
    N_GIT_REPO * repo
)

Stage all modified and untracked files.

Parameters:

Return: 0 on success, -1 on failure.

function n_git_stage

int n_git_stage(
    N_GIT_REPO * repo,
    const char * filepath
)

Stage a file or directory by path.

Parameters:

  • repo An opened N_GIT_REPO handle.
  • filepath Relative path of the file or directory to stage.

Return: 0 on success, -1 on failure.

If filepath ends with '/' it is treated as a directory and all files beneath it are staged via pathspec matching (git_index_add_all). Otherwise a single file is staged via git_index_add_bypath.

Paths should use forward slashes as separators (the convention used by libgit2 on all platforms).

function n_git_set_remote_auth

int n_git_set_remote_auth(
    N_GIT_REPO * repo,
    const N_GIT_REMOTE_AUTH * auth
)

Set remote authentication credentials on a repo handle.

Parameters:

  • repo An opened N_GIT_REPO handle.
  • auth Pointer to auth struct. Contents are copied (caller can free).

Return: 0 on success, -1 on failure.

The credentials are used by n_git_push and n_git_pull.

function n_git_remote_set_url

int n_git_remote_set_url(
    N_GIT_REPO * repo,
    const char * url
)

Ensure a remote named "origin" exists with the given URL.

Parameters:

  • repo An opened N_GIT_REPO handle.
  • url Remote URL (HTTPS or SSH).

Return: 0 on success, -1 on failure.

If "origin" exists with a different URL, it is updated.

function n_git_push

int n_git_push(
    N_GIT_REPO * repo
)

Push the current branch to the "origin" remote.

Parameters:

Return: 0 on success, -1 on failure.

Uses credentials set via n_git_set_remote_auth.

function n_git_pull

int n_git_pull(
    N_GIT_REPO * repo
)

Fetch from "origin" and fast-forward merge the current branch.

Parameters:

Return: 0 on success, -1 on failure.

Uses credentials set via n_git_set_remote_auth. If the merge is not a fast-forward, returns -1 (no conflict resolution).

function n_git_open

N_GIT_REPO * n_git_open(
    const char * path
)

Open an existing Git repository.

Parameters:

  • path Filesystem path to the repository root (or .git dir).

Return: A new N_GIT_REPO handle, or NULL on failure.

function n_git_log

LIST * n_git_log(
    N_GIT_REPO * repo,
    size_t max_entries
)

Retrieve commit log entries starting from HEAD.

Parameters:

  • repo An opened N_GIT_REPO handle.
  • max_entries Maximum number of log entries to retrieve.

Return: A LIST of N_GIT_COMMIT_INFO pointers, or NULL on failure.

function n_git_list_branches

LIST * n_git_list_branches(
    N_GIT_REPO * repo
)

List all local branch names.

Parameters:

Return: A LIST of char* branch name strings, or NULL on failure.

function n_git_init_ex

N_GIT_REPO * n_git_init_ex(
    const char * path,
    const char * initial_branch
)

Initialize a new Git repository with a custom initial branch name.

Parameters:

  • path Filesystem path where the repository will be created.
  • initial_branch Name of the initial branch (e.g. "main"). If NULL, uses libgit2 default.

Return: A new N_GIT_REPO handle, or NULL on failure.

function n_git_init

N_GIT_REPO * n_git_init(
    const char * path
)

Initialize a new Git repository.

Parameters:

  • path Filesystem path where the repository will be created.

Return: A new N_GIT_REPO handle, or NULL on failure.

function n_git_fetch

int n_git_fetch(
    N_GIT_REPO * repo
)

Fetch from the "origin" remote without merging.

Parameters:

Return: 0 on success, -1 on failure.

Uses credentials set via n_git_set_remote_auth.

function n_git_diff_workdir

N_STR * n_git_diff_workdir(
    N_GIT_REPO * repo
)

Get a diff of the working directory against the index.

Parameters:

Return: An N_STR containing the diff text, or NULL on failure.

function n_git_diff_commits

N_STR * n_git_diff_commits(
    N_GIT_REPO * repo,
    const char * hash_a,
    const char * hash_b
)

Get a diff between two commits identified by hash.

Parameters:

  • repo An opened N_GIT_REPO handle.
  • hash_a Hash (or prefix) of the first commit.
  • hash_b Hash (or prefix) of the second commit.

Return: An N_STR containing the diff text, or NULL on failure.

function n_git_delete_branch

int n_git_delete_branch(
    N_GIT_REPO * repo,
    const char * branch_name
)

Delete a local branch.

Parameters:

  • repo An opened N_GIT_REPO handle.
  • branch_name Name of the branch to delete.

Return: 0 on success, -1 on failure.

function n_git_current_branch

int n_git_current_branch(
    N_GIT_REPO * repo,
    char * out,
    size_t out_size
)

Get the name of the current branch.

Parameters:

  • repo An opened N_GIT_REPO handle.
  • out Buffer to receive the branch name.
  • out_size Size of the output buffer.

Return: 0 on success, -1 on failure.

function n_git_create_branch

int n_git_create_branch(
    N_GIT_REPO * repo,
    const char * branch_name
)

Create a new branch from HEAD.

Parameters:

  • repo An opened N_GIT_REPO handle.
  • branch_name Name for the new branch.

Return: 0 on success, -1 on failure.

On an empty repository (no commits yet), this updates HEAD to point to the new branch symbolically (equivalent to git checkout -b).

function n_git_commit

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.

Parameters:

  • repo An opened N_GIT_REPO handle.
  • message The commit message.
  • author_name Author name string.
  • author_email Author email string.

Return: 0 on success, -1 on failure.

function n_git_close

void n_git_close(
    N_GIT_REPO ** repo
)

Close a Git repository and free resources.

Parameters:

  • repo Pointer to the N_GIT_REPO handle (set to NULL after close).

function n_git_checkout_path

int n_git_checkout_path(
    N_GIT_REPO * repo,
    const char * filepath
)

Restore a single file from HEAD (discard working directory changes).

Parameters:

  • repo An opened N_GIT_REPO handle.
  • filepath Relative path of the file to restore.

Return: 0 on success, -1 on failure.

function n_git_checkout_commit

int n_git_checkout_commit(
    N_GIT_REPO * repo,
    const char * hash
)

Checkout a specific commit (detached HEAD).

Parameters:

  • repo An opened N_GIT_REPO handle.
  • hash Hash (or prefix) of the commit to checkout.

Return: 0 on success, -1 on failure.

function n_git_ahead_behind

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.

Parameters:

  • repo An opened N_GIT_REPO handle.
  • ahead Output: number of local commits not on the remote. May be NULL.
  • behind Output: number of remote commits not in local. May be NULL.

Return: 0 on success, -1 on failure (e.g. no tracking branch).

Compares HEAD against refs/remotes/origin/ for the current branch.

Macros Documentation

define N_GIT_STATUS_STAGED

#define N_GIT_STATUS_STAGED 8

File is staged in the index.

define N_GIT_STATUS_NEW

#define N_GIT_STATUS_NEW 1

File is new / untracked.

define N_GIT_STATUS_MODIFIED

#define N_GIT_STATUS_MODIFIED 2

File has been modified.

define N_GIT_STATUS_DELETED

#define N_GIT_STATUS_DELETED 4

File has been deleted.

define N_GIT_AUTH_TOKEN

#define N_GIT_AUTH_TOKEN 1

Remote auth: personal access token (used as password with empty or "x-access-token" user).

define N_GIT_AUTH_SSH

#define N_GIT_AUTH_SSH 3

Remote auth: SSH key file.

define N_GIT_AUTH_NONE

#define N_GIT_AUTH_NONE 0

Remote auth: no authentication.

define N_GIT_AUTH_BASIC

#define N_GIT_AUTH_BASIC 2

Remote auth: username + password.