751 lines
23 KiB
C
751 lines
23 KiB
C
#include "cache.h"
|
|
#include "commit.h"
|
|
#include "refs.h"
|
|
#include "diff.h"
|
|
#include "diffcore.h"
|
|
#include "xdiff-interface.h"
|
|
#include "ll-merge.h"
|
|
#include "dir.h"
|
|
#include "notes.h"
|
|
#include "notes-merge.h"
|
|
#include "strbuf.h"
|
|
#include "notes-utils.h"
|
|
|
|
struct notes_merge_pair {
|
|
struct object_id obj, base, local, remote;
|
|
};
|
|
|
|
void init_notes_merge_options(struct notes_merge_options *o)
|
|
{
|
|
memset(o, 0, sizeof(struct notes_merge_options));
|
|
strbuf_init(&(o->commit_msg), 0);
|
|
o->verbosity = NOTES_MERGE_VERBOSITY_DEFAULT;
|
|
}
|
|
|
|
static int path_to_sha1(const char *path, unsigned char *sha1)
|
|
{
|
|
char hex_sha1[40];
|
|
int i = 0;
|
|
while (*path && i < 40) {
|
|
if (*path != '/')
|
|
hex_sha1[i++] = *path;
|
|
path++;
|
|
}
|
|
if (*path || i != 40)
|
|
return -1;
|
|
return get_sha1_hex(hex_sha1, sha1);
|
|
}
|
|
|
|
static int verify_notes_filepair(struct diff_filepair *p, unsigned char *sha1)
|
|
{
|
|
switch (p->status) {
|
|
case DIFF_STATUS_MODIFIED:
|
|
assert(p->one->mode == p->two->mode);
|
|
assert(!is_null_oid(&p->one->oid));
|
|
assert(!is_null_oid(&p->two->oid));
|
|
break;
|
|
case DIFF_STATUS_ADDED:
|
|
assert(is_null_oid(&p->one->oid));
|
|
break;
|
|
case DIFF_STATUS_DELETED:
|
|
assert(is_null_oid(&p->two->oid));
|
|
break;
|
|
default:
|
|
return -1;
|
|
}
|
|
assert(!strcmp(p->one->path, p->two->path));
|
|
return path_to_sha1(p->one->path, sha1);
|
|
}
|
|
|
|
static struct notes_merge_pair *find_notes_merge_pair_pos(
|
|
struct notes_merge_pair *list, int len, unsigned char *obj,
|
|
int insert_new, int *occupied)
|
|
{
|
|
/*
|
|
* Both diff_tree_remote() and diff_tree_local() tend to process
|
|
* merge_pairs in ascending order. Therefore, cache last returned
|
|
* index, and search sequentially from there until the appropriate
|
|
* position is found.
|
|
*
|
|
* Since inserts only happen from diff_tree_remote() (which mainly
|
|
* _appends_), we don't care that inserting into the middle of the
|
|
* list is expensive (using memmove()).
|
|
*/
|
|
static int last_index;
|
|
int i = last_index < len ? last_index : len - 1;
|
|
int prev_cmp = 0, cmp = -1;
|
|
while (i >= 0 && i < len) {
|
|
cmp = hashcmp(obj, list[i].obj.hash);
|
|
if (!cmp) /* obj belongs @ i */
|
|
break;
|
|
else if (cmp < 0 && prev_cmp <= 0) /* obj belongs < i */
|
|
i--;
|
|
else if (cmp < 0) /* obj belongs between i-1 and i */
|
|
break;
|
|
else if (cmp > 0 && prev_cmp >= 0) /* obj belongs > i */
|
|
i++;
|
|
else /* if (cmp > 0) */ { /* obj belongs between i and i+1 */
|
|
i++;
|
|
break;
|
|
}
|
|
prev_cmp = cmp;
|
|
}
|
|
if (i < 0)
|
|
i = 0;
|
|
/* obj belongs at, or immediately preceding, index i (0 <= i <= len) */
|
|
|
|
if (!cmp)
|
|
*occupied = 1;
|
|
else {
|
|
*occupied = 0;
|
|
if (insert_new && i < len) {
|
|
memmove(list + i + 1, list + i,
|
|
(len - i) * sizeof(struct notes_merge_pair));
|
|
memset(list + i, 0, sizeof(struct notes_merge_pair));
|
|
}
|
|
}
|
|
last_index = i;
|
|
return list + i;
|
|
}
|
|
|
|
static struct object_id uninitialized = {
|
|
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" \
|
|
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
|
|
};
|
|
|
|
static struct notes_merge_pair *diff_tree_remote(struct notes_merge_options *o,
|
|
const unsigned char *base,
|
|
const unsigned char *remote,
|
|
int *num_changes)
|
|
{
|
|
struct diff_options opt;
|
|
struct notes_merge_pair *changes;
|
|
int i, len = 0;
|
|
|
|
trace_printf("\tdiff_tree_remote(base = %.7s, remote = %.7s)\n",
|
|
sha1_to_hex(base), sha1_to_hex(remote));
|
|
|
|
diff_setup(&opt);
|
|
DIFF_OPT_SET(&opt, RECURSIVE);
|
|
opt.output_format = DIFF_FORMAT_NO_OUTPUT;
|
|
diff_setup_done(&opt);
|
|
diff_tree_sha1(base, remote, "", &opt);
|
|
diffcore_std(&opt);
|
|
|
|
changes = xcalloc(diff_queued_diff.nr, sizeof(struct notes_merge_pair));
|
|
|
|
for (i = 0; i < diff_queued_diff.nr; i++) {
|
|
struct diff_filepair *p = diff_queued_diff.queue[i];
|
|
struct notes_merge_pair *mp;
|
|
int occupied;
|
|
unsigned char obj[20];
|
|
|
|
if (verify_notes_filepair(p, obj)) {
|
|
trace_printf("\t\tCannot merge entry '%s' (%c): "
|
|
"%.7s -> %.7s. Skipping!\n", p->one->path,
|
|
p->status, oid_to_hex(&p->one->oid),
|
|
oid_to_hex(&p->two->oid));
|
|
continue;
|
|
}
|
|
mp = find_notes_merge_pair_pos(changes, len, obj, 1, &occupied);
|
|
if (occupied) {
|
|
/* We've found an addition/deletion pair */
|
|
assert(!hashcmp(mp->obj.hash, obj));
|
|
if (is_null_oid(&p->one->oid)) { /* addition */
|
|
|