Backtestshugorun-20261007-020926#14937
Add build.cleanDestinationDir config option
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches let users keep extra files such as `.git`, `.domains`, `_redirects` and nested paths when `--cleanDestinationDir` runs. B is preferred because it solves the issue additively with less risk. Patch B adds `build.cleanDestinationDir.keep`, a list of globs matched against the path relative to the publishDir. It leaves the existing behavior intact: the root `cleanDestinationDir` bool and flag still work, and `.gitignore`, `.gitattributes` and dot-directories are still always preserved. The change to the `keep` callback is minimal: it passes the destination-relative path in addition to the FileInfo. A matched directory is kept whole, which covers the issue's file-or-directory examples. B includes: - a unit test of compiling and matching the globs, including leading-slash input, nested paths, the `**` behavior, invalid-glob errors and cloning; - a new testscript built around the issue's exact scenario (a worktree `.git` file, `.domains`, `_redirects`, `foo/bar/.mysecretfile`), run with and without a static dir; - documentation, including the multihost caveat. Patch A is a broader redesign. It moves the setting to `build.cleanDestinationDir.enable`, deprecates and removes `RootConfig.CleanDestinationDir`, remaps the CLI flag to a nested key, and splits patterns into `keepFiles` and `keepDirs` with negation support. The hard-coded defaults become overridable default patterns. This carries more risk: - Removing the root field could break any other consumers. - The flag-to-nested-key mapping and the legacy normalization are new code paths. - Setting `keepFiles` or `keepDirs` replaces the defaults entirely. A's own scenario 4 shows `.gitignore` and `.hidden/` being deleted once a user adds `.domains`. A user following the issue would therefore silently lose protections they had before, unless they re-list them. A's default protection of `.git` files is a nice touch, and its testscript coverage is decent. However, the extra surface area and the regression-prone override semantics outweigh those benefits. B fixes the reported need cleanly, stays backward compatible, and has more targeted tests.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
39m 6s
Wall-clock time in the container
Full test suite
No new failures
3,779 passed · 101 skipped · 3,882 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
207vs 194 merged
Changed lines, ours vs merged
The issue
View on GitHub<!-- Describe this new feature. Think about if it really belongs in the Hugo core module; you may want to discuss it on https://discourse.gohugo.io/ first. -->
Usecase
I'm using Codeberg Pages to host my website, you can find the repostitory at https://codeberg.org/ilovelinux/pages
I have two branches:
main: hugo projectpages: hugo build (output ofpublic/)
Since I'm using Codeberg Pages with a custom domain, I also need a .domains file[^codebergDomains] to be pushed in the pages branch.
When I
- Have files in the
static/directory. - Switch to
mainbranch. - Create the
public/folder using a git worktree of thepagesbranch. - Run
hugo build --cleanDestinationDir.
I need
- To remove outdated content
- To retain:
.gitfile created bygit worktree[^gitWorktreeFile]..domainsfile used by Codeberg Pages in order to allow custom domains[^codebergDomains]._redirectsfile for redirects (however I'm not using it yet)[^codebergRedirects].
Instead
- The files I need to be retained are deleted.
Proposal
Add a configuration field to list relative file paths to public/ that should be retained when using --cleanDestinationDir.
Example
filesToRetain:
- .git
- .domains
- _redirects
- foo/bar/.mysecretfile
Files retained:
public/.gitpublic/.domainspublic/_redirectspublic/foo/bar/.mysecretfile
[!NOTE]
I'm not suggesting to usefilesToRetainas configuration field, but I couldn't think of a better name.
Notes
[!IMPORTANT]
--cleanDestinationDirdoes nothing when when thestatic/directory doesn't exist since its value is used inside thecopyStaticTofunction.
https://github.com/gohugoio/hugo/blob/c4bbc2805c7092deafabae01e71ff2dda37bd877/commands/hugobuilder.go#L481
Workaround
[!WARNING]
This approach is error-prone.
I implemented a workaround in my Makefile that:
- Repairs the worktree folder, as suggested in https://github.com/gohugoio/hugo/pull/6261#issuecomment-1692321365.
- Restores the
.domainsfile.
Related issues & PR
- https://github.com/gohugoio/hugo/issues/3202 was about
.gitfolder being deleted. Fixed thanks to: - https://github.com/spf13/fsync/pull/10
- https://github.com/gohugoio/hugo/pull/3210.
- https://github.com/gohugoio/hugo/issues/4546 was about
.gitfile of a submodule[^gitSubmoduleFile] being deleted. Closed because it was considered a discussion. - https://github.com/gohugoio/hugo/pull/6261 was introducing a fix for the
.gitfile in order to work correctly with git worktrees and submodules. Closed because of the Stale bot but it looks like it was still kept into consideration since @bep kept moving it among milestones after that. However this wouldn't solve my issue since.domainsand_redirectsfiles would still be deleted.
[^gitWorktreeFile]: https://git-scm.com/docs/git-worktree#_details
[^gitSubmoduleFile]: https://git-scm.com/docs/git-submodule#Documentation/git-submodule.txt-absorbgitdirs
[^codebergDomains]: https://docs.codeberg.org/codeberg-pages/using-custom-domain/
[^codebergRedirects]: https://docs.codeberg.org/codeberg-pages/redirects/
The two patches
commands/hugobuilder.goindex a9cacaada..3d87f2abf 100644@@ -467,7 +467,10 @@ func (c *hugoBuilder) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint fs := &countingStatFs{Fs: sourceFs.Fs} syncer := fsync.NewSyncer()- var clean bool+ var (+ clean bool+ cleanConf config.CleanDestinationDir+ ) c.withConf(func(conf *commonConfig) { syncer.NoTimes = conf.configs.Base.NoTimes syncer.NoChmod = conf.configs.Base.NoChmod@@ -475,6 +478,7 @@ func (c *hugoBuilder) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint syncer.DestFs = conf.fs.PublishDirStatic clean = conf.configs.Base.CleanDestinationDir+ cleanConf = conf.configs.Base.Build.CleanDestinationDir }) syncer.SrcFs = fs@@ -494,7 +498,12 @@ func (c *hugoBuilder) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint if clean { infol.Logf("removing stale files from destination") - keep := func(f fsync.FileInfo) bool {+ keep := func(path string, f fsync.FileInfo) bool {+ // Keep files and directories matching build.cleanDestinationDir.keep.+ if cleanConf.ShouldKeep(path) {+ return true+ }+ name := f.Name() // Keep git metadata files anywhere.@@ -522,7 +531,8 @@ func (c *hugoBuilder) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint // removeStale removes entries in dstDir on dst that keep reports as false and // that don't exist in srcDir on src. Unlike fsync's delete pass, it applies // keep at every level, so kept files survive inside stale directories.-func removeStale(dst, src afero.Fs, dstDir, srcDir string, keep func(fsync.FileInfo) bool) error {+// The path passed to keep is the entry's path relative to the root of dst.+func removeStale(dst, src afero.Fs, dstDir, srcDir string, keep func(path string, f fsync.FileInfo) bool) error { entries, err := afero.ReadDir(dst, dstDir) if err != nil { if herrors.IsNotExist(err) {@@ -531,10 +541,10 @@ func removeStale(dst, src afero.Fs, dstDir, srcDir string, keep func(fsync.FileI return err } for _, entry := range entries {- if keep(entry) {+ dstPath := filepath.Join(dstDir, entry.Name())+ if keep(dstPath, entry) { continue }- dstPath := filepath.Join(dstDir, entry.Name()) srcPath := filepath.Join(srcDir, entry.Name()) if _, err := src.Stat(srcPath); err == nil { if entry.IsDir() {config/commonConfig.goindex dae0ed905..0b6c2eb6b 100644@@ -16,6 +16,7 @@ package config import ( "fmt" "net/http"+ "path/filepath" "regexp" "slices" "sort"@@ -26,6 +27,7 @@ import ( "github.com/gohugoio/hugo/common/hstrings" "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/types"+ "github.com/gohugoio/hugo/hugofs/hglob" "github.com/gohugoio/hugo/common/herrors" "github.com/mitchellh/mapstructure"@@ -112,6 +114,45 @@ type BuildConfig struct { // Can used to control how the resource cache gets evicted on rebuilds. CacheBusters []CacheBuster++ // Configures the removal of stale files from the publishDir when+ // cleanDestinationDir is enabled.+ CleanDestinationDir CleanDestinationDir+}++// CleanDestinationDir configures the removal of stale files from the publishDir+// when cleanDestinationDir is enabled.+type CleanDestinationDir struct {+ // Glob patterns matching files and directories, relative to the publishDir,+ // that should never be removed, e.g. ".domains", "_redirects" or "**/.git" (the latter does not match a .git at the root).+ // A matching directory is kept with all of its content.+ Keep []string++ compiledKeep []hstrings.Matcher+}++func (c *CleanDestinationDir) compileConfig() error {+ c.compiledKeep = nil+ for _, pattern := range c.Keep {+ g, err := hglob.GetGlob(strings.TrimPrefix(filepath.ToSlash(pattern), "/"))+ if err != nil {+ return fmt.Errorf("failed to compile build.cleanDestinationDir.keep glob %q: %w", pattern, err)+ }+ c.compiledKeep = append(c.compiledKeep, g)+ }+ return nil+}++// ShouldKeep reports whether the file or directory at p, relative to the publishDir,+// matches one of the Keep patterns.+func (c CleanDestinationDir) ShouldKeep(p string) bool {+ p = strings.TrimPrefix(filepath.ToSlash(p), "/")+ for _, g := range c.compiledKeep {+ if g.Match(p) {+ return true+ }+ }+ return false } // BuildStats configures if and what to write to the hugo_stats.json file.@@ -131,6 +172,8 @@ func (w BuildStats) Enabled() bool { func (b BuildConfig) clone() BuildConfig { b.CacheBusters = slices.Clone(b.CacheBusters)+ b.CleanDestinationDir.Keep = slices.Clone(b.CleanDestinationDir.Keep)+ b.CleanDestinationDir.compiledKeep = slices.Clone(b.CleanDestinationDir.compiledKeep) return b } @@ -174,7 +217,7 @@ func (b *BuildConfig) CompileConfig(logger loggers.Logger) error { } b.CacheBusters[i] = cb }- return nil+ return b.CleanDestinationDir.compileConfig() } func DecodeBuildConfig(cfg Provider) BuildConfig {config/commonConfig_test.goindex 05ba185e3..a5ed237f8 100644@@ -195,3 +195,45 @@ func TestBuildConfigCacheBusterstTailwindSetup(t *testing.T) { c.Assert(err, qt.IsNil) c.Assert(m("css"), qt.IsTrue) }++func TestBuildConfigCleanDestinationDir(t *testing.T) {+ c := qt.New(t)+ cfg := New()+ cfg.Set("build", map[string]any{+ "cleanDestinationDir": map[string]any{+ "keep": []any{".git", "/.domains", "_redirects", "foo/bar/.mysecretfile", "**/*.keep"},+ },+ })++ conf := DecodeBuildConfig(cfg)+ c.Assert(conf.CompileConfig(loggers.NewDefault()), qt.IsNil)++ cd := conf.CleanDestinationDir+ c.Assert(cd.ShouldKeep(".git"), qt.IsTrue)+ c.Assert(cd.ShouldKeep("/.domains"), qt.IsTrue)+ c.Assert(cd.ShouldKeep("_redirects"), qt.IsTrue)+ c.Assert(cd.ShouldKeep("foo/bar/.mysecretfile"), qt.IsTrue)+ c.Assert(cd.ShouldKeep("a/b/c.keep"), qt.IsTrue)+ c.Assert(cd.ShouldKeep("sub/.git"), qt.IsFalse)+ c.Assert(cd.ShouldKeep("foo/bar"), qt.IsFalse)+ c.Assert(cd.ShouldKeep("foo.txt"), qt.IsFalse)++ // Clones must not share the compiled state.+ cloned := conf.clone()+ c.Assert(cloned.CleanDestinationDir.ShouldKeep(".git"), qt.IsTrue)++ // No configuration keeps nothing.+ conf = DecodeBuildConfig(New())+ c.Assert(conf.CompileConfig(loggers.NewDefault()), qt.IsNil)+ c.Assert(conf.CleanDestinationDir.ShouldKeep(".git"), qt.IsFalse)++ // Invalid globs fail compilation.+ cfg = New()+ cfg.Set("build", map[string]any{+ "cleanDestinationDir": map[string]any{+ "keep": []any{"[a-"},+ },+ })+ conf = DecodeBuildConfig(cfg)+ c.Assert(conf.CompileConfig(loggers.NewDefault()), qt.ErrorMatches, `.*build.cleanDestinationDir.keep.*`)+}docs/content/en/configuration/all.mdindex 842465254..bd47d6823 100644@@ -46,7 +46,7 @@ aliases: [/getting-started/configuration/] : See [configure cascade][]. `cleanDestinationDir`-: (`bool`) Whether to remove files from the [`publishDir`](#publishdir) that do not exist in the [`staticDir`](#staticdir). Hugo performs this cleanup before rendering, so the current build's rendered output is unaffected. If the `staticDir` does not exist, the cleanup does not run. Note that `.gitignore` and `.gitattributes` files are always preserved, as are directories whose names begin with a dot, provided their parent directory also survives. Default is `false`.+: (`bool`) Whether to remove files from the [`publishDir`](#publishdir) that do not exist in the [`staticDir`](#staticdir). Hugo performs this cleanup before rendering, so the current build's rendered output is unaffected. If the `staticDir` does not exist, the cleanup does not run. Note that `.gitignore` and `.gitattributes` files are always preserved, as are directories whose names begin with a dot, provided their parent directory also survives. Use [`build.cleanDestinationDir.keep`](/configuration/build/#clean-destination-directory) to preserve other files and directories. Default is `false`. `contentDir` : (`string`) The designated directory for content files. Default is `content`. {{% module-mounts-note %}}docs/content/en/configuration/build.mdindex 6a7924301..1a0fb5f91 100644@@ -17,6 +17,9 @@ This is the default configuration: `cachebusters` : See the [cache busters](#cache-busters) section below. +`cleanDestinationDir`+: See the [clean destination directory](#clean-destination-directory) section below.+ `noJSConfigInAssets` : (`bool`) Whether to disable writing a `jsconfig.json` in your `assets` directory with mapping of imports from running [js.Build][]. This file is intended to help with intellisense/navigation inside code editors such as [VS Code][]. Note that if you do not use `js.Build`, no file will be written. @@ -55,6 +58,23 @@ When `buildStats` is enabled, Hugo writes a `hugo_stats.json` file on each build `target` : (`string`) A [regular expression](g) matching the keys in the resource cache that should be expired when `source` changes. You can use the matching regexp groups from `source` in the expression, e.g. `$1`. +## Clean destination directory++When you enable [`cleanDestinationDir`][], either in your project configuration or with the `--cleanDestinationDir` command line flag, Hugo removes files from the [`publishDir`][] that do not exist in the [`staticDir`][]. Use the `build.cleanDestinationDir` setting to retain additional files and directories, for example the `.git` file created by `git worktree`, or files required by your hosting provider:++{{< code-toggle file=hugo >}}+cleanDestinationDir = true+[build.cleanDestinationDir]+ keep = ['.git', '.domains', '_redirects', 'foo/bar/.mysecretfile']+{{< /code-toggle >}}++`keep`+: (`[]string`) A slice of [glob](g) patterns matching files and directories, relative to the `publishDir`, that Hugo should never remove. Use `**` to match in subdirectories, e.g. `**/.git` matches `.git` in any subdirectory but not at the root of the `publishDir`; list both `.git` and `**/.git` to match at any depth. In a multihost project, paths include the language directory, e.g. `en/.domains`. When a pattern matches a directory, Hugo retains the directory and all of its content. Default is `[]`.++[`cleanDestinationDir`]: /configuration/all/#cleandestinationdir+[`publishDir`]: /configuration/all/#publishdir+[`staticDir`]: /configuration/all/#staticdir+ ## Build stats {{< code-toggle config=build.buildStats />}}docs/data/docs.yamlindex 6d2578339..24c22aeb0 100644@@ -1336,6 +1336,8 @@ config: cacheBusters: - source: '(postcss|tailwind)\.config\.(js|mjs|cjs)' target: (css|styles|scss|sass)+ cleanDestinationDir:+ keep: null noJSConfigInAssets: false useResourceCacheWhen: fallback buildDrafts: falsetestscripts/commands/hugo__clean_destination_dir_keep.txtnew file mode 100644index 000000000..0d52f4647@@ -0,0 +1,76 @@+# Files and directories matching build.cleanDestinationDir.keep survive --cleanDestinationDir.++# --- Scenario 1: Without static directory ---++cpr golden_public public++hugo --cleanDestinationDir+tree public+cmp stdout clean_build.txt++# --- Scenario 2: With static directory ---++rm public++cpr golden_public public+cpr golden_static static++hugo --cleanDestinationDir+tree public+cmp stdout with_static_clean_build.txt++# Files++-- hugo.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+[build.cleanDestinationDir]+keep = ['.git', '.domains', '_redirects', 'foo/bar/.mysecretfile', 'keepdir', '**/*.keep']+-- content/_index.md --+---+title: home+---+-- layouts/home.html --+{{ .Title }}++-- golden_public/.git --+gitdir: /somewhere/.git/worktrees/pages+-- golden_public/.domains --+-- golden_public/_redirects --+-- golden_public/stale.txt --+-- golden_public/foo/bar/.mysecretfile --+-- golden_public/foo/bar/stale.txt --+-- golden_public/foo/stale.txt --+-- golden_public/keepdir/a.txt --+-- golden_public/dir/.git --+-- golden_public/dir/b.keep --+-- golden_public/dir/c.txt --++-- golden_static/l.txt --++-- clean_build.txt --+.+└─.domains+└─.git+└─_redirects+└─dir/+ └─b.keep+└─foo/+ └─bar/+ └─.mysecretfile+└─index.html+└─keepdir/+ └─a.txt+-- with_static_clean_build.txt --+.+└─.domains+└─.git+└─_redirects+└─dir/+ └─b.keep+└─foo/+ └─bar/+ └─.mysecretfile+└─index.html+└─keepdir/+ └─a.txt+└─l.txt
commands/helpers.goindex fbe1d362a..16555b6c4 100644@@ -76,9 +76,10 @@ func flagsToCfgWithAdditionalConfigBase(cd *simplecobra.Commandeer, cfg config.P // Flags with a different name in the config. keyMap := map[string]string{- "minify": "minify.minifyOutput",- "destination": "publishDir",- "editor": "newContentEditor",+ "minify": "minify.minifyOutput",+ "destination": "publishDir",+ "editor": "newContentEditor",+ "cleanDestinationDir": "build.cleanDestinationDir.enable", } // Flags that we for some reason don't want to expose in the project config.commands/hugobuilder.goindex a9cacaada..963db35c2 100644@@ -467,14 +467,14 @@ func (c *hugoBuilder) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint fs := &countingStatFs{Fs: sourceFs.Fs} syncer := fsync.NewSyncer()- var clean bool+ var clean config.CleanDestinationDir c.withConf(func(conf *commonConfig) { syncer.NoTimes = conf.configs.Base.NoTimes syncer.NoChmod = conf.configs.Base.NoChmod syncer.ChmodFilter = chmodFilter syncer.DestFs = conf.fs.PublishDirStatic- clean = conf.configs.Base.CleanDestinationDir+ clean = conf.configs.Base.Build.CleanDestinationDir }) syncer.SrcFs = fs@@ -491,22 +491,9 @@ func (c *hugoBuilder) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint return 0, err } - if clean {+ if clean.Enable { infol.Logf("removing stale files from destination")-- keep := func(f fsync.FileInfo) bool {- name := f.Name()-- // Keep git metadata files anywhere.- if name == ".gitignore" || name == ".gitattributes" {- return true- }-- // Keep dot-directories anywhere.- return f.IsDir() && strings.HasPrefix(name, ".")- }-- if err := removeStale(syncer.DestFs, sourceFs.Fs, publishDir, helpers.FilePathSeparator, keep); err != nil {+ if err := removeStale(syncer.DestFs, sourceFs.Fs, publishDir, helpers.FilePathSeparator, clean.Keep); err != nil { return 0, err } }@@ -522,7 +509,8 @@ func (c *hugoBuilder) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint // removeStale removes entries in dstDir on dst that keep reports as false and // that don't exist in srcDir on src. Unlike fsync's delete pass, it applies // keep at every level, so kept files survive inside stale directories.-func removeStale(dst, src afero.Fs, dstDir, srcDir string, keep func(fsync.FileInfo) bool) error {+// keep gets the slash separated path relative to srcDir's root, e.g. "dir/.gitignore".+func removeStale(dst, src afero.Fs, dstDir, srcDir string, keep func(path string, isDir bool) bool) error { entries, err := afero.ReadDir(dst, dstDir) if err != nil { if herrors.IsNotExist(err) {@@ -531,11 +519,11 @@ func removeStale(dst, src afero.Fs, dstDir, srcDir string, keep func(fsync.FileI return err } for _, entry := range entries {- if keep(entry) {- continue- } dstPath := filepath.Join(dstDir, entry.Name()) srcPath := filepath.Join(srcDir, entry.Name())+ if keep(strings.TrimPrefix(filepath.ToSlash(srcPath), "/"), entry.IsDir()) {+ continue+ } if _, err := src.Stat(srcPath); err == nil { if entry.IsDir() { if err := removeStale(dst, src, dstPath, srcPath, keep); err != nil {@@ -644,7 +632,7 @@ func (c *hugoBuilder) fullBuild(noBuildLock bool) error { // and it does so at the end of copyStatic() call. var cleanDestinationDir bool c.withConf(func(conf *commonConfig) {- cleanDestinationDir = conf.configs.Base.CleanDestinationDir+ cleanDestinationDir = conf.configs.Base.Build.CleanDestinationDir.Enable }) if cleanDestinationDir { if err := copyStaticFunc(); err != nil {config/allconfig/allconfig.goindex 806cedc71..a84545edb 100644@@ -785,10 +785,6 @@ type RootConfig struct { // Don't sync modification time of files for the static mounts. NoChmod bool - // Clean the destination folder before a new build.- // This currently only handles static files.- CleanDestinationDir bool- // A Glob pattern of module paths to ignore in the _vendor folder. IgnoreVendorPaths string config/allconfig/load.goindex bc35af9d6..76eb65703 100644@@ -198,6 +198,13 @@ func (l configLoader) normalizeCfg(cfg config.Provider) error { } } + if b, ok := cfg.Get("cleanDestinationDir").(bool); ok {+ hugo.Deprecate("project config cleanDestinationDir", "Use build.cleanDestinationDir.enable instead.", "v0.167.0")+ if b {+ cfg.Set("build.cleanDestinationDir.enable", true)+ }+ }+ return nil } config/commonConfig.goindex dae0ed905..cadfcb5ef 100644@@ -25,7 +25,9 @@ import ( "github.com/gobwas/glob" "github.com/gohugoio/hugo/common/hstrings" "github.com/gohugoio/hugo/common/loggers"+ "github.com/gohugoio/hugo/common/predicate" "github.com/gohugoio/hugo/common/types"+ "github.com/gohugoio/hugo/hugofs/hglob" "github.com/gohugoio/hugo/common/herrors" "github.com/mitchellh/mapstructure"@@ -86,6 +88,10 @@ type LoadConfigResult struct { var defaultBuild = BuildConfig{ UseResourceCacheWhen: "fallback", BuildStats: BuildStats{},+ CleanDestinationDir: CleanDestinationDir{+ KeepFiles: []string{"{**/,}.{git,gitignore,gitattributes}"},+ KeepDirs: []string{"{**/,}.*"},+ }, CacheBusters: []CacheBuster{ {@@ -112,6 +118,42 @@ type BuildConfig struct { // Can used to control how the resource cache gets evicted on rebuilds. CacheBusters []CacheBuster++ // Removes files from the publish directory that are not found in the static source.+ CleanDestinationDir CleanDestinationDir+}++// CleanDestinationDir configures the removal of stale files from the publish directory.+type CleanDestinationDir struct {+ // Whether to remove stale files. Can also be enabled with the --cleanDestinationDir flag.+ Enable bool++ // Glob patterns of files to keep, matched against the slash separated path relative to the publish directory, e.g. "dir/.gitignore".+ // A pattern prefixed with "! " is a negation.+ KeepFiles []string++ // Glob patterns of directories to keep with all of their content.+ KeepDirs []string++ keepFiles predicate.P[string]+ keepDirs predicate.P[string]+}++// Keep reports whether path, relative to the publish directory, should be kept.+func (c CleanDestinationDir) Keep(path string, isDir bool) bool {+ if isDir {+ return c.keepDirs(path)+ }+ return c.keepFiles(path)+}++func (c *CleanDestinationDir) compile() error {+ var err error+ if c.keepFiles, err = predicate.NewStringPredicateFromGlobs(c.KeepFiles, hglob.GetGlob); err != nil {+ return err+ }+ c.keepDirs, err = predicate.NewStringPredicateFromGlobs(c.KeepDirs, hglob.GetGlob)+ return err } // BuildStats configures if and what to write to the hugo_stats.json file.@@ -131,6 +173,8 @@ func (w BuildStats) Enabled() bool { func (b BuildConfig) clone() BuildConfig { b.CacheBusters = slices.Clone(b.CacheBusters)+ b.CleanDestinationDir.KeepFiles = slices.Clone(b.CleanDestinationDir.KeepFiles)+ b.CleanDestinationDir.KeepDirs = slices.Clone(b.CleanDestinationDir.KeepDirs) return b } @@ -174,6 +218,9 @@ func (b *BuildConfig) CompileConfig(logger loggers.Logger) error { } b.CacheBusters[i] = cb }+ if err := b.CleanDestinationDir.compile(); err != nil {+ return fmt.Errorf("failed to compile build.cleanDestinationDir: %w", err)+ } return nil } testscripts/commands/hugo__clean_destination_dir.txtindex 9191c77e9..c3273227e 100644@@ -37,24 +37,76 @@ hugo --cleanDestinationDir tree public cmp stdout mismatch_clean_build.txt +# --- Scenario 4: Custom keepFiles and keepDirs ---++rm public static++cpr golden_public_keep public++hugo --config hugo_keep.toml+tree public+cmp stdout keep_clean_build.txt++# --- Scenario 5: Negated keepFiles ---++rm public++cpr golden_public_keep public++hugo --config hugo_keep_negate.toml+tree public+cmp stdout keep_negate_clean_build.txt++# --- Scenario 6: Deprecated root cleanDestinationDir key with default keep patterns ---++rm public++cpr golden_public_keep public++hugo --config hugo_legacy.toml+tree public+cmp stdout legacy_clean_build.txt+ # Files -- hugo.toml -- disableKinds = ['page','rss','section','sitemap','taxonomy','term']+-- hugo_keep.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+[build.cleanDestinationDir]+enable = true+keepFiles = ['.domains', '_redirects']+keepDirs = ['keep-me']+-- hugo_keep_negate.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+# Render home as JSON so no HTML gets rendered.+[outputs]+home = ['json']+[build.cleanDestinationDir]+enable = true+# Remove all HTML files, keep everything else.+keepFiles = ['! **.html']+-- hugo_legacy.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+cleanDestinationDir = true -- content/_index.md -- --- title: home --- -- layouts/home.html -- {{ .Title }}+-- layouts/home.json --+{{ .Title | jsonify }} -- golden_public/.dir-a/a.txt -- -- golden_public/.b.txt --+-- golden_public/.git -- -- golden_public/.gitattributes -- -- golden_public/.gitignore -- -- golden_public/c.txt -- -- golden_public/dir-b/.dir-c/d.txt -- -- golden_public/dir-b/.e.txt --+-- golden_public/dir-b/.git -- -- golden_public/dir-b/.gitignore -- -- golden_public/dir-b/f.txt -- -- golden_public/dir-d/g.txt --@@ -68,6 +120,16 @@ title: home -- golden_static/dir-e/.m.txt -- -- golden_static/dir-e/n.txt -- +-- golden_public_keep/.domains --+-- golden_public_keep/.gitignore --+-- golden_public_keep/.hidden/x.txt --+-- golden_public_keep/_redirects --+-- golden_public_keep/a.html --+-- golden_public_keep/b.txt --+-- golden_public_keep/dir/.domains --+-- golden_public_keep/keep-me/x.html --+-- golden_public_keep/keep-me/y.txt --+ -- golden_public_mismatch/type-a/x.txt -- -- golden_public_mismatch/type-b -- @@ -79,6 +141,7 @@ title: home └─.b.txt └─.dir-a/ └─a.txt+└─.git └─.gitattributes └─.gitignore └─c.txt@@ -86,6 +149,7 @@ title: home └─.dir-c/ └─d.txt └─.e.txt+ └─.git └─.gitignore └─f.txt └─dir-d/@@ -101,11 +165,13 @@ title: home . └─.dir-a/ └─a.txt+└─.git └─.gitattributes └─.gitignore └─dir-b/ └─.dir-c/ └─d.txt+ └─.git └─.gitignore └─dir-e/ └─.dir-f/@@ -117,6 +183,7 @@ title: home └─.b.txt └─.dir-a/ └─a.txt+└─.git └─.gitattributes └─.gitignore └─.k.txt@@ -125,6 +192,7 @@ title: home └─.dir-c/ └─d.txt └─.e.txt+ └─.git └─.gitignore └─f.txt └─dir-d/@@ -143,12 +211,14 @@ title: home . └─.dir-a/ └─a.txt+└─.git └─.gitattributes └─.gitignore └─.k.txt └─dir-b/ └─.dir-c/ └─d.txt+ └─.git └─.gitignore └─dir-e/ └─.dir-f/@@ -164,3 +234,30 @@ title: home └─type-a └─type-b/ └─y.txt+-- keep_clean_build.txt --+.+└─.domains+└─_redirects+└─index.html+└─keep-me/+ └─x.html+ └─y.txt+-- keep_negate_clean_build.txt --+.+└─.domains+└─.gitignore+└─.hidden/+ └─x.txt+└─_redirects+└─b.txt+└─dir/+ └─.domains+└─index.json+└─keep-me/+ └─y.txt+-- legacy_clean_build.txt --+.+└─.gitignore+└─.hidden/+ └─x.txt+└─index.html