omnifuse-git 0.2.0

Git backend for OmniFuse
Documentation
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! Integration tests for GitBackend.

#![allow(clippy::expect_used)]

use std::path::{Path, PathBuf};

use omnifuse_core::{Backend, PathProtection, RemoteApplyMode, RemoteRefresh, RemoteRefreshResult};
use omnifuse_git::{GitBackend, GitConfig};

/// Timeout for async tests (30s — git operations can be slow).
const TEST_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);

/// Create a bare repo + clone for backend tests.
async fn create_bare_and_clone() -> (tempfile::TempDir, std::path::PathBuf, std::path::PathBuf) {
  let tmp = tempfile::tempdir().expect("tempdir");
  let base = tmp.path().to_path_buf();

  let bare_path = base.join("bare.git");
  let clone_path = base.join("clone");

  // Create bare repo
  tokio::process::Command::new("git")
    .args(["init", "--bare", "-b", "main"])
    .arg(&bare_path)
    .output()
    .await
    .expect("git init --bare");

  // Clone
  tokio::process::Command::new("git")
    .args(["clone"])
    .arg(&bare_path)
    .arg(&clone_path)
    .output()
    .await
    .expect("clone");

  // Configure
  tokio::process::Command::new("git")
    .current_dir(&clone_path)
    .args(["config", "user.email", "test@test.com"])
    .output()
    .await
    .expect("config");
  tokio::process::Command::new("git")
    .current_dir(&clone_path)
    .args(["config", "user.name", "Test"])
    .output()
    .await
    .expect("config");

  // Initial commit
  tokio::fs::write(clone_path.join("README.md"), "# Test")
    .await
    .expect("write");
  tokio::process::Command::new("git")
    .current_dir(&clone_path)
    .args(["add", "."])
    .output()
    .await
    .expect("add");
  tokio::process::Command::new("git")
    .current_dir(&clone_path)
    .args(["commit", "-m", "initial"])
    .output()
    .await
    .expect("commit");
  tokio::process::Command::new("git")
    .current_dir(&clone_path)
    .args(["push", "-u", "origin", "main"])
    .output()
    .await
    .expect("push");

  (tmp, bare_path, clone_path)
}

struct StaticPathProtection {
  paths: Vec<PathBuf>
}

impl StaticPathProtection {
  fn new(paths: Vec<PathBuf>) -> Self {
    Self { paths }
  }
}

impl PathProtection for StaticPathProtection {
  fn is_protected(&self, path: &Path) -> bool {
    self
      .paths
      .iter()
      .any(|protected| path == protected || path.starts_with(protected))
  }
}

async fn create_clone(bare_path: &Path, clone_path: &Path) {
  tokio::process::Command::new("git")
    .args(["clone"])
    .arg(bare_path)
    .arg(clone_path)
    .output()
    .await
    .expect("clone");

  tokio::process::Command::new("git")
    .current_dir(clone_path)
    .args(["config", "user.email", "other@test.com"])
    .output()
    .await
    .expect("config email");
  tokio::process::Command::new("git")
    .current_dir(clone_path)
    .args(["config", "user.name", "Other"])
    .output()
    .await
    .expect("config name");
}

async fn create_remote_commit(repo_path: &Path, file: &str, content: &str) {
  tokio::fs::write(repo_path.join(file), content).await.expect("write");
  tokio::process::Command::new("git")
    .current_dir(repo_path)
    .args(["add", file])
    .output()
    .await
    .expect("add");
  tokio::process::Command::new("git")
    .current_dir(repo_path)
    .args(["commit", "-m", "remote change"])
    .output()
    .await
    .expect("commit");
  tokio::process::Command::new("git")
    .current_dir(repo_path)
    .args(["push"])
    .output()
    .await
    .expect("push");
}

#[tokio::test]
async fn test_should_track_hides_git() {
  eprintln!("[TEST] test_should_track_hides_git");
  let config = GitConfig {
    source: "/nonexistent".to_string(),
    ..GitConfig::default()
  };
  let backend = GitBackend::new(config);

  // Before init, filter is not initialized, but .git is still hidden
  assert!(!backend.should_track(Path::new(".git")), ".git should be hidden");
  assert!(
    !backend.should_track(Path::new(".git/config")),
    ".git/config should be hidden"
  );
  assert!(
    !backend.should_track(Path::new("subdir/.git/HEAD")),
    "nested .git should be hidden"
  );
}

#[tokio::test]
async fn test_should_track_normal() {
  eprintln!("[TEST] test_should_track_normal");
  let config = GitConfig {
    source: "/nonexistent".to_string(),
    ..GitConfig::default()
  };
  let backend = GitBackend::new(config);

  // Before init(), filter is not initialized — regular files should be visible
  assert!(
    backend.should_track(Path::new("README.md")),
    "README.md should be visible"
  );
  assert!(
    backend.should_track(Path::new("src/main.rs")),
    "src/main.rs should be visible"
  );
}

#[tokio::test]
async fn test_should_track_gitignore() {
  eprintln!("[TEST] test_should_track_gitignore");
  let (_tmp, _bare, clone_path) = create_bare_and_clone().await;

  // Create .gitignore
  tokio::fs::write(clone_path.join(".gitignore"), "*.log\ntarget/\n")
    .await
    .expect("write gitignore");

  let config = GitConfig {
    source: clone_path.display().to_string(),
    ..GitConfig::default()
  };
  let backend = GitBackend::new(config);

  // init() initializes the filter
  let local_dir = clone_path.join(".vfs");
  tokio::fs::create_dir_all(&local_dir).await.expect("mkdir");
  let _result = backend.init(&local_dir).await.expect("init");

  // After init, filter should work
  assert!(
    !backend.should_track(&clone_path.join("test.log")),
    "*.log should be ignored after init"
  );
  assert!(
    backend.should_track(&clone_path.join("src/main.rs")),
    "main.rs should not be ignored"
  );
}

#[tokio::test]
async fn test_init_local_repo() {
  eprintln!("[TEST] test_init_local_repo");
  let (_tmp, _bare, clone_path) = create_bare_and_clone().await;

  let config = GitConfig {
    source: clone_path.display().to_string(),
    ..GitConfig::default()
  };
  let backend = GitBackend::new(config);

  let local_dir = clone_path.join(".vfs");
  tokio::fs::create_dir_all(&local_dir).await.expect("mkdir");

  let result = backend.init(&local_dir).await.expect("init");
  assert!(
    matches!(
      result,
      omnifuse_core::InitResult::UpToDate | omnifuse_core::InitResult::Updated
    ),
    "init local repo: {result:?}"
  );
}

#[tokio::test]
async fn remote_git_init_uses_configured_local_dir() {
  eprintln!("[TEST] remote_git_init_uses_configured_local_dir");
  let (_tmp, bare_path, _clone_path) = create_bare_and_clone().await;
  let work = tempfile::tempdir().expect("work dir");
  let configured = work.path().join("configured");

  let backend = GitBackend::new(GitConfig {
    source: format!("file://{}", bare_path.display()),
    branch: "main".to_string(),
    max_push_retries: 1,
    poll_interval_secs: 30,
    local_dir: configured.clone()
  });

  backend.init(&configured).await.expect("init");

  assert!(configured.join(".git").exists());
}

#[tokio::test]
async fn test_sync_commits_pushes() {
  eprintln!("[TEST] test_sync_commits_pushes");
  tokio::time::timeout(TEST_TIMEOUT, async {
    let (_tmp, _bare, clone_path) = create_bare_and_clone().await;

    let config = GitConfig {
      source: clone_path.display().to_string(),
      ..GitConfig::default()
    };
    let backend = GitBackend::new(config);

    let local_dir = clone_path.join(".vfs");
    tokio::fs::create_dir_all(&local_dir).await.expect("mkdir");
    backend.init(&local_dir).await.expect("init");

    // Create a file and sync
    let new_file = clone_path.join("synced.txt");
    tokio::fs::write(&new_file, "sync data").await.expect("write");

    let result = backend.sync(&[new_file]).await.expect("sync");
    assert!(
      matches!(result, omnifuse_core::SyncResult::Success { .. }),
      "sync should be Success: {result:?}"
    );

    // Verify the commit was created
    let output = tokio::process::Command::new("git")
      .current_dir(&clone_path)
      .args(["log", "-1", "--format=%s"])
      .output()
      .await
      .expect("git log");
    let message = String::from_utf8_lossy(&output.stdout);
    assert!(message.contains("[auto]"), "commit should contain [auto]: {message}");
  })
  .await
  .expect("test timed out — possible deadlock");
}

/// Creating a symlink in a repository: git tracks it.
#[cfg(unix)]
#[tokio::test]
async fn test_symlink_in_repo() {
  eprintln!("[TEST] test_symlink_in_repo");
  let (_tmp, _bare, clone_path) = create_bare_and_clone().await;

  // Create the target file
  tokio::fs::write(clone_path.join("target.txt"), "symlink target")
    .await
    .expect("write target");

  // Create a symlink
  tokio::fs::symlink(clone_path.join("target.txt"), clone_path.join("link.txt"))
    .await
    .expect("create symlink");

  // git add the symlink
  tokio::process::Command::new("git")
    .current_dir(&clone_path)
    .args(["add", "link.txt", "target.txt"])
    .output()
    .await
    .expect("git add");

  // Verify git tracks the symlink
  let output = tokio::process::Command::new("git")
    .current_dir(&clone_path)
    .args(["status", "--porcelain"])
    .output()
    .await
    .expect("git status");
  let status = String::from_utf8_lossy(&output.stdout);
  assert!(status.contains("link.txt"), "symlink should be in git status: {status}");
}

/// Creating a file in repo — git status --porcelain shows untracked.
#[tokio::test]
async fn test_git_status_after_write() {
  eprintln!("[TEST] test_git_status_after_write");
  let (_tmp, _bare, clone_path) = create_bare_and_clone().await;

  // Create a new file (not added to git)
  tokio::fs::write(clone_path.join("untracked.txt"), "new file")
    .await
    .expect("write");

  // Verify git status --porcelain
  let output = tokio::process::Command::new("git")
    .current_dir(&clone_path)
    .args(["status", "--porcelain"])
    .output()
    .await
    .expect("git status");
  let status = String::from_utf8_lossy(&output.stdout);
  assert!(
    status.contains("?? untracked.txt"),
    "untracked file should have '??' in git status: {status}"
  );
}

/// .gitignore with *.log pattern: should_track("debug.log") = false,
/// should_track("readme.md") = true.
#[tokio::test]
async fn test_should_track_gitignore_patterns() {
  eprintln!("[TEST] test_should_track_gitignore_patterns");
  let (_tmp, _bare, clone_path) = create_bare_and_clone().await;

  // Create .gitignore with *.log pattern
  tokio::fs::write(clone_path.join(".gitignore"), "*.log\n")
    .await
    .expect("write gitignore");

  let config = GitConfig {
    source: clone_path.display().to_string(),
    ..GitConfig::default()
  };
  let backend = GitBackend::new(config);

  // init() initializes the filter
  let local_dir = clone_path.join(".vfs");
  tokio::fs::create_dir_all(&local_dir).await.expect("mkdir");
  let _result = backend.init(&local_dir).await.expect("init");

  // *.log should be ignored
  assert!(
    !backend.should_track(&clone_path.join("debug.log")),
    "debug.log should be ignored (pattern *.log)"
  );

  // readme.md does not match *.log
  assert!(
    backend.should_track(&clone_path.join("readme.md")),
    "readme.md should not be ignored"
  );
}

/// refresh_remote after a remote commit: applies safe changes.
#[tokio::test]
async fn test_refresh_remote_applies_remote_commit() {
  eprintln!("[TEST] test_refresh_remote_applies_remote_commit");
  tokio::time::timeout(TEST_TIMEOUT, async {
    let (_tmp, bare_path, clone_path) = create_bare_and_clone().await;
    let other_clone = bare_path.parent().expect("parent").join("other_clone");
    create_clone(&bare_path, &other_clone).await;

    let config = GitConfig {
      source: clone_path.display().to_string(),
      local_dir: clone_path.clone(),
      ..GitConfig::default()
    };
    let backend = GitBackend::new(config);
    backend.init(&clone_path).await.expect("init");

    create_remote_commit(&other_clone, "remote_change.md", "# Remote change").await;

    let protected = StaticPathProtection::new(Vec::new());
    let result = backend
      .refresh_remote(RemoteRefresh {
        protected_paths: &protected,
        mode: RemoteApplyMode::ApplySafe
      })
      .await
      .expect("refresh");

    assert!(matches!(result, RemoteRefreshResult::Applied { .. }));
    assert_eq!(
      tokio::fs::read_to_string(clone_path.join("remote_change.md"))
        .await
        .expect("read"),
      "# Remote change"
    );
  })
  .await
  .expect("test timed out");
}

#[tokio::test]
async fn git_refresh_defers_when_remote_change_hits_protected_path() {
  eprintln!("[TEST] git_refresh_defers_when_remote_change_hits_protected_path");
  tokio::time::timeout(TEST_TIMEOUT, async {
    let (_tmp, bare_path, clone_path) = create_bare_and_clone().await;
    let other_clone = bare_path.parent().expect("parent").join("other_clone");
    create_clone(&bare_path, &other_clone).await;

    let backend = GitBackend::new(GitConfig {
      source: clone_path.display().to_string(),
      local_dir: clone_path.clone(),
      ..GitConfig::default()
    });
    backend.init(&clone_path).await.expect("init");

    create_remote_commit(&other_clone, "shared.txt", "remote").await;

    let protected = StaticPathProtection::new(vec![clone_path.join("shared.txt")]);
    let result = backend
      .refresh_remote(RemoteRefresh {
        protected_paths: &protected,
        mode: RemoteApplyMode::ApplySafe
      })
      .await
      .expect("refresh");

    assert!(matches!(result, RemoteRefreshResult::Deferred { .. }));
    assert!(
      !clone_path.join("shared.txt").exists(),
      "protected remote file should not be applied"
    );
  })
  .await
  .expect("test timed out");
}

/// is_online() returns true for a reachable local repo.
#[tokio::test]
async fn test_is_online_local_repo() {
  eprintln!("[TEST] test_is_online_local_repo");
  let (_tmp, _bare, clone_path) = create_bare_and_clone().await;

  let config = GitConfig {
    source: clone_path.display().to_string(),
    ..GitConfig::default()
  };
  let backend = GitBackend::new(config);
  let local_dir = clone_path.join(".vfs");
  tokio::fs::create_dir_all(&local_dir).await.expect("mkdir");
  backend.init(&local_dir).await.expect("init");

  // Local bare repo is always "online"
  let online = backend.is_online().await;
  assert!(online, "is_online() should return true for local repo",);
}