fix 'got revert' with new realpath(3) ENOENT behaviour
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
diff --git a/got/got.c b/got/got.c
index f0d4319..e090e01 100644
--- a/got/got.c
+++ b/got/got.c
@@ -3078,6 +3078,7 @@ cmd_revert(int argc, char *argv[])
char *cwd = NULL, *path = NULL;
struct got_pathlist_head paths;
struct got_pathlist_entry *pe;
+ const char *worktree_path;
int ch, i;
TAILQ_INIT(&paths);
@@ -3101,21 +3102,6 @@ cmd_revert(int argc, char *argv[])
if (argc < 1)
usage_revert();
- for (i = 0; i < argc; i++) {
- char *path = realpath(argv[i], NULL);
- if (path == NULL) {
- error = got_error_from_errno2("realpath", argv[i]);
- goto done;
- }
-
- got_path_strip_trailing_slashes(path);
- error = got_pathlist_insert(&pe, &paths, path, NULL);
- if (error) {
- free(path);
- goto done;
- }
- }
-
cwd = getcwd(NULL, 0);
if (cwd == NULL) {
error = got_error_from_errno("getcwd");
@@ -3134,6 +3120,39 @@ cmd_revert(int argc, char *argv[])
if (error)
goto done;
+ worktree_path = got_worktree_get_root_path(worktree);
+ for (i = 0; i < argc; i++) {
+ char *path = realpath(argv[i], NULL);
+ if (path == NULL) {
+ if (errno != ENOENT) {
+ error = got_error_from_errno2("realpath",
+ argv[i]);
+ goto done;
+ }
+ if (got_path_is_child(argv[i], worktree_path,
+ strlen(worktree_path))) {
+ path = strdup(argv[i]);
+ if (path == NULL) {
+ error = got_error_from_errno("strdup");
+ goto done;
+ }
+
+ } else if (asprintf(&path, "%s/%s",
+ got_worktree_get_root_path(worktree),
+ argv[i]) == -1) {
+ error = got_error_from_errno("asprintf");
+ goto done;
+ }
+ }
+
+ got_path_strip_trailing_slashes(path);
+ error = got_pathlist_insert(&pe, &paths, path, NULL);
+ if (error) {
+ free(path);
+ goto done;
+ }
+ }
+
error = got_worktree_revert(worktree, &paths,
revert_progress, NULL, repo);
if (error)