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
#[macro_use]
extern crate failure;
extern crate flate2;
extern crate regex;
extern crate reqwest;
extern crate semver;
extern crate serde_json;
extern crate tar;

use failure::{Error, ResultExt};
use flate2::read::GzDecoder;
use regex::Regex;
use reqwest::StatusCode;
use serde_json::Value;
use std::env;
use std::process::Command;
use tar::Archive;

pub fn clone(method_name: &str, spec: &str, extra: &[&str]) -> Result<(), Error> {
    let mut parts = spec.splitn(2, ':');
    let name = parts.next().unwrap();
    let pkg_info = get_pkg_info(name)?;
    let repo = get_repo(&pkg_info)?;
    let (method, repo) = match method_name {
        "auto" => {
            if let Some(repo) = repo {
                detect_repo(&repo)?
            } else {
                ("crate", "".to_string())
            }
        }
        "crate" => ("crate", "".to_string()),
        _ => {
            if repo.is_none() {
                bail!("Could not find repository path in crates.io.");
            }
            (method_name, repo.unwrap())
        }
    };
    match method {
        "crate" => clone_crate(spec, &pkg_info, extra)?,
        "git" | "hg" | "pijul" | "fossil" => run_clone(method, &repo, extra)?,
        _ => bail!("Unsupported method `{}`", method),
    }

    Ok(())
}

fn detect_repo(repo: &str) -> Result<(&'static str, String), Error> {
    if repo.ends_with(".git") {
        return Ok(("git", repo.to_string()));
    }
    if let Some(c) = Regex::new(r"https?://(?:www\.)?github\.com/([^/]+)/([^/]+)")
        .unwrap()
        .captures(repo)
    {
        return Ok((
            "git",
            format!(
                "https://github.com/{}/{}.git",
                c.get(1).unwrap().as_str(),
                c.get(2).unwrap().as_str()
            ),
        ));
    }
    if let Some(c) = Regex::new(r"https?://(?:www\.)?gitlab\.com/([^/]+)/([^/]+)")
        .unwrap()
        .captures(repo)
    {
        return Ok((
            "git",
            format!(
                "https://gitlab.com/{}/{}.git",
                c.get(1).unwrap().as_str(),
                c.get(2).unwrap().as_str()
            ),
        ));
    }
    if let Some(c) = Regex::new(r"https?://(?:www\.)?bitbucket\.(?:org|com)/([^/]+)/([^/]+)")
        .unwrap()
        .captures(repo)
    {
        let user = c.get(1).unwrap().as_str();
        let name = c.get(2).unwrap().as_str();
        return bitbucket(user, name);
    }
    if repo.starts_with("https://nest.pijul.com/") {
        return Ok(("pijul", repo.to_string()));
    }
    bail!(
        "Could not determine the VCS from repo `{}`, \
         use the `--method` option to specify how to download.",
        repo
    );
}

fn bitbucket(user: &str, name: &str) -> Result<(&'static str, String), Error> {
    // Determine if it is git or hg.
    let api_url = &format!(
        "https://api.bitbucket.org/2.0/repositories/{}/{}",
        user, name
    );
    let mut repo_info = reqwest::get(api_url).context("Failed to fetch repo info from bitbucket.")?;
    let code = repo_info.status();
    if code != StatusCode::Ok {
        bail!(
            "Failed to get repo info from bitbucket API `{}`: `{}`",
            api_url,
            code
        );
    }
    let repo_info: Value = repo_info
        .json()
        .context("Failed to convert to bitbucket json.")?;
    let method = repo_info["scm"]
        .as_str()
        .expect("Could not get `scm` from bitbucket.");
    let method = match method {
        "git" => "git",
        "hg" => "hg",
        _ => bail!("Unexpected bitbucket scm: `{}`", method),
    };
    let clones = repo_info["links"]["clone"]
        .as_array()
        .expect("Could not get `clone` from bitbucket.");
    let href = clones
        .iter()
        .find(|c| {
            c["name"]
                .as_str()
                .expect("Could not get clone `name` from bitbucket.") == "https"
        })
        .expect("Could not find `https` clone in bitbucket.")["href"]
        .as_str()
        .expect("Could not get clone `href` from bitbucket.");
    Ok((method, href.to_string()))
}

/// Grab package info from crates.io.
fn get_pkg_info(name: &str) -> Result<Value, Error> {
    let mut pkg_info = reqwest::get(&format!("https://crates.io/api/v1/crates/{}", name))
        .context("Failed to fetch package info from crates.io.")?;
    let code = pkg_info.status();
    match code {
        StatusCode::Ok => {}
        StatusCode::NotFound => bail!("Package `{}` not found on crates.io.", name),
        _ => bail!("Failed to get package info from crates.io: `{}`", code),
    }
    let pkg_info: Value = pkg_info.json().context("Failed to convert to json.")?;
    Ok(pkg_info)
}

/// Determine the repo path from the package info.
fn get_repo(pkg_info: &Value) -> Result<Option<String>, Error> {
    let krate = pkg_info
        .get("crate")
        .ok_or_else(|| format_err!("`crate` expected in pkg info"))?;
    let repo = &krate["repository"];
    if repo.is_string() {
        return Ok(Some(repo.as_str().unwrap().to_string()));
    }
    let home = &krate["homepage"];
    if home.is_string() {
        return Ok(Some(home.as_str().unwrap().to_string()));
    }
    Ok(None)
}

/// Download a crate from crates.io.
fn clone_crate(spec: &str, pkg_info: &Value, extra: &[&str]) -> Result<(), Error> {
    let mut parts = spec.splitn(2, ':');
    let name = parts.next().unwrap();
    let version = parts.next();

    if !extra.is_empty() {
        bail!("Got extra arguments, crate downloads take no extra arguments.");
    }

    let dst = env::current_dir()?;

    // Determine which version to download.
    let versions = pkg_info["versions"]
        .as_array()
        .expect("Could not find `versions` array on crates.io.");
    let versions = versions.iter().map(|crate_version| {
        let num = crate_version["num"]
            .as_str()
            .expect("Could not get `num` from version.");
        let v = semver::Version::parse(num).expect("Could not parse crate `num`.");
        (crate_version, v)
    });
    let mut versions: Vec<_> = if let Some(version) = version {
        let req = semver::VersionReq::parse(version)?;
        versions
            .filter(|(_crate_version, ver)| req.matches(ver))
            .collect()
    } else {
        versions.collect()
    };
    // Find the largest version.
    if versions.is_empty() {
        bail!("Could not find any matching versions.");
    }
    versions.sort_unstable_by_key(|x| x.1.clone());
    let last = versions.last().unwrap().0;
    let dl_path = last["dl_path"]
        .as_str()
        .expect("Could not find `dl_path` in crate version info.");
    let dl_path = format!("https://crates.io{}", dl_path);
    let version = last["num"]
        .as_str()
        .expect("Could not find `num` in crate version info.");
    println!("Downloading `{}`", dl_path);
    let mut response = reqwest::get(&dl_path).context(format!("Failed to download `{}`", dl_path))?;
    // TODO: This could be much better.
    let mut body = Vec::new();
    response.copy_to(&mut body)?;
    let gz = GzDecoder::new(body.as_slice());
    let mut tar = Archive::new(gz);
    let base = format!("{}-{}", name, version);

    for entry in tar.entries()? {
        let mut entry = entry.context("Failed to get tar entry.")?;
        let entry_path = entry
            .path()
            .context("Failed to read entry path.")?
            .into_owned();
        println!("{}", entry_path.display());

        // Sanity check.
        if !entry_path.starts_with(&base) {
            bail!(
                "Expected path `{}` in tarball, got `{}`.",
                base,
                entry_path.display()
            );
        }

        entry.unpack_in(&dst).context(format!(
            "failed to unpack entry at `{}`",
            entry_path.display()
        ))?;
    }
    Ok(())
}

/// Runs the clone process.
fn run_clone(method: &str, repo: &str, extra: &[&str]) -> Result<(), Error> {
    let status = Command::new(method)
        .arg("clone")
        .arg(repo)
        .args(extra)
        .status()
        .context(format!("Failed to run `{}`.", method))?;
    if !status.success() {
        bail!("`{} clone` did not finish successfully.", method);
    }
    Ok(())
}