Skip to content

Commit

Permalink
Integrate with Sapling or Git if they exist
Browse files Browse the repository at this point in the history
  • Loading branch information
bigfootjon committed May 4, 2024
1 parent cd9246e commit a0e58ea
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 45 deletions.
2 changes: 1 addition & 1 deletion compiler/crates/relay-compiler/src/build_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use rustc_hash::FxHashSet;
use schema::SDLSchema;
use schema_diff::check::IncrementalBuildSchemaChange;
use schema_diff::check::SchemaChangeSafety;
pub use source_control::add_to_mercurial;
pub use source_control::source_control_fn;
pub use validate::validate;
pub use validate::AdditionalValidations;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use serde::Serializer;
use sha1::Digest;
use sha1::Sha1;

use crate::build_project::source_control::SourceControlFn;
use crate::errors::BuildProjectError;
use crate::errors::Error;

Expand All @@ -40,8 +41,6 @@ pub trait ArtifactWriter {
fn finalize(&self) -> crate::errors::Result<()>;
}

type SourceControlFn =
fn(&PathBuf, &Mutex<Vec<PathBuf>>, &Mutex<Vec<PathBuf>>) -> crate::errors::Result<()>;
#[derive(Default)]
pub struct ArtifactFileWriter {
added: Mutex<Vec<PathBuf>>,
Expand Down
155 changes: 116 additions & 39 deletions compiler/crates/relay-compiler/src/build_project/source_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,127 @@ use std::sync::Mutex;

use log::info;

pub fn add_to_mercurial(
root_dir: &PathBuf,
added: &Mutex<Vec<PathBuf>>,
removed: &Mutex<Vec<PathBuf>>,
) -> crate::errors::Result<()> {
{
let mut added = added.lock().unwrap();
if !added.is_empty() {
for added_files in added.chunks(100) {
if Command::new("hg")
.current_dir(root_dir)
.arg("add")
.args(added_files)
.stdout(Stdio::null())
.stderr(Stdio::null())
.stdin(Stdio::null())
.spawn()
.is_err()
{
info!("Failed to run `hg add`.");
pub type SourceControlFn =
fn(&PathBuf, &Mutex<Vec<PathBuf>>, &Mutex<Vec<PathBuf>>) -> crate::errors::Result<()>;

enum SourceControl {
Sapling, // Meta's fork of Mercurial
Git,
}

impl SourceControl {
fn start_tracking_command(&self) -> Command {
let mut command = match self {
Self::Sapling => Command::new("sl"),
Self::Git => Command::new("git"),
};

match self {
Self::Sapling => command.arg("add"),
Self::Git => command.arg("add"),
};

command
}

fn stop_tracking_command(&self) -> Command {
let mut command = match self {
Self::Sapling => Command::new("sl"),
Self::Git => Command::new("git"),
};

match self {
Self::Sapling => command.arg("forget"),
Self::Git => command.arg("rm").arg("--cached"),
};

command
}

fn add_and_remove_files(
&self,
root_dir: &PathBuf,
added: &Mutex<Vec<PathBuf>>,
removed: &Mutex<Vec<PathBuf>>,
) -> crate::errors::Result<()> {
{
let mut added = added.lock().unwrap();
if !added.is_empty() {
for added_files in added.chunks(100) {
if self
.start_tracking_command()
.current_dir(root_dir)
.args(added_files)
.stdout(Stdio::null())
.stderr(Stdio::null())
.stdin(Stdio::null())
.spawn()
.is_err()
{
info!("Failed to run `hg add`.");
}
}
added.clear();
}
added.clear();
}
}
{
let mut removed = removed.lock().unwrap();
if !removed.is_empty() {
for removed_files in removed.chunks(100) {
if Command::new("hg")
.current_dir(root_dir)
.arg("forget")
.args(removed_files)
.stdout(Stdio::null())
.stderr(Stdio::null())
.stdin(Stdio::null())
.spawn()
.is_err()
{
info!("Failed to run `hg forget`.");
{
let mut removed = removed.lock().unwrap();
if !removed.is_empty() {
for removed_files in removed.chunks(100) {
if self
.stop_tracking_command()
.current_dir(root_dir)
.args(removed_files)
.stdout(Stdio::null())
.stderr(Stdio::null())
.stdin(Stdio::null())
.spawn()
.is_err()
{
info!("Failed to run `hg forget`.");
}
}
removed.clear();
}
removed.clear();
}
Ok(())
}
}

pub fn source_control_fn(root_dir: &PathBuf) -> Option<SourceControlFn> {
let check_sapling = Command::new("sl")
.arg("root")
.current_dir(root_dir)
.output();

if check_sapling.is_ok() {
return Some(add_to_sapling);
}

let check_git = Command::new("git")
.arg("status")
.current_dir(root_dir)
.output();

if check_git.is_ok() {
return Some(add_to_git);
}
Ok(())

None
}

pub fn add_to_sapling(
root_dir: &PathBuf,
added: &Mutex<Vec<PathBuf>>,
removed: &Mutex<Vec<PathBuf>>,
) -> crate::errors::Result<()> {
SourceControl::Sapling.add_and_remove_files(root_dir, added, removed)
}

pub fn add_to_git(
root_dir: &PathBuf,
added: &Mutex<Vec<PathBuf>>,
removed: &Mutex<Vec<PathBuf>>,
) -> crate::errors::Result<()> {
SourceControl::Git.add_and_remove_files(root_dir, added, removed)
}
12 changes: 10 additions & 2 deletions compiler/crates/relay-compiler/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ use crate::errors::ConfigValidationError;
use crate::errors::Error;
use crate::errors::Result;
use crate::saved_state::SavedStateLoader;
use crate::source_control_fn;
use crate::status_reporter::ConsoleStatusReporter;
use crate::status_reporter::StatusReporter;

Expand Down Expand Up @@ -418,7 +419,10 @@ impl Config {

let config = Self {
name: config_file.name,
artifact_writer: Box::new(ArtifactFileWriter::new(None, root_dir.clone())),
artifact_writer: Box::new(ArtifactFileWriter::new(
source_control_fn(&root_dir),
root_dir.clone(),
)),
status_reporter: Box::new(ConsoleStatusReporter::new(
root_dir.clone(),
is_multi_project,
Expand Down Expand Up @@ -602,7 +606,11 @@ impl fmt::Debug for Config {
} = self;

fn option_fn_to_string<T>(option: &Option<T>) -> &'static str {
if option.is_some() { "Some(Fn)" } else { "None" }
if option.is_some() {
"Some(Fn)"
} else {
"None"
}
}

f.debug_struct("Config")
Expand Down
2 changes: 1 addition & 1 deletion compiler/crates/relay-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub mod status_reporter;
mod utils;

pub use artifact_map::ArtifactSourceKey;
pub use build_project::add_to_mercurial;
pub use build_project::source_control_fn;
pub use build_project::artifact_writer::ArtifactDifferenceShardedWriter;
pub use build_project::artifact_writer::ArtifactDifferenceWriter;
pub use build_project::artifact_writer::ArtifactFileWriter;
Expand Down

0 comments on commit a0e58ea

Please sign in to comment.