jj_release_core 0.11.0

Core library for jj-release: semantic releases for jj-vcs
Documentation
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! Publish a new version to the AUR.
//!
//! Clones the package's AUR repo, bumps `pkgver` and resets `pkgrel`,
//! downloads the new remote sources to recompute their checksums, updates
//! `PKGBUILD` and `.SRCINFO`, then commits and pushes.
//!
//! `.SRCINFO` is regenerated with `makepkg --printsrcinfo` when `makepkg` is
//! available, and edited in place otherwise (only version-dependent fields
//! change on a bump, so targeted edits are reliable).
//!
//! Supported: a single package with `sha256sums` and/or `b2sums`. Other
//! checksum arrays and architecture-specific sources are rejected with a
//! clear error. Non-HTTP sources (local files, VCS sources) keep their
//! existing checksums.

use std::{fmt::Write as _, fs, path::Path, process::Command};

use blake2::Blake2b512;
use regex::{NoExpand, Regex};
use semver::Version;
use sha2::{Digest, Sha256};

use super::{Check, download};
use crate::config::AurConfig;
use crate::detect;
use crate::errors::{ReleaseError, Result};

/// Checksum arrays makepkg supports that this module does not.
const UNSUPPORTED_SUMS: &[&str] = &[
    "md5sums",
    "sha1sums",
    "sha224sums",
    "sha384sums",
    "sha512sums",
    "cksums",
];

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ChecksumKind {
    Sha256,
    B2,
}

impl ChecksumKind {
    const ALL: [Self; 2] = [Self::Sha256, Self::B2];

    const fn key(self) -> &'static str {
        match self {
            Self::Sha256 => "sha256sums",
            Self::B2 => "b2sums",
        }
    }

    fn hash(self, bytes: &[u8]) -> String {
        match self {
            Self::Sha256 => to_hex(&Sha256::digest(bytes)),
            Self::B2 => to_hex(&Blake2b512::digest(bytes)),
        }
    }
}

/// Bump the AUR package to `version`, then commit and push.
///
/// With `dry_run`, prints the staged diff and stops before committing.
///
/// # Errors
/// Returns an error if cloning, downloading a source, editing the package
/// files, or any git command fails, or if the package uses a checksum
/// array or source layout this module doesn't support.
pub fn publish(cfg: &AurConfig, version: &Version, dry_run: bool) -> Result<()> {
    let new = version.to_string();

    let tmp = tempfile::tempdir().map_err(|e| msg(format!("creating temp dir: {e}")))?;
    let dir = tmp.path().join(&cfg.package);

    let status = Command::new("git")
        .args(["clone", "--depth", "1", &cfg.repo_url()])
        .arg(&dir)
        .status()
        .map_err(|e| msg(format!("spawning git clone: {e}")))?;
    if !status.success() {
        return Err(msg(format!("git clone {} failed", cfg.repo_url())));
    }

    let pkgbuild_path = dir.join("PKGBUILD");
    let srcinfo_path = dir.join(".SRCINFO");
    let pkgbuild = read(&pkgbuild_path)?;
    let srcinfo = read(&srcinfo_path)?;

    let old = srcinfo_values(&srcinfo, "pkgver")
        .first()
        .copied()
        .ok_or_else(|| msg("no pkgver in .SRCINFO"))?
        .to_owned();
    if old == new {
        println!(
            "  📦 AUR package {} is already at {new}, skipping",
            cfg.package
        );
        return Ok(());
    }

    let kinds = detect_checksums(&srcinfo)?;
    let bumped = bump_srcinfo(&srcinfo, &old, &new);
    let sources = srcinfo_values(&bumped, "source");

    // Download each remote source once, reuse it for every checksum kind.
    let downloads: Vec<Option<Vec<u8>>> = sources
        .iter()
        .map(|s| source_url(s).map(download).transpose())
        .collect::<Result<_>>()?;

    let mut checksums = Vec::new();
    for kind in kinds {
        let old_sums = srcinfo_values(&srcinfo, kind.key());
        if old_sums.len() != sources.len() {
            return Err(msg(format!(
                "{} has {} entries but there are {} sources",
                kind.key(),
                old_sums.len(),
                sources.len()
            )));
        }
        let sums: Vec<String> = downloads
            .iter()
            .zip(&old_sums)
            .map(|(bytes, old_sum)| {
                bytes
                    .as_deref()
                    .map_or_else(|| (*old_sum).to_owned(), |b| kind.hash(b))
            })
            .collect();
        checksums.push((kind, sums));
    }

    write(&pkgbuild_path, &bump_pkgbuild(&pkgbuild, &new, &checksums)?)?;

    let new_srcinfo = makepkg_srcinfo(&dir).unwrap_or_else(|| {
        checksums.iter().fold(bumped.clone(), |s, (kind, sums)| {
            set_srcinfo_checksums(&s, *kind, sums)
        })
    });
    write(&srcinfo_path, &new_srcinfo)?;

    git(&dir, &["add", "PKGBUILD", ".SRCINFO"])?;

    if dry_run {
        git(&dir, &["--no-pager", "diff", "--cached"])?;
        println!("  📦 Dry run: not pushing {} {new} to the AUR", cfg.package);
        return Ok(());
    }

    git(&dir, &["commit", "-m", &format!("Update to {new}")])?;
    git(&dir, &["push"])?;

    println!("  📦 Pushed {} {new} to the AUR", cfg.package);
    Ok(())
}

// -- Helpers --

fn msg(s: impl Into<String>) -> ReleaseError {
    ReleaseError::Message(s.into())
}

fn read(path: &Path) -> Result<String> {
    fs::read_to_string(path).map_err(|e| msg(format!("reading {}: {e}", path.display())))
}

fn write(path: &Path, contents: &str) -> Result<()> {
    fs::write(path, contents).map_err(|e| msg(format!("writing {}: {e}", path.display())))
}

fn regex(pattern: &str) -> Result<Regex> {
    Regex::new(pattern).map_err(|e| msg(format!("invalid regex {pattern:?}: {e}")))
}

fn git(dir: &Path, args: &[&str]) -> Result<()> {
    let status = Command::new("git")
        .arg("-C")
        .arg(dir)
        .args(args)
        .status()
        .map_err(|e| msg(format!("spawning git: {e}")))?;
    if !status.success() {
        return Err(msg(format!("git {} failed", args.join(" "))));
    }
    Ok(())
}

fn to_hex(bytes: &[u8]) -> String {
    bytes
        .iter()
        .fold(String::with_capacity(bytes.len() * 2), |mut s, b| {
            let _ = write!(s, "{b:02x}");
            s
        })
}

/// `makepkg --printsrcinfo`, or `None` if makepkg isn't available or fails.
fn makepkg_srcinfo(dir: &Path) -> Option<String> {
    let out = Command::new("makepkg")
        .arg("--printsrcinfo")
        .current_dir(dir)
        .output()
        .ok()?;
    out.status
        .success()
        .then(|| String::from_utf8_lossy(&out.stdout).into_owned())
}

// -- Pure transformations (unit-tested) --

/// All values for `key` in a .SRCINFO, in order.
fn srcinfo_values<'a>(srcinfo: &'a str, key: &str) -> Vec<&'a str> {
    srcinfo
        .lines()
        .filter_map(|l| l.trim().split_once(" = "))
        .filter(|(k, _)| *k == key)
        .map(|(_, v)| v)
        .collect()
}

/// Which supported checksum arrays the package uses. Errors on layouts
/// this module can't handle.
fn detect_checksums(srcinfo: &str) -> Result<Vec<ChecksumKind>> {
    let keys: Vec<&str> = srcinfo
        .lines()
        .filter_map(|l| l.trim().split_once(" = ").map(|(k, _)| k))
        .collect();

    for key in &keys {
        let (base, arch_specific) = key
            .split_once('_')
            .map_or((*key, false), |(base, _)| (base, true));
        if UNSUPPORTED_SUMS.contains(&base) {
            return Err(msg(format!(
                "unsupported checksum array `{key}` in .SRCINFO; use sha256sums or b2sums"
            )));
        }
        if arch_specific && matches!(base, "source" | "sha256sums" | "b2sums") {
            return Err(msg(format!(
                "architecture-specific `{key}` in .SRCINFO is not supported yet"
            )));
        }
    }

    let kinds: Vec<ChecksumKind> = ChecksumKind::ALL
        .into_iter()
        .filter(|kind| keys.contains(&kind.key()))
        .collect();
    if kinds.is_empty() {
        return Err(msg("no sha256sums or b2sums found in .SRCINFO"));
    }
    Ok(kinds)
}

/// The URL of a source entry (`name::url` or `url`), if it's downloadable
/// over HTTP. Local files and VCS sources return `None`.
fn source_url(entry: &str) -> Option<&str> {
    let url = entry.split_once("::").map_or(entry, |(_, url)| url);
    (url.starts_with("https://") || url.starts_with("http://")).then_some(url)
}

/// Set `pkgver`, reset `pkgrel`, and move `source` entries to the new version.
fn bump_srcinfo(srcinfo: &str, old: &str, new: &str) -> String {
    let mut out = String::with_capacity(srcinfo.len());
    for line in srcinfo.lines() {
        let indent = &line[..line.len() - line.trim_start().len()];
        match line.trim().split_once(" = ") {
            Some(("pkgver", _)) => {
                let _ = writeln!(out, "{indent}pkgver = {new}");
            }
            Some(("pkgrel", _)) => {
                let _ = writeln!(out, "{indent}pkgrel = 1");
            }
            Some(("source", value)) => {
                let _ = writeln!(out, "{indent}source = {}", value.replace(old, new));
            }
            _ => {
                out.push_str(line);
                out.push('\n');
            }
        }
    }
    out
}

/// Replace every `kind` line in a .SRCINFO with `sums`, in place of the first.
fn set_srcinfo_checksums(srcinfo: &str, kind: ChecksumKind, sums: &[String]) -> String {
    let mut out = String::with_capacity(srcinfo.len());
    let mut written = false;
    for line in srcinfo.lines() {
        if let Some((key, _)) = line.trim().split_once(" = ")
            && key == kind.key()
        {
            if !written {
                let indent = &line[..line.len() - line.trim_start().len()];
                for sum in sums {
                    let _ = writeln!(out, "{indent}{} = {sum}", kind.key());
                }
                written = true;
            }
            continue;
        }
        out.push_str(line);
        out.push('\n');
    }
    out
}

/// Format a Bash array the way PKGBUILDs usually align multi-line arrays.
fn bash_array(key: &str, values: &[String]) -> String {
    let pad = " ".repeat(key.len() + 2);
    let items: Vec<String> = values.iter().map(|v| format!("'{v}'")).collect();
    format!("{key}=({})", items.join(&format!("\n{pad}")))
}

/// Set `pkgver`, reset `pkgrel`, and replace checksum arrays in a PKGBUILD.
fn bump_pkgbuild(
    pkgbuild: &str,
    new: &str,
    checksums: &[(ChecksumKind, Vec<String>)],
) -> Result<String> {
    let pkgver = regex(r"(?m)^pkgver=.*$")?;
    let pkgrel = regex(r"(?m)^pkgrel=.*$")?;
    if !pkgver.is_match(pkgbuild) {
        return Err(msg("no pkgver= line in PKGBUILD"));
    }

    let mut out = pkgver
        .replace(pkgbuild, NoExpand(&format!("pkgver={new}")))
        .into_owned();
    out = pkgrel.replace(&out, NoExpand("pkgrel=1")).into_owned();

    for (kind, sums) in checksums {
        let array = regex(&format!(r"(?ms)^{}=\(.*?\)", kind.key()))?;
        if !array.is_match(&out) {
            return Err(msg(format!("no {}=(...) array in PKGBUILD", kind.key())));
        }
        let replacement = bash_array(kind.key(), sums);
        out = array.replace(&out, NoExpand(&replacement)).into_owned();
    }
    Ok(out)
}

/// Pre-flight checks: tools, the package's current layout, and SSH access.
#[must_use]
pub fn preflight(cfg: &AurConfig) -> Vec<Check> {
    let tools = super::require_tools(&["git", "curl"]).map(|found| {
        if detect::tool_available("makepkg") {
            format!("{found}, makepkg (regenerates .SRCINFO)")
        } else {
            format!("{found} (no makepkg: .SRCINFO edited in place)")
        }
    });
    vec![
        ("AUR tools", tools),
        ("AUR package", check_package(cfg)),
        ("AUR SSH access", check_access(cfg)),
    ]
}

/// Fetch the current .SRCINFO over HTTPS and make sure we can handle it.
fn check_package(cfg: &AurConfig) -> std::result::Result<String, String> {
    if cfg.repo.is_some() {
        return Ok("custom repo, layout checked at publish time".to_owned());
    }
    let url = format!(
        "https://aur.archlinux.org/cgit/aur.git/plain/.SRCINFO?h={}",
        cfg.package
    );
    let bytes =
        super::fetch(&url).map_err(|e| format!("{} not found on the AUR ({e})", cfg.package))?;
    let srcinfo = String::from_utf8_lossy(&bytes);

    let version = srcinfo_values(&srcinfo, "pkgver")
        .first()
        .copied()
        .ok_or_else(|| "no pkgver in .SRCINFO".to_owned())?
        .to_owned();
    let kinds = detect_checksums(&srcinfo).map_err(|e| e.to_string())?;
    let sums: Vec<&str> = kinds.iter().map(|k| k.key()).collect();

    Ok(format!(
        "{} at {version}, {}",
        cfg.package,
        sums.join(" + ")
    ))
}

/// `git ls-remote` proves the SSH key is accepted without cloning anything.
fn check_access(cfg: &AurConfig) -> std::result::Result<String, String> {
    let url = cfg.repo_url();
    let output = Command::new("git")
        .args(["ls-remote", "--heads", &url])
        .output()
        .map_err(|e| format!("spawning git: {e}"))?;
    if output.status.success() {
        Ok(format!("can reach {url}"))
    } else {
        Err(format!(
            "cannot reach {url}: {}",
            String::from_utf8_lossy(&output.stderr).trim()
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const SRCINFO: &str = "pkgbase = jj-release
\tpkgdesc = Semantic releases and changelog generation for jj-vcs repositories
\tpkgver = 0.8.0
\tpkgrel = 2
\turl = https://github.com/saylesss88/jj-release
\tarch = x86_64
\tsource = jj-release-0.8.0.tar.gz::https://static.crates.io/crates/jj-release/jj-release-0.8.0.crate
\tb2sums = oldhash

pkgname = jj-release
";

    const PKGBUILD: &str = "pkgname=jj-release
pkgver=0.8.0
pkgrel=2
source=(\"$pkgname-$pkgver.tar.gz::https://static.crates.io/crates/$pkgname/$pkgname-$pkgver.crate\")
b2sums=('oldhash')

build() {
  cargo build --release
}
";

    #[test]
    fn hashes_match_known_vectors() {
        assert_eq!(
            ChecksumKind::Sha256.hash(b"abc"),
            "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
        );
        assert_eq!(
            ChecksumKind::B2.hash(b"abc"),
            "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1\
             7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923"
        );
    }

    #[test]
    fn bumps_srcinfo_version_rel_and_source() {
        let out = bump_srcinfo(SRCINFO, "0.8.0", "0.9.0");
        assert!(out.contains("\tpkgver = 0.9.0\n"));
        assert!(out.contains("\tpkgrel = 1\n"));
        assert!(out.contains(
            "\tsource = jj-release-0.9.0.tar.gz::https://static.crates.io/crates/jj-release/jj-release-0.9.0.crate\n"
        ));
        // Untouched lines, blank separator and pkgname section survive.
        assert!(out.contains("\turl = https://github.com/saylesss88/jj-release\n"));
        assert!(out.contains("\n\npkgname = jj-release\n"));
    }

    #[test]
    fn sets_srcinfo_checksums() {
        let out = set_srcinfo_checksums(SRCINFO, ChecksumKind::B2, &["newhash".to_owned()]);
        assert!(out.contains("\tb2sums = newhash\n"));
        assert!(!out.contains("oldhash"));
    }

    #[test]
    fn bumps_pkgbuild() {
        let out = bump_pkgbuild(
            PKGBUILD,
            "0.9.0",
            &[(ChecksumKind::B2, vec!["newhash".to_owned()])],
        )
        .unwrap();
        assert!(out.contains("\npkgver=0.9.0\n"));
        assert!(out.contains("\npkgrel=1\n"));
        assert!(out.contains("\nb2sums=('newhash')\n"));
        // The source line uses $pkgver, so it must be left alone.
        assert!(out.contains("$pkgname-$pkgver.crate"));
        assert!(out.contains("cargo build --release"));
    }

    #[test]
    fn bumps_multiline_pkgbuild_array() {
        let pkgbuild = "pkgver=1.0.0\npkgrel=1\nsha256sums=('a'\n            'b')\n";
        let out = bump_pkgbuild(
            pkgbuild,
            "1.1.0",
            &[(ChecksumKind::Sha256, vec!["c".to_owned(), "d".to_owned()])],
        )
        .unwrap();
        assert!(out.contains("sha256sums=('c'\n            'd')\n"));
    }

    #[test]
    fn detects_b2sums() {
        assert_eq!(detect_checksums(SRCINFO).unwrap(), vec![ChecksumKind::B2]);
    }

    #[test]
    fn rejects_unsupported_checksums() {
        let srcinfo = "\tsource = https://example.com/a.tar.gz\n\tsha512sums = x\n";
        assert!(detect_checksums(srcinfo).is_err());
    }

    #[test]
    fn rejects_arch_specific_sources() {
        let srcinfo = "\tsource_x86_64 = https://example.com/a.tar.gz\n\tb2sums_x86_64 = x\n";
        assert!(detect_checksums(srcinfo).is_err());
    }

    #[test]
    fn parses_source_urls() {
        assert_eq!(
            source_url("name.tar.gz::https://example.com/a.crate"),
            Some("https://example.com/a.crate")
        );
        assert_eq!(
            source_url("https://example.com/a.tar.gz"),
            Some("https://example.com/a.tar.gz")
        );
        assert_eq!(source_url("jj-release.patch"), None);
        assert_eq!(source_url("git+https://example.com/repo.git"), None);
    }

    #[test]
    fn bumps_split_pkgbuild() {
        let pkgbuild = r#"pkgbase=jj-release
pkgname=('jj-release' 'jj-release-full')
pkgver=0.8.0
pkgrel=1
source=("$pkgbase-$pkgver.tar.gz::https://static.crates.io/crates/$pkgbase/$pkgbase-$pkgver.crate")
b2sums=('oldhash')

package_jj-release() {
  install -Dm0755 target/release/jj-release "$pkgdir/usr/bin/jj-release"
}

package_jj-release-full() {
  provides=('jj-release')
  conflicts=('jj-release')
  install -Dm0755 target/release/jj-release-full "$pkgdir/usr/bin/jj-release"
}
"#;
        let out = bump_pkgbuild(
            pkgbuild,
            "0.9.0",
            &[(ChecksumKind::B2, vec!["newhash".to_owned()])],
        )
        .unwrap();
        assert!(out.contains("\npkgver=0.9.0\n"));
        assert!(out.contains("\npkgrel=1\n"));
        assert!(out.contains("\nb2sums=('newhash')\n"));
        // package_ functions untouched
        assert!(out.contains("package_jj-release()"));
        assert!(out.contains("package_jj-release-full()"));
        assert!(out.contains("provides=('jj-release')"));
    }
}