rustic-git 0.6.0

A Rustic Git - clean type-safe API over git cli
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
use crate::types::Hash;
use crate::utils::git;
use crate::{Repository, Result};
use std::fmt;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BranchType {
    Local,
    RemoteTracking,
}

impl fmt::Display for BranchType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            BranchType::Local => write!(f, "local"),
            BranchType::RemoteTracking => write!(f, "remote-tracking"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Branch {
    pub name: String,
    pub branch_type: BranchType,
    pub is_current: bool,
    pub commit_hash: Hash,
    pub upstream: Option<String>,
}

impl Branch {
    /// Check if this is a local branch
    pub fn is_local(&self) -> bool {
        matches!(self.branch_type, BranchType::Local)
    }

    /// Check if this is a remote-tracking branch
    pub fn is_remote(&self) -> bool {
        matches!(self.branch_type, BranchType::RemoteTracking)
    }

    /// Get the short name of the branch (without remote prefix for remote branches)
    pub fn short_name(&self) -> &str {
        if self.is_remote() && self.name.contains('/') {
            self.name.split('/').nth(1).unwrap_or(&self.name)
        } else {
            &self.name
        }
    }
}

impl fmt::Display for Branch {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let marker = if self.is_current { "*" } else { " " };
        write!(f, "{} {}", marker, self.name)
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct BranchList {
    branches: Box<[Branch]>,
}

impl BranchList {
    /// Create a new BranchList from a vector of branches
    pub fn new(branches: Vec<Branch>) -> Self {
        Self {
            branches: branches.into_boxed_slice(),
        }
    }

    /// Get all branches
    pub fn all(&self) -> &[Branch] {
        &self.branches
    }

    /// Get an iterator over all branches
    pub fn iter(&self) -> impl Iterator<Item = &Branch> {
        self.branches.iter()
    }

    /// Get an iterator over local branches
    pub fn local(&self) -> impl Iterator<Item = &Branch> {
        self.branches.iter().filter(|b| b.is_local())
    }

    /// Get an iterator over remote-tracking branches
    pub fn remote(&self) -> impl Iterator<Item = &Branch> {
        self.branches.iter().filter(|b| b.is_remote())
    }

    /// Get the current branch
    pub fn current(&self) -> Option<&Branch> {
        self.branches.iter().find(|b| b.is_current)
    }

    /// Find a branch by name
    pub fn find(&self, name: &str) -> Option<&Branch> {
        self.branches.iter().find(|b| b.name == name)
    }

    /// Find a branch by short name (useful for remote branches)
    pub fn find_by_short_name(&self, short_name: &str) -> Option<&Branch> {
        self.branches.iter().find(|b| b.short_name() == short_name)
    }

    /// Check if the list is empty
    pub fn is_empty(&self) -> bool {
        self.branches.is_empty()
    }

    /// Get the count of branches
    pub fn len(&self) -> usize {
        self.branches.len()
    }

    /// Get count of local branches
    pub fn local_count(&self) -> usize {
        self.local().count()
    }

    /// Get count of remote-tracking branches
    pub fn remote_count(&self) -> usize {
        self.remote().count()
    }
}

impl fmt::Display for BranchList {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for branch in &self.branches {
            writeln!(f, "{}", branch)?;
        }
        Ok(())
    }
}

impl Repository {
    /// List all branches in the repository
    pub fn branches(&self) -> Result<BranchList> {
        Self::ensure_git()?;

        // Use git branch -vv --all for comprehensive branch information
        let stdout = git(&["branch", "-vv", "--all"], Some(self.repo_path()))?;

        let branches = parse_branch_output(&stdout)?;
        Ok(BranchList::new(branches))
    }

    /// Get the current branch
    pub fn current_branch(&self) -> Result<Option<Branch>> {
        Self::ensure_git()?;

        let stdout = git(&["branch", "--show-current"], Some(self.repo_path()))?;
        let current_name = stdout.trim();

        if current_name.is_empty() {
            // Might be in detached HEAD state
            return Ok(None);
        }

        // Get detailed info about the current branch
        let branches = self.branches()?;
        Ok(branches.current().cloned())
    }

    /// Create a new branch
    pub fn create_branch(&self, name: &str, start_point: Option<&str>) -> Result<Branch> {
        Self::ensure_git()?;

        let mut args = vec!["branch", name];
        if let Some(start) = start_point {
            args.push(start);
        }

        let _stdout = git(&args, Some(self.repo_path()))?;

        // Get information about the newly created branch
        let branches = self.branches()?;
        branches.find(name).cloned().ok_or_else(|| {
            crate::error::GitError::CommandFailed(format!("Failed to create branch: {}", name))
        })
    }

    /// Delete a branch
    pub fn delete_branch(&self, branch: &Branch, force: bool) -> Result<()> {
        Self::ensure_git()?;

        if branch.is_current {
            return Err(crate::error::GitError::CommandFailed(
                "Cannot delete the current branch".to_string(),
            ));
        }

        let flag = if force { "-D" } else { "-d" };
        let args = vec!["branch", flag, &branch.name];

        let _stdout = git(&args, Some(self.repo_path()))?;
        Ok(())
    }

    /// Switch to an existing branch
    pub fn checkout(&self, branch: &Branch) -> Result<()> {
        Self::ensure_git()?;

        let branch_name = if branch.is_remote() {
            branch.short_name()
        } else {
            &branch.name
        };

        let _stdout = git(&["checkout", branch_name], Some(self.repo_path()))?;
        Ok(())
    }

    /// Create a new branch and switch to it
    pub fn checkout_new(&self, name: &str, start_point: Option<&str>) -> Result<Branch> {
        Self::ensure_git()?;

        let mut args = vec!["checkout", "-b", name];
        if let Some(start) = start_point {
            args.push(start);
        }

        let _stdout = git(&args, Some(self.repo_path()))?;

        // Get information about the newly created and checked out branch
        self.current_branch()?.ok_or_else(|| {
            crate::error::GitError::CommandFailed(format!(
                "Failed to create and checkout branch: {}",
                name
            ))
        })
    }
}

/// Parse the output of `git branch -vv --all`
fn parse_branch_output(output: &str) -> Result<Vec<Branch>> {
    let mut branches = Vec::new();

    for line in output.lines() {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }

        // Skip the line that shows HEAD -> branch mapping for remotes
        if line.contains("->") {
            continue;
        }

        let is_current = line.starts_with('*');
        let line = if is_current {
            line[1..].trim() // Skip the '*' and trim
        } else {
            line.trim() // Just trim whitespace for non-current branches
        };

        // Parse branch name (first word)
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.is_empty() {
            continue;
        }

        let name = parts[0].to_string();

        // Determine branch type
        let branch_type = if name.starts_with("remotes/") {
            BranchType::RemoteTracking
        } else {
            BranchType::Local
        };

        // Extract commit hash (second part if available)
        let commit_hash = if parts.len() > 1 {
            Hash::from(parts[1].to_string())
        } else {
            Hash::from("0000000000000000000000000000000000000000".to_string())
        };

        // Extract upstream information (look for [upstream] pattern)
        let upstream = if let Some(bracket_start) = line.find('[') {
            if let Some(bracket_end) = line.find(']') {
                let upstream_info = &line[bracket_start + 1..bracket_end];
                // Extract just the upstream branch name, ignore ahead/behind info
                let upstream_branch = upstream_info
                    .split(':')
                    .next()
                    .unwrap_or(upstream_info)
                    .trim();
                if upstream_branch.is_empty() {
                    None
                } else {
                    Some(upstream_branch.to_string())
                }
            } else {
                None
            }
        } else {
            None
        };

        // Clean up remote branch names
        let clean_name = if branch_type == BranchType::RemoteTracking {
            name.strip_prefix("remotes/").unwrap_or(&name).to_string()
        } else {
            name
        };

        let branch = Branch {
            name: clean_name,
            branch_type,
            is_current,
            commit_hash,
            upstream,
        };

        branches.push(branch);
    }

    Ok(branches)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::path::Path;

    #[test]
    fn test_branch_type_display() {
        assert_eq!(format!("{}", BranchType::Local), "local");
        assert_eq!(format!("{}", BranchType::RemoteTracking), "remote-tracking");
    }

    #[test]
    fn test_branch_is_local() {
        let branch = Branch {
            name: "main".to_string(),
            branch_type: BranchType::Local,
            is_current: true,
            commit_hash: Hash::from("abc123".to_string()),
            upstream: None,
        };

        assert!(branch.is_local());
        assert!(!branch.is_remote());
    }

    #[test]
    fn test_branch_is_remote() {
        let branch = Branch {
            name: "origin/main".to_string(),
            branch_type: BranchType::RemoteTracking,
            is_current: false,
            commit_hash: Hash::from("abc123".to_string()),
            upstream: None,
        };

        assert!(branch.is_remote());
        assert!(!branch.is_local());
    }

    #[test]
    fn test_branch_short_name() {
        let local_branch = Branch {
            name: "feature".to_string(),
            branch_type: BranchType::Local,
            is_current: false,
            commit_hash: Hash::from("abc123".to_string()),
            upstream: None,
        };

        let remote_branch = Branch {
            name: "origin/feature".to_string(),
            branch_type: BranchType::RemoteTracking,
            is_current: false,
            commit_hash: Hash::from("abc123".to_string()),
            upstream: None,
        };

        assert_eq!(local_branch.short_name(), "feature");
        assert_eq!(remote_branch.short_name(), "feature");
    }

    #[test]
    fn test_branch_display() {
        let current_branch = Branch {
            name: "main".to_string(),
            branch_type: BranchType::Local,
            is_current: true,
            commit_hash: Hash::from("abc123".to_string()),
            upstream: None,
        };

        let other_branch = Branch {
            name: "feature".to_string(),
            branch_type: BranchType::Local,
            is_current: false,
            commit_hash: Hash::from("def456".to_string()),
            upstream: None,
        };

        assert_eq!(format!("{}", current_branch), "* main");
        assert_eq!(format!("{}", other_branch), "  feature");
    }

    #[test]
    fn test_branch_list_creation() {
        let branches = vec![
            Branch {
                name: "main".to_string(),
                branch_type: BranchType::Local,
                is_current: true,
                commit_hash: Hash::from("abc123".to_string()),
                upstream: Some("origin/main".to_string()),
            },
            Branch {
                name: "origin/main".to_string(),
                branch_type: BranchType::RemoteTracking,
                is_current: false,
                commit_hash: Hash::from("abc123".to_string()),
                upstream: None,
            },
        ];

        let branch_list = BranchList::new(branches);

        assert_eq!(branch_list.len(), 2);
        assert_eq!(branch_list.local_count(), 1);
        assert_eq!(branch_list.remote_count(), 1);
        assert!(!branch_list.is_empty());
    }

    #[test]
    fn test_branch_list_find() {
        let branches = vec![
            Branch {
                name: "main".to_string(),
                branch_type: BranchType::Local,
                is_current: true,
                commit_hash: Hash::from("abc123".to_string()),
                upstream: None,
            },
            Branch {
                name: "origin/feature".to_string(),
                branch_type: BranchType::RemoteTracking,
                is_current: false,
                commit_hash: Hash::from("def456".to_string()),
                upstream: None,
            },
        ];

        let branch_list = BranchList::new(branches);

        assert!(branch_list.find("main").is_some());
        assert!(branch_list.find("origin/feature").is_some());
        assert!(branch_list.find("nonexistent").is_none());

        assert!(branch_list.find_by_short_name("main").is_some());
        assert!(branch_list.find_by_short_name("feature").is_some());
    }

    #[test]
    fn test_branch_list_current() {
        let branches = vec![
            Branch {
                name: "main".to_string(),
                branch_type: BranchType::Local,
                is_current: true,
                commit_hash: Hash::from("abc123".to_string()),
                upstream: None,
            },
            Branch {
                name: "feature".to_string(),
                branch_type: BranchType::Local,
                is_current: false,
                commit_hash: Hash::from("def456".to_string()),
                upstream: None,
            },
        ];

        let branch_list = BranchList::new(branches);
        let current = branch_list.current().unwrap();

        assert_eq!(current.name, "main");
        assert!(current.is_current);
    }

    #[test]
    fn test_parse_branch_output() {
        let output = r#"
* main                abc1234 [origin/main] Initial commit
  feature             def5678 Feature branch
  remotes/origin/main abc1234 Initial commit
"#;

        let branches = parse_branch_output(output).unwrap();

        assert_eq!(branches.len(), 3);

        // Check main branch
        let main_branch = branches.iter().find(|b| b.name == "main");
        assert!(main_branch.is_some());
        let main_branch = main_branch.unwrap();
        assert!(main_branch.is_current);
        assert_eq!(main_branch.branch_type, BranchType::Local);
        assert_eq!(main_branch.upstream, Some("origin/main".to_string()));

        // Check feature branch
        let feature_branch = branches.iter().find(|b| b.name == "feature").unwrap();
        assert!(!feature_branch.is_current);
        assert_eq!(feature_branch.branch_type, BranchType::Local);
        assert_eq!(feature_branch.upstream, None);

        // Check remote branch
        let remote_branch = branches.iter().find(|b| b.name == "origin/main").unwrap();
        assert!(!remote_branch.is_current);
        assert_eq!(remote_branch.branch_type, BranchType::RemoteTracking);
    }

    #[test]
    fn test_repository_current_branch() {
        let test_path = "/tmp/test_current_branch_repo";

        // Clean up if exists
        if Path::new(test_path).exists() {
            fs::remove_dir_all(test_path).unwrap();
        }

        // Create a repository and test current branch
        let repo = Repository::init(test_path, false).unwrap();

        // In a new repo, there might not be a current branch until first commit
        let _current = repo.current_branch().unwrap();
        // This might be None in a fresh repository with no commits

        // Clean up
        fs::remove_dir_all(test_path).unwrap();
    }

    #[test]
    fn test_repository_create_branch() {
        let test_path = "/tmp/test_create_branch_repo";

        // Clean up if exists
        if Path::new(test_path).exists() {
            fs::remove_dir_all(test_path).unwrap();
        }

        // Create a repository with an initial commit
        let repo = Repository::init(test_path, false).unwrap();

        // Configure git user for this repository to enable commits
        repo.config()
            .set_user("Test User", "test@example.com")
            .unwrap();

        // Create a test file and commit to have a valid HEAD
        std::fs::write(format!("{}/test.txt", test_path), "test content").unwrap();
        repo.add(&["test.txt"]).unwrap();
        repo.commit("Initial commit").unwrap();

        // Create a new branch
        let branch = repo.create_branch("feature", None).unwrap();
        assert_eq!(branch.name, "feature");
        assert_eq!(branch.branch_type, BranchType::Local);
        assert!(!branch.is_current);

        // Verify the branch exists in the branch list
        let branches = repo.branches().unwrap();
        assert!(branches.find("feature").is_some());

        // Clean up
        fs::remove_dir_all(test_path).unwrap();
    }
}