pijul 1.0.0-beta.12

A distributed version control system.
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use std::collections::{HashMap, HashSet};
use std::io::BufWriter;
use std::io::{BufRead, Read, Write};
use std::path::{Path, PathBuf};
use std::sync::{Arc, LazyLock};

use crate::commands::common_opts::RepoPath;
use crate::commands::load_channel_exact;
use anyhow::bail;
use byteorder::{BigEndian, WriteBytesExt};
use clap::Parser;
use log::{debug, error, warn};
use pijul_core::*;
use pijul_repository::Repository;
use regex::Regex;

/// This command is not meant to be run by the user,
/// instead it is called over SSH
#[derive(Parser, Debug)]
pub struct Protocol {
    #[clap(flatten)]
    base: RepoPath,
    /// Use this protocol version
    #[clap(long = "version")]
    version: usize,
}

static APPLY: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r#"apply\s+(\S+)\s+([^ ]*) ([0-9]+)\s+"#).unwrap());
static ARCHIVE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r#"archive\s+(\S+)\s*(( ([^:]+))*)( :(.*))?\n"#).unwrap());
static CHANGE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r#"((change)|(partial))\s+([^ ]*)\s+"#).unwrap());
static CHANGELIST_PATHS: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r#""(((\\")|[^"])+)""#).unwrap());
static CHANGELIST: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r#"changelist\s+(\S+)\s+([0-9]+)(.*)\s+"#).unwrap());
// static CHANNEL: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#"channel\s+(\S+)\s+"#).unwrap());
static ID: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#"id\s+(\S+)\s+"#).unwrap());
static IDENTITIES: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r#"identities(\s+([0-9]+))?\s+"#).unwrap());
static STATE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r#"state\s+(\S+)(\s+([0-9]+)?)\s+"#).unwrap());
static TAG: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#"^tag\s+(\S+)\s+"#).unwrap());
static TAGUP: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r#"^tagup\s+(\S+)\s+(\S+)\s+([0-9]+)\s+"#).unwrap());

const PARTIAL_CHANGE_SIZE: u64 = 1 << 20;

type W = pijul_core::working_copy::filesystem::FileSystem;

impl Protocol {
    pub fn repository_path(&self) -> Option<&Path> {
        self.base.repo_path()
    }

    pub fn run(self) -> Result<(), anyhow::Error> {
        let mut repo = Repository::find_root(self.base.repo_path())?;
        let pristine = Arc::new(repo.pristine);
        let txn = pristine.arc_txn_begin()?;
        let mut ws = pijul_core::ApplyWorkspace::new();
        let mut buf = String::new();
        let mut buf2 = vec![0; 4096 * 10];
        let s = std::io::stdin();
        let mut s = s.lock();
        let o = std::io::stdout();
        let mut o = BufWriter::new(o.lock());
        let mut applied = HashMap::new();

        debug!("reading");
        while s.read_line(&mut buf)? > 0 {
            debug!("{:?}", buf);
            if let Some(cap) = ID.captures(&buf) {
                let channel = load_channel_exact(&cap[1], &*txn.read())?;
                let c = channel.read();
                writeln!(o, "{}", c.id)?;
                o.flush()?;
            } else if let Some(cap) = STATE.captures(&buf) {
                let channel = load_channel_exact(&cap[1], &*txn.read())?;
                let init = if let Some(u) = cap.get(3) {
                    u.as_str().parse().ok()
                } else {
                    None
                };
                if let Some(pos) = init {
                    let txn = txn.read();
                    for x in txn.log(&*channel.read(), pos)? {
                        let (n, (_, m)) = x?;
                        match n.cmp(&pos) {
                            std::cmp::Ordering::Less => continue,
                            std::cmp::Ordering::Greater => {
                                writeln!(o, "-")?;
                                break;
                            }
                            std::cmp::Ordering::Equal => {
                                let m: pijul_core::Merkle = m.into();
                                let m2 = if let Some(x) = txn
                                    .rev_iter_tags(txn.tags(&*channel.read()), Some(n))?
                                    .next()
                                {
                                    x?.1.b.into()
                                } else {
                                    Merkle::zero()
                                };
                                writeln!(o, "{} {} {}", n, m.to_base32(), m2.to_base32())?;
                                break;
                            }
                        }
                    }
                } else {
                    let txn = txn.read();
                    if let Some(x) = txn.reverse_log(&*channel.read(), None)?.next() {
                        let (n, (_, m)) = x?;
                        let m: Merkle = m.into();
                        let m2 = if let Some(x) = txn
                            .rev_iter_tags(txn.tags(&*channel.read()), Some(n))?
                            .next()
                        {
                            x?.1.b.into()
                        } else {
                            Merkle::zero()
                        };
                        writeln!(o, "{} {} {}", n, m.to_base32(), m2.to_base32())?
                    } else {
                        writeln!(o, "-")?;
                    }
                }
                o.flush()?;
            } else if let Some(cap) = CHANGELIST.captures(&buf) {
                let channel = load_channel_exact(&cap[1], &*txn.read())?;
                let from: u64 = cap[2].parse().unwrap();
                let mut paths = Vec::new();
                let txn = txn.read();
                {
                    for r in CHANGELIST_PATHS.captures_iter(&cap[3]) {
                        let s: String = r[1].replace("\\\"", "\"");
                        if let Ok((p, ambiguous)) =
                            txn.follow_oldest_path(&repo.changes, &channel, &s)
                        {
                            if ambiguous {
                                bail!("Ambiguous path")
                            }
                            let h: pijul_core::Hash = txn.get_external(&p.change)?.unwrap().into();
                            writeln!(o, "{}.{}", h.to_base32(), p.pos.0)?;
                            paths.push(s);
                        } else {
                            debug!("protocol line: {:?}", buf);
                            bail!("Protocol error")
                        }
                    }
                }
                let mut tagsi = 0;
                (pijul_remote::local::Local {
                    channel: (&cap[1]).to_string(),
                    root: PathBuf::new(),
                    changes_dir: PathBuf::new(),
                    pristine: pristine.clone(),
                    name: String::new(),
                })
                .download_changelist_(
                    |_, n, h, m, is_tag| {
                        if is_tag {
                            writeln!(o, "{}.{}.{}.", n, h.to_base32(), m.to_base32())?;
                            tagsi += 1;
                        } else {
                            writeln!(o, "{}.{}.{}", n, h.to_base32(), m.to_base32())?;
                        }
                        Ok(())
                    },
                    &mut (),
                    from,
                    &paths,
                    &*txn,
                    &channel,
                )?;
                writeln!(o)?;
                o.flush()?;
            } else if let Some(cap) = TAG.captures(&buf) {
                if let Some(state) = Merkle::from_base32(cap[1].as_bytes()) {
                    let mut tag_path = repo.changes_dir.clone();
                    pijul_core::changestore::filesystem::push_tag_filename(&mut tag_path, &state);
                    let mut tag = pijul_core::tag::OpenTagFile::open(&tag_path, &state)?;
                    let mut buf = Vec::new();
                    tag.short(&mut buf)?;
                    o.write_u64::<BigEndian>(buf.len() as u64)?;
                    o.write_all(&buf)?;
                    o.flush()?;
                }
            } else if let Some(cap) = TAGUP.captures(&buf) {
                if let Some(state) = Merkle::from_base32(cap[1].as_bytes()) {
                    let channel = load_channel_exact(&cap[2], &*txn.read())?;
                    let m = pijul_core::pristine::current_state(&*txn.read(), &*channel.read())?;
                    if m == state {
                        let mut tag_path = repo.changes_dir.clone();
                        pijul_core::changestore::filesystem::push_tag_filename(&mut tag_path, &m);
                        if std::fs::metadata(&tag_path).is_ok() {
                            bail!("Tag for state {} already exists", m.to_base32());
                        }

                        let last_t = if let Some(n) =
                            txn.read().reverse_log(&*channel.read(), None)?.next()
                        {
                            n?.0.into()
                        } else {
                            bail!("Channel {} is empty", &cap[2]);
                        };
                        if txn.read().is_tagged(&channel.read().tags, last_t)? {
                            bail!("Current state is already tagged")
                        }

                        let size: usize = cap[3].parse().unwrap();
                        let mut buf = vec![0; size];
                        s.read_exact(&mut buf)?;

                        let header =
                            pijul_core::tag::read_short(std::io::Cursor::new(&buf[..]), &m)?;

                        let temp_path = tag_path.with_extension("tmp");

                        std::fs::create_dir_all(temp_path.parent().unwrap())?;
                        let mut w = std::fs::File::create(&temp_path)?;
                        pijul_core::tag::from_channel(&*txn.read(), &cap[2], &header, &mut w)?;

                        std::fs::rename(&temp_path, &tag_path)?;
                        txn.write()
                            .put_tags(&mut channel.write().tags, last_t.into(), &m)?;
                    } else {
                        bail!("Wrong state, cannot tag")
                    }
                }
            } else if let Some(cap) = CHANGE.captures(&buf) {
                let h_ = &cap[4];
                let h = if let Some(h) = Hash::from_base32(h_.as_bytes()) {
                    h
                } else {
                    debug!("protocol error: {:?}", buf);
                    bail!("Protocol error")
                };
                pijul_core::changestore::filesystem::push_filename(&mut repo.changes_dir, &h);
                debug!("repo = {:?}", repo.changes_dir);
                let mut f = std::fs::File::open(&repo.changes_dir)?;
                let size = std::fs::metadata(&repo.changes_dir)?.len();
                let size = if &cap[1] == "change" || size <= PARTIAL_CHANGE_SIZE {
                    size
                } else {
                    pijul_core::change::Change::size_no_contents(&mut f)?
                };
                o.write_u64::<BigEndian>(size)?;
                let mut size = size as usize;
                while size > 0 {
                    if size < buf2.len() {
                        buf2.truncate(size as usize);
                    }
                    let n = f.read(&mut buf2[..])?;
                    if n == 0 {
                        break;
                    }
                    size -= n;
                    o.write_all(&buf2[..n])?;
                }
                o.flush()?;
                pijul_core::changestore::filesystem::pop_filename(&mut repo.changes_dir);
            } else if let Some(cap) = APPLY.captures(&buf) {
                let h = if let Some(h) = Hash::from_base32(cap[2].as_bytes()) {
                    h
                } else {
                    debug!("protocol error {:?}", buf);
                    bail!("Protocol error");
                };
                let mut path = repo.changes_dir.clone();
                pijul_core::changestore::filesystem::push_filename(&mut path, &h);
                std::fs::create_dir_all(path.parent().unwrap())?;
                let size: usize = cap[3].parse().unwrap();
                buf2.resize(size, 0);
                s.read_exact(&mut buf2)?;
                std::fs::write(&path, &buf2)?;
                pijul_core::change::Change::deserialize(&path.to_string_lossy(), Some(&h))?;
                let channel = load_channel_exact(&cap[1], &*txn.read())?;
                {
                    let mut channel_ = channel.write();
                    txn.write()
                        .apply_change_ws(&repo.changes, &mut channel_, &h, &mut ws)?;
                }
                applied.insert(cap[1].to_string(), channel);
            } else if let Some(cap) = ARCHIVE.captures(&buf) {
                let mut w = Vec::new();
                let mut tarball = pijul_core::output::Tarball::new(
                    &mut w,
                    cap.get(6).map(|x| x.as_str().to_string()),
                    0,
                );
                let channel = load_channel_exact(&cap[1], &*txn.read())?;
                let conflicts = if let Some(caps) = cap.get(2) {
                    debug!("caps = {:?}", caps.as_str());
                    let mut hashes = caps.as_str().split(' ').filter(|x| !x.is_empty());
                    let state: pijul_core::Merkle = hashes.next().unwrap().parse().unwrap();
                    let extra: Vec<pijul_core::Hash> = hashes.map(|x| x.parse().unwrap()).collect();
                    debug!("state = {:?}, extra = {:?}", state, extra);
                    if txn.read().current_state(&*channel.read())? == state && extra.is_empty() {
                        txn.archive::<_, _, W>(&repo.changes, &channel, &mut tarball)?
                    } else {
                        use rand::RngExt;
                        let fork_name: String = rand::rng()
                            .sample_iter(&rand::distr::Alphanumeric)
                            .take(30)
                            .map(|x| x as char)
                            .collect();
                        let mut fork = {
                            let mut txn = txn.write();
                            txn.fork(&channel, &fork_name)?
                        };
                        let conflicts = txn.archive_with_state(
                            &repo.changes,
                            &mut fork,
                            &state,
                            &extra,
                            &mut tarball,
                            0,
                            &repo.working_copy,
                        )?;
                        txn.write().drop_channel(&fork_name)?;
                        conflicts
                    }
                } else {
                    txn.archive::<_, _, W>(&repo.changes, &channel, &mut tarball)?
                };
                std::mem::drop(tarball);
                let mut o = std::io::stdout();
                o.write_u64::<BigEndian>(w.len() as u64)?;
                o.write_u64::<BigEndian>(conflicts.len() as u64)?;
                o.write_all(&w)?;
                o.flush()?;
            } else if let Some(cap) = IDENTITIES.captures(&buf) {
                let last_touched: u64 = if let Some(last) = cap.get(2) {
                    last.as_str().parse().unwrap()
                } else {
                    0
                };
                let mut id_dir = repo.path.clone();
                id_dir.push(DOT_DIR);
                id_dir.push("identities");
                let r = if let Ok(r) = std::fs::read_dir(&id_dir) {
                    r
                } else {
                    writeln!(o)?;
                    o.flush()?;
                    continue;
                };
                let mut at_least_one = false;
                for id in r {
                    at_least_one |= output_id(id, last_touched, &mut o).unwrap_or(false);
                }
                debug!("at least one {:?}", at_least_one);
                if !at_least_one {
                    writeln!(o)?;
                }
                writeln!(o)?;
                o.flush()?;
            } else {
                error!("unmatched")
            }
            buf.clear();
        }
        let applied_nonempty = !applied.is_empty();
        for (_, channel) in applied {
            pijul_core::output::output_repository_no_pending(
                &repo.working_copy,
                &repo.changes,
                &txn,
                &channel,
                "",
                true,
                None,
                std::thread::available_parallelism()?.get(),
                0,
            )?;
        }
        if applied_nonempty {
            txn.commit()?;
        }
        Ok(())
    }
}

fn output_id<W: Write>(
    id: Result<std::fs::DirEntry, std::io::Error>,
    last_touched: u64,
    mut o: W,
) -> Result<bool, anyhow::Error> {
    let id = id?;
    let m = id.metadata()?;
    let p = id.path();
    debug!("{:?}", p);
    let mod_ts = m
        .modified()?
        .duration_since(std::time::SystemTime::UNIX_EPOCH)
        .unwrap()
        .as_secs();
    if mod_ts >= last_touched {
        let mut done = HashSet::new();
        if p.file_name() == Some("publickey.json".as_ref()) {
            warn!("Skipping serializing old public key format.");
            return Ok(false);
        } else {
            let mut idf = if let Ok(f) = std::fs::File::open(&p) {
                f
            } else {
                return Ok(false);
            };
            let id: Result<pijul_identity::Complete, _> = serde_json::from_reader(&mut idf);
            if let Ok(id) = id {
                if !done.insert(id.public_key.key.clone()) {
                    return Ok(false);
                }
                serde_json::to_writer(&mut o, &id.as_portable()).unwrap();
                writeln!(o)?;
                return Ok(true);
            }
        }
    }
    Ok(false)
}