Skip to content

Commit f46e8ff

Browse files
authored
Use EUSER instead of generic error to stop foreach (#699)
Previously foreach returned a generic generic error to signal the user asked to stop iterating. If a user got that error, they can't know for certain if it's because they asked to stop iterating, or if a legitimate error occurred. libgit2 provides EUSER as a reserved error code for this case, so we use it. Closes 698
1 parent 3b4d35d commit f46e8ff

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

src/diff.rs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,9 @@ pub extern "C" fn print_cb(
338338
(*data)(delta, hunk, line)
339339
});
340340
if r == Some(true) {
341-
0
341+
raw::GIT_OK
342342
} else {
343-
-1
343+
raw::GIT_EUSER
344344
}
345345
}
346346
}
@@ -361,9 +361,9 @@ pub extern "C" fn file_cb_c(
361361
}
362362
});
363363
if r == Some(true) {
364-
0
364+
raw::GIT_OK
365365
} else {
366-
-1
366+
raw::GIT_EUSER
367367
}
368368
}
369369
}
@@ -385,9 +385,9 @@ pub extern "C" fn binary_cb_c(
385385
}
386386
});
387387
if r == Some(true) {
388-
0
388+
raw::GIT_OK
389389
} else {
390-
-1
390+
raw::GIT_EUSER
391391
}
392392
}
393393
}
@@ -409,9 +409,9 @@ pub extern "C" fn hunk_cb_c(
409409
}
410410
});
411411
if r == Some(true) {
412-
0
412+
raw::GIT_OK
413413
} else {
414-
-1
414+
raw::GIT_EUSER
415415
}
416416
}
417417
}
@@ -435,9 +435,9 @@ pub extern "C" fn line_cb_c(
435435
}
436436
});
437437
if r == Some(true) {
438-
0
438+
raw::GIT_OK
439439
} else {
440-
-1
440+
raw::GIT_EUSER
441441
}
442442
}
443443
}
@@ -1811,4 +1811,34 @@ mod tests {
18111811
assert_eq!(origin_values.len(), 1);
18121812
assert_eq!(origin_values[0], DiffLineType::Addition);
18131813
}
1814+
1815+
#[test]
1816+
fn foreach_exits_with_euser() {
1817+
let foo_path = Path::new("foo");
1818+
let bar_path = Path::new("foo");
1819+
1820+
let (td, repo) = crate::test::repo_init();
1821+
t!(t!(File::create(&td.path().join(foo_path))).write_all(b"bar\n"));
1822+
1823+
let mut index = t!(repo.index());
1824+
t!(index.add_path(foo_path));
1825+
t!(index.add_path(bar_path));
1826+
1827+
let mut opts = DiffOptions::new();
1828+
opts.include_untracked(true);
1829+
let diff = t!(repo.diff_tree_to_index(None, Some(&index), Some(&mut opts)));
1830+
1831+
let mut calls = 0;
1832+
let result = diff.foreach(
1833+
&mut |_file, _progress| {
1834+
calls += 1;
1835+
false
1836+
},
1837+
None,
1838+
None,
1839+
None,
1840+
);
1841+
1842+
assert_eq!(result.unwrap_err().code(), crate::ErrorCode::User);
1843+
}
18141844
}

0 commit comments

Comments
 (0)