git-synchronizer 0.1.1

Easily synchronize your local branches and worktrees
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use std::collections::HashSet;

use anyhow::Result;
use globset::{Glob, GlobSet, GlobSetBuilder};

use crate::config::Config;
use crate::git::Git;

/// Build a `GlobSet` from the protected branch patterns in config.
pub fn build_protected_matcher(config: &Config) -> Result<GlobSet> {
    let mut builder = GlobSetBuilder::new();
    for pattern in &config.protected {
        builder.add(Glob::new(pattern)?);
    }
    Ok(builder.build()?)
}

/// Check whether a branch is protected, considering both global glob patterns
/// and per-branch `branch.<name>.sync-protected` config.
fn is_protected(branch: &str, matcher: &GlobSet, branch_protected: &HashSet<String>) -> bool {
    matcher.is_match(branch) || branch_protected.contains(branch)
}

/// Resolve protected patterns to actual existing local branch names.
///
/// Literal patterns (e.g. "main") are kept as-is if they exist.
/// Glob patterns (e.g. "release/*") are expanded to matching branches.
/// Branches marked with per-branch `sync-protected` config are also included.
fn resolve_merge_targets(git: &Git, config: &Config) -> Result<Vec<String>> {
    let matcher = build_protected_matcher(config)?;
    let branch_protected: HashSet<String> = git.branch_protected_list()?.into_iter().collect();
    let all_branches = git.local_branches()?;

    let targets: Vec<String> = all_branches
        .into_iter()
        .filter(|b| is_protected(b, &matcher, &branch_protected))
        .collect();

    Ok(targets)
}

/// Return local branches that are merged into *any* of the protected branches
/// and are not themselves protected.
pub fn find_merged_local(git: &Git, config: &Config) -> Result<Vec<String>> {
    let matcher = build_protected_matcher(config)?;
    let branch_protected: HashSet<String> = git.branch_protected_list()?.into_iter().collect();
    let current = git.current_branch()?;
    let targets = resolve_merge_targets(git, config)?;

    let mut candidates: Vec<String> = Vec::new();

    for target in &targets {
        let merged = git.merged_branches(target)?;
        for branch in merged {
            if branch == current {
                continue;
            }
            if is_protected(&branch, &matcher, &branch_protected) {
                continue;
            }
            if !candidates.contains(&branch) {
                candidates.push(branch);
            }
        }
    }

    // Also check branches not caught by --merged (rebase merge detection via git cherry)
    let all_branches = git.local_branches()?;
    for branch in &all_branches {
        if candidates.contains(branch)
            || *branch == current
            || is_protected(branch, &matcher, &branch_protected)
        {
            continue;
        }
        for target in &targets {
            if git.cherry_merged(target, branch).unwrap_or(false) && !candidates.contains(branch) {
                candidates.push(branch.clone());
                break;
            }
        }
    }

    candidates.sort();
    Ok(candidates)
}

/// Return remote branches that are merged into *any* of the protected branches
/// and are not themselves protected, for the given remote.
pub fn find_merged_remote(git: &Git, config: &Config, remote: &str) -> Result<Vec<String>> {
    let matcher = build_protected_matcher(config)?;
    let branch_protected: HashSet<String> = git.branch_protected_list()?.into_iter().collect();
    let targets = resolve_merge_targets(git, config)?;

    let mut candidates: Vec<String> = Vec::new();

    for target in &targets {
        let merged = git.merged_remote_branches(target, remote)?;
        for branch in merged {
            if is_protected(&branch, &matcher, &branch_protected) {
                continue;
            }
            if !candidates.contains(&branch) {
                candidates.push(branch);
            }
        }
    }

    candidates.sort();
    Ok(candidates)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::process::Command as StdCommand;

    fn init_repo_with_branches() -> (tempfile::TempDir, Git) {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path();

        StdCommand::new("git")
            .args(["init", "--initial-branch=main"])
            .current_dir(path)
            .output()
            .unwrap();
        StdCommand::new("git")
            .args(["config", "user.email", "test@test.com"])
            .current_dir(path)
            .output()
            .unwrap();
        StdCommand::new("git")
            .args(["config", "user.name", "Test"])
            .current_dir(path)
            .output()
            .unwrap();

        std::fs::write(path.join("README.md"), "# test").unwrap();
        StdCommand::new("git")
            .args(["add", "."])
            .current_dir(path)
            .output()
            .unwrap();
        StdCommand::new("git")
            .args(["commit", "-m", "init"])
            .current_dir(path)
            .output()
            .unwrap();

        // Create and merge a feature branch
        StdCommand::new("git")
            .args(["checkout", "-b", "feature/done"])
            .current_dir(path)
            .output()
            .unwrap();
        std::fs::write(path.join("done.txt"), "done").unwrap();
        StdCommand::new("git")
            .args(["add", "."])
            .current_dir(path)
            .output()
            .unwrap();
        StdCommand::new("git")
            .args(["commit", "-m", "feature done"])
            .current_dir(path)
            .output()
            .unwrap();
        StdCommand::new("git")
            .args(["checkout", "main"])
            .current_dir(path)
            .output()
            .unwrap();
        StdCommand::new("git")
            .args(["merge", "feature/done"])
            .current_dir(path)
            .output()
            .unwrap();

        // Create an unmerged branch
        StdCommand::new("git")
            .args(["checkout", "-b", "feature/wip"])
            .current_dir(path)
            .output()
            .unwrap();
        std::fs::write(path.join("wip.txt"), "wip").unwrap();
        StdCommand::new("git")
            .args(["add", "."])
            .current_dir(path)
            .output()
            .unwrap();
        StdCommand::new("git")
            .args(["commit", "-m", "work in progress"])
            .current_dir(path)
            .output()
            .unwrap();

        // Create a release branch (should be protected)
        StdCommand::new("git")
            .args(["checkout", "main"])
            .current_dir(path)
            .output()
            .unwrap();
        StdCommand::new("git")
            .args(["checkout", "-b", "release/1.0"])
            .current_dir(path)
            .output()
            .unwrap();
        StdCommand::new("git")
            .args(["checkout", "main"])
            .current_dir(path)
            .output()
            .unwrap();

        let git = Git::with_workdir(false, path);
        (dir, git)
    }

    #[test]
    fn test_build_protected_matcher() {
        let config = Config {
            protected: vec!["main".to_string(), "release/*".to_string()],
            remotes: None,
            worktrunk: None,
        };
        let matcher = build_protected_matcher(&config).unwrap();
        assert!(matcher.is_match("main"));
        assert!(matcher.is_match("release/1.0"));
        assert!(matcher.is_match("release/2.0-beta"));
        assert!(!matcher.is_match("feature/foo"));
        assert!(!matcher.is_match("develop"));
    }

    #[test]
    fn test_find_merged_local() {
        let (_dir, git) = init_repo_with_branches();
        let config = Config {
            protected: vec!["main".to_string(), "release/*".to_string()],
            remotes: None,
            worktrunk: None,
        };

        let merged = find_merged_local(&git, &config).unwrap();

        // feature/done was merged, so it should appear
        assert!(merged.contains(&"feature/done".to_string()));
        // feature/wip was NOT merged
        assert!(!merged.contains(&"feature/wip".to_string()));
        // main is protected
        assert!(!merged.contains(&"main".to_string()));
        // release/1.0 matches the release/* pattern
        assert!(!merged.contains(&"release/1.0".to_string()));
    }

    #[test]
    fn test_find_merged_local_excludes_current_branch() {
        let (_dir, git) = init_repo_with_branches();
        let config = Config {
            protected: vec!["main".to_string()],
            remotes: None,
            worktrunk: None,
        };

        let current = git.current_branch().unwrap();
        let merged = find_merged_local(&git, &config).unwrap();
        assert!(!merged.contains(&current));
    }

    #[test]
    fn test_find_merged_local_detects_cherry_picked_branches() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path();

        StdCommand::new("git")
            .args(["init", "--initial-branch=main"])
            .current_dir(path)
            .output()
            .unwrap();
        StdCommand::new("git")
            .args(["config", "user.email", "test@test.com"])
            .current_dir(path)
            .output()
            .unwrap();
        StdCommand::new("git")
            .args(["config", "user.name", "Test"])
            .current_dir(path)
            .output()
            .unwrap();

        std::fs::write(path.join("README.md"), "# test").unwrap();
        StdCommand::new("git")
            .args(["add", "."])
            .current_dir(path)
            .output()
            .unwrap();
        StdCommand::new("git")
            .args(["commit", "-m", "init"])
            .current_dir(path)
            .output()
            .unwrap();

        // Create a feature branch with a commit
        StdCommand::new("git")
            .args(["checkout", "-b", "feature/cherry"])
            .current_dir(path)
            .output()
            .unwrap();
        std::fs::write(path.join("cherry.txt"), "cherry").unwrap();
        StdCommand::new("git")
            .args(["add", "."])
            .current_dir(path)
            .output()
            .unwrap();
        StdCommand::new("git")
            .args(["commit", "-m", "cherry feature"])
            .current_dir(path)
            .output()
            .unwrap();

        // Cherry-pick onto main (simulating a rebase merge)
        let log_output = StdCommand::new("git")
            .args(["rev-parse", "HEAD"])
            .current_dir(path)
            .output()
            .unwrap();
        let commit_sha = String::from_utf8_lossy(&log_output.stdout)
            .trim()
            .to_string();

        StdCommand::new("git")
            .args(["checkout", "main"])
            .current_dir(path)
            .output()
            .unwrap();

        // Add a diverging commit on main so cherry-pick creates a distinct commit
        std::fs::write(path.join("diverge.txt"), "diverge").unwrap();
        StdCommand::new("git")
            .args(["add", "."])
            .current_dir(path)
            .output()
            .unwrap();
        StdCommand::new("git")
            .args(["commit", "-m", "diverge"])
            .current_dir(path)
            .output()
            .unwrap();

        StdCommand::new("git")
            .args(["cherry-pick", &commit_sha])
            .current_dir(path)
            .output()
            .unwrap();

        let git = Git::with_workdir(false, path);

        // Cherry-picked branch should always be detected as merged
        let config = Config {
            protected: vec!["main".to_string()],
            remotes: None,
            worktrunk: None,
        };
        let merged = find_merged_local(&git, &config).unwrap();
        assert!(merged.contains(&"feature/cherry".to_string()));
    }

    #[test]
    fn test_resolve_merge_targets_with_globs() {
        let (_dir, git) = init_repo_with_branches();
        let config = Config {
            protected: vec!["main".to_string(), "release/*".to_string()],
            remotes: None,
            worktrunk: None,
        };

        let targets = resolve_merge_targets(&git, &config).unwrap();
        assert!(targets.contains(&"main".to_string()));
        assert!(targets.contains(&"release/1.0".to_string()));
        assert!(!targets.contains(&"feature/done".to_string()));
        assert!(!targets.contains(&"feature/wip".to_string()));
    }

    #[test]
    fn test_find_merged_local_no_targets() {
        let (_dir, git) = init_repo_with_branches();
        // Use a pattern that matches nothing
        let config = Config {
            protected: vec!["nonexistent-branch".to_string()],
            remotes: None,
            worktrunk: None,
        };

        let merged = find_merged_local(&git, &config).unwrap();
        assert!(merged.is_empty());
    }

    #[test]
    fn test_find_merged_local_respects_branch_protected() {
        let (_dir, git) = init_repo_with_branches();
        let config = Config {
            protected: vec!["main".to_string()],
            remotes: None,
            worktrunk: None,
        };

        // Without per-branch protection, feature/done should be a candidate
        let merged = find_merged_local(&git, &config).unwrap();
        assert!(merged.contains(&"feature/done".to_string()));

        // Mark feature/done as per-branch protected
        git.set_branch_protected("feature/done", true).unwrap();
        let merged = find_merged_local(&git, &config).unwrap();
        assert!(
            !merged.contains(&"feature/done".to_string()),
            "per-branch protected branch should not be a deletion candidate"
        );

        // Clean up
        git.set_branch_protected("feature/done", false).unwrap();
    }

    #[test]
    fn test_branch_protected_serves_as_merge_target() {
        let (_dir, git) = init_repo_with_branches();
        // Only use per-branch protection on "main" (no global patterns match anything)
        let config = Config {
            protected: vec!["nonexistent-branch".to_string()],
            remotes: None,
            worktrunk: None,
        };

        // Without any real protected branches, nothing is a merge target
        let merged = find_merged_local(&git, &config).unwrap();
        assert!(merged.is_empty());

        // Mark "main" as per-branch protected — it should now be a merge target
        git.set_branch_protected("main", true).unwrap();
        let merged = find_merged_local(&git, &config).unwrap();
        assert!(
            merged.contains(&"feature/done".to_string()),
            "branches merged into a per-branch protected branch should be candidates"
        );

        // Clean up
        git.set_branch_protected("main", false).unwrap();
    }
}