From 75f33bd4230eb563241adba1e44952d632022cd9 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Mon, 4 Jul 2022 23:52:16 -0700 Subject: [PATCH] ci: Use base/head from GH environment. (#180) * Use env vars. * Fix git. * Try again. * Try github context. * Prepend head with prefix. * Fix unexpected rev. --- .github/workflows/moon.yml | 2 +- .../src/actions/target/hasher.rs | 5 ++-- crates/vcs/src/git.rs | 29 ++++++++++++++----- packages/cli/CHANGELOG.md | 2 ++ 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/.github/workflows/moon.yml b/.github/workflows/moon.yml index 1a9d4c37a8c..af312ee7366 100644 --- a/.github/workflows/moon.yml +++ b/.github/workflows/moon.yml @@ -60,4 +60,4 @@ jobs: - uses: actions-rs/cargo@v1 with: command: run - args: -- --color --log trace ci + args: -- --color --log trace ci --base ${{ github.base_ref || 'master' }} diff --git a/crates/action-runner/src/actions/target/hasher.rs b/crates/action-runner/src/actions/target/hasher.rs index d6734574c3e..1a7a3a5ad4f 100644 --- a/crates/action-runner/src/actions/target/hasher.rs +++ b/crates/action-runner/src/actions/target/hasher.rs @@ -67,9 +67,10 @@ pub async fn create_target_hasher( // For input files, hash them with the vcs layer first if !task.input_paths.is_empty() { let files = convert_paths_to_strings(&task.input_paths, &workspace.root)?; - let hashed_files = vcs.get_file_hashes(&files).await?; - hasher.hash_inputs(hashed_files); + if !files.is_empty() { + hasher.hash_inputs(vcs.get_file_hashes(&files).await?); + } } // For input globs, it's much more performant to: diff --git a/crates/vcs/src/git.rs b/crates/vcs/src/git.rs index 64124b11c85..77569e6d466 100644 --- a/crates/vcs/src/git.rs +++ b/crates/vcs/src/git.rs @@ -2,8 +2,8 @@ use crate::errors::VcsError; use crate::vcs::{TouchedFiles, Vcs, VcsResult}; use async_trait::async_trait; use ignore::gitignore::{Gitignore, GitignoreBuilder}; -use moon_utils::fs; use moon_utils::process::{output_to_string, output_to_trimmed_string, Command}; +use moon_utils::{fs, string_vec}; use regex::Regex; use std::collections::{BTreeMap, HashMap, HashSet}; use std::path::{Path, PathBuf}; @@ -41,24 +41,38 @@ impl Git { } async fn get_merge_base(&self, base: &str, head: &str) -> VcsResult { - let candidates = [ + let mut args = string_vec!["merge-base", head]; + + // To start, we need to find a working base origin + for candidate in [ base.to_owned(), format!("origin/{}", base), format!("upstream/{}", base), - ]; - - for candidate in candidates { - if let Ok(hash) = self + ] { + if self .run_command( &mut self.create_command(vec!["merge-base", &candidate, head]), true, ) .await + .is_ok() { - return Ok(hash); + args.push(candidate.clone()); } } + // Then we need to run it again and extract the base hash using the found origins + // This is necessary to support comparisons between forks! + if let Ok(hash) = self + .run_command( + &mut self.create_command(args.iter().map(|a| a.as_str()).collect()), + true, + ) + .await + { + return Ok(hash); + } + Ok(base.to_owned()) } @@ -322,7 +336,6 @@ impl Vcs for Git { // are displayed as-is and are not quoted/escaped "-z", &base, - revision, ]), false, ) diff --git a/packages/cli/CHANGELOG.md b/packages/cli/CHANGELOG.md index c6ee6520f03..59b677acb68 100644 --- a/packages/cli/CHANGELOG.md +++ b/packages/cli/CHANGELOG.md @@ -14,6 +14,8 @@ #### 🐞 Fixes - Fixed an issue with a globally installed moon not being executable in PowerShell. +- Fixed an issue with empty files being passed to `git hash-object`. +- Fixed an issue where a `git merge-base` could not be resolved when base and head are provided. #### ⚙️ Internal