Skip to content

Commit 65524ed

Browse files
author
Stephan Dilly
committed
allow walking repo including all available refs
1 parent 070ad1b commit 65524ed

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

asyncgit/src/sync/branch/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ pub fn delete_branch(
325325
}
326326

327327
/// creates a new branch pointing to current HEAD commit and updating HEAD to new branch
328-
pub fn create_branch(repo_path: &str, name: &str) -> Result<()> {
328+
pub fn create_branch(repo_path: &str, name: &str) -> Result<String> {
329329
scope_time!("create_branch");
330330

331331
let repo = utils::repo(repo_path)?;
@@ -338,7 +338,7 @@ pub fn create_branch(repo_path: &str, name: &str) -> Result<()> {
338338
let branch_ref_name = bytes2string(branch_ref.name_bytes())?;
339339
repo.set_head(branch_ref_name.as_str())?;
340340

341-
Ok(())
341+
Ok(branch_ref_name)
342342
}
343343

344344
#[cfg(test)]

asyncgit/src/sync/logwalker.rs

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ use super::CommitId;
22
use crate::error::Result;
33
use git2::{Repository, Revwalk, Sort};
44

5+
///
6+
pub enum Mode {
7+
HeadOnly,
8+
AllRefs,
9+
}
10+
511
///
612
pub struct LogWalker<'a> {
713
repo: &'a Repository,
814
revwalk: Option<Revwalk<'a>>,
15+
mode: Mode,
916
}
1017

1118
impl<'a> LogWalker<'a> {
@@ -14,9 +21,17 @@ impl<'a> LogWalker<'a> {
1421
Self {
1522
repo,
1623
revwalk: None,
24+
mode: Mode::HeadOnly,
1725
}
1826
}
1927

28+
///
29+
pub const fn mode(self, mode: Mode) -> Self {
30+
let mut res = self;
31+
res.mode = mode;
32+
res
33+
}
34+
2035
///
2136
pub fn read(
2237
&mut self,
@@ -28,7 +43,12 @@ impl<'a> LogWalker<'a> {
2843
if self.revwalk.is_none() {
2944
let mut walk = self.repo.revwalk()?;
3045

31-
walk.push_head()?;
46+
if matches!(self.mode, Mode::HeadOnly) {
47+
walk.push_head()?;
48+
} else {
49+
walk.push_glob("*")?;
50+
}
51+
3252
walk.set_sorting(Sort::TIME)?;
3353

3454
self.revwalk = Some(walk);
@@ -53,9 +73,12 @@ impl<'a> LogWalker<'a> {
5373
mod tests {
5474
use super::*;
5575
use crate::sync::{
56-
commit, get_commits_info, stage_add_file,
57-
tests::repo_init_empty,
76+
checkout_branch, commit, create_branch, get_commits_info,
77+
stage_add_file,
78+
tests::{repo_init_empty, write_commit_file_at},
5879
};
80+
use git2::Time;
81+
use pretty_assertions::assert_eq;
5982
use std::{fs::File, io::Write, path::Path};
6083

6184
#[test]
@@ -113,4 +136,60 @@ mod tests {
113136

114137
Ok(())
115138
}
139+
140+
fn walk_all_commits(repo: &Repository) -> Vec<CommitId> {
141+
let mut items = Vec::new();
142+
let mut walk = LogWalker::new(&repo).mode(Mode::AllRefs);
143+
walk.read(&mut items, 10).unwrap();
144+
items
145+
}
146+
147+
#[test]
148+
fn test_multiple_branches() {
149+
let (td, repo) = repo_init_empty().unwrap();
150+
let repo_path = td.path().to_string_lossy();
151+
152+
let c1 = write_commit_file_at(
153+
&repo,
154+
"test.txt",
155+
"",
156+
"c1",
157+
Time::new(1, 0),
158+
);
159+
160+
let items = walk_all_commits(&repo);
161+
162+
assert_eq!(items, vec![c1]);
163+
164+
let b1 = create_branch(&repo_path, "b1").unwrap();
165+
166+
let c2 = write_commit_file_at(
167+
&repo,
168+
"test2.txt",
169+
"",
170+
"c2",
171+
Time::new(2, 0),
172+
);
173+
174+
let items = walk_all_commits(&repo);
175+
assert_eq!(items, vec![c2, c1]);
176+
177+
let _b2 = create_branch(&repo_path, "b2").unwrap();
178+
179+
let c3 = write_commit_file_at(
180+
&repo,
181+
"test3.txt",
182+
"",
183+
"c3",
184+
Time::new(3, 0),
185+
);
186+
187+
let items = walk_all_commits(&repo);
188+
assert_eq!(items, vec![c3, c2, c1]);
189+
190+
checkout_branch(&repo_path, &b1).unwrap();
191+
192+
let items = walk_all_commits(&repo);
193+
assert_eq!(items, vec![c3, c2, c1]);
194+
}
116195
}

0 commit comments

Comments
 (0)