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
use std::env;
use std::fs;
use std::io;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::process::Output;
#[cfg(unix)]
use std::os::unix::fs::symlink;
mod hash;
pub struct LTS {
git_dir: PathBuf,
home: PathBuf,
}
pub struct Branch {
pub name: String,
pub head: String,
}
impl LTS {
pub fn new(dir: Option<PathBuf>) -> Self {
let home = get_cargo_home();
let git_dir = dir.unwrap_or_else(|| home.join("registry/index/github.com-1ecc6299db9ec823/.git"));
LTS {
git_dir: git_dir,
home: home,
}
}
pub fn fetch(&self) -> io::Result<()> {
if !self.git(&["rev-parse", "snapshot-2018-09-26", "--"]).is_ok() {
self.git(&["fetch", "https://github.com/rust-lang/crates.io-index", "snapshot-2018-09-26:snapshot-2018-09-26"])?;
}
Ok(())
}
pub fn cut_branch_at(&self, cutoff: &str) -> io::Result<Branch> {
let last_commit_hash = self.git(&["log", "--all", "-1", "--format=%H", "--until", cutoff])?;
let treeish = format!("{}^{{tree}}", last_commit_hash);
let msg = format!("Registry at {}", cutoff);
let new_head = self.git(&["commit-tree", &treeish, "-m", &msg])?;
let fork_name = format!("lts-repo-at-{}", &last_commit_hash[0..10]);
if self.git(&["branch", &fork_name, &new_head]).is_err() {
let refname = format!("refs/heads/{}", fork_name);
self.git(&["update-ref", &refname, &new_head])?;
}
Ok(Branch {
name: fork_name,
head: new_head,
})
}
pub fn clone_to(&self, branch: &Branch, fork_destination_dir: &Path, bare: bool) -> io::Result<String> {
let _ = fs::remove_dir_all(&fork_destination_dir);
let mut cmd = Command::new("git");
cmd.args(&["clone", "--single-branch", "--branch", &branch.name]);
if bare {
cmd.arg("--bare");
}
cmd.arg(&self.git_dir).arg(&fork_destination_dir);
check(cmd.output()?)?;
let tmp;
let fork_git_dir = if bare {
fork_destination_dir
} else {
tmp = fork_destination_dir.join(".git");
&tmp
};
git(&fork_git_dir, &["update-ref", "HEAD", &branch.head])?;
if bare {
git(&fork_git_dir, &["branch", "master", &branch.head])?;
} else {
git(&fork_git_dir, &["checkout", "-b", "master", &branch.head])?;
}
let fork_repo_abs = fs::canonicalize(&fork_git_dir)?;
let fork_url = format!("file://{}", fork_repo_abs.display());
self.make_cache_shared(&fork_url);
Ok(fork_url)
}
pub fn git_dir(&self) -> &Path {
&self.git_dir
}
fn git(&self, args: &[&str]) -> io::Result<String> {
git(&self.git_dir, args)
}
fn make_cache_shared(&self, url: &str) {
let hash = hash::short_hash(url);
let fork_cache_dir = self.home.join(format!("registry/cache/-{}", hash));
let git_cache_dir = self.home.join("registry/cache/github.com-1ecc6299db9ec823");
#[cfg(unix)]
let _ = symlink(git_cache_dir, fork_cache_dir);
let fork_src_dir = self.home.join(format!("registry/src/-{}", hash));
let git_src_dir = self.home.join("registry/src/github.com-1ecc6299db9ec823");
#[cfg(unix)]
let _ = symlink(git_src_dir, fork_src_dir);
}
}
fn git(git_dir: &Path, args: &[&str]) -> io::Result<String> {
let out = Command::new("git")
.arg("--git-dir")
.arg(git_dir)
.args(args)
.output()?;
check(out)
}
fn check(out: Output) -> io::Result<String> {
if !out.status.success() {
return Err(io::Error::new(io::ErrorKind::Other, String::from_utf8_lossy(&out.stderr).into_owned()));
}
Ok(String::from_utf8_lossy(&out.stdout).trim().to_string())
}
#[allow(deprecated)]
fn get_cargo_home() -> PathBuf {
env::var_os("CARGO_HOME").map(PathBuf::from).or_else(|| env::home_dir().map(|d| d.join(".cargo"))).expect("$CARGO_HOME not set")
}