commit d9db2ff90a20aac31e00f040738fd49a56b38d6b from: Omar Polo via: Thomas Adam date: Sat Apr 16 08:06:59 2022 UTC got patch: allow to strip path components Move some bits from the libexec helper to the main process so we know if the patch was generated by git or not and finally document the automatic stripping of a/ and b/ prefixes added by git-diff(1). ok stsp@ commit - fc414659b454e49203f7ba02fb9866da42ffb4d0 commit + d9db2ff90a20aac31e00f040738fd49a56b38d6b blob - a9f60f53d707e188b849ee505d8cedffab3a89b9 blob + bd6e06c564ce9499b313b1d667097c58c2d16873 --- got/got.1 +++ got/got.1 @@ -1285,7 +1285,7 @@ option) .El .El .Tg pa -.It Cm patch Oo Fl n Oc Op Ar patchfile +.It Cm patch Oo Fl n Oc Oo Fl p Ar strip-count Oc Op Ar patchfile .Dl Pq alias: Cm pa Apply changes from .Ar patchfile @@ -1354,6 +1354,19 @@ If the .Ar patchfile contains diffs that affect the same file multiple times, the results displayed may be incorrect. +.It Fl p Ar strip-count +Specify the number of leading path components to strip from paths +parsed from +.Ar patchfile . +If the +.Fl p +option is not used, +.Sq a/ +and +.Sq b/ +path prefixes generated by +.Xr git-diff 1 +will be recognized and stripped automatically. .El .Tg rv .It Cm revert Oo Fl p Oc Oo Fl F Ar response-script Oc Oo Fl R Oc Ar path ... blob - 9ad83f6fda96bd86090467d9e6fae196fc2aacf4 blob + 277ccda9527fb4d2a732cdb084aa26a0db44e611 --- got/got.c +++ got/got.c @@ -7222,15 +7222,22 @@ cmd_patch(int argc, char *argv[]) const struct got_error *error = NULL, *close_error = NULL; struct got_worktree *worktree = NULL; struct got_repository *repo = NULL; + const char *errstr; char *cwd = NULL; - int ch, nop = 0; + int ch, nop = 0, strip = -1; int patchfd; - while ((ch = getopt(argc, argv, "n")) != -1) { + while ((ch = getopt(argc, argv, "np:")) != -1) { switch (ch) { case 'n': nop = 1; break; + case 'p': + strip = strtonum(optarg, 0, INT_MAX, &errstr); + if (errstr != NULL) + errx(1, "pathname strip count is %s: %s", + errstr, optarg); + break; default: usage_patch(); /* NOTREACHED */ @@ -7278,8 +7285,8 @@ cmd_patch(int argc, char *argv[]) err(1, "pledge"); #endif - error = got_patch(patchfd, worktree, repo, nop, &patch_progress, - NULL, check_cancelled, NULL); + error = got_patch(patchfd, worktree, repo, nop, strip, + &patch_progress, NULL, check_cancelled, NULL); done: if (repo) { blob - 959a2129f32e5f561fc9ccfdc186d10507d45e00 blob + 62c76b0eb2926a03364ef10e416183cdfa18f398 --- include/got_patch.h +++ include/got_patch.h @@ -31,5 +31,5 @@ typedef const struct got_error *(*got_patch_progress_c * The patch file descriptor *must* be seekable. */ const struct got_error * -got_patch(int, struct got_worktree *, struct got_repository *, int, +got_patch(int, struct got_worktree *, struct got_repository *, int, int, got_patch_progress_cb, void *, got_cancel_cb, void *); blob - f2d6ba2e472496846c8519d36002ed09491009ed blob + a223de14baf9eab1c747904cbc10cbaf30003d8c --- include/got_path.h +++ include/got_path.h @@ -41,6 +41,12 @@ const struct got_error *got_canonpath(const char *, ch const struct got_error *got_path_skip_common_ancestor(char **, const char *, const char *); +/* + * Remove leading components from path. It's an error to strip more + * component than present. The result is allocated dynamically. + */ +const struct got_error *got_path_strip(char **, const char *, int); + /* Determine whether a path points to the root directory "/" . */ int got_path_is_root_dir(const char *); blob - c371ffffbafaa4ddf41755886bc725bae926417e blob + 94dfb7f11c1e4edb26fbc644468d4a6943aeb167 --- lib/got_lib_privsep.h +++ lib/got_lib_privsep.h @@ -525,6 +525,7 @@ struct got_imsg_remotes { * Structure for GOT_IMSG_PATCH data. */ struct got_imsg_patch { + int git; char old[PATH_MAX]; char new[PATH_MAX]; }; blob - ca2bf2a6e7fadfa6cda8ee4b2b02d1c8ee736429 blob + 18dce1424914426c4a283effee2391af5dc10a53 --- lib/patch.c +++ lib/patch.c @@ -141,7 +141,7 @@ pushline(struct got_patch_hunk *h, const char *line) } static const struct got_error * -recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p) +recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip) { const struct got_error *err = NULL; struct imsg imsg; @@ -170,14 +170,30 @@ recv_patch(struct imsgbuf *ibuf, int *done, struct got goto done; } memcpy(&patch, imsg.data, sizeof(patch)); - if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) { - err = got_error_from_errno("strdup"); - goto done; - } - if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) { - err = got_error_from_errno("strdup"); - goto done; + + /* automatically set strip=1 for git-style diffs */ + if (strip == -1 && patch.git && + (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) && + (*patch.new == '\0' || !strncmp(patch.new, "b/", 2))) + strip = 1; + + /* prefer the new name if not /dev/null for not git-style diffs */ + if (!patch.git && *patch.new != '\0' && *patch.old != '\0') { + err = got_path_strip(&p->old, patch.new, strip); + if (err) + goto done; + } else if (*patch.old != '\0') { + err = got_path_strip(&p->old, patch.old, strip); + if (err) + goto done; } + + if (*patch.new != '\0') { + err = got_path_strip(&p->new, patch.new, strip); + if (err) + goto done; + } + if (p->old == NULL && p->new == NULL) { err = got_error(GOT_ERR_PATCH_MALFORMED); goto done; @@ -647,7 +663,7 @@ done: const struct got_error * got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo, - int nop, got_patch_progress_cb progress_cb, void *progress_arg, + int nop, int strip, got_patch_progress_cb progress_cb, void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg) { const struct got_error *err = NULL; @@ -703,7 +719,7 @@ got_patch(int fd, struct got_worktree *worktree, struc pa.progress_arg = progress_arg; pa.head = &p.head; - err = recv_patch(ibuf, &done, &p); + err = recv_patch(ibuf, &done, &p, strip); if (err || done) break; blob - b155ceebba7297ab8d3b872d569d7a5847b09303 blob + 0aaa95186aaed46ad2d54256cec06525008bd20f --- lib/path.c +++ lib/path.c @@ -114,7 +114,29 @@ got_path_skip_common_ancestor(char **child, const char free(*child); *child = NULL; return err; + } + return NULL; +} + +const struct got_error * +got_path_strip(char **out, const char *path, int n) +{ + const char *p, *c; + + p = path; + *out = NULL; + + while (n > 0 && (c = strchr(path, '/')) != NULL) { + path = c + 1; + n--; } + + if (n > 0) + return got_error_fmt(GOT_ERR_BAD_PATH, + "can't strip %d path-components from %s", n, p); + + if ((*out = strdup(path)) == NULL) + return got_error_from_errno("strdup"); return NULL; } blob - 12c9bd49a5357533fda3b3fe26668b7b656c4419 blob + a02d83bf9c79d0605368317e57b9019b5bf59ee6 --- libexec/got-read-patch/got-read-patch.c +++ libexec/got-read-patch/got-read-patch.c @@ -65,18 +65,13 @@ send_patch(const char *oldname, const char *newname, i memset(&p, 0, sizeof(p)); - /* - * Prefer the new name if it's not /dev/null and it's not - * a git-style diff. - */ - if (!git && newname != NULL && oldname != NULL) - strlcpy(p.old, newname, sizeof(p.old)); - else if (oldname != NULL) + if (oldname != NULL) strlcpy(p.old, oldname, sizeof(p.old)); if (newname != NULL) strlcpy(p.new, newname, sizeof(p.new)); + p.git = git; if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1, &p, sizeof(p)) == -1) return got_error_from_errno("imsg_compose GOT_IMSG_PATCH"); @@ -96,10 +91,9 @@ send_patch_done(void) /* based on fetchname from usr.bin/patch/util.c */ static const struct got_error * -filename(const char *at, char **name, int strip) +filename(const char *at, char **name) { - char *fullname, *t; - int l, tab; + char *tmp, *t; *name = NULL; if (*at == '\0') @@ -112,24 +106,16 @@ filename(const char *at, char **name, int strip) if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1)) return NULL; - t = strdup(at); - if (t == NULL) + tmp = strdup(at); + if (tmp == NULL) return got_error_from_errno("strdup"); - *name = fullname = t; - tab = strchr(t, '\t') != NULL; + if ((t = strchr(tmp, '\t')) != NULL) + *t = '\0'; + if ((t = strchr(tmp, '\n')) != NULL) + *t = '\0'; - /* strip off path components and NUL-terminate */ - for (l = strip; - *t != '\0' && ((tab && *t != '\t') || !isspace((unsigned char)*t)); - ++t) { - if (t[0] == '/' && t[1] != '/' && t[1] != '\0') - if (--l >= 0) - *name = t + 1; - } - *t = '\0'; - - *name = strdup(*name); - free(fullname); + *name = strdup(tmp); + free(tmp); if (*name == NULL) return got_error_from_errno("strdup"); return NULL; @@ -151,18 +137,12 @@ find_patch(FILE *fp) * we don't have to follow POSIX. */ - if (git && !strncmp(line, "--- a/", 6)) { + if (!strncmp(line, "--- ", 4)) { free(old); - err = filename(line+6, &old, 0); - } else if (!strncmp(line, "--- ", 4)) { - free(old); - err = filename(line+4, &old, 0); - } else if (git && !strncmp(line, "+++ b/", 6)) { - free(new); - err = filename(line+6, &new, 0); + err = filename(line+4, &old); } else if (!strncmp(line, "+++ ", 4)) { free(new); - err = filename(line+4, &new, 0); + err = filename(line+4, &new); } else if (!strncmp(line, "diff --git a/", 13)) git = 1; blob - 654d85e31ae466ed47907c34cc051be70a8351e4 blob + 24c65df8f37d93d010464929c0370750824f4534 --- regress/cmdline/patch.sh +++ regress/cmdline/patch.sh @@ -1211,6 +1211,68 @@ EOF test_done $testroot $ret } +test_patch_strip() { + local testroot=`test_init patch_strip` + + got checkout $testroot/repo $testroot/wt > /dev/null + ret=$? + if [ $ret -ne 0 ]; then + test_done $testroot $ret + return 1 + fi + + cat < $testroot/wt/patch +--- foo/bar/alpha.orig ++++ foo/bar/alpha +@@ -1 +1 @@ +-alpha ++ALPHA +EOF + + (cd $testroot/wt && got patch -p2 patch) > $testroot/stdout + ret=$? + if [ $ret -ne 0 ]; then + test_done $testroot $ret + return 1 + fi + + echo "M alpha" >> $testroot/stdout.expected + cmp -s $testroot/stdout.expected $testroot/stdout + ret=$? + if [ $ret -ne 0 ]; then + diff -u $testroot/stdout.expected $testroot/stdout + test_done $testroot $ret + return 1 + fi + + (cd $testroot/wt && got revert alpha) > /dev/null 2>&1 + ret=$? + if [ $ret -ne 0 ]; then + test_done $testroot $ret + return 1 + fi + + (cd $testroot/wt && got patch -p3 patch) \ + 2> $testroot/stderr + ret=$? + if [ $ret -eq 0 ]; then + echo "stripped more components than available!" + test_done $testroot 1 + return 1 + fi + + cat < $testroot/stderr.expected +got: can't strip 1 path-components from foo/bar/alpha: bad path +EOF + + cmp -s $testroot/stderr.expected $testroot/stderr + ret=$? + if [ $ret -ne 0 ]; then + diff -u $testroot/stderr.expected $testroot/stderr + fi + test_done $testroot 0 +} + test_parseargs "$@" run_test test_patch_simple_add_file run_test test_patch_simple_rm_file @@ -1231,3 +1293,4 @@ run_test test_patch_create_dirs run_test test_patch_with_offset run_test test_patch_prefer_new_path run_test test_patch_no_newline +run_test test_patch_strip