Replace git checkout branch logic from bash

This commit is contained in:
Wildan M 2026-05-26 22:52:51 +07:00
parent 67dccc053a
commit 0fd6b596ef
No known key found for this signature in database
GPG Key ID: 01AC53185C679C79
3 changed files with 49 additions and 45 deletions

View File

@ -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)?;
}

View File

@ -388,6 +388,23 @@ impl GitRemoteTracking {
..Default::default()
}
}
pub fn check_updated(&self, url: &str, branch: &Option<String>) -> 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<GitRemoteTracking> {
@ -470,6 +487,17 @@ pub fn get_git_remote_tracking(dir: &PathBuf) -> Result<GitRemoteTracking> {
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<String> {
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<GitRemoteTracking> {
))?;
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()];
}

View File

@ -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);