Blame src/blame_git.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "blame_git.h"
Packit Service 20376f
#include "commit.h"
Packit Service 20376f
#include "blob.h"
Packit Service 20376f
#include "xdiff/xinclude.h"
Packit Service 20376f
#include "diff_xdiff.h"
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Origin is refcounted and usually we keep the blob contents to be
Packit Service 20376f
 * reused.
Packit Service 20376f
 */
Packit Service 20376f
static git_blame__origin *origin_incref(git_blame__origin *o)
Packit Service 20376f
{
Packit Service 20376f
	if (o)
Packit Service 20376f
		o->refcnt++;
Packit Service 20376f
	return o;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void origin_decref(git_blame__origin *o)
Packit Service 20376f
{
Packit Service 20376f
	if (o && --o->refcnt <= 0) {
Packit Service 20376f
		if (o->previous)
Packit Service 20376f
			origin_decref(o->previous);
Packit Service 20376f
		git_blob_free(o->blob);
Packit Service 20376f
		git_commit_free(o->commit);
Packit Service 20376f
		git__free(o);
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* Given a commit and a path in it, create a new origin structure. */
Packit Service 20376f
static int make_origin(git_blame__origin **out, git_commit *commit, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	git_blame__origin *o;
Packit Service 20376f
	git_object *blob;
Packit Service 20376f
	size_t path_len = strlen(path), alloc_len;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_object_lookup_bypath(&blob, (git_object*)commit,
Packit Service 20376f
			path, GIT_OBJ_BLOB)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloc_len, sizeof(*o), path_len);
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 1);
Packit Service 20376f
	o = git__calloc(1, alloc_len);
Packit Service 20376f
	GITERR_CHECK_ALLOC(o);
Packit Service 20376f
Packit Service 20376f
	o->commit = commit;
Packit Service 20376f
	o->blob = (git_blob *) blob;
Packit Service 20376f
	o->refcnt = 1;
Packit Service 20376f
	strcpy(o->path, path);
Packit Service 20376f
Packit Service 20376f
	*out = o;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* Locate an existing origin or create a new one. */
Packit Service 20376f
int git_blame__get_origin(
Packit Service 20376f
		git_blame__origin **out,
Packit Service 20376f
		git_blame *blame,
Packit Service 20376f
		git_commit *commit,
Packit Service 20376f
		const char *path)
Packit Service 20376f
{
Packit Service 20376f
	git_blame__entry *e;
Packit Service 20376f
Packit Service 20376f
	for (e = blame->ent; e; e = e->next) {
Packit Service 20376f
		if (e->suspect->commit == commit && !strcmp(e->suspect->path, path)) {
Packit Service 20376f
			*out = origin_incref(e->suspect);
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
	return make_origin(out, commit, path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
typedef struct blame_chunk_cb_data {
Packit Service 20376f
	git_blame *blame;
Packit Service 20376f
	git_blame__origin *target;
Packit Service 20376f
	git_blame__origin *parent;
Packit Service 20376f
	long tlno;
Packit Service 20376f
	long plno;
Packit Service 20376f
}blame_chunk_cb_data;
Packit Service 20376f
Packit Service 20376f
static bool same_suspect(git_blame__origin *a, git_blame__origin *b)
Packit Service 20376f
{
Packit Service 20376f
	if (a == b)
Packit Service 20376f
		return true;
Packit Service 20376f
	if (git_oid_cmp(git_commit_id(a->commit), git_commit_id(b->commit)))
Packit Service 20376f
		return false;
Packit Service 20376f
	return 0 == strcmp(a->path, b->path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* find the line number of the last line the target is suspected for */
Packit Service 20376f
static bool find_last_in_target(size_t *out, git_blame *blame, git_blame__origin *target)
Packit Service 20376f
{
Packit Service 20376f
	git_blame__entry *e;
Packit Service 20376f
	size_t last_in_target = 0;
Packit Service 20376f
	bool found = false;
Packit Service 20376f
Packit Service 20376f
	*out = 0;
Packit Service 20376f
Packit Service 20376f
	for (e=blame->ent; e; e=e->next) {
Packit Service 20376f
		if (e->guilty || !same_suspect(e->suspect, target))
Packit Service 20376f
			continue;
Packit Service 20376f
		if (last_in_target < e->s_lno + e->num_lines) {
Packit Service 20376f
			found = true;
Packit Service 20376f
			last_in_target = e->s_lno + e->num_lines;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*out = last_in_target;
Packit Service 20376f
	return found;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * It is known that lines between tlno to same came from parent, and e
Packit Service 20376f
 * has an overlap with that range.  it also is known that parent's
Packit Service 20376f
 * line plno corresponds to e's line tlno.
Packit Service 20376f
 *
Packit Service 20376f
 *                <---- e ----->
Packit Service 20376f
 *                   <------>         (entirely within)
Packit Service 20376f
 *                   <------------>   (extends past)
Packit Service 20376f
 *             <------------>         (starts before)
Packit Service 20376f
 *             <------------------>   (entirely encloses)
Packit Service 20376f
 *
Packit Service 20376f
 * Split e into potentially three parts; before this chunk, the chunk
Packit Service 20376f
 * to be blamed for the parent, and after that portion.
Packit Service 20376f
 */
Packit Service 20376f
static void split_overlap(git_blame__entry *split, git_blame__entry *e,
Packit Service 20376f
		size_t tlno, size_t plno, size_t same, git_blame__origin *parent)
Packit Service 20376f
{
Packit Service 20376f
	size_t chunk_end_lno;
Packit Service 20376f
Packit Service 20376f
	if (e->s_lno < tlno) {
Packit Service 20376f
		/* there is a pre-chunk part not blamed on the parent */
Packit Service 20376f
		split[0].suspect = origin_incref(e->suspect);
Packit Service 20376f
		split[0].lno = e->lno;
Packit Service 20376f
		split[0].s_lno = e->s_lno;
Packit Service 20376f
		split[0].num_lines = tlno - e->s_lno;
Packit Service 20376f
		split[1].lno = e->lno + tlno - e->s_lno;
Packit Service 20376f
		split[1].s_lno = plno;
Packit Service 20376f
	} else {
Packit Service 20376f
		split[1].lno = e->lno;
Packit Service 20376f
		split[1].s_lno = plno + (e->s_lno - tlno);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (same < e->s_lno + e->num_lines) {
Packit Service 20376f
		/* there is a post-chunk part not blamed on parent */
Packit Service 20376f
		split[2].suspect = origin_incref(e->suspect);
Packit Service 20376f
		split[2].lno = e->lno + (same - e->s_lno);
Packit Service 20376f
		split[2].s_lno = e->s_lno + (same - e->s_lno);
Packit Service 20376f
		split[2].num_lines = e->s_lno + e->num_lines - same;
Packit Service 20376f
		chunk_end_lno = split[2].lno;
Packit Service 20376f
	} else {
Packit Service 20376f
		chunk_end_lno = e->lno + e->num_lines;
Packit Service 20376f
	}
Packit Service 20376f
	split[1].num_lines = chunk_end_lno - split[1].lno;
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * if it turns out there is nothing to blame the parent for, forget about
Packit Service 20376f
	 * the splitting. !split[1].suspect signals this.
Packit Service 20376f
	 */
Packit Service 20376f
	if (split[1].num_lines < 1)
Packit Service 20376f
		return;
Packit Service 20376f
	split[1].suspect = origin_incref(parent);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Link in a new blame entry to the scoreboard. Entries that cover the same
Packit Service 20376f
 * line range have been removed from the scoreboard previously.
Packit Service 20376f
 */
Packit Service 20376f
static void add_blame_entry(git_blame *blame, git_blame__entry *e)
Packit Service 20376f
{
Packit Service 20376f
	git_blame__entry *ent, *prev = NULL;
Packit Service 20376f
Packit Service 20376f
	origin_incref(e->suspect);
Packit Service 20376f
Packit Service 20376f
	for (ent = blame->ent; ent && ent->lno < e->lno; ent = ent->next)
Packit Service 20376f
		prev = ent;
Packit Service 20376f
Packit Service 20376f
	/* prev, if not NULL, is the last one that is below e */
Packit Service 20376f
	e->prev = prev;
Packit Service 20376f
	if (prev) {
Packit Service 20376f
		e->next = prev->next;
Packit Service 20376f
		prev->next = e;
Packit Service 20376f
	} else {
Packit Service 20376f
		e->next = blame->ent;
Packit Service 20376f
		blame->ent = e;
Packit Service 20376f
	}
Packit Service 20376f
	if (e->next)
Packit Service 20376f
		e->next->prev = e;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * src typically is on-stack; we want to copy the information in it to
Packit Service 20376f
 * a malloced blame_entry that is already on the linked list of the scoreboard.
Packit Service 20376f
 * The origin of dst loses a refcnt while the origin of src gains one.
Packit Service 20376f
 */
Packit Service 20376f
static void dup_entry(git_blame__entry *dst, git_blame__entry *src)
Packit Service 20376f
{
Packit Service 20376f
	git_blame__entry *p, *n;
Packit Service 20376f
Packit Service 20376f
	p = dst->prev;
Packit Service 20376f
	n = dst->next;
Packit Service 20376f
	origin_incref(src->suspect);
Packit Service 20376f
	origin_decref(dst->suspect);
Packit Service 20376f
	memcpy(dst, src, sizeof(*src));
Packit Service 20376f
	dst->prev = p;
Packit Service 20376f
	dst->next = n;
Packit Service 20376f
	dst->score = 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * split_overlap() divided an existing blame e into up to three parts in split.
Packit Service 20376f
 * Adjust the linked list of blames in the scoreboard to reflect the split.
Packit Service 20376f
 */
Packit Service 20376f
static void split_blame(git_blame *blame, git_blame__entry *split, git_blame__entry *e)
Packit Service 20376f
{
Packit Service 20376f
	git_blame__entry *new_entry;
Packit Service 20376f
Packit Service 20376f
	if (split[0].suspect && split[2].suspect) {
Packit Service 20376f
		/* The first part (reuse storage for the existing entry e */
Packit Service 20376f
		dup_entry(e, &split[0]);
Packit Service 20376f
Packit Service 20376f
		/* The last part -- me */
Packit Service 20376f
		new_entry = git__malloc(sizeof(*new_entry));
Packit Service 20376f
		memcpy(new_entry, &(split[2]), sizeof(git_blame__entry));
Packit Service 20376f
		add_blame_entry(blame, new_entry);
Packit Service 20376f
Packit Service 20376f
		/* ... and the middle part -- parent */
Packit Service 20376f
		new_entry = git__malloc(sizeof(*new_entry));
Packit Service 20376f
		memcpy(new_entry, &(split[1]), sizeof(git_blame__entry));
Packit Service 20376f
		add_blame_entry(blame, new_entry);
Packit Service 20376f
	} else if (!split[0].suspect && !split[2].suspect) {
Packit Service 20376f
		/*
Packit Service 20376f
		 * The parent covers the entire area; reuse storage for e and replace it
Packit Service 20376f
		 * with the parent
Packit Service 20376f
		 */
Packit Service 20376f
		dup_entry(e, &split[1]);
Packit Service 20376f
	} else if (split[0].suspect) {
Packit Service 20376f
		/* me and then parent */
Packit Service 20376f
		dup_entry(e, &split[0]);
Packit Service 20376f
		new_entry = git__malloc(sizeof(*new_entry));
Packit Service 20376f
		memcpy(new_entry, &(split[1]), sizeof(git_blame__entry));
Packit Service 20376f
		add_blame_entry(blame, new_entry);
Packit Service 20376f
	} else {
Packit Service 20376f
		/* parent and then me */
Packit Service 20376f
		dup_entry(e, &split[1]);
Packit Service 20376f
		new_entry = git__malloc(sizeof(*new_entry));
Packit Service 20376f
		memcpy(new_entry, &(split[2]), sizeof(git_blame__entry));
Packit Service 20376f
		add_blame_entry(blame, new_entry);
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* 
Packit Service 20376f
 * After splitting the blame, the origins used by the on-stack blame_entry
Packit Service 20376f
 * should lose one refcnt each.
Packit Service 20376f
 */
Packit Service 20376f
static void decref_split(git_blame__entry *split)
Packit Service 20376f
{
Packit Service 20376f
	int i;
Packit Service 20376f
	for (i=0; i<3; i++)
Packit Service 20376f
		origin_decref(split[i].suspect);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Helper for blame_chunk(). blame_entry e is known to overlap with the patch
Packit Service 20376f
 * hunk; split it and pass blame to the parent.
Packit Service 20376f
 */
Packit Service 20376f
static void blame_overlap(
Packit Service 20376f
		git_blame *blame,
Packit Service 20376f
		git_blame__entry *e,
Packit Service 20376f
		size_t tlno,
Packit Service 20376f
		size_t plno,
Packit Service 20376f
		size_t same,
Packit Service 20376f
		git_blame__origin *parent)
Packit Service 20376f
{
Packit Service 20376f
	git_blame__entry split[3] = {{0}};
Packit Service 20376f
Packit Service 20376f
	split_overlap(split, e, tlno, plno, same, parent);
Packit Service 20376f
	if (split[1].suspect)
Packit Service 20376f
		split_blame(blame, split, e);
Packit Service 20376f
	decref_split(split);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Process one hunk from the patch between the current suspect for blame_entry
Packit Service 20376f
 * e and its parent. Find and split the overlap, and pass blame to the
Packit Service 20376f
 * overlapping part to the parent.
Packit Service 20376f
 */
Packit Service 20376f
static void blame_chunk(
Packit Service 20376f
		git_blame *blame,
Packit Service 20376f
		size_t tlno,
Packit Service 20376f
		size_t plno,
Packit Service 20376f
		size_t same,
Packit Service 20376f
		git_blame__origin *target,
Packit Service 20376f
		git_blame__origin *parent)
Packit Service 20376f
{
Packit Service 20376f
	git_blame__entry *e;
Packit Service 20376f
Packit Service 20376f
	for (e = blame->ent; e; e = e->next) {
Packit Service 20376f
		if (e->guilty || !same_suspect(e->suspect, target))
Packit Service 20376f
			continue;
Packit Service 20376f
		if (same <= e->s_lno)
Packit Service 20376f
			continue;
Packit Service 20376f
		if (tlno < e->s_lno + e->num_lines) {
Packit Service 20376f
			blame_overlap(blame, e, tlno, plno, same, parent);
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int my_emit(
Packit Service 20376f
	long start_a, long count_a,
Packit Service 20376f
	long start_b, long count_b,
Packit Service 20376f
	void *cb_data)
Packit Service 20376f
{
Packit Service 20376f
	blame_chunk_cb_data *d = (blame_chunk_cb_data *)cb_data;
Packit Service 20376f
Packit Service 20376f
	blame_chunk(d->blame, d->tlno, d->plno, start_b, d->target, d->parent);
Packit Service 20376f
	d->plno = start_a + count_a;
Packit Service 20376f
	d->tlno = start_b + count_b;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx)
Packit Service 20376f
{
Packit Service 20376f
	const int blk = 1024;
Packit Service 20376f
	long trimmed = 0, recovered = 0;
Packit Service 20376f
	char *ap = a->ptr + a->size;
Packit Service 20376f
	char *bp = b->ptr + b->size;
Packit Service 20376f
	long smaller = (long)((a->size < b->size) ? a->size : b->size);
Packit Service 20376f
Packit Service 20376f
	if (ctx)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
Packit Service 20376f
		trimmed += blk;
Packit Service 20376f
		ap -= blk;
Packit Service 20376f
		bp -= blk;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	while (recovered < trimmed)
Packit Service 20376f
		if (ap[recovered++] == '\n')
Packit Service 20376f
			break;
Packit Service 20376f
	a->size -= trimmed - recovered;
Packit Service 20376f
	b->size -= trimmed - recovered;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int diff_hunks(mmfile_t file_a, mmfile_t file_b, void *cb_data)
Packit Service 20376f
{
Packit Service 20376f
	xpparam_t xpp = {0};
Packit Service 20376f
	xdemitconf_t xecfg = {0};
Packit Service 20376f
	xdemitcb_t ecb = {0};
Packit Service 20376f
Packit Service 20376f
	xecfg.hunk_func = my_emit;
Packit Service 20376f
	ecb.priv = cb_data;
Packit Service 20376f
Packit Service 20376f
	trim_common_tail(&file_a, &file_b, 0);
Packit Service 20376f
Packit Service 20376f
	if (file_a.size > GIT_XDIFF_MAX_SIZE ||
Packit Service 20376f
		file_b.size > GIT_XDIFF_MAX_SIZE) {
Packit Service 20376f
		giterr_set(GITERR_INVALID, "file too large to blame");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return xdl_diff(&file_a, &file_b, &xpp, &xecfg, &ecb;;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void fill_origin_blob(git_blame__origin *o, mmfile_t *file)
Packit Service 20376f
{
Packit Service 20376f
	memset(file, 0, sizeof(*file));
Packit Service 20376f
	if (o->blob) {
Packit Service 20376f
		file->ptr = (char*)git_blob_rawcontent(o->blob);
Packit Service 20376f
		file->size = (size_t)git_blob_rawsize(o->blob);
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int pass_blame_to_parent(
Packit Service 20376f
		git_blame *blame,
Packit Service 20376f
		git_blame__origin *target,
Packit Service 20376f
		git_blame__origin *parent)
Packit Service 20376f
{
Packit Service 20376f
	size_t last_in_target;
Packit Service 20376f
	mmfile_t file_p, file_o;
Packit Service 20376f
	blame_chunk_cb_data d = { blame, target, parent, 0, 0 };
Packit Service 20376f
Packit Service 20376f
	if (!find_last_in_target(&last_in_target, blame, target))
Packit Service 20376f
		return 1; /* nothing remains for this target */
Packit Service 20376f
Packit Service 20376f
	fill_origin_blob(parent, &file_p);
Packit Service 20376f
	fill_origin_blob(target, &file_o);
Packit Service 20376f
Packit Service 20376f
	if (diff_hunks(file_p, file_o, &d) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* The reset (i.e. anything after tlno) are the same as the parent */
Packit Service 20376f
	blame_chunk(blame, d.tlno, d.plno, last_in_target, target, parent);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int paths_on_dup(void **old, void *new)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(old);
Packit Service 20376f
	git__free(new);
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static git_blame__origin* find_origin(
Packit Service 20376f
		git_blame *blame,
Packit Service 20376f
		git_commit *parent,
Packit Service 20376f
		git_blame__origin *origin)
Packit Service 20376f
{
Packit Service 20376f
	git_blame__origin *porigin = NULL;
Packit Service 20376f
	git_diff *difflist = NULL;
Packit Service 20376f
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
Packit Service 20376f
	git_tree *otree=NULL, *ptree=NULL;
Packit Service 20376f
Packit Service 20376f
	/* Get the trees from this commit and its parent */
Packit Service 20376f
	if (0 != git_commit_tree(&otree, origin->commit) ||
Packit Service 20376f
	    0 != git_commit_tree(&ptree, parent))
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	/* Configure the diff */
Packit Service 20376f
	diffopts.context_lines = 0;
Packit Service 20376f
	diffopts.flags = GIT_DIFF_SKIP_BINARY_CHECK;
Packit Service 20376f
Packit Service 20376f
	/* Check to see if files we're interested have changed */
Packit Service 20376f
	diffopts.pathspec.count = blame->paths.length;
Packit Service 20376f
	diffopts.pathspec.strings = (char**)blame->paths.contents;
Packit Service 20376f
	if (0 != git_diff_tree_to_tree(&difflist, blame->repository, ptree, otree, &diffopts))
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if (!git_diff_num_deltas(difflist)) {
Packit Service 20376f
		/* No changes; copy data */
Packit Service 20376f
		git_blame__get_origin(&porigin, blame, parent, origin->path);
Packit Service 20376f
	} else {
Packit Service 20376f
		git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;
Packit Service 20376f
		int i;
Packit Service 20376f
Packit Service 20376f
		/* Generate a full diff between the two trees */
Packit Service 20376f
		git_diff_free(difflist);
Packit Service 20376f
		diffopts.pathspec.count = 0;
Packit Service 20376f
		if (0 != git_diff_tree_to_tree(&difflist, blame->repository, ptree, otree, &diffopts))
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
Packit Service 20376f
		/* Let diff find renames */
Packit Service 20376f
		findopts.flags = GIT_DIFF_FIND_RENAMES;
Packit Service 20376f
		if (0 != git_diff_find_similar(difflist, &findopts))
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
Packit Service 20376f
		/* Find one that matches */
Packit Service 20376f
		for (i=0; i<(int)git_diff_num_deltas(difflist); i++) {
Packit Service 20376f
			const git_diff_delta *delta = git_diff_get_delta(difflist, i);
Packit Service 20376f
Packit Service 20376f
			if (!git_vector_bsearch(NULL, &blame->paths, delta->new_file.path))
Packit Service 20376f
			{
Packit Service 20376f
				git_vector_insert_sorted(&blame->paths, (void*)git__strdup(delta->old_file.path),
Packit Service 20376f
						paths_on_dup);
Packit Service 20376f
				make_origin(&porigin, parent, delta->old_file.path);
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	git_diff_free(difflist);
Packit Service 20376f
	git_tree_free(otree);
Packit Service 20376f
	git_tree_free(ptree);
Packit Service 20376f
	return porigin;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * The blobs of origin and porigin exactly match, so everything origin is
Packit Service 20376f
 * suspected for can be blamed on the parent.
Packit Service 20376f
 */
Packit Service 20376f
static int pass_whole_blame(git_blame *blame,
Packit Service 20376f
		git_blame__origin *origin, git_blame__origin *porigin)
Packit Service 20376f
{
Packit Service 20376f
	git_blame__entry *e;
Packit Service 20376f
Packit Service 20376f
	if (!porigin->blob &&
Packit Service 20376f
	    git_object_lookup((git_object**)&porigin->blob, blame->repository,
Packit Service 20376f
				git_blob_id(origin->blob), GIT_OBJ_BLOB) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
	for (e=blame->ent; e; e=e->next) {
Packit Service 20376f
		if (!same_suspect(e->suspect, origin))
Packit Service 20376f
			continue;
Packit Service 20376f
		origin_incref(porigin);
Packit Service 20376f
		origin_decref(e->suspect);
Packit Service 20376f
		e->suspect = porigin;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int pass_blame(git_blame *blame, git_blame__origin *origin, uint32_t opt)
Packit Service 20376f
{
Packit Service 20376f
	git_commit *commit = origin->commit;
Packit Service 20376f
	int i, num_parents;
Packit Service 20376f
	git_blame__origin *sg_buf[16];
Packit Service 20376f
	git_blame__origin *porigin, **sg_origin = sg_buf;
Packit Service 20376f
	int ret, error = 0;
Packit Service 20376f
Packit Service 20376f
	num_parents = git_commit_parentcount(commit);
Packit Service 20376f
	if (!git_oid_cmp(git_commit_id(commit), &blame->options.oldest_commit))
Packit Service 20376f
		/* Stop at oldest specified commit */
Packit Service 20376f
		num_parents = 0;
Packit Service 20376f
	else if (opt & GIT_BLAME_FIRST_PARENT && num_parents > 1)
Packit Service 20376f
		/* Limit search to the first parent */
Packit Service 20376f
		num_parents = 1;
Packit Service 20376f
Packit Service 20376f
	if (!num_parents) {
Packit Service 20376f
		git_oid_cpy(&blame->options.oldest_commit, git_commit_id(commit));
Packit Service 20376f
		goto finish;
Packit Service 20376f
	} else if (num_parents < (int)ARRAY_SIZE(sg_buf))
Packit Service 20376f
		memset(sg_buf, 0, sizeof(sg_buf));
Packit Service 20376f
	else {
Packit Service 20376f
		sg_origin = git__calloc(num_parents, sizeof(*sg_origin));
Packit Service 20376f
		GITERR_CHECK_ALLOC(sg_origin);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	for (i=0; i
Packit Service 20376f
		git_commit *p;
Packit Service 20376f
		int j, same;
Packit Service 20376f
Packit Service 20376f
		if (sg_origin[i])
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		if ((error = git_commit_parent(&p, origin->commit, i)) < 0)
Packit Service 20376f
			goto finish;
Packit Service 20376f
		porigin = find_origin(blame, p, origin);
Packit Service 20376f
Packit Service 20376f
		if (!porigin) {
Packit Service 20376f
			/*
Packit Service 20376f
			 * We only have to decrement the parent's
Packit Service 20376f
			 * reference count when no porigin has
Packit Service 20376f
			 * been created, as otherwise the commit
Packit Service 20376f
			 * is assigned to the created object.
Packit Service 20376f
			 */
Packit Service 20376f
			git_commit_free(p);
Packit Service 20376f
			continue;
Packit Service 20376f
		}
Packit Service 20376f
		if (porigin->blob && origin->blob &&
Packit Service 20376f
		    !git_oid_cmp(git_blob_id(porigin->blob), git_blob_id(origin->blob))) {
Packit Service 20376f
			error = pass_whole_blame(blame, origin, porigin);
Packit Service 20376f
			origin_decref(porigin);
Packit Service 20376f
			goto finish;
Packit Service 20376f
		}
Packit Service 20376f
		for (j = same = 0; j
Packit Service 20376f
			if (sg_origin[j] &&
Packit Service 20376f
				 !git_oid_cmp(git_blob_id(sg_origin[j]->blob), git_blob_id(porigin->blob))) {
Packit Service 20376f
				same = 1;
Packit Service 20376f
				break;
Packit Service 20376f
			}
Packit Service 20376f
		if (!same)
Packit Service 20376f
			sg_origin[i] = porigin;
Packit Service 20376f
		else
Packit Service 20376f
			origin_decref(porigin);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Standard blame */
Packit Service 20376f
	for (i=0; i
Packit Service 20376f
		git_blame__origin *porigin = sg_origin[i];
Packit Service 20376f
		if (!porigin)
Packit Service 20376f
			continue;
Packit Service 20376f
		if (!origin->previous) {
Packit Service 20376f
			origin_incref(porigin);
Packit Service 20376f
			origin->previous = porigin;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if ((ret = pass_blame_to_parent(blame, origin, porigin)) != 0) {
Packit Service 20376f
			if (ret < 0)
Packit Service 20376f
				error = -1;
Packit Service 20376f
Packit Service 20376f
			goto finish;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* TODO: optionally find moves in parents' files */
Packit Service 20376f
Packit Service 20376f
	/* TODO: optionally find copies in parents' files */
Packit Service 20376f
Packit Service 20376f
finish:
Packit Service 20376f
	for (i=0; i
Packit Service 20376f
		if (sg_origin[i])
Packit Service 20376f
			origin_decref(sg_origin[i]);
Packit Service 20376f
	if (sg_origin != sg_buf)
Packit Service 20376f
		git__free(sg_origin);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * If two blame entries that are next to each other came from
Packit Service 20376f
 * contiguous lines in the same origin (i.e. <commit, path> pair),
Packit Service 20376f
 * merge them together.
Packit Service 20376f
 */
Packit Service 20376f
static void coalesce(git_blame *blame)
Packit Service 20376f
{
Packit Service 20376f
	git_blame__entry *ent, *next;
Packit Service 20376f
Packit Service 20376f
	for (ent=blame->ent; ent && (next = ent->next); ent = next) {
Packit Service 20376f
		if (same_suspect(ent->suspect, next->suspect) &&
Packit Service 20376f
		    ent->guilty == next->guilty &&
Packit Service 20376f
		    ent->s_lno + ent->num_lines == next->s_lno)
Packit Service 20376f
		{
Packit Service 20376f
			ent->num_lines += next->num_lines;
Packit Service 20376f
			ent->next = next->next;
Packit Service 20376f
			if (ent->next)
Packit Service 20376f
				ent->next->prev = ent;
Packit Service 20376f
			origin_decref(next->suspect);
Packit Service 20376f
			git__free(next);
Packit Service 20376f
			ent->score = 0;
Packit Service 20376f
			next = ent; /* again */
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_blame__like_git(git_blame *blame, uint32_t opt)
Packit Service 20376f
{
Packit Service 20376f
	while (true) {
Packit Service 20376f
		git_blame__entry *ent;
Packit Service 20376f
		git_blame__origin *suspect = NULL;
Packit Service 20376f
Packit Service 20376f
		/* Find a suspect to break down */
Packit Service 20376f
		for (ent = blame->ent; !suspect && ent; ent = ent->next)
Packit Service 20376f
			if (!ent->guilty)
Packit Service 20376f
				suspect = ent->suspect;
Packit Service 20376f
		if (!suspect)
Packit Service 20376f
			return 0; /* all done */
Packit Service 20376f
Packit Service 20376f
		/* We'll use this suspect later in the loop, so hold on to it for now. */
Packit Service 20376f
		origin_incref(suspect);
Packit Service 20376f
Packit Service 20376f
		if (pass_blame(blame, suspect, opt) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		/* Take responsibility for the remaining entries */
Packit Service 20376f
		for (ent = blame->ent; ent; ent = ent->next) {
Packit Service 20376f
			if (same_suspect(ent->suspect, suspect)) {
Packit Service 20376f
				ent->guilty = true;
Packit Service 20376f
				ent->is_boundary = !git_oid_cmp(
Packit Service 20376f
						git_commit_id(suspect->commit),
Packit Service 20376f
						&blame->options.oldest_commit);
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
		origin_decref(suspect);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	coalesce(blame);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_blame__free_entry(git_blame__entry *ent)
Packit Service 20376f
{
Packit Service 20376f
	if (!ent) return;
Packit Service 20376f
	origin_decref(ent->suspect);
Packit Service 20376f
	git__free(ent);
Packit Service 20376f
}