add 'got backout' command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
diff --git a/got/got.1 b/got/got.1
index c120894..ccaccdb 100644
--- a/got/got.1
+++ b/got/got.1
@@ -412,6 +412,38 @@ to a single base commit with
.Cm got update .
If the work tree already contains files with merge conflicts, these
conflicts must be resolved first.
+.It Cm backout Ar commit
+Reverse-merge changes from a single
+.Ar commit
+into the work tree.
+The specified
+.Ar commit
+must be on the same branch as the work tree's base commit.
+The expected argument is a reference or a SHA1 hash which corresponds to
+a commit object.
+.Pp
+Show the status of each affected file, using the following status codes:
+.Bl -column YXZ description
+.It G Ta file was merged
+.It C Ta file was merged and conflicts occurred during merge
+.It ! Ta changes destined for a missing file were not merged
+.It D Ta file was deleted
+.It d Ta file's deletion was obstructed by local modifications
+.It A Ta new file was added
+.It ~ Ta changes destined for a non-regular file were not merged
+.El
+.Pp
+The reverse-merged changes will appear as local changes in the work tree,
+which may be viewed with
+.Cm got diff ,
+amended manually or with further
+.Cm got cherrypick
+comands,
+committed with
+.Cm got commit ,
+or discarded again with
+.Cm got revert .
+.Pp
.El
.Sh ENVIRONMENT
.Bl -tag -width GOT_AUTHOR
diff --git a/got/got.c b/got/got.c
index 19de688..fd6712a 100644
--- a/got/got.c
+++ b/got/got.c
@@ -87,6 +87,7 @@ __dead static void usage_rm(void);
__dead static void usage_revert(void);
__dead static void usage_commit(void);
__dead static void usage_cherrypick(void);
+__dead static void usage_backout(void);
static const struct got_error* cmd_checkout(int, char *[]);
static const struct got_error* cmd_update(int, char *[]);
@@ -101,6 +102,7 @@ static const struct got_error* cmd_rm(int, char *[]);
static const struct got_error* cmd_revert(int, char *[]);
static const struct got_error* cmd_commit(int, char *[]);
static const struct got_error* cmd_cherrypick(int, char *[]);
+static const struct got_error* cmd_backout(int, char *[]);
static struct cmd got_commands[] = {
{ "checkout", cmd_checkout, usage_checkout,
@@ -129,6 +131,8 @@ static struct cmd got_commands[] = {
"write changes from work tree to repository" },
{ "cherrypick", cmd_cherrypick, usage_cherrypick,
"merge a single commit from another branch into a work tree" },
+ { "backout", cmd_backout, usage_backout,
+ "reverse-merge changes from a commit into a work tree" },
};
int
@@ -2719,3 +2723,110 @@ done:
got_repo_close(repo);
return error;
}
+
+__dead static void
+usage_backout(void)
+{
+ fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
+ exit(1);
+}
+
+static const struct got_error *
+cmd_backout(int argc, char *argv[])
+{
+ const struct got_error *error = NULL;
+ struct got_worktree *worktree = NULL;
+ struct got_repository *repo = NULL;
+ char *cwd = NULL, *commit_id_str = NULL;
+ struct got_object_id *commit_id = NULL;
+ struct got_commit_object *commit = NULL;
+ struct got_object_qid *pid;
+ struct got_reference *head_ref = NULL;
+ int ch, did_something = 0;
+
+ while ((ch = getopt(argc, argv, "")) != -1) {
+ switch (ch) {
+ default:
+ usage_backout();
+ /* NOTREACHED */
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (argc != 1)
+ usage_backout();
+
+ cwd = getcwd(NULL, 0);
+ if (cwd == NULL) {
+ error = got_error_from_errno("getcwd");
+ goto done;
+ }
+ error = got_worktree_open(&worktree, cwd);
+ if (error)
+ goto done;
+
+ error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
+ if (error != NULL)
+ goto done;
+
+ error = apply_unveil(got_repo_get_path(repo), 0,
+ got_worktree_get_root_path(worktree), 0);
+ if (error)
+ goto done;
+
+ error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
+ if (error != NULL) {
+ struct got_reference *ref;
+ if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
+ goto done;
+ error = got_ref_open(&ref, repo, argv[0], 0);
+ if (error != NULL)
+ goto done;
+ error = got_ref_resolve(&commit_id, repo, ref);
+ got_ref_close(ref);
+ if (error != NULL)
+ goto done;
+ }
+ error = got_object_id_str(&commit_id_str, commit_id);
+ if (error)
+ goto done;
+
+ error = got_ref_open(&head_ref, repo,
+ got_worktree_get_head_ref_name(worktree), 0);
+ if (error != NULL)
+ goto done;
+
+ error = check_same_branch(commit_id, head_ref, repo);
+ if (error)
+ goto done;
+
+ error = got_object_open_as_commit(&commit, repo, commit_id);
+ if (error)
+ goto done;
+ pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
+ if (pid == NULL) {
+ error = got_error(GOT_ERR_ROOT_COMMIT);
+ goto done;
+ }
+
+ error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
+ update_progress, &did_something, check_cancelled, NULL);
+ if (error != NULL)
+ goto done;
+
+ if (did_something)
+ printf("backed out commit %s\n", commit_id_str);
+done:
+ if (commit)
+ got_object_commit_close(commit);
+ free(commit_id_str);
+ if (head_ref)
+ got_ref_close(head_ref);
+ if (worktree)
+ got_worktree_close(worktree);
+ if (repo)
+ got_repo_close(repo);
+ return error;
+}
diff --git a/include/got_error.h b/include/got_error.h
index e19e395..0186e72 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -93,7 +93,7 @@
#define GOT_ERR_BRANCH_MOVED 77
#define GOT_ERR_OBJ_TOO_LARGE 78
#define GOT_ERR_SAME_BRANCH 79
-/* 80 is currently free for re-use */
+#define GOT_ERR_ROOT_COMMIT 80
#define GOT_ERR_MIXED_COMMITS 81
#define GOT_ERR_CONFLICTS 82
@@ -182,7 +182,7 @@ static const struct got_error {
"different branch; new head reference and/or update -b required" },
{ GOT_ERR_OBJ_TOO_LARGE, "object too large" },
{ GOT_ERR_SAME_BRANCH, "commit is already contained in this branch" },
- { 80, "unused error code" },
+ { GOT_ERR_ROOT_COMMIT, "specified commit has no parent commit" },
{ GOT_ERR_MIXED_COMMITS,"work tree contains files from multiple "
"base commits; the entire work tree must be updated first" },
{ GOT_ERR_CONFLICTS, "work tree contains conflicted files; these "
diff --git a/regress/cmdline/Makefile b/regress/cmdline/Makefile
index 5776af5..a834049 100644
--- a/regress/cmdline/Makefile
+++ b/regress/cmdline/Makefile
@@ -28,4 +28,7 @@ commit:
cherrypick:
./cherrypick.sh
+backout:
+ ./backout.sh
+
.include <bsd.regress.mk>
diff --git a/regress/cmdline/backout.sh b/regress/cmdline/backout.sh
new file mode 100755
index 0000000..d6140ca
--- /dev/null
+++ b/regress/cmdline/backout.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+#
+# Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+. ./common.sh
+
+function test_backout_basic {
+ local testroot=`test_init backout_basic`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "modified alpha" > $testroot/wt/alpha
+ (cd $testroot/wt && got commit -m "changing alpha" > /dev/null)
+
+ local bad_commit=`git_show_head $testroot/repo`
+
+
+ (cd $testroot/wt && got update > /dev/null)
+
+ echo "modified beta" > $testroot/wt/beta
+ (cd $testroot/wt && got commit -m "changing beta" > /dev/null)
+
+ (cd $testroot/wt && got update > /dev/null)
+
+ (cd $testroot/wt && got backout $bad_commit > $testroot/stdout)
+
+ echo "G alpha" > $testroot/stdout.expected
+ echo "backed out commit $bad_commit" >> $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "alpha" > $testroot/content.expected
+ cat $testroot/wt/alpha > $testroot/content
+ cmp -s $testroot/content.expected $testroot/content
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/content.expected $testroot/content
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo 'M alpha' > $testroot/stdout.expected
+ (cd $testroot/wt && got status > $testroot/stdout)
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
+run_test test_backout_basic