OXYGENHUB

Coverage / crates/oxy-core/src/repo/workspace.rs 86.6%

220 of 254 lines run, 37 of 54 functions · run #251 on main · run missed

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 {
452423×
    fn drop(&mut self) {
462423×
        HELD.with(|h| {
472423×
            let mut h = h.borrow_mut();
482423×
            if let Some(n) = h.get_mut(&self.path) {
492422×
                *n -= 1;
502422×
                if *n == 0 {
511459×
                    h.remove(&self.path);
521459×
                }
531×
            }
542423×
        });
552423×
    }
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.
6088×
pub(crate) fn lock_is_stale(path: &Path) -> bool {
6188×
    let old = fs::metadata(path)
6288×
        .and_then(|m| m.modified())
6388×
        .map(|t| t.elapsed().map(|d| d.as_secs() > 60).unwrap_or(false))
6488×
        .unwrap_or(true);
6588×
    let proc_fs = Path::new("/proc/self").exists();
6688×
    match fs::read_to_string(path)
6788×
        .ok()
6888×
        .and_then(|s| s.trim().parse::<u32>().ok())
69
    {
7088×
        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.
720×
        _ => old,
73
    }
7488×
}
75
76
impl Drop for FileLock {
774550×
    fn drop(&mut self) {
784550×
        let _ = fs::remove_file(&self.path);
794550×
    }
80
}
81
82
impl FileLock {
83
    /// Wait up to about five seconds for `path`; `busy` is the error after that.
843091×
    pub(crate) fn acquire(path: PathBuf, busy: &str) -> Result<FileLock> {
853091×
        Self::acquire_within(path, busy, std::time::Duration::from_secs(5))
863091×
    }
87
88
    /// Wait up to `wait` for `path`; `busy` is the error after that.
894550×
    pub(crate) fn acquire_within(
904550×
        path: PathBuf,
914550×
        busy: &str,
924550×
        wait: std::time::Duration,
934550×
    ) -> Result<FileLock> {
944550×
        let until = std::time::Instant::now() + wait;
95
        loop {
964637×
            match fs::OpenOptions::new()
974637×
                .write(true)
984637×
                .create_new(true)
994637×
                .open(&path)
100
            {
1014550×
                Ok(mut f) => {
1024550×
                    let _ = write!(f, "{}", std::process::id());
1034550×
                    return Ok(FileLock { path });
104
                }
10587×
                Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
10687×
                    if lock_is_stale(&path) {
1071×
                        let _ = fs::remove_file(&path);
1081×
                        continue;
10986×
                    }
11086×
                    if std::time::Instant::now() >= until {
1110×
                        return fail(busy);
11286×
                    }
11386×
                    std::thread::sleep(std::time::Duration::from_millis(10));
114
                }
1150×
                Err(e) => return Err(e.into()),
116
            }
117
        }
1184550×
    }
119
}
120
121
/// Parse a secondary workspace's `.oxy` file.
1226×
pub(crate) fn parse_workspace_file(text: &str) -> Option<(PathBuf, WorkspaceId)> {
1236×
    let mut dir = None;
1246×
    let mut id = None;
12512×
    for line in text.lines() {
12612×
        if let Some(v) = line.strip_prefix("oxydir:") {
1276×
            dir = Some(PathBuf::from(v.trim()));
1286×
        } else if let Some(v) = line.strip_prefix("workspace:") {
1296×
            id = WorkspaceId::from_hex(v.trim());
1306×
        }
131
    }
1326×
    Some((dir?, id?))
1336×
}
134
135
impl Repo {
1362860×
    fn workspace_dir(&self, id: &WorkspaceId) -> PathBuf {
1372860×
        self.oxy.join("workspaces").join(id.to_hex())
1382860×
    }
139
140
    /// Record where a workspace lives.
141433×
    pub(crate) fn register_workspace(&self, id: &WorkspaceId, path: &Path) -> Result<()> {
142433×
        let dir = self.workspace_dir(id);
143433×
        fs::create_dir_all(&dir)?;
144433×
        write_atomic(&dir.join("path"), path.to_string_lossy().as_bytes())
145433×
    }
146
147
    /// The directory of a workspace, if registered.
1483×
    pub fn workspace_path(&self, id: &WorkspaceId) -> Option<PathBuf> {
1493×
        fs::read_to_string(self.workspace_dir(id).join("path"))
1503×
            .ok()
1513×
            .map(|s| PathBuf::from(s.trim()))
1523×
    }
153
154
    /// Every workspace this repository knows about.
1556×
    pub fn workspaces(&self) -> Result<Vec<WorkspaceInfo>> {
1566×
        let view = self.view()?;
1576×
        let mut ids: BTreeSet<WorkspaceId> = view.workspaces.keys().copied().collect();
1586×
        if let Ok(rd) = fs::read_dir(self.oxy.join("workspaces")) {
1599×
            for e in rd.flatten() {
1609×
                if let Some(id) = WorkspaceId::from_hex(&e.file_name().to_string_lossy()) {
1619×
                    if e.path().join("path").exists() {
1629×
                        ids.insert(id);
1639×
                    }
1640×
                }
165
            }
1660×
        }
1676×
        let mut out = Vec::new();
1689×
        for id in ids {
1699×
            let path = if id == self.workspace {
1706×
                self.root.clone()
171
            } else {
1723×
                self.workspace_path(&id)
1733×
                    .unwrap_or_else(|| PathBuf::from("(unknown)"))
174
            };
1759×
            out.push(WorkspaceInfo {
1769×
                id,
1779×
                path,
1789×
                working_copy: view.workspaces.get(&id).copied(),
1799×
                current: id == self.workspace,
1809×
            });
181
        }
1826×
        Ok(out)
1836×
    }
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.
1894×
    pub fn workspace_add(&self, dir: &Path, heads: Option<&[ChangeHash]>) -> Result<Repo> {
1904×
        self.workspace_add_on(dir, heads, None)
1914×
    }
192
193
    /// [`Repo::workspace_add`] with the new workspace on `bookmark`.
1945×
    pub fn workspace_add_on(
1955×
        &self,
1965×
        dir: &Path,
1975×
        heads: Option<&[ChangeHash]>,
1985×
        bookmark: Option<&str>,
1995×
    ) -> Result<Repo> {
2005×
        if dir.exists() && fs::read_dir(dir)?.next().is_some() {
2010×
            return fail(format!("{} exists and is not empty", dir.display()));
2025×
        }
2035×
        fs::create_dir_all(dir)?;
2045×
        let dir = dir.canonicalize()?;
2055×
        if dir == self.root || dir.starts_with(&self.root) || self.root.starts_with(&dir) {
2061×
            return fail("a workspace cannot be inside another workspace of the same repository");
2074×
        }
2084×
        let heads = match heads {
2091×
            Some(h) => h.to_vec(),
2103×
            None => self.bookmark_heads()?,
211
        };
2124×
        let id = WorkspaceId::random();
2134×
        let wc = self.new_working_copy(heads.clone(), "");
2144×
        let wc_hash = self.store.put_change(&wc)?;
2154×
        let (op_ref, op) = self.head_op()?;
2164×
        let mut view = op.view.clone();
2174×
        view.workspaces.insert(id, wc_hash);
2184×
        self.record_op(
2194×
            &[op_ref],
2204×
            &format!("oxy workspace add {}", dir.display()),
2214×
            view,
2220×
        )?;
2234×
        self.register_workspace(&id, &dir)?;
2244×
        self.set_workspace_bookmark(&id, bookmark.unwrap_or(&self.config.bookmark))?;
2254×
        write_atomic(
2264×
            &dir.join(OXY_DIR),
2274×
            format!(
2284×
                "oxydir: {}\nworkspace: {}\n",
2294×
                self.oxy.display(),
2304×
                id.to_hex()
2314×
            )
2324×
            .as_bytes(),
2330×
        )?;
2344×
        let repo = Repo::open(&dir)?.with_home(self.keys_dir.parent().unwrap_or(&self.keys_dir));
2354×
        repo.checkout(&heads, true, "oxy workspace add (checkout)")?;
2364×
        self.copy_subrepos_into(&repo);
2374×
        Ok(repo)
2385×
    }
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.
2454×
    fn copy_subrepos_into(&self, new: &Repo) {
2464×
        let Ok(subs) = new.subrepos() else { return };
2474×
        let modules = new.modules_on_disk();
2484×
        for s in subs {
2491×
            let (src, dst) = (self.root.join(&s.path), new.root.join(&s.path));
2501×
            if dst.join(OXY_DIR).exists() || dst.join(".git").exists() {
2510×
                continue;
2521×
            }
2531×
            let url = modules.entries.get(&s.path).map(|m| m.url.clone());
2541×
            if let Some(parent) = dst.parent() {
2551×
                let _ = fs::create_dir_all(parent);
2561×
            }
2571×
            let _ = fs::remove_dir(&dst);
2581×
            if src.join(OXY_DIR).exists() {
2591×
                match Repo::clone(&src.to_string_lossy(), &dst) {
2601×
                    Ok(mut sub) => {
2611×
                        if let Some(u) = &url {
2621×
                            if sub.config.set("remote.origin.url", u).is_ok() {
2631×
                                let _ = sub.save_config();
2641×
                            }
2650×
                        }
266
                    }
2670×
                    Err(_) => continue,
268
                }
2690×
            } else if src.join(".git").exists() {
2700×
                let cloned = std::process::Command::new("git")
2710×
                    .args(["clone", "-q", "--local"])
2720×
                    .arg(&src)
2730×
                    .arg(&dst)
2740×
                    .status()
2750×
                    .is_ok_and(|st| st.success());
2760×
                if !cloned {
2770×
                    continue;
2780×
                }
2790×
                if let Some(u) = &url {
2800×
                    let _ = std::process::Command::new("git")
2810×
                        .arg("-C")
2820×
                        .arg(&dst)
2830×
                        .args(["remote", "set-url", "origin", u])
2840×
                        .status();
2850×
                }
286
            } else {
2870×
                continue;
288
            }
2891×
            let _ = new.sync_subrepo(&s.path, &s.pin, false);
290
        }
2914×
    }
292
293
    /// Drop a workspace from the view and registry; its files stay.
2942×
    pub fn workspace_forget(&self, id: &WorkspaceId) -> Result<()> {
2952×
        if *id == self.workspace {
2961×
            return fail("cannot forget the workspace you are in");
2971×
        }
2981×
        let (op_ref, op) = self.head_op()?;
2991×
        let mut view = op.view.clone();
3001×
        if view.workspaces.remove(id).is_none() && self.workspace_path(id).is_none() {
3010×
            return fail(format!("no workspace {}", id.short(8)));
3021×
        }
3031×
        self.record_op(
3041×
            &[op_ref],
3051×
            &format!("oxy workspace forget {}", id.short(8)),
3061×
            view,
3070×
        )?;
3081×
        let _ = fs::remove_dir_all(self.workspace_dir(id));
3091×
        Ok(())
3102×
    }
311
312
    /// Take this workspace's snapshot lock (spec §12.1).
3131171×
    pub(crate) fn lock_snapshot(&self) -> Result<SnapshotLock> {
3141171×
        self.lock_working_copy()
3151171×
    }
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.
3192423×
    pub fn lock_working_copy(&self) -> Result<WorkingCopyLock> {
3202423×
        let dir = self.workspace_dir(&self.workspace);
3212423×
        if self.bare {
322
            // No working copy to guard.
3231×
            return Ok(WorkingCopyLock {
3241×
                path: dir,
3251×
                _file: None,
3261×
            });
3272422×
        }
3282422×
        fs::create_dir_all(&dir)?;
3292422×
        let path = dir.join("lock");
3302422×
        let held = HELD.with(|h| {
3312422×
            let mut h = h.borrow_mut();
3322422×
            match h.get_mut(&path) {
333963×
                Some(n) => {
334963×
                    *n += 1;
335963×
                    true
336
                }
3371459×
                None => false,
338
            }
3392422×
        });
3402422×
        if held {
341963×
            return Ok(WorkingCopyLock { path, _file: None });
3421459×
        }
3431459×
        let file = FileLock::acquire_within(
3441459×
            path.clone(),
3451459×
            "another oxy process is using this working copy (a checkout or snapshot); try again when it finishes",
3461459×
            std::time::Duration::from_secs(60),
3470×
        )?;
3481459×
        HELD.with(|h| h.borrow_mut().insert(path.clone(), 1));
3491459×
        Ok(WorkingCopyLock {
3501459×
            path,
3511459×
            _file: Some(file),
3521459×
        })
3532423×
    }
354
}