Skip to content

Commit 6975d67

Browse files
committed
all tests pass, time to remove git2
1 parent 1774f94 commit 6975d67

File tree

4 files changed

+52
-21
lines changed

4 files changed

+52
-21
lines changed

src/index/diff/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl Index {
8383
.unwrap_or_else(|| git::hash::ObjectId::empty_tree(repo.object_hash()));
8484
let remote_name = self
8585
.remote_name
86+
.as_deref()
8687
.expect("always set for this old portion of the code");
8788
let to = {
8889
let repo = git2::Repository::open(repo.git_dir())?;
@@ -141,6 +142,7 @@ impl Index {
141142
let to = {
142143
let mut remote = self
143144
.remote_name
145+
.as_deref()
144146
.and_then(|name| {
145147
self.repo.find_remote(name).ok().or_else(|| {
146148
self.repo
@@ -179,7 +181,7 @@ impl Index {
179181
if remote.refspecs(git::remote::Direction::Fetch).is_empty() {
180182
let spec = format!(
181183
"+refs/heads/{branch}:refs/remotes/{remote}/{branch}",
182-
remote = self.remote_name.unwrap_or("origin"),
184+
remote = self.remote_name.as_deref().unwrap_or("origin"),
183185
branch = self.branch_name,
184186
);
185187
remote
@@ -241,6 +243,11 @@ impl Index {
241243

242244
/// Find changes while changing the underlying repository in one way or another.
243245
impl Index {
246+
/// As `fetch_changes_with_options`, but without the options.
247+
pub fn fetch_changes2(&self) -> Result<Vec<Change>, Error> {
248+
self.fetch_changes_with_options2(git::progress::Discard, &AtomicBool::default())
249+
}
250+
244251
/// As `fetch_changes_with_options`, but without the options.
245252
pub fn fetch_changes(&self) -> Result<Vec<Change>, Error> {
246253
self.fetch_changes_with_options(None)

src/index/init.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl Index {
101101
repo.object_cache_size_if_unset(4 * 1024 * 1024);
102102
Ok(Index {
103103
repo,
104-
remote_name: Some("origin"),
104+
remote_name: Some("origin".into()),
105105
branch_name: "master",
106106
seen_ref_name: LAST_SEEN_REFNAME,
107107
})
@@ -148,9 +148,14 @@ impl Index {
148148
.apply_environment();
149149

150150
repo.object_cache_size_if_unset(4 * 1024 * 1024);
151+
let remote_name = repo
152+
.remote_names()
153+
.into_iter()
154+
.next()
155+
.map(ToOwned::to_owned);
151156
Ok(Index {
152157
repo,
153-
remote_name: Some("origin"),
158+
remote_name,
154159
branch_name: "master",
155160
seen_ref_name: LAST_SEEN_REFNAME,
156161
})

src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct Index {
1313
pub branch_name: &'static str,
1414
/// The name of the symbolic name of the remote to fetch from.
1515
/// If `None`, obtain the remote name from the configuration of the currently checked-out branch.
16-
pub remote_name: Option<&'static str>,
16+
pub remote_name: Option<String>,
1717
/// The git repository to use for diffing
1818
pub(crate) repo: git::Repository,
1919
}

tests/index/mod.rs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crates_index_diff::Index;
22
use git_repository as git;
3+
use git_repository::refs::transaction::PreviousValue;
34
use git_testtools::tempfile::TempDir;
45
use std::path::PathBuf;
56
use std::sync::atomic::AtomicBool;
@@ -60,20 +61,19 @@ fn clone_if_needed() {
6061
}
6162

6263
#[test]
63-
#[ignore]
64-
fn changes_since_last_fetch() -> crate::Result {
65-
let (mut index, _tmp) = index_rw()?;
64+
fn changes_since_last_fetch() {
65+
let (mut index, _tmp) = index_rw().unwrap();
6666
let repo = index.repository();
6767
assert!(index.last_seen_reference().is_err(), "no marker exists");
68-
let num_changes_since_first_commit = index.fetch_changes()?.len();
68+
let num_changes_since_first_commit = index.fetch_changes2().unwrap().len();
6969
assert_eq!(
7070
num_changes_since_first_commit, NUM_CHANGES_SINCE_EVER,
7171
"all changes since ever"
7272
);
7373
let mut marker = index
7474
.last_seen_reference()
7575
.expect("must be created/update now");
76-
let remote_main = repo.find_reference("refs/remotes/origin/main")?;
76+
let remote_main = repo.find_reference("refs/remotes/origin/main").unwrap();
7777
assert_eq!(
7878
marker.target(),
7979
remote_main.target(),
@@ -83,15 +83,16 @@ fn changes_since_last_fetch() -> crate::Result {
8383
// reset to previous one
8484
marker
8585
.set_target_id(
86-
repo.rev_parse(format!("{}~1", index.seen_ref_name).as_str())?
86+
repo.rev_parse(format!("{}~1", index.seen_ref_name).as_str())
87+
.unwrap()
8788
.single()
8889
.unwrap(),
8990
"resetting to previous commit",
9091
)
9192
.expect("reset success");
92-
let num_seen_after_reset = index.fetch_changes()?.len();
93+
let num_seen_after_reset = index.fetch_changes2().unwrap().len();
9394
assert_eq!(
94-
index.last_seen_reference()?.target(),
95+
index.last_seen_reference().unwrap().target(),
9596
remote_main.target(),
9697
"seen branch was updated again"
9798
);
@@ -101,26 +102,45 @@ fn changes_since_last_fetch() -> crate::Result {
101102
);
102103

103104
assert_eq!(
104-
index.fetch_changes()?.len(),
105+
index.fetch_changes2().unwrap().len(),
105106
0,
106107
"nothing if there was no change"
107108
);
108109

109110
// now the remote has squashed their history, we should still be able to get the correct changes.
110-
git2::Repository::open(repo.git_dir())?.remote("local", repo.git_dir().to_str().unwrap())?;
111-
index.remote_name = Some("local");
111+
let repo_name = "local";
112+
{
113+
let git_dir = repo.git_dir().to_owned();
114+
let mut config = index.repository_mut().config_snapshot_mut();
115+
// TODO: use `remote.save_as_to()` here, requires a way to get the mutable repo ref again.
116+
config
117+
.set_raw_value("remote", Some(repo_name), "url", git_dir.to_str().unwrap())
118+
.unwrap();
119+
config
120+
.set_raw_value(
121+
"remote",
122+
Some(repo_name),
123+
"fetch",
124+
"+refs/heads/*:refs/remotes/local/*",
125+
)
126+
.unwrap();
127+
}
128+
index.remote_name = Some(repo_name.into());
112129
index
113130
.repository()
114-
.find_reference("refs/heads/main")?
115-
.set_target_id(
131+
.reference(
132+
"refs/heads/main",
116133
index
117134
.repository()
118-
.rev_parse("origin/squashed")?
135+
.rev_parse("origin/squashed")
136+
.unwrap()
119137
.single()
120138
.unwrap(),
139+
PreviousValue::Any,
121140
"adjust to simulate remote with new squashed history",
122-
)?;
123-
let changes = index.fetch_changes()?;
141+
)
142+
.unwrap();
143+
let changes = index.fetch_changes2().unwrap();
124144
assert_eq!(changes.len(), 1);
125145
assert_eq!(
126146
changes
@@ -129,7 +149,6 @@ fn changes_since_last_fetch() -> crate::Result {
129149
Some(("git-repository", "1.0.0")),
130150
"there was just one actual changes compared to the previous state"
131151
);
132-
Ok(())
133152
}
134153

135154
fn index_ro() -> crate::Result<Index> {

0 commit comments

Comments
 (0)