Stack: Fix git repo new compose file initialization

This commit is contained in:
mbecker20
2025-03-01 11:39:44 -08:00
parent 0380bfb4b0
commit eb2ca707e2
7 changed files with 84 additions and 22 deletions
+1
View File
@@ -102,6 +102,7 @@ impl Resolve<WriteArgs> for RenameStack {
}
impl Resolve<WriteArgs> for WriteStackFileContents {
#[instrument(name = "WriteStackFileContents", skip(user))]
async fn resolve(
self,
WriteArgs { user }: &WriteArgs,
+2
View File
@@ -226,6 +226,7 @@ impl Resolve<WriteArgs> for WriteSyncFileContents {
&format!("{}: Commit Resource File", user.username),
&root,
&resource_path.join(&file_path),
&sync.config.branch,
)
.await;
@@ -353,6 +354,7 @@ impl Resolve<WriteArgs> for CommitSync {
&root,
&resource_path,
&res.toml,
&sync.config.branch,
)
.await
{
+8 -1
View File
@@ -268,7 +268,14 @@ impl Resolve<super::Args> for WriteCommitComposeContents {
hash,
message,
..
} = write_commit_file(&msg, &root, &file_path, &contents).await?;
} = write_commit_file(
&msg,
&root,
&file_path,
&contents,
&stack.config.branch,
)
.await?;
Ok(RepoActionResponse {
logs,
+2 -2
View File
@@ -72,9 +72,9 @@ where
}
Err(e) => {
logs.push(Log::simple(
"latest commit",
"Latest Commit",
format_serror(
&e.context("failed to get latest commit").into(),
&e.context("Failed to get latest commit").into(),
),
));
(None, None)
+41 -16
View File
@@ -17,6 +17,7 @@ pub async fn write_commit_file(
// relative to repo root
file: &Path,
contents: &str,
branch: &str,
) -> anyhow::Result<GitRes> {
// Clean up the path by stripping any redundant `/./`
let path = repo_dir.join(file).components().collect::<PathBuf>();
@@ -35,7 +36,8 @@ pub async fn write_commit_file(
format!("File contents written to {path:?}"),
));
commit_file_inner(commit_msg, &mut res, repo_dir, file).await;
commit_file_inner(commit_msg, &mut res, repo_dir, file, branch)
.await;
Ok(res)
}
@@ -47,9 +49,11 @@ pub async fn commit_file(
repo_dir: &Path,
// relative to repo root
file: &Path,
branch: &str,
) -> GitRes {
let mut res = GitRes::default();
commit_file_inner(commit_msg, &mut res, repo_dir, file).await;
commit_file_inner(commit_msg, &mut res, repo_dir, file, branch)
.await;
res
}
@@ -59,11 +63,12 @@ pub async fn commit_file_inner(
repo_dir: &Path,
// relative to repo root
file: &Path,
branch: &str,
) {
ensure_global_git_config_set().await;
let add_log = run_komodo_command(
"add files",
"Add Files",
repo_dir,
format!("git add {}", file.display()),
false,
@@ -75,7 +80,7 @@ pub async fn commit_file_inner(
}
let commit_log = run_komodo_command(
"commit",
"Commit",
repo_dir,
format!(
"git commit -m \"[Komodo] {commit_msg}: update {file:?}\"",
@@ -83,9 +88,15 @@ pub async fn commit_file_inner(
false,
)
.await;
res.logs.push(commit_log);
if !all_logs_success(&res.logs) {
return;
if !commit_log.success {
// The user may have nothing to commit, but still should continue push the changes
if !commit_log.stdout.contains("nothing to commit") {
res.logs.push(commit_log);
return;
}
} else {
res.logs.push(commit_log);
}
match get_commit_hash_log(repo_dir).await {
@@ -96,27 +107,36 @@ pub async fn commit_file_inner(
}
Err(e) => {
res.logs.push(Log::error(
"get commit hash",
"Get commit hash",
format_serror(&e.into()),
));
return;
}
};
let push_log =
run_komodo_command("push", repo_dir, "git push -f", false).await;
let push_log = run_komodo_command(
"Push",
repo_dir,
format!("git push -f --set-upstream origin {branch}"),
false,
)
.await;
res.logs.push(push_log);
}
/// Add, commit, and force push.
/// Repo must be cloned.
pub async fn commit_all(repo_dir: &Path, message: &str) -> GitRes {
pub async fn commit_all(
repo_dir: &Path,
message: &str,
branch: &str,
) -> GitRes {
ensure_global_git_config_set().await;
let mut res = GitRes::default();
let add_log =
run_komodo_command("add files", repo_dir, "git add -A", false)
run_komodo_command("Add Files", repo_dir, "git add -A", false)
.await;
res.logs.push(add_log);
if !all_logs_success(&res.logs) {
@@ -124,7 +144,7 @@ pub async fn commit_all(repo_dir: &Path, message: &str) -> GitRes {
}
let commit_log = run_komodo_command(
"commit",
"Commit",
repo_dir,
format!("git commit -m \"[Komodo] {message}\""),
false,
@@ -143,15 +163,20 @@ pub async fn commit_all(repo_dir: &Path, message: &str) -> GitRes {
}
Err(e) => {
res.logs.push(Log::error(
"get commit hash",
"Get commit hash",
format_serror(&e.into()),
));
return res;
}
};
let push_log =
run_komodo_command("push", repo_dir, "git push -f", false).await;
let push_log = run_komodo_command(
"Push",
repo_dir,
format!("git push -f --set-upstream origin {branch}"),
false,
)
.await;
res.logs.push(push_log);
res
+1 -1
View File
@@ -69,7 +69,7 @@ pub async fn get_commit_hash_log(
.to_string(),
);
let log = Log {
stage: "latest commit".into(),
stage: "Latest Commit".into(),
command,
stdout: format!(
"{} {}\n{} {}",
+29 -2
View File
@@ -90,6 +90,33 @@ where
env_file_path: None,
});
}
let repo_url = args.remote_url(access_token.as_deref())?;
// Set remote url
let mut set_remote = run_komodo_command(
"Set git remote",
folder_path.as_ref(),
format!("git remote add origin {repo_url}"),
false,
)
.await;
// Sanitize the output
if let Some(token) = &access_token {
set_remote.command =
set_remote.command.replace(token, "<TOKEN>");
set_remote.stdout =
set_remote.stdout.replace(token, "<TOKEN>");
set_remote.stderr =
set_remote.stderr.replace(token, "<TOKEN>");
}
if !set_remote.success {
logs.push(set_remote);
return Ok(GitRes {
logs,
hash: None,
message: None,
env_file_path: None,
});
}
}
let repo_url = args.remote_url(access_token.as_deref())?;
@@ -174,9 +201,9 @@ where
}
Err(e) => {
logs.push(Log::simple(
"Latest commit",
"Latest Commit",
format_serror(
&e.context("failed to get latest commit").into(),
&e.context("Failed to get latest commit").into(),
),
));
(None, None)