]> git.proxmox.com Git - libgit2.git/blob - examples/common.h
f86e92c3574c7f46d8335c714a35859b4e24fdcd
[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 #ifndef INCLUDE_examples_common_h__
15 #define INCLUDE_examples_common_h__
16
17 #include <assert.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <git2.h>
24 #include <fcntl.h>
25
26 #ifdef _WIN32
27 # include <io.h>
28 # include <Windows.h>
29 # define open _open
30 # define read _read
31 # define close _close
32 # define ssize_t int
33 # define sleep(a) Sleep(a * 1000)
34 #else
35 # include <unistd.h>
36 #endif
37
38 #ifndef PRIuZ
39 /* Define the printf format specifier to use for size_t output */
40 #if defined(_MSC_VER) || defined(__MINGW32__)
41 # define PRIuZ "Iu"
42 #else
43 # define PRIuZ "zu"
44 #endif
45 #endif
46
47 #ifdef _MSC_VER
48 #define snprintf sprintf_s
49 #define strcasecmp strcmpi
50 #endif
51
52 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
53 #define UNUSED(x) (void)(x)
54
55 #include "args.h"
56
57 extern int lg2_add(git_repository *repo, int argc, char **argv);
58 extern int lg2_blame(git_repository *repo, int argc, char **argv);
59 extern int lg2_cat_file(git_repository *repo, int argc, char **argv);
60 extern int lg2_checkout(git_repository *repo, int argc, char **argv);
61 extern int lg2_clone(git_repository *repo, int argc, char **argv);
62 extern int lg2_commit(git_repository *repo, int argc, char **argv);
63 extern int lg2_config(git_repository *repo, int argc, char **argv);
64 extern int lg2_describe(git_repository *repo, int argc, char **argv);
65 extern int lg2_diff(git_repository *repo, int argc, char **argv);
66 extern int lg2_fetch(git_repository *repo, int argc, char **argv);
67 extern int lg2_for_each_ref(git_repository *repo, int argc, char **argv);
68 extern int lg2_general(git_repository *repo, int argc, char **argv);
69 extern int lg2_index_pack(git_repository *repo, int argc, char **argv);
70 extern int lg2_init(git_repository *repo, int argc, char **argv);
71 extern int lg2_log(git_repository *repo, int argc, char **argv);
72 extern int lg2_ls_files(git_repository *repo, int argc, char **argv);
73 extern int lg2_ls_remote(git_repository *repo, int argc, char **argv);
74 extern int lg2_merge(git_repository *repo, int argc, char **argv);
75 extern int lg2_push(git_repository *repo, int argc, char **argv);
76 extern int lg2_remote(git_repository *repo, int argc, char **argv);
77 extern int lg2_rev_list(git_repository *repo, int argc, char **argv);
78 extern int lg2_rev_parse(git_repository *repo, int argc, char **argv);
79 extern int lg2_show_index(git_repository *repo, int argc, char **argv);
80 extern int lg2_stash(git_repository *repo, int argc, char **argv);
81 extern int lg2_status(git_repository *repo, int argc, char **argv);
82 extern int lg2_tag(git_repository *repo, int argc, char **argv);
83
84 /**
85 * Check libgit2 error code, printing error to stderr on failure and
86 * exiting the program.
87 */
88 extern void check_lg2(int error, const char *message, const char *extra);
89
90 /**
91 * Read a file into a buffer
92 *
93 * @param path The path to the file that shall be read
94 * @return NUL-terminated buffer if the file was successfully read, NULL-pointer otherwise
95 */
96 extern char *read_file(const char *path);
97
98 /**
99 * Exit the program, printing error to stderr
100 */
101 extern void fatal(const char *message, const char *extra);
102
103 /**
104 * Basic output function for plain text diff output
105 * Pass `FILE*` such as `stdout` or `stderr` as payload (or NULL == `stdout`)
106 */
107 extern int diff_output(
108 const git_diff_delta*, const git_diff_hunk*, const git_diff_line*, void*);
109
110 /**
111 * Convert a treeish argument to an actual tree; this will call check_lg2
112 * and exit the program if `treeish` cannot be resolved to a tree
113 */
114 extern void treeish_to_tree(
115 git_tree **out, git_repository *repo, const char *treeish);
116
117 /**
118 * A realloc that exits on failure
119 */
120 extern void *xrealloc(void *oldp, size_t newsz);
121
122 /**
123 * Convert a refish to an annotated commit.
124 */
125 extern int resolve_refish(git_annotated_commit **commit, git_repository *repo, const char *refish);
126
127 /**
128 * Acquire credentials via command line
129 */
130 extern int cred_acquire_cb(git_credential **out,
131 const char *url,
132 const char *username_from_url,
133 unsigned int allowed_types,
134 void *payload);
135
136 #endif