int merge_trees(struct merge_options *o, struct tree *head, struct tree *merge, struct tree *common, struct tree **result) { int code, clean; if (o->subtree_merge) { merge = shift_tree_object(head, merge); common = shift_tree_object(head, common); } if (sha_eq(common->object.sha1, merge->object.sha1)) { output(o, 0, "Already uptodate!"); *result = head; return 1; } code = git_merge_trees(o->call_depth, common, head, merge); if (code != 0) die("merging of trees %s and %s failed", sha1_to_hex(head->object.sha1), sha1_to_hex(merge->object.sha1)); if (unmerged_cache()) { struct string_list *entries, *re_head, *re_merge; int i; string_list_clear(&o->current_file_set, 1); string_list_clear(&o->current_directory_set, 1); get_files_dirs(o, head); get_files_dirs(o, merge); entries = get_unmerged(); re_head = get_renames(o, head, common, head, merge, entries); re_merge = get_renames(o, merge, common, head, merge, entries); clean = process_renames(o, re_head, re_merge); for (i = 0; i < entries->nr; i++) { const char *path = entries->items[i].string; struct stage_data *e = entries->items[i].util; if (!e->processed && !process_entry(o, path, e)) clean = 0; } string_list_clear(re_merge, 0); string_list_clear(re_head, 0); string_list_clear(entries, 1); } else clean = 1; if (o->call_depth) *result = write_tree_from_memory(o); return clean; }
static int merge_working_tree(const struct checkout_opts *opts, struct branch_info *old_branch_info, struct branch_info *new_branch_info, int *writeout_error) { int ret; struct lock_file lock_file = LOCK_INIT; hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); if (read_cache_preload(NULL) < 0) return error(_("index file corrupt")); resolve_undo_clear(); if (opts->force) { ret = reset_tree(get_commit_tree(new_branch_info->commit), opts, 1, writeout_error); if (ret) return ret; } else { struct tree_desc trees[2]; struct tree *tree; struct unpack_trees_options topts; memset(&topts, 0, sizeof(topts)); topts.head_idx = -1; topts.src_index = &the_index; topts.dst_index = &the_index; setup_unpack_trees_porcelain(&topts, "checkout"); refresh_cache(REFRESH_QUIET); if (unmerged_cache()) { error(_("you need to resolve your current index first")); return 1; } /* 2-way merge to the new branch */ topts.initial_checkout = is_cache_unborn(); topts.update = 1; topts.merge = 1; topts.gently = opts->merge && old_branch_info->commit; topts.verbose_update = opts->show_progress; topts.fn = twoway_merge; if (opts->overwrite_ignore) { topts.dir = xcalloc(1, sizeof(*topts.dir)); topts.dir->flags |= DIR_SHOW_IGNORED; setup_standard_excludes(topts.dir); } tree = parse_tree_indirect(old_branch_info->commit ? &old_branch_info->commit->object.oid : the_hash_algo->empty_tree); init_tree_desc(&trees[0], tree->buffer, tree->size); tree = parse_tree_indirect(&new_branch_info->commit->object.oid); init_tree_desc(&trees[1], tree->buffer, tree->size); ret = unpack_trees(2, trees, &topts); clear_unpack_trees_porcelain(&topts); if (ret == -1) { /* * Unpack couldn't do a trivial merge; either * give up or do a real merge, depending on * whether the merge flag was used. */ struct tree *result; struct tree *work; struct merge_options o; if (!opts->merge) return 1; /* * Without old_branch_info->commit, the below is the same as * the two-tree unpack we already tried and failed. */ if (!old_branch_info->commit) return 1; /* Do more real merge */ /* * We update the index fully, then write the * tree from the index, then merge the new * branch with the current tree, with the old * branch as the base. Then we reset the index * (but not the working tree) to the new * branch, leaving the working tree as the * merged version, but skipping unmerged * entries in the index. */ add_files_to_cache(NULL, NULL, 0); /* * NEEDSWORK: carrying over local changes * when branches have different end-of-line * normalization (or clean+smudge rules) is * a pain; plumb in an option to set * o.renormalize? */ init_merge_options(&o, the_repository); o.verbosity = 0; work = write_tree_from_memory(&o); ret = reset_tree(get_commit_tree(new_branch_info->commit), opts, 1, writeout_error); if (ret) return ret; o.ancestor = old_branch_info->name; o.branch1 = new_branch_info->name; o.branch2 = "local"; ret = merge_trees(&o, get_commit_tree(new_branch_info->commit), work, get_commit_tree(old_branch_info->commit), &result); if (ret < 0) exit(128); ret = reset_tree(get_commit_tree(new_branch_info->commit), opts, 0, writeout_error); strbuf_release(&o.obuf); if (ret) return ret; } } if (!active_cache_tree) active_cache_tree = cache_tree(); if (!cache_tree_fully_valid(active_cache_tree)) cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR); if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) die(_("unable to write new index file")); if (!opts->force && !opts->quiet) show_local_changes(&new_branch_info->commit->object, &opts->diff_options); return 0; }
/* * Merge the commits h1 and h2, return merged tree root id * and a flag indicating the cleanness of the merge. * Return 0 if merge is done (no matter clean or not); -1 otherwise. */ int merge_recursive(struct merge_options *o, const char *h1_root, const char *h2_root, const char *ca_root, int *clean, char **root_id) { SeafDir *head, *remote, *common; int code, ret = 0; struct unpack_trees_options opts; char *error = NULL; *clean = 1; head = seaf_fs_manager_get_seafdir (seaf->fs_mgr, h1_root); remote = seaf_fs_manager_get_seafdir (seaf->fs_mgr, h2_root); common = seaf_fs_manager_get_seafdir (seaf->fs_mgr, ca_root); if (!head || !remote || !common) { g_warning ("Invalid commits!\n"); return -1; } /* Get merged index. */ code = seafile_merge_trees(o, &opts, common, head, remote, &error); if (code != 0) { ret = -1; goto out; } /* If only collect blocks, return success. */ if (o->collect_blocks_only) { process_unmerged_entries (o, head, remote); goto out; } /* Update worktree. */ /* On windows, we have to Check if any files need to be updated * are locked by other program (e.g. Office). If no file is locked, * update worktree; otherwise just quit. * * Note that if we're recovering merge on startup, we need to update * worktree no matter files are locked or not, since we cannot retry * this operation. This will produce more confusing results, but * it doesn't hurt data integrity. */ #ifdef WIN32 if (o->recover_merge || o->force_merge || !files_locked_on_windows (o->index, o->worktree)) { update_worktree (&opts, o->recover_merge, o->remote_head, o->branch2, NULL); *clean = process_unmerged_entries (o, head, remote); } else { /* Don't update anything. */ g_debug ("[merge] files are locked, quit merge now.\n"); ret = -1; goto out; } #else update_worktree (&opts, o->recover_merge, o->remote_head, o->branch2, NULL); *clean = process_unmerged_entries (o, head, remote); #endif if (*clean) *root_id = write_tree_from_memory(o); out: seaf_dir_free (head); seaf_dir_free (remote); seaf_dir_free (common); return ret; }