Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 20 additions & 52 deletions src/changed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,46 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

//! Changed-file command execution.
//! Changed-file command helpers.

use std::{
ffi::OsStr,
path::{Path, PathBuf},
};
use std::path::Path;

use snafu::{ResultExt, Whatever};

use crate::{cli::ChangedFormat, config::RepositoryUse, git, layout::REPO_DIR};

pub(crate) fn is_proposal_path(mut p: PathBuf) -> bool {
// Only lint `content/00001.md` and `content/00001/index.md` files.

// content/00000.md | content/00000/index.md
// ^^^^^^^^ | ^^^^^^^^
match p.file_name() {
Some(n) if n == "index.md" => {
p.pop();
}
Some(_) if p.extension().map(|x| x == "md").unwrap_or(false) => {
p.set_extension("");
}
None | Some(_) => return false,
}

// content/00000
// ^^^^^
match p.file_name().and_then(OsStr::to_str) {
None => return false,
Some(f) if f.parse::<u64>().is_err() => return false,
Some(_) => {
p.pop();
}
}

// content
// ^^^^^^^
match p.file_name() {
Some(f) if f == "content" => {
p.pop();
}
_ => return false,
}

p == OsStr::new("")
}
use crate::{
cli::ChangedFormat, execution::ResolvedExecution, git, layout::REPO_DIR,
proposal::is_proposal_path,
};

pub(crate) fn run(
root_path: &Path,
resolved: &ResolvedExecution,
build_path: &Path,
repo_use: RepositoryUse,
all: bool,
format: &ChangedFormat,
) -> Result<(), Whatever> {
let repo_path = build_path.join(REPO_DIR);

let both = git::Fresh::new(root_path, &repo_path, repo_use)
.whatever_context("initializing build repo")?
.clone_src()
.whatever_context("cloning source repo")?
.fetch_upstream()
.whatever_context("fetching upstream repo")?;
let both = git::Fresh::new(
&resolved.root_path,
&repo_path,
resolved.repository_use.clone(),
resolved.source_materialization,
)
.whatever_context("initializing build repo")?
.clone_src()
.whatever_context("cloning source repo")?
.fetch_upstream()
.whatever_context("fetching upstream repo")?;

let changed_files: Vec<_> = both
.changed_files()
.whatever_context("unable to list changed files")?
.into_iter()
.filter(|p| all || is_proposal_path(p.into()))
.filter(|p| all || is_proposal_path(p))
.map(|p| repo_path.join(p))
.collect();

format.print(&changed_files, &repo_path);

Ok(())
}
Loading
Loading