Commit b6f60a4d964d66b562092fdeb8f76e902bf05fa4

Ben Straub 2013-09-21T22:02:23

Clean up ported code

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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
diff --git a/src/blame.c b/src/blame.c
index ed816da..0722ba0 100644
--- a/src/blame.c
+++ b/src/blame.c
@@ -260,6 +260,8 @@ static int load_blob(git_blame *blame)
 {
 	int error;
 
+	if (blame->final_blob) return 0;
+
 	error = git_commit_lookup(&blame->final, blame->repository, &blame->options.newest_commit);
 	if (error < 0)
 		goto cleanup;
@@ -276,20 +278,13 @@ 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)
-		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)
+	if ((error = load_blob(blame)) < 0 ||
+	    (error = git_blame__get_origin(&o, blame, blame->final, blame->path)) < 0)
 		goto cleanup;
+	blame->final_buf = git_blob_rawcontent(blame->final_blob);
+	blame->final_buf_size = git_blob_rawsize(blame->final_blob);
 
 	ent = git__calloc(1, sizeof(git_blame__entry));
 	ent->num_lines = index_blob_lines(blame);
@@ -303,8 +298,7 @@ static int blame_internal(git_blame *blame)
 	blame->ent = ent;
 	blame->path = blame->path;
 
-	assign_blame(blame, blame->options.flags);
-	coalesce(blame);
+	git_blame__like_git(blame, blame->options.flags);
 
 cleanup:
 	for (ent = blame->ent; ent; ) {
@@ -312,12 +306,10 @@ cleanup:
 
 		git_vector_insert(&blame->hunks, hunk_from_entry(ent));
 
-		origin_decref(ent->suspect);
-		git__free(ent);
+		git_blame__free_entry(ent);
 		ent = e;
 	}
 
-	git_blob_free(blob);
 	return error;
 }
 
diff --git a/src/blame_git.c b/src/blame_git.c
index 3f5fe8d..94e13f8 100644
--- a/src/blame_git.c
+++ b/src/blame_git.c
@@ -7,30 +7,32 @@
 
 #include "blame_git.h"
 #include "commit.h"
-#include "xdiff/xinclude.h"
+#include "blob.h"
 
 /*
- * Locate an existing origin or create a new one.
+ * Origin is refcounted and usually we keep the blob contents to be
+ * reused.
  */
-int get_origin(git_blame__origin **out, git_blame *blame, git_commit *commit, const char *path)
+static git_blame__origin *origin_incref(git_blame__origin *o)
 {
-	git_blame__entry *e;
+	if (o)
+		o->refcnt++;
+	return o;
+}
 
-	for (e = blame->ent; e; e = e->next) {
-		if (e->suspect->commit == commit && !strcmp(e->suspect->path, path)) {
-			*out = origin_incref(e->suspect);
-		}
+static void origin_decref(git_blame__origin *o)
+{
+	if (o && --o->refcnt <= 0) {
+		if (o->previous)
+			origin_decref(o->previous);
+		git_blob_free(o->blob);
+		git_commit_free(o->commit);
+		git__free(o);
 	}
-	return make_origin(out, commit, path);
 }
 
-/*
- * Given a commit and a path in it, create a new origin structure.
- * The callers that add blame to the scoreboard should use
- * get_origin() to obtain shared, refcounted copy instead of calling
- * this function directly.
- */
-int make_origin(git_blame__origin **out, git_commit *commit, const char *path)
+/* Given a commit and a path in it, create a new origin structure. */
+static int make_origin(git_blame__origin **out, git_commit *commit, const char *path)
 {
 	int error = 0;
 	git_blame__origin *o;
@@ -50,13 +52,30 @@ int make_origin(git_blame__origin **out, git_commit *commit, const char *path)
 	return error;
 }
 
-struct blame_chunk_cb_data {
+/* Locate an existing origin or create a new one. */
+int git_blame__get_origin(
+		git_blame__origin **out,
+		git_blame *blame,
+		git_commit *commit,
+		const char *path)
+{
+	git_blame__entry *e;
+
+	for (e = blame->ent; e; e = e->next) {
+		if (e->suspect->commit == commit && !strcmp(e->suspect->path, path)) {
+			*out = origin_incref(e->suspect);
+		}
+	}
+	return make_origin(out, commit, path);
+}
+
+typedef struct blame_chunk_cb_data {
 	git_blame *blame;
 	git_blame__origin *target;
 	git_blame__origin *parent;
 	long tlno;
 	long plno;
-};
+}blame_chunk_cb_data;
 
 static bool same_suspect(git_blame__origin *a, git_blame__origin *b)
 {
@@ -88,10 +107,10 @@ static int find_last_in_target(git_blame *blame, git_blame__origin *target)
  * line plno corresponds to e's line tlno.
  *
  *                <---- e ----->
- *                   <------>
- *                   <------------>
- *             <------------>
- *             <------------------>
+ *                   <------>         (entirely within)
+ *                   <------------>   (overlaps after)
+ *             <------------>         (overlaps before)
+ *             <------------------>   (overlaps both)
  *
  * Split e into potentially three parts; before this chunk, the chunk
  * to be blamed for the parent, and after that portion.
@@ -237,7 +256,13 @@ static void decref_split(git_blame__entry *split)
  * Helper for blame_chunk(). blame_entry e is known to overlap with the patch
  * hunk; split it and pass blame to the parent.
  */
-static void blame_overlap(git_blame *blame, git_blame__entry *e, int tlno, int plno, int same, git_blame__origin *parent)
+static void blame_overlap(
+		git_blame *blame,
+		git_blame__entry *e,
+		int tlno,
+		int plno,
+		int same,
+		git_blame__origin *parent)
 {
 	git_blame__entry split[3] = {{0}};
 
@@ -252,7 +277,13 @@ static void blame_overlap(git_blame *blame, git_blame__entry *e, int tlno, int p
  * e and its parent. Find and split the overlap, and pass blame to the
  * overlapping part to the parent.
  */
-static void blame_chunk(git_blame *blame, int tlno, int plno, int same, git_blame__origin *target, git_blame__origin *parent)
+static void blame_chunk(
+		git_blame *blame,
+		int tlno,
+		int plno,
+		int same,
+		git_blame__origin *target,
+		git_blame__origin *parent)
 {
 	git_blame__entry *e;
 
@@ -267,21 +298,20 @@ static void blame_chunk(git_blame *blame, int tlno, int plno, int same, git_blam
 	}
 }
 
-static void blame_chunk_cb(long start_a, long count_a, long start_b, long count_b, void *data)
-{
-	struct blame_chunk_cb_data *d = data;
-	blame_chunk(d->blame, d->tlno, d->plno, start_b, d->target, d->parent);
-	d->plno = start_a + count_a;
-	d->tlno = start_b + count_b;
-}
-
-static int my_emit_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, xdemitconf_t const *xecfg)
+static int my_emit(
+		xdfenv_t *xe,
+		xdchange_t *xscr,
+		xdemitcb_t *ecb,
+		xdemitconf_t const *xecfg)
 {
 	xdchange_t *xch = xscr;
 	GIT_UNUSED(xe);
 	GIT_UNUSED(xecfg);
 	while (xch) {
-		blame_chunk_cb(xch->i1, xch->chg1, xch->i2, xch->chg2, ecb->priv);
+		blame_chunk_cb_data *d = ecb->priv;
+		blame_chunk(d->blame, d->tlno, d->plno, xch->i2, d->target, d->parent);
+		d->plno = xch->i1 + xch->chg1;
+		d->tlno = xch->i2 + xch->chg2;
 		xch = xch->next;
 	}
 	return 0;
@@ -311,26 +341,17 @@ static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx)
 	b->size -= trimmed - recovered;
 }
 
-static int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
-{
-	mmfile_t a = *mf1;
-	mmfile_t b = *mf2;
-
-	trim_common_tail(&a, &b, xecfg->ctxlen);
-
-	return xdl_diff(&a, &b, xpp, xecfg, xecb);
-}
-
-
-static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b, void *cb_data)
+static int diff_hunks(mmfile_t file_a, mmfile_t file_b, void *cb_data)
 {
 	xpparam_t xpp = {0};
 	xdemitconf_t xecfg = {0};
 	xdemitcb_t ecb = {0};
 
-	xecfg.emit_func = (void(*)(void))my_emit_func;
+	xecfg.emit_func = (void(*)(void))my_emit;
 	ecb.priv = cb_data;
-	return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb);
+
+	trim_common_tail(&file_a, &file_b, 0);
+	return xdl_diff(&file_a, &file_b, &xpp, &xecfg, &ecb);
 }
 
 static void fill_origin_blob(git_blame__origin *o, mmfile_t *file)
@@ -342,13 +363,14 @@ static void fill_origin_blob(git_blame__origin *o, mmfile_t *file)
 	}
 }
 
-static int pass_blame_to_parent(git_blame *blame,
-				git_blame__origin *target,
-				git_blame__origin *parent)
+static int pass_blame_to_parent(
+		git_blame *blame,
+		git_blame__origin *target,
+		git_blame__origin *parent)
 {
 	int last_in_target;
 	mmfile_t file_p, file_o;
-	struct blame_chunk_cb_data d = { blame, target, parent, 0, 0 };
+	blame_chunk_cb_data d = { blame, target, parent, 0, 0 };
 
 	last_in_target = find_last_in_target(blame, target);
 	if (last_in_target < 0)
@@ -357,7 +379,7 @@ static int pass_blame_to_parent(git_blame *blame,
 	fill_origin_blob(parent, &file_p);
 	fill_origin_blob(target, &file_o);
 
-	diff_hunks(&file_p, &file_o, &d);
+	diff_hunks(file_p, file_o, &d);
 	/* The reset (i.e. anything after tlno) are the same as the parent */
 	blame_chunk(blame, d.tlno, d.plno, last_in_target, target, parent);
 
@@ -370,7 +392,10 @@ static int paths_on_dup(void **old, void *new)
 	git__free(new);
 	return -1;
 }
-static git_blame__origin* find_origin(git_blame *blame, git_commit *parent,
+
+static git_blame__origin* find_origin(
+		git_blame *blame,
+		git_commit *parent,
 		git_blame__origin *origin)
 {
 	git_blame__origin *porigin = NULL;
@@ -395,7 +420,7 @@ static git_blame__origin* find_origin(git_blame *blame, git_commit *parent,
 
 	if (!git_diff_num_deltas(difflist)) {
 		/* No changes; copy data */
-		get_origin(&porigin, blame, parent, origin->path);
+		git_blame__get_origin(&porigin, blame, parent, origin->path);
 	} else {
 		git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;
 		int i;
@@ -418,7 +443,8 @@ static git_blame__origin* find_origin(git_blame *blame, git_commit *parent,
 			if (git_vector_bsearch(NULL, &blame->paths, delta->new_file.path) != 0)
 				continue;
 
-			git_vector_insert_sorted(&blame->paths, (void*)git__strdup(delta->old_file.path), paths_on_dup);
+			git_vector_insert_sorted(&blame->paths, (void*)git__strdup(delta->old_file.path),
+					paths_on_dup);
 			make_origin(&porigin, parent, delta->old_file.path);
 		}
 	}
@@ -440,8 +466,8 @@ static void pass_whole_blame(git_blame *blame,
 	git_blame__entry *e;
 
 	if (!porigin->blob)
-		git_object_lookup((git_object**)&porigin->blob, blame->repository, git_blob_id(origin->blob),
-				GIT_OBJ_BLOB);
+		git_object_lookup((git_object**)&porigin->blob, blame->repository,
+				git_blob_id(origin->blob), GIT_OBJ_BLOB);
 	for (e=blame->ent; e; e=e->next) {
 		if (!same_suspect(e->suspect, origin))
 			continue;
@@ -454,25 +480,26 @@ static void pass_whole_blame(git_blame *blame,
 static void pass_blame(git_blame *blame, git_blame__origin *origin, uint32_t opt)
 {
 	git_commit *commit = origin->commit;
-	int i, num_sg;
+	int i, num_parents;
 	git_blame__origin *sg_buf[16];
 	git_blame__origin *porigin, **sg_origin = sg_buf;
 
 	GIT_UNUSED(opt);
 
-	num_sg = git_commit_parentcount(commit);
+	num_parents = git_commit_parentcount(commit);
 	if (!git_oid_cmp(git_commit_id(commit), &blame->options.oldest_commit))
-		num_sg = 0;
-	if (!num_sg) {
+		/* Stop at oldest specified commit */
+		num_parents = 0;
+	if (!num_parents) {
 		git_oid_cpy(&blame->options.oldest_commit, git_commit_id(commit));
 		goto finish;
 	}
-	else if (num_sg < (int)ARRAY_SIZE(sg_buf))
+	else if (num_parents < (int)ARRAY_SIZE(sg_buf))
 		memset(sg_buf, 0, sizeof(sg_buf));
 	else
-		sg_origin = git__calloc(num_sg, sizeof(*sg_origin));
+		sg_origin = git__calloc(num_parents, sizeof(*sg_origin));
 
-	for (i=0; i<num_sg; i++) {
+	for (i=0; i<num_parents; i++) {
 		git_commit *p;
 		int j, same;
 
@@ -502,7 +529,8 @@ static void pass_blame(git_blame *blame, git_blame__origin *origin, uint32_t opt
 			origin_decref(porigin);
 	}
 
-	for (i=0; i<num_sg; i++) {
+	/* Standard blame */
+	for (i=0; i<num_parents; i++) {
 		git_blame__origin *porigin = sg_origin[i];
 		if (!porigin)
 			continue;
@@ -519,7 +547,7 @@ static void pass_blame(git_blame *blame, git_blame__origin *origin, uint32_t opt
 	/* TODO: optionally find copies in parents' files */
 
 finish:
-	for (i=0; i<num_sg; i++)
+	for (i=0; i<num_parents; i++)
 		if (sg_origin[i])
 			origin_decref(sg_origin[i]);
 	if (sg_origin != sg_buf)
@@ -528,28 +556,32 @@ finish:
 }
 
 /*
- * Origin is refcounted and usually we keep the blob contents to be
- * reused.
+ * If two blame entries that are next to each other came from
+ * contiguous lines in the same origin (i.e. <commit, path> pair),
+ * merge them together.
  */
-git_blame__origin *origin_incref(git_blame__origin *o)
+static void coalesce(git_blame *blame)
 {
-	if (o)
-		o->refcnt++;
-	return o;
-}
+	git_blame__entry *ent, *next;
 
-void origin_decref(git_blame__origin *o)
-{
-	if (o && --o->refcnt <= 0) {
-		if (o->previous)
-			origin_decref(o->previous);
-		git_blob_free(o->blob);
-		git_commit_free(o->commit);
-		git__free(o);
+	for (ent=blame->ent; ent && (next = ent->next); ent = next) {
+		if (same_suspect(ent->suspect, next->suspect) &&
+		    ent->guilty == next->guilty &&
+		    ent->s_lno + ent->num_lines == next->s_lno)
+		{
+			ent->num_lines += next->num_lines;
+			ent->next = next->next;
+			if (ent->next)
+				ent->next->prev = ent;
+			origin_decref(next->suspect);
+			git__free(next);
+			ent->score = 0;
+			next = ent; /* again */
+		}
 	}
 }
 
-void assign_blame(git_blame *blame, uint32_t opt)
+void git_blame__like_git(git_blame *blame, uint32_t opt)
 {
 	while (true) {
 		git_blame__entry *ent;
@@ -577,26 +609,13 @@ void assign_blame(git_blame *blame, uint32_t opt)
 		}
 		origin_decref(suspect);
 	}
+
+	coalesce(blame);
 }
 
-void coalesce(git_blame *blame)
+void git_blame__free_entry(git_blame__entry *ent)
 {
-	git_blame__entry *ent, *next;
-
-	for (ent=blame->ent; ent && (next = ent->next); ent = next) {
-		if (same_suspect(ent->suspect, next->suspect) &&
-		    ent->guilty == next->guilty &&
-		    ent->s_lno + ent->num_lines == next->s_lno)
-		{
-			ent->num_lines += next->num_lines;
-			ent->next = next->next;
-			if (ent->next)
-				ent->next->prev = ent;
-			origin_decref(next->suspect);
-			git__free(next);
-			ent->score = 0;
-			next = ent; /* again */
-		}
-	}
+	if (!ent) return;
+	origin_decref(ent->suspect);
+	git__free(ent);
 }
-
diff --git a/src/blame_git.h b/src/blame_git.h
index 28d6422..2b78011 100644
--- a/src/blame_git.h
+++ b/src/blame_git.h
@@ -1,16 +1,21 @@
-
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
 #ifndef INCLUDE_blame_git__
 #define INCLUDE_blame_git__
 
-#include "git2.h"
 #include "blame.h"
 #include "xdiff/xinclude.h"
 
-int get_origin(git_blame__origin **out, git_blame *sb, git_commit *commit, const char *path);
-int make_origin(git_blame__origin **out, git_commit *commit, const char *path);
-git_blame__origin *origin_incref(git_blame__origin *o);
-void origin_decref(git_blame__origin *o);
-void assign_blame(git_blame *sb, uint32_t flags);
-void coalesce(git_blame *sb);
+int git_blame__get_origin(
+		git_blame__origin **out,
+		git_blame *sb,
+		git_commit *commit,
+		const char *path);
+void git_blame__free_entry(git_blame__entry *ent);
+void git_blame__like_git(git_blame *sb, uint32_t flags);
 
 #endif