Repository: goreleaser/goreleaser
Pull request #2620 (2021-11-01 09:02:02)

feat(source): add prefix_template option
If applied, this change will add a `prefix_template` option to the source archive pipe. This allows the generated tarball to use a subdirectory for all files in the archive, as described in [`git-archive`](https://git-scm.com/docs/git-archive).

Currently, archives generated by the source archive step do not use a prefix, whereas traditionally the prefix of `{{ .ProjectName }}-{{ .Version }}/` is used. See https://github.com/twpayne/chezmoi/issues/1576.

The default prefix is none, to maintain compatibility for existing users.


codecov[bot]: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2620?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) Report
> Merging [#2620](https://codecov.io/gh/goreleaser/goreleaser/pull/2620?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (6bbe917) into [master](https://codecov.io/gh/goreleaser/goreleaser/commit/5dc82f78da3df5a86f2317f716f59b79c648a0ce?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (5dc82f7) will **decrease** coverage by `0.01%`.
> The diff coverage is `75.00%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/2620/graphs/tree.svg?width=650&height=150&src=pr&token=3bJvv6b7hN&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)](https://codecov.io/gh/goreleaser/goreleaser/pull/2620?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)

```diff
@@            Coverage Diff             @@
##           master    #2620      +/-   ##
==========================================
- Coverage   84.65%   84.63%   -0.02%     
==========================================
  Files         100      100              
  Lines        7325     7336      +11     
==========================================
+ Hits         6201     6209       +8     
- Misses        929      931       +2     
- Partials      195      196       +1     
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/2620?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) | Coverage Δ | |
|---|---|---|
| [pkg/config/config.go](https://codecov.io/gh/goreleaser/goreleaser/pull/2620/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-cGtnL2NvbmZpZy9jb25maWcuZ28=) | `87.00% <ø> (ø)` | |
| [internal/pipe/sourcearchive/source.go](https://codecov.io/gh/goreleaser/goreleaser/pull/2620/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-aW50ZXJuYWwvcGlwZS9zb3VyY2VhcmNoaXZlL3NvdXJjZS5nbw==) | `93.02% <75.00%> (-6.98%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2620?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2620?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Last update [5dc82f7...6bbe917](https://codecov.io/gh/goreleaser/goreleaser/pull/2620?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).


Patch:
diff --git a/USERS.md b/USERS.md
index edef32eb..e33aa757 100644
--- a/USERS.md
+++ b/USERS.md
@@ -7,6 +7,7 @@ Currently, the following organizations are using GoReleaser:
 
 1. [Charm](https://charm.sh)
 1. [Hugo](https://gohugo.io/)
+1. [Ministry of Justice (UK)](https://mojdigital.blog.gov.uk/)
 1. [Numary](https://numary.com)
 1. [Schwarz IT](https://jobs.schwarz/)
 1. [TOTVS Labs](https://totvslabs.com)
diff --git a/internal/pipe/sourcearchive/source.go b/internal/pipe/sourcearchive/source.go
index c8459738..a88f1866 100644
--- a/internal/pipe/sourcearchive/source.go
+++ b/internal/pipe/sourcearchive/source.go
@@ -31,7 +31,19 @@ func (Pipe) Run(ctx *context.Context) (err error) {
 	filename := name + "." + ctx.Config.Source.Format
 	path := filepath.Join(ctx.Config.Dist, filename)
 	log.WithField("file", filename).Info("creating source archive")
-	out, err := git.Clean(git.Run("archive", "-o", path, ctx.Git.FullCommit))
+	args := []string{
+		"archive",
+		"-o", path,
+	}
+	if ctx.Config.Source.PrefixTemplate != "" {
+		prefix, err := tmpl.New(ctx).Apply(ctx.Config.Source.PrefixTemplate)
+		if err != nil {
+			return err
+		}
+		args = append(args, "--prefix", prefix)
+	}
+	args = append(args, ctx.Git.FullCommit)
+	out, err := git.Clean(git.Run(args...))
 	log.Debug(out)
 	ctx.Artifacts.Add(&artifact.Artifact{
 		Type: artifact.UploadableSourceArchive,
diff --git a/pkg/config/config.go b/pkg/config/config.go
index febe5b71..f6143af7 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -756,9 +756,10 @@ type Publisher struct {
 
 // Source configuration.
 type Source struct {
-	NameTemplate string `yaml:"name_template,omitempty"`
-	Format       string `yaml:"format,omitempty"`
-	Enabled      bool   `yaml:"enabled,omitempty"`
+	NameTemplate   string `yaml:"name_template,omitempty"`
+	Format         string `yaml:"format,omitempty"`
+	Enabled        bool   `yaml:"enabled,omitempty"`
+	PrefixTemplate string `yaml:"prefix_template,omitempty"`
 }
 
 // Project includes all project configuration.
diff --git a/www/docs/customization/source.md b/www/docs/customization/source.md
index 24f539e8..c4e2670a 100644
--- a/www/docs/customization/source.md
+++ b/www/docs/customization/source.md
@@ -18,6 +18,11 @@ source:
   # Any format git-archive supports, this supports too.
   # Defaults to `tar.gz`
   format: 'tar'
+
+  # Prefix template.
+  # String to prepend to each filename in the archive.
+  # Defaults to empty
+  prefix_template: '{{ .ProjectName }}-{{ .Version }}/'
 ```
 
 !!! tip


Test patch:
diff --git a/internal/pipe/sourcearchive/source_test.go b/internal/pipe/sourcearchive/source_test.go
index e8bd0a7e..33b7d621 100644
--- a/internal/pipe/sourcearchive/source_test.go
+++ b/internal/pipe/sourcearchive/source_test.go
@@ -28,8 +28,9 @@ func TestArchive(t *testing.T) {
 				ProjectName: "foo",
 				Dist:        "dist",
 				Source: config.Source{
-					Format:  format,
-					Enabled: true,
+					Format:         format,
+					Enabled:        true,
+					PrefixTemplate: "{{ .ProjectName }}-{{ .Version }}/",
 				},
 			})
 			ctx.Git.FullCommit = "HEAD"