initial 'got unstage' implementation
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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
diff --git a/got/got.c b/got/got.c
index 16ce520..18df91b 100644
--- a/got/got.c
+++ b/got/got.c
@@ -96,6 +96,7 @@ __dead static void usage_backout(void);
__dead static void usage_rebase(void);
__dead static void usage_histedit(void);
__dead static void usage_stage(void);
+__dead static void usage_unstage(void);
static const struct got_error* cmd_init(int, char *[]);
static const struct got_error* cmd_import(int, char *[]);
@@ -117,6 +118,7 @@ static const struct got_error* cmd_backout(int, char *[]);
static const struct got_error* cmd_rebase(int, char *[]);
static const struct got_error* cmd_histedit(int, char *[]);
static const struct got_error* cmd_stage(int, char *[]);
+static const struct got_error* cmd_unstage(int, char *[]);
static struct got_cmd got_commands[] = {
{ "init", cmd_init, usage_init, "" },
@@ -139,6 +141,7 @@ static struct got_cmd got_commands[] = {
{ "rebase", cmd_rebase, usage_rebase, "rb" },
{ "histedit", cmd_histedit, usage_histedit, "he" },
{ "stage", cmd_stage, usage_stage, "sg" },
+ { "unstage", cmd_unstage, usage_unstage, "ug" },
};
static void
@@ -5356,3 +5359,78 @@ done:
free(cwd);
return error;
}
+
+__dead static void
+usage_unstage(void)
+{
+ fprintf(stderr, "usage: %s unstage [file-path ...]\n",
+ getprogname());
+ exit(1);
+}
+
+
+static const struct got_error *
+cmd_unstage(int argc, char *argv[])
+{
+ const struct got_error *error = NULL;
+ struct got_repository *repo = NULL;
+ struct got_worktree *worktree = NULL;
+ char *cwd = NULL;
+ struct got_pathlist_head paths;
+ struct got_pathlist_entry *pe;
+ int ch, did_something = 0;
+
+ TAILQ_INIT(&paths);
+
+ while ((ch = getopt(argc, argv, "")) != -1) {
+ switch (ch) {
+ default:
+ usage_unstage();
+ /* NOTREACHED */
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+#ifndef PROFILE
+ if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
+ "unveil", NULL) == -1)
+ err(1, "pledge");
+#endif
+ 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), 1,
+ got_worktree_get_root_path(worktree));
+ if (error)
+ goto done;
+
+ error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
+ if (error)
+ goto done;
+
+ error = got_worktree_unstage(worktree, &paths, update_progress,
+ &did_something, repo);
+done:
+ if (repo)
+ got_repo_close(repo);
+ if (worktree)
+ got_worktree_close(worktree);
+ TAILQ_FOREACH(pe, &paths, entry)
+ free((char *)pe->path);
+ got_pathlist_free(&paths);
+ free(cwd);
+ return error;
+}
diff --git a/include/got_worktree.h b/include/got_worktree.h
index 371f24f..0b3c446 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -382,3 +382,11 @@ const struct got_error *got_worktree_get_histedit_script_path(char **,
const struct got_error *got_worktree_stage(struct got_worktree *,
struct got_pathlist_head *, got_worktree_status_cb, void *,
struct got_repository *);
+
+/*
+ * Merge staged changes for the specified paths back into the work tree
+ * and mark the paths as non-staged again.
+ */
+const struct got_error *got_worktree_unstage(struct got_worktree *,
+ struct got_pathlist_head *, got_worktree_checkout_cb, void *,
+ struct got_repository *);
diff --git a/lib/worktree.c b/lib/worktree.c
index 2e5c680..5c9c914 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -5229,3 +5229,116 @@ done:
err = unlockerr;
return err;
}
+
+struct unstage_path_arg {
+ struct got_worktree *worktree;
+ struct got_fileindex *fileindex;
+ struct got_repository *repo;
+ got_worktree_checkout_cb progress_cb;
+ void *progress_arg;
+};
+
+static const struct got_error *
+unstage_path(void *arg, unsigned char status,
+ unsigned char staged_status, const char *relpath,
+ struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
+ struct got_object_id *commit_id)
+{
+ const struct got_error *err = NULL;
+ struct unstage_path_arg *a = arg;
+ struct got_fileindex_entry *ie;
+ struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
+ char *ondisk_path = NULL;
+ int local_changes_subsumed;
+
+ ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
+ if (ie == NULL)
+ return got_error_path(relpath, GOT_ERR_BAD_PATH);
+
+ switch (staged_status) {
+ case GOT_STATUS_MODIFY:
+ err = got_object_open_as_blob(&blob_base, a->repo,
+ blob_id, 8192);
+ if (err)
+ break;
+ /* fall through */
+ case GOT_STATUS_ADD:
+ err = got_object_open_as_blob(&blob_staged, a->repo,
+ staged_blob_id, 8192);
+ if (err)
+ break;
+
+ if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
+ relpath) == -1) {
+ err= got_error_from_errno("asprintf");
+ break;
+ }
+
+ err = merge_blob(&local_changes_subsumed, a->worktree,
+ blob_base, ondisk_path, relpath,
+ got_fileindex_perms_to_st(ie), blob_staged,
+ commit_id ? commit_id : a->worktree->base_commit_id,
+ a->repo, a->progress_cb, a->progress_arg);
+ if (err == NULL)
+ got_fileindex_entry_stage_set(ie,
+ GOT_FILEIDX_STAGE_NONE);
+ break;
+ case GOT_STATUS_DELETE:
+ got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
+ err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
+ relpath);
+ break;
+ }
+
+ free(ondisk_path);
+ if (blob_base)
+ got_object_blob_close(blob_base);
+ if (blob_staged)
+ got_object_blob_close(blob_staged);
+ return err;
+}
+
+const struct got_error *
+got_worktree_unstage(struct got_worktree *worktree,
+ struct got_pathlist_head *paths,
+ got_worktree_checkout_cb progress_cb, void *progress_arg,
+ struct got_repository *repo)
+{
+ const struct got_error *err = NULL, *sync_err, *unlockerr;
+ struct got_pathlist_entry *pe;
+ struct got_fileindex *fileindex = NULL;
+ char *fileindex_path = NULL;
+ struct unstage_path_arg upa;
+
+ err = lock_worktree(worktree, LOCK_EX);
+ if (err)
+ return err;
+
+ err = open_fileindex(&fileindex, &fileindex_path, worktree);
+ if (err)
+ goto done;
+
+ upa.worktree = worktree;
+ upa.fileindex = fileindex;
+ upa.repo = repo;
+ upa.progress_cb = progress_cb;
+ upa.progress_arg = progress_arg;
+ TAILQ_FOREACH(pe, paths, entry) {
+ err = worktree_status(worktree, pe->path, fileindex, repo,
+ unstage_path, &upa, NULL, NULL);
+ if (err)
+ goto done;
+ }
+
+ sync_err = sync_fileindex(fileindex, fileindex_path);
+ if (sync_err && err == NULL)
+ err = sync_err;
+done:
+ free(fileindex_path);
+ if (fileindex)
+ got_fileindex_free(fileindex);
+ unlockerr = lock_worktree(worktree, LOCK_SH);
+ if (unlockerr && err == NULL)
+ err = unlockerr;
+ return err;
+}
diff --git a/regress/cmdline/Makefile b/regress/cmdline/Makefile
index a8d8fbf..e86ea7f 100644
--- a/regress/cmdline/Makefile
+++ b/regress/cmdline/Makefile
@@ -1,5 +1,5 @@
REGRESS_TARGETS=checkout update status log add rm diff blame branch commit \
- revert cherrypick backout rebase import histedit stage
+ revert cherrypick backout rebase import histedit stage unstage
NOOBJ=Yes
checkout:
@@ -53,4 +53,7 @@ histedit:
stage:
./stage.sh
+unstage:
+ ./unstage.sh
+
.include <bsd.regress.mk>
diff --git a/regress/cmdline/unstage.sh b/regress/cmdline/unstage.sh
new file mode 100755
index 0000000..1377492
--- /dev/null
+++ b/regress/cmdline/unstage.sh
@@ -0,0 +1,70 @@
+#!/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_unstage_basic {
+ local testroot=`test_init unstage_basic`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "modified file" > $testroot/wt/alpha
+ (cd $testroot/wt && got rm beta > /dev/null)
+ echo "new file" > $testroot/wt/foo
+ (cd $testroot/wt && got add foo > /dev/null)
+
+ echo ' M alpha' > $testroot/stdout.expected
+ echo ' D beta' >> $testroot/stdout.expected
+ echo ' A foo' >> $testroot/stdout.expected
+ (cd $testroot/wt && got stage alpha beta foo > /dev/null)
+
+ (cd $testroot/wt && got unstage > $testroot/stdout)
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got stage command succeeded unexpectedly" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ echo 'G alpha' > $testroot/stdout.expected
+ echo 'D beta' >> $testroot/stdout.expected
+ echo 'G foo' >> $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 'M alpha' > $testroot/stdout.expected
+ echo 'D beta' >> $testroot/stdout.expected
+ echo 'A foo' >> $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_unstage_basic