forked from libgit2/libgit2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
grabc.c
116 lines (97 loc) · 2.93 KB
/
grabc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <git2.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include "common.h"
static void print_signature(const char *header, const git_signature *sig)
{
char sign;
int offset, hours, minutes;
if (!sig)
return;
offset = sig->when.offset;
if (offset < 0) {
sign = '-';
offset = -offset;
} else {
sign = '+';
}
hours = offset / 60;
minutes = offset % 60;
printf("%s %s <%s> %ld %c%02d%02d\n",
header, sig->name, sig->email, (long)sig->when.time,
sign, hours, minutes);
}
// This is the output on which sha is calculated: prepending commit %n\0
static void show_commit_body (git_commit *commit)
{
unsigned int i, max_i;
char oidstr[GIT_OID_HEXSZ + 1];
git_oid_tostr(oidstr, sizeof(oidstr), git_commit_tree_id(commit));
max_i = (unsigned int)git_commit_parentcount(commit);
for (i = 0; i < max_i; ++i) {
git_oid_tostr(oidstr, sizeof(oidstr), git_commit_parent_id(commit, i));
}
printf("%s", git_commit_raw_header(commit));
if (git_commit_message_raw(commit))
printf("\n%s", git_commit_message_raw(commit));
}
/** Helper to print a commit object. */
static void print_commit(git_commit *commit)
{
// char buf[300*(GIT_OID_HEXSZ + 1)];
char tidstr[GIT_OID_HEXSZ + 1];
char cidstr[GIT_OID_HEXSZ + 1];
const git_signature *sig;
const char *scan, *eol;
git_oid_tostr(tidstr, sizeof(tidstr), git_commit_tree_id(commit));
size_t i, max_i = (unsigned int)git_commit_parentcount(commit);
char * buf = malloc ((max_i+1)*(GIT_OID_HEXSZ + 1));
if (buf == NULL){
fprintf (stderr, "can not allocate %ld parents\n", max_i);
return;
}
//if (max_i > 299){
// fprintf (stderr, "too many parents: %ld\n", max_i);
// max_i = 299;
//}
buf[0]=0;
buf[1]=0;
for (i = 0; i < max_i; ++i) {
char oidstr[GIT_OID_HEXSZ + 1];
git_oid_tostr (oidstr, sizeof(oidstr), git_commit_parent_id(commit, i));
sprintf(buf+i*(GIT_OID_HEXSZ + 1),",%s", oidstr);
}
git_oid_tostr(cidstr, sizeof(cidstr), git_commit_id(commit));
printf("%s;%s;%s;%ld\n", cidstr, tidstr, buf+1, git_commit_time(commit));
show_commit_body (commit);
printf("%s;%s;%s;%ld\n", cidstr, tidstr, buf+1, git_commit_time(commit));
free (buf);
}
int main(int argc, char *argv[])
{
git_repository *repo;
git_object *obj = NULL;
git_libgit2_init();
if (check_lg2 (git_repository_open_bare(&repo, argv[1]),
"Could not open repository", NULL) != 0)
exit (-1);
char *l0 = NULL;
size_t size = 0;
while (getline (&l0, &size, stdin)>=0){
char* l1 = strdup (l0);
l1 [strlen(l1)-1] = 0;
if (obj != NULL) git_object_free(obj);
if (check_lg2(git_revparse_single(&obj, repo, l1), "Could not resolve", l1) != 0){
}else{
if (git_object_type (obj) == GIT_OBJ_COMMIT) {
git_commit *commit = (git_commit*)obj;
print_commit (commit);
}
}
free (l1);
}
git_repository_free (repo);
git_libgit2_shutdown ();
return 0;
}