Skip to content

Commit 78e0167

Browse files
Merge pull request #30 from alexphillips-dev/backmerge/main-to-dev-manual
Sync main into dev
2 parents 1e5659a + 86168a6 commit 78e0167

3 files changed

Lines changed: 180 additions & 11 deletions

File tree

scripts/sync_main_to_dev.sh

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,56 @@ release_only_path() {
2323
}
2424

2525
restore_release_only_paths_from_previous() {
26+
reconcile_release_only_paths_from_ref HEAD^ "$@"
27+
}
28+
29+
path_exists_at_ref() {
30+
local ref="${1:-}"
31+
local path="${2:-}"
32+
git cat-file -e "${ref}:${path}" 2>/dev/null
33+
}
34+
35+
reconcile_release_only_paths_from_ref() {
36+
local source_ref="${1:-}"
37+
shift || true
2638
local file=""
2739
local -a restore_paths=()
40+
local -a remove_paths=()
2841
for file in "$@"; do
2942
if release_only_path "${file}"; then
30-
restore_paths+=("${file}")
43+
if path_exists_at_ref "${source_ref}" "${file}"; then
44+
restore_paths+=("${file}")
45+
else
46+
remove_paths+=("${file}")
47+
fi
3148
fi
3249
done
33-
if [ "${#restore_paths[@]}" -eq 0 ]; then
34-
return 0
50+
if [ "${#restore_paths[@]}" -gt 0 ]; then
51+
git restore --source="${source_ref}" --staged --worktree -- "${restore_paths[@]}"
52+
fi
53+
if [ "${#remove_paths[@]}" -gt 0 ]; then
54+
git rm -f --ignore-unmatch -- "${remove_paths[@]}" >/dev/null 2>&1 || true
3555
fi
36-
git restore --source=HEAD^ --staged --worktree -- "${restore_paths[@]}"
56+
}
57+
58+
commit_paths_for_sync() {
59+
local commit="${1:-}"
60+
local status=""
61+
local first_path=""
62+
local second_path=""
63+
while IFS=$'\t' read -r status first_path second_path; do
64+
if [ -z "${status}" ]; then
65+
continue
66+
fi
67+
case "${status}" in
68+
R*|C*)
69+
printf '%s\n' "${first_path}" "${second_path}"
70+
;;
71+
*)
72+
printf '%s\n' "${first_path}"
73+
;;
74+
esac
75+
done < <(git show --pretty=format: --name-status --find-renames "${commit}")
3776
}
3877

3978
latest_dev_merge_into_main() {
@@ -42,6 +81,8 @@ latest_dev_merge_into_main() {
4281

4382
resolve_release_only_conflicts() {
4483
local path=""
84+
local -a commit_paths=("$@")
85+
local -a reconcile_paths=()
4586
mapfile -t CONFLICT_PATHS < <(git diff --name-only --diff-filter=U || true)
4687
if [ "${#CONFLICT_PATHS[@]}" -eq 0 ]; then
4788
return 1
@@ -51,8 +92,8 @@ resolve_release_only_conflicts() {
5192
return 1
5293
fi
5394
done
54-
git checkout --ours -- "${CONFLICT_PATHS[@]}"
55-
git add -- "${CONFLICT_PATHS[@]}"
95+
reconcile_paths=("${commit_paths[@]}" "${CONFLICT_PATHS[@]}")
96+
reconcile_release_only_paths_from_ref HEAD "${reconcile_paths[@]}"
5697
return 0
5798
}
5899

@@ -102,7 +143,7 @@ fi
102143
applied_commits=0
103144

104145
for COMMIT in "${MAIN_ONLY_COMMITS[@]}"; do
105-
mapfile -t COMMIT_PATHS < <(git show --pretty=format: --name-only "${COMMIT}" | sed '/^[[:space:]]*$/d')
146+
mapfile -t COMMIT_PATHS < <(commit_paths_for_sync "${COMMIT}" | sed '/^[[:space:]]*$/d')
106147
if [ "${#COMMIT_PATHS[@]}" -eq 0 ]; then
107148
continue
108149
fi
@@ -122,14 +163,37 @@ for COMMIT in "${MAIN_ONLY_COMMITS[@]}"; do
122163
continue
123164
fi
124165

166+
commit_applied=0
167+
125168
if ! git cherry-pick -x "${COMMIT}"; then
126-
if resolve_release_only_conflicts && git cherry-pick --continue; then
127-
:
169+
if resolve_release_only_conflicts "${COMMIT_PATHS[@]}"; then
170+
mapfile -t REMAINING_CONFLICT_PATHS < <(git diff --name-only --diff-filter=U || true)
171+
if [ "${#REMAINING_CONFLICT_PATHS[@]}" -gt 0 ]; then
172+
echo "Cherry-pick failed for ${COMMIT}; aborting auto back-merge." >&2
173+
git cherry-pick --abort
174+
exit 1
175+
fi
176+
if git diff --cached --quiet && git diff --quiet; then
177+
git cherry-pick --skip
178+
continue
179+
elif git cherry-pick --continue; then
180+
commit_applied=1
181+
else
182+
echo "Cherry-pick failed for ${COMMIT}; aborting auto back-merge." >&2
183+
git cherry-pick --abort
184+
exit 1
185+
fi
128186
else
129187
echo "Cherry-pick failed for ${COMMIT}; aborting auto back-merge." >&2
130188
git cherry-pick --abort
131189
exit 1
132190
fi
191+
else
192+
commit_applied=1
193+
fi
194+
195+
if [ "${commit_applied}" -eq 0 ]; then
196+
continue
133197
fi
134198

135199
if [ "${commit_touched_release_paths}" -eq 1 ]; then

tests/sync-main-to-dev.test.mjs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import test from 'node:test';
2+
import assert from 'node:assert/strict';
3+
import fs from 'node:fs';
4+
import os from 'node:os';
5+
import path from 'node:path';
6+
import { execFileSync } from 'node:child_process';
7+
8+
const repoRoot = path.resolve(process.cwd());
9+
const syncMainToDevPath = path.join(repoRoot, 'scripts/sync_main_to_dev.sh');
10+
11+
function gitTestEnv(extraEnv = {}) {
12+
const env = { ...process.env, ...extraEnv };
13+
delete env.GIT_ALTERNATE_OBJECT_DIRECTORIES;
14+
delete env.GIT_COMMON_DIR;
15+
delete env.GIT_DIR;
16+
delete env.GIT_INDEX_FILE;
17+
delete env.GIT_NAMESPACE;
18+
delete env.GIT_OBJECT_DIRECTORY;
19+
delete env.GIT_PREFIX;
20+
delete env.GIT_WORK_TREE;
21+
return env;
22+
}
23+
24+
function runGit(args, cwd) {
25+
return execFileSync('git', args, {
26+
cwd,
27+
env: gitTestEnv(),
28+
encoding: 'utf8',
29+
stdio: ['ignore', 'pipe', 'pipe']
30+
}).trim();
31+
}
32+
33+
function writeFile(targetPath, contents) {
34+
fs.mkdirSync(path.dirname(targetPath), { recursive: true });
35+
fs.writeFileSync(targetPath, contents);
36+
}
37+
38+
test('sync_main_to_dev keeps dev release artifacts when main release commits rename and add release-only paths', (t) => {
39+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'fvplus-sync-main-to-dev-'));
40+
t.after(() => {
41+
fs.rmSync(tempDir, { recursive: true, force: true });
42+
});
43+
44+
const remoteDir = path.join(tempDir, 'remote.git');
45+
const workDir = path.join(tempDir, 'work');
46+
47+
runGit(['init', '--bare', remoteDir], tempDir);
48+
runGit(['clone', remoteDir, workDir], tempDir);
49+
runGit(['config', 'user.name', 'FolderView Plus Test'], workDir);
50+
runGit(['config', 'user.email', 'folderviewplus@example.com'], workDir);
51+
52+
writeFile(path.join(workDir, 'scripts', 'sync_main_to_dev.sh'), fs.readFileSync(syncMainToDevPath, 'utf8'));
53+
writeFile(path.join(workDir, 'app.txt'), 'base\n');
54+
writeFile(path.join(workDir, 'folderview.plus.plg'), 'version=2026.04.05.12\n');
55+
writeFile(path.join(workDir, 'docs', 'releases', '2026.04.05.12.md'), 'Release 12\n');
56+
writeFile(path.join(workDir, 'archive', 'folderview.plus-2026.04.05.12.txz'), 'archive 12\n');
57+
writeFile(path.join(workDir, 'archive', 'folderview.plus-2026.04.05.12.txz.sha256'), 'sha256 12\n');
58+
59+
runGit(['add', '.'], workDir);
60+
runGit(['commit', '-m', 'Initial repository state'], workDir);
61+
runGit(['branch', '-M', 'main'], workDir);
62+
runGit(['push', '-u', 'origin', 'main'], workDir);
63+
64+
runGit(['checkout', '-B', 'dev'], workDir);
65+
runGit(['mv', 'docs/releases/2026.04.05.12.md', 'docs/releases/2026.04.05.14.md'], workDir);
66+
runGit(['mv', 'archive/folderview.plus-2026.04.05.12.txz', 'archive/folderview.plus-2026.04.05.14.txz'], workDir);
67+
fs.rmSync(path.join(workDir, 'archive', 'folderview.plus-2026.04.05.12.txz.sha256'));
68+
writeFile(path.join(workDir, 'archive', 'folderview.plus-2026.04.05.14.txz.sha256'), 'sha256 14\n');
69+
writeFile(path.join(workDir, 'folderview.plus.plg'), 'version=2026.04.05.14\n');
70+
runGit(['add', '-A'], workDir);
71+
runGit(['commit', '-m', 'Dev release artifacts'], workDir);
72+
runGit(['push', '-u', 'origin', 'dev'], workDir);
73+
74+
runGit(['checkout', 'main'], workDir);
75+
writeFile(path.join(workDir, 'app.txt'), 'base\nmain change\n');
76+
runGit(['mv', 'docs/releases/2026.04.05.12.md', 'docs/releases/2026.04.05.13.md'], workDir);
77+
runGit(['mv', 'archive/folderview.plus-2026.04.05.12.txz', 'archive/folderview.plus-2026.04.05.13.txz'], workDir);
78+
fs.rmSync(path.join(workDir, 'archive', 'folderview.plus-2026.04.05.12.txz.sha256'));
79+
writeFile(path.join(workDir, 'archive', 'folderview.plus-2026.04.05.13.txz.sha256'), 'sha256 13\n');
80+
writeFile(path.join(workDir, 'folderview.plus.plg'), 'version=2026.04.05.13\n');
81+
runGit(['add', '-A'], workDir);
82+
runGit(['commit', '-m', 'Stable release 2026.04.05.13'], workDir);
83+
runGit(['push', 'origin', 'main'], workDir);
84+
85+
execFileSync('bash', ['scripts/sync_main_to_dev.sh'], {
86+
cwd: workDir,
87+
encoding: 'utf8',
88+
env: gitTestEnv({
89+
FVPLUS_BACKMERGE_LOCAL_BRANCH: 'backmerge/test'
90+
}),
91+
stdio: ['ignore', 'pipe', 'pipe']
92+
});
93+
94+
const diffFiles = runGit(['diff', '--name-only', 'origin/dev..backmerge/test'], workDir)
95+
.split(/\r?\n/)
96+
.filter(Boolean);
97+
assert.deepEqual(diffFiles, ['app.txt']);
98+
assert.equal(runGit(['show', 'backmerge/test:app.txt'], workDir), 'base\nmain change');
99+
assert.equal(runGit(['show', 'backmerge/test:folderview.plus.plg'], workDir), 'version=2026.04.05.14');
100+
assert.match(runGit(['ls-tree', '-r', '--name-only', 'backmerge/test'], workDir), /docs\/releases\/2026\.04\.05\.14\.md/);
101+
assert.doesNotMatch(runGit(['ls-tree', '-r', '--name-only', 'backmerge/test'], workDir), /2026\.04\.05\.13/);
102+
});

tests/versioning-guard.test.mjs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,11 +526,14 @@ test('back-merge sync script keeps dev linear and drops stable release artifacts
526526
assert.match(syncMainToDev, /git rev-list --reverse --first-parent --no-merges "\$\{SYNC_BASE\}\.\.\$\{MAIN_REF\}"/);
527527
assert.match(syncMainToDev, /git cherry-pick -x "\$\{COMMIT\}"/);
528528
assert.match(syncMainToDev, /git diff --name-only --diff-filter=U/);
529-
assert.match(syncMainToDev, /git checkout --ours -- "\$\{CONFLICT_PATHS\[@\]\}"/);
529+
assert.match(syncMainToDev, /commit_paths_for_sync/);
530+
assert.match(syncMainToDev, /git show --pretty=format: --name-status --find-renames "\$\{commit\}"/);
531+
assert.match(syncMainToDev, /reconcile_release_only_paths_from_ref HEAD "\$\{reconcile_paths\[@\]\}"/);
530532
assert.match(syncMainToDev, /git cherry-pick --continue/);
531533
assert.match(syncMainToDev, /docs\/releases\/\*\.md/);
532534
assert.match(syncMainToDev, /restore_release_only_paths_from_previous/);
533-
assert.match(syncMainToDev, /git restore --source=HEAD\^ --staged --worktree -- "\$\{restore_paths\[@\]\}"/);
535+
assert.match(syncMainToDev, /git restore --source="\$\{source_ref\}" --staged --worktree -- "\$\{restore_paths\[@\]\}"/);
536+
assert.match(syncMainToDev, /git rm -f --ignore-unmatch -- "\$\{remove_paths\[@\]\}"/);
534537
assert.doesNotMatch(syncMainToDev, /git merge --no-ff --no-commit/);
535538
});
536539

0 commit comments

Comments
 (0)