/* * Check whether the current head of @repo is consistent (including fs and block), * if not, find and reset its head to the last consistent commit. * Note that this procedure will not work with a corrupted commit object. */ static void check_and_reset_consistent_state (SeafRepo *repo) { FsckRes res; SeafCommit *rep_commit; SeafCommit *new_commit; seaf_message ("Checking file system integrity of repo %s(%.8s)...\n", repo->name, repo->id); memset (&res, 0, sizeof(res)); res.repo = repo; res.existing_blocks = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); seaf_commit_manager_traverse_commit_tree (seaf->commit_mgr, repo->id, repo->version, repo->head->commit_id, check_fs_integrity, &res, TRUE); g_hash_table_destroy (res.existing_blocks); if (!res.consistent_head) { recover_corrupted_repo_head (repo->id); return; } /* If the current head is not consistent, reset it. */ if (strcmp (res.consistent_head, repo->head->commit_id) != 0) { rep_commit = seaf_commit_manager_get_commit (seaf->commit_mgr, repo->id, repo->version, res.consistent_head); if (rep_commit) { new_commit = cre_commit_from_parent (repo->id, rep_commit); if (new_commit == NULL) { seaf_warning ("Failed to update branch head.\n"); } else { seaf_message ("Resetting head of repo %.8s to commit %.8s.\n", repo->id, new_commit->commit_id); seaf_branch_set_commit (repo->head, new_commit->commit_id); if (seaf_branch_manager_update_branch (seaf->branch_mgr, repo->head) < 0) { seaf_warning ("Failed to update branch head.\n"); } else { seaf_commit_manager_add_commit (seaf->commit_mgr, new_commit); } seaf_commit_unref (new_commit); } seaf_commit_unref (rep_commit); } else { seaf_warning ("Failed to update branch head.\n"); } } g_free (res.consistent_head); }
static int do_real_merge (SeafRepo *repo, SeafBranch *head_branch, SeafCommit *head, SeafBranch *remote_branch, SeafCommit *remote, SeafCommit *common, gboolean recover_merge, char **error) { struct merge_options opts; char index_path[SEAF_PATH_MAX]; struct index_state istate; char *root_id = NULL; SeafCommit *merged; int ret = 0, clean; memset (&istate, 0, sizeof(istate)); snprintf (index_path, SEAF_PATH_MAX, "%s/%s", repo->manager->index_dir, repo->id); if (read_index_from (&istate, index_path) < 0) { g_warning ("Failed to load index.\n"); *error = g_strdup ("Internal error.\n"); return -1; } init_merge_options (&opts); opts.index = &istate; opts.worktree = repo->worktree; opts.ancestor = "common ancestor"; opts.branch1 = seaf->session->base.user_name; opts.branch2 = remote->creator_name; opts.remote_head = remote->commit_id; opts.recover_merge = recover_merge; if (repo->encrypted) { opts.crypt = seafile_crypt_new (repo->enc_version, repo->enc_key, repo->enc_iv); } ret = merge_recursive (&opts, head->root_id, remote->root_id, common->root_id, &clean, &root_id); if (ret < 0) goto out; if (update_index (&istate, index_path) < 0) { *error = g_strdup ("Internal error.\n"); ret = -1; goto out; } if (clean) { merged = seaf_commit_new (NULL, repo->id, root_id, repo->email ? repo->email : seaf->session->base.user_name, seaf->session->base.id, "Auto merge by seafile system", 0); merged->parent_id = g_strdup(head->commit_id); merged->second_parent_id = g_strdup(remote->commit_id); seaf_repo_to_commit (repo, merged); if (seaf_commit_manager_add_commit (seaf->commit_mgr, merged) < 0) { seaf_commit_unref (merged); *error = g_strdup ("Internal error.\n"); ret = -1; goto out; } seaf_branch_set_commit (head_branch, merged->commit_id); seaf_branch_manager_update_branch (seaf->branch_mgr, head_branch); g_debug ("Auto merged.\n"); seaf_commit_unref (merged); } else { ret = -1; g_debug ("Auto merge failed.\n"); } out: if (root_id) g_free (root_id); g_free (opts.crypt); clear_merge_options (&opts); discard_index (&istate); return ret; }
static void enable_sync_repo (const char *repo_id) { SeafRepo *repo = NULL; SeafCommit *parent_commit = NULL; SeafCommit *new_commit = NULL; gboolean exists; if (!is_uuid_valid (repo_id)) { seaf_warning ("Invalid repo id %s.\n", repo_id); return; } exists = seaf_repo_manager_repo_exists (seaf->repo_mgr, repo_id); if (!exists) { seaf_warning ("Repo %.8s doesn't exist.\n", repo_id); return; } repo = seaf_repo_manager_get_repo (seaf->repo_mgr, repo_id); if (!repo) return; if (!repo->repaired) { seaf_repo_unref (repo); return; } seaf_message ("Enabling sync repo %s.\n", repo_id); parent_commit = seaf_commit_manager_get_commit_compatible (seaf->commit_mgr, repo_id, repo->head->commit_id); if (!parent_commit) { seaf_warning ("Commit %s:%s is missing\n", repo_id, repo->head->commit_id); goto out; } new_commit = seaf_commit_new (NULL, repo_id, parent_commit->root_id, parent_commit->creator_name, parent_commit->creator_id, "Enable sync repo", 0); if (!new_commit) { seaf_warning ("Out of memory when create commit.\n"); goto out; } new_commit->parent_id = g_strdup (parent_commit->commit_id); seaf_repo_to_commit (repo, new_commit); new_commit->repaired = FALSE; if (seaf_commit_manager_add_commit (seaf->commit_mgr, new_commit) < 0) { seaf_warning ("Failed to save commit %.8s for repo %.8s.\n", new_commit->commit_id, repo_id); goto out; } seaf_branch_set_commit (repo->head, new_commit->commit_id); if (seaf_branch_manager_update_branch (seaf->branch_mgr, repo->head) < 0) { seaf_warning ("Failed to update head commit %.8s to repo %.8s.\n", new_commit->commit_id, repo_id); } else { seaf_message ("Enable sync repo %.8s success.\n", repo_id); } out: if (parent_commit) seaf_commit_unref (parent_commit); if (new_commit) seaf_commit_unref (new_commit); if (repo) seaf_repo_unref (repo); }
int merge_branches (SeafRepo *repo, SeafBranch *remote_branch, char **error, gboolean *real_merge) { SeafCommit *common = NULL; SeafCommit *head, *remote; int ret = 0; SeafRepoMergeInfo minfo; g_assert (repo && remote_branch && error); *real_merge = FALSE; memset (&minfo, 0, sizeof(minfo)); if (seaf_repo_manager_get_merge_info (repo->manager, repo->id, &minfo) < 0) { g_warning ("Failed to get merge status of repo %s.\n", repo->id); return -1; } head = seaf_commit_manager_get_commit (seaf->commit_mgr, repo->head->commit_id); if (!head) { *error = g_strdup("Internal error: current branch corrupted.\n"); return -1; } remote = seaf_commit_manager_get_commit (seaf->commit_mgr, remote_branch->commit_id); if (!remote) { *error = g_strdup("Invalid remote branch.\n"); ret = -1; goto free_head; } /* Are we going to recover from the last interrupted merge? */ if (minfo.in_merge) { /* We don't need to recover 2 cases, since the last merge was actually finished. * - "master" and "local" are the same; * - index is unmerged. * * The first case is a clean merge; the second case is unclean merge. */ if (strcmp (head->commit_id, remote->commit_id) == 0 || seaf_repo_is_index_unmerged (repo)) { seaf_repo_manager_clear_merge (repo->manager, repo->id); goto free_head; } } /* We use the same logic for normal merge and recover. */ /* Set in_merge state. */ seaf_repo_manager_set_merge (repo->manager, repo->id, remote_branch->commit_id); common = get_merge_base (head, remote); if (!common) { g_warning ("Cannot find common ancestor\n"); *error = g_strdup ("Cannot find common ancestor\n"); ret = -1; goto free_remote; } /* printf ("common commit id is %s.\n", common->commit_id); */ if (strcmp(common->commit_id, remote->commit_id) == 0) { /* We are already up to date. */ g_debug ("Already up to date.\n"); } else if (strcmp(common->commit_id, head->commit_id) == 0) { /* Fast forward. */ if (seaf_repo_checkout_commit (repo, remote, minfo.in_merge, error) < 0) { ret = -1; goto out; } seaf_branch_set_commit (repo->head, remote->commit_id); seaf_branch_manager_update_branch (seaf->branch_mgr, repo->head); /* Repo info on the client is in memory. */ g_free (repo->name); repo->name = g_strdup(remote->repo_name); g_free (repo->desc); repo->desc = g_strdup(remote->repo_desc); g_debug ("Fast forward.\n"); } else { /* Not up-to-date and ff, we need a real merge. */ *real_merge = TRUE; ret = do_real_merge (repo, repo->head, head, remote_branch, remote, common, minfo.in_merge, error); } out: /* Clear in_merge state, no matter clean or not. */ seaf_repo_manager_clear_merge (repo->manager, repo->id); seaf_commit_unref (common); free_remote: seaf_commit_unref (remote); free_head: seaf_commit_unref (head); return ret; }