Skip to content

Commit

Permalink
remove duplicate SCM initializations
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon committed Jan 31, 2024
1 parent 287d54c commit 939a0a7
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 20 deletions.
4 changes: 2 additions & 2 deletions crates/turborepo-lib/src/daemon/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ where

let package_discovery = Arc::new(AsyncMutex::new(package_discovery));

let scm = SCM::new(&repo_root);
let scm = Box::leak(Box::new(SCM::new(&repo_root)));

let package_json_path = repo_root.join_component("package.json");
let root_package_json = PackageJson::load(&package_json_path).unwrap();
Expand Down Expand Up @@ -330,7 +330,7 @@ where
repo_root,
);

tracing::debug!("initing package hash watcher");
tracing::debug!("initializing package hash watcher");
let package_hashes = AsyncMutex::new(
WatchingPackageHasher::new(
package_discovery.clone(),
Expand Down
8 changes: 6 additions & 2 deletions crates/turborepo-lib/src/run/global_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub fn get_global_hash_inputs<'a, L: ?Sized + Lockfile>(
env_mode: EnvMode,
framework_inference: bool,
dot_env: Option<&'a [RelativeUnixPathBuf]>,
hasher: &SCM,
) -> Result<GlobalHashableInputs<'a>, Error> {
let global_hashable_env_vars =
get_global_hashable_env_vars(env_at_execution_start, global_env)?;
Expand All @@ -84,8 +85,6 @@ pub fn get_global_hash_inputs<'a, L: ?Sized + Lockfile>(
}
}

let hasher = SCM::new(root_path);

let global_deps_paths = global_deps
.iter()
.map(|p| root_path.anchor(p).expect("path should be from root"))
Expand Down Expand Up @@ -222,6 +221,7 @@ mod tests {
use turborepo_env::EnvironmentVariableMap;
use turborepo_lockfiles::Lockfile;
use turborepo_repository::package_manager::PackageManager;
use turborepo_scm::SCM;

use super::get_global_hash_inputs;
use crate::{cli::EnvMode, run::global_hash::collect_global_deps};
Expand All @@ -243,6 +243,9 @@ mod tests {

let env_var_map = EnvironmentVariableMap::default();
let lockfile: Option<&dyn Lockfile> = None;

let scm = SCM::new(&root);

#[cfg(windows)]
let file_deps = ["C:\\some\\path".to_string()];
#[cfg(not(windows))]
Expand All @@ -259,6 +262,7 @@ mod tests {
EnvMode::Infer,
false,
None,
&scm,
);
assert!(result.is_ok());
}
Expand Down
4 changes: 3 additions & 1 deletion crates/turborepo-lib/src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ impl Run {
self.opts.run_opts.env_mode,
self.opts.run_opts.framework_inference,
root_turbo_json.global_dot_env.as_deref(),
&scm,
)?;

let global_hash = global_hash_inputs.calculate_global_hash_from_inputs();
Expand All @@ -391,7 +392,7 @@ impl Run {
} else {
(
Some(package_hashes::LocalPackageHashes::new(
scm,
&scm,
pkg_dep_graph
.workspaces()
.map(|(name, info)| (name.to_owned(), info.to_owned()))
Expand Down Expand Up @@ -482,6 +483,7 @@ impl Run {
api_client,
self.api_auth.clone(),
Vendor::get_user(),
Some(&scm),
);

let mut visitor = Visitor::new(
Expand Down
10 changes: 5 additions & 5 deletions crates/turborepo-lib/src/run/package_hashes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ pub trait PackageHasher {
) -> impl std::future::Future<Output = Result<PackageInputsHashes, Error>> + Send;
}

pub struct LocalPackageHashes {
scm: SCM,
pub struct LocalPackageHashes<'a> {
scm: &'a SCM,
workspaces: HashMap<WorkspaceName, WorkspaceInfo>,
tasks: Vec<TaskNode>,
task_definitions: HashMap<TaskId<'static>, TaskDefinition>,
repo_root: AbsoluteSystemPathBuf,
}

impl LocalPackageHashes {
impl<'a> LocalPackageHashes<'a> {
pub fn new(
scm: SCM,
scm: &'a SCM,
workspaces: HashMap<WorkspaceName, WorkspaceInfo>,
tasks: impl Iterator<Item = TaskNode>,
task_definitions: HashMap<TaskId<'static>, TaskDefinition>,
Expand All @@ -55,7 +55,7 @@ impl LocalPackageHashes {
}
}

impl PackageHasher for LocalPackageHashes {
impl<'a> PackageHasher for LocalPackageHashes<'a> {
async fn calculate_hashes(
&mut self,
run_telemetry: GenericEventBuilder,
Expand Down
2 changes: 2 additions & 0 deletions crates/turborepo-lib/src/run/package_hashes/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ impl<PD, PH: PackageHasher> WatchingPackageHasher<PD, PH> {
interval: Duration,
watcher_rx: watch::Receiver<Option<Arc<FileWatching>>>,
) -> Self {
tracing::debug!("pre-populating package-task hashes");
let map = Arc::new(Mutex::new(
fallback
.calculate_hashes(Default::default())
.await
.unwrap()
.hashes,
));
tracing::debug!("done pre-populating");

// listen to updates from the file watcher and update the map
let _subscriber = tokio::task::spawn({
Expand Down
4 changes: 3 additions & 1 deletion crates/turborepo-lib/src/run/summary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use turbopath::{AbsoluteSystemPath, AbsoluteSystemPathBuf, AnchoredSystemPath};
use turborepo_api_client::{spaces::CreateSpaceRunPayload, APIAuth, APIClient};
use turborepo_env::EnvironmentVariableMap;
use turborepo_repository::package_graph::{PackageGraph, WorkspaceName};
use turborepo_scm::SCM;
use turborepo_ui::{color, cprintln, cwriteln, BOLD, BOLD_CYAN, GREY, UI};

use self::{
Expand Down Expand Up @@ -154,8 +155,9 @@ impl RunTracker {
spaces_api_client: APIClient,
api_auth: Option<APIAuth>,
user: String,
scm: Option<&SCM>,
) -> Self {
let scm = SCMState::get(env_at_execution_start, repo_root);
let scm = SCMState::get(env_at_execution_start, repo_root, scm);

let spaces_client_handle =
SpacesClient::new(spaces_id.clone(), spaces_api_client, api_auth).and_then(
Expand Down
12 changes: 10 additions & 2 deletions crates/turborepo-lib/src/run/summary/scm.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

use serde::{Deserialize, Serialize};
use turbopath::AbsoluteSystemPath;
use turborepo_ci::Vendor;
Expand All @@ -19,7 +21,11 @@ pub(crate) struct SCMState {
}

impl SCMState {
pub fn get(env_vars: &EnvironmentVariableMap, dir: &AbsoluteSystemPath) -> Self {
pub fn get(
env_vars: &EnvironmentVariableMap,
dir: &AbsoluteSystemPath,
scm: Option<&SCM>,
) -> Self {
let mut state = SCMState {
ty: SCMType::Git,
sha: None,
Expand All @@ -40,7 +46,9 @@ impl SCMState {

// Fall back to using `git`
if state.branch.is_none() && state.sha.is_none() {
let scm = SCM::new(dir);
let scm = scm
.map(Cow::Borrowed)
.unwrap_or_else(|| Cow::Owned(SCM::new(dir)));

if state.branch.is_none() {
state.branch = scm.get_current_branch(dir).ok();
Expand Down
7 changes: 2 additions & 5 deletions crates/turborepo-scm/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,11 @@ impl SCM {
///
/// returns: Result<HashSet<String, RandomState>, Error>
pub fn changed_files(
git_root: PathBuf,
turbo_root: PathBuf,
from_commit: Option<&str>,
to_commit: &str,
scm: &SCM,
) -> Result<HashSet<String>, Error> {
let git_root = AbsoluteSystemPath::from_std_path(&git_root)?;
let scm = SCM::new(git_root);

let turbo_root = AbsoluteSystemPathBuf::try_from(turbo_root.as_path())?;
let files = scm.changed_files(&turbo_root, from_commit, to_commit)?;
Ok(files
Expand Down Expand Up @@ -210,10 +207,10 @@ pub fn previous_content(
git_root: PathBuf,
from_commit: &str,
file_path: String,
scm: &SCM,
) -> Result<Vec<u8>, Error> {
// If git root is not absolute, we error.
let git_root = AbsoluteSystemPathBuf::try_from(git_root)?;
let scm = SCM::new(&git_root);

// However for file path we handle both absolute and relative paths
// Note that we assume any relative file path is relative to the git root
Expand Down
4 changes: 2 additions & 2 deletions crates/turborepo-scm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub(crate) fn wait_for_success<R: Read, T>(
Err(Error::Git(err_text, Backtrace::capture()))
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Git {
root: AbsoluteSystemPathBuf,
bin: AbsoluteSystemPathBuf,
Expand Down Expand Up @@ -191,7 +191,7 @@ fn find_git_root(turbo_root: &AbsoluteSystemPath) -> Result<AbsoluteSystemPathBu
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum SCM {
Git(Git),
Manual,
Expand Down

0 comments on commit 939a0a7

Please sign in to comment.