flake-edit 0.3.6

Edit your flake inputs with ease.
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
use nix_uri::{FlakeRef, RefKind};
use ropey::Rope;
use std::cmp::Ordering;
use std::collections::HashSet;
use std::sync::Mutex;

use super::api::{BatchLookup, ForgeClient};
use super::channel::{
    UpdateStrategy, channel_probe_candidates, detect_strategy, find_latest_channel,
    parse_channel_ref,
};
use super::version::{is_downgrade, parse_ref};
use crate::edit::InputMap;
use crate::input::Input;
use crate::uri::is_git_url;

/// Cap on concurrent forge requests during the fetch phase. Overlaps
/// per-request latency without tripping anonymous rate limits.
const FETCH_CONCURRENCY: usize = 4;

/// Rewrites flake.nix URIs for `update`, `pin`, and `unpin`.
///
/// Owns one [`ForgeClient`] for the lifetime of the updater so the
/// inner forge fetches share a single HTTP agent and memoize repeats
/// across all inputs in one pass.
#[derive(Debug)]
pub struct Updater {
    text: Rope,
    inputs: Vec<UpdateInput>,
    /// Cumulative char delta from edits applied earlier in the current pass.
    /// Measured in *characters*, since ropey indexes by char.
    offset: i32,
    client: ForgeClient,
}

/// Per-input outcome from the fetch phase.
///
/// Kept separate from the edit phase so multiple inputs can race on
/// the forge while edits to the source text stay strictly sequential
/// and in source order.
struct UpdatePlan {
    /// `ref_or_rev` as it stood before this update.
    ///
    /// Empty when `init` is on and the input was unpinned: the edit
    /// phase prints `Initialized ...` in that case.
    previous_ref: String,
    /// Final ref string after normalisation (`refs/tags/` /
    /// `refs/heads/` re-applied where needed). Compared against
    /// `previous_ref` to decide whether to emit `already on the
    /// latest version`.
    final_change: String,
    updated_uri: String,
}

impl Updater {
    fn print_update_status(id: &str, previous_version: &str, final_change: &str) -> bool {
        let is_up_to_date = previous_version == final_change;
        let initialized = previous_version.is_empty();

        if is_up_to_date {
            println!(
                "{} is already on the latest version: {previous_version}.",
                id
            );
            return false;
        }

        if initialized {
            println!("Initialized {} version pin at {final_change}.", id);
        } else {
            println!("Updated {} from {previous_version} to {final_change}.", id);
        }

        true
    }

    /// Build an [`Updater`] from `text` and the inputs in `map`. Inputs
    /// without an editable URL are skipped.
    pub fn new(text: Rope, map: InputMap) -> Self {
        let client = ForgeClient::new();
        let mut inputs = vec![];
        for (_id, input) in map {
            if !input.has_editable_url() {
                continue;
            }
            // `Input::range` carries rnix `TextRange` *byte* offsets, but ropey
            // indexes by *character*. Convert once against the pristine text so
            // later in-place edits can use simple char-offset arithmetic.
            let url_start = text.byte_to_char(input.range.start) + 1;
            let url_end = text.byte_to_char(input.range.end) - 1;
            inputs.push(UpdateInput {
                input,
                url_start,
                url_end,
            });
        }
        Self {
            inputs,
            text,
            offset: 0,
            client,
        }
    }

    /// Char-index range of the URL string *contents* (without the surrounding `"`),
    /// adjusted for earlier in-place edits.
    fn url_char_range(&self, input: &UpdateInput) -> (usize, usize) {
        let start = (input.url_start as i32 + self.offset) as usize;
        let end = (input.url_end as i32 + self.offset) as usize;
        (start, end)
    }
    fn get_index(&self, id: &str) -> Option<usize> {
        let bare = id
            .strip_prefix('"')
            .and_then(|s| s.strip_suffix('"'))
            .unwrap_or(id);
        self.inputs
            .iter()
            .position(|n| n.input.id().as_str() == bare)
    }
    /// Pin the input named `id` to `rev`.
    ///
    /// # Errors
    ///
    /// Returns the requested `id` if no such input exists.
    pub fn pin_input_to_ref(&mut self, id: &str, rev: &str) -> Result<(), String> {
        self.sort();
        let idx = self.get_index(id).ok_or_else(|| id.to_string())?;
        let input = self.inputs[idx].clone();
        tracing::debug!("Input: {:?}", input);
        self.change_input_to_rev(&input, rev);
        Ok(())
    }

    /// Remove any `?ref=` or `?rev=` pin from `id`.
    ///
    /// # Errors
    ///
    /// Returns the requested `id` if no such input exists.
    pub fn unpin_input(&mut self, id: &str) -> Result<(), String> {
        self.sort();
        let idx = self.get_index(id).ok_or_else(|| id.to_string())?;
        let input = self.inputs[idx].clone();
        tracing::debug!("Input: {:?}", input);
        self.remove_ref_and_rev(&input);
        Ok(())
    }

    pub fn update_all_to_latest_semver(&mut self, init: bool) {
        self.update_matching(|_| true, init);
    }

    /// Update only the inputs whose id appears in `ids`.
    ///
    /// IDs that do not name an editable input are silently skipped, the
    /// same way the all-inputs path skips inputs without an editable URL.
    /// Duplicates collapse: each matching input is processed at most once.
    pub fn update_inputs_to_latest_semver(&mut self, ids: &[&str], init: bool) {
        if ids.is_empty() {
            return;
        }
        let set: HashSet<&str> = ids.iter().copied().collect();
        self.update_matching(|id| set.contains(id), init);
    }

    /// Two-phase update over the inputs whose id satisfies `keep`.
    ///
    /// Phase 1 fans the fetch out across a bounded worker pool so the
    /// per-input forge round-trips overlap. Phase 2 walks the results
    /// in source order and applies each rewrite serially, because
    /// [`Updater::update_input`]'s cumulative `offset` arithmetic is
    /// only valid when edits land left-to-right in the source text.
    ///
    /// Status prints (`Updated X ...`, `Initialized X ...`, `X is
    /// already on the latest version`) are emitted by the edit phase
    /// so they too appear in source order, regardless of the order
    /// in which workers actually finished their fetches.
    fn update_matching<F: Fn(&str) -> bool>(&mut self, keep: F, init: bool) {
        self.sort();

        // Snapshot URIs against the pristine source text. `self.offset`
        // is zero on entry, so [`Self::get_input_text`] returns exactly
        // what's in the original source; later edit-phase rewrites
        // shift the offset and would otherwise corrupt these slices.
        let pending: Vec<(UpdateInput, String)> = self
            .inputs
            .iter()
            .filter(|i| keep(i.input.id.as_str()))
            .map(|i| {
                let uri = self.get_input_text(i);
                (i.clone(), uri)
            })
            .collect();

        if pending.is_empty() {
            return;
        }

        // One GraphQL POST resolves every github.com lookup in
        // `pending`. The REST path stays intact for non-github
        // forges and as a fallback if the warm step fails (anonymous
        // run, partial errors, transport hiccup), so worst case is a
        // wasted POST plus the existing per-input REST round trips.
        let github_lookups = build_github_batch_lookups(&pending);
        // Threshold of 2: a single-input batch trades a REST GET for
        // a GraphQL POST of the same wall-clock cost without any
        // overlap dividend, so the parallel-fetch path keeps it.
        // Two or more inputs are where the round-trip count actually
        // collapses.
        if github_lookups.len() >= 2
            && let Err(e) = self.client.batch_warm_github(&github_lookups)
        {
            tracing::debug!(
                "GraphQL batch warm failed; falling back to REST per input: {}",
                e
            );
        }

        let results = parallel_fetch(&self.client, pending, init);

        for (input, plan) in results {
            let Some(plan) = plan else { continue };
            if Self::print_update_status(
                input.input.id.as_str(),
                &plan.previous_ref,
                &plan.final_change,
            ) {
                self.update_input(input, &plan.updated_uri);
            }
        }
    }

    /// Current source after all queued edits.
    pub fn get_changes(&self) -> String {
        self.text.to_string()
    }

    fn get_input_text(&self, input: &UpdateInput) -> String {
        let (start, end) = self.url_char_range(input);
        self.text.slice(start..end).to_string()
    }

    /// Rewrite `input`'s URL to pin it to `rev`.
    pub(crate) fn change_input_to_rev(&mut self, input: &UpdateInput, rev: &str) {
        let uri = self.get_input_text(input);
        match uri.parse::<FlakeRef>() {
            Ok(parsed) => {
                let updated = parsed.pin_to_rev(rev.into()).into_uri();
                self.update_input(input.clone(), &updated);
            }
            Err(e) => {
                tracing::error!("Error while changing input: {}", e);
            }
        }
    }
    fn remove_ref_and_rev(&mut self, input: &UpdateInput) {
        let uri = self.get_input_text(input);
        match uri.parse::<FlakeRef>() {
            Ok(mut parsed) => {
                if parsed.ref_kind() == RefKind::None {
                    return;
                }
                parsed.set_ref(None);
                parsed.set_rev(None);
                self.update_input(input.clone(), &parsed.into_uri());
            }
            Err(e) => {
                tracing::error!("Error while changing input: {}", e);
            }
        }
    }
    // Sort by source range so multi-edit passes stay aligned with `offset`.
    fn sort(&mut self) {
        self.inputs.sort();
    }
    fn update_input(&mut self, input: UpdateInput, change: &str) {
        let (start, end) = self.url_char_range(&input);
        let previous_len = (end - start) as i32;
        self.text.remove(start..end);
        self.text.insert(start, change);
        self.offset += change.chars().count() as i32 - previous_len;
    }
}

/// Wrapper that lets [`Updater`] sort inputs by source position.
#[derive(Debug, Clone)]
pub(crate) struct UpdateInput {
    input: Input,
    /// Char index of the first URL character (inside the quotes) in the
    /// original, unmodified text.
    url_start: usize,
    /// Char index one past the last URL character in the original text.
    url_end: usize,
}

impl Ord for UpdateInput {
    fn cmp(&self, other: &Self) -> Ordering {
        self.url_start.cmp(&other.url_start)
    }
}

impl PartialEq for UpdateInput {
    fn eq(&self, other: &Self) -> bool {
        self.url_start == other.url_start
    }
}

impl PartialOrd for UpdateInput {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Eq for UpdateInput {}

/// Results come back in the same order as `pending`, which the
/// caller is expected to keep sorted by source position so the
/// subsequent edit phase can walk results left-to-right without a
/// second sort.
///
/// Workers re-use one [`ForgeClient`], so fan-out also amortises
/// TCP / TLS over its connection pool rather than running N
/// independent handshakes.
fn parallel_fetch(
    client: &ForgeClient,
    pending: Vec<(UpdateInput, String)>,
    init: bool,
) -> Vec<(UpdateInput, Option<UpdatePlan>)> {
    let n = pending.len();
    if n == 0 {
        return Vec::new();
    }
    let cap = std::cmp::min(n, FETCH_CONCURRENCY);

    // With nothing to overlap, skip the pool entirely; keeps the
    // single-input `update <id>` path on the calling thread.
    if cap <= 1 {
        let mut results = Vec::with_capacity(n);
        for (input, uri) in pending {
            let plan = compute_change(client, &uri, init);
            results.push((input, plan));
        }
        return results;
    }

    // Pop order is irrelevant: each work item carries its index so
    // results land in source-order `slots` regardless of which
    // worker happens to claim it.
    type WorkItem = (usize, UpdateInput, String);
    type ResultSlot = Mutex<Option<(UpdateInput, Option<UpdatePlan>)>>;
    let work: Mutex<Vec<WorkItem>> = Mutex::new(
        pending
            .into_iter()
            .enumerate()
            .map(|(i, (u, s))| (i, u, s))
            .collect(),
    );
    let slots: Vec<ResultSlot> = (0..n).map(|_| Mutex::new(None)).collect();

    std::thread::scope(|s| {
        for _ in 0..cap {
            let work = &work;
            let slots = &slots;
            s.spawn(move || {
                loop {
                    let next = work.lock().expect("fetch work queue poisoned").pop();
                    let Some((idx, input, uri)) = next else { break };
                    let plan = compute_change(client, &uri, init);
                    *slots[idx].lock().expect("fetch result slot poisoned") = Some((input, plan));
                }
            });
        }
    });

    slots
        .into_iter()
        .map(|m| {
            m.into_inner()
                .expect("fetch result slot poisoned")
                .expect("scope returned with an unfilled fetch slot")
        })
        .collect()
}

/// Plan one [`BatchLookup`] per github.com input in `pending`.
///
/// Inputs whose canonical domain is not `github.com`, or whose ref
/// is unpinned, unstable, or unrecognized, are skipped: those inputs
/// fall through to the REST path uncached. The returned list keeps
/// `pending`'s order so aliases in the GraphQL document line up
/// 1:1 with source-ordered inputs, which keeps debug logs and
/// breakpoints readable.
fn build_github_batch_lookups(pending: &[(UpdateInput, String)]) -> Vec<BatchLookup> {
    let mut lookups = Vec::new();
    for (_, uri) in pending {
        let Ok(parsed) = uri.parse::<FlakeRef>() else {
            continue;
        };
        // canonical_domain(None) == "github.com": shorthand inputs
        // route through the github cache slot, so they batch the same
        // way explicit `https://github.com/...` inputs do.
        let canonical = match parsed.domain() {
            None => "github.com",
            Some(d) => d,
        };
        if canonical != "github.com" {
            continue;
        }
        let (Some(owner), Some(repo)) = (parsed.owner(), parsed.repo()) else {
            continue;
        };
        match detect_strategy(owner, repo) {
            UpdateStrategy::SemverTags => {
                lookups.push(BatchLookup::Tags {
                    owner: owner.to_string(),
                    repo: repo.to_string(),
                });
            }
            UpdateStrategy::NixpkgsChannel
            | UpdateStrategy::HomeManagerChannel
            | UpdateStrategy::NixDarwinChannel => {
                let current_ref = parsed.ref_or_rev().unwrap_or_default();
                if current_ref.is_empty() {
                    continue;
                }
                let channel = parse_channel_ref(current_ref);
                let (Some(prefix), Some(current_version)) = (channel.prefix(), channel.version())
                else {
                    continue;
                };
                let candidates = channel_probe_candidates(prefix, current_version);
                lookups.push(BatchLookup::ChannelCandidates {
                    owner: owner.to_string(),
                    repo: repo.to_string(),
                    prefix: prefix.to_string(),
                    candidates,
                });
            }
        }
    }
    lookups
}

/// Resolve the new URI for a single input.
///
/// Pure with respect to the [`Updater`] state: takes only
/// `&ForgeClient` plus the snapshotted URI text, so it is safe to
/// run on a worker thread without aliasing `Updater::text` /
/// `Updater::offset`. Per-input forge errors are logged via
/// `tracing` and returned as `None`, so one flaky input never
/// aborts the rest of the update run.
fn compute_change(client: &ForgeClient, uri: &str, init: bool) -> Option<UpdatePlan> {
    let parsed = match uri.parse::<FlakeRef>() {
        Ok(p) => p,
        Err(e) => {
            tracing::error!("Failed to parse URI: {}", e);
            return None;
        }
    };

    let owner = match parsed.owner() {
        Some(o) => o.to_owned(),
        None => {
            tracing::debug!("Skipping input without owner");
            return None;
        }
    };
    let repo = match parsed.repo() {
        Some(r) => r.to_owned(),
        None => {
            tracing::debug!("Skipping input without repo");
            return None;
        }
    };

    let strategy = detect_strategy(&owner, &repo);
    tracing::debug!("Update strategy for {}/{}: {:?}", owner, repo, strategy);

    match strategy {
        UpdateStrategy::NixpkgsChannel
        | UpdateStrategy::HomeManagerChannel
        | UpdateStrategy::NixDarwinChannel => {
            compute_channel_change(client, &parsed, &owner, &repo)
        }
        UpdateStrategy::SemverTags => {
            compute_semver_change(client, uri, &parsed, &owner, &repo, init)
        }
    }
}

/// Resolve the new URI for a channel-strategy input (nixpkgs,
/// home-manager, nix-darwin). Returns `None` when the input is
/// unpinned, the ref is unstable, or the forge probe fails. The
/// failure case logs through `tracing` so a flaky channel does not
/// abort the rest of the run. Preserves a `refs/heads/` prefix on
/// the final ref iff the input already carried one.
fn compute_channel_change(
    client: &ForgeClient,
    parsed: &FlakeRef,
    owner: &str,
    repo: &str,
) -> Option<UpdatePlan> {
    let domain = parsed.domain();
    let current_ref = parsed.ref_or_rev().unwrap_or_default().to_owned();

    if current_ref.is_empty() {
        tracing::debug!("Skipping unpinned channel input: {}/{}", owner, repo);
        return None;
    }

    let has_refs_heads_prefix = current_ref.starts_with("refs/heads/");

    let latest = match find_latest_channel(client, &current_ref, owner, repo, domain) {
        Ok(Some(latest)) => latest,
        Ok(None) => return None,
        Err(e) => {
            tracing::error!(
                "Failed to resolve latest channel for {}/{}: {}",
                owner,
                repo,
                e
            );
            return None;
        }
    };

    let final_ref = if has_refs_heads_prefix {
        format!("refs/heads/{}", latest)
    } else {
        latest.clone()
    };
    let updated_uri = parsed.clone().with_ref(Some(final_ref.clone())).into_uri();

    Some(UpdatePlan {
        previous_ref: current_ref,
        final_change: final_ref,
        updated_uri,
    })
}

/// Resolve the new URI for a semver-strategy input. Returns `None`
/// when the current ref does not parse as semver and `init` is off
/// (so non-semver pins aren't silently overwritten), or when the
/// tag listing fails. Preserves a `refs/tags/` prefix on the final
/// ref iff the input already carried one.
fn compute_semver_change(
    client: &ForgeClient,
    uri: &str,
    parsed: &FlakeRef,
    owner: &str,
    repo: &str,
    init: bool,
) -> Option<UpdatePlan> {
    let is_git = is_git_url(uri);
    let maybe_version = parsed.ref_or_rev().unwrap_or_default();
    let parsed_ref = parse_ref(maybe_version, init);

    if !init && let Err(e) = semver::Version::parse(&parsed_ref.normalized_for_semver) {
        tracing::debug!("Skip non semver version: {}: {}", maybe_version, e);
        return None;
    }

    let tags = if is_git {
        let domain = parsed.domain()?.to_owned();
        match client.list_tags(owner, repo, Some(&domain)) {
            Ok(t) => t,
            Err(_) => {
                tracing::error!("Failed to fetch tags for {}/{} on {}", owner, repo, domain);
                return None;
            }
        }
    } else {
        match client.list_tags(owner, repo, None) {
            Ok(t) => t,
            Err(_) => {
                tracing::error!("Failed to fetch tags for {}/{}", owner, repo);
                return None;
            }
        }
    };

    let change = match tags.get_latest_tag() {
        Some(c) => c,
        None => {
            tracing::error!("Could not find latest version for {}/{}", owner, repo);
            return None;
        }
    };

    if !init && is_downgrade(maybe_version, &change) {
        tracing::warn!(
            "Refusing to downgrade {}/{} from {} to {}",
            owner,
            repo,
            maybe_version,
            change
        );
        eprintln!(
            "Warning: skipping {}/{}: latest tag {} is older than the current pin {}.",
            owner, repo, change, maybe_version
        );
        return None;
    }

    let final_change = if parsed_ref.has_refs_tags_prefix {
        format!("refs/tags/{}", change)
    } else {
        change.clone()
    };
    let updated_uri = parsed
        .clone()
        .with_ref(Some(final_change.clone()))
        .into_uri();

    Some(UpdatePlan {
        previous_ref: parsed_ref.previous_ref,
        final_change,
        updated_uri,
    })
}