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/>.
23 void check_lg2(int error
, const char *message
, const char *extra
)
25 const git_error
*lg2err
;
26 const char *lg2msg
= "", *lg2spacer
= "";
31 if ((lg2err
= git_error_last()) != NULL
&& lg2err
->message
!= NULL
) {
32 lg2msg
= lg2err
->message
;
37 fprintf(stderr
, "%s '%s' [%d]%s%s\n",
38 message
, extra
, error
, lg2spacer
, lg2msg
);
40 fprintf(stderr
, "%s [%d]%s%s\n",
41 message
, error
, lg2spacer
, lg2msg
);
46 void fatal(const char *message
, const char *extra
)
49 fprintf(stderr
, "%s %s\n", message
, extra
);
51 fprintf(stderr
, "%s\n", message
);
57 const git_diff_delta
*d
,
58 const git_diff_hunk
*h
,
59 const git_diff_line
*l
,
69 if (l
->origin
== GIT_DIFF_LINE_CONTEXT
||
70 l
->origin
== GIT_DIFF_LINE_ADDITION
||
71 l
->origin
== GIT_DIFF_LINE_DELETION
)
74 fwrite(l
->content
, 1, l
->content_len
, fp
);
80 git_tree
**out
, git_repository
*repo
, const char *treeish
)
82 git_object
*obj
= NULL
;
85 git_revparse_single(&obj
, repo
, treeish
),
86 "looking up object", treeish
);
89 git_object_peel((git_object
**)out
, obj
, GIT_OBJECT_TREE
),
90 "resolving object to tree", treeish
);
95 void *xrealloc(void *oldp
, size_t newsz
)
97 void *p
= realloc(oldp
, newsz
);
99 fprintf(stderr
, "Cannot allocate memory, exiting.\n");
105 int resolve_refish(git_annotated_commit
**commit
, git_repository
*repo
, const char *refish
)
111 assert(commit
!= NULL
);
113 err
= git_reference_dwim(&ref
, repo
, refish
);
115 git_annotated_commit_from_ref(commit
, repo
, ref
);
116 git_reference_free(ref
);
120 err
= git_revparse_single(&obj
, repo
, refish
);
122 err
= git_annotated_commit_lookup(commit
, repo
, git_object_id(obj
));
123 git_object_free(obj
);
129 static int readline(char **out
)
131 int c
, error
= 0, length
= 0, allocated
= 0;
136 while ((c
= getchar()) != EOF
) {
137 if (length
== allocated
) {
140 if ((line
= realloc(line
, allocated
)) == NULL
) {
166 static int ask(char **out
, const char *prompt
, char optional
)
168 printf("%s ", prompt
);
171 if (!readline(out
) && !optional
) {
172 fprintf(stderr
, "Could not read response: %s", strerror(errno
));
179 int cred_acquire_cb(git_credential
**out
,
181 const char *username_from_url
,
182 unsigned int allowed_types
,
185 char *username
= NULL
, *password
= NULL
, *privkey
= NULL
, *pubkey
= NULL
;
191 if (username_from_url
) {
192 if ((username
= strdup(username_from_url
)) == NULL
)
194 } else if ((error
= ask(&username
, "Username:", 0)) < 0) {
198 if (allowed_types
& GIT_CREDENTIAL_SSH_KEY
) {
201 if ((error
= ask(&privkey
, "SSH Key:", 0)) < 0 ||
202 (error
= ask(&password
, "Password:", 1)) < 0)
205 if ((n
= snprintf(NULL
, 0, "%s.pub", privkey
)) < 0 ||
206 (pubkey
= malloc(n
+ 1)) == NULL
||
207 (n
= snprintf(pubkey
, n
+ 1, "%s.pub", privkey
)) < 0)
210 error
= git_credential_ssh_key_new(out
, username
, pubkey
, privkey
, password
);
211 } else if (allowed_types
& GIT_CREDENTIAL_USERPASS_PLAINTEXT
) {
212 if ((error
= ask(&password
, "Password:", 1)) < 0)
215 error
= git_credential_userpass_plaintext_new(out
, username
, password
);
216 } else if (allowed_types
& GIT_CREDENTIAL_USERNAME
) {
217 error
= git_credential_username_new(out
, username
);
228 char *read_file(const char *path
)
235 if ((fd
= open(path
, O_RDONLY
)) < 0 || fstat(fd
, &st
) < 0)
238 if ((buf
= malloc(st
.st_size
+ 1)) == NULL
)
241 while (total
< st
.st_size
) {
242 ssize_t bytes
= read(fd
, buf
+ total
, st
.st_size
- total
);
244 if (errno
== EAGAIN
|| errno
== EINTR
)