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
use std::collections::{BTreeMap, HashSet};
use std::path::Path;
use core::PackageId;
use core::registry::PackageRegistry;
use core::{Resolve, SourceId, Package};
use core::resolver::Method;
use ops;
use util::config::Config;
use util::CargoResult;
pub struct UpdateOptions<'a> {
pub config: &'a Config,
pub to_update: &'a [String],
pub precise: Option<&'a str>,
pub aggressive: bool,
}
pub fn generate_lockfile(manifest_path: &Path, config: &Config)
-> CargoResult<()> {
let package = try!(Package::for_path(manifest_path, config));
let mut registry = PackageRegistry::new(config);
let resolve = try!(ops::resolve_with_previous(&mut registry, &package,
Method::Everything,
None, None));
try!(ops::write_pkg_lockfile(&package, &resolve, config));
Ok(())
}
pub fn update_lockfile(manifest_path: &Path,
opts: &UpdateOptions) -> CargoResult<()> {
if opts.aggressive && opts.precise.is_some() {
bail!("cannot specify both aggressive and precise simultaneously")
}
let package = try!(Package::for_path(manifest_path, opts.config));
let previous_resolve = match try!(ops::load_pkg_lockfile(&package, opts.config)) {
Some(resolve) => resolve,
None => return generate_lockfile(manifest_path, opts.config),
};
let mut registry = PackageRegistry::new(opts.config);
let mut to_avoid = HashSet::new();
if opts.to_update.is_empty() {
to_avoid.extend(previous_resolve.iter());
} else {
let mut sources = Vec::new();
for name in opts.to_update {
let dep = try!(previous_resolve.query(name));
if opts.aggressive {
fill_with_deps(&previous_resolve, dep, &mut to_avoid,
&mut HashSet::new());
} else {
to_avoid.insert(dep);
sources.push(match opts.precise {
Some(precise) => {
let precise = if dep.source_id().is_registry() {
format!("{}={}", dep.name(), precise)
} else {
precise.to_string()
};
dep.source_id().clone().with_precise(Some(precise))
}
None => {
dep.source_id().clone().with_precise(None)
}
});
}
}
try!(registry.add_sources(&sources));
}
let resolve = try!(ops::resolve_with_previous(&mut registry,
&package,
Method::Everything,
Some(&previous_resolve),
Some(&to_avoid)));
let print_change = |status: &str, msg: String| {
opts.config.shell().status(status, msg)
};
for (removed, added) in compare_dependency_graphs(&previous_resolve, &resolve) {
if removed.len() == 1 && added.len() == 1 {
let msg = if removed[0].source_id().is_git() {
format!("{} -> #{}", removed[0],
&added[0].source_id().precise().unwrap()[..8])
} else {
format!("{} -> v{}", removed[0], added[0].version())
};
try!(print_change("Updating", msg));
} else {
for package in removed.iter() {
try!(print_change("Removing", format!("{}", package)));
}
for package in added.iter() {
try!(print_change("Adding", format!("{}", package)));
}
}
}
try!(ops::write_pkg_lockfile(&package, &resolve, opts.config));
return Ok(());
fn fill_with_deps<'a>(resolve: &'a Resolve, dep: &'a PackageId,
set: &mut HashSet<&'a PackageId>,
visited: &mut HashSet<&'a PackageId>) {
if !visited.insert(dep) {
return
}
set.insert(dep);
for dep in resolve.deps(dep) {
fill_with_deps(resolve, dep, set, visited);
}
}
fn compare_dependency_graphs<'a>(previous_resolve: &'a Resolve,
resolve: &'a Resolve) ->
Vec<(Vec<&'a PackageId>, Vec<&'a PackageId>)> {
fn key(dep: &PackageId) -> (&str, &SourceId) {
(dep.name(), dep.source_id())
}
fn vec_subtract<'a>(a: &[&'a PackageId],
b: &[&'a PackageId]) -> Vec<&'a PackageId> {
a.iter().filter(|a| {
let i = match b.binary_search(a) {
Ok(i) => i,
Err(..) => return true,
};
if a.source_id().is_registry() {
return false
}
b[i..].iter().take_while(|b| a == b).all(|b| {
a.source_id().precise() != b.source_id().precise()
})
}).cloned().collect()
}
let mut changes = BTreeMap::new();
let empty = (Vec::new(), Vec::new());
for dep in previous_resolve.iter() {
changes.entry(key(dep)).or_insert(empty.clone()).0.push(dep);
}
for dep in resolve.iter() {
changes.entry(key(dep)).or_insert(empty.clone()).1.push(dep);
}
for (_, v) in changes.iter_mut() {
let (ref mut old, ref mut new) = *v;
old.sort();
new.sort();
let removed = vec_subtract(old, new);
let added = vec_subtract(new, old);
*old = removed;
*new = added;
}
debug!("{:#?}", changes);
changes.into_iter().map(|(_, v)| v).collect()
}
}