diff --git a/src/cook/fetch.rs b/src/cook/fetch.rs index e5267fa74..8c30205ef 100644 --- a/src/cook/fetch.rs +++ b/src/cook/fetch.rs @@ -252,22 +252,9 @@ pub fn fetch(recipe: &CookRecipe, check_source: bool, logger: &PtyOut) -> Result } } } - (None, false) => { - let remote = get_git_remote_tracking(&source_dir)?; - if remote.local_branch != remote.tracking_branch { - false - } else if let Some(branch) = branch - && branch != &remote.tracking_branch - { - false - } else if branch.is_none() && remote.remote_branch != remote.tracking_branch - { - false - } else if remote.remote_name != "origin" - || &remote.remote_url != chop_dot_git(git) - { - false - } else { + (None, false) => match get_git_remote_tracking(&source_dir) { + Ok(remote) if !remote.check_updated(git, branch) => false, + Ok(remote) => { git_run_fetch(logger, &source_dir, git)?; fetch_is_ran = true; match get_git_fetch_rev( @@ -282,7 +269,11 @@ pub fn fetch(recipe: &CookRecipe, check_source: bool, logger: &PtyOut) -> Result } } } - } + Err(e) => { + log_to_pty!(logger, "{}", e); + false + } + }, _ => false, } }; @@ -312,14 +303,17 @@ pub fn fetch(recipe: &CookRecipe, check_source: bool, logger: &PtyOut) -> Result command.arg("-C").arg(&source_dir); command.arg("checkout").arg(rev); run_command(command, logger)?; - } else if !is_redox() { - //TODO: complicated stuff to check and reset branch to origin - //TODO: redox can't undestand this (got exit status 1) - let mut command = Command::new("bash"); - command.arg("-c").arg(GIT_RESET_BRANCH); - if let Some(branch) = branch { - command.env("BRANCH", branch); - } + } else { + let branch = match branch { + Some(branch) => branch.clone(), + None => get_git_remote_branch(&source_dir, "origin")?, + }; + let mut command = Command::new("git"); + command + .arg("checkout") + .arg("-B") + .arg(&branch) + .arg(format!("remotes/origin/{branch}")); command.current_dir(&source_dir); run_command(command, logger)?; } diff --git a/src/cook/fs.rs b/src/cook/fs.rs index c7fe1c0e2..c3c5f864f 100644 --- a/src/cook/fs.rs +++ b/src/cook/fs.rs @@ -388,6 +388,23 @@ impl GitRemoteTracking { ..Default::default() } } + pub fn check_updated(&self, url: &str, branch: &Option) -> bool { + if self.local_branch != self.tracking_branch { + return false; + } + if let Some(branch) = branch + && branch != &self.tracking_branch + { + return false; + } + if branch.is_none() && self.remote_branch != self.tracking_branch { + return false; + } + if self.remote_name != "origin" || &self.remote_url != chop_dot_git(url) { + return false; + } + true + } } pub fn get_git_remote_tracking(dir: &PathBuf) -> Result { @@ -470,6 +487,17 @@ pub fn get_git_remote_tracking(dir: &PathBuf) -> Result { remote_name ))?; + let remote_branch = get_git_remote_branch(dir, &remote_name)?; + Ok(GitRemoteTracking { + local_branch, + tracking_branch, + remote_name, + remote_branch, + remote_url, + }) +} + +pub fn get_git_remote_branch(dir: &PathBuf, remote_name: &str) -> Result { let remote_branch = { let head_path = format!(".git/refs/remotes/{remote_name}/HEAD"); let git_remote_head = dir.join(&head_path); @@ -482,16 +510,10 @@ pub fn get_git_remote_tracking(dir: &PathBuf) -> Result { ))?; get_git_branch_name(path.trim_end())? }; - Ok(GitRemoteTracking { - local_branch, - tracking_branch, - remote_name, - remote_branch, - remote_url, - }) + Ok(remote_branch) } -pub(crate) fn chop_dot_git(url: &str) -> &str { +fn chop_dot_git(url: &str) -> &str { if url.ends_with(".git") { return &url[..url.len() - ".git".len()]; } diff --git a/src/cook/script.rs b/src/cook/script.rs index f3f32eef0..8db592156 100644 --- a/src/cook/script.rs +++ b/src/cook/script.rs @@ -401,18 +401,6 @@ do done "#; -pub(crate) static GIT_RESET_BRANCH: &str = r#" -ORIGIN_BRANCH="$(git branch --remotes | grep '^ origin/HEAD -> ' | cut -d ' ' -f 5-)" -if [ -n "$BRANCH" ] -then - ORIGIN_BRANCH="origin/$BRANCH" -fi - -if [ "$(git rev-parse HEAD)" != "$(git rev-parse $ORIGIN_BRANCH)" ] -then - git checkout -B "$(echo "$ORIGIN_BRANCH" | cut -d / -f 2-)" "$ORIGIN_BRANCH" -fi"#; - pub static KILL_ALL_PID: &str = r#" THISPID=$$ CHILDREN=$(ps -o pid= --ppid $PID | grep -v $THISPID);