| 1 |
|
| /* |
| 2 |
|
| * Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 |
|
| * |
| 4 |
|
| * This file is part of libgit2, distributed under the GNU GPL v2 with |
| 5 |
|
| * a Linking Exception. For full terms see the included COPYING file. |
| 6 |
|
| */ |
| 7 |
|
| |
| 8 |
|
| #include "commit_list.h" |
| 9 |
|
| |
| 10 |
|
| #include "revwalk.h" |
| 11 |
|
| #include "pool.h" |
| 12 |
|
| #include "odb.h" |
| 13 |
|
| #include "commit.h" |
| 14 |
|
| |
| 15 |
169 |
| int git_commit_list_generation_cmp(const void *a, const void *b) |
| 16 |
|
| { |
| 17 |
169 |
| uint32_t generation_a = ((git_commit_list_node *) a)->generation; |
| 18 |
169 |
| uint32_t generation_b = ((git_commit_list_node *) b)->generation; |
| 19 |
|
| |
| 20 |
169 |
▶ | if (!generation_a || !generation_b) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!generation_a || !generation_b) { |
0 |
169 |
169 |
| if (!generation_a || !generation_b) { |
0 |
0 |
0 |
|
| 21 |
|
| /* Fall back to comparing by timestamps if at least one commit lacks a generation. */ |
| 22 |
169 |
| return git_commit_list_time_cmp(a, b); |
| 23 |
|
| } |
| 24 |
|
| |
| 25 |
0 |
▶ | if (generation_a < generation_b) |
Branch: 1 branch
| Branch | True | False | Total |
| if (generation_a < generation_b) |
0 |
0 |
0 |
|
| 26 |
0 |
| return 1; |
| 27 |
0 |
▶ | if (generation_a > generation_b) |
Branch: 1 branch
| Branch | True | False | Total |
| if (generation_a > generation_b) |
0 |
0 |
0 |
|
| 28 |
0 |
| return -1; |
| 29 |
|
| |
| 30 |
0 |
| return 0; |
| 31 |
|
| } |
| 32 |
|
| |
| 33 |
215 |
| int git_commit_list_time_cmp(const void *a, const void *b) |
| 34 |
|
| { |
| 35 |
215 |
| int64_t time_a = ((git_commit_list_node *) a)->time; |
| 36 |
215 |
| int64_t time_b = ((git_commit_list_node *) b)->time; |
| 37 |
|
| |
| 38 |
215 |
▶ | if (time_a < time_b) |
Branch: 1 branch
| Branch | True | False | Total |
| if (time_a < time_b) |
14 |
201 |
215 |
|
| 39 |
14 |
| return 1; |
| 40 |
201 |
▶ | if (time_a > time_b) |
Branch: 1 branch
| Branch | True | False | Total |
| if (time_a > time_b) |
17 |
184 |
201 |
|
| 41 |
17 |
| return -1; |
| 42 |
|
| |
| 43 |
184 |
| return 0; |
| 44 |
|
| } |
| 45 |
|
| |
| 46 |
292 |
| git_commit_list *git_commit_list_insert(git_commit_list_node *item, git_commit_list **list_p) |
| 47 |
|
| { |
| 48 |
292 |
| git_commit_list *new_list = git__malloc(sizeof(git_commit_list)); |
| 49 |
292 |
▶ | if (new_list != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_list != NULL) { |
292 |
0 |
292 |
|
| 50 |
292 |
| new_list->item = item; |
| 51 |
292 |
| new_list->next = *list_p; |
| 52 |
|
| } |
| 53 |
292 |
| *list_p = new_list; |
| 54 |
292 |
| return new_list; |
| 55 |
|
| } |
| 56 |
|
| |
| 57 |
87 |
| git_commit_list *git_commit_list_insert_by_date(git_commit_list_node *item, git_commit_list **list_p) |
| 58 |
|
| { |
| 59 |
87 |
| git_commit_list **pp = list_p; |
| 60 |
|
| git_commit_list *p; |
| 61 |
|
| |
| 62 |
97 |
▶ | while ((p = *pp) != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((p = *pp) != NULL) { |
10 |
87 |
97 |
|
| 63 |
10 |
▶ | if (git_commit_list_time_cmp(p->item, item) > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_commit_list_time_cmp(p->item, item) > 0) |
0 |
10 |
10 |
|
| 64 |
0 |
| break; |
| 65 |
|
| |
| 66 |
10 |
| pp = &p->next; |
| 67 |
|
| } |
| 68 |
|
| |
| 69 |
87 |
| return git_commit_list_insert(item, pp); |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
182 |
| git_commit_list_node *git_commit_list_alloc_node(git_revwalk *walk) |
| 73 |
|
| { |
| 74 |
182 |
| return (git_commit_list_node *)git_pool_mallocz(&walk->commit_pool, 1); |
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
178 |
| static git_commit_list_node **alloc_parents( |
| 78 |
|
| git_revwalk *walk, git_commit_list_node *commit, size_t n_parents) |
| 79 |
|
| { |
| 80 |
|
| size_t bytes; |
| 81 |
|
| |
| 82 |
178 |
▶ | if (n_parents <= PARENTS_PER_COMMIT) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n_parents <= PARENTS_PER_COMMIT) |
178 |
0 |
178 |
|
| 83 |
178 |
| return (git_commit_list_node **)((char *)commit + sizeof(git_commit_list_node)); |
| 84 |
|
| |
| 85 |
0 |
▶ | if (git__multiply_sizet_overflow(&bytes, n_parents, sizeof(git_commit_list_node *))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (git__multiply_sizet_overflow(&bytes, n_parents, sizeof(git_commit_list_node *))) |
0 |
0 |
0 |
|
| 86 |
0 |
| return NULL; |
| 87 |
|
| |
| 88 |
0 |
| return (git_commit_list_node **)git_pool_malloc(&walk->commit_pool, bytes); |
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
|
| |
| 92 |
424 |
| void git_commit_list_free(git_commit_list **list_p) |
| 93 |
|
| { |
| 94 |
424 |
| git_commit_list *list = *list_p; |
| 95 |
|
| |
| 96 |
424 |
▶ | if (list == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (list == NULL) |
339 |
85 |
424 |
|
| 97 |
339 |
| return; |
| 98 |
|
| |
| 99 |
207 |
▶ | while (list) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (list) { |
122 |
85 |
207 |
|
| 100 |
122 |
| git_commit_list *temp = list; |
| 101 |
122 |
| list = temp->next; |
| 102 |
122 |
| git__free(temp); |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
85 |
| *list_p = NULL; |
| 106 |
|
| } |
| 107 |
|
| |
| 108 |
191 |
| git_commit_list_node *git_commit_list_pop(git_commit_list **stack) |
| 109 |
|
| { |
| 110 |
191 |
| git_commit_list *top = *stack; |
| 111 |
191 |
▶ | git_commit_list_node *item = top ? top->item : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| git_commit_list_node *item = top ? top->item : NULL; |
170 |
21 |
191 |
|
| 112 |
|
| |
| 113 |
191 |
▶ | if (top) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (top) { |
170 |
21 |
191 |
|
| 114 |
170 |
| *stack = top->next; |
| 115 |
170 |
| git__free(top); |
| 116 |
|
| } |
| 117 |
191 |
| return item; |
| 118 |
|
| } |
| 119 |
|
| |
| 120 |
178 |
| static int commit_quick_parse( |
| 121 |
|
| git_revwalk *walk, |
| 122 |
|
| git_commit_list_node *node, |
| 123 |
|
| git_odb_object *obj) |
| 124 |
|
| { |
| 125 |
|
| git_oid *parent_oid; |
| 126 |
|
| git_commit *commit; |
| 127 |
|
| size_t i; |
| 128 |
|
| |
| 129 |
178 |
| commit = git__calloc(1, sizeof(*commit)); |
| 130 |
178 |
▶ | GIT_ERROR_CHECK_ALLOC(commit); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(commit); |
0 |
178 |
178 |
|
| 131 |
178 |
| commit->object.repo = walk->repo; |
| 132 |
|
| |
| 133 |
178 |
▶ | if (git_commit__parse_ext(commit, obj, GIT_COMMIT_PARSE_QUICK) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_commit__parse_ext(commit, obj, GIT_COMMIT_PARSE_QUICK) < 0) { |
0 |
178 |
178 |
|
| 134 |
0 |
| git__free(commit); |
| 135 |
0 |
| return -1; |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
178 |
▶ | if (!git__is_uint16(git_array_size(commit->parent_ids))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!git__is_uint16(git_array_size(commit->parent_ids))) { |
0 |
178 |
178 |
|
| 139 |
0 |
| git__free(commit); |
| 140 |
0 |
| git_error_set(GIT_ERROR_INVALID, "commit has more than 2^16 parents"); |
| 141 |
0 |
| return -1; |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
178 |
| node->generation = 0; |
| 145 |
178 |
| node->time = commit->committer->when.time; |
| 146 |
178 |
| node->out_degree = (uint16_t) git_array_size(commit->parent_ids); |
| 147 |
178 |
| node->parents = alloc_parents(walk, node, node->out_degree); |
| 148 |
178 |
▶ | GIT_ERROR_CHECK_ALLOC(node->parents); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(node->parents); |
0 |
178 |
178 |
|
| 149 |
|
| |
| 150 |
323 |
▶ | git_array_foreach(commit->parent_ids, i, parent_oid) { |
Branch: 2 branches
| Branch | True | False | Total |
| git_array_foreach(commit->parent_ids, i, parent_oid) { |
145 |
178 |
323 |
| git_array_foreach(commit->parent_ids, i, parent_oid) { |
145 |
0 |
145 |
|
| 151 |
145 |
| node->parents[i] = git_revwalk__commit_lookup(walk, parent_oid); |
| 152 |
|
| } |
| 153 |
|
| |
| 154 |
178 |
| git_commit__free(commit); |
| 155 |
|
| |
| 156 |
178 |
| node->parsed = 1; |
| 157 |
|
| |
| 158 |
178 |
| return 0; |
| 159 |
|
| } |
| 160 |
|
| |
| 161 |
293 |
| int git_commit_list_parse(git_revwalk *walk, git_commit_list_node *commit) |
| 162 |
|
| { |
| 163 |
|
| git_odb_object *obj; |
| 164 |
293 |
| git_commit_graph_file *cgraph_file = NULL; |
| 165 |
|
| int error; |
| 166 |
|
| |
| 167 |
293 |
▶ | if (commit->parsed) |
Branch: 1 branch
| Branch | True | False | Total |
| if (commit->parsed) |
114 |
179 |
293 |
|
| 168 |
114 |
| return 0; |
| 169 |
|
| |
| 170 |
|
| /* Let's try to use the commit graph first. */ |
| 171 |
179 |
| git_odb__get_commit_graph_file(&cgraph_file, walk->odb); |
| 172 |
179 |
▶ | if (cgraph_file) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cgraph_file) { |
0 |
179 |
179 |
|
| 173 |
|
| git_commit_graph_entry e; |
| 174 |
|
| |
| 175 |
0 |
| error = git_commit_graph_entry_find(&e, cgraph_file, &commit->oid, GIT_OID_RAWSZ); |
| 176 |
0 |
▶ | if (error == 0 && git__is_uint16(e.parent_count)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (error == 0 && git__is_uint16(e.parent_count)) { |
0 |
0 |
0 |
| if (error == 0 && git__is_uint16(e.parent_count)) { |
0 |
0 |
0 |
|
| 177 |
|
| size_t i; |
| 178 |
0 |
| commit->generation = (uint32_t)e.generation; |
| 179 |
0 |
| commit->time = e.commit_time; |
| 180 |
0 |
| commit->out_degree = (uint16_t)e.parent_count; |
| 181 |
0 |
| commit->parents = alloc_parents(walk, commit, commit->out_degree); |
| 182 |
0 |
▶ | GIT_ERROR_CHECK_ALLOC(commit->parents); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(commit->parents); |
0 |
0 |
0 |
|
| 183 |
|
| |
| 184 |
0 |
▶ | for (i = 0; i < commit->out_degree; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < commit->out_degree; ++i) { |
0 |
0 |
0 |
|
| 185 |
|
| git_commit_graph_entry parent; |
| 186 |
0 |
| error = git_commit_graph_entry_parent(&parent, cgraph_file, &e, i); |
| 187 |
0 |
▶ | if (error < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (error < 0) |
0 |
0 |
0 |
|
| 188 |
0 |
| return error; |
| 189 |
0 |
| commit->parents[i] = git_revwalk__commit_lookup(walk, &parent.sha1); |
| 190 |
|
| } |
| 191 |
0 |
| commit->parsed = 1; |
| 192 |
0 |
| return 0; |
| 193 |
|
| } |
| 194 |
|
| } |
| 195 |
|
| |
| 196 |
179 |
▶ | if ((error = git_odb_read(&obj, walk->odb, &commit->oid)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_odb_read(&obj, walk->odb, &commit->oid)) < 0) |
1 |
178 |
179 |
|
| 197 |
1 |
| return error; |
| 198 |
|
| |
| 199 |
178 |
▶ | if (obj->cached.type != GIT_OBJECT_COMMIT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (obj->cached.type != GIT_OBJECT_COMMIT) { |
0 |
178 |
178 |
|
| 200 |
0 |
| git_error_set(GIT_ERROR_INVALID, "object is no commit object"); |
| 201 |
0 |
| error = -1; |
| 202 |
|
| } else |
| 203 |
178 |
| error = commit_quick_parse(walk, commit, obj); |
| 204 |
|
| |
| 205 |
178 |
| git_odb_object_free(obj); |
| 206 |
178 |
| return error; |
| 207 |
|
| } |
| 208 |
|
| |