Clean up old methods, format long lines Added back the line index. We'll need it later.
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
diff --git a/src/blame.c b/src/blame.c
index c85aa7d..ccfbb1e 100644
--- a/src/blame.c
+++ b/src/blame.c
@@ -17,21 +17,20 @@
#include "blame_git.h"
-static int hunk_search_cmp_helper(const void *key, size_t start_line, size_t num_lines)
+static int hunk_byfinalline_search_cmp(const void *key, const void *entry)
{
uint32_t lineno = *(size_t*)key;
- if (lineno < start_line)
+ git_blame_hunk *hunk = (git_blame_hunk*)entry;
+
+ if (lineno < hunk->final_start_line_number)
return -1;
- if (lineno >= ((uint32_t)start_line + num_lines))
+ if (lineno >= hunk->final_start_line_number + hunk->lines_in_hunk)
return 1;
return 0;
}
-static int hunk_byfinalline_search_cmp(const void *key, const void *entry)
-{
- git_blame_hunk *hunk = (git_blame_hunk*)entry;
- return hunk_search_cmp_helper(key, hunk->final_start_line_number, hunk->lines_in_hunk);
-}
-static int hunk_sort_cmp_by_start_line(const void *_a, const void *_b)
+
+static int paths_cmp(const void *a, const void *b) { return git__strcmp((char*)a, (char*)b); }
+static int hunk_cmp(const void *_a, const void *_b)
{
git_blame_hunk *a = (git_blame_hunk*)_a,
*b = (git_blame_hunk*)_b;
@@ -49,7 +48,11 @@ static bool hunk_starts_at_or_after_line(git_blame_hunk *hunk, size_t line)
return line <= hunk->final_start_line_number;
}
-static git_blame_hunk* new_hunk(uint16_t start, uint16_t lines, uint16_t orig_start, const char *path)
+static git_blame_hunk* new_hunk(
+ uint16_t start,
+ uint16_t lines,
+ uint16_t orig_start,
+ const char *path)
{
git_blame_hunk *hunk = git__calloc(1, sizeof(git_blame_hunk));
if (!hunk) return NULL;
@@ -64,7 +67,11 @@ static git_blame_hunk* new_hunk(uint16_t start, uint16_t lines, uint16_t orig_st
static git_blame_hunk* dup_hunk(git_blame_hunk *hunk)
{
- git_blame_hunk *newhunk = new_hunk(hunk->final_start_line_number, hunk->lines_in_hunk, hunk->orig_start_line_number, hunk->orig_path);
+ git_blame_hunk *newhunk = new_hunk(
+ hunk->final_start_line_number,
+ hunk->lines_in_hunk,
+ hunk->orig_start_line_number,
+ hunk->orig_path);
git_oid_cpy(&newhunk->orig_commit_id, &hunk->orig_commit_id);
git_oid_cpy(&newhunk->final_commit_id, &hunk->final_commit_id);
return newhunk;
@@ -78,7 +85,7 @@ static void free_hunk(git_blame_hunk *hunk)
/* Starting with the hunk that includes start_line, shift all following hunks'
* final_start_line by shift_by lines */
-static void shift_hunks_by_final(git_vector *v, size_t start_line, int shift_by)
+static void shift_hunks_by(git_vector *v, size_t start_line, int shift_by)
{
size_t i;
@@ -89,7 +96,7 @@ static void shift_hunks_by_final(git_vector *v, size_t start_line, int shift_by)
}
}
}
-static int paths_cmp(const void *a, const void *b) { return git__strcmp((char*)a, (char*)b); }
+
git_blame* git_blame__alloc(
git_repository *repo,
git_blame_options opts,
@@ -100,7 +107,7 @@ git_blame* git_blame__alloc(
giterr_set_oom();
return NULL;
}
- git_vector_init(&gbr->hunks, 8, hunk_sort_cmp_by_start_line);
+ git_vector_init(&gbr->hunks, 8, hunk_cmp);
git_vector_init(&gbr->paths, 8, paths_cmp);
gbr->repository = repo;
gbr->options = opts;
@@ -125,6 +132,8 @@ void git_blame_free(git_blame *blame)
git__free(path);
git_vector_free(&blame->paths);
+ git_array_clear(blame->line_index);
+
git__free((void*)blame->path);
git_blob_free(blame->final_blob);
git__free(blame);
@@ -205,32 +214,39 @@ static git_blame_hunk *split_hunk_in_vector(
}
}
-
/*
- * To allow quick access to the contents of nth line in the
- * final image, prepare an index in the scoreboard.
+ * Construct a list of char indices for where lines begin
+ * Adapted from core git:
+ * https://github.com/gitster/git/blob/be5c9fb9049ed470e7005f159bb923a5f4de1309/builtin/blame.c#L1760-L1789
*/
-static int prepare_lines(git_blame *blame)
+static int index_blob_lines(git_blame *blame)
{
- const char *buf = blame->final_buf;
- git_off_t len = blame->final_buf_size;
- int num = 0, incomplete = 0, bol = 1;
-
- if (len && buf[len-1] != '\n')
- incomplete++; /* incomplete line at the end */
- while (len--) {
- if (bol) {
- bol = 0;
- }
- if (*buf++ == '\n') {
- num++;
- bol = 1;
- }
- }
- blame->num_lines = num + incomplete;
- return blame->num_lines;
+ const char *buf = blame->final_buf;
+ git_off_t len = blame->final_buf_size;
+ int num = 0, incomplete = 0, bol = 1;
+ size_t *i;
+
+ if (len && buf[len-1] != '\n')
+ incomplete++; /* incomplete line at the end */
+ while (len--) {
+ if (bol) {
+ i = git_array_alloc(blame->line_index);
+ GITERR_CHECK_ALLOC(i);
+ *i = buf - blame->final_buf;
+ bol = 0;
+ }
+ if (*buf++ == '\n') {
+ num++;
+ bol = 1;
+ }
+ }
+ i = git_array_alloc(blame->line_index);
+ GITERR_CHECK_ALLOC(i);
+ *i = buf - blame->final_buf;
+ blame->num_lines = num + incomplete;
+ return blame->num_lines;
}
-
+
static git_blame_hunk* hunk_from_entry(git_blame__entry *e)
{
git_blame_hunk *h = new_hunk(
@@ -240,29 +256,31 @@ static git_blame_hunk* hunk_from_entry(git_blame__entry *e)
return h;
}
-static int walk_and_mark(git_blame *blame)
+static int blame_internal(git_blame *blame)
{
int error;
-
git_blame__entry *ent = NULL;
git_blob *blob = NULL;
git_blame__origin *o;
- if ((error = git_commit_lookup(&blame->final, blame->repository, &blame->options.newest_commit)) < 0 ||
- (error = git_object_lookup_bypath((git_object**)&blob, (git_object*)blame->final, blame->path, GIT_OBJ_BLOB)) < 0)
+ if ((error = git_commit_lookup(&blame->final, blame->repository,
+ &blame->options.newest_commit))
+ < 0 ||
+ (error = git_object_lookup_bypath((git_object**)&blob, (git_object*)blame->final,
+ blame->path, GIT_OBJ_BLOB))
+ < 0)
goto cleanup;
blame->final_buf = git_blob_rawcontent(blob);
blame->final_buf_size = git_blob_rawsize(blob);
if ((error = get_origin(&o, blame, blame->final, blame->path)) < 0)
goto cleanup;
- ent = git__calloc(1, sizeof(*ent));
- ent->num_lines = prepare_lines(blame);
+ ent = git__calloc(1, sizeof(git_blame__entry));
+ ent->num_lines = index_blob_lines(blame);
ent->lno = blame->options.min_line - 1;
ent->num_lines = ent->num_lines - blame->options.min_line + 1;
- if (blame->options.max_line > 0) {
+ if (blame->options.max_line > 0)
ent->num_lines = blame->options.max_line - blame->options.min_line + 1;
- }
ent->s_lno = ent->lno;
ent->suspect = o;
@@ -275,11 +293,10 @@ static int walk_and_mark(git_blame *blame)
cleanup:
for (ent = blame->ent; ent; ) {
git_blame__entry *e = ent->next;
- git_blame__origin *o = ent->suspect;
git_vector_insert(&blame->hunks, hunk_from_entry(ent));
- origin_decref(o);
+ origin_decref(ent->suspect);
git__free(ent);
ent = e;
}
@@ -288,6 +305,10 @@ cleanup:
return error;
}
+/*******************************************************************************
+ * File blaming
+ ******************************************************************************/
+
static int load_blob(git_blame *blame, git_repository *repo, git_oid *commit_id, const char *path)
{
int retval = -1;
@@ -309,10 +330,6 @@ cleanup:
return retval;
}
-/*******************************************************************************
- * File blaming
- ******************************************************************************/
-
int git_blame_file(
git_blame **out,
git_repository *repo,
@@ -332,7 +349,7 @@ int git_blame_file(
if ((error = load_blob(blame, repo, &normOptions.newest_commit, path)) < 0)
goto on_error;
- if ((error = walk_and_mark(blame)) < 0)
+ if ((error = blame_internal(blame)) < 0)
goto on_error;
*out = blame;
@@ -407,10 +424,10 @@ static int buffer_line_cb(
hunk_ends_at_or_before_line(blame->current_hunk, blame->current_diff_line)) {
/* Append to the current buffer-blame hunk */
blame->current_hunk->lines_in_hunk++;
- shift_hunks_by_final(&blame->hunks, blame->current_diff_line+1, 1);
+ shift_hunks_by(&blame->hunks, blame->current_diff_line+1, 1);
} else {
/* Create a new buffer-blame hunk with this line */
- shift_hunks_by_final(&blame->hunks, blame->current_diff_line, 1);
+ shift_hunks_by(&blame->hunks, blame->current_diff_line, 1);
blame->current_hunk = new_hunk(blame->current_diff_line, 1, 0, blame->path);
git_vector_insert_sorted(&blame->hunks, blame->current_hunk, NULL);
}
@@ -430,7 +447,7 @@ static int buffer_line_cb(
blame->current_hunk = (git_blame_hunk*)git_blame_get_hunk_byindex(blame, i);
}
}
- shift_hunks_by_final(&blame->hunks, shift_base, -1);
+ shift_hunks_by(&blame->hunks, shift_base, -1);
}
return 0;
}
diff --git a/src/blame.h b/src/blame.h
index d730cfb..09d349d 100644
--- a/src/blame.h
+++ b/src/blame.h
@@ -72,6 +72,7 @@ struct git_blame {
git_vector paths;
git_blob *final_blob;
+ git_array_t(size_t) line_index;
size_t current_diff_line;
git_blame_hunk *current_hunk;