Skip to content

Commit 5768c1f

Browse files
authored
Merge pull request #12534 from quarto-dev/fix/publish-gh-pages-worktree
Cleanup leftover worktrees
2 parents e8cb90f + 8b96af7 commit 5768c1f

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

news/changelog-1.7.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ All changes included in 1.7:
162162

163163
- ([#12366](https://github.com/quarto-dev/quarto-cli/pull/12366)): Added Bulgarian translation for Quarto UI text (credit: @sakelariev)
164164

165+
## `quarto publish`
166+
167+
- ([#9929](https://github.com/quarto-dev/quarto-cli/issues/9929)): `quarto publish gh-pages` will now clean previous worktree directory leftover from previous deploys.
168+
165169
## Other Fixes and Improvements
166170

167171
- 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`).

src/publish/gh-pages/gh-pages.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Copyright (C) 2020-2022 Posit Software, PBC
55
*/
66

7-
import { info } from "../../deno_ral/log.ts";
7+
import { debug, info } from "../../deno_ral/log.ts";
88
import { dirname, join, relative } from "../../deno_ral/path.ts";
99
import { copy } from "../../deno_ral/fs.ts";
1010
import * as colors from "fmt/colors";
@@ -33,6 +33,8 @@ import {
3333
gitHubContextForPublish,
3434
verifyContext,
3535
} from "../common/git.ts";
36+
import { createTempContext } from "../../core/temp.ts";
37+
import { projectScratchPath } from "../../project/project-scratch.ts";
3638

3739
export const kGhpages = "gh-pages";
3840
const kGhpagesDescription = "GitHub Pages";
@@ -189,12 +191,35 @@ async function publish(
189191
type === "site" ? target?.url : undefined,
190192
);
191193

194+
const kPublishWorktreeDir = "quarto-publish-worktree-";
192195
// allocate worktree dir
193-
const tempDir = Deno.makeTempDirSync({ dir: input });
196+
const temp = createTempContext(
197+
{ prefix: kPublishWorktreeDir, dir: projectScratchPath(input) },
198+
);
199+
const tempDir = temp.baseDir;
194200
removeIfExists(tempDir);
195201

196-
const deployId = shortUuid();
202+
// cleaning up leftover by listing folder with prefix .quarto-publish-worktree- and calling git worktree rm on them
203+
const worktreeDir = Deno.readDirSync(projectScratchPath(input));
204+
for (const entry of worktreeDir) {
205+
if (
206+
entry.isDirectory && entry.name.startsWith(kPublishWorktreeDir)
207+
) {
208+
debug(
209+
`Cleaning up leftover worktree folder ${entry.name} from past deploys`,
210+
);
211+
const worktreePath = join(projectScratchPath(input), entry.name);
212+
await execProcess({
213+
cmd: ["git", "worktree", "remove", worktreePath],
214+
cwd: projectScratchPath(input),
215+
});
216+
removeIfExists(worktreePath);
217+
}
218+
}
197219

220+
// create worktree and deploy from it
221+
const deployId = shortUuid();
222+
debug(`Deploying from worktree ${tempDir} with deployId ${deployId}`);
198223
await withWorktree(input, relative(input, tempDir), async () => {
199224
// copy output to tempdir and add .nojekyll (include deployId
200225
// in .nojekyll so we can poll for completed deployment)
@@ -209,6 +234,7 @@ async function publish(
209234
["push", "--force", "origin", "HEAD:gh-pages"],
210235
]);
211236
});
237+
temp.cleanup();
212238
info("");
213239

214240
// if this is the creation of gh-pages AND this is a user home/default site

0 commit comments

Comments
 (0)