]>
git.proxmox.com Git - libgit2.git/blob - examples/common.c
e20767a9e942bb32156a318d64f4b492b63858e4
2 * Utilities library for libgit2 examples
4 * Written by the libgit2 contributors
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.
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/>.
19 void check_lg2(int error
, const char *message
, const char *extra
)
21 const git_error
*lg2err
;
22 const char *lg2msg
= "", *lg2spacer
= "";
27 if ((lg2err
= git_error_last()) != NULL
&& lg2err
->message
!= NULL
) {
28 lg2msg
= lg2err
->message
;
33 fprintf(stderr
, "%s '%s' [%d]%s%s\n",
34 message
, extra
, error
, lg2spacer
, lg2msg
);
36 fprintf(stderr
, "%s [%d]%s%s\n",
37 message
, error
, lg2spacer
, lg2msg
);
42 void fatal(const char *message
, const char *extra
)
45 fprintf(stderr
, "%s %s\n", message
, extra
);
47 fprintf(stderr
, "%s\n", message
);
52 size_t is_prefixed(const char *str
, const char *pfx
)
54 size_t len
= strlen(pfx
);
55 return strncmp(str
, pfx
, len
) ? 0 : len
;
59 const char **out
, struct args_info
*args
, const char *opt
, const char *def
)
61 const char *found
= args
->argv
[args
->pos
];
62 size_t len
= is_prefixed(found
, opt
);
68 if (args
->pos
+ 1 == args
->argc
) {
73 *out
= args
->argv
[args
->pos
];
77 if (found
[len
] == '=') {
78 *out
= found
+ len
+ 1;
86 const char **out
, struct args_info
*args
, const char *opt
)
88 const char *found
= args
->argv
[args
->pos
];
89 size_t len
= is_prefixed(found
, opt
);
95 if (args
->pos
+ 1 == args
->argc
)
96 fatal("expected value following argument", opt
);
98 *out
= args
->argv
[args
->pos
];
102 if (found
[len
] == '=') {
103 *out
= found
+ len
+ 1;
110 static const char *match_numeric_arg(struct args_info
*args
, const char *opt
)
112 const char *found
= args
->argv
[args
->pos
];
113 size_t len
= is_prefixed(found
, opt
);
119 if (args
->pos
+ 1 == args
->argc
)
120 fatal("expected numeric value following argument", opt
);
122 found
= args
->argv
[args
->pos
];
132 int match_uint16_arg(
133 uint16_t *out
, struct args_info
*args
, const char *opt
)
135 const char *found
= match_numeric_arg(args
, opt
);
142 val
= (uint16_t)strtoul(found
, &endptr
, 0);
143 if (!endptr
|| *endptr
!= '\0')
144 fatal("expected number after argument", opt
);
151 int match_uint32_arg(
152 uint32_t *out
, struct args_info
*args
, const char *opt
)
154 const char *found
= match_numeric_arg(args
, opt
);
161 val
= (uint32_t)strtoul(found
, &endptr
, 0);
162 if (!endptr
|| *endptr
!= '\0')
163 fatal("expected number after argument", opt
);
170 static int match_int_internal(
171 int *out
, const char *str
, int allow_negative
, const char *opt
)
174 int val
= (int)strtol(str
, &endptr
, 10);
176 if (!endptr
|| *endptr
!= '\0')
177 fatal("expected number", opt
);
178 else if (val
< 0 && !allow_negative
)
179 fatal("negative values are not allowed", opt
);
187 int match_bool_arg(int *out
, struct args_info
*args
, const char *opt
)
189 const char *found
= args
->argv
[args
->pos
];
191 if (!strcmp(found
, opt
)) {
196 if (!strncmp(found
, "--no-", strlen("--no-")) &&
197 !strcmp(found
+ strlen("--no-"), opt
+ 2)) {
206 int is_integer(int *out
, const char *str
, int allow_negative
)
208 return match_int_internal(out
, str
, allow_negative
, NULL
);
212 int *out
, struct args_info
*args
, const char *opt
, int allow_negative
)
214 const char *found
= match_numeric_arg(args
, opt
);
217 return match_int_internal(out
, found
, allow_negative
, opt
);
221 const git_diff_delta
*d
,
222 const git_diff_hunk
*h
,
223 const git_diff_line
*l
,
233 if (l
->origin
== GIT_DIFF_LINE_CONTEXT
||
234 l
->origin
== GIT_DIFF_LINE_ADDITION
||
235 l
->origin
== GIT_DIFF_LINE_DELETION
)
236 fputc(l
->origin
, fp
);
238 fwrite(l
->content
, 1, l
->content_len
, fp
);
243 void treeish_to_tree(
244 git_tree
**out
, git_repository
*repo
, const char *treeish
)
246 git_object
*obj
= NULL
;
249 git_revparse_single(&obj
, repo
, treeish
),
250 "looking up object", treeish
);
253 git_object_peel((git_object
**)out
, obj
, GIT_OBJECT_TREE
),
254 "resolving object to tree", treeish
);
256 git_object_free(obj
);
259 void *xrealloc(void *oldp
, size_t newsz
)
261 void *p
= realloc(oldp
, newsz
);
263 fprintf(stderr
, "Cannot allocate memory, exiting.\n");
269 int resolve_refish(git_annotated_commit
**commit
, git_repository
*repo
, const char *refish
)
275 assert(commit
!= NULL
);
277 err
= git_reference_dwim(&ref
, repo
, refish
);
279 git_annotated_commit_from_ref(commit
, repo
, ref
);
280 git_reference_free(ref
);
284 err
= git_revparse_single(&obj
, repo
, refish
);
286 err
= git_annotated_commit_lookup(commit
, repo
, git_object_id(obj
));
287 git_object_free(obj
);