Skip to content

Commit fa9ed89

Browse files
committed
Fix sync for untracked incoming case
1 parent 14eb67a commit fa9ed89

1 file changed

Lines changed: 49 additions & 22 deletions

File tree

‎scripts/sync.ts‎

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export async function sync() {
3333
if (!currentStage) {
3434
throw new Error("Current branch is not a tutorial stage");
3535
}
36-
const preservedUntrackedFiles = await preserveUntrackedFiles();
36+
const preservedUntrackedFiles = await createPreservedUntrackedFiles();
37+
await preserveCurrentUntrackedFiles(preservedUntrackedFiles);
3738

3839
// get the current commit message
3940
const commitMessage = await getCurrentCommitMessage();
@@ -48,36 +49,50 @@ export async function sync() {
4849

4950
try {
5051
for (const stage of previousStages) {
51-
await switchBranch(stage.name);
52+
await switchBranchPreservingUntracked(
53+
stage.name,
54+
preservedUntrackedFiles,
55+
);
5256
await pullToThisStage(currentBranch);
5357
const futurePaths = getFuturePathsFrom(stage.name);
5458
await removeFuturePaths(futurePaths);
5559
await commitAllChanges(commitMessage);
5660
}
5761

5862
for (const stage of nextStages) {
59-
await switchBranch(stage.name);
63+
await switchBranchPreservingUntracked(
64+
stage.name,
65+
preservedUntrackedFiles,
66+
);
6067
await pullToThisStage(currentBranch);
6168
const futurePaths = getFuturePathsFrom(stage.name);
6269
await restoreFuturePaths(futurePaths);
6370
await commitAllChanges(commitMessage);
6471
}
6572

6673
if (currentBranch !== "main") {
67-
await switchBranch("main");
74+
await switchBranchPreservingUntracked("main", preservedUntrackedFiles);
6875
await pullToThisStage(currentBranch);
6976
await commitAllChanges(commitMessage);
7077
}
7178

72-
await switchBranch(currentBranch);
79+
await switchBranchPreservingUntracked(
80+
currentBranch,
81+
preservedUntrackedFiles,
82+
);
7383

7484
await syncDocsContent();
7585
} finally {
76-
if ((await getCurrentBranch()) !== currentBranch) {
77-
await switchBranch(currentBranch);
86+
try {
87+
if ((await getCurrentBranch()) !== currentBranch) {
88+
await switchBranchPreservingUntracked(
89+
currentBranch,
90+
preservedUntrackedFiles,
91+
);
92+
}
93+
} finally {
94+
await restorePreservedUntrackedFiles(preservedUntrackedFiles);
7895
}
79-
80-
await restorePreservedUntrackedFiles(preservedUntrackedFiles);
8196
}
8297

8398
// for stages before this one
@@ -107,37 +122,49 @@ export async function sync() {
107122
type PreservedUntrackedFiles = {
108123
backupRoot: string;
109124
files: string[];
110-
} | null;
125+
};
126+
127+
async function createPreservedUntrackedFiles(): Promise<PreservedUntrackedFiles> {
128+
return {
129+
backupRoot: await mkdtemp(join(tmpdir(), "tutorial-sync-untracked-")),
130+
files: [],
131+
};
132+
}
133+
134+
async function switchBranchPreservingUntracked(
135+
branchName: string,
136+
preservedFiles: PreservedUntrackedFiles,
137+
): Promise<void> {
138+
await preserveCurrentUntrackedFiles(preservedFiles);
139+
await switchBranch(branchName);
140+
}
111141

112-
async function preserveUntrackedFiles(): Promise<PreservedUntrackedFiles> {
142+
async function preserveCurrentUntrackedFiles(
143+
preservedFiles: PreservedUntrackedFiles,
144+
): Promise<void> {
113145
const files = await getUntrackedFiles();
114146
if (files.length === 0) {
115-
return null;
147+
return;
116148
}
117149

118-
const backupRoot = await mkdtemp(join(tmpdir(), "tutorial-sync-untracked-"));
119-
120150
for (const file of files) {
121151
if (!existsSync(file)) {
122152
continue;
123153
}
124154

125-
const targetPath = join(backupRoot, file);
155+
const targetPath = join(preservedFiles.backupRoot, file);
126156
await mkdir(dirname(targetPath), { recursive: true });
127-
await copyFile(file, targetPath);
157+
if (!existsSync(targetPath)) {
158+
await copyFile(file, targetPath);
159+
preservedFiles.files.push(file);
160+
}
128161
await rm(file, { force: true });
129162
}
130-
131-
return { backupRoot, files };
132163
}
133164

134165
async function restorePreservedUntrackedFiles(
135166
preservedFiles: PreservedUntrackedFiles,
136167
): Promise<void> {
137-
if (!preservedFiles) {
138-
return;
139-
}
140-
141168
try {
142169
for (const file of preservedFiles.files) {
143170
if (existsSync(file)) {

0 commit comments

Comments
 (0)