Skip to content

Commit c576f68

Browse files
committed
refactor(context): Abstract lookup for offline flag
1 parent 615a6f8 commit c576f68

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

src/cargo/sources/git/utils.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -955,15 +955,12 @@ pub fn fetch(
955955
gctx: &GlobalContext,
956956
remote_kind: RemoteKind,
957957
) -> CargoResult<()> {
958-
if gctx.frozen() {
958+
if let Some(offline_flag) = gctx.offline_flag() {
959959
anyhow::bail!(
960-
"attempting to update a git repository, but --frozen \
960+
"attempting to update a git repository, but {offline_flag} \
961961
was specified"
962962
)
963963
}
964-
if !gctx.network_allowed() {
965-
anyhow::bail!("can't update a git repository in the offline mode")
966-
}
967964

968965
let shallow = remote_kind.to_shallow_setting(repo.is_shallow(), gctx);
969966

src/cargo/util/context/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,17 @@ impl GlobalContext {
11611161
}
11621162

11631163
pub fn network_allowed(&self) -> bool {
1164-
!self.frozen() && !self.offline()
1164+
!self.offline_flag().is_some()
1165+
}
1166+
1167+
pub fn offline_flag(&self) -> Option<&'static str> {
1168+
if self.frozen {
1169+
Some("--frozen")
1170+
} else if self.offline {
1171+
Some("--offline")
1172+
} else {
1173+
None
1174+
}
11651175
}
11661176

11671177
pub fn offline(&self) -> bool {

src/cargo/util/network/http.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,9 @@ pub fn http_handle(gctx: &GlobalContext) -> CargoResult<Easy> {
2525
}
2626

2727
pub fn http_handle_and_timeout(gctx: &GlobalContext) -> CargoResult<(Easy, HttpTimeout)> {
28-
if gctx.frozen() {
28+
if let Some(offline_flag) = gctx.offline_flag() {
2929
bail!(
30-
"attempting to make an HTTP request, but --frozen was \
31-
specified"
32-
)
33-
}
34-
if gctx.offline() {
35-
bail!(
36-
"attempting to make an HTTP request, but --offline was \
30+
"attempting to make an HTTP request, but {offline_flag} was \
3731
specified"
3832
)
3933
}

0 commit comments

Comments
 (0)