rlyx 0.3.1

rlyx is a fast release manager that automatically bumps versions, creates changelogs, tags commits, and publishes GitHub releases across JS, Rust, and Python projects with first class monorepos support.
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
use crate::util::{run_capture, run_quiet, run_status};
use anyhow::{anyhow, Result};
use fs_err as fs;
use serde_json::Value;
use std::path::{Path, PathBuf};

pub fn current_repo_name() -> Result<String> {
    let remote = run_capture(
        "git",
        &["config", "--get", "remote.origin.url"],
    )?;
    let repo = remote
        .trim()
        .trim_start_matches("git@github.com:")
        .trim_start_matches("https://github.com/")
        .trim_end_matches(".git")
        .to_string();
    if repo.is_empty() {
        return Err(anyhow!("no origin remote configured"));
    }
    Ok(repo)
}

pub fn add_all() -> Result<()> {
    run_quiet("git", &["add", "-A"], "git add")?;
    Ok(())
}

pub fn has_staged_changes() -> Result<bool> {
    let no_changes =
        run_status("git", &["diff", "--cached", "--quiet"]);
    Ok(!no_changes)
}

pub fn working_tree_dirty() -> bool {
    !run_status("git", &["diff", "--quiet"])
        || !run_status("git", &["diff", "--cached", "--quiet"])
}

pub fn commit_with_message(msg: &str) -> Result<()> {
    run_quiet("git", &["commit", "-m", msg], "git commit")?;
    Ok(())
}

pub fn create_annotated_tag(
    tag: &str,
    message: Option<&str>,
) -> Result<()> {
    // single-repo tags should be clean: just the message (if any), no rlyx trailers
    let m = message.unwrap_or(tag);
    run_quiet("git", &["tag", "-a", tag, "-m", m], "git tag")?;
    Ok(())
}

pub fn create_annotated_tag_at(
    tag: &str,
    message: Option<&str>,
    target: &str,
) -> Result<()> {
    // same cleanliness when tagging a specific target
    let m = message.unwrap_or(tag);
    run_quiet(
        "git",
        &["tag", "-a", tag, "-m", m, target],
        "git tag at",
    )?;
    Ok(())
}

pub fn push_commits(dry: bool) -> Result<()> {
    if !dry {
        run_quiet("git", &["push"], "git push")?;
    }
    Ok(())
}

pub fn upstream_remote() -> Option<String> {
    let up = run_capture(
        "git",
        &[
            "rev-parse",
            "--abbrev-ref",
            "--symbolic-full-name",
            "@{u}",
        ],
    )
    .ok()?;
    Some(up.split('/').next().unwrap_or("origin").to_string())
}

pub fn push_tag(tag: &str, dry: bool) -> Result<()> {
    if !dry {
        let remote =
            upstream_remote().unwrap_or_else(|| "origin".to_string());
        run_quiet(
            "git",
            &["push", &remote, &format!("refs/tags/{}", tag)],
            "git push tag",
        )?;
    }
    Ok(())
}

pub fn list_tags(pattern: &str) -> Result<Vec<String>> {
    let s = run_capture(
        "git",
        &["tag", "--list", pattern, "--sort=-v:refname"],
    )?;
    Ok(s.lines()
        .map(|l| l.trim().to_string())
        .filter(|l| !l.is_empty())
        .collect())
}

pub fn last_reachable_tag() -> Option<String> {
    run_capture("git", &["describe", "--tags", "--abbrev=0"]).ok()
}

pub fn tag_exists(tag: &str) -> Result<bool> {
    Ok(run_status(
        "git",
        &[
            "rev-parse",
            "-q",
            "--verify",
            &format!("refs/tags/{}", tag),
        ],
    ))
}

pub fn gh_pr_title(owner_repo: &str, number: &str) -> Option<String> {
    let path = format!("repos/{}/pulls/{}", owner_repo, number);
    let out = crate::util::run_capture("gh", &["api", &path]).ok()?;
    let v: Value = serde_json::from_str(&out).ok()?;
    v.get("title")
        .and_then(|t| t.as_str())
        .map(|s| s.to_string())
}

/// derive the canonical GitHub release title from the tag.
/// - single repo tags look like: "v1.2.3"  -> title "v1.2.3"
/// - monorepo tags look like:  "<pkg>@v1.2.3" -> title "<pkg>@1.2.3"
fn inferred_release_title(tag: &str) -> String {
    if let Some((pkg, ver_with_v)) = tag.rsplit_once('@') {
        let ver = ver_with_v.trim_start_matches('v');
        format!("{}@{}", pkg, ver)
    } else {
        tag.to_string()
    }
}

pub fn publish_github_release(
    tag: &str,
    owner_repo: &str,
    notes: &str,
) -> Result<()> {
    let tmp: PathBuf = std::env::temp_dir()
        .join(format!("rlyx-{}-notes.md", safe_tmp_name(tag)));
    fs::write(&tmp, notes)?;

    // Always set the release title explicitly so GitHub doesn't infer it from the body.
    let title = inferred_release_title(tag);

    run_quiet(
        "gh",
        &[
            "release",
            "create",
            tag,
            "-F",
            tmp.to_string_lossy().as_ref(),
            "--repo",
            owner_repo,
            "--title",
            &title,
        ],
        "gh release create",
    )?;
    let _ = fs::remove_file(&tmp);
    Ok(())
}

fn safe_tmp_name(tag: &str) -> String {
    tag.chars()
        .map(|c| {
            if c.is_ascii_alphanumeric()
                || c == '.'
                || c == '_'
                || c == '-'
            {
                c
            } else {
                '_'
            }
        })
        .collect()
}

pub fn delete_github_release(
    tag: &str,
    owner_repo: &str,
) -> Result<()> {
    run_quiet(
        "gh",
        &["release", "delete", tag, "--yes", "--repo", owner_repo],
        "gh release delete",
    )?;
    Ok(())
}

pub fn delete_local_tag(tag: &str) -> Result<()> {
    run_quiet("git", &["tag", "-d", tag], "git tag -d")?;
    Ok(())
}

pub fn delete_remote_tag(tag: &str) -> Result<()> {
    let remote =
        upstream_remote().unwrap_or_else(|| "origin".to_string());
    run_quiet(
        "git",
        &["push", &remote, "--delete", tag],
        "git push --delete",
    )?;
    Ok(())
}

pub fn tag_pointing_at_head() -> Option<String> {
    let s =
        run_capture("git", &["tag", "--points-at", "HEAD"]).ok()?;
    let mut tags: Vec<String> = s
        .lines()
        .map(|l| l.trim().to_string())
        .filter(|l| !l.is_empty())
        .collect();
    if tags.is_empty() {
        return None;
    }
    tags.sort();
    tags.pop()
}

pub fn tags_pointing_at_head() -> Vec<String> {
    run_capture("git", &["tag", "--points-at", "HEAD"])
        .ok()
        .map(|s| {
            s.lines()
                .map(|l| l.trim().to_string())
                .filter(|l| !l.is_empty())
                .collect::<Vec<_>>()
        })
        .unwrap_or_default()
}

pub fn last_commit_subject() -> Option<String> {
    run_capture("git", &["log", "-1", "--pretty=%s"]).ok()
}

pub fn current_branch() -> Option<String> {
    run_capture("git", &["rev-parse", "--abbrev-ref", "HEAD"]).ok()
}

pub fn reset_hard(target: &str) -> Result<()> {
    run_quiet(
        "git",
        &["reset", "--hard", target],
        "git reset --hard",
    )?;
    Ok(())
}

pub fn push_force_with_lease() -> Result<()> {
    run_quiet(
        "git",
        &["push", "--force-with-lease"],
        "git push --force-with-lease",
    )?;
    Ok(())
}

pub fn run_pre_hooks(_: &crate::config::Config) -> Result<()> {
    Ok(())
}
pub fn run_post_hooks(_: &crate::config::Config) -> Result<()> {
    Ok(())
}

pub fn diff_names(range: &str) -> Result<Vec<String>> {
    let out = run_capture("git", &["diff", "--name-only", range])
        .unwrap_or_default();
    Ok(out
        .lines()
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .collect())
}

pub fn release_url(owner_repo: &str, tag: &str) -> String {
    fn enc(tag: &str) -> String {
        let mut out = String::new();
        for b in tag.bytes() {
            let c = b as char;
            if c.is_ascii_alphanumeric()
                || c == '-'
                || c == '_'
                || c == '.'
                || c == '~'
            {
                out.push(c);
            } else {
                out.push_str(&format!("%{:02X}", b));
            }
        }
        out
    }
    format!(
        "https://github.com/{}/releases/tag/{}",
        owner_repo,
        enc(tag)
    )
}

pub fn rev_parse(rev: &str) -> Option<String> {
    run_capture("git", &["rev-parse", rev])
        .ok()
        .map(|s| s.trim().to_string())
}

pub fn head_commit() -> Option<String> {
    rev_parse("HEAD")
}

pub fn is_ancestor(ancestor: &str, descendant: &str) -> bool {
    run_status(
        "git",
        &["merge-base", "--is-ancestor", ancestor, descendant],
    )
}

pub fn tag_commit(tag: &str) -> Option<String> {
    run_capture("git", &["rev-list", "-n", "1", tag])
        .ok()
        .or_else(|| {
            let alt = tag.replace('/', "_");
            if alt != tag {
                run_capture("git", &["rev-list", "-n", "1", &alt])
                    .ok()
            } else {
                None
            }
        })
        .map(|s| s.trim().to_string())
}

pub fn has_commits_since(
    tag_opt: Option<&str>,
    path: &Path,
) -> Result<bool> {
    let range = tag_opt
        .map(|t| format!("{}..HEAD", t))
        .unwrap_or_else(|| "HEAD".to_string());
    let out = run_capture(
        "git",
        &[
            "log",
            "--oneline",
            &range,
            "--",
            path.to_string_lossy().as_ref(),
        ],
    )
    .unwrap_or_default();
    Ok(!out.trim().is_empty())
}

pub fn scoped_commits_since(
    tag_opt: Option<&str>,
    path: &Path,
) -> Result<Vec<String>> {
    let range = tag_opt
        .map(|t| format!("{}..HEAD", t))
        .unwrap_or_else(|| "HEAD".to_string());
    let out = run_capture(
        "git",
        &[
            "log",
            "--no-merges",
            "--pretty=- %h %s",
            &range,
            "--",
            path.to_string_lossy().as_ref(),
        ],
    )
    .unwrap_or_default();
    Ok(out.lines().map(|s| s.to_string()).collect())
}

pub fn first_commit() -> Option<String> {
    let out =
        run_capture("git", &["rev-list", "--max-parents=0", "HEAD"])
            .ok()?;
    out.lines().next().map(|s| s.trim().to_string())
}

pub fn commit_count(range: &str) -> usize {
    run_capture("git", &["rev-list", "--count", range])
        .ok()
        .and_then(|s| s.trim().parse::<usize>().ok())
        .unwrap_or(0)
}