Blob Blame History Raw
diff -up ./builtin/apply.c.original ./builtin/apply.c
--- ./builtin/apply.c.original	2023-05-18 14:44:01.287997256 +0900
+++ ./builtin/apply.c	2023-05-18 14:44:45.513756974 +0900
@@ -4151,7 +4151,7 @@ static int write_out_one_reject(struct p
 	FILE *rej;
 	char namebuf[PATH_MAX];
 	struct fragment *frag;
-	int cnt = 0;
+	int fd, cnt = 0;
 	struct strbuf sb = STRBUF_INIT;
 
 	for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) {
@@ -4190,7 +4190,17 @@ static int write_out_one_reject(struct p
 	memcpy(namebuf, patch->new_name, cnt);
 	memcpy(namebuf + cnt, ".rej", 5);
 
-	rej = fopen(namebuf, "w");
+	fd = open(namebuf, O_CREAT | O_EXCL | O_WRONLY, 0666);
+	if (fd < 0) {
+		if (errno != EEXIST)
+			die_errno(_("cannot open %s"), namebuf);
+		if (unlink(namebuf))
+			die_errno(_("cannot unlink '%s'"), namebuf);
+		fd = open(namebuf, O_CREAT | O_EXCL | O_WRONLY, 0666);
+		if (fd < 0)
+			die_errno(_("cannot open %s"), namebuf);
+	}
+	rej = fdopen(fd, "w");
 	if (!rej)
 		return error(_("cannot open %s: %s"), namebuf, strerror(errno));
 
diff -up ./t/t4115-apply-symlink.sh.original ./t/t4115-apply-symlink.sh
--- ./t/t4115-apply-symlink.sh.original	2023-05-18 14:45:50.675876356 +0900
+++ ./t/t4115-apply-symlink.sh	2023-05-18 14:46:42.075759315 +0900
@@ -46,4 +46,19 @@ test_expect_success SYMLINKS 'apply --in
 
 '
 
+test_expect_success SYMLINKS '--reject removes .rej symlink if it exists' '
+	test_when_finished "git reset --hard && git clean -dfx" &&
+
+	test_commit file &&
+	echo modified >file.t &&
+	git diff -- file.t >patch &&
+	echo modified-again >file.t &&
+
+	ln -s foo file.t.rej &&
+	test_must_fail git apply patch --reject 2>err &&
+	test_i18ngrep "Rejected hunk" err &&
+	test_path_is_missing foo &&
+	test_path_is_file file.t.rej
+'
+
 test_done