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
mod file_index;
mod git_index;
pub use self::file_index::init_file_index;
use self::git_index::GitIndex;
use checksum::Checksum;
use core::errors::*;
use core::{Range, RelativePath, RpPackage, Version};
use git;
use objects::Objects;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use update::Update;
use url::Url;
pub struct IndexConfig {
pub repo_dir: PathBuf,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Deployment {
pub version: Version,
pub object: Checksum,
}
impl Deployment {
pub fn new(version: Version, object: Checksum) -> Deployment {
Deployment {
version: version,
object: object,
}
}
}
pub trait Index {
fn resolve(&self, package: &RpPackage, range: &Range) -> Result<Vec<Deployment>>;
fn resolve_by_prefix(&self, package: &RpPackage) -> Result<Vec<(Deployment, RpPackage)>>;
fn all(&self, package: &RpPackage) -> Result<Vec<Deployment>>;
fn put_version(
&self,
checksum: &Checksum,
package: &RpPackage,
version: &Version,
force: bool,
) -> Result<()>;
fn get_deployments(&self, package: &RpPackage, version: &Version) -> Result<Vec<Deployment>>;
fn objects_url(&self) -> Result<&str>;
fn objects_from_index(&self, relative_path: &RelativePath) -> Result<Box<Objects>>;
fn update(&self) -> Result<Vec<Update>> {
Ok(vec![])
}
}
pub struct NoIndex;
impl Index for NoIndex {
fn resolve(&self, _: &RpPackage, _: &Range) -> Result<Vec<Deployment>> {
Ok(vec![])
}
fn resolve_by_prefix(&self, _: &RpPackage) -> Result<Vec<(Deployment, RpPackage)>> {
Ok(vec![])
}
fn all(&self, _: &RpPackage) -> Result<Vec<Deployment>> {
Ok(vec![])
}
fn put_version(&self, _: &Checksum, _: &RpPackage, _: &Version, _: bool) -> Result<()> {
Err("Empty Index".into())
}
fn get_deployments(&self, _: &RpPackage, _: &Version) -> Result<Vec<Deployment>> {
Ok(vec![])
}
fn objects_url(&self) -> Result<&str> {
Err("Empty Index".into())
}
fn objects_from_index(&self, _: &RelativePath) -> Result<Box<Objects>> {
Err("Empty Index".into())
}
}
pub fn index_from_path(path: &Path) -> Result<Box<Index>> {
if !path.is_dir() {
return Err(format!("index: no such directory: {}", path.display()).into());
}
if path.join(".git").is_dir() {
let git_repo = git::open_git_repo(path)?;
let url = Url::from_file_path(path).map_err(|_| "failed to construct url")?;
return open_git_index(&url, git_repo, true);
}
Ok(Box::new(file_index::FileIndex::new(&path)?))
}
fn open_git_index(url: &Url, git_repo: git::GitRepo, publishing: bool) -> Result<Box<Index>> {
let git_repo = Rc::new(git_repo);
let file_objects = file_index::FileIndex::new(git_repo.path())?;
let index = GitIndex::new(url.clone(), git_repo, file_objects, publishing);
Ok(Box::new(index))
}
pub fn index_from_git<'a, I>(
config: IndexConfig,
scheme: I,
url: &'a Url,
publishing: bool,
) -> Result<Box<Index>>
where
I: IntoIterator<Item = &'a str>,
{
let mut scheme = scheme.into_iter();
let sub_scheme = scheme
.next()
.ok_or_else(|| format!("bad scheme ({}), expected git+scheme", url.scheme()))?;
let git_repo = if sub_scheme == "file" {
let mut url = url.clone();
url.set_scheme("file")
.map_err(|_| "failed to set scheme to `file`")?;
let path = url.to_file_path()
.map_err(|_| format!("url is not a file path: {}", url))?;
git::open_git_repo(path)?
} else {
git::setup_git_repo(&config.repo_dir, sub_scheme, url)?
};
open_git_index(url, git_repo, publishing)
}
pub fn index_from_url(config: IndexConfig, url: &Url, publishing: bool) -> Result<Box<Index>> {
let mut scheme = url.scheme().split("+");
let first = scheme.next().ok_or_else(|| format!("bad scheme: {}", url))?;
match first {
"file" => url.to_file_path()
.map_err(|_| format!("url is not a file path: {}", url).into())
.and_then(|path| index_from_path(&path)),
"git" => index_from_git(config, scheme, url, publishing),
scheme => Err(format!("bad scheme: {}", scheme).into()),
}.chain_err(|| format!("loading index from URL: {}", url))
}