void git_blame__like_git(git_blame *blame, uint32_t opt) { while (true) { git_blame__entry *ent; git_blame__origin *suspect = NULL; /* Find a suspect to break down */ for (ent = blame->ent; !suspect && ent; ent = ent->next) if (!ent->guilty) suspect = ent->suspect; if (!suspect) return; /* all done */ /* We'll use this suspect later in the loop, so hold on to it for now. */ origin_incref(suspect); pass_blame(blame, suspect, opt); /* Take responsibility for the remaining entries */ for (ent = blame->ent; ent; ent = ent->next) { if (same_suspect(ent->suspect, suspect)) { ent->guilty = true; ent->is_boundary = !git_oid_cmp( git_commit_id(suspect->commit), &blame->options.oldest_commit); } } origin_decref(suspect); } coalesce(blame); }
/* * src typically is on-stack; we want to copy the information in it to * a malloced blame_entry that is already on the linked list of the scoreboard. * The origin of dst loses a refcnt while the origin of src gains one. */ static void dup_entry(git_blame__entry *dst, git_blame__entry *src) { git_blame__entry *p, *n; p = dst->prev; n = dst->next; origin_incref(src->suspect); origin_decref(dst->suspect); memcpy(dst, src, sizeof(*src)); dst->prev = p; dst->next = n; dst->score = 0; }
/* * It is known that lines between tlno to same came from parent, and e * has an overlap with that range. it also is known that parent's * line plno corresponds to e's line tlno. * * <---- e -----> * <------> (entirely within) * <------------> (extends past) * <------------> (starts before) * <------------------> (entirely encloses) * * Split e into potentially three parts; before this chunk, the chunk * to be blamed for the parent, and after that portion. */ static void split_overlap(git_blame__entry *split, git_blame__entry *e, size_t tlno, size_t plno, size_t same, git_blame__origin *parent) { size_t chunk_end_lno; if (e->s_lno < tlno) { /* there is a pre-chunk part not blamed on the parent */ split[0].suspect = origin_incref(e->suspect); split[0].lno = e->lno; split[0].s_lno = e->s_lno; split[0].num_lines = tlno - e->s_lno; split[1].lno = e->lno + tlno - e->s_lno; split[1].s_lno = plno; } else { split[1].lno = e->lno; split[1].s_lno = plno + (e->s_lno - tlno); } if (same < e->s_lno + e->num_lines) { /* there is a post-chunk part not blamed on parent */ split[2].suspect = origin_incref(e->suspect); split[2].lno = e->lno + (same - e->s_lno); split[2].s_lno = e->s_lno + (same - e->s_lno); split[2].num_lines = e->s_lno + e->num_lines - same; chunk_end_lno = split[2].lno; } else { chunk_end_lno = e->lno + e->num_lines; } split[1].num_lines = chunk_end_lno - split[1].lno; /* * if it turns out there is nothing to blame the parent for, forget about * the splitting. !split[1].suspect signals this. */ if (split[1].num_lines < 1) return; split[1].suspect = origin_incref(parent); }
/* 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); }
/* * The blobs of origin and porigin exactly match, so everything origin is * suspected for can be blamed on the parent. */ static void pass_whole_blame(git_blame *blame, git_blame__origin *origin, git_blame__origin *porigin) { git_blame__entry *e; if (!porigin->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; origin_incref(porigin); origin_decref(e->suspect); e->suspect = porigin; } }
/* * Link in a new blame entry to the scoreboard. Entries that cover the same * line range have been removed from the scoreboard previously. */ static void add_blame_entry(git_blame *blame, git_blame__entry *e) { git_blame__entry *ent, *prev = NULL; origin_incref(e->suspect); for (ent = blame->ent; ent && ent->lno < e->lno; ent = ent->next) prev = ent; /* prev, if not NULL, is the last one that is below e */ e->prev = prev; if (prev) { e->next = prev->next; prev->next = e; } else { e->next = blame->ent; blame->ent = e; } if (e->next) e->next->prev = e; }
static void pass_blame(git_blame *blame, git_blame__origin *origin, uint32_t opt) { git_commit *commit = origin->commit; int i, num_parents; git_blame__origin *sg_buf[16]; git_blame__origin *porigin, **sg_origin = sg_buf; num_parents = git_commit_parentcount(commit); if (!git_oid_cmp(git_commit_id(commit), &blame->options.oldest_commit)) /* Stop at oldest specified commit */ num_parents = 0; else if (opt & GIT_BLAME_FIRST_PARENT && num_parents > 1) /* Limit search to the first parent */ num_parents = 1; if (!num_parents) { git_oid_cpy(&blame->options.oldest_commit, git_commit_id(commit)); goto finish; } else if (num_parents < (int)ARRAY_SIZE(sg_buf)) memset(sg_buf, 0, sizeof(sg_buf)); else sg_origin = (git_blame__origin **) git__calloc(num_parents, sizeof(*sg_origin)); for (i=0; i<num_parents; i++) { git_commit *p; int j, same; if (sg_origin[i]) continue; git_commit_parent(&p, origin->commit, i); porigin = find_origin(blame, p, origin); if (!porigin) continue; if (porigin->blob && origin->blob && !git_oid_cmp(git_blob_id(porigin->blob), git_blob_id(origin->blob))) { pass_whole_blame(blame, origin, porigin); origin_decref(porigin); goto finish; } for (j = same = 0; j<i; j++) if (sg_origin[j] && !git_oid_cmp(git_blob_id(sg_origin[j]->blob), git_blob_id(porigin->blob))) { same = 1; break; } if (!same) sg_origin[i] = porigin; else origin_decref(porigin); } /* Standard blame */ for (i=0; i<num_parents; i++) { git_blame__origin *porigin = sg_origin[i]; if (!porigin) continue; if (!origin->previous) { origin_incref(porigin); origin->previous = porigin; } if (pass_blame_to_parent(blame, origin, porigin)) goto finish; } /* TODO: optionally find moves in parents' files */ /* TODO: optionally find copies in parents' files */ finish: for (i=0; i<num_parents; i++) if (sg_origin[i]) origin_decref(sg_origin[i]); if (sg_origin != sg_buf) git__free(sg_origin); return; }