Commit b90500f0 by Russell Belfer

Improve docs, examples, warnings

This improves docs in some of the public header files, cleans
up and improves some of the example code, and fixes a couple
of pedantic warnings in places.
parent dbd6850d
.PHONY: all .PHONY: all
CC = gcc CC = gcc
CFLAGS = -g -I../include -I../src CFLAGS = -g -I../include -I../src -Wall -Wextra -Wmissing-prototypes
LFLAGS = -L../build -lgit2 -lz LFLAGS = -L../build -lgit2 -lz
APPS = general showindex diff APPS = general showindex diff
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
void check(int error, const char *message) static void check(int error, const char *message)
{ {
if (error) { if (error) {
fprintf(stderr, "%s (%d)\n", message, error); fprintf(stderr, "%s (%d)\n", message, error);
...@@ -11,7 +11,8 @@ void check(int error, const char *message) ...@@ -11,7 +11,8 @@ void check(int error, const char *message)
} }
} }
int resolve_to_tree(git_repository *repo, const char *identifier, git_tree **tree) static int resolve_to_tree(
git_repository *repo, const char *identifier, git_tree **tree)
{ {
int err = 0; int err = 0;
size_t len = strlen(identifier); size_t len = strlen(identifier);
...@@ -61,16 +62,18 @@ char *colors[] = { ...@@ -61,16 +62,18 @@ char *colors[] = {
"\033[36m" /* cyan */ "\033[36m" /* cyan */
}; };
int printer( static int printer(
void *data, void *data,
git_diff_delta *delta, const git_diff_delta *delta,
git_diff_range *range, const git_diff_range *range,
char usage, char usage,
const char *line, const char *line,
size_t line_len) size_t line_len)
{ {
int *last_color = data, color = 0; int *last_color = data, color = 0;
(void)delta; (void)range; (void)line_len;
if (*last_color >= 0) { if (*last_color >= 0) {
switch (usage) { switch (usage) {
case GIT_DIFF_LINE_ADDITION: color = 3; break; case GIT_DIFF_LINE_ADDITION: color = 3; break;
...@@ -93,7 +96,7 @@ int printer( ...@@ -93,7 +96,7 @@ int printer(
return 0; return 0;
} }
int check_uint16_param(const char *arg, const char *pattern, uint16_t *val) static int check_uint16_param(const char *arg, const char *pattern, uint16_t *val)
{ {
size_t len = strlen(pattern); size_t len = strlen(pattern);
uint16_t strval; uint16_t strval;
...@@ -107,7 +110,7 @@ int check_uint16_param(const char *arg, const char *pattern, uint16_t *val) ...@@ -107,7 +110,7 @@ int check_uint16_param(const char *arg, const char *pattern, uint16_t *val)
return 1; return 1;
} }
int check_str_param(const char *arg, const char *pattern, char **val) static int check_str_param(const char *arg, const char *pattern, char **val)
{ {
size_t len = strlen(pattern); size_t len = strlen(pattern);
if (strncmp(arg, pattern, len)) if (strncmp(arg, pattern, len))
...@@ -116,7 +119,7 @@ int check_str_param(const char *arg, const char *pattern, char **val) ...@@ -116,7 +119,7 @@ int check_str_param(const char *arg, const char *pattern, char **val)
return 1; return 1;
} }
void usage(const char *message, const char *arg) static void usage(const char *message, const char *arg)
{ {
if (message && arg) if (message && arg)
fprintf(stderr, "%s: %s\n", message, arg); fprintf(stderr, "%s: %s\n", message, arg);
...@@ -128,14 +131,15 @@ void usage(const char *message, const char *arg) ...@@ -128,14 +131,15 @@ void usage(const char *message, const char *arg)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char path[GIT_PATH_MAX];
git_repository *repo = NULL; git_repository *repo = NULL;
git_tree *t1 = NULL, *t2 = NULL; git_tree *t1 = NULL, *t2 = NULL;
git_diff_options opts = {0}; git_diff_options opts;
git_diff_list *diff; git_diff_list *diff;
int i, color = -1, compact = 0, cached = 0; int i, color = -1, compact = 0, cached = 0;
char *a, *dir = ".", *treeish1 = NULL, *treeish2 = NULL; char *a, *dir = ".", *treeish1 = NULL, *treeish2 = NULL;
memset(&opts, 0, sizeof(opts));
/* parse arguments as copied from git-diff */ /* parse arguments as copied from git-diff */
for (i = 1; i < argc; ++i) { for (i = 1; i < argc; ++i) {
......
...@@ -3,42 +3,53 @@ ...@@ -3,42 +3,53 @@
int main (int argc, char** argv) int main (int argc, char** argv)
{ {
git_repository *repo; git_repository *repo;
git_index *index; git_index *index;
unsigned int i, e, ecount; unsigned int i, ecount;
git_index_entry **entries; char *dir = ".";
git_oid oid; char out[41];
out[40] = '\0';
char out[41];
out[40] = '\0'; if (argc > 1)
dir = argv[1];
git_repository_open(&repo, "/opt/libgit2-test/.git"); if (argc > 2) {
fprintf(stderr, "usage: showindex [<repo-dir>]\n");
git_repository_index(&index, repo); return 1;
git_index_read(index); }
ecount = git_index_entrycount(index); if (git_repository_open_ext(&repo, dir, 0, NULL) < 0) {
for (i = 0; i < ecount; ++i) { fprintf(stderr, "could not open repository: %s\n", dir);
git_index_entry *e = git_index_get_byindex(index, i); return 1;
}
oid = e->oid;
git_oid_fmt(out, &oid); git_repository_index(&index, repo);
git_index_read(index);
printf("File Path: %s\n", e->path);
printf(" Stage: %d\n", git_index_entry_stage(e)); ecount = git_index_entrycount(index);
printf(" Blob SHA: %s\n", out); if (!ecount)
printf("File Size: %d\n", (int)e->file_size); printf("Empty index\n");
printf(" Device: %d\n", (int)e->dev);
printf(" Inode: %d\n", (int)e->ino); for (i = 0; i < ecount; ++i) {
printf(" UID: %d\n", (int)e->uid); const git_index_entry *e = git_index_get_byindex(index, i);
printf(" GID: %d\n", (int)e->gid);
printf(" ctime: %d\n", (int)e->ctime.seconds); git_oid_fmt(out, &e->oid);
printf(" mtime: %d\n", (int)e->mtime.seconds);
printf("\n"); printf("File Path: %s\n", e->path);
} printf(" Stage: %d\n", git_index_entry_stage(e));
printf(" Blob SHA: %s\n", out);
git_index_free(index); printf("File Size: %d\n", (int)e->file_size);
printf(" Device: %d\n", (int)e->dev);
git_repository_free(repo); printf(" Inode: %d\n", (int)e->ino);
printf(" UID: %d\n", (int)e->uid);
printf(" GID: %d\n", (int)e->gid);
printf(" ctime: %d\n", (int)e->ctime.seconds);
printf(" mtime: %d\n", (int)e->mtime.seconds);
printf("\n");
}
git_index_free(index);
git_repository_free(repo);
return 0;
} }
...@@ -19,6 +19,16 @@ ...@@ -19,6 +19,16 @@
*/ */
GIT_BEGIN_DECL GIT_BEGIN_DECL
/**
* Status flags for a single file.
*
* A combination of these values will be returned to indicate the status of
* a file. Status compares the working directory, the index, and the
* current HEAD of the repository. The `GIT_STATUS_INDEX` set of flags
* represents the status of file in the index relative to the HEAD, and the
* `GIT_STATUS_WT` set of flags represent the status of the file in the
* working directory relative to the index.
*/
typedef enum { typedef enum {
GIT_STATUS_CURRENT = 0, GIT_STATUS_CURRENT = 0,
...@@ -39,12 +49,16 @@ typedef enum { ...@@ -39,12 +49,16 @@ typedef enum {
/** /**
* Gather file statuses and run a callback for each one. * Gather file statuses and run a callback for each one.
* *
* The callback is passed the path of the file, the status and the data * The callback is passed the path of the file, the status (a combination of
* pointer passed to this function. If the callback returns something other * the `git_status_t` values above) and the `payload` data pointer passed
* than 0, this function will stop looping and return GIT_EUSER. * into this function.
*
* If the callback returns a non-zero value, this function will stop looping
* and return GIT_EUSER.
* *
* @param repo a repository object * @param repo A repository object
* @param callback the function to call on each file * @param callback The function to call on each file
* @param payload Pointer to pass through to callback function
* @return 0 on success, GIT_EUSER on non-zero callback, or error code * @return 0 on success, GIT_EUSER on non-zero callback, or error code
*/ */
GIT_EXTERN(int) git_status_foreach( GIT_EXTERN(int) git_status_foreach(
...@@ -53,7 +67,7 @@ GIT_EXTERN(int) git_status_foreach( ...@@ -53,7 +67,7 @@ GIT_EXTERN(int) git_status_foreach(
void *payload); void *payload);
/** /**
* Select the files on which to report status. * For extended status, select the files on which to report status.
* *
* - GIT_STATUS_SHOW_INDEX_AND_WORKDIR is the default. This is the * - GIT_STATUS_SHOW_INDEX_AND_WORKDIR is the default. This is the
* rough equivalent of `git status --porcelain` where each file * rough equivalent of `git status --porcelain` where each file
...@@ -81,40 +95,55 @@ typedef enum { ...@@ -81,40 +95,55 @@ typedef enum {
/** /**
* Flags to control status callbacks * Flags to control status callbacks
* *
* - GIT_STATUS_OPT_INCLUDE_UNTRACKED says that callbacks should * - GIT_STATUS_OPT_INCLUDE_UNTRACKED says that callbacks should be made
* be made on untracked files. These will only be made if the * on untracked files. These will only be made if the workdir files are
* workdir files are included in the status "show" option. * included in the status "show" option.
* - GIT_STATUS_OPT_INCLUDE_IGNORED says that ignored files should * - GIT_STATUS_OPT_INCLUDE_IGNORED says that ignored files should get
* get callbacks. Again, these callbacks will only be made if * callbacks. Again, these callbacks will only be made if the workdir
* the workdir files are included in the status "show" option. * files are included in the status "show" option. Right now, there is
* Right now, there is no option to include all files in * no option to include all files in directories that are ignored
* directories that are ignored completely. * completely.
* - GIT_STATUS_OPT_INCLUDE_UNMODIFIED indicates that callback * - GIT_STATUS_OPT_INCLUDE_UNMODIFIED indicates that callback should be
* should be made even on unmodified files. * made even on unmodified files.
* - GIT_STATUS_OPT_EXCLUDE_SUBMODULES indicates that directories * - GIT_STATUS_OPT_EXCLUDE_SUBMODULES indicates that directories which
* which appear to be submodules should just be skipped over. * appear to be submodules should just be skipped over.
* - GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS indicates that the * - GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS indicates that the contents of
* contents of untracked directories should be included in the * untracked directories should be included in the status. Normally if
* status. Normally if an entire directory is new, then just * an entire directory is new, then just the top-level directory will be
* the top-level directory will be included (with a trailing * included (with a trailing slash on the entry name). Given this flag,
* slash on the entry name). Given this flag, the directory * the directory itself will not be included, but all the files in it
* itself will not be included, but all the files in it will. * will.
* - GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH indicates that the given * - GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH indicates that the given path
* path will be treated as a literal path, and not as a pathspec. * will be treated as a literal path, and not as a pathspec.
*
* Calling `git_status_foreach()` is like calling the extended version
* with: GIT_STATUS_OPT_INCLUDE_IGNORED, GIT_STATUS_OPT_INCLUDE_UNTRACKED,
* and GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS.
*/ */
typedef enum {
enum {
GIT_STATUS_OPT_INCLUDE_UNTRACKED = (1 << 0), GIT_STATUS_OPT_INCLUDE_UNTRACKED = (1 << 0),
GIT_STATUS_OPT_INCLUDE_IGNORED = (1 << 1), GIT_STATUS_OPT_INCLUDE_IGNORED = (1 << 1),
GIT_STATUS_OPT_INCLUDE_UNMODIFIED = (1 << 2), GIT_STATUS_OPT_INCLUDE_UNMODIFIED = (1 << 2),
GIT_STATUS_OPT_EXCLUDE_SUBMODULES = (1 << 3), GIT_STATUS_OPT_EXCLUDE_SUBMODULES = (1 << 3),
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS = (1 << 4), GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS = (1 << 4),
GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH = (1 << 5), GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH = (1 << 5),
}; } git_status_opt_t;
/** /**
* Options to control how callbacks will be made by * Options to control how `git_status_foreach_ext()` will issue callbacks.
* `git_status_foreach_ext()`. *
* This structure is set so that zeroing it out will give you relatively
* sane defaults.
*
* The `show` value is one of the `git_status_show_t` constants that
* control which files to scan and in what order.
*
* The `flags` value is an OR'ed combination of the `git_status_opt_t`
* values above.
*
* The `pathspec` is an array of path patterns to match (using
* fnmatch-style matching), or just an array of paths to match exactly if
* `GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH` is specified in the flags.
*/ */
typedef struct { typedef struct {
git_status_show_t show; git_status_show_t show;
...@@ -124,6 +153,17 @@ typedef struct { ...@@ -124,6 +153,17 @@ typedef struct {
/** /**
* Gather file status information and run callbacks as requested. * Gather file status information and run callbacks as requested.
*
* This is an extended version of the `git_status_foreach()` API that
* allows for more granular control over which paths will be processed and
* in what order. See the `git_status_options` structure for details
* about the additional controls that this makes available.
*
* @param repo Repository object
* @param opts Status options structure
* @param callback The function to call on each file
* @param payload Pointer to pass through to callback function
* @return 0 on success, GIT_EUSER on non-zero callback, or error code
*/ */
GIT_EXTERN(int) git_status_foreach_ext( GIT_EXTERN(int) git_status_foreach_ext(
git_repository *repo, git_repository *repo,
...@@ -132,14 +172,17 @@ GIT_EXTERN(int) git_status_foreach_ext( ...@@ -132,14 +172,17 @@ GIT_EXTERN(int) git_status_foreach_ext(
void *payload); void *payload);
/** /**
* Get file status for a single file * Get file status for a single file.
* *
* @param status_flags the status value * This is not quite the same as calling `git_status_foreach_ext()` with
* @param repo a repository object * the pathspec set to the specified path.
* @param path the file to retrieve status for, rooted at the repo's workdir *
* @return GIT_EINVALIDPATH when `path` points at a folder, GIT_ENOTFOUND when * @param status_flags The status value for the file
* the file doesn't exist in any of HEAD, the index or the worktree, * @param repo A repository object
* 0 otherwise * @param path The file to retrieve status for, rooted at the repo's workdir
* @return 0 on success, GIT_ENOTFOUND if the file is not found in the HEAD,
* index, and work tree, GIT_EINVALIDPATH if `path` points at a folder,
* GIT_EAMBIGUOUS if "path" matches multiple files, -1 on other error.
*/ */
GIT_EXTERN(int) git_status_file( GIT_EXTERN(int) git_status_file(
unsigned int *status_flags, unsigned int *status_flags,
...@@ -156,9 +199,9 @@ GIT_EXTERN(int) git_status_file( ...@@ -156,9 +199,9 @@ GIT_EXTERN(int) git_status_file(
* One way to think of this is if you were to do "git add ." on the * One way to think of this is if you were to do "git add ." on the
* directory containing the file, would it be added or not? * directory containing the file, would it be added or not?
* *
* @param ignored boolean returning 0 if the file is not ignored, 1 if it is * @param ignored Boolean returning 0 if the file is not ignored, 1 if it is
* @param repo a repository object * @param repo A repository object
* @param path the file to check ignores for, rooted at the repo's workdir. * @param path The file to check ignores for, rooted at the repo's workdir.
* @return 0 if ignore rules could be processed for the file (regardless * @return 0 if ignore rules could be processed for the file (regardless
* of whether it exists or not), or an error < 0 if they could not. * of whether it exists or not), or an error < 0 if they could not.
*/ */
......
...@@ -1949,10 +1949,10 @@ int git_reference_peel( ...@@ -1949,10 +1949,10 @@ int git_reference_peel(
peel_error(error, ref, "Cannot retrieve reference target"); peel_error(error, ref, "Cannot retrieve reference target");
goto cleanup; goto cleanup;
} }
if (target_type == GIT_OBJ_ANY && git_object_type(target) != GIT_OBJ_TAG) if (target_type == GIT_OBJ_ANY && git_object_type(target) != GIT_OBJ_TAG)
error = git_object__dup(peeled, target); error = git_object__dup(peeled, target);
else else
error = git_object_peel(peeled, target, target_type); error = git_object_peel(peeled, target, target_type);
cleanup: cleanup:
......
...@@ -217,7 +217,7 @@ static int get_one_status(const char *path, unsigned int status, void *data) ...@@ -217,7 +217,7 @@ static int get_one_status(const char *path, unsigned int status, void *data)
sfi->count++; sfi->count++;
sfi->status = status; sfi->status = status;
if (sfi->count > 1 || if (sfi->count > 1 ||
(strcmp(sfi->expected, path) != 0 && (strcmp(sfi->expected, path) != 0 &&
p_fnmatch(sfi->expected, path, 0) != 0)) { p_fnmatch(sfi->expected, path, 0) != 0)) {
giterr_set(GITERR_INVALID, giterr_set(GITERR_INVALID,
......
...@@ -18,10 +18,7 @@ void test_network_fetch__cleanup(void) ...@@ -18,10 +18,7 @@ void test_network_fetch__cleanup(void)
static int update_tips(const char *refname, const git_oid *a, const git_oid *b, void *data) static int update_tips(const char *refname, const git_oid *a, const git_oid *b, void *data)
{ {
refname = refname; GIT_UNUSED(refname); GIT_UNUSED(a); GIT_UNUSED(b); GIT_UNUSED(data);
a = a;
b = b;
data = data;
++counter; ++counter;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment