Skip to content

Commit 6bb7aa1

Browse files
Vicent Martiageric
authored andcommitted
Added new error codes. Improved error handling.
Signed-off-by: Vicent Marti <tanoku@gmail.com> Signed-off-by: Andreas Ericsson <ae@op5.se>
1 parent 0daa6cd commit 6bb7aa1

File tree

6 files changed

+57
-37
lines changed

6 files changed

+57
-37
lines changed

src/commit.c

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ int git_commit_parse_existing(git_commit *commit)
8181
return 0;
8282

8383
if (git_odb_read(&commit_obj, commit->object.pool->db, &commit->object.id) < 0)
84-
return -1;
84+
return GIT_ENOTFOUND;
8585

8686
if (commit_obj.type != GIT_OBJ_COMMIT)
8787
goto error_cleanup;
@@ -128,27 +128,27 @@ git_commit *git_commit_lookup(git_revpool *pool, const git_oid *id)
128128
int git_commit__parse_time(time_t *commit_time, char *buffer, const char *buffer_end)
129129
{
130130
if (memcmp(buffer, "author ", 7) != 0)
131-
return -1;
131+
return GIT_EOBJCORRUPTED;
132132

133133
buffer = memchr(buffer, '\n', buffer_end - buffer);
134134
if (buffer == 0 || ++buffer >= buffer_end)
135-
return -1;
135+
return GIT_EOBJCORRUPTED;
136136

137137
if (memcmp(buffer, "committer ", 10) != 0)
138-
return -1;
138+
return GIT_EOBJCORRUPTED;
139139

140140
buffer = memchr(buffer, '>', buffer_end - buffer);
141141
if (buffer == 0 || ++buffer >= buffer_end)
142-
return -1;
142+
return GIT_EOBJCORRUPTED;
143143

144144
*commit_time = strtol(buffer, &buffer, 10);
145145

146146
if (*commit_time == 0)
147-
return -1;
147+
return GIT_EOBJCORRUPTED;
148148

149149
buffer = memchr(buffer, '\n', buffer_end - buffer);
150150
if (buffer == 0 || ++buffer >= buffer_end)
151-
return -1;
151+
return GIT_EOBJCORRUPTED;
152152

153153
return (buffer < buffer_end) ? 0 : -1;
154154
}
@@ -161,16 +161,16 @@ int git_commit__parse_oid(git_oid *oid, char **buffer_out, const char *buffer_en
161161
char *buffer = *buffer_out;
162162

163163
if (buffer + (header_len + sha_len + 1) > buffer_end)
164-
return -1;
164+
return GIT_EOBJCORRUPTED;
165165

166166
if (memcmp(buffer, header, header_len) != 0)
167-
return -1;
167+
return GIT_EOBJCORRUPTED;
168168

169169
if (buffer[header_len + sha_len] != '\n')
170-
return -1;
170+
return GIT_EOBJCORRUPTED;
171171

172172
if (git_oid_mkstr(oid, buffer + header_len) < 0)
173-
return -1;
173+
return GIT_EOBJCORRUPTED;
174174

175175
*buffer_out = buffer + (header_len + sha_len + 1);
176176

@@ -188,7 +188,7 @@ int git_commit__parse_buffer(git_commit *commit, void *data, size_t len)
188188
return 0;
189189

190190
if (git_commit__parse_oid(&oid, &buffer, buffer_end, "tree ") < 0)
191-
return -1;
191+
return GIT_EOBJCORRUPTED;
192192

193193
/*
194194
* TODO: load tree into commit object
@@ -199,7 +199,7 @@ int git_commit__parse_buffer(git_commit *commit, void *data, size_t len)
199199
git_commit *parent;
200200

201201
if ((parent = git_commit_lookup(commit->object.pool, &oid)) == NULL)
202-
return -1;
202+
return GIT_ENOTFOUND;
203203

204204
// Inherit uninteresting flag
205205
if (commit->uninteresting)
@@ -209,21 +209,21 @@ int git_commit__parse_buffer(git_commit *commit, void *data, size_t len)
209209
}
210210

211211
if (git_commit__parse_time(&commit->commit_time, buffer, buffer_end) < 0)
212-
return -1;
212+
return GIT_EOBJCORRUPTED;
213213

214214
commit->parsed = 1;
215215

216216
return 0;
217217
}
218218

219-
void git_commit_list_push_back(git_commit_list *list, git_commit *commit)
219+
int git_commit_list_push_back(git_commit_list *list, git_commit *commit)
220220
{
221221
git_commit_node *node = NULL;
222222

223223
node = git__malloc(sizeof(git_commit_list));
224224

225225
if (node == NULL)
226-
return;
226+
return GIT_ENOMEM;
227227

228228
node->commit = commit;
229229
node->next = NULL;
@@ -237,16 +237,17 @@ void git_commit_list_push_back(git_commit_list *list, git_commit *commit)
237237
}
238238

239239
list->size++;
240+
return 0;
240241
}
241242

242-
void git_commit_list_push_front(git_commit_list *list, git_commit *commit)
243+
int git_commit_list_push_front(git_commit_list *list, git_commit *commit)
243244
{
244245
git_commit_node *node = NULL;
245246

246247
node = git__malloc(sizeof(git_commit_list));
247248

248249
if (node == NULL)
249-
return;
250+
return GIT_ENOMEM;
250251

251252
node->commit = commit;
252253
node->next = list->head;
@@ -260,6 +261,7 @@ void git_commit_list_push_front(git_commit_list *list, git_commit *commit)
260261
}
261262

262263
list->size++;
264+
return 0;
263265
}
264266

265267

src/commit.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ void git_commit__mark_uninteresting(git_commit *commit);
4444
int git_commit_parse_existing(git_commit *commit);
4545

4646

47-
void git_commit_list_push_back(git_commit_list *list, git_commit *commit);
48-
void git_commit_list_push_front(git_commit_list *list, git_commit *commit);
47+
int git_commit_list_push_back(git_commit_list *list, git_commit *commit);
48+
int git_commit_list_push_front(git_commit_list *list, git_commit *commit);
4949

5050
git_commit *git_commit_list_pop_back(git_commit_list *list);
5151
git_commit *git_commit_list_pop_front(git_commit_list *list);

src/git/common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@
7777
/** Consult the OS error information. */
7878
#define GIT_EOSERR (GIT_ERROR - 4)
7979

80+
/** The specified object is of invalid type */
81+
#define GIT_EOBJTYPE (GIT_ERROR - 5)
82+
83+
/** The specified object has its data corrupted */
84+
#define GIT_EOBJCORRUPTED (GIT_ERROR - 6)
85+
8086
GIT_BEGIN_DECL
8187

8288
/** A revision traversal pool. */

src/git/revwalk.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ GIT_EXTERN(void) gitrp_reset(git_revpool *pool);
6767
* @param pool the pool being used for the traversal.
6868
* @param commit the commit to start from.
6969
*/
70-
GIT_EXTERN(void) gitrp_push(git_revpool *pool, git_commit *commit);
70+
GIT_EXTERN(int) gitrp_push(git_revpool *pool, git_commit *commit);
7171

7272
/**
7373
* Mark a commit (and its ancestors) uninteresting for the output.
7474
* @param pool the pool being used for the traversal.
7575
* @param commit the commit that will be ignored during the traversal
7676
*/
77-
GIT_EXTERN(void) gitrp_hide(git_revpool *pool, git_commit *commit);
77+
GIT_EXTERN(int) gitrp_hide(git_revpool *pool, git_commit *commit);
7878

7979
/**
8080
* Get the next commit from the revision traversal.

src/revwalk.c

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ void gitrp_sorting(git_revpool *pool, unsigned int sort_mode)
6262
gitrp_reset(pool);
6363
}
6464

65-
void gitrp_push(git_revpool *pool, git_commit *commit)
65+
int gitrp_push(git_revpool *pool, git_commit *commit)
6666
{
6767
if (commit == NULL || commit->seen)
68-
return;
68+
return GIT_ENOTFOUND;
6969

7070
if (commit->object.pool != pool || pool->walking)
71-
return;
71+
return GIT_ERROR;
7272

7373
if (!commit->parsed) {
7474
if (git_commit_parse_existing(commit) < 0)
75-
return;
75+
return GIT_EOBJCORRUPTED;
7676
}
7777

7878
// Sanity check: make sure that if the commit
@@ -81,36 +81,48 @@ void gitrp_push(git_revpool *pool, git_commit *commit)
8181
if (commit->uninteresting)
8282
git_commit__mark_uninteresting(commit);
8383

84-
git_commit_list_push_back(&pool->roots, commit);
84+
if (git_commit_list_push_back(&pool->roots, commit) < 0)
85+
return GIT_ENOMEM;
86+
87+
return 0;
8588
}
8689

87-
void gitrp_hide(git_revpool *pool, git_commit *commit)
90+
int gitrp_hide(git_revpool *pool, git_commit *commit)
8891
{
8992
if (pool->walking)
90-
return;
93+
return GIT_ERROR;
9194

9295
git_commit__mark_uninteresting(commit);
93-
gitrp_push(pool, commit);
96+
return gitrp_push(pool, commit);
9497
}
9598

96-
void gitrp__enroot(git_revpool *pool, git_commit *commit)
99+
int gitrp__enroot(git_revpool *pool, git_commit *commit)
97100
{
101+
int error;
98102
git_commit_node *parents;
99103

100104
if (commit->seen)
101-
return;
105+
return 0;
102106

103-
if (commit->parsed == 0)
104-
git_commit_parse_existing(commit);
107+
if (commit->parsed == 0) {
108+
if (git_commit_parse_existing(commit))
109+
return GIT_EOBJCORRUPTED;
110+
}
105111

106112
commit->seen = 1;
107113

108114
for (parents = commit->parents.head; parents != NULL; parents = parents->next) {
109115
parents->commit->in_degree++;
110-
gitrp__enroot(pool, parents->commit);
116+
117+
error = gitrp__enroot(pool, parents->commit);
118+
if (error < 0)
119+
return error;
111120
}
112121

113-
git_commit_list_push_back(&pool->iterator, commit);
122+
if (git_commit_list_push_back(&pool->iterator, commit))
123+
return GIT_ENOMEM;
124+
125+
return 0;
114126
}
115127

116128
void gitrp__prepare_walk(git_revpool *pool)

src/revwalk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ struct git_revpool {
1818
};
1919

2020
void gitrp__prepare_walk(git_revpool *pool);
21-
void gitrp__enroot(git_revpool *pool, git_commit *commit);
21+
int gitrp__enroot(git_revpool *pool, git_commit *commit);
2222

2323
#endif /* INCLUDE_revwalk_h__ */

0 commit comments

Comments
 (0)