From eb2ca707e2d11ca73a4a15acfbc335bf052ce746 Mon Sep 17 00:00:00 2001 From: mbecker20 Date: Sat, 1 Mar 2025 11:39:44 -0800 Subject: [PATCH] Stack: Fix git repo new compose file initialization --- bin/core/src/api/write/stack.rs | 1 + bin/core/src/api/write/sync.rs | 2 ++ bin/periphery/src/api/compose.rs | 9 ++++- lib/git/src/clone.rs | 4 +-- lib/git/src/commit.rs | 57 +++++++++++++++++++++++--------- lib/git/src/lib.rs | 2 +- lib/git/src/pull.rs | 31 +++++++++++++++-- 7 files changed, 84 insertions(+), 22 deletions(-) diff --git a/bin/core/src/api/write/stack.rs b/bin/core/src/api/write/stack.rs index 5ec4c3d16..b9c9232ee 100644 --- a/bin/core/src/api/write/stack.rs +++ b/bin/core/src/api/write/stack.rs @@ -102,6 +102,7 @@ impl Resolve for RenameStack { } impl Resolve for WriteStackFileContents { + #[instrument(name = "WriteStackFileContents", skip(user))] async fn resolve( self, WriteArgs { user }: &WriteArgs, diff --git a/bin/core/src/api/write/sync.rs b/bin/core/src/api/write/sync.rs index 47ec02b3b..3c80b0531 100644 --- a/bin/core/src/api/write/sync.rs +++ b/bin/core/src/api/write/sync.rs @@ -226,6 +226,7 @@ impl Resolve for WriteSyncFileContents { &format!("{}: Commit Resource File", user.username), &root, &resource_path.join(&file_path), + &sync.config.branch, ) .await; @@ -353,6 +354,7 @@ impl Resolve for CommitSync { &root, &resource_path, &res.toml, + &sync.config.branch, ) .await { diff --git a/bin/periphery/src/api/compose.rs b/bin/periphery/src/api/compose.rs index 4b5f59f81..23fd6c7cc 100644 --- a/bin/periphery/src/api/compose.rs +++ b/bin/periphery/src/api/compose.rs @@ -268,7 +268,14 @@ impl Resolve 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, diff --git a/lib/git/src/clone.rs b/lib/git/src/clone.rs index 57095dbda..f018fb8cd 100644 --- a/lib/git/src/clone.rs +++ b/lib/git/src/clone.rs @@ -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) diff --git a/lib/git/src/commit.rs b/lib/git/src/commit.rs index bc3361899..d01d3cd8d 100644 --- a/lib/git/src/commit.rs +++ b/lib/git/src/commit.rs @@ -17,6 +17,7 @@ pub async fn write_commit_file( // relative to repo root file: &Path, contents: &str, + branch: &str, ) -> anyhow::Result { // Clean up the path by stripping any redundant `/./` let path = repo_dir.join(file).components().collect::(); @@ -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 diff --git a/lib/git/src/lib.rs b/lib/git/src/lib.rs index 26ee2176b..8191eb18b 100644 --- a/lib/git/src/lib.rs +++ b/lib/git/src/lib.rs @@ -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{} {}", diff --git a/lib/git/src/pull.rs b/lib/git/src/pull.rs index 2688a6f9c..0e05e70e0 100644 --- a/lib/git/src/pull.rs +++ b/lib/git/src/pull.rs @@ -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, ""); + set_remote.stdout = + set_remote.stdout.replace(token, ""); + set_remote.stderr = + set_remote.stderr.replace(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)