-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlib.rs
1210 lines (1059 loc) · 38.3 KB
/
lib.rs
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![deny(clippy::expect_used, clippy::panic, clippy::unwrap_used)]
use anyhow::{Context, Result, anyhow, bail, ensure};
use cargo_metadata::{
Dependency, DependencyKind, Metadata, MetadataCommand, Package,
semver::{Version, VersionReq},
};
use clap::{Parser, crate_version};
use crates_index::GitIndex;
use home::cargo_home;
use std::{
cell::RefCell,
collections::{HashMap, HashSet},
env::args,
ffi::OsStr,
fs::File,
io::{BufRead, IsTerminal},
path::{Path, PathBuf},
process::{Command, Stdio, exit},
str::FromStr,
sync::{
LazyLock,
atomic::{AtomicBool, Ordering},
},
time::{Duration, SystemTime},
};
use tempfile::TempDir;
use termcolor::{ColorChoice, ColorSpec, StandardStream, WriteColor};
use toml::{Table, Value};
pub mod flush;
pub mod github;
pub mod packaging;
mod curl;
mod on_disk_cache;
mod opts;
mod progress;
mod serialize;
mod verbose;
#[cfg(feature = "lock-index")]
mod flock;
use github::{Github as _, Impl as Github};
mod repo_status;
use repo_status::RepoStatus;
mod url;
use url::{Url, urls};
const SECS_PER_DAY: u64 = 24 * 60 * 60;
/// Percentage multiplier for similarity calculation
const SIMILARITY_SCALE: usize = 100;
/// Minimum similarity threshold (30%)
const SIMILARITY_THRESHOLD: usize = 30;
#[derive(Debug, Parser)]
#[clap(bin_name = "cargo", display_name = "cargo")]
struct Cargo {
#[clap(subcommand)]
subcmd: CargoSubCommand,
}
#[derive(Debug, Parser)]
enum CargoSubCommand {
Unmaintained(Opts),
}
include!(concat!(env!("OUT_DIR"), "/after_help.rs"));
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Parser)]
#[remain::sorted]
#[clap(
version = crate_version!(),
about = "Find unmaintained packages in Rust projects",
after_help = AFTER_HELP
)]
struct Opts {
#[clap(
long,
help = "When to use color: always, auto, or never",
default_value = "auto",
value_name = "WHEN"
)]
color: ColorChoice,
#[clap(
long,
help = "Exit as soon as an unmaintained package is found",
conflicts_with = "no_exit_code"
)]
fail_fast: bool,
#[clap(long, help = "Output JSON (experimental)")]
json: bool,
#[clap(
long,
help = "Age in days that a repository's last commit must not exceed for the repository to \
be considered current; 0 effectively disables this check, though ages are still \
reported",
value_name = "DAYS",
default_value = "365"
)]
max_age: u64,
#[cfg(all(feature = "on-disk-cache", not(windows)))]
#[clap(long, help = "Do not cache data on disk for future runs")]
no_cache: bool,
#[clap(
long,
help = "Do not set exit status when unmaintained packages are found",
conflicts_with = "fail_fast"
)]
no_exit_code: bool,
#[clap(long, help = "Do not show warnings")]
no_warnings: bool,
#[clap(
long,
short,
help = "Check only whether package NAME is unmaintained",
value_name = "NAME"
)]
package: Option<String>,
#[cfg(all(feature = "on-disk-cache", not(windows)))]
#[clap(long, help = "Remove all cached data from disk and exit")]
purge: bool,
#[cfg(not(windows))]
#[clap(
long,
help = "Read a personal access token from standard input and save it to \
$HOME/.config/cargo-unmaintained/token.txt"
)]
save_token: bool,
#[cfg(windows)]
#[clap(
long,
help = "Read a personal access token from standard input and save it to \
%LOCALAPPDATA%\\cargo-unmaintained\\token.txt"
)]
save_token: bool,
#[clap(long, help = "Show paths to unmaintained packages")]
tree: bool,
#[clap(long, help = "Show information about what cargo-unmaintained is doing")]
verbose: bool,
}
struct UnmaintainedPkg<'a> {
pkg: &'a Package,
repo_age: RepoStatus<'a, u64>,
newer_version_is_available: bool,
outdated_deps: Vec<OutdatedDep<'a>>,
}
struct OutdatedDep<'a> {
dep: &'a Dependency,
version_used: &'a Version,
version_latest: Version,
}
struct DepReq<'a> {
name: &'a str,
req: VersionReq,
}
impl<'a> DepReq<'a> {
#[allow(dead_code)]
fn new(name: &'a str, req: VersionReq) -> Self {
Self { name, req }
}
fn matches(&self, pkg: &Package) -> bool {
self.name == pkg.name && self.req.matches(&pkg.version)
}
}
impl<'a> From<&'a Dependency> for DepReq<'a> {
fn from(value: &'a Dependency) -> Self {
Self {
name: &value.name,
req: value.req.clone(),
}
}
}
#[macro_export]
macro_rules! warn {
(
$fmt:expr,
$($arg:tt)*
) => {
if $crate::opts::get().no_warnings {
log::debug!($fmt, $($arg)*);
} else {
$crate::verbose::newline!();
$crate::PROGRESS.with_borrow_mut(|progress| progress.as_mut().map($crate::progress::Progress::newline));
eprintln!(concat!("warning: ", $fmt), $($arg)*);
}
};
}
thread_local! {
#[allow(clippy::unwrap_used)]
static INDEX: LazyLock<GitIndex> = LazyLock::new(|| {
let _lock = lock_index().unwrap();
let mut index = GitIndex::new_cargo_default().unwrap();
if let Err(error) = index.update() {
warn!("failed to update index: {}", error);
}
index
});
static PROGRESS: RefCell<Option<progress::Progress>> = const { RefCell::new(None) };
// smoelius: The next four statics are "in-memory" caches.
// smoelius: Note that repositories are (currently) stored in both an in-memory cache and an
// on-disk cache. The former is keyed by url; the latter is keyed by package.
// smoelius: A reason for having the former is the following. Multiple packages map to the same
// url, and multiple urls map to the same shortened url. Thus, a cache keyed by url has a
// greater chance of a cache hit.
static GENERAL_STATUS_CACHE: RefCell<
HashMap<Url<'static>, RepoStatus<'static, ()>>
> = RefCell::new(HashMap::new());
static LATEST_VERSION_CACHE: RefCell<HashMap<String, Version>> = RefCell::new(HashMap::new());
static TIMESTAMP_CACHE: RefCell<
HashMap<Url<'static>, RepoStatus<'static, SystemTime>>
> = RefCell::new(HashMap::new());
static REPOSITORY_CACHE: RefCell<
HashMap<Url<'static>, RepoStatus<'static, PathBuf>>
> = RefCell::new(HashMap::new());
}
static TOKEN_FOUND: AtomicBool = AtomicBool::new(false);
pub fn run() -> Result<()> {
env_logger::init();
let Cargo {
subcmd: CargoSubCommand::Unmaintained(opts),
} = Cargo::parse_from(args());
opts::init(opts);
if opts::get().save_token {
// smoelius: Currently, if additional options are passed besides --save-token, they are
// ignored and no error is emitted. This is ugly.
return Github::save_token();
}
#[cfg(all(feature = "on-disk-cache", not(windows)))]
if opts::get().purge {
return on_disk_cache::purge_cache();
}
if Github::load_token(|_| Ok(()))? {
TOKEN_FOUND.store(true, Ordering::SeqCst);
}
match unmaintained() {
Ok(false) => exit(0),
Ok(true) => exit(1),
Err(error) => {
eprintln!("Error: {error:?}");
exit(2);
}
}
}
fn unmaintained() -> Result<bool> {
let mut unmaintained_pkgs = Vec::new();
let metadata = metadata()?;
let packages = packages(&metadata)?;
eprintln!(
"Scanning {} packages and their dependencies{}",
packages.len(),
if opts::get().verbose {
""
} else {
" (pass --verbose for more information)"
}
);
if std::io::stderr().is_terminal() && !opts::get().verbose {
PROGRESS.with_borrow_mut(|progress| {
*progress = Some(progress::Progress::new(packages.len()));
});
}
for pkg in packages {
PROGRESS.with_borrow_mut(|progress| {
progress
.as_mut()
.map_or(Ok(()), |progress| progress.advance(&pkg.name))
})?;
if let Some(mut unmaintained_pkg) = is_unmaintained_package(&metadata, pkg)? {
// smoelius: Before considering a package unmaintained, verify that its latest version
// would be considered unmaintained as well. Note that we still report the details of
// the version currently used. We may want to revisit this in the future.
let newer_version_is_available = newer_version_is_available(pkg)?;
if !newer_version_is_available || latest_version_is_unmaintained(&pkg.name)? {
unmaintained_pkg.newer_version_is_available = newer_version_is_available;
unmaintained_pkgs.push(unmaintained_pkg);
if opts::get().fail_fast {
break;
}
}
}
}
PROGRESS
.with_borrow_mut(|progress| progress.as_mut().map_or(Ok(()), progress::Progress::finish))?;
if opts::get().json {
unmaintained_pkgs.sort_by_key(|unmaintained| &unmaintained.pkg.id);
let json = serde_json::to_string_pretty(&unmaintained_pkgs)?;
println!("{json}");
} else {
if unmaintained_pkgs.is_empty() {
eprintln!("No unmaintained packages found");
return Ok(false);
}
unmaintained_pkgs.sort_by_key(|unmaintained| unmaintained.repo_age.erase_url());
display_unmaintained_pkgs(&unmaintained_pkgs)?;
}
Ok(!opts::get().no_exit_code)
}
fn metadata() -> Result<Metadata> {
let mut command = MetadataCommand::new();
// smoelius: See tests/snapbox.rs for another use of this conditional initialization trick.
let tempdir: TempDir;
if let Some(name) = &opts::get().package {
tempdir = packaging::temp_package(name)?;
command.current_dir(tempdir.path());
}
command.exec().map_err(Into::into)
}
fn packages(metadata: &Metadata) -> Result<Vec<&Package>> {
let ignored_packages = ignored_packages(metadata)?;
for name in &ignored_packages {
if !metadata.packages.iter().any(|pkg| pkg.name == *name) {
warn!(
"workspace metadata says to ignore `{}`, but workspace does not depend upon `{}`",
name, name
);
}
}
filter_packages(metadata, &ignored_packages)
}
#[derive(serde::Deserialize)]
struct UnmaintainedMetadata {
ignore: Option<Vec<String>>,
}
fn ignored_packages(metadata: &Metadata) -> Result<HashSet<String>> {
let serde_json::Value::Object(object) = &metadata.workspace_metadata else {
return Ok(HashSet::default());
};
let Some(value) = object.get("unmaintained") else {
return Ok(HashSet::default());
};
let metadata = serde_json::value::from_value::<UnmaintainedMetadata>(value.clone())?;
Ok(metadata.ignore.unwrap_or_default().into_iter().collect())
}
fn filter_packages<'a>(
metadata: &'a Metadata,
ignored_packages: &HashSet<String>,
) -> Result<Vec<&'a Package>> {
let mut packages = Vec::new();
// smoelius: If a project relies on multiple versions of a package, check only the latest one.
let metadata_latest_version_map = build_metadata_latest_version_map(metadata);
for pkg in &metadata.packages {
// smoelius: Don't consider whether workspace members are unmaintained.
if metadata.workspace_members.contains(&pkg.id) {
continue;
}
if ignored_packages.contains(&pkg.name) {
continue;
}
#[allow(clippy::panic)]
let version = metadata_latest_version_map
.get(&pkg.name)
.unwrap_or_else(|| {
panic!(
"`metadata_latest_version_map` does not contain {}",
pkg.name
)
});
if pkg.version != *version {
continue;
}
if let Some(name) = &opts::get().package {
if pkg.name != *name {
continue;
}
}
packages.push(pkg);
}
if let Some(name) = &opts::get().package {
if packages.len() >= 2 {
bail!("found multiple packages matching `{name}`: {:#?}", packages);
}
if packages.is_empty() {
bail!("found no packages matching `{name}`");
}
}
Ok(packages)
}
fn build_metadata_latest_version_map(metadata: &Metadata) -> HashMap<String, Version> {
let mut map: HashMap<String, Version> = HashMap::new();
for pkg in &metadata.packages {
if let Some(version) = map.get_mut(&pkg.name) {
if *version < pkg.version {
*version = pkg.version.clone();
}
} else {
map.insert(pkg.name.clone(), pkg.version.clone());
}
}
map
}
fn newer_version_is_available(pkg: &Package) -> Result<bool> {
if pkg
.source
.as_ref()
.is_none_or(|source| !source.is_crates_io())
{
return Ok(false);
}
let latest_version = latest_version(&pkg.name)?;
Ok(pkg.version != latest_version)
}
fn latest_version_is_unmaintained(name: &str) -> Result<bool> {
let tempdir = packaging::temp_package(name)?;
let metadata = MetadataCommand::new().current_dir(tempdir.path()).exec()?;
#[allow(clippy::panic)]
let pkg = metadata
.packages
.iter()
.find(|pkg| name == pkg.name)
.unwrap_or_else(|| panic!("failed to find package `{name}`"));
let unmaintained_package = is_unmaintained_package(&metadata, pkg)?;
Ok(unmaintained_package.is_some())
}
fn is_unmaintained_package<'a>(
metadata: &'a Metadata,
pkg: &'a Package,
) -> Result<Option<UnmaintainedPkg<'a>>> {
if let Some(url_string) = &pkg.repository {
let can_use_github_api =
TOKEN_FOUND.load(Ordering::SeqCst) && url_string.starts_with("https://github.com/");
let url = url_string.as_str().into();
if can_use_github_api {
let repo_status = general_status(&pkg.name, url)?;
if repo_status.is_failure() {
return Ok(Some(UnmaintainedPkg {
pkg,
repo_age: repo_status.map_failure(),
newer_version_is_available: false,
outdated_deps: Vec::new(),
}));
}
}
let repo_status = clone_repository(pkg)?;
if repo_status.is_failure() {
// smoelius: Mercurial repos get a pass.
if matches!(repo_status, RepoStatus::Uncloneable(_)) && curl::is_mercurial_repo(url)? {
return Ok(None);
}
return Ok(Some(UnmaintainedPkg {
pkg,
repo_age: repo_status.map_failure(),
newer_version_is_available: false,
outdated_deps: Vec::new(),
}));
}
}
let outdated_deps = outdated_deps(metadata, pkg)?;
if outdated_deps.is_empty() {
return Ok(None);
}
let repo_age = latest_commit_age(pkg)?;
if repo_age
.as_success()
.is_some_and(|(_, &age)| age < opts::get().max_age * SECS_PER_DAY)
{
return Ok(None);
}
Ok(Some(UnmaintainedPkg {
pkg,
repo_age,
newer_version_is_available: false,
outdated_deps,
}))
}
fn general_status(name: &str, url: Url) -> Result<RepoStatus<'static, ()>> {
GENERAL_STATUS_CACHE.with_borrow_mut(|general_status_cache| {
if let Some(&value) = general_status_cache.get(&url) {
return Ok(value);
}
let to_string: &dyn Fn(&RepoStatus<'static, ()>) -> String;
let (use_github_api, what, how) = if TOKEN_FOUND.load(Ordering::SeqCst)
&& url.as_str().starts_with("https://github.com/")
{
to_string = &RepoStatus::to_archival_status_string;
(true, "archival status", "GitHub API")
} else {
to_string = &RepoStatus::to_existence_string;
(false, "existence", "HTTP request")
};
verbose::wrap!(
|| {
let repo_status = (if use_github_api {
Github::archival_status(url)
} else {
curl::existence(url)
})
.unwrap_or_else(|error| {
warn!("failed to determine `{}` {}: {}", name, what, error);
RepoStatus::Success(url, ())
})
.leak_url();
general_status_cache.insert(url.leak(), repo_status);
Ok(repo_status)
},
to_string,
"{} of `{}` using {}",
what,
name,
how
)
})
}
#[allow(clippy::unnecessary_wraps)]
fn outdated_deps<'a>(metadata: &'a Metadata, pkg: &'a Package) -> Result<Vec<OutdatedDep<'a>>> {
if !published(pkg) {
return Ok(Vec::new());
}
let mut deps = Vec::new();
for dep in &pkg.dependencies {
// smoelius: Don't check dependencies in private registries.
if dep.registry.is_some() {
continue;
}
// smoelius: Don't check dependencies specified by path.
if dep.path.is_some() {
continue;
}
let Some(dep_pkg) = find_packages(metadata, dep.into()).next() else {
debug_assert!(dep.kind == DependencyKind::Development || dep.optional);
continue;
};
let Ok(version_latest) = latest_version(&dep.name).map_err(|error| {
// smoelius: I don't understand why a package can fail to be in the index, but I have
// seen it happen.
warn!("failed to get latest version of `{}`: {}", dep.name, error);
}) else {
continue;
};
if dep_pkg.version <= version_latest && !dep.req.matches(&version_latest) {
let versions = versions(&dep_pkg.name)?;
// smoelius: Require at least one incompatible version of the dependency that is more
// than `max_age` days old.
if versions
.iter()
.try_fold(false, |init, version| -> Result<_> {
if init {
return Ok(true);
}
let duration = SystemTime::now().duration_since(version.created_at.into())?;
let version_num = Version::parse(&version.num)?;
Ok(duration.as_secs() >= opts::get().max_age * SECS_PER_DAY
&& dep_pkg.version <= version_num
&& !dep.req.matches(&version_num))
})?
{
deps.push(OutdatedDep {
dep,
version_used: &dep_pkg.version,
version_latest,
});
}
}
}
// smoelius: A dependency could appear more than once, e.g., because it is used with different
// features as a normal and as a development dependency.
deps.dedup_by(|lhs, rhs| lhs.dep.name == rhs.dep.name && lhs.dep.req == rhs.dep.req);
Ok(deps)
}
fn published(pkg: &Package) -> bool {
pkg.publish.as_deref() != Some(&[])
}
fn find_packages<'a>(
metadata: &'a Metadata,
dep_req: DepReq<'a>,
) -> impl Iterator<Item = &'a Package> {
metadata
.packages
.iter()
.filter(move |pkg| dep_req.matches(pkg))
}
#[cfg_attr(dylint_lib = "general", allow(non_local_effect_before_error_return))]
fn latest_version(name: &str) -> Result<Version> {
LATEST_VERSION_CACHE.with_borrow_mut(|latest_version_cache| {
if let Some(version) = latest_version_cache.get(name) {
return Ok(version.clone());
}
verbose::wrap!(
|| {
let krate = INDEX.with(|index| {
let _ = LazyLock::force(index);
let _lock = lock_index()?;
index
.crate_(name)
.ok_or_else(|| anyhow!("failed to find `{}` in index", name))
})?;
let latest_version_index = krate
.highest_normal_version()
.ok_or_else(|| anyhow!("`{}` has no normal version", name))?;
let latest_version = Version::from_str(latest_version_index.version())?;
latest_version_cache.insert(name.to_owned(), latest_version.clone());
Ok(latest_version)
},
ToString::to_string,
"latest version of `{}` using crates.io index",
name
)
})
}
fn versions(name: &str) -> Result<Vec<crates_io_api::Version>> {
on_disk_cache::with_cache(|cache| -> Result<_> {
verbose::wrap!(
|| { cache.fetch_versions(name) },
|versions: &[crates_io_api::Version]| format!("{} versions", versions.len()),
"versions of `{}` using crates.io API",
name
)
})
}
fn latest_commit_age(pkg: &Package) -> Result<RepoStatus<'_, u64>> {
let repo_status = timestamp(pkg)?;
repo_status
.map(|timestamp| {
let duration = SystemTime::now().duration_since(timestamp)?;
Ok(duration.as_secs())
})
.transpose()
}
#[cfg_attr(dylint_lib = "general", allow(non_local_effect_before_error_return))]
fn timestamp(pkg: &Package) -> Result<RepoStatus<'_, SystemTime>> {
TIMESTAMP_CACHE.with_borrow_mut(|timestamp_cache| {
// smoelius: Check both the regular and the shortened url.
for url in urls(pkg) {
if let Some(&repo_status) = timestamp_cache.get(&url) {
// smoelius: If a previous attempt to timestamp the repository failed (e.g., because
// of spurious network errors), then don't bother checking the repository cache.
let Some((url_timestamped, ×tamp)) = repo_status.as_success() else {
return Ok(repo_status);
};
assert_eq!(url, url_timestamped);
// smoelius: `pkg`'s repository could contain other packages that were already
// timestamped. Thus, `pkg`'s repository could already be in the timestamp cache.
// But in that case, we still need to verify that `pkg` appears in its repository.
let repo_status = clone_repository(pkg)?;
let Some((url_cloned, _)) = repo_status.as_success() else {
return Ok(repo_status.map_failure());
};
assert_eq!(url, url_cloned);
return Ok(RepoStatus::Success(url, timestamp));
}
}
let repo_status = timestamp_uncached(pkg)?;
if let Some((url, _)) = repo_status.as_success() {
timestamp_cache.insert(url.leak(), repo_status.leak_url());
} else {
// smoelius: In the event of failure, set all urls associated with the
// repository.
for url in urls(pkg) {
timestamp_cache.insert(url.leak(), repo_status.leak_url());
}
}
Ok(repo_status)
})
}
fn timestamp_uncached(pkg: &Package) -> Result<RepoStatus<'_, SystemTime>> {
if pkg.repository.is_none() {
return Ok(RepoStatus::Unnamed);
}
timestamp_from_clone(pkg)
}
fn timestamp_from_clone(pkg: &Package) -> Result<RepoStatus<'_, SystemTime>> {
let repo_status = clone_repository(pkg)?;
let Some((url, repo_dir)) = repo_status.as_success() else {
return Ok(repo_status.map_failure());
};
let mut command = Command::new("git");
command
.args(["log", "-1", "--pretty=format:%ct"])
.current_dir(repo_dir);
let output = command
.output()
.with_context(|| format!("failed to run command: {command:?}"))?;
ensure!(output.status.success(), "command failed: {command:?}");
let stdout = std::str::from_utf8(&output.stdout)?;
let secs = u64::from_str(stdout.trim_end())?;
let timestamp = SystemTime::UNIX_EPOCH + Duration::from_secs(secs);
Ok(RepoStatus::Success(url, timestamp))
}
#[cfg_attr(dylint_lib = "general", allow(non_local_effect_before_error_return))]
#[cfg_attr(dylint_lib = "supplementary", allow(commented_code))]
fn clone_repository(pkg: &Package) -> Result<RepoStatus<PathBuf>> {
let repo_status = REPOSITORY_CACHE.with_borrow_mut(|repository_cache| -> Result<_> {
on_disk_cache::with_cache(|cache| -> Result<_> {
// smoelius: Check all urls associated with the package.
for url in urls(pkg) {
if let Some(repo_status) = repository_cache.get(&url) {
return Ok(repo_status.clone());
}
}
// smoelius: To make verbose printing easier, "membership" is printed regardless of the
// check's purpose, and the `Purpose` type was removed.
/* let what = match purpose {
Purpose::Membership => "membership",
Purpose::Timestamp => "timestamp",
}; */
verbose::wrap!(
|| {
let url_and_dir = cache.clone_repository(pkg);
match url_and_dir {
Ok((url_string, repo_dir)) => {
// smoelius: Note the use of `leak` in the next line. But the url is
// acting as a key in a global map, so it is not so bad.
let url = Url::from(url_string.as_str()).leak();
repository_cache
.insert(url, RepoStatus::Success(url, repo_dir.clone()).leak_url());
Ok(RepoStatus::Success(url, repo_dir))
}
Err(error) => {
let repo_status = if let Some(url_string) = &pkg.repository {
let url = url_string.as_str().into();
// smoelius: If cloning failed because the repository does not
// exist, adjust the repo status.
let existence = general_status(&pkg.name, url)?;
let repo_status = if existence.is_failure() {
existence.map_failure()
} else {
RepoStatus::Uncloneable(url)
};
warn!("failed to clone `{}`: {}", url_string, error);
repo_status
} else {
RepoStatus::Unnamed
};
// smoelius: In the event of failure, set all urls associated with
// the repository.
for url in urls(pkg) {
repository_cache.insert(url.leak(), repo_status.clone().leak_url());
}
Ok(repo_status)
}
}
},
RepoStatus::to_membership_string,
"membership of `{}` using shallow clone",
pkg.name
)
})
})?;
let Some((url, repo_dir)) = repo_status.as_success() else {
return Ok(repo_status);
};
// smoelius: Even if `purpose` is `Purpose::Timestamp`, verify that `pkg` is a member of the
// repository.
if membership_in_clone(pkg, repo_dir)? {
Ok(repo_status)
} else {
Ok(RepoStatus::Unassociated(url))
}
}
const LINE_PREFIX: &str = "D ";
fn membership_in_clone(pkg: &Package, repo_dir: &Path) -> Result<bool> {
let mut command = Command::new("git");
command.args(["status", "--porcelain"]);
command.current_dir(repo_dir);
command.stdout(Stdio::piped());
let mut child = command
.spawn()
.with_context(|| format!("command failed: {command:?}"))?;
#[allow(clippy::unwrap_used)]
let stdout = child.stdout.take().unwrap();
let reader = std::io::BufReader::new(stdout);
for result in reader.lines() {
let line = result.with_context(|| format!("failed to read `{}`", repo_dir.display()))?;
#[allow(clippy::panic)]
let path = line.strip_prefix(LINE_PREFIX).map_or_else(
|| panic!("cache is corrupt at `{}`", repo_dir.display()),
Path::new,
);
if path.file_name() != Some(OsStr::new("Cargo.toml")) {
continue;
}
let contents = show(repo_dir, path)?;
let Ok(table) = contents.parse::<Table>() else
/* smoelius: This "failed to parse" warning is a little too noisy.
.map_err(|error| {
warn!(
"failed to parse {:?}: {}",
path,
error.to_string().trim_end()
);
}) */
{
continue;
};
// First check exact name match (existing behavior)
if table
.get("package")
.and_then(Value::as_table)
.and_then(|table| table.get("name"))
.and_then(Value::as_str)
== Some(&pkg.name)
{
return Ok(true);
}
// If name doesn't match, check if it might be a renamed package
if is_same_package_except_name(pkg, &table) {
return Ok(true);
}
}
Ok(false)
}
/// Checks if a package is the same as one defined in a Cargo.toml table, except for its name.
/// This helps identify renamed packages.
fn is_same_package_except_name(pkg: &Package, cargo_toml: &Table) -> bool {
let Some(pkg_table) = cargo_toml.get("package").and_then(Value::as_table) else {
return false;
};
// Check repository URL (if present in both)
if let (Some(original_repo), Some(candidate_repo)) = (
&pkg.repository,
pkg_table.get("repository").and_then(Value::as_str),
) {
if original_repo == candidate_repo {
return true;
}
}
// Check other invariant fields
// 1. Check authors (if present in both)
if !pkg.authors.is_empty() {
if let Some(candidate_authors) = pkg_table.get("authors").and_then(Value::as_array) {
let candidate_authors: Vec<&str> = candidate_authors
.iter()
.filter_map(|a| a.as_str())
.collect();
if !candidate_authors.is_empty() && have_common_author(&pkg.authors, &candidate_authors)
{
return true;
}
}
}
// 2. Check version (exact match)
if let Some(version_str) = pkg_table.get("version").and_then(Value::as_str) {
if let Ok(version) = Version::from_str(version_str) {
if version == pkg.version {
return true;
}
}
}
// 3. Check description similarity (if present in both)
if let (Some(original_desc), Some(candidate_desc)) = (
&pkg.description,
pkg_table.get("description").and_then(Value::as_str),
) {
if high_similarity(original_desc, candidate_desc) {
return true;
}
}
false
}
/// Checks if two lists of authors have at least one author in common.
fn have_common_author(authors1: &[String], authors2: &[&str]) -> bool {
for author1 in authors1 {
if authors2.contains(&author1.as_str()) {
return true;
}
}
false
}
/// Checks if two strings have high textual similarity.
/// Returns true if they share a significant portion of words.
fn high_similarity(s1: &str, s2: &str) -> bool {
let s1_words: HashSet<&str> = s1.split_whitespace().collect();
let s2_words: HashSet<&str> = s2.split_whitespace().collect();
if s1_words.is_empty() || s2_words.is_empty() {
return false;
}
let common_words = s1_words.intersection(&s2_words).count();
let min_words = s1_words.len().min(s2_words.len());
// Avoid precision loss by doing integer division first
let similarity = if min_words > 0 {
(common_words * SIMILARITY_SCALE) / min_words
} else {
0
};