-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparrotgit.c
213 lines (192 loc) · 5.31 KB
/
parrotgit.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <git2.h>
#include <stdio.h>
#include <string.h>
git_repository * open_repo(char * dir){
git_repository *repo = NULL;
int ret;
ret = git_repository_open_ext(&repo, dir, 0, NULL);
if (ret < 0){
fprintf(stderr, "Error opening repository\n");
}
return repo;
}
git_repository * init_repository(char * dir){
git_repository *repo;
int ret;
ret = git_repository_init(&repo, dir, 0);
if (ret < 0){
fprintf(stderr, "Error initializing repository\n");
}
return repo;
}
git_repository * init_repository2(char * dir, unsigned bare){
git_repository *repo;
int ret;
ret = git_repository_init(&repo, dir, bare);
if (ret < 0){
fprintf(stderr, "Error initializing repository\n");
}
return repo;
}
git_index * repo_index(git_repository * repo){
git_index *index;
int ret;
ret = git_repository_index(&index, repo);
if (ret < 0)
fprintf(stderr, "Error opening repository index\n");
return index;
}
git_reference * repo_head(git_repository * repo){
git_reference * head = NULL;
int error = 0;
error = git_repository_head(&head, repo);
if (error){
fprintf(stderr, "Orphaned Head or not found.\n");
}
return head;
}
git_config * get_config(char * config_path){
git_config * config;
int ret = 0;
ret = git_config_open_ondisk(&config, config_path);
if (ret < 0){
fprintf(stderr, "Error getting config file\n");
}
return config;
}
git_commit * commit_lookup(git_repository * repo, git_oid * oid){
int ret;
git_commit * commit;
ret = git_commit_lookup(&commit, repo, oid);
if (ret < 0){
fprintf(stderr, "Error looking up commit\n");
}
return commit;
}
git_commit * parent(git_commit * commit, int p){
git_commit *parent;
git_commit_parent(&parent, commit, p);
return parent;
}
const char * branchname(git_repository * repo){
git_reference * head;
head = repo_head(repo);
const char * branchname;
branchname = git_reference_name(head);
return branchname;
}
git_revwalk * new_revwalk(git_repository * repo){
git_revwalk * new;
git_revwalk_new(&new, repo);
return new;
}
git_blob * blob_lookup(git_repository * repo, git_oid * oid){
git_blob * blob;
git_blob_lookup(&blob, repo, oid);
return blob;
}
int get_config_int32(git_config * config, char * cfg_variable){
int j;
git_config_get_int32(&j, config, cfg_variable);
return j;
}
const char * get_config_string(git_config * config, char * cfg_variable){
const char * cfgstring;
int ret;
ret = git_config_get_string(&cfgstring, config, cfg_variable);
if(ret < 0){
const git_error *err = giterr_last();
if (err) printf("ERROR %d: %s\n", err->klass, err->message);
else printf("ERROR %d: no detailed info\n", ret);
}
printf("%s\n", cfgstring);
return cfgstring;
}
git_remote * remote_create(git_repository *repo, char *name, char * url){
git_remote * remote;
int ret;
ret = git_remote_create(&remote, repo, name, url);
if (ret == GIT_EINVALIDSPEC){
fprintf(stderr, "Invalid spec\n");
}
else if (ret == GIT_EEXISTS){
fprintf(stderr, "Remote Exists.\n");
}
else if (ret < 0){
fprintf(stderr, "Error creating remote.\n");
}
return remote;
}
git_object * revparse_single(git_repository * repo, char * spec){
git_object * object;
int ret = git_revparse_single(&object, repo, spec);
if (ret == GIT_ENOTFOUND){
fprintf(stderr, "Not found.\n");
}
else if(ret == GIT_EAMBIGUOUS){
fprintf(stderr, "Ambiguous.\n");
}
else if(ret == GIT_EINVALIDSPEC){
fprintf(stderr, "Invalid Spec.\n");
}
else if(ret < 0){
fprintf(stderr, "Error finding object.\n");
}
return object;
}
git_object * object_lookup(git_repository * repo, const git_oid * oid){
git_object * object;
int ret;
ret = git_object_lookup(&object, repo, oid, GIT_OBJ_ANY);
if (ret < 0){
fprintf(stderr, "Error looking up object.\n");
}
return object;
}
git_tag * tag_lookup(git_repository * repo, const git_oid * oid){
git_tag * tag;
int ret;
ret = git_tag_lookup(&tag, repo, oid);
if (ret < 0){
fprintf(stderr, "Error in tag lookup.\n");
}
return tag;
}
git_repository * clone(const char * url, const char * path){
// Clone options not supported for now.
git_repository *cloned_repo = NULL;
int ret;
ret = git_clone(&cloned_repo, url, path, NULL);
if (ret < 0){
const git_error *err = giterr_last();
if (err) printf("ERROR %d: %s\n", err->klass, err->message);
else printf("ERROR %d: no detailed info\n", ret);
}
return cloned_repo;
}
git_reference * reference_lookup(git_repository * repo, const char * name){
git_reference * ref;
int ret;
ret = git_reference_lookup(&ref, repo, name);
if (ret == GIT_ENOTFOUND){
fprintf(stderr, "Reference not found\n");
}
else if (ret == GIT_EINVALIDSPEC){
fprintf(stderr, "Invalid spec.\n");
}
else if (ret < 0){
fprintf(stderr, "Error looking up reference\n");
}
return ref;
}
git_tree * tree_lookup(git_repository * repo, const git_oid * id){
git_tree * tree;
int ret;
ret = git_tree_lookup(&tree, repo, id);
if (ret < 0){
const git_error *err = giterr_last();
if (err) printf("ERROR %d: %s\n", err->klass, err->message);
else printf("ERROR %d: no detailed info\n", ret);
}
return tree;
}