Download raw body.
show original content in merge conflicts
On Fri, Oct 11, 2019 at 12:29:04PM +0200, Stefan Sperling wrote: > On Fri, Oct 11, 2019 at 11:12:09AM +0200, Sebastien Marie wrote: > > Do you think it would be valuable to expand it more than just the commit id, > > with terms like "workdir", "base" or "commit" ? or does it would just add > > confusion for others people or eventually some tools ? > > > > <<<<<<< commit 1234.... > > content of new file in repository > > ||||||| base 1235.... > > content of original file in repository > > ======= workdir > > content of locally modified file > > >>>>>>> > > Yes, I agree that improving the labels like this would be nice. > I will try to work on that soon. Coming back to this suggestion from you, how is this? <<<<<<< merged change: commit 1234abc.... content of new file in repository ||||||| 3-way merge base: commit 5678def.... content of original file in repository ======= content of locally modified file >>>>>>> I think the most important information to display is the commit IDs and what they represent in terms of input into the merge algorithm. I have left the ======= and >>>>>>> without further context annotation. The user already knows the path or directory of the affected file since they are editing it or are reading a diff of it, so I don't see the point of showing the filename here. Does this diff look good? diff refs/heads/master refs/heads/labels blob - d8e2403ac8b60843492a2228a507dad1ac532791 blob + 4e9889d1ca69a14aedc6dcae7c29125180dae6cc --- lib/diff3.c +++ lib/diff3.c @@ -146,7 +146,9 @@ struct diff3_state { * is stored in last[1-3]; */ int last[4]; - char f1mark[PATH_MAX], f3mark[PATH_MAX]; /* markers for -E and -X */ + char f1mark[PATH_MAX]; + char f2mark[PATH_MAX]; + char f3mark[PATH_MAX]; char *buf; @@ -173,7 +175,7 @@ static const struct got_error *repos(int, struct diff3 static const struct got_error *increase(struct diff3_state *); static const struct got_error *diff3_internal(char *, char *, char *, char *, char *, const char *, const char *, struct diff3_state *, - const char *, const char *); + const char *, const char *, const char *); static const struct got_error * diff_output(BUF *diffbuf, const char *fmt, ...) @@ -269,7 +271,7 @@ done: */ const struct got_error * got_merge_diff3(int *overlapcnt, int outfd, const char *p1, const char *p2, - const char *p3, const char *label1, const char *label3) + const char *p3, const char *label1, const char *label2, const char *label3) { const struct got_error *err = NULL; char *dp13, *dp23, *path1, *path2, *path3; @@ -367,7 +369,7 @@ got_merge_diff3(int *overlapcnt, int outfd, const char d3s->diffbuf = diffb; err = diff3_internal(dp13, dp23, path1, path2, path3, - label1, label3, d3s, label1, label3); + label1, label3, d3s, label1, label2, label3); if (err) { buf_free(diffb); diffb = NULL; @@ -422,19 +424,27 @@ out: static const struct got_error * diff3_internal(char *dp13, char *dp23, char *path1, char *path2, char *path3, const char *fmark, const char *rmark, struct diff3_state *d3s, - const char *label1, const char *label3) + const char *label1, const char *label2, const char *label3) { const struct got_error *err = NULL; ssize_t m, n; int i; i = snprintf(d3s->f1mark, sizeof(d3s->f1mark), - "%s %s", GOT_DIFF_CONFLICT_MARKER_BEGIN, label1); + "%s%s%s", GOT_DIFF_CONFLICT_MARKER_BEGIN, + label1 ? " " : "", label1 ? label1 : ""); if (i < 0 || i >= (int)sizeof(d3s->f1mark)) return got_error(GOT_ERR_NO_SPACE); + i = snprintf(d3s->f2mark, sizeof(d3s->f2mark), + "%s%s%s", GOT_DIFF_CONFLICT_MARKER_ORIG, + label2 ? " " : "", label2 ? label2 : ""); + if (i < 0 || i >= (int)sizeof(d3s->f2mark)) + return got_error(GOT_ERR_NO_SPACE); + i = snprintf(d3s->f3mark, sizeof(d3s->f3mark), - "%s %s", GOT_DIFF_CONFLICT_MARKER_END, label3); + "%s%s%s", GOT_DIFF_CONFLICT_MARKER_END, + label3 ? " " : "", label3 ? label3 : ""); if (i < 0 || i >= (int)sizeof(d3s->f3mark)) return got_error(GOT_ERR_NO_SPACE); @@ -983,8 +993,7 @@ edscript(int n, struct diff3_state *d3s) } else if (d3s->de[n].oldo.from < d3s->de[n].oldo.to) { /* Output a block of 3-way diff base file content. */ err = diff_output(d3s->diffbuf, "%da\n%s\n", - d3s->de[n].old.to -1, - GOT_DIFF_CONFLICT_MARKER_ORIG); + d3s->de[n].old.to - 1, d3s->f2mark); if (err) return err; if (fseeko(d3s->fp[1], d3s->de[n].oldo.from, SEEK_SET) blob - 0d74b1bbdb78b195fcfd6b16c5042af2a781aa5f blob + ace2137e04157dfffb2d383748e8d9561c074ac1 --- lib/got_lib_diff.h +++ lib/got_lib_diff.h @@ -150,7 +150,7 @@ const struct got_error *got_diff_blob_file_lines_chang void got_diff_free_changes(struct got_diff_changes *); const struct got_error *got_merge_diff3(int *, int, const char *, const char *, - const char *, const char *, const char *); + const char *, const char *, const char *, const char *); const struct got_error *got_diff_files(struct got_diff_changes **, struct got_diff_state **, struct got_diff_args **, int *, FILE *, size_t, blob - e289542324a5a24dba52bcccb0d657378875d631 blob + c4df32a19603acd84d6a098d33892f92581f2f71 --- lib/worktree.c +++ lib/worktree.c @@ -60,6 +60,9 @@ #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b)) #endif +#define GOT_MERGE_LABEL_MERGED "merged change" +#define GOT_MERGE_LABEL_BASE "3-way merge base" + static const struct got_error * create_meta_file(const char *path_got, const char *name, const char *content) { @@ -728,7 +731,8 @@ static const struct got_error * merge_file(int *local_changes_subsumed, struct got_worktree *worktree, struct got_blob_object *blob_orig, const char *ondisk_path, const char *path, uint16_t st_mode, const char *deriv_path, - const char *label_deriv, struct got_repository *repo, + const char *label_orig, const char *label_deriv, + struct got_repository *repo, got_worktree_checkout_cb progress_cb, void *progress_arg) { const struct got_error *err = NULL; @@ -776,7 +780,7 @@ merge_file(int *local_changes_subsumed, struct got_wor } err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path, - blob_orig_path, ondisk_path, label_deriv, path); + blob_orig_path, ondisk_path, label_deriv, label_orig, NULL); if (err) goto done; @@ -832,10 +836,10 @@ done: static const struct got_error * merge_blob(int *local_changes_subsumed, struct got_worktree *worktree, struct got_blob_object *blob_orig, const char *ondisk_path, - const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv, - struct got_object_id *deriv_base_commit_id, - struct got_repository *repo, got_worktree_checkout_cb progress_cb, - void *progress_arg) + const char *path, uint16_t st_mode, const char *label_orig, + struct got_blob_object *blob_deriv, + struct got_object_id *deriv_base_commit_id, struct got_repository *repo, + got_worktree_checkout_cb progress_cb, void *progress_arg) { const struct got_error *err = NULL; FILE *f_deriv = NULL; @@ -866,14 +870,15 @@ merge_blob(int *local_changes_subsumed, struct got_wor err = got_object_id_str(&id_str, deriv_base_commit_id); if (err) goto done; - if (asprintf(&label_deriv, "commit %s", id_str) == -1) { + if (asprintf(&label_deriv, "%s: commit %s", + GOT_MERGE_LABEL_MERGED, id_str) == -1) { err = got_error_from_errno("asprintf"); goto done; } err = merge_file(local_changes_subsumed, worktree, blob_orig, - ondisk_path, path, st_mode, blob_deriv_path, label_deriv, - repo, progress_cb, progress_arg); + ondisk_path, path, st_mode, blob_deriv_path, label_orig, + label_deriv, repo, progress_cb, progress_arg); done: if (f_deriv && fclose(f_deriv) != 0 && err == NULL) err = got_error_from_errno("fclose"); @@ -1280,6 +1285,7 @@ update_blob(struct got_worktree *worktree, if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) { int update_timestamps; struct got_blob_object *blob2 = NULL; + char *label_orig = NULL; if (got_fileindex_entry_has_blob(ie)) { struct got_object_id id2; memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH); @@ -1287,10 +1293,24 @@ update_blob(struct got_worktree *worktree, if (err) goto done; } + if (got_fileindex_entry_has_commit(ie)) { + char id_str[SHA1_DIGEST_STRING_LENGTH]; + if (got_sha1_digest_to_str(ie->commit_sha1, id_str, + sizeof(id_str)) == NULL) { + err = got_error(GOT_ERR_BAD_OBJ_ID_STR); + goto done; + } + if (asprintf(&label_orig, "%s: commit %s", + GOT_MERGE_LABEL_BASE, id_str) == -1) { + err = got_error_from_errno("asprintf"); + goto done; + } + } err = merge_blob(&update_timestamps, worktree, blob2, - ondisk_path, path, sb.st_mode, blob, + ondisk_path, path, sb.st_mode, label_orig, blob, worktree->base_commit_id, repo, progress_cb, progress_arg); + free(label_orig); if (blob2) got_object_blob_close(blob2); if (err) @@ -1979,6 +1999,7 @@ struct merge_file_cb_arg { void *progress_arg; got_cancel_cb cancel_cb; void *cancel_arg; + const char *label_orig; struct got_object_id *commit_id2; }; @@ -2025,8 +2046,8 @@ merge_file_cb(void *arg, struct got_blob_object *blob1 } err = merge_blob(&local_changes_subsumed, a->worktree, blob1, - ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo, - a->progress_cb, a->progress_arg); + ondisk_path, path2, sb.st_mode, a->label_orig, blob2, + a->commit_id2, repo, a->progress_cb, a->progress_arg); } else if (blob1) { ie = got_fileindex_entry_get(a->fileindex, path1, strlen(path1)); @@ -2099,9 +2120,10 @@ merge_file_cb(void *arg, struct got_blob_object *blob1 goto done; } err = merge_blob(&local_changes_subsumed, a->worktree, - NULL, ondisk_path, path2, sb.st_mode, blob2, - a->commit_id2, repo, - a->progress_cb, a->progress_arg); + NULL, ondisk_path, path2, sb.st_mode, + a->label_orig, blob2, a->commit_id2, repo, + a->progress_cb, + a->progress_arg); if (status == GOT_STATUS_DELETE) { err = update_blob_fileindex_entry(a->worktree, a->fileindex, ie, ondisk_path, ie->path, @@ -2178,8 +2200,11 @@ merge_files(struct got_worktree *worktree, struct got_ struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL; struct got_tree_object *tree1 = NULL, *tree2 = NULL; struct merge_file_cb_arg arg; + char *label_orig = NULL; if (commit_id1) { + char *id_str; + err = got_object_id_by_path(&tree_id1, repo, commit_id1, worktree->path_prefix); if (err) @@ -2188,6 +2213,18 @@ merge_files(struct got_worktree *worktree, struct got_ err = got_object_open_as_tree(&tree1, repo, tree_id1); if (err) goto done; + + err = got_object_id_str(&id_str, commit_id1); + if (err) + goto done; + + if (asprintf(&label_orig, "%s: commit %s", + GOT_MERGE_LABEL_BASE, id_str) == -1) { + err = got_error_from_errno("asprintf"); + free(id_str); + goto done; + } + free(id_str); } err = got_object_id_by_path(&tree_id2, repo, commit_id2, @@ -2205,6 +2242,7 @@ merge_files(struct got_worktree *worktree, struct got_ arg.progress_arg = progress_arg; arg.cancel_cb = cancel_cb; arg.cancel_arg = cancel_arg; + arg.label_orig = label_orig; arg.commit_id2 = commit_id2; err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1); sync_err = sync_fileindex(fileindex, fileindex_path); @@ -2215,6 +2253,7 @@ done: got_object_tree_close(tree1); if (tree2) got_object_tree_close(tree2); + free(label_orig); return err; } @@ -6227,6 +6266,7 @@ unstage_path(void *arg, unsigned char status, struct got_blob_object *blob_base = NULL, *blob_staged = NULL; char *ondisk_path = NULL, *path_unstaged_content = NULL; char *path_new_staged_content = NULL; + char *id_str = NULL, *label_orig = NULL; int local_changes_subsumed; struct stat sb; @@ -6243,6 +6283,16 @@ unstage_path(void *arg, unsigned char status, == -1) return got_error_from_errno("asprintf"); + err = got_object_id_str(&id_str, + commit_id ? commit_id : a->worktree->base_commit_id); + if (err) + goto done; + if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE, + id_str) == -1) { + err = got_error_from_errno("asprintf"); + goto done; + } + switch (staged_status) { case GOT_STATUS_MODIFY: err = got_object_open_as_blob(&blob_base, a->repo, @@ -6282,8 +6332,9 @@ unstage_path(void *arg, unsigned char status, err = merge_file(&local_changes_subsumed, a->worktree, blob_base, ondisk_path, relpath, got_fileindex_perms_to_st(ie), - path_unstaged_content, "unstaged", - a->repo, a->progress_cb, a->progress_arg); + path_unstaged_content, label_orig, + "unstaged", a->repo, a->progress_cb, + a->progress_arg); if (err == NULL && path_new_staged_content == NULL) got_fileindex_entry_stage_set(ie, @@ -6297,7 +6348,7 @@ unstage_path(void *arg, unsigned char status, break; err = merge_blob(&local_changes_subsumed, a->worktree, blob_base, ondisk_path, relpath, - got_fileindex_perms_to_st(ie), blob_staged, + got_fileindex_perms_to_st(ie), label_orig, blob_staged, commit_id ? commit_id : a->worktree->base_commit_id, a->repo, a->progress_cb, a->progress_arg); if (err == NULL) @@ -6325,7 +6376,7 @@ unstage_path(void *arg, unsigned char status, err = (*a->progress_cb)(a->progress_arg, status, relpath); break; } - +done: free(ondisk_path); if (path_unstaged_content && unlink(path_unstaged_content) == -1 && err == NULL) @@ -6339,6 +6390,8 @@ unstage_path(void *arg, unsigned char status, got_object_blob_close(blob_base); if (blob_staged) got_object_blob_close(blob_staged); + free(id_str); + free(label_orig); return err; } blob - 31587c10a62466ca183a27d0985a8816a43d7053 blob + 43a0f3a840e30d3749f4690d900e34f9cc447465 --- regress/cmdline/diff.sh +++ regress/cmdline/diff.sh @@ -102,6 +102,7 @@ function test_diff_shows_conflict { echo "8" >> $testroot/repo/numbers (cd $testroot/repo && git add numbers) git_commit $testroot/repo -m "added numbers file" + local base_commit=`git_show_head $testroot/repo` got checkout $testroot/repo $testroot/wt > /dev/null ret="$?" @@ -142,25 +143,29 @@ function test_diff_shows_conflict { echo '+++ numbers' >> $testroot/stdout.expected echo '@@ -1,8 +1,20 @@' >> $testroot/stdout.expected echo ' 1' >> $testroot/stdout.expected - echo "+<<<<<<< commit $head_rev" >> $testroot/stdout.expected + echo "+<<<<<<< merged change: commit $head_rev" \ + >> $testroot/stdout.expected echo ' 22' >> $testroot/stdout.expected - echo '+|||||||' >> $testroot/stdout.expected + echo "+||||||| 3-way merge base: commit $base_commit" \ + >> $testroot/stdout.expected echo '+2' >> $testroot/stdout.expected echo '+=======' >> $testroot/stdout.expected echo '+77' >> $testroot/stdout.expected - echo '+>>>>>>> numbers' >> $testroot/stdout.expected + echo '+>>>>>>>' >> $testroot/stdout.expected echo ' 3' >> $testroot/stdout.expected echo ' 4' >> $testroot/stdout.expected echo ' 5' >> $testroot/stdout.expected echo ' 6' >> $testroot/stdout.expected echo ' 7' >> $testroot/stdout.expected - echo "+<<<<<<< commit $head_rev" >> $testroot/stdout.expected + echo "+<<<<<<< merged change: commit $head_rev" \ + >> $testroot/stdout.expected echo ' 33' >> $testroot/stdout.expected - echo '+|||||||' >> $testroot/stdout.expected + echo "+||||||| 3-way merge base: commit $base_commit" \ + >> $testroot/stdout.expected echo '+8' >> $testroot/stdout.expected echo '+=======' >> $testroot/stdout.expected echo '+88' >> $testroot/stdout.expected - echo '+>>>>>>> numbers' >> $testroot/stdout.expected + echo '+>>>>>>>' >> $testroot/stdout.expected (cd $testroot/wt && got diff > $testroot/stdout) blob - c94727a0b05067fb3a4deadd1017588265245e68 blob + 62a0455a66d4559f543efeddcfbe821bfa742d26 --- regress/cmdline/rebase.sh +++ regress/cmdline/rebase.sh @@ -176,6 +176,7 @@ function test_rebase_ancestry_check { function test_rebase_continue { local testroot=`test_init rebase_continue` + local init_commit=`git_show_head $testroot/repo` (cd $testroot/repo && git checkout -q -b newbranch) echo "modified alpha on branch" > $testroot/repo/alpha @@ -216,13 +217,15 @@ function test_rebase_continue { return 1 fi - echo "<<<<<<< commit $orig_commit1" > $testroot/content.expected + echo "<<<<<<< merged change: commit $orig_commit1" \ + > $testroot/content.expected echo "modified alpha on branch" >> $testroot/content.expected - echo "|||||||" >> $testroot/content.expected + echo "||||||| 3-way merge base: commit $init_commit" \ + >> $testroot/content.expected echo "alpha" >> $testroot/content.expected echo "=======" >> $testroot/content.expected echo "modified alpha on master" >> $testroot/content.expected - echo '>>>>>>> alpha' >> $testroot/content.expected + echo '>>>>>>>' >> $testroot/content.expected cat $testroot/wt/alpha > $testroot/content cmp -s $testroot/content.expected $testroot/content ret="$?" @@ -347,13 +350,15 @@ function test_rebase_abort { return 1 fi - echo "<<<<<<< commit $orig_commit1" > $testroot/content.expected + echo "<<<<<<< merged change: commit $orig_commit1" \ + > $testroot/content.expected echo "modified alpha on branch" >> $testroot/content.expected - echo "|||||||" >> $testroot/content.expected + echo "||||||| 3-way merge base: commit $init_commit" \ + >> $testroot/content.expected echo "alpha" >> $testroot/content.expected echo "=======" >> $testroot/content.expected echo "modified alpha on master" >> $testroot/content.expected - echo '>>>>>>> alpha' >> $testroot/content.expected + echo '>>>>>>>' >> $testroot/content.expected cat $testroot/wt/alpha > $testroot/content cmp -s $testroot/content.expected $testroot/content ret="$?" @@ -457,13 +462,15 @@ function test_rebase_no_op_change { return 1 fi - echo "<<<<<<< commit $orig_commit1" > $testroot/content.expected + echo "<<<<<<< merged change: commit $orig_commit1" \ + > $testroot/content.expected echo "modified alpha on branch" >> $testroot/content.expected - echo "|||||||" >> $testroot/content.expected + echo "||||||| 3-way merge base: commit $init_commit" \ + >> $testroot/content.expected echo "alpha" >> $testroot/content.expected echo "=======" >> $testroot/content.expected echo "modified alpha on master" >> $testroot/content.expected - echo '>>>>>>> alpha' >> $testroot/content.expected + echo '>>>>>>>' >> $testroot/content.expected cat $testroot/wt/alpha > $testroot/content cmp -s $testroot/content.expected $testroot/content ret="$?" @@ -564,13 +571,15 @@ function test_rebase_in_progress { return 1 fi - echo "<<<<<<< commit $orig_commit1" > $testroot/content.expected + echo "<<<<<<< merged change: commit $orig_commit1" \ + > $testroot/content.expected echo "modified alpha on branch" >> $testroot/content.expected - echo "|||||||" >> $testroot/content.expected + echo "||||||| 3-way merge base: commit $init_commit" \ + >> $testroot/content.expected echo "alpha" >> $testroot/content.expected echo "=======" >> $testroot/content.expected echo "modified alpha on master" >> $testroot/content.expected - echo '>>>>>>> alpha' >> $testroot/content.expected + echo '>>>>>>>' >> $testroot/content.expected cat $testroot/wt/alpha > $testroot/content cmp -s $testroot/content.expected $testroot/content ret="$?" blob - 4aa56793bd1b5831b7a8e3680b244722e712a0dd blob + 2225b5ae82ab9273a4760df29891aa221c5898b3 --- regress/cmdline/update.sh +++ regress/cmdline/update.sh @@ -642,6 +642,7 @@ function test_update_merges_file_edits { echo "8" >> $testroot/repo/numbers (cd $testroot/repo && git add numbers) git_commit $testroot/repo -m "added numbers file" + local base_commit=`git_show_head $testroot/repo` got checkout $testroot/repo $testroot/wt > /dev/null ret="$?" @@ -676,15 +677,16 @@ function test_update_merges_file_edits { return 1 fi - echo -n "<<<<<<< commit " > $testroot/content.expected + echo -n "<<<<<<< merged change: commit " > $testroot/content.expected git_show_head $testroot/repo >> $testroot/content.expected echo >> $testroot/content.expected echo "modified alpha" >> $testroot/content.expected - echo "|||||||" >> $testroot/content.expected + echo "||||||| 3-way merge base: commit $base_commit" \ + >> $testroot/content.expected echo "alpha" >> $testroot/content.expected echo "=======" >> $testroot/content.expected echo "modified alpha, too" >> $testroot/content.expected - echo '>>>>>>> alpha' >> $testroot/content.expected + echo '>>>>>>>' >> $testroot/content.expected echo "modified beta" >> $testroot/content.expected echo "1" >> $testroot/content.expected echo "22" >> $testroot/content.expected @@ -881,13 +883,13 @@ function test_update_conflict_wt_add_vs_repo_add { return 1 fi - echo -n "<<<<<<< commit " > $testroot/content.expected + echo -n "<<<<<<< merged change: commit " > $testroot/content.expected git_show_head $testroot/repo >> $testroot/content.expected echo >> $testroot/content.expected echo "new" >> $testroot/content.expected echo "=======" >> $testroot/content.expected echo "also new" >> $testroot/content.expected - echo '>>>>>>> gamma/new' >> $testroot/content.expected + echo '>>>>>>>' >> $testroot/content.expected cat $testroot/wt/gamma/new > $testroot/content @@ -1323,6 +1325,7 @@ function test_update_moved_branch_ref { function test_update_to_another_branch { local testroot=`test_init update_to_another_branch` + local base_commit=`git_show_head $testroot/repo` got checkout $testroot/repo $testroot/wt > /dev/null ret="$?" @@ -1362,15 +1365,16 @@ function test_update_to_another_branch { return 1 fi - echo -n "<<<<<<< commit " > $testroot/content.expected + echo -n "<<<<<<< merged change: commit " > $testroot/content.expected git_show_head $testroot/repo >> $testroot/content.expected echo >> $testroot/content.expected echo "modified alpha on new branch" >> $testroot/content.expected - echo "|||||||" >> $testroot/content.expected + echo "||||||| 3-way merge base: commit $base_commit" \ + >> $testroot/content.expected echo "alpha" >> $testroot/content.expected echo "=======" >> $testroot/content.expected echo "modified alpha in work tree" >> $testroot/content.expected - echo '>>>>>>> alpha' >> $testroot/content.expected + echo '>>>>>>>' >> $testroot/content.expected cat $testroot/wt/alpha > $testroot/content
show original content in merge conflicts