got patch: resolve paths from the current working directory this allow to apply patches from subdirectories of the work tree root. Prodded by naddy@, ok stsp@.
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
diff --git a/lib/patch.c b/lib/patch.c
index 9841808..adf9459 100644
--- a/lib/patch.c
+++ b/lib/patch.c
@@ -565,13 +565,14 @@ patch_add(void *arg, unsigned char status, const char *path)
static const struct got_error *
apply_patch(struct got_worktree *worktree, struct got_repository *repo,
- const char *oldpath, const char *newpath, struct got_patch *p,
- int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
+ const char *old, const char *new, struct got_patch *p, int nop,
+ struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
{
const struct got_error *err = NULL;
struct got_pathlist_head oldpaths, newpaths;
struct got_pathlist_entry *pe;
int file_renamed = 0;
+ char *oldpath = NULL, *newpath = NULL;
char *tmppath = NULL, *template = NULL, *parent = NULL;;
FILE *tmp = NULL;
mode_t mode = GOT_DEFAULT_FILE_MODE;
@@ -579,13 +580,25 @@ apply_patch(struct got_worktree *worktree, struct got_repository *repo,
TAILQ_INIT(&oldpaths);
TAILQ_INIT(&newpaths);
- err = got_pathlist_insert(&pe, &oldpaths, oldpath, NULL);
+ err = got_pathlist_insert(&pe, &oldpaths, old, NULL);
if (err)
goto done;
- err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
+ err = got_pathlist_insert(&pe, &newpaths, new, NULL);
if (err)
goto done;
+ if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
+ old) == -1) {
+ err = got_error_from_errno("asprintf");
+ goto done;
+ }
+
+ if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
+ new) == -1) {
+ err = got_error_from_errno("asprintf");
+ goto done;
+ }
+
file_renamed = strcmp(oldpath, newpath);
if (asprintf(&template, "%s/got-patch",
@@ -650,8 +663,7 @@ apply_patch(struct got_worktree *worktree, struct got_repository *repo,
if (err)
unlink(newpath);
} else
- err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
- NULL);
+ err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
done:
got_pathlist_free(&oldpaths);
@@ -661,6 +673,8 @@ done:
if (tmppath != NULL)
unlink(tmppath);
free(tmppath);
+ free(oldpath);
+ free(newpath);
return err;
}
diff --git a/regress/cmdline/patch.sh b/regress/cmdline/patch.sh
index b3f10d2..af1fd72 100755
--- a/regress/cmdline/patch.sh
+++ b/regress/cmdline/patch.sh
@@ -1273,6 +1273,46 @@ EOF
test_done $testroot 0
}
+test_patch_relative_paths() {
+ local testroot=`test_init patch_orig`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ cat <<EOF > $testroot/wt/gamma/patch
+--- delta
++++ delta
+@@ -1 +1 @@
+-delta
++DELTA
+--- /dev/null
++++ eta
+@@ -0,0 +1 @@
++eta
+EOF
+
+ (cd $testroot/wt/gamma && got patch patch) > $testroot/stdout
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ echo 'M gamma/delta' > $testroot/stdout.expected
+ echo 'A gamma/eta' >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done $testroot $ret
+}
+
test_parseargs "$@"
run_test test_patch_simple_add_file
run_test test_patch_simple_rm_file
@@ -1294,3 +1334,4 @@ run_test test_patch_with_offset
run_test test_patch_prefer_new_path
run_test test_patch_no_newline
run_test test_patch_strip
+run_test test_patch_relative_paths