# [PATCH] merge-file: correctly open files when in a subdir

2 messages from 2015-02-08 to 2015-02-09. Participants: Aleksander Boruch-Gruszecki, Duy Nguyen.
Thread: https://gitlist.dev/t/38529

## Aleksander Boruch-Gruszecki, 2015-02-08 16:53

Subject: [PATCH] merge-file: correctly open files when in a subdir
Message-ID: <CAPHKiG7yP=QZBVdDbyQp=uyMX0EsmUqgO-LO8pscn7hcbP0RCw@mail.gmail.com>
URL: https://gitlist.dev/e/CAPHKiG7yP%3DQZBVdDbyQp%3DuyMX0EsmUqgO-LO8pscn7hcbP0RCw%40mail.gmail.com

```
run_setup_gently() is called before merge-file. This may result in changing
current working directory, which wasn't taken into account when opening a file
for writing.

Fix by prepending the passed prefix. Previous var is left so that error
messages keep referring to the file from the user's working directory
perspective.

Signed-off-by: Aleksander Boruch-Gruszecki
    <aleksander.boruchgruszecki@gmail.com>
---
I'm sorry if I'm sending the copies to wrong people, but I had no idea
who to send them to since there were barely any changes to the file.
Since the fix is so trivial I didn't add any tests, but I can create
some if they're needed.

 builtin/merge-file.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/builtin/merge-file.c b/builtin/merge-file.c
index 844f84f..232b768 100644
--- a/builtin/merge-file.c
+++ b/builtin/merge-file.c
@@ -90,7 +90,8 @@ int cmd_merge_file(int argc, const char **argv,
const char *prefix)

     if (ret >= 0) {
         const char *filename = argv[0];
-        FILE *f = to_stdout ? stdout : fopen(filename, "wb");
+        const char *fpath = prefix_filename(prefix, prefixlen, argv[0]);
+        FILE *f = to_stdout ? stdout : fopen(fpath, "wb");

         if (!f)
             ret = error("Could not open %s for writing", filename);
-- 
1.9.1

```

## Duy Nguyen, 2015-02-09 10:06

Subject: Re: [PATCH] merge-file: correctly open files when in a subdir
Message-ID: <CACsJy8Abyi00UeDBWCoz6_+KE=k1-KkZCw8yww_g2pWbB+RYqg@mail.gmail.com>
URL: https://gitlist.dev/e/CACsJy8Abyi00UeDBWCoz6_%2BKE%3Dk1-KkZCw8yww_g2pWbB%2BRYqg%40mail.gmail.com
In-Reply-To: <CAPHKiG7yP=QZBVdDbyQp=uyMX0EsmUqgO-LO8pscn7hcbP0RCw@mail.gmail.com>

```
On Sun, Feb 8, 2015 at 11:53 PM, Aleksander Boruch-Gruszecki
<aleksander.boruchgruszecki@gmail.com> wrote:
> run_setup_gently() is called before merge-file. This may result in changing
> current working directory, which wasn't taken into account when opening a file
> for writing.
>
> Fix by prepending the passed prefix. Previous var is left so that error
> messages keep referring to the file from the user's working directory
> perspective.

Sounds about right. Thomas Rast fixed something similar in 55846b9
(merge-file: correctly find files when called in subdir - 2010-10-17)
but he missed this part :-D. Perhaps a test case in
t/t6023-merge-file.sh to prove this patch is working?
-- 
Duy

```
