Use diff to generate commit description.

This commit is contained in:
Jonathan Xu 2018-07-10 18:54:34 +08:00
parent fc20436db2
commit 7d7116c68e
6 changed files with 32 additions and 41 deletions

View File

@ -743,10 +743,10 @@ diff_results_to_description (GList *results)
GList *p;
DiffEntry *de;
char *add_mod_file = NULL, *removed_file = NULL;
char *renamed_file = NULL;
char *renamed_file = NULL, *renamed_dir = NULL;
char *new_dir = NULL, *removed_dir = NULL;
int n_add_mod = 0, n_removed = 0, n_renamed = 0;
int n_new_dir = 0, n_removed_dir = 0;
int n_new_dir = 0, n_removed_dir = 0, n_renamed_dir = 0;
GString *desc;
if (results == NULL)
@ -770,6 +770,11 @@ diff_results_to_description (GList *results)
renamed_file = get_basename(de->name);
n_renamed++;
break;
case DIFF_STATUS_DIR_RENAMED:
if (n_renamed_dir == 0)
renamed_dir = get_basename(de->name);
n_renamed_dir++;
break;
case DIFF_STATUS_MODIFIED:
if (n_add_mod == 0)
add_mod_file = get_basename(de->name);
@ -808,6 +813,12 @@ diff_results_to_description (GList *results)
g_string_append_printf (desc, "Renamed \"%s\" and %d more files.\n",
renamed_file, n_renamed - 1);
if (n_renamed_dir == 1)
g_string_append_printf (desc, "Renamed directory \"%s\".\n", renamed_dir);
else if (n_renamed_dir > 1)
g_string_append_printf (desc, "Renamed \"%s\" and %d more directories.\n",
renamed_dir, n_renamed_dir - 1);
if (n_new_dir == 1)
g_string_append_printf (desc, "Added directory \"%s\".\n", new_dir);
else if (n_new_dir > 1)

View File

@ -266,7 +266,6 @@ changeset_free (ChangeSet *changeset)
if (!changeset)
return;
g_list_free_full (changeset->diff, (GDestroyNotify)diff_entry_free);
changeset_dir_free (changeset->tree_root);
g_regex_unref (changeset->case_conflict_pattern);
g_free (changeset);
@ -545,12 +544,6 @@ add_to_changeset (ChangeSet *changeset,
const char *path,
const char *new_path)
{
DiffEntry *de;
unsigned char allzero[20] = {0};
de = diff_entry_new (DIFF_TYPE_INDEX, status, allzero, path);
changeset->diff = g_list_prepend (changeset->diff, de);
apply_to_tree (changeset,
status, sha1, st, modifier, path, new_path);
}
@ -589,17 +582,8 @@ remove_from_changeset (ChangeSet *changeset,
char status,
const char *path,
gboolean remove_parent,
const char *top_dir,
gboolean add_to_diff)
const char *top_dir)
{
DiffEntry *de;
unsigned char allzero[20] = {0};
if (add_to_diff) {
de = diff_entry_new (DIFF_TYPE_INDEX, status, allzero, path);
changeset->diff = g_list_prepend (changeset->diff, de);
}
remove_from_changeset_recursive (changeset, path, remove_parent, top_dir);
}

View File

@ -10,8 +10,6 @@ struct _ChangeSetDir;
struct _ChangeSet {
char repo_id[37];
/* List of diff entries, used to generate commit description. */
GList *diff;
/* A partial tree for all changed directories. */
struct _ChangeSetDir *tree_root;
/* Used to match case conflict paths. */
@ -42,8 +40,7 @@ remove_from_changeset (ChangeSet *changeset,
char status,
const char *path,
gboolean remove_parent,
const char *top_dir,
gboolean add_to_diff);
const char *top_dir);
char *
commit_tree_from_changeset (ChangeSet *changeset);

View File

@ -1965,8 +1965,7 @@ remove_deleted (struct index_state *istate, const char *worktree, const char *pr
DIFF_STATUS_DIR_DELETED,
ce->name,
TRUE,
prefix,
TRUE);
prefix);
} else if (!is_empty_dir (path, ignore_list)) {
/* Don't add to changeset if empty dir became non-empty. */
ce->ce_flags |= CE_REMOVE;
@ -1987,8 +1986,7 @@ remove_deleted (struct index_state *istate, const char *worktree, const char *pr
DIFF_STATUS_DELETED,
ce->name,
TRUE,
prefix,
TRUE);
prefix);
}
}
}
@ -3201,8 +3199,7 @@ handle_rename (SeafRepo *repo, struct index_state *istate,
DIFF_STATUS_DELETED,
event->path,
FALSE,
NULL,
FALSE);
NULL);
}
return;
}
@ -3766,8 +3763,7 @@ apply_worktree_changes_to_index (SeafRepo *repo, struct index_state *istate,
DIFF_STATUS_DELETED,
event->path,
FALSE,
NULL,
TRUE);
NULL);
try_add_empty_parent_dir_entry_from_wt (repo->worktree,
istate,
@ -3926,7 +3922,7 @@ print_index (struct index_state *istate)
#endif
char *
seaf_repo_index_commit (SeafRepo *repo, const char *desc,
seaf_repo_index_commit (SeafRepo *repo,
gboolean is_force_commit,
gboolean is_initial_commit,
GError **error)
@ -3938,7 +3934,8 @@ seaf_repo_index_commit (SeafRepo *repo, const char *desc,
char *new_root_id = NULL;
char commit_id[41];
ChangeSet *changeset = NULL;
char *my_desc = NULL;
GList *diff_results = NULL;
char *desc = NULL;
char *ret = NULL;
if (!check_worktree_common (repo))
@ -3968,10 +3965,6 @@ seaf_repo_index_commit (SeafRepo *repo, const char *desc,
if (!istate.cache_changed)
goto out;
my_desc = diff_results_to_description (changeset->diff);
if (!my_desc)
my_desc = g_strdup("");
if (!is_initial_commit && !is_force_commit) {
new_root_id = commit_tree_from_changeset (changeset);
if (!new_root_id) {
@ -4020,7 +4013,12 @@ seaf_repo_index_commit (SeafRepo *repo, const char *desc,
goto out;
}
if (commit_tree (repo, new_root_id, my_desc, commit_id) < 0) {
diff_commit_roots (repo->id, repo->version, head->root_id, new_root_id, &diff_results, TRUE);
desc = diff_results_to_description (diff_results);
if (!desc)
desc = g_strdup("");
if (commit_tree (repo, new_root_id, desc, commit_id) < 0) {
seaf_warning ("Failed to save commit file");
g_set_error (error, SEAFILE_DOMAIN, SEAF_ERR_INTERNAL, "Internal error");
goto out;
@ -4036,10 +4034,11 @@ seaf_repo_index_commit (SeafRepo *repo, const char *desc,
ret = g_strdup(commit_id);
out:
g_free (my_desc);
g_free (desc);
seaf_commit_unref (head);
g_free (new_root_id);
changeset_free (changeset);
g_list_free_full (diff_results, (GDestroyNotify)diff_entry_free);
discard_index (&istate);
return ret;
}

View File

@ -142,7 +142,7 @@ GList *
seaf_repo_get_commits (SeafRepo *repo);
char *
seaf_repo_index_commit (SeafRepo *repo, const char *desc,
seaf_repo_index_commit (SeafRepo *repo,
gboolean is_force_commit,
gboolean is_initial_commit,
GError **error);

View File

@ -923,7 +923,7 @@ commit_job (void *vtask)
res->changed = TRUE;
res->success = TRUE;
char *commit_id = seaf_repo_index_commit (repo, "",
char *commit_id = seaf_repo_index_commit (repo,
task->is_manual_sync,
task->is_initial_commit,
&error);