# Bug: git-archive: --add-virtual-file doesn't seem to respect --prefix

2 messages from 2024-02-15 to 2024-02-15. Participants: Ron Yorston, stefan.naewe@atlas-elektronik.com.
Thread: https://gitlist.dev/t/60922

## Ron Yorston, 2024-02-15 06:07

Subject: Bug: git-archive: --add-virtual-file doesn't seem to respect --prefix
Message-ID: <65cdaa23.MHIIVADqwryAD0ON%rmy@frippery.org>
URL: https://gitlist.dev/e/65cdaa23.MHIIVADqwryAD0ON%25rmy%40frippery.org

```
The man page for git-archive has similar language regarding the
--add-file and --add-virtual-file options:

   The path of the file in the archive is built by concatenating the
   value of the last --prefix option (if any) before...

However this doesn't seem to be true for --add-virtual-file.  In any
git repository:

   $ touch real_added_file
   $ git archive --format=tar --prefix=prefix/ --add-file=real_added_file \
      --add-virtual-file=virtual_added_file: HEAD | \
      tar tvf - | grep added_file
   -rw-rw-r-- root/root         0 2017-02-22 17:18 prefix/real_added_file
   -rw-rw-r-- root/root         0 2017-02-22 17:18 virtual_added_file

I expected to see 'prefix/virtual_added_file'.

Ron

```

## stefan.naewe@atlas-elektronik.com, 2024-02-15 11:48

Subject: AW: git-archive: --add-virtual-file doesn't seem to respect --prefix
Message-ID: <a143e25a70b44b82b4ee6fa3bb2bcda4@atlas-elektronik.com>
URL: https://gitlist.dev/e/a143e25a70b44b82b4ee6fa3bb2bcda4%40atlas-elektronik.com
In-Reply-To: <65cdaa23.MHIIVADqwryAD0ON%rmy@frippery.org>

```
Classification - ISMS: Offen | VS: OFFEN

> -----Ursprüngliche Nachricht-----
> Von: Ron Yorston <rmy@frippery.org>
> Gesendet: Donnerstag, 15. Februar 2024 07:08
> An: git@vger.kernel.org
> Betreff: Bug: git-archive: --add-virtual-file doesn't seem to respect --prefix
> 
> The man page for git-archive has similar language regarding the --add-file and --
> add-virtual-file options:
> 
>    The path of the file in the archive is built by concatenating the
>    value of the last --prefix option (if any) before...
> 
> However this doesn't seem to be true for --add-virtual-file.  In any git repository:
> 
>    $ touch real_added_file
>    $ git archive --format=tar --prefix=prefix/ --add-file=real_added_file \
>       --add-virtual-file=virtual_added_file: HEAD | \
>       tar tvf - | grep added_file
>    -rw-rw-r-- root/root         0 2017-02-22 17:18 prefix/real_added_file
>    -rw-rw-r-- root/root         0 2017-02-22 17:18 virtual_added_file
> 
> I expected to see 'prefix/virtual_added_file'.
> 
> Ron

Maybe something like this does the job:


diff --git a/archive.c b/archive.c
index 941495f5d7..b6b885a632 100644
--- a/archive.c
+++ b/archive.c
@@ -331,11 +331,11 @@ int write_archive_entries(struct archiver_args *args,

                put_be64(fake_oid.hash, i + 1);

+               strbuf_reset(&path_in_archive);
+               if (info->base)
+                       strbuf_addstr(&path_in_archive, info->base);
+               strbuf_addstr(&path_in_archive, basename(path));
                if (!info->content) {
-                       strbuf_reset(&path_in_archive);
-                       if (info->base)
-                               strbuf_addstr(&path_in_archive, info->base);
-                       strbuf_addstr(&path_in_archive, basename(path));

                        strbuf_reset(&content);
                        if (strbuf_read_file(&content, path, info->stat.st_size) < 0)
@@ -347,7 +347,7 @@ int write_archive_entries(struct archiver_args *args,
                                                  content.buf, content.len);
                } else {
                        err = write_entry(args, &fake_oid,
-                                         path, strlen(path),
+                                         path_in_archive.buf, path_in_archive.len,
                                          canon_mode(info->stat.st_mode),
                                          info->content, info->stat.st_size);
                }


Stefan

```
