]> git.proxmox.com Git - libgit2.git/blob - examples/common.h
af42324c23c6e4ab3495984bedeaa2dabcc1a712
[libgit2.git] / examples / common.h
1 /*
2 * Utilities library for libgit2 examples
3 *
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/>.
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 */
24 extern void check_lg2(int error, const char *message, const char *extra);
25
26 /**
27 * Exit the program, printing error to stderr
28 */
29 extern 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 */
35 extern 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 */
40 extern int is_integer(int *out, const char *str, int allow_negative);
41
42 struct args_info {
43 int argc;
44 char **argv;
45 int pos;
46 };
47 #define ARGS_INFO_INIT { argc, argv, 0 }
48
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 */
55 extern int optional_str_arg(
56 const char **out, struct args_info *args, const char *opt, const char *def);
57
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 */
63 extern 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 */
72 extern int match_uint16_arg(
73 uint16_t *out, struct args_info *args, const char *opt);
74
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 */
81 extern int match_uint32_arg(
82 uint32_t *out, struct args_info *args, const char *opt);
83
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 */
90 extern int match_int_arg(
91 int *out, struct args_info *args, const char *opt, int allow_negative);
92
93 /**
94 * Basic output function for plain text diff output
95 * Pass `FILE*` such as `stdout` or `stderr` as payload (or NULL == `stdout`)
96 */
97 extern int diff_output(
98 const git_diff_delta*, const git_diff_hunk*, const git_diff_line*, void*);
99
100 /**
101 * Convert a treeish argument to an actual tree; this will call check_lg2
102 * and exit the program if `treeish` cannot be resolved to a tree
103 */
104 extern void treeish_to_tree(
105 git_tree **out, git_repository *repo, const char *treeish);
106
107 /**
108 * A realloc that exits on failure
109 */
110 extern void *xrealloc(void *oldp, size_t newsz);