140
}
141
}
142
143
-static void free_origin(struct origin *o)
143
+static struct origin *get_origin(struct scoreboard *sb,
144
+ struct commit *commit,
145
+ const char *path)
146
{
145
- free(o);
146
-}
147
-
148
-static struct origin *find_origin(struct scoreboard *sb,
149
- struct commit *commit,
150
- const char *path)
151
-{
152
- struct blame_entry *ent;
147
+ struct blame_entry *e;
148
struct origin *o;
154
- unsigned mode;
155
- char type[10];
149
157
- for (ent = sb->ent; ent; ent = ent->next) {
158
- if (ent->suspect->commit == commit &&
159
- !strcmp(ent->suspect->path, path))
160
- return ent->suspect;
150
+ for (e = sb->ent; e; e = e->next) {
151
+ if (e->suspect->commit == commit &&
152
+ !strcmp(e->suspect->path, path))
153
+ return e->suspect;
154
}
162
-
155
o = xcalloc(1, sizeof(*o) + strlen(path) + 1);
156
o->commit = commit;
157
strcpy(o->path, path);
166
- if (get_tree_entry(commit->object.sha1, path, o->blob_sha1, &mode))
167
- goto err_out;
168
- if (sha1_object_info(o->blob_sha1, type, NULL) ||
169
- strcmp(type, blob_type))
170
- goto err_out;
158
return o;
172
- err_out:
173
- free_origin(o);
174
- return NULL;
159
}
160
177
-static struct origin *find_rename(struct scoreboard *sb,
161
+static int fill_blob_sha1(struct origin *origin)
162
+{
163
+ unsigned mode;
164
+ char type[10];
165
+
166
+ if (!is_null_sha1(origin->blob_sha1))
167
+ return 0;
168
+ if (get_tree_entry(origin->commit->object.sha1,
169
+ origin->path,
170
+ origin->blob_sha1, &mode))
171
+ goto error_out;
172
+ if (sha1_object_info(origin->blob_sha1, type, NULL) ||
173
+ strcmp(type, blob_type))
174
+ goto error_out;
175
+ return 0;
176
+ error_out:
177
+ hashclr(origin->blob_sha1);
178
+ return -1;
179
+}
180
+
181
+static struct origin *find_origin(struct scoreboard *sb,
182
struct commit *parent,
183
struct origin *origin)
184
{
185
struct origin *porigin = NULL;
186
struct diff_options diff_opts;
187
int i;
184
- const char *paths[1];
188
+ const char *paths[2];
189
+
190
+ /* See if the origin->path is different between parent
191
+ * and origin first. Most of the time they are the
192
+ * same and diff-tree is fairly efficient about this.
193
+ */
194
+ diff_setup(&diff_opts);
195
+ diff_opts.recursive = 1;
196
+ diff_opts.detect_rename = 0;
197
+ diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
198
+ paths[0] = origin->path;
199
+ paths[1] = NULL;
200
+
201
+ diff_tree_setup_paths(paths, &diff_opts);
202
+ if (diff_setup_done(&diff_opts) < 0)
203
+ die("diff-setup");
204
+ diff_tree_sha1(parent->tree->object.sha1,
205
+ origin->commit->tree->object.sha1,
206
+ "", &diff_opts);
207
+ diffcore_std(&diff_opts);
208
+
209
+ /* It is either one entry that says "modified", or "created",
210
+ * or nothing.
211
+ */
212
+ if (!diff_queued_diff.nr) {
213
+ /* The path is the same as parent */
214
+ porigin = get_origin(sb, parent, origin->path);
215
+ hashcpy(porigin->blob_sha1, origin->blob_sha1);
216
+ }
217
+ else if (diff_queued_diff.nr != 1)
218
+ die("internal error in pickaxe::find_origin");
219
+ else {
220
+ struct diff_filepair *p = diff_queued_diff.queue[0];
221
+ switch (p->status) {
222
+ default:
223
+ die("internal error in pickaxe::find_origin (%c)",
224
+ p->status);
225
+ case 'M':
226
+ porigin = get_origin(sb, parent, origin->path);
227
+ hashcpy(porigin->blob_sha1, p->one->sha1);
228
+ break;
229
+ case 'A':
230
+ case 'T':
231
+ /* Did not exist in parent, or type changed */
232
+ break;
233
+ }
234
+ }
235
+ diff_flush(&diff_opts);
236
+ if (porigin)
237
+ return porigin;
238
+
239
+ /* Otherwise we would look for a rename */
240
241
diff_setup(&diff_opts);
242
diff_opts.recursive = 1;
246
diff_tree_setup_paths(paths, &diff_opts);
247
if (diff_setup_done(&diff_opts) < 0)
248
die("diff-setup");
194
- diff_tree_sha1(origin->commit->tree->object.sha1,
195
- parent->tree->object.sha1,
249
+ diff_tree_sha1(parent->tree->object.sha1,
250
+ origin->commit->tree->object.sha1,
251
"", &diff_opts);
252
diffcore_std(&diff_opts);
253
254
for (i = 0; i < diff_queued_diff.nr; i++) {
255
struct diff_filepair *p = diff_queued_diff.queue[i];
256
if ((p->status == 'R' || p->status == 'C') &&
202
- !strcmp(p->one->path, origin->path)) {
203
- porigin = find_origin(sb, parent, p->two->path);
257
+ !strcmp(p->two->path, origin->path)) {
258
+ porigin = get_origin(sb, parent, p->one->path);
259
+ hashcpy(porigin->blob_sha1, p->one->sha1);
260
break;
261
}
262
}
761
"", &diff_opts);
762
diffcore_std(&diff_opts);
763
764
+
765
+ /*
766
+ * NEEDSWORK: This loop is wasteful in that it opens the same
767
+ * blob in the parent number of times. We should swap the
768
+ * loop inside out, which would require keeping track of
769
+ * "best blame so far" for blame entries that the current
770
+ * "target" is being suspected.
771
+ */
772
+
773
for (e = sb->ent; e; e = e->next) {
774
struct blame_entry split[3];
775
if (e->guilty || cmp_suspect(e->suspect, target))
789
if (porigin && !strcmp(p->one->path, porigin->path))
790
/* find_move already dealt with this path */
791
continue;
727
- norigin = find_origin(sb, parent, p->one->path);
728
-
792
+ norigin = get_origin(sb, parent, p->one->path);
793
+ hashcpy(norigin->blob_sha1, p->one->sha1);
794
blob = read_sha1_file(norigin->blob_sha1, type,
795
(unsigned long *) &file_p.size);
796
file_p.ptr = blob;
800
}
801
find_copy_in_blob(sb, e, norigin, this, &file_p);
802
copy_split_if_better(sb, split, this);
803
+ free(blob);
804
}
805
if (split[1].suspect &&
806
blame_copy_score < ent_score(sb, &split[1]))
828
829
if (parse_commit(p))
830
continue;
765
- porigin = find_origin(sb, parent->item, origin->path);
766
- if (!porigin)
767
- porigin = find_rename(sb, parent->item, origin);
831
+ porigin = find_origin(sb, parent->item, origin);
832
if (!porigin)
833
continue;
834
if (!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {
1487
*/
1488
prepare_revision_walk(&revs);
1489
1426
- o = find_origin(&sb, sb.final, path);
1427
- if (!o)
1490
+ o = get_origin(&sb, sb.final, path);
1491
+ if (fill_blob_sha1(o))
1492
die("no such path %s in %s", path, final_commit_name);
1493
1494
sb.final_buf = read_sha1_file(o->blob_sha1, type, &sb.final_buf_size);