OXYGENHUB
◆ v0.4.1 oxygen / crates / oxy-core / src / repo / subrepos.rs
16.2 KiB · Rust · last changed submodule: name .oxymodules entries that were never added 1 month ago source blame history raw
1
//! Subrepositories in the working copy (spec §7.8): reading pins from
2
//! nested repositories, moving them to pins, and the `oxy submodule`
3
//! commands.
4
5
use super::*;
6
use crate::subrepo::{Module, Modules, Pin};
7
use std::process::Command;
8
9
/// What syncing a nested repository to its pin did.
10
#[derive(Clone, Debug, PartialEq, Eq)]
11
pub enum SubrepoSync {
12
    Updated,
13
    UpToDate,
14
    Missing,
15
    Dirty,
16
    /// The working copy was brought to the pin, but the nested repository's
17
    /// bookmark has changes the pin lacks, so it was left where it is: a
18
    /// bookmark is never moved backwards (spec §7.8).
19
    KeptBookmark {
20
        bookmark: String,
21
        newer: usize,
22
    },
23
    Failed(String),
24
}
25
26
impl SubrepoSync {
27
    /// A note for a checkout report, when there is something to say.
28
    pub fn note(&self, path: &str) -> Option<String> {
29
        match self {
30
            SubrepoSync::Updated | SubrepoSync::UpToDate => None,
31
            SubrepoSync::Missing => Some(format!(
32
                "{path}: nested repository not present; run oxy submodule update --init"
33
            )),
34
            SubrepoSync::Dirty => Some(format!(
35
                "{path}: nested repository has uncommitted changes; left as is"
36
            )),
37
            SubrepoSync::KeptBookmark { bookmark, newer } => Some(format!(
38
                "{path}: checked out at the pin; its bookmark {bookmark} keeps {newer} newer change{} (oxy bookmark switch {bookmark} there to return to them)",
39
                if *newer == 1 { "" } else { "s" }
40
            )),
41
            SubrepoSync::Failed(e) => Some(format!("{path}: {e}")),
42
        }
43
    }
44
}
45
46
/// One subrepo as `oxy submodule status` shows it.
47
#[derive(Clone, Debug)]
48
pub struct SubrepoInfo {
49
    pub path: String,
50
    pub pin: Pin,
51
    pub url: Option<String>,
52
    /// The nested repository's own pin now, if present.
53
    pub actual: Option<Pin>,
54
    pub dirty: bool,
55
    /// Whether the working copy's base records it. An added subrepo
56
    /// (registered in `.oxymodules`, not committed yet) has `pin` set to
57
    /// what the next commit records.
58
    pub committed: bool,
59
}
60
61
fn git_out(dir: &Path, args: &[&str]) -> Result<String> {
62
    let out = Command::new("git")
63
        .arg("-C")
64
        .arg(dir)
65
        .args(args)
66
        .output()
67
        .map_err(|e| RepoError::Msg(format!("running git: {e}")))?;
68
    if !out.status.success() {
69
        return fail(format!(
70
            "git {} in {}: {}",
71
            args.join(" "),
72
            dir.display(),
73
            String::from_utf8_lossy(&out.stderr).trim()
74
        ));
75
    }
76
    Ok(String::from_utf8_lossy(&out.stdout).trim().to_string())
77
}
78
79
/// The pin a nested repository at `dir` is at: the base of its working
80
/// copy for an Oxygen repository (which is its bookmark's heads unless a
81
/// parent checked out an older pin), `HEAD` for a Git one.
82
pub(crate) fn read_pin(dir: &Path, bookmark_hint: Option<&str>) -> Result<Pin> {
83
    if dir.join(OXY_DIR).exists() {
84
        let nested = Repo::open(dir)?;
85
        return Ok(Pin::Oxy {
86
            repo: nested.id()?,
87
            heads: nested.working_copy()?.1.deps,
88
            bookmark: nested.config.bookmark.clone(),
89
        });
90
    }
91
    if dir.join(".git").exists() {
92
        let sha = git_out(dir, &["rev-parse", "HEAD"])?;
93
        return Ok(Pin::Git {
94
            sha,
95
            branch: bookmark_hint.unwrap_or("HEAD").to_string(),
96
        });
97
    }
98
    fail(format!("{} is not a repository", dir.display()))
99
}
100
101
/// Whether a nested repository has uncommitted work.
102
fn nested_dirty(dir: &Path) -> Result<bool> {
103
    if dir.join(OXY_DIR).exists() {
104
        let nested = Repo::open(dir)?;
105
        let snap = nested.snapshot("oxy submodule")?;
106
        return Ok(!snap.wc.tree_ops.is_empty() || !snap.wc.edits.is_empty());
107
    }
108
    Ok(!git_out(dir, &["status", "--porcelain"])?.is_empty())
109
}
110
111
/// Whether `url` is an Oxygen repository (else it is taken to be Git),
112
/// probed with `options`, the credentials a clone of it would use; with
113
/// the probe's error when it is not, to report if Git cannot clone it
114
/// either.
115
fn probe_oxy_url(url: &str, options: &RemoteOptions) -> std::result::Result<(), String> {
116
    if url.starts_with("http://") || url.starts_with("https://") {
117
        return crate::protocol::open_remote(url, options)
118
            .and_then(|r| r.repo_id())
119
            .map(|_| ())
120
            .map_err(|e| e.to_string());
121
    }
122
    let p = Path::new(url.strip_prefix("file://").unwrap_or(url));
123
    if p.join(OXY_DIR).exists() {
124
        Ok(())
125
    } else {
126
        Err(format!("{} has no {OXY_DIR} directory", p.display()))
127
    }
128
}
129
130
impl Repo {
131
    pub(crate) fn modules_on_disk(&self) -> Modules {
132
        fs::read_to_string(self.root.join(".oxymodules"))
133
            .map(|t| Modules::parse(&t))
134
            .unwrap_or_default()
135
    }
136
137
    fn write_modules(&self, m: &Modules) -> Result<()> {
138
        if m.entries.is_empty() {
139
            let _ = fs::remove_file(self.root.join(".oxymodules"));
140
            return Ok(());
141
        }
142
        write_atomic(&self.root.join(".oxymodules"), m.to_text().as_bytes())
143
    }
144
145
    /// Bring the nested repository at `path` to `pin`, cloning it first when
146
    /// `init` and it is absent.
147
    pub(crate) fn sync_subrepo(&self, path: &str, pin: &Pin, init: bool) -> Result<SubrepoSync> {
148
        let dir = self.root.join(path);
149
        let module = self.modules_on_disk().entries.get(path).cloned();
150
        let present = dir.join(OXY_DIR).exists() || dir.join(".git").exists();
151
        let cloned = !present;
152
        if !present {
153
            if !init {
154
                return Ok(SubrepoSync::Missing);
155
            }
156
            let Some(m) = &module else {
157
                return Ok(SubrepoSync::Failed(
158
                    "no .oxymodules entry to clone from".into(),
159
                ));
160
            };
161
            if let Some(parent) = dir.parent() {
162
                fs::create_dir_all(parent)?;
163
            }
164
            let _ = fs::remove_dir(&dir);
165
            let r = match pin {
166
                Pin::Oxy { .. } => self.clone_as_subrepo(&m.url, &dir).map(|_| ()),
167
                Pin::Git { .. } => Command::new("git")
168
                    .args(["clone", "-q", &m.url])
169
                    .arg(&dir)
170
                    .status()
171
                    .map_err(|e| RepoError::Msg(e.to_string()))
172
                    .and_then(|s| {
173
                        if s.success() {
174
                            Ok(())
175
                        } else {
176
                            fail(format!("git clone {} failed", m.url))
177
                        }
178
                    }),
179
            };
180
            if let Err(e) = r {
181
                return Ok(SubrepoSync::Failed(format!("cloning {}: {e}", m.url)));
182
            }
183
        }
184
        let actual = read_pin(&dir, module.as_ref().and_then(|m| m.bookmark.as_deref()))?;
185
        let same = match (pin, &actual) {
186
            (Pin::Oxy { heads: a, .. }, Pin::Oxy { heads: b, .. }) => {
187
                let (mut a, mut b) = (a.clone(), b.clone());
188
                a.sort();
189
                b.sort();
190
                a == b
191
            }
192
            (Pin::Git { sha: a, .. }, Pin::Git { sha: b, .. }) => a == b,
193
            _ => false,
194
        };
195
        if same {
196
            return Ok(if cloned {
197
                SubrepoSync::Updated
198
            } else {
199
                SubrepoSync::UpToDate
200
            });
201
        }
202
        if nested_dirty(&dir)? {
203
            return Ok(SubrepoSync::Dirty);
204
        }
205
        match pin {
206
            Pin::Oxy { heads, .. } => {
207
                let nested = Repo::open(&dir)?;
208
                if nested.load_state(heads).is_err() {
209
                    // The pinned changes are not here yet: fetch from origin.
210
                    if let Err(e) = nested.fetch("origin") {
211
                        return Ok(SubrepoSync::Failed(format!(
212
                            "fetching the pinned changes: {e}"
213
                        )));
214
                    }
215
                }
216
                match nested.checkout(heads, false, "oxy submodule update") {
217
                    Ok(_) => {
218
                        // Its bookmark follows only forward: work it has
219
                        // beyond the pin stays on it (metalgear, oxygen#38).
220
                        let bookmark = nested.config.bookmark.clone();
221
                        let at = nested.bookmark_heads().unwrap_or_default();
222
                        let newer = nested.state_gap(&at, heads).map(|g| g.lacks).unwrap_or(0);
223
                        if newer == 0 {
224
                            let _ = nested.bookmark_set(&bookmark, heads);
225
                            Ok(SubrepoSync::Updated)
226
                        } else {
227
                            Ok(SubrepoSync::KeptBookmark { bookmark, newer })
228
                        }
229
                    }
230
                    Err(e) => Ok(SubrepoSync::Failed(e.to_string())),
231
                }
232
            }
233
            Pin::Git { sha, .. } => {
234
                if git_out(&dir, &["cat-file", "-e", &format!("{sha}^{{commit}}")]).is_err() {
235
                    if let Err(e) = git_out(&dir, &["fetch", "-q", "origin"]) {
236
                        return Ok(SubrepoSync::Failed(e.to_string()));
237
                    }
238
                }
239
                match git_out(&dir, &["checkout", "-q", "--detach", sha]) {
240
                    Ok(_) => Ok(SubrepoSync::Updated),
241
                    Err(e) => Ok(SubrepoSync::Failed(e.to_string())),
242
                }
243
            }
244
        }
245
    }
246
247
    /// Every subrepo of the working copy's base state.
248
    pub fn subrepos(&self) -> Result<Vec<SubrepoInfo>> {
249
        let (_, wc) = self.working_copy()?;
250
        let snap = self.load_state(&wc.deps)?.materialize();
251
        let modules = self.modules_on_disk();
252
        let mut out = Vec::new();
253
        for (path, f) in &snap.files {
254
            if f.kind != NodeKind::Subrepo {
255
                continue;
256
            }
257
            let FileContent::Opaque { blobs } = &f.content else {
258
                continue;
259
            };
260
            let Some(b) = blobs.first() else { continue };
261
            let pin = Pin::decode(&self.read_blob(b)?)?;
262
            let dir = self.root.join(path);
263
            let present = dir.join(OXY_DIR).exists() || dir.join(".git").exists();
264
            let actual = if present {
265
                read_pin(
266
                    &dir,
267
                    modules
268
                        .entries
269
                        .get(path)
270
                        .and_then(|m| m.bookmark.as_deref()),
271
                )
272
                .ok()
273
            } else {
274
                None
275
            };
276
            let dirty = present && nested_dirty(&dir).unwrap_or(false);
277
            out.push(SubrepoInfo {
278
                path: path.clone(),
279
                pin,
280
                url: modules.entries.get(path).map(|m| m.url.clone()),
281
                actual,
282
                dirty,
283
                committed: true,
284
            });
285
        }
286
        // Registered but not committed yet: pinned at what they are now.
287
        for (path, m) in &modules.entries {
288
            if snap.files.contains_key(path) {
289
                continue;
290
            }
291
            let dir = self.root.join(path);
292
            let Ok(pin) = read_pin(&dir, m.bookmark.as_deref()) else {
293
                continue;
294
            };
295
            out.push(SubrepoInfo {
296
                path: path.clone(),
297
                pin: pin.clone(),
298
                url: Some(m.url.clone()),
299
                actual: Some(pin),
300
                dirty: nested_dirty(&dir).unwrap_or(false),
301
                committed: false,
302
            });
303
        }
304
        out.sort_by(|a, b| a.path.cmp(&b.path));
305
        Ok(out)
306
    }
307
308
    /// `.oxymodules` entries with no subrepo behind them: never added, so
309
    /// there is no pin to clone or check out (oxygen#72).
310
    pub fn unpinned_modules(&self) -> Result<Vec<(String, Module)>> {
311
        let pinned: Vec<String> = self.subrepos()?.into_iter().map(|s| s.path).collect();
312
        Ok(self
313
            .modules_on_disk()
314
            .entries
315
            .into_iter()
316
            .filter(|(path, _)| !pinned.contains(path))
317
            .collect())
318
    }
319
320
    /// Clone `url` at `path`, register it in `.oxymodules`, and return the
321
    /// pin the next snapshot will record.
322
    pub fn submodule_add(&self, url: &str, path: &str, bookmark: Option<&str>) -> Result<Pin> {
323
        let path = path.trim_end_matches('/').to_string();
324
        if path.is_empty()
325
            || path.starts_with('/')
326
            || path.split('/').any(|c| c == ".." || c == ".")
327
        {
328
            return fail(format!("{path:?} is not a valid subrepo path"));
329
        }
330
        let dir = self.root.join(&path);
331
        if dir.exists() && fs::read_dir(&dir)?.next().is_some() {
332
            return fail(format!("{path} exists and is not empty"));
333
        }
334
        if let Some(parent) = dir.parent() {
335
            fs::create_dir_all(parent)?;
336
        }
337
        let probe = probe_oxy_url(url, &self.credentials_for(url));
338
        let git = probe.is_err();
339
        if git {
340
            let mut cmd = Command::new("git");
341
            cmd.args(["clone", "-q"]);
342
            if let Some(b) = bookmark {
343
                cmd.args(["--branch", b]);
344
            }
345
            let status = cmd
346
                .arg(url)
347
                .arg(&dir)
348
                .status()
349
                .map_err(|e| RepoError::Msg(format!("running git: {e}")))?;
350
            if !status.success() {
351
                let oxy_error = probe.err().unwrap_or_default();
352
                return fail(format!(
353
                    "git clone {url} failed; as an Oxygen repository: {oxy_error}"
354
                ));
355
            }
356
        } else {
357
            let _ = fs::remove_dir(&dir);
358
            let mut nested = self.clone_as_subrepo(url, &dir)?;
359
            if let Some(b) = bookmark {
360
                nested.bookmark_switch(b, true)?;
361
            }
362
        }
363
        let mut modules = self.modules_on_disk();
364
        modules.entries.insert(
365
            path.clone(),
366
            Module {
367
                url: url.to_string(),
368
                bookmark: bookmark.map(str::to_string),
369
                git,
370
            },
371
        );
372
        self.write_modules(&modules)?;
373
        read_pin(&dir, bookmark)
374
    }
375
376
    /// Bring nested repositories to their pins (`paths` empty = all).
377
    pub fn submodule_update(
378
        &self,
379
        init: bool,
380
        paths: &[String],
381
    ) -> Result<Vec<(String, SubrepoSync)>> {
382
        let mut out = Vec::new();
383
        for s in self.subrepos()? {
384
            if !paths.is_empty() && !paths.iter().any(|p| p.trim_end_matches('/') == s.path) {
385
                continue;
386
            }
387
            let r = self.sync_subrepo(&s.path, &s.pin, init)?;
388
            out.push((s.path, r));
389
        }
390
        Ok(out)
391
    }
392
393
    /// Drop the `.oxymodules` entry; the node goes with the next snapshot
394
    /// only if the directory is removed, since a registered or tracked
395
    /// nested repository keeps contributing its pin. Here the directory is
396
    /// left in place and the node is removed explicitly from the working
397
    /// copy's next change by unregistering and moving the directory aside.
398
    pub fn submodule_remove(&self, path: &str) -> Result<()> {
399
        let path = path.trim_end_matches('/').to_string();
400
        let mut modules = self.modules_on_disk();
401
        let known = modules.entries.remove(&path).is_some()
402
            || self.subrepos()?.iter().any(|s| s.path == path);
403
        if !known {
404
            return fail(format!("{path} is not a subrepo"));
405
        }
406
        self.write_modules(&modules)?;
407
        // The directory stays; the walker ignores an unregistered nested
408
        // repository unless tracked, so mark it as removed for the next
409
        // snapshot by recording an explicit removal.
410
        let dir = self.oxy.join("subrepos-removed");
411
        fs::create_dir_all(&dir)?;
412
        write_atomic(&dir.join(path.replace('/', "%2F")), b"")?;
413
        Ok(())
414
    }
415
416
    /// Paths whose subrepo node was removed with `oxy submodule remove` and
417
    /// whose directory is still on disk.
418
    pub(crate) fn removed_subrepos(&self) -> BTreeSet<String> {
419
        let mut out = BTreeSet::new();
420
        if let Ok(rd) = fs::read_dir(self.oxy.join("subrepos-removed")) {
421
            for e in rd.flatten() {
422
                out.insert(e.file_name().to_string_lossy().replace("%2F", "/"));
423
            }
424
        }
425
        out
426
    }
427
428
    pub(crate) fn clear_removed_subrepo(&self, path: &str) {
429
        let _ = fs::remove_file(
430
            self.oxy
431
                .join("subrepos-removed")
432
                .join(path.replace('/', "%2F")),
433
        );
434
    }
435
}