OXYGENHUB
◆ v0.4.5 oxygen / crates / oxy-core / src / repo / workspace.rs
12.7 KiB · Rust · last changed checkout BOOKMARK switches to it; workspace add takes a bookmark and its subrepos 1 month ago source blame history raw
1
//! Multiple workspaces (spec §12.2): extra checkout directories sharing one
2
//! `.oxy`. A secondary workspace holds a `.oxy` *file* of the form
3
//! `oxydir: <path>\nworkspace: <id>`; the repository lists workspaces under
4
//! `.oxy/workspaces/<id>/path`. Each workspace has its own working-copy
5
//! change, manifest, and short-lived snapshot lock.
6
7
use super::*;
8
9
#[derive(Debug, Clone, PartialEq, Eq)]
10
pub struct WorkspaceInfo {
11
    pub id: WorkspaceId,
12
    pub path: PathBuf,
13
    /// Working-copy change hash from the current view, if the workspace is registered there.
14
    pub working_copy: Option<ChangeHash>,
15
    pub current: bool,
16
}
17
18
/// A lock file, taken by creating it exclusively and released on drop: a
19
/// workspace's working-copy lock, the operation head's lock. A stale one,
20
/// whose process is gone (or, where that cannot be told, older than a
21
/// minute), is taken over.
22
pub struct FileLock {
23
    path: PathBuf,
24
}
25
26
/// A workspace's working-copy lock (spec §12.1), held by everything that
27
/// reads or writes the working copy and records its working-copy change:
28
/// snapshot, checkout, bookmark switch, commit, discard, pull. It is
29
/// re-entrant within a thread, so a checkout that snapshots first holds it
30
/// once.
31
pub struct WorkingCopyLock {
32
    path: PathBuf,
33
    _file: Option<FileLock>,
34
}
35
36
/// The working-copy lock; its old name.
37
pub type SnapshotLock = WorkingCopyLock;
38
39
thread_local! {
40
    /// Working-copy locks this thread holds, with their depth.
41
    static HELD: std::cell::RefCell<HashMap<PathBuf, usize>> = Default::default();
42
}
43
44
impl Drop for WorkingCopyLock {
45
    fn drop(&mut self) {
46
        HELD.with(|h| {
47
            let mut h = h.borrow_mut();
48
            if let Some(n) = h.get_mut(&self.path) {
49
                *n -= 1;
50
                if *n == 0 {
51
                    h.remove(&self.path);
52
                }
53
            }
54
        });
55
    }
56
}
57
58
/// Whether the process that wrote a lock file is gone. Where that cannot
59
/// be told (no `/proc`), a lock older than a minute counts as stale.
60
pub(crate) fn lock_is_stale(path: &Path) -> bool {
61
    let old = fs::metadata(path)
62
        .and_then(|m| m.modified())
63
        .map(|t| t.elapsed().map(|d| d.as_secs() > 60).unwrap_or(false))
64
        .unwrap_or(true);
65
    let proc_fs = Path::new("/proc/self").exists();
66
    match fs::read_to_string(path)
67
        .ok()
68
        .and_then(|s| s.trim().parse::<u32>().ok())
69
    {
70
        Some(pid) if proc_fs => !Path::new("/proc").join(pid.to_string()).exists(),
71
        // Being written this instant, or by something that is not oxy.
72
        _ => old,
73
    }
74
}
75
76
impl Drop for FileLock {
77
    fn drop(&mut self) {
78
        let _ = fs::remove_file(&self.path);
79
    }
80
}
81
82
impl FileLock {
83
    /// Wait up to about five seconds for `path`; `busy` is the error after that.
84
    pub(crate) fn acquire(path: PathBuf, busy: &str) -> Result<FileLock> {
85
        Self::acquire_within(path, busy, std::time::Duration::from_secs(5))
86
    }
87
88
    /// Wait up to `wait` for `path`; `busy` is the error after that.
89
    pub(crate) fn acquire_within(
90
        path: PathBuf,
91
        busy: &str,
92
        wait: std::time::Duration,
93
    ) -> Result<FileLock> {
94
        let until = std::time::Instant::now() + wait;
95
        loop {
96
            match fs::OpenOptions::new()
97
                .write(true)
98
                .create_new(true)
99
                .open(&path)
100
            {
101
                Ok(mut f) => {
102
                    let _ = write!(f, "{}", std::process::id());
103
                    return Ok(FileLock { path });
104
                }
105
                Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
106
                    if lock_is_stale(&path) {
107
                        let _ = fs::remove_file(&path);
108
                        continue;
109
                    }
110
                    if std::time::Instant::now() >= until {
111
                        return fail(busy);
112
                    }
113
                    std::thread::sleep(std::time::Duration::from_millis(10));
114
                }
115
                Err(e) => return Err(e.into()),
116
            }
117
        }
118
    }
119
}
120
121
/// Parse a secondary workspace's `.oxy` file.
122
pub(crate) fn parse_workspace_file(text: &str) -> Option<(PathBuf, WorkspaceId)> {
123
    let mut dir = None;
124
    let mut id = None;
125
    for line in text.lines() {
126
        if let Some(v) = line.strip_prefix("oxydir:") {
127
            dir = Some(PathBuf::from(v.trim()));
128
        } else if let Some(v) = line.strip_prefix("workspace:") {
129
            id = WorkspaceId::from_hex(v.trim());
130
        }
131
    }
132
    Some((dir?, id?))
133
}
134
135
impl Repo {
136
    fn workspace_dir(&self, id: &WorkspaceId) -> PathBuf {
137
        self.oxy.join("workspaces").join(id.to_hex())
138
    }
139
140
    /// Record where a workspace lives.
141
    pub(crate) fn register_workspace(&self, id: &WorkspaceId, path: &Path) -> Result<()> {
142
        let dir = self.workspace_dir(id);
143
        fs::create_dir_all(&dir)?;
144
        write_atomic(&dir.join("path"), path.to_string_lossy().as_bytes())
145
    }
146
147
    /// The directory of a workspace, if registered.
148
    pub fn workspace_path(&self, id: &WorkspaceId) -> Option<PathBuf> {
149
        fs::read_to_string(self.workspace_dir(id).join("path"))
150
            .ok()
151
            .map(|s| PathBuf::from(s.trim()))
152
    }
153
154
    /// Every workspace this repository knows about.
155
    pub fn workspaces(&self) -> Result<Vec<WorkspaceInfo>> {
156
        let view = self.view()?;
157
        let mut ids: BTreeSet<WorkspaceId> = view.workspaces.keys().copied().collect();
158
        if let Ok(rd) = fs::read_dir(self.oxy.join("workspaces")) {
159
            for e in rd.flatten() {
160
                if let Some(id) = WorkspaceId::from_hex(&e.file_name().to_string_lossy()) {
161
                    if e.path().join("path").exists() {
162
                        ids.insert(id);
163
                    }
164
                }
165
            }
166
        }
167
        let mut out = Vec::new();
168
        for id in ids {
169
            let path = if id == self.workspace {
170
                self.root.clone()
171
            } else {
172
                self.workspace_path(&id)
173
                    .unwrap_or_else(|| PathBuf::from("(unknown)"))
174
            };
175
            out.push(WorkspaceInfo {
176
                id,
177
                path,
178
                working_copy: view.workspaces.get(&id).copied(),
179
                current: id == self.workspace,
180
            });
181
        }
182
        Ok(out)
183
    }
184
185
    /// Create a workspace at `dir` checked out on `heads` (default the
186
    /// bookmark's), and return a `Repo` rooted there.
187
    /// `bookmark` is the new workspace's current bookmark (oxygen#60), by
188
    /// default this workspace's.
189
    pub fn workspace_add(&self, dir: &Path, heads: Option<&[ChangeHash]>) -> Result<Repo> {
190
        self.workspace_add_on(dir, heads, None)
191
    }
192
193
    /// [`Repo::workspace_add`] with the new workspace on `bookmark`.
194
    pub fn workspace_add_on(
195
        &self,
196
        dir: &Path,
197
        heads: Option<&[ChangeHash]>,
198
        bookmark: Option<&str>,
199
    ) -> Result<Repo> {
200
        if dir.exists() && fs::read_dir(dir)?.next().is_some() {
201
            return fail(format!("{} exists and is not empty", dir.display()));
202
        }
203
        fs::create_dir_all(dir)?;
204
        let dir = dir.canonicalize()?;
205
        if dir == self.root || dir.starts_with(&self.root) || self.root.starts_with(&dir) {
206
            return fail("a workspace cannot be inside another workspace of the same repository");
207
        }
208
        let heads = match heads {
209
            Some(h) => h.to_vec(),
210
            None => self.bookmark_heads()?,
211
        };
212
        let id = WorkspaceId::random();
213
        let wc = self.new_working_copy(heads.clone(), "");
214
        let wc_hash = self.store.put_change(&wc)?;
215
        let (op_ref, op) = self.head_op()?;
216
        let mut view = op.view.clone();
217
        view.workspaces.insert(id, wc_hash);
218
        self.record_op(
219
            &[op_ref],
220
            &format!("oxy workspace add {}", dir.display()),
221
            view,
222
        )?;
223
        self.register_workspace(&id, &dir)?;
224
        self.set_workspace_bookmark(&id, bookmark.unwrap_or(&self.config.bookmark))?;
225
        write_atomic(
226
            &dir.join(OXY_DIR),
227
            format!(
228
                "oxydir: {}\nworkspace: {}\n",
229
                self.oxy.display(),
230
                id.to_hex()
231
            )
232
            .as_bytes(),
233
        )?;
234
        let repo = Repo::open(&dir)?.with_home(self.keys_dir.parent().unwrap_or(&self.keys_dir));
235
        repo.checkout(&heads, true, "oxy workspace add (checkout)")?;
236
        self.copy_subrepos_into(&repo);
237
        Ok(repo)
238
    }
239
240
    /// The new workspace's subrepos at their pins, from this workspace's
241
    /// own copies (oxygen#61): a local clone, its remote then pointed at
242
    /// the subrepo's URL, and the pin checked out, with no network unless
243
    /// this copy lacks the pinned changes. A subrepo this workspace does
244
    /// not have stays for `oxy submodule update --init`. Best effort.
245
    fn copy_subrepos_into(&self, new: &Repo) {
246
        let Ok(subs) = new.subrepos() else { return };
247
        let modules = new.modules_on_disk();
248
        for s in subs {
249
            let (src, dst) = (self.root.join(&s.path), new.root.join(&s.path));
250
            if dst.join(OXY_DIR).exists() || dst.join(".git").exists() {
251
                continue;
252
            }
253
            let url = modules.entries.get(&s.path).map(|m| m.url.clone());
254
            if let Some(parent) = dst.parent() {
255
                let _ = fs::create_dir_all(parent);
256
            }
257
            let _ = fs::remove_dir(&dst);
258
            if src.join(OXY_DIR).exists() {
259
                match Repo::clone(&src.to_string_lossy(), &dst) {
260
                    Ok(mut sub) => {
261
                        if let Some(u) = &url {
262
                            if sub.config.set("remote.origin.url", u).is_ok() {
263
                                let _ = sub.save_config();
264
                            }
265
                        }
266
                    }
267
                    Err(_) => continue,
268
                }
269
            } else if src.join(".git").exists() {
270
                let cloned = std::process::Command::new("git")
271
                    .args(["clone", "-q", "--local"])
272
                    .arg(&src)
273
                    .arg(&dst)
274
                    .status()
275
                    .is_ok_and(|st| st.success());
276
                if !cloned {
277
                    continue;
278
                }
279
                if let Some(u) = &url {
280
                    let _ = std::process::Command::new("git")
281
                        .arg("-C")
282
                        .arg(&dst)
283
                        .args(["remote", "set-url", "origin", u])
284
                        .status();
285
                }
286
            } else {
287
                continue;
288
            }
289
            let _ = new.sync_subrepo(&s.path, &s.pin, false);
290
        }
291
    }
292
293
    /// Drop a workspace from the view and registry; its files stay.
294
    pub fn workspace_forget(&self, id: &WorkspaceId) -> Result<()> {
295
        if *id == self.workspace {
296
            return fail("cannot forget the workspace you are in");
297
        }
298
        let (op_ref, op) = self.head_op()?;
299
        let mut view = op.view.clone();
300
        if view.workspaces.remove(id).is_none() && self.workspace_path(id).is_none() {
301
            return fail(format!("no workspace {}", id.short(8)));
302
        }
303
        self.record_op(
304
            &[op_ref],
305
            &format!("oxy workspace forget {}", id.short(8)),
306
            view,
307
        )?;
308
        let _ = fs::remove_dir_all(self.workspace_dir(id));
309
        Ok(())
310
    }
311
312
    /// Take this workspace's snapshot lock (spec §12.1).
313
    pub(crate) fn lock_snapshot(&self) -> Result<SnapshotLock> {
314
        self.lock_working_copy()
315
    }
316
317
    /// Take this workspace's working-copy lock (spec §12.1), waiting up to
318
    /// a minute for another process's checkout or snapshot to finish.
319
    pub fn lock_working_copy(&self) -> Result<WorkingCopyLock> {
320
        let dir = self.workspace_dir(&self.workspace);
321
        if self.bare {
322
            // No working copy to guard.
323
            return Ok(WorkingCopyLock {
324
                path: dir,
325
                _file: None,
326
            });
327
        }
328
        fs::create_dir_all(&dir)?;
329
        let path = dir.join("lock");
330
        let held = HELD.with(|h| {
331
            let mut h = h.borrow_mut();
332
            match h.get_mut(&path) {
333
                Some(n) => {
334
                    *n += 1;
335
                    true
336
                }
337
                None => false,
338
            }
339
        });
340
        if held {
341
            return Ok(WorkingCopyLock { path, _file: None });
342
        }
343
        let file = FileLock::acquire_within(
344
            path.clone(),
345
            "another oxy process is using this working copy (a checkout or snapshot); try again when it finishes",
346
            std::time::Duration::from_secs(60),
347
        )?;
348
        HELD.with(|h| h.borrow_mut().insert(path.clone(), 1));
349
        Ok(WorkingCopyLock {
350
            path,
351
            _file: Some(file),
352
        })
353
    }
354
}