Blame examples/common.h

Packit ae9e2a
/*
Packit ae9e2a
 * Utilities library for libgit2 examples
Packit ae9e2a
 *
Packit ae9e2a
 * Written by the libgit2 contributors
Packit ae9e2a
 *
Packit ae9e2a
 * To the extent possible under law, the author(s) have dedicated all copyright
Packit ae9e2a
 * and related and neighboring rights to this software to the public domain
Packit ae9e2a
 * worldwide. This software is distributed without any warranty.
Packit ae9e2a
 *
Packit ae9e2a
 * You should have received a copy of the CC0 Public Domain Dedication along
Packit ae9e2a
 * with this software. If not, see
Packit ae9e2a
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
#include <stdio.h>
Packit ae9e2a
#include <string.h>
Packit ae9e2a
#include <stdlib.h>
Packit ae9e2a
#include <git2.h>
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Check libgit2 error code, printing error to stderr on failure and
Packit ae9e2a
 * exiting the program.
Packit ae9e2a
 */
Packit ae9e2a
extern void check_lg2(int error, const char *message, const char *extra);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Exit the program, printing error to stderr
Packit ae9e2a
 */
Packit ae9e2a
extern void fatal(const char *message, const char *extra);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Check if a string has the given prefix.  Returns 0 if not prefixed
Packit ae9e2a
 * or the length of the prefix if it is.
Packit ae9e2a
 */
Packit ae9e2a
extern size_t is_prefixed(const char *str, const char *pfx);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Match an integer string, returning 1 if matched, 0 if not.
Packit ae9e2a
 */
Packit ae9e2a
extern int is_integer(int *out, const char *str, int allow_negative);
Packit ae9e2a
Packit ae9e2a
struct args_info {
Packit ae9e2a
	int    argc;
Packit ae9e2a
	char **argv;
Packit ae9e2a
	int    pos;
Packit ae9e2a
};
Packit ae9e2a
#define ARGS_INFO_INIT { argc, argv, 0 }
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Check current `args` entry against `opt` string.  If it matches
Packit ae9e2a
 * exactly, take the next arg as a string; if it matches as a prefix with
Packit ae9e2a
 * an equal sign, take the remainder as a string; if value not supplied, 
Packit ae9e2a
 * default value `def` will be given. otherwise return 0.
Packit ae9e2a
 */
Packit ae9e2a
extern int optional_str_arg(
Packit ae9e2a
	const char **out, struct args_info *args, const char *opt, const char *def);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Check current `args` entry against `opt` string.  If it matches
Packit ae9e2a
 * exactly, take the next arg as a string; if it matches as a prefix with
Packit ae9e2a
 * an equal sign, take the remainder as a string; otherwise return 0.
Packit ae9e2a
 */
Packit ae9e2a
extern int match_str_arg(
Packit ae9e2a
	const char **out, struct args_info *args, const char *opt);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Check current `args` entry against `opt` string parsing as uint16.  If
Packit ae9e2a
 * `opt` matches exactly, take the next arg as a uint16_t value; if `opt`
Packit ae9e2a
 * is a prefix (equal sign optional), take the remainder of the arg as a
Packit ae9e2a
 * uint16_t value; otherwise return 0.
Packit ae9e2a
 */
Packit ae9e2a
extern int match_uint16_arg(
Packit ae9e2a
	uint16_t *out, struct args_info *args, const char *opt);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Check current `args` entry against `opt` string parsing as uint32.  If
Packit ae9e2a
 * `opt` matches exactly, take the next arg as a uint16_t value; if `opt`
Packit ae9e2a
 * is a prefix (equal sign optional), take the remainder of the arg as a
Packit ae9e2a
 * uint32_t value; otherwise return 0.
Packit ae9e2a
 */
Packit ae9e2a
extern int match_uint32_arg(
Packit ae9e2a
	uint32_t *out, struct args_info *args, const char *opt);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Check current `args` entry against `opt` string parsing as int.  If
Packit ae9e2a
 * `opt` matches exactly, take the next arg as an int value; if it matches
Packit ae9e2a
 * as a prefix (equal sign optional), take the remainder of the arg as a
Packit ae9e2a
 * int value; otherwise return 0.
Packit ae9e2a
 */
Packit ae9e2a
extern int match_int_arg(
Packit ae9e2a
	int *out, struct args_info *args, const char *opt, int allow_negative);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Basic output function for plain text diff output
Packit ae9e2a
 * Pass `FILE*` such as `stdout` or `stderr` as payload (or NULL == `stdout`)
Packit ae9e2a
 */
Packit ae9e2a
extern int diff_output(
Packit ae9e2a
	const git_diff_delta*, const git_diff_hunk*, const git_diff_line*, void*);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Convert a treeish argument to an actual tree; this will call check_lg2
Packit ae9e2a
 * and exit the program if `treeish` cannot be resolved to a tree
Packit ae9e2a
 */
Packit ae9e2a
extern void treeish_to_tree(
Packit ae9e2a
	git_tree **out, git_repository *repo, const char *treeish);