]> git.proxmox.com Git - libgit2.git/blame - examples/common.h
New upstream version 0.28.4+dfsg.1
[libgit2.git] / examples / common.h
CommitLineData
66902d47 1/*
6cb831bd 2 * Utilities library for libgit2 examples
66902d47 3 *
6cb831bd
BS
4 * Written by the libgit2 contributors
5 *
6 * To the extent possible under law, the author(s) have dedicated all copyright
7 * and related and neighboring rights to this software to the public domain
8 * worldwide. This software is distributed without any warranty.
9 *
10 * You should have received a copy of the CC0 Public Domain Dedication along
11 * with this software. If not, see
12 * <http://creativecommons.org/publicdomain/zero/1.0/>.
66902d47
RB
13 */
14
15#include <stdio.h>
16#include <string.h>
17#include <stdlib.h>
18#include <git2.h>
19
20/**
21 * Check libgit2 error code, printing error to stderr on failure and
22 * exiting the program.
23 */
24extern void check_lg2(int error, const char *message, const char *extra);
25
26/**
27 * Exit the program, printing error to stderr
28 */
29extern void fatal(const char *message, const char *extra);
30
31/**
32 * Check if a string has the given prefix. Returns 0 if not prefixed
33 * or the length of the prefix if it is.
34 */
35extern size_t is_prefixed(const char *str, const char *pfx);
36
37/**
38 * Match an integer string, returning 1 if matched, 0 if not.
39 */
40extern int is_integer(int *out, const char *str, int allow_negative);
41
42struct args_info {
43 int argc;
44 char **argv;
45 int pos;
46};
47#define ARGS_INFO_INIT { argc, argv, 0 }
48
d6bbcefc
L
49/**
50 * Check current `args` entry against `opt` string. If it matches
51 * exactly, take the next arg as a string; if it matches as a prefix with
52 * an equal sign, take the remainder as a string; if value not supplied,
53 * default value `def` will be given. otherwise return 0.
54 */
55extern int optional_str_arg(
56 const char **out, struct args_info *args, const char *opt, const char *def);
57
66902d47
RB
58/**
59 * Check current `args` entry against `opt` string. If it matches
60 * exactly, take the next arg as a string; if it matches as a prefix with
61 * an equal sign, take the remainder as a string; otherwise return 0.
62 */
63extern int match_str_arg(
64 const char **out, struct args_info *args, const char *opt);
65
66/**
67 * Check current `args` entry against `opt` string parsing as uint16. If
68 * `opt` matches exactly, take the next arg as a uint16_t value; if `opt`
69 * is a prefix (equal sign optional), take the remainder of the arg as a
70 * uint16_t value; otherwise return 0.
71 */
72extern int match_uint16_arg(
73 uint16_t *out, struct args_info *args, const char *opt);
74
5c2a8361
PS
75/**
76 * Check current `args` entry against `opt` string parsing as uint32. If
77 * `opt` matches exactly, take the next arg as a uint16_t value; if `opt`
78 * is a prefix (equal sign optional), take the remainder of the arg as a
79 * uint32_t value; otherwise return 0.
80 */
81extern int match_uint32_arg(
82 uint32_t *out, struct args_info *args, const char *opt);
83
66902d47
RB
84/**
85 * Check current `args` entry against `opt` string parsing as int. If
86 * `opt` matches exactly, take the next arg as an int value; if it matches
87 * as a prefix (equal sign optional), take the remainder of the arg as a
88 * int value; otherwise return 0.
89 */
90extern int match_int_arg(
91 int *out, struct args_info *args, const char *opt, int allow_negative);
92
ac3d33df
JK
93/**
94 * Check current `args` entry against a "bool" `opt` (ie. --[no-]progress).
95 * If `opt` matches positively, out will be set to 1, or if `opt` matches
96 * negatively, out will be set to 0, and in both cases 1 will be returned.
97 * If neither the positive or the negative form of opt matched, out will be -1,
98 * and 0 will be returned.
99 */
100extern int match_bool_arg(int *out, struct args_info *args, const char *opt);
101
66902d47
RB
102/**
103 * Basic output function for plain text diff output
104 * Pass `FILE*` such as `stdout` or `stderr` as payload (or NULL == `stdout`)
105 */
106extern int diff_output(
107 const git_diff_delta*, const git_diff_hunk*, const git_diff_line*, void*);
108
109/**
110 * Convert a treeish argument to an actual tree; this will call check_lg2
111 * and exit the program if `treeish` cannot be resolved to a tree
112 */
113extern void treeish_to_tree(
114 git_tree **out, git_repository *repo, const char *treeish);
eae0bfdc
PP
115
116/**
117 * A realloc that exits on failure
118 */
119extern void *xrealloc(void *oldp, size_t newsz);
ac3d33df
JK
120
121/**
122 * Convert a refish to an annotated commit.
123 */
124extern int resolve_refish(git_annotated_commit **commit, git_repository *repo, const char *refish);