Skip to content

Commit ed3a09d

Browse files
Fix back-merge release artifact reconciliation
1 parent edb6afd commit ed3a09d

3 files changed

Lines changed: 165 additions & 11 deletions

File tree

scripts/sync_main_to_dev.sh

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +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-
if git cat-file -e "HEAD^:${file}" 2>/dev/null; then
43+
if path_exists_at_ref "${source_ref}" "${file}"; then
3144
restore_paths+=("${file}")
45+
else
46+
remove_paths+=("${file}")
3247
fi
3348
fi
3449
done
35-
if [ "${#restore_paths[@]}" -eq 0 ]; then
36-
return 0
50+
if [ "${#restore_paths[@]}" -gt 0 ]; then
51+
git restore --source="${source_ref}" --staged --worktree -- "${restore_paths[@]}"
3752
fi
38-
git restore --source=HEAD^ --staged --worktree -- "${restore_paths[@]}"
53+
if [ "${#remove_paths[@]}" -gt 0 ]; then
54+
git rm -f --ignore-unmatch -- "${remove_paths[@]}" >/dev/null 2>&1 || true
55+
fi
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}")
3976
}
4077

4178
latest_dev_merge_into_main() {
@@ -44,6 +81,8 @@ latest_dev_merge_into_main() {
4481

4582
resolve_release_only_conflicts() {
4683
local path=""
84+
local -a commit_paths=("$@")
85+
local -a reconcile_paths=()
4786
mapfile -t CONFLICT_PATHS < <(git diff --name-only --diff-filter=U || true)
4887
if [ "${#CONFLICT_PATHS[@]}" -eq 0 ]; then
4988
return 1
@@ -53,8 +92,8 @@ resolve_release_only_conflicts() {
5392
return 1
5493
fi
5594
done
56-
git checkout --ours -- "${CONFLICT_PATHS[@]}"
57-
git add -- "${CONFLICT_PATHS[@]}"
95+
reconcile_paths=("${commit_paths[@]}" "${CONFLICT_PATHS[@]}")
96+
reconcile_release_only_paths_from_ref HEAD "${reconcile_paths[@]}"
5897
return 0
5998
}
6099

@@ -104,7 +143,7 @@ fi
104143
applied_commits=0
105144

106145
for COMMIT in "${MAIN_ONLY_COMMITS[@]}"; do
107-
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')
108147
if [ "${#COMMIT_PATHS[@]}" -eq 0 ]; then
109148
continue
110149
fi
@@ -124,14 +163,37 @@ for COMMIT in "${MAIN_ONLY_COMMITS[@]}"; do
124163
continue
125164
fi
126165

166+
commit_applied=0
167+
127168
if ! git cherry-pick -x "${COMMIT}"; then
128-
if resolve_release_only_conflicts && git cherry-pick --continue; then
129-
:
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
130186
else
131187
echo "Cherry-pick failed for ${COMMIT}; aborting auto back-merge." >&2
132188
git cherry-pick --abort
133189
exit 1
134190
fi
191+
else
192+
commit_applied=1
193+
fi
194+
195+
if [ "${commit_applied}" -eq 0 ]; then
196+
continue
135197
fi
136198

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

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

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

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)