dracon-sync 0.112.13

Invisible git sync daemon for deterministic AI-assisted development
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use crate::policy::{RepoPolicyOverride, SyncPolicy};
use anyhow::{Context, Result};
use std::path::{Path, PathBuf};

pub(crate) fn ensure_standard_files(
    repo: &Path,
    policy: &SyncPolicy,
    repo_override: &RepoPolicyOverride,
    policy_base_dir: Option<&Path>,
    dry_run: bool,
) -> Result<Vec<PathBuf>> {
    if policy.standard_files.is_empty() {
        return Ok(vec![]);
    }

    let sync_base = policy_base_dir
        .map(|p| p.to_path_buf())
        .or_else(|| dirs::home_dir().map(|h| h.join(".dracon/utilities/sync")));

    let Some(base) = sync_base else {
        anyhow::bail!("cannot resolve standard files base dir: no policy path and no home dir");
    };

    let mut copied = Vec::new();

    for cfg in &policy.standard_files {
        if repo_override.skip_standard_files.contains(&cfg.target) {
            continue;
        }

        let target_path = repo.join(&cfg.target);

        if target_path.exists() && !cfg.overwrite {
            continue;
        }

        let source_path = cfg.source_path(&base);

        if !source_path.exists() {
            eprintln!(
                "⚠️ standard file template missing: {} (tried {})",
                cfg.target,
                source_path.display()
            );
            continue;
        }

        if dry_run {
            println!(
                "📝 Would copy standard file: {} -> {}",
                source_path.display(),
                target_path.display()
            );
            copied.push(target_path);
            continue;
        }

        if target_path.exists() && cfg.overwrite {
            if target_path.is_dir() {
                std::fs::remove_dir_all(&target_path).with_context(|| {
                    format!("failed to remove existing directory {}", cfg.target)
                })?;
            } else {
                std::fs::remove_file(&target_path)
                    .with_context(|| format!("failed to remove existing {}", cfg.target))?;
            }
        }

        if let Some(parent) = target_path.parent() {
            std::fs::create_dir_all(parent)
                .with_context(|| format!("failed to create directory {}", parent.display()))?;
        }

        std::fs::copy(&source_path, &target_path).with_context(|| {
            format!(
                "failed to copy {} to {}",
                source_path.display(),
                target_path.display()
            )
        })?;

        copied.push(target_path);
    }

    Ok(copied)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::policy::StandardFileConfig;
    use tempfile::TempDir;

    fn make_policy(standard_files: Vec<StandardFileConfig>) -> SyncPolicy {
        SyncPolicy {
            standard_files,
            ..Default::default()
        }
    }

    fn make_override(skip: Vec<String>) -> RepoPolicyOverride {
        RepoPolicyOverride {
            skip_standard_files: skip,
            ..Default::default()
        }
    }

    #[test]
    fn test_copies_file_when_missing() {
        let dir = TempDir::new().unwrap();
        let repo_dir = dir.path();
        let template_dir = dir.path().join("templates");
        std::fs::create_dir(&template_dir).unwrap();
        std::fs::write(template_dir.join("LICENSE"), "AGPL").unwrap();
        let sync_path = dir.path().join("sync.toml");
        let sync_dir = sync_path.parent().unwrap();

        let policy = make_policy(vec![StandardFileConfig {
            source: "templates/LICENSE".to_string(),
            target: "LICENSE".to_string(),
            overwrite: false,
        }]);

        let repo_override = make_override(vec![]);
        let result =
            ensure_standard_files(repo_dir, &policy, &repo_override, Some(sync_dir), false);
        assert!(result.is_ok());
        let copied = result.unwrap();
        assert_eq!(copied.len(), 1);
        assert_eq!(
            std::fs::read_to_string(repo_dir.join("LICENSE")).unwrap(),
            "AGPL"
        );
    }

    #[test]
    fn test_skips_when_target_exists() {
        let dir = TempDir::new().unwrap();
        let repo_dir = dir.path();
        let template_dir = dir.path().join("templates");
        std::fs::create_dir(&template_dir).unwrap();
        std::fs::write(template_dir.join("LICENSE"), "AGPL").unwrap();
        std::fs::write(repo_dir.join("LICENSE"), "EXISTING").unwrap();
        let sync_path = dir.path().join("sync.toml");
        let sync_dir = sync_path.parent().unwrap();

        let policy = make_policy(vec![StandardFileConfig {
            source: "templates/LICENSE".to_string(),
            target: "LICENSE".to_string(),
            overwrite: false,
        }]);

        let repo_override = make_override(vec![]);
        let result =
            ensure_standard_files(repo_dir, &policy, &repo_override, Some(sync_dir), false);
        assert!(result.is_ok());
        let copied = result.unwrap();
        assert!(copied.is_empty());
        assert_eq!(
            std::fs::read_to_string(repo_dir.join("LICENSE")).unwrap(),
            "EXISTING"
        );
    }

    #[test]
    fn test_overwrites_when_configured() {
        let dir = TempDir::new().unwrap();
        let repo_dir = dir.path();
        let template_dir = dir.path().join("templates");
        std::fs::create_dir(&template_dir).unwrap();
        std::fs::write(template_dir.join("LICENSE"), "NEW_AGPL").unwrap();
        std::fs::write(repo_dir.join("LICENSE"), "OLD_LICENSE").unwrap();
        let sync_path = dir.path().join("sync.toml");
        let sync_dir = sync_path.parent().unwrap();

        let policy = make_policy(vec![StandardFileConfig {
            source: "templates/LICENSE".to_string(),
            target: "LICENSE".to_string(),
            overwrite: true,
        }]);

        let repo_override = make_override(vec![]);
        let result =
            ensure_standard_files(repo_dir, &policy, &repo_override, Some(sync_dir), false);
        assert!(result.is_ok());
        let copied = result.unwrap();
        assert_eq!(copied.len(), 1);
        assert_eq!(
            std::fs::read_to_string(repo_dir.join("LICENSE")).unwrap(),
            "NEW_AGPL"
        );
    }

    #[test]
    fn test_skips_from_repo_override() {
        let dir = TempDir::new().unwrap();
        let repo_dir = dir.path();
        let template_dir = dir.path().join("templates");
        std::fs::create_dir(&template_dir).unwrap();
        std::fs::write(template_dir.join("CUSTOM.md"), "custom content").unwrap();

        let policy = make_policy(vec![StandardFileConfig {
            source: "templates/CUSTOM.md".to_string(),
            target: "CUSTOM.md".to_string(),
            overwrite: false,
        }]);

        let repo_override = make_override(vec!["CUSTOM.md".to_string()]);
        let result = ensure_standard_files(repo_dir, &policy, &repo_override, None, false);
        assert!(result.is_ok());
        let copied = result.unwrap();
        assert!(copied.is_empty());
        assert!(!repo_dir.join("CUSTOM.md").exists());
    }

    #[test]
    fn test_warns_when_template_missing() {
        let dir = TempDir::new().unwrap();
        let repo_dir = dir.path();

        let policy = make_policy(vec![StandardFileConfig {
            source: "templates/NONEXISTENT".to_string(),
            target: "NONEXISTENT.txt".to_string(),
            overwrite: false,
        }]);

        let repo_override = make_override(vec![]);
        let result = ensure_standard_files(repo_dir, &policy, &repo_override, None, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_dry_run_does_not_copy_files() {
        let dir = TempDir::new().unwrap();
        let repo_dir = dir.path();
        let template_dir = dir.path().join("templates");
        std::fs::create_dir(&template_dir).unwrap();
        std::fs::write(template_dir.join("LICENSE"), "AGPL").unwrap();
        let sync_path = dir.path().join("sync.toml");
        let sync_dir = sync_path.parent().unwrap();

        let policy = make_policy(vec![StandardFileConfig {
            source: "templates/LICENSE".to_string(),
            target: "LICENSE".to_string(),
            overwrite: false,
        }]);

        let repo_override = make_override(vec![]);
        let result = ensure_standard_files(repo_dir, &policy, &repo_override, Some(sync_dir), true);
        assert!(result.is_ok());
        let copied = result.unwrap();
        assert_eq!(copied.len(), 1);
        assert!(
            !repo_dir.join("LICENSE").exists(),
            "dry-run must not write files"
        );
    }

    #[test]
    fn test_subdirectory_target_creates_parent() {
        let dir = TempDir::new().unwrap();
        let repo_dir = dir.path();
        let template_dir = dir.path().join("templates");
        std::fs::create_dir(&template_dir).unwrap();
        std::fs::write(template_dir.join("LICENSE"), "AGPL").unwrap();
        let sync_path = dir.path().join("sync.toml");
        let sync_dir = sync_path.parent().unwrap();

        let policy = make_policy(vec![StandardFileConfig {
            source: "templates/LICENSE".to_string(),
            target: "docs/LICENSE".to_string(),
            overwrite: false,
        }]);

        let repo_override = make_override(vec![]);
        let result =
            ensure_standard_files(repo_dir, &policy, &repo_override, Some(sync_dir), false);
        assert!(result.is_ok());
        let copied = result.unwrap();
        assert_eq!(copied.len(), 1);
        assert_eq!(
            std::fs::read_to_string(repo_dir.join("docs/LICENSE")).unwrap(),
            "AGPL"
        );
    }

    #[test]
    fn test_overwrite_directory_target() {
        let dir = TempDir::new().unwrap();
        let repo_dir = dir.path();
        let template_dir = dir.path().join("templates");
        std::fs::create_dir(&template_dir).unwrap();
        std::fs::write(template_dir.join("LICENSE"), "AGPL").unwrap();
        std::fs::create_dir(repo_dir.join("LICENSE")).unwrap();
        let sync_path = dir.path().join("sync.toml");
        let sync_dir = sync_path.parent().unwrap();

        let policy = make_policy(vec![StandardFileConfig {
            source: "templates/LICENSE".to_string(),
            target: "LICENSE".to_string(),
            overwrite: true,
        }]);

        let repo_override = make_override(vec![]);
        let result =
            ensure_standard_files(repo_dir, &policy, &repo_override, Some(sync_dir), false);
        assert!(result.is_ok());
        assert!(repo_dir.join("LICENSE").is_file());
        assert_eq!(
            std::fs::read_to_string(repo_dir.join("LICENSE")).unwrap(),
            "AGPL"
        );
    }

    #[test]
    fn test_absolute_source_path() {
        let dir = TempDir::new().unwrap();
        let repo_dir = dir.path();
        let abs_template = dir.path().join("custom_license.txt");
        std::fs::write(&abs_template, "CUSTOM").unwrap();

        let policy = make_policy(vec![StandardFileConfig {
            source: abs_template.to_string_lossy().to_string(),
            target: "LICENSE".to_string(),
            overwrite: false,
        }]);

        let repo_override = make_override(vec![]);
        let result = ensure_standard_files(repo_dir, &policy, &repo_override, None, false);
        assert!(result.is_ok());
        let copied = result.unwrap();
        assert_eq!(copied.len(), 1);
        assert_eq!(
            std::fs::read_to_string(repo_dir.join("LICENSE")).unwrap(),
            "CUSTOM"
        );
    }

    #[test]
    fn test_funding_yml_in_dot_github_subdir() {
        // GitHub discovers FUNDING.yml at .github/FUNDING.yml. The standard
        // files flow must allow long-form entries that target subdirectories
        // like .github/ while pulling the source from templates/FUNDING.yml.
        let dir = TempDir::new().unwrap();
        let repo_dir = dir.path();
        let template_dir = dir.path().join("templates");
        std::fs::create_dir(&template_dir).unwrap();
        std::fs::write(template_dir.join("FUNDING.yml"), "github: []\n").unwrap();
        let sync_path = dir.path().join("sync.toml");
        let sync_dir = sync_path.parent().unwrap();

        let policy = make_policy(vec![StandardFileConfig {
            source: "templates/FUNDING.yml".to_string(),
            target: ".github/FUNDING.yml".to_string(),
            overwrite: false,
        }]);

        let repo_override = make_override(vec![]);
        let result =
            ensure_standard_files(repo_dir, &policy, &repo_override, Some(sync_dir), false);
        assert!(result.is_ok());
        let copied = result.unwrap();
        assert_eq!(copied.len(), 1);
        assert!(repo_dir.join(".github/FUNDING.yml").exists());
        assert_eq!(
            std::fs::read_to_string(repo_dir.join(".github/FUNDING.yml")).unwrap(),
            "github: []\n"
        );
    }

    #[test]
    fn test_funding_yml_skip_standard_files_optout() {
        // Per-repo skip_standard_files must opt out FUNDING.yml cleanly.
        let dir = TempDir::new().unwrap();
        let repo_dir = dir.path();
        let template_dir = dir.path().join("templates");
        std::fs::create_dir(&template_dir).unwrap();
        std::fs::write(template_dir.join("FUNDING.yml"), "github: []\n").unwrap();

        let policy = make_policy(vec![StandardFileConfig {
            source: "templates/FUNDING.yml".to_string(),
            target: ".github/FUNDING.yml".to_string(),
            overwrite: false,
        }]);

        let repo_override = make_override(vec![".github/FUNDING.yml".to_string()]);
        let result = ensure_standard_files(repo_dir, &policy, &repo_override, None, false);
        assert!(result.is_ok());
        let copied = result.unwrap();
        assert!(copied.is_empty());
        assert!(!repo_dir.join(".github/FUNDING.yml").exists());
    }

    #[test]
    fn test_short_form_source_resolution() {
        let dir = TempDir::new().unwrap();
        let repo_dir = dir.path();
        let template_dir = dir.path().join("templates");
        std::fs::create_dir(&template_dir).unwrap();
        std::fs::write(template_dir.join("LICENSE"), "AGPLv3").unwrap();
        let sync_path = dir.path().join("sync.toml");
        let sync_dir = sync_path.parent().unwrap();

        let policy = make_policy(vec![StandardFileConfig {
            source: "templates/LICENSE".to_string(),
            target: "LICENSE".to_string(),
            overwrite: false,
        }]);

        let repo_override = make_override(vec![]);
        let result =
            ensure_standard_files(repo_dir, &policy, &repo_override, Some(sync_dir), false);
        assert!(result.is_ok());
        let copied = result.unwrap();
        assert_eq!(copied.len(), 1);
        assert_eq!(
            std::fs::read_to_string(repo_dir.join("LICENSE")).unwrap(),
            "AGPLv3"
        );
    }
}