diff --git a/news/changelog-1.7.md b/news/changelog-1.7.md index 8554c5d4e16..9578926bd0c 100644 --- a/news/changelog-1.7.md +++ b/news/changelog-1.7.md @@ -162,6 +162,10 @@ All changes included in 1.7: - ([#12366](https://github.com/quarto-dev/quarto-cli/pull/12366)): Added Bulgarian translation for Quarto UI text (credit: @sakelariev) +## `quarto publish` + +- ([#9929](https://github.com/quarto-dev/quarto-cli/issues/9929)): `quarto publish gh-pages` will now clean previous worktree directory leftover from previous deploys. + ## Other Fixes and Improvements - A new folder `quarto-session-temp` can be created in `.quarto` to store temporary files created by Quarto during a rendering. Reminder: `.quarto` is for internal use of Quarto and should not be versioned (thus added to `.gitignore`). diff --git a/src/publish/gh-pages/gh-pages.ts b/src/publish/gh-pages/gh-pages.ts index 3644aa22794..019faf8ab9d 100644 --- a/src/publish/gh-pages/gh-pages.ts +++ b/src/publish/gh-pages/gh-pages.ts @@ -4,7 +4,7 @@ * Copyright (C) 2020-2022 Posit Software, PBC */ -import { info } from "../../deno_ral/log.ts"; +import { debug, info } from "../../deno_ral/log.ts"; import { dirname, join, relative } from "../../deno_ral/path.ts"; import { copy } from "../../deno_ral/fs.ts"; import * as colors from "fmt/colors"; @@ -33,6 +33,8 @@ import { gitHubContextForPublish, verifyContext, } from "../common/git.ts"; +import { createTempContext } from "../../core/temp.ts"; +import { projectScratchPath } from "../../project/project-scratch.ts"; export const kGhpages = "gh-pages"; const kGhpagesDescription = "GitHub Pages"; @@ -189,12 +191,35 @@ async function publish( type === "site" ? target?.url : undefined, ); + const kPublishWorktreeDir = "quarto-publish-worktree-"; // allocate worktree dir - const tempDir = Deno.makeTempDirSync({ dir: input }); + const temp = createTempContext( + { prefix: kPublishWorktreeDir, dir: projectScratchPath(input) }, + ); + const tempDir = temp.baseDir; removeIfExists(tempDir); - const deployId = shortUuid(); + // cleaning up leftover by listing folder with prefix .quarto-publish-worktree- and calling git worktree rm on them + const worktreeDir = Deno.readDirSync(projectScratchPath(input)); + for (const entry of worktreeDir) { + if ( + entry.isDirectory && entry.name.startsWith(kPublishWorktreeDir) + ) { + debug( + `Cleaning up leftover worktree folder ${entry.name} from past deploys`, + ); + const worktreePath = join(projectScratchPath(input), entry.name); + await execProcess({ + cmd: ["git", "worktree", "remove", worktreePath], + cwd: projectScratchPath(input), + }); + removeIfExists(worktreePath); + } + } + // create worktree and deploy from it + const deployId = shortUuid(); + debug(`Deploying from worktree ${tempDir} with deployId ${deployId}`); await withWorktree(input, relative(input, tempDir), async () => { // copy output to tempdir and add .nojekyll (include deployId // in .nojekyll so we can poll for completed deployment) @@ -209,6 +234,7 @@ async function publish( ["push", "--force", "origin", "HEAD:gh-pages"], ]); }); + temp.cleanup(); info(""); // if this is the creation of gh-pages AND this is a user home/default site