Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
GIT: libgit2 wrapper for repository operations

Data Structures

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

Macros

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

Functions

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).
 

Detailed Description


Data Structure Documentation

◆ N_GIT_COMMIT_INFO

struct N_GIT_COMMIT_INFO

Commit metadata.

Examples
ex_git.c.

Definition at line 101 of file n_git.h.

+ Collaboration diagram for N_GIT_COMMIT_INFO:
Data Fields
char author[256] author name and email
char hash[42] full hex hash
char message[512] first line of commit message
char short_hash[10] short (abbreviated) hex hash
int64_t timestamp unix timestamp of the commit

◆ N_GIT_REMOTE_AUTH

struct N_GIT_REMOTE_AUTH

Credentials for remote operations.

Definition at line 65 of file n_git.h.

+ Collaboration diagram for N_GIT_REMOTE_AUTH:
Data Fields
char * password password or personal access token, or NULL
char * ssh_passphrase passphrase for SSH key, or NULL
char * ssh_privkey_path path to SSH private key file, or NULL
char * ssh_pubkey_path path to SSH public key file, or NULL (derived from private key + ".pub")
int type auth type: N_GIT_AUTH_NONE/TOKEN/BASIC/SSH
char * username username for basic auth or SSH, or NULL

◆ N_GIT_REPO

struct N_GIT_REPO

Wrapper around a git_repository handle.

Examples
ex_git.c.

Definition at line 81 of file n_git.h.

+ Collaboration diagram for N_GIT_REPO:
Data Fields
char last_status[512] human-readable status message from the last operation
char * path filesystem path to the repository
N_GIT_REMOTE_AUTH * remote_auth credentials for remote operations, or NULL
git_repository * repo libgit2 repository handle

◆ N_GIT_STATUS_ENTRY

struct N_GIT_STATUS_ENTRY

Single file status entry.

Examples
ex_git.c.

Definition at line 93 of file n_git.h.

+ Collaboration diagram for N_GIT_STATUS_ENTRY:
Data Fields
int flags combination of N_GIT_STATUS_* flags
char path[512] relative path of the file

Macro Definition Documentation

◆ N_GIT_AUTH_BASIC

#define N_GIT_AUTH_BASIC   2

Remote auth: username + password.

Definition at line 60 of file n_git.h.

◆ N_GIT_AUTH_NONE

#define N_GIT_AUTH_NONE   0

Remote auth: no authentication.

Definition at line 56 of file n_git.h.

◆ N_GIT_AUTH_SSH

#define N_GIT_AUTH_SSH   3

Remote auth: SSH key file.

Definition at line 62 of file n_git.h.

◆ 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)

Definition at line 58 of file n_git.h.

◆ N_GIT_STATUS_DELETED

#define N_GIT_STATUS_DELETED   4

File has been deleted.

Definition at line 51 of file n_git.h.

◆ N_GIT_STATUS_MODIFIED

#define N_GIT_STATUS_MODIFIED   2

File has been modified.

Definition at line 49 of file n_git.h.

◆ N_GIT_STATUS_NEW

#define N_GIT_STATUS_NEW   1

File is new / untracked.

Definition at line 47 of file n_git.h.

◆ N_GIT_STATUS_STAGED

#define N_GIT_STATUS_STAGED   8

File is staged in the index.

Definition at line 53 of file n_git.h.

Function Documentation

◆ 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.

Compares HEAD against refs/remotes/origin/<branch-name> for the current branch.

Parameters
repoAn opened N_GIT_REPO handle.
aheadOutput: number of local commits not on the remote. May be NULL.
behindOutput: number of remote commits not in local. May be NULL.
Returns
0 on success, -1 on failure (e.g. no tracking branch).

Definition at line 1477 of file n_git.c.

References __n_assert, _n_git_ensure_init(), _n_git_log_error(), _n_git_set_status(), n_git_current_branch(), and N_GIT_REPO::repo.

+ Here is the call graph for this function:

◆ n_git_checkout_commit()

int n_git_checkout_commit ( N_GIT_REPO repo,
const char *  hash 
)

Checkout a specific commit (detached HEAD).

Parameters
repoAn opened N_GIT_REPO handle.
hashHash (or prefix) of the commit to checkout.
Returns
0 on success, -1 on failure.

Definition at line 827 of file n_git.c.

References __n_assert, _n_git_log_error(), and N_GIT_REPO::repo.

+ Here is the call graph for this 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
repoAn opened N_GIT_REPO handle.
filepathRelative path of the file to restore.
Returns
0 on success, -1 on failure.

Definition at line 796 of file n_git.c.

References __n_assert, _n_git_log_error(), _n_git_set_status(), and N_GIT_REPO::repo.

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_git_close()

void n_git_close ( N_GIT_REPO **  repo)

Close a Git repository and free resources.

Parameters
repoPointer to the N_GIT_REPO handle (set to NULL after close).

Definition at line 241 of file n_git.c.

References __n_assert, and FreeNoLog.

Referenced by main().

+ Here is the caller graph for this 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
repoAn opened N_GIT_REPO handle.
messageThe commit message.
author_nameAuthor name string.
author_emailAuthor email string.
Returns
0 on success, -1 on failure.

Definition at line 507 of file n_git.c.

References __n_assert, _n_git_log_error(), _n_git_set_status(), GIT_OID_SHA1_HEXSIZE, and N_GIT_REPO::repo.

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_git_create_branch()

int n_git_create_branch ( N_GIT_REPO repo,
const char *  branch_name 
)

Create a new branch from HEAD.

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

Parameters
repoAn opened N_GIT_REPO handle.
branch_nameName for the new branch.
Returns
0 on success, -1 on failure.

Definition at line 911 of file n_git.c.

References __n_assert, _n_git_log_error(), _n_git_set_status(), LOG_ERR, LOG_INFO, n_log, and N_GIT_REPO::repo.

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this 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
repoAn opened N_GIT_REPO handle.
outBuffer to receive the branch name.
out_sizeSize of the output buffer.
Returns
0 on success, -1 on failure.

Definition at line 1030 of file n_git.c.

References __n_assert, _n_git_log_error(), and N_GIT_REPO::repo.

Referenced by main(), n_git_ahead_behind(), n_git_pull(), and n_git_push().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_git_delete_branch()

int n_git_delete_branch ( N_GIT_REPO repo,
const char *  branch_name 
)

Delete a local branch.

Parameters
repoAn opened N_GIT_REPO handle.
branch_nameName of the branch to delete.
Returns
0 on success, -1 on failure.

Definition at line 1079 of file n_git.c.

References __n_assert, _n_git_log_error(), and N_GIT_REPO::repo.

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this 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
repoAn opened N_GIT_REPO handle.
hash_aHash (or prefix) of the first commit.
hash_bHash (or prefix) of the second commit.
Returns
An N_STR containing the diff text, or NULL on failure.

Definition at line 714 of file n_git.c.

References __n_assert, _n_git_diff_print_cb(), _n_git_log_error(), new_nstr(), and N_GIT_REPO::repo.

+ Here is the call graph for this 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
repoAn opened N_GIT_REPO handle.
Returns
An N_STR containing the diff text, or NULL on failure.

Definition at line 684 of file n_git.c.

References __n_assert, _n_git_diff_print_cb(), _n_git_log_error(), new_nstr(), and N_GIT_REPO::repo.

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_git_fetch()

int n_git_fetch ( N_GIT_REPO repo)

Fetch from the "origin" remote without merging.

Uses credentials set via n_git_set_remote_auth.

Parameters
repoAn opened N_GIT_REPO handle.
Returns
0 on success, -1 on failure.

Definition at line 1438 of file n_git.c.

References __n_assert, _n_git_cred_cb(), _n_git_ensure_init(), _n_git_log_error(), _n_git_set_status(), LOG_INFO, n_log, N_GIT_REPO::remote_auth, and N_GIT_REPO::repo.

+ Here is the call graph for this function:

◆ n_git_init()

N_GIT_REPO * n_git_init ( const char *  path)

Initialize a new Git repository.

Parameters
pathFilesystem path where the repository will be created.
Returns
A new N_GIT_REPO handle, or NULL on failure.

Definition at line 195 of file n_git.c.

References n_git_init_ex().

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this 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
pathFilesystem path where the repository will be created.
initial_branchName of the initial branch (e.g. "main"). If NULL, uses libgit2 default.
Returns
A new N_GIT_REPO handle, or NULL on failure.

Definition at line 205 of file n_git.c.

References __n_assert, _n_git_ensure_init(), _n_git_log_error(), FreeNoLog, LOG_ERR, Malloc, n_log, N_GIT_REPO::path, and N_GIT_REPO::repo.

Referenced by n_git_init().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_git_list_branches()

LIST * n_git_list_branches ( N_GIT_REPO repo)

List all local branch names.

Parameters
repoAn opened N_GIT_REPO handle.
Returns
A LIST of char* branch name strings, or NULL on failure.

Definition at line 868 of file n_git.c.

References __n_assert, _n_git_log_error(), _n_git_string_destroy(), list_push(), new_generic_list(), N_GIT_REPO::repo, and UNLIMITED_LIST_ITEMS.

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_git_log()

LIST * n_git_log ( N_GIT_REPO repo,
size_t  max_entries 
)

Retrieve commit log entries starting from HEAD.

Parameters
repoAn opened N_GIT_REPO handle.
max_entriesMaximum number of log entries to retrieve.
Returns
A LIST of N_GIT_COMMIT_INFO pointers, or NULL on failure.

Definition at line 596 of file n_git.c.

References __n_assert, _n_git_commit_info_destroy(), _n_git_log_error(), N_GIT_COMMIT_INFO::author, GIT_OID_SHA1_HEXSIZE, N_GIT_COMMIT_INFO::hash, list_push(), Malloc, N_GIT_COMMIT_INFO::message, new_generic_list(), N_GIT_REPO::repo, N_GIT_COMMIT_INFO::short_hash, N_GIT_COMMIT_INFO::timestamp, and UNLIMITED_LIST_ITEMS.

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_git_open()

N_GIT_REPO * n_git_open ( const char *  path)

Open an existing Git repository.

Parameters
pathFilesystem path to the repository root (or .git dir).
Returns
A new N_GIT_REPO handle, or NULL on failure.

Definition at line 163 of file n_git.c.

References __n_assert, _n_git_ensure_init(), _n_git_log_error(), FreeNoLog, LOG_ERR, Malloc, n_log, N_GIT_REPO::path, and N_GIT_REPO::repo.

+ Here is the call graph for this function:

◆ n_git_pull()

int n_git_pull ( N_GIT_REPO repo)

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

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

Parameters
repoAn opened N_GIT_REPO handle.
Returns
0 on success, -1 on failure.

Definition at line 1295 of file n_git.c.

References __n_assert, _n_git_cred_cb(), _n_git_ensure_init(), _n_git_log_error(), _n_git_set_status(), LOG_ERR, LOG_INFO, n_git_current_branch(), n_log, N_GIT_REPO::remote_auth, and N_GIT_REPO::repo.

+ Here is the call graph for this function:

◆ n_git_push()

int n_git_push ( N_GIT_REPO repo)

Push the current branch to the "origin" remote.

Uses credentials set via n_git_set_remote_auth.

Parameters
repoAn opened N_GIT_REPO handle.
Returns
0 on success, -1 on failure.

Definition at line 1225 of file n_git.c.

References __n_assert, _n_git_cred_cb(), _n_git_ensure_init(), _n_git_log_error(), _n_git_set_status(), LOG_ERR, LOG_INFO, n_git_current_branch(), n_log, N_GIT_REPO::remote_auth, and N_GIT_REPO::repo.

+ Here is the call graph for this 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.

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

Parameters
repoAn opened N_GIT_REPO handle.
urlRemote URL (HTTPS or SSH).
Returns
0 on success, -1 on failure.

Definition at line 1187 of file n_git.c.

References __n_assert, _n_git_ensure_init(), _n_git_log_error(), and N_GIT_REPO::repo.

+ Here is the call graph for this 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.

The credentials are used by n_git_push and n_git_pull.

Parameters
repoAn opened N_GIT_REPO handle.
authPointer to auth struct. Contents are copied (caller can free).
Returns
0 on success, -1 on failure.

Definition at line 1154 of file n_git.c.

References __n_assert, FreeNoLog, Malloc, N_GIT_REMOTE_AUTH::password, N_GIT_REPO::remote_auth, 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.

◆ n_git_stage()

int n_git_stage ( N_GIT_REPO repo,
const char *  filepath 
)

Stage a file or directory by path.

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).

Parameters
repoAn opened N_GIT_REPO handle.
filepathRelative path of the file or directory to stage.
Returns
0 on success, -1 on failure.

Definition at line 354 of file n_git.c.

References __n_assert, _n_git_log_error(), _n_git_set_status(), and N_GIT_REPO::repo.

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_git_stage_all()

int n_git_stage_all ( N_GIT_REPO repo)

Stage all modified and untracked files.

Parameters
repoAn opened N_GIT_REPO handle.
Returns
0 on success, -1 on failure.

Definition at line 416 of file n_git.c.

References __n_assert, _n_git_log_error(), _n_git_set_status(), and N_GIT_REPO::repo.

+ Here is the call graph for this function:

◆ n_git_status()

LIST * n_git_status ( N_GIT_REPO repo)

Get the status of all files in the working directory.

Parameters
repoAn opened N_GIT_REPO handle.
Returns
A LIST of N_GIT_STATUS_ENTRY pointers, or NULL on failure.

Definition at line 266 of file n_git.c.

References __n_assert, _n_git_log_error(), _n_git_status_entry_destroy(), N_GIT_STATUS_ENTRY::flags, list_push(), Malloc, N_GIT_STATUS_DELETED, N_GIT_STATUS_MODIFIED, N_GIT_STATUS_NEW, N_GIT_STATUS_STAGED, new_generic_list(), N_GIT_STATUS_ENTRY::path, N_GIT_REPO::repo, and UNLIMITED_LIST_ITEMS.

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this 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
repoAn opened N_GIT_REPO handle.
branch_nameName of the branch to switch to.
Returns
0 on success, -1 on failure.

Definition at line 980 of file n_git.c.

References __n_assert, _n_git_log_error(), _n_git_set_status(), and N_GIT_REPO::repo.

+ Here is the call graph for this function:

◆ n_git_unstage()

int n_git_unstage ( N_GIT_REPO repo,
const char *  filepath 
)

Unstage a single file (reset from HEAD).

Parameters
repoAn opened N_GIT_REPO handle.
filepathRelative path of the file to unstage.
Returns
0 on success, -1 on failure.

Definition at line 458 of file n_git.c.

References __n_assert, _n_git_log_error(), _n_git_set_status(), and N_GIT_REPO::repo.

+ Here is the call graph for this function: