cranko 0.17.3

A cross-platform, cross-language release automation tool
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
// Copyright 2020 Peter Williams <peter@newton.cx> and collaborators
// Licensed under the MIT License.

//! State for the Cranko CLI application.

use anyhow::{anyhow, Context};
use log::{error, info, warn};
use std::collections::HashMap;
use thiserror::Error as ThisError;

use crate::{
    atry,
    config::{ConfigurationFile, NpmConfiguration},
    errors::Result,
    graph::{ProjectGraph, ProjectGraphBuilder, RepoHistories},
    project::{DepRequirement, ProjectId},
    repository::{
        ChangeList, CommitId, PathMatcher, RcCommitInfo, RcProjectInfo, ReleaseAvailability,
        ReleaseCommitInfo, Repository,
    },
    version::Version,
};

/// Setting up a Cranko application session.
pub struct AppBuilder {
    pub repo: Repository,
    pub graph: ProjectGraphBuilder,

    ci_info: ci_info::types::CiInfo,
    populate_graph: bool,
}

impl AppBuilder {
    /// Start initializing an application session.
    ///
    /// This first phase of initialization may fail if the environment doesn't
    /// associate the process with a proper Git repository with a work tree.
    pub fn new() -> Result<AppBuilder> {
        let repo = Repository::open_from_env()?;
        let graph = ProjectGraphBuilder::new();
        let ci_info = ci_info::get();

        Ok(AppBuilder {
            graph,
            repo,
            ci_info,
            populate_graph: true,
        })
    }

    pub fn populate_graph(mut self, do_populate: bool) -> Self {
        self.populate_graph = do_populate;
        self
    }

    /// Finish app initialization, yielding a full AppSession object.
    pub fn initialize(mut self) -> Result<AppSession> {
        // Start by loading the configuration file, if it exists. If it doesn't
        // we'll get a sensible default.

        let mut cfg_path = self.repo.resolve_config_dir();
        cfg_path.push("config.toml");
        let config = ConfigurationFile::get(&cfg_path).with_context(|| {
            format!(
                "failed to load repository config file `{}`",
                cfg_path.display()
            )
        })?;

        self.repo
            .apply_config(config.repo)
            .with_context(|| "failed to finalize repository setup")?;

        let proj_config = config.projects;

        // Now auto-detect everything in the repo index.

        if self.populate_graph {
            let mut cargo = crate::cargo::CargoLoader::default();
            let mut csproj = crate::csproj::CsProjLoader::default();
            let mut npm = crate::npm::NpmLoader::default();
            let mut pypa = crate::pypa::PypaLoader::default();
            let mut generic = crate::generic::GenericLoader::default();

            // Dumb hack around the borrowchecker to allow mutable reference to
            // the graph while iterating over the repo:
            let repo = self.repo;
            let mut graph = self.graph;

            repo.scan_paths(|p| {
                let (dirname, basename) = p.split_basename();
                cargo.process_index_item(dirname, basename);
                csproj.process_index_item(&repo, p, dirname, basename)?;
                npm.process_index_item(&repo, &mut graph, p, dirname, basename, &proj_config)?;
                pypa.process_index_item(dirname, basename);
                generic.process_index_item(dirname, basename)?;
                Ok(())
            })?;

            self.repo = repo;
            self.graph = graph;
            // End dumb hack.

            cargo.finalize(&mut self, &proj_config)?;
            csproj.finalize(&mut self, &proj_config)?;
            npm.finalize(&mut self)?;
            pypa.finalize(&mut self, &proj_config)?;
            generic.finalize(&mut self, &proj_config)?;
        }

        // Apply project config and compile the graph.

        let graph = atry!(
            self.graph.complete_loading();
            ["the project graph is invalid"]
        );

        // All done.
        Ok(AppSession {
            repo: self.repo,
            graph,
            npm_config: config.npm,
            ci_info: self.ci_info,
        })
    }
}

/// An error returned when one project in the repository needs a newer release
/// of another project. The inner values are the user-facing names of the two
/// projects: the first named project depends on the second one.
#[derive(Debug, ThisError)]
#[error("unsatisfied internal requirement: `{0}` needs newer `{1}`")]
pub struct UnsatisfiedInternalRequirementError(pub String, pub String);

/// The main Cranko CLI application state structure.
pub struct AppSession {
    /// The backing repository.
    pub repo: Repository,

    /// It feels hacky to have this here, but this is where we need it.
    pub npm_config: NpmConfiguration,

    /// The graph of projects contained within the repo.
    graph: ProjectGraph,

    /// Information about the CI environment that we may be running in.
    ci_info: ci_info::types::CiInfo,
}

impl AppSession {
    /// Create a new app session with totally default parameters
    pub fn initialize_default() -> Result<Self> {
        AppBuilder::new()?.initialize()
    }

    /// Characterize the repository environment in which this process is
    /// running.
    pub fn execution_environment(&self) -> Result<ExecutionEnvironment> {
        if !self.ci_info.ci {
            Ok(ExecutionEnvironment::NotCi)
        } else {
            let maybe_pr = self.ci_info.pr;
            let maybe_ci_branch = self.ci_info.branch_name.as_ref().map(|s| s.as_ref());
            let rc_name = self.repo.upstream_rc_name();
            let release_name = self.repo.upstream_release_name();

            // Since we're running in CI, might as well log generously
            let ci_vendor_desc = self
                .ci_info
                .vendor
                .map(|v| format!("{:?}", v))
                .unwrap_or_else(|| "Unknown".to_owned());
            let ci_build_type = maybe_pr
                .map(|is_pr| {
                    if is_pr {
                        "pull request"
                    } else {
                        "branch update"
                    }
                })
                .unwrap_or("unknown type");
            let ci_branch_desc = maybe_ci_branch
                .map(|b| format!("`{b}`"))
                .unwrap_or_else(|| "unknown".to_owned());

            info!(
                "{ci_vendor_desc} CI environment detected, {ci_build_type} build, source branch {ci_branch_desc}"
            );

            if maybe_ci_branch.is_none() {
                warn!("cannot determine the triggering branch name in this CI environment");
                warn!("... this will affect many workflow safety checks")
            }

            if let Some(true) = maybe_pr {
                if maybe_ci_branch == Some(rc_name) {
                    warn!("cranko seems to be running in a pull request to the `{}` branch; this is not recommended", rc_name);
                    warn!("... treating as a non-CI environment for safety");
                    return Ok(ExecutionEnvironment::NotCi);
                }

                if maybe_ci_branch == Some(release_name) {
                    warn!("cranko seems to be running in a pull request to the `{}` branch; this is not recommended", release_name);
                    warn!("... treating as a non-CI environment for safety");
                    return Ok(ExecutionEnvironment::NotCi);
                }
            }

            if maybe_ci_branch == Some(release_name) {
                warn!("cranko seems to be running in an update to the `{}` branch; this is not recommended", release_name);
                warn!("... treating as a non-CI environment for safety");
                return Ok(ExecutionEnvironment::NotCi);
            }

            // Gather some useful parameters ... Note: on Azure Pipelines, the
            // initial checkout is in detached-HEAD state, so on pushes to the
            // `rc` branch we can't determine `current_branch`. It would be kind
            // of tedious to force all Azure users to manually check out the RC
            // branch, so if we can parse out the RC info, let's assume that's
            // what's going on.

            let is_rc_update = maybe_ci_branch == Some(rc_name);
            let current_is_release = self
                .repo
                .current_branch_name()?
                .as_ref()
                .map(|s| s.as_ref())
                == Some(release_name);

            // If the current branch is called `release`, we insist that we can
            // parse release info from HEAD. We must be in dev mode (due to PR,
            // or dev branch update) unless we have been triggered by an update to
            // the `rc` branch.

            if current_is_release {
                let rel_info = self.repo.parse_release_info_from_head()?;
                let dev_mode = !is_rc_update;
                return Ok(ExecutionEnvironment::CiReleaseMode(dev_mode, rel_info));
            }

            // Otherwise, we must be in RC mode. If we're an update to the `rc`
            // branch, we are *not* in dev mode and we insist that we can parse
            // actual RC info from HEAD. Otherwise, we are in dev mode and we
            // fake an RC request for all projects.

            let (dev_mode, rc_info) = if is_rc_update {
                (false, self.repo.parse_rc_info_from_head()?)
            } else {
                (true, self.default_dev_rc_info())
            };

            Ok(ExecutionEnvironment::CiRcMode(dev_mode, rc_info))
        }
    }

    /// Check that the current process is running *outside* of a CI environment.
    pub fn ensure_not_ci(&self, force: bool) -> Result<()> {
        match self.execution_environment()? {
            ExecutionEnvironment::NotCi => Ok(()),

            _ => {
                warn!("CI environment detected; this is unexpected for this command");
                if force {
                    Ok(())
                } else {
                    Err(anyhow!(
                        "refusing to proceed (use \"force\" mode to override)",
                    ))
                }
            }
        }
    }

    /// Check that the current process is running in the "release mode" CI
    /// environment, returning the latest release information. Any other
    /// circumstance results in an error.
    ///
    /// The returned boolean is true if in a "development"-like mode, false if
    /// in the intended `rc` mode.
    pub fn ensure_ci_release_mode(&self) -> Result<(bool, ReleaseCommitInfo)> {
        match self.execution_environment()? {
            ExecutionEnvironment::NotCi => {
                error!("no CI environment detected; this is unexpected for this command");
                Err(anyhow!(
                    "don't know how to obtain release information -- cannot proceed",
                ))
            }

            ExecutionEnvironment::CiReleaseMode(dev, ri) => Ok((dev, ri)),

            _ => {
                error!("unexpected CI environment detected");
                error!("... this command should only be run after switching to a local `release`-type branch");
                Err(anyhow!(
                    "don't know how to obtain release information -- cannot proceed",
                ))
            }
        }
    }

    /// Check that the current process is running and "RC"-like CI mode.
    ///
    /// The returned boolean is true if in a "development"-like mode, false if
    /// in the intended `rc` mode.
    pub fn ensure_ci_rc_mode(&self, force: bool) -> Result<(bool, RcCommitInfo)> {
        match self.execution_environment()? {
            ExecutionEnvironment::CiRcMode(dev, rci) => Ok((dev, rci)),

            ExecutionEnvironment::NotCi => {
                warn!("no CI environment detected; this is unexpected for this command");
                if force {
                    Ok((true, self.default_dev_rc_info()))
                } else {
                    Err(anyhow!(
                        "refusing to proceed (use \"force\" mode to override)",
                    ))
                }
            }

            _ => {
                warn!("unexpected CI environment detected");
                warn!("... this command should only be run in `rc` contexts");
                if force {
                    Ok((true, self.default_dev_rc_info()))
                } else {
                    Err(anyhow!(
                        "refusing to proceed (use \"force\" mode to override)",
                    ))
                }
            }
        }
    }

    /// Check that the working tree is completely clean. We allow untracked and
    /// ignored files but otherwise don't want any modifications, etc. Returns
    /// Ok if clean, an Err downcastable to DirtyRepositoryError if not. The
    /// error may have a different cause if, e.g., there is an I/O failure.
    pub fn ensure_fully_clean(&self) -> Result<()> {
        use crate::repository::DirtyRepositoryError;

        if let Some(changed_path) = self.repo.check_if_dirty(&[])? {
            Err(DirtyRepositoryError(changed_path).into())
        } else {
            Ok(())
        }
    }

    /// Check that the working tree is clean, excepting modifications to any
    /// files interpreted as changelogs. Returns Ok if clean, an Err
    /// downcastable to DirtyRepositoryError if not. The error may have a
    /// different cause if, e.g., there is an I/O failure.
    pub fn ensure_changelog_clean(&self) -> Result<()> {
        use crate::repository::DirtyRepositoryError;

        let mut matchers: Vec<Result<PathMatcher>> = self
            .graph
            .projects()
            .map(|p| p.changelog.create_path_matcher(p))
            .collect();
        let matchers: Result<Vec<PathMatcher>> = matchers.drain(..).collect();
        let matchers = matchers?;

        if let Some(changed_path) = self.repo.check_if_dirty(&matchers[..])? {
            Err(DirtyRepositoryError(changed_path).into())
        } else {
            Ok(())
        }
    }

    /// Get the graph of projects inside this app session.
    pub fn graph(&self) -> &ProjectGraph {
        &self.graph
    }

    /// Get the graph of projects inside this app session, mutably.
    pub fn graph_mut(&mut self) -> &mut ProjectGraph {
        &mut self.graph
    }

    /// Walk the project graph and solve internal dependencies.
    ///
    /// This method walks the graph in topologically-sorted order. For each
    /// project, the callback `process` is called, which should return true if a
    /// new release of the project is being scheduled. By the time the callback
    /// is called, the project's internal dependency information will have been
    /// updated: for DepRequirement::Commit deps, `resolved_version` will be a
    /// Some value containing the required version. It is possible that this
    /// version will be being released "right now".
    ///
    /// By the time the callback returns, the project's `version` field should
    /// have been updated with its reference version for this release process --
    /// which should be a new value, if the callback returns true.
    ///
    /// After processing all projects, the function will return an error if
    /// there are unsatisfiable internal dependencies. This can happen either
    /// because no sufficiently new release of the dependee exists (and it's not
    /// being released now), or the internal version requirement information
    /// hasn't been annotated.
    pub fn solve_internal_deps<F>(&mut self, mut process: F) -> Result<()>
    where
        F: FnMut(&mut Repository, &mut ProjectGraph, ProjectId) -> Result<bool>,
    {
        let mut new_versions: HashMap<ProjectId, Version> = HashMap::new();
        let toposorted_idents: Vec<_> = self.graph.toposorted().collect();
        let mut unsatisfied_deps = Vec::new();

        for ident in (toposorted_idents[..]).iter().copied() {
            // We can't conveniently navigate the deps while holding a mutable
            // ref to depending project, so do some lifetime futzing and buffer
            // up modifications to its dep info.

            unsatisfied_deps.clear();

            let mut resolved_versions = {
                let proj = self.graph.lookup(ident);
                let mut resolved_versions = Vec::new();

                for (idx, dep) in proj.internal_deps.iter().enumerate() {
                    match dep.cranko_requirement {
                        // If the requirement is of a specific commit, we need
                        // to resolve its corresponding release and/or make sure
                        // that the dependee project is also being released in
                        // this batch.
                        DepRequirement::Commit(ref cid) => {
                            let dependee_proj = self.graph.lookup(dep.ident);
                            let avail = self
                                .repo
                                .find_earliest_release_containing(dependee_proj, cid)?;

                            let resolved = match avail {
                                ReleaseAvailability::NotAvailable => {
                                    unsatisfied_deps
                                        .push(dependee_proj.user_facing_name.to_string());
                                    dependee_proj.version.clone()
                                }

                                ReleaseAvailability::ExistingRelease(ref v) => v.clone(),

                                ReleaseAvailability::NewRelease => {
                                    if let Some(v) = new_versions.get(&dep.ident) {
                                        v.clone()
                                    } else {
                                        unsatisfied_deps
                                            .push(dependee_proj.user_facing_name.to_string());
                                        dependee_proj.version.clone()
                                    }
                                }
                            };

                            resolved_versions.push((idx, resolved));
                        }

                        DepRequirement::Manual(_) => {}

                        DepRequirement::Unavailable => {
                            let dependee_proj = self.graph.lookup(dep.ident);
                            unsatisfied_deps.push(dependee_proj.user_facing_name.to_string());
                            resolved_versions.push((idx, dependee_proj.version.clone()));
                        }
                    }
                }

                resolved_versions
            };

            {
                let proj = self.graph.lookup_mut(ident);

                for (idx, resolved) in resolved_versions.drain(..) {
                    proj.internal_deps[idx].resolved_version = Some(resolved);
                }
            }

            // Now, let the callback do its thing with the project, and tell us
            // if it gets a new release.

            let updated_version = atry!(
                process(&mut self.repo, &mut self.graph, ident);
                ["failed to solve internal dependencies of project `{}`", self.graph.lookup(ident).user_facing_name]
            );

            let proj = self.graph.lookup(ident);

            if updated_version {
                if !unsatisfied_deps.is_empty() {
                    return Err(UnsatisfiedInternalRequirementError(
                        proj.user_facing_name.to_string(),
                        unsatisfied_deps.join(", "),
                    )
                    .into());
                }

                new_versions.insert(ident, proj.version.clone());
            } else if !unsatisfied_deps.is_empty() {
                warn!(
                    "project `{}` has internal requirements that won't be satisfiable in the wild, \
                     but that's OK since it's not going to be released",
                    proj.user_facing_name
                );
            }
        }

        Ok(())
    }

    /// A fake version of `solve_internal_deps`. Rather than properly expressing
    /// internal version requirements, this manually assigns each internal
    /// dependency to match exactly the version of the depended-upon package.
    /// This functionality is needed for Lerna, which otherwise isn't clever
    /// enough to correctly detect the internal dependency.
    pub fn fake_internal_deps(&mut self) {
        let toposorted_idents: Vec<_> = self.graph.toposorted().collect();

        for ident in (toposorted_idents[..]).iter().copied() {
            let mut resolved_versions = {
                let proj = self.graph.lookup(ident);
                let mut resolved_versions = Vec::new();

                for (idx, dep) in proj.internal_deps.iter().enumerate() {
                    let dependee_proj = self.graph.lookup(dep.ident);
                    resolved_versions.push((idx, dependee_proj.version.clone()));
                }

                resolved_versions
            };

            {
                let proj = self.graph.lookup_mut(ident);

                for (idx, resolved) in resolved_versions.drain(..) {
                    proj.internal_deps[idx].cranko_requirement =
                        DepRequirement::Manual(resolved.to_string());
                    proj.internal_deps[idx].resolved_version = Some(resolved);
                }
            }
        }
    }

    /// Apply version numbers given the current repository state and bump
    /// specifications.
    ///
    /// This also involves solving the version requirements for internal
    /// dependencies. If an internal dependency is unsatisfiable, the returned
    /// error will be downcastable to an UnsatisfiedInternalRequirementError.
    pub fn apply_versions(&mut self, rc_info: &RcCommitInfo) -> Result<()> {
        let latest_info = self.repo.get_latest_release_info()?;

        self.solve_internal_deps(|_repo, graph, ident| {
            let proj = graph.lookup_mut(ident);

            // Set the baseline version to the last release.

            let latest_release = latest_info.lookup_project(proj);

            proj.version = if let Some(info) = latest_release {
                proj.version.parse_like(&info.version)?
            } else {
                proj.version.zero_like()
            };

            let baseline_version = proj.version.clone();

            // If there's a bump, apply it.

            Ok(if let Some(rc) = rc_info.lookup_project(proj) {
                let scheme = proj.version.parse_bump_scheme(&rc.bump_spec)?;
                scheme.apply(&mut proj.version)?;
                info!(
                    "{}: {} => {}",
                    proj.user_facing_name, baseline_version, proj.version
                );
                true
            } else {
                info!(
                    "{}: unchanged from {}",
                    proj.user_facing_name, baseline_version
                );
                false
            })
        })
        .with_context(|| "failed to solve internal dependencies")?;

        Ok(())
    }

    /// Rewrite everyone's metadata to match our internal state.
    pub fn rewrite(&self) -> Result<ChangeList> {
        let mut changes = ChangeList::default();

        for ident in self.graph.toposorted() {
            let proj = self.graph.lookup(ident);

            for rw in &proj.rewriters {
                rw.rewrite(self, &mut changes)?;
            }
        }

        Ok(changes)
    }

    /// Like rewrite(), but only for the special Cranko requirements metadata.
    /// This is convenience functionality not needed for the main workflows.
    pub fn rewrite_cranko_requirements(&self) -> Result<ChangeList> {
        let mut changes = ChangeList::default();

        for ident in self.graph.toposorted() {
            let proj = self.graph.lookup(ident);

            for rw in &proj.rewriters {
                rw.rewrite_cranko_requirements(self, &mut changes)?;
            }
        }

        Ok(changes)
    }

    pub fn make_release_commit(&mut self, rci: &RcCommitInfo) -> Result<()> {
        self.repo.make_release_commit(&self.graph, rci)
    }

    pub fn make_rc_commit(
        &mut self,
        rcinfo: Vec<RcProjectInfo>,
        changes: &ChangeList,
    ) -> Result<()> {
        self.repo.make_rc_commit(rcinfo, changes)?;
        Ok(())
    }

    pub fn analyze_histories(&self) -> Result<RepoHistories> {
        self.graph.analyze_histories(&self.repo)
    }

    pub fn default_dev_rc_info(&self) -> RcCommitInfo {
        let mut rcinfo = RcCommitInfo::default();

        for proj in self.graph.projects() {
            rcinfo.projects.push(RcProjectInfo {
                qnames: proj.qualified_names().to_owned(),
                bump_spec: "dev-datecode".to_owned(),
            })
        }

        rcinfo
    }

    /// Rewrite all packages' changelogs to include their full release-branch
    /// content. Packages staged for release will have new entries created
    /// giving their final version numbers and other release information.
    pub fn apply_changelogs(
        &self,
        latest_release_commit: Option<CommitId>,
        rcinfo: &RcCommitInfo,
        changes: &mut ChangeList,
    ) -> Result<()> {
        // This step could plausibly be implemented in the "rewriter" framework,
        // probably? I dodn't have a great reason for doing otherwise, other
        // than that it seemed easier at the time.

        for ident in self.graph.toposorted() {
            let proj = self.graph.lookup(ident);

            if rcinfo.lookup_project(proj).is_some() {
                proj.changelog
                    .finalize_changelog(proj, &self.repo, changes)?;
            } else if let Some(cid) = latest_release_commit {
                // If the project is not being released, we still have to copy
                // out its most recent changelog so as not to lose it from the
                // release branch.
                proj.changelog.replace_changelog(proj, self, changes, cid)?;
            }
        }

        Ok(())
    }

    /// Create version control tags for new releases.
    pub fn create_tags(&mut self, rel_info: &ReleaseCommitInfo) -> Result<()> {
        for proj in self.graph.toposorted_mut() {
            if let Some(rel) = rel_info.lookup_if_released(proj) {
                self.repo.tag_project_at_head(proj, rel)?;
            }
        }

        Ok(())
    }
}

/// Different categorizations of the environment in which the program is
/// running.
pub enum ExecutionEnvironment {
    /// The program is running in a CI environment, in "release request" mode
    /// where we have not yet created a "release commit" with final version
    /// number information. If the boolean is true, we are in a development mode
    /// where version numbers are temporary and release artifacts will not be
    /// deployed.
    CiRcMode(bool, RcCommitInfo),

    /// The program is running in a CI environment, in a "release deployment"
    /// mode where HEAD is a Cranko release commit. If the boolean is true, we
    /// are in a development mode where version numbers are temporary and
    /// release artifacts will not be deployed (but this mode still can be
    /// useful for creating artifacts and so on).
    CiReleaseMode(bool, ReleaseCommitInfo),

    /// The program does not appear to be running in a CI environment. We infer
    /// that we're running in an individual development environment.
    NotCi,
}