| 13 | } |
| 14 | |
| 15 | fn populate(&self, conn: &Connection, repo: &mut GitRepo) -> Result<()> { |
| 16 | let mut stmt = conn.prepare( |
| 17 | r#" |
| 18 | INSERT INTO worktrees ( |
| 19 | name, path, head_id, branch, is_bare, is_detached, |
| 20 | is_locked, lock_reason, is_prunable, repo |
| 21 | ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10) |
| 22 | "#, |
| 23 | )?; |
| 24 | |
| 25 | let repo_path = repo.path().to_string(); |
| 26 | let git_repo = repo.inner(); |
| 27 | |
| 28 | // Add main worktree |
| 29 | let main_path = git_repo |
| 30 | .workdir() |
| 31 | .map(|p| p.to_string_lossy().to_string()) |
| 32 | .unwrap_or_else(|| git_repo.path().to_string_lossy().to_string()); |
| 33 | |
| 34 | let head_id = git_repo |
| 35 | .head() |
| 36 | .ok() |
| 37 | .and_then(|h| h.target()) |
| 38 | .map(|oid| oid.to_string()); |
| 39 | let branch = git_repo.head().ok().and_then(|h| { |
| 40 | if h.is_branch() { |
| 41 | h.shorthand().map(|s| s.to_string()) |
| 42 | } else { |
| 43 | None |
| 44 | } |
| 45 | }); |
| 46 | let is_bare = git_repo.is_bare(); |
| 47 | let is_detached = git_repo.head_detached().unwrap_or(false); |
| 48 | |
| 49 | stmt.execute(( |
| 50 | "main", |
| 51 | &main_path, |
| 52 | &head_id, |
| 53 | &branch, |
| 54 | if is_bare { 1 } else { 0 }, |
| 55 | if is_detached { 1 } else { 0 }, |
| 56 | 0, // not locked |
| 57 | Option::<String>::None, |
| 58 | 0, // not prunable |
| 59 | &repo_path, |
| 60 | ))?; |
| 61 | |
| 62 | // Check for linked worktrees in .git/worktrees |
| 63 | let worktrees_dir = git_repo.path().join("worktrees"); |
| 64 | if worktrees_dir.exists() && worktrees_dir.is_dir() { |
| 65 | if let Ok(entries) = fs::read_dir(&worktrees_dir) { |
| 66 | for entry in entries.flatten() { |
| 67 | let wt_name = entry.file_name().to_string_lossy().to_string(); |
| 68 | let wt_path = entry.path(); |
| 69 | |
| 70 | // Read gitdir file to get actual worktree path |
| 71 | let gitdir_file = wt_path.join("gitdir"); |
| 72 | let actual_path = if gitdir_file.exists() { |