Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 109 additions & 41 deletions src/app/actions/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,51 @@ pub(crate) struct ApplyPatchsetRequest {
pub patchset_path: String,
}

#[derive(Debug)]
pub(crate) struct AppliedPatchset {
pub message: String,
pub applied_branch: String,
}

pub(crate) fn apply_patchset(
request: &ApplyPatchsetRequest,
fs: &dyn FileSystemTrait,
shell: &dyn ShellTrait,
config: &ConfigSnapshot,
) -> Result<String, String> {
) -> Result<AppliedPatchset, String> {
let kernel_tree = validate_kernel_tree(fs, config)?;
check_git_state(fs, shell, kernel_tree)?;

let original_branch = get_current_branch(shell, kernel_tree)?;
let target_branch = create_target_branch(shell, kernel_tree, config)?;

let git_am_result = run_git_am(request, shell, kernel_tree, config);
switch_to_branch(shell, kernel_tree, &original_branch)?;

match git_am_result {
Ok(_) => Ok(format!(
" Patchset '{}' applied successfully!\n\n - Kernel Tree: '{}'\n\n - Base Branch: '{}'\n\n - Applied branch: '{}'",
request.patch_title,
kernel_tree.path(),
kernel_tree.branch(),
&target_branch
)),
Err(e) => Err(format!(" `git am` failed\n{}{}", &original_branch, e)),
Ok(_) => {
let current_branch = if config.stay_on_applied_branch() {
target_branch.clone()
} else {
switch_to_branch(shell, kernel_tree, &original_branch)?;
original_branch
};

Ok(AppliedPatchset {
message: format!(
" Patchset '{}' applied successfully!\n\n - Kernel Tree: '{}'\n\n - Base Branch: '{}'\n\n - Applied branch: '{}'\n\n - Current branch: '{}'",
request.patch_title,
kernel_tree.path(),
kernel_tree.branch(),
&target_branch,
current_branch
),
applied_branch: target_branch,
})
}
Err(e) => {
switch_to_branch(shell, kernel_tree, &original_branch)?;
Err(format!(" `git am` failed\n{}{}", &original_branch, e))
}
}
}

Expand Down Expand Up @@ -250,6 +271,8 @@ mod tests {
const BASE_BRANCH: &str = "main";
const PATCHSET_PATH: &str = "/tmp/patchset.mbx";

// `stay_on_applied_branch` is deliberately absent so the tests below
// exercise the serde default (true) that existing config files inherit.
fn config() -> ConfigSnapshot {
serde_json::from_value::<ConfigState>(serde_json::json!({
"kernel_trees": {
Expand All @@ -266,6 +289,23 @@ mod tests {
.to_snapshot()
}

fn config_stay_disabled() -> ConfigSnapshot {
serde_json::from_value::<ConfigState>(serde_json::json!({
"kernel_trees": {
"linux": {
"path": KERNEL_TREE_PATH,
"branch": BASE_BRANCH
}
},
"target_kernel_tree": "linux",
"git_am_options": "--signoff --3way",
"git_am_branch_prefix": "patchset-",
"stay_on_applied_branch": false
}))
.expect("test config should deserialize")
.to_snapshot()
}

fn config_without_target() -> ConfigSnapshot {
ConfigState::default().to_snapshot()
}
Expand Down Expand Up @@ -327,7 +367,7 @@ mod tests {
}

#[test]
fn apply_success_runs_expected_git_sequence() {
fn apply_success_stays_on_applied_branch_by_default() {
let fs = clean_fs();
let (shell, calls) = shell_with_outputs(vec![
output("", "", true),
Expand All @@ -336,14 +376,20 @@ mod tests {
output("", "", true),
output("", "", true),
output("", "", true),
output("", "", true),
]);

let result = apply_patchset(&request(), &fs, &shell, &config()).unwrap();
let applied = apply_patchset(&request(), &fs, &shell, &config()).unwrap();

assert!(result.contains("Patchset '[PATCH] test' applied successfully"));
assert!(result.contains("Applied branch: 'patchset-"));
assert!(applied.applied_branch.starts_with("patchset-"));
assert!(applied
.message
.contains("Patchset '[PATCH] test' applied successfully"));
assert!(applied.message.contains("Applied branch: 'patchset-"));
assert!(applied
.message
.contains(&format!("Current branch: '{}'", applied.applied_branch)));
let calls = calls.lock().unwrap();
assert_eq!(6, calls.len());
assert_eq!(
&calls[0],
&command(&["git", "-C", KERNEL_TREE_PATH, "status", "--porcelain"])
Expand Down Expand Up @@ -392,6 +438,26 @@ mod tests {
"--3way"
])
);
}

#[test]
fn apply_success_switches_back_when_stay_disabled() {
let fs = clean_fs();
let (shell, calls) = shell_with_outputs(vec![
output("", "", true),
output("", "", true),
output("feature\n", "", true),
output("", "", true),
output("", "", true),
output("", "", true),
output("", "", true),
]);

let applied = apply_patchset(&request(), &fs, &shell, &config_stay_disabled()).unwrap();

assert!(applied.message.contains("Current branch: 'feature'"));
let calls = calls.lock().unwrap();
assert_eq!(7, calls.len());
assert_eq!(
&calls[6],
&command(&["git", "-C", KERNEL_TREE_PATH, "switch", "feature"])
Expand Down Expand Up @@ -438,31 +504,33 @@ mod tests {

#[test]
fn failed_git_am_aborts_and_switches_back() {
let fs = clean_fs();
let (shell, calls) = shell_with_outputs(vec![
output("", "", true),
output("", "", true),
output("feature\n", "", true),
output("", "", true),
output("", "", true),
output("", "apply failed", false),
output("", "", true),
output("", "", true),
]);

let result = apply_patchset(&request(), &fs, &shell, &config()).unwrap_err();

assert!(result.contains("`git am` failed"));
assert!(result.contains("feature"));
assert!(result.contains("apply failed"));
let calls = calls.lock().unwrap();
assert_eq!(
&calls[6],
&command(&["git", "-C", KERNEL_TREE_PATH, "am", "--abort"])
);
assert_eq!(
&calls[7],
&command(&["git", "-C", KERNEL_TREE_PATH, "switch", "feature"])
);
for config in [config(), config_stay_disabled()] {
let fs = clean_fs();
let (shell, calls) = shell_with_outputs(vec![
output("", "", true),
output("", "", true),
output("feature\n", "", true),
output("", "", true),
output("", "", true),
output("", "apply failed", false),
output("", "", true),
output("", "", true),
]);

let result = apply_patchset(&request(), &fs, &shell, &config).unwrap_err();

assert!(result.contains("`git am` failed"));
assert!(result.contains("feature"));
assert!(result.contains("apply failed"));
let calls = calls.lock().unwrap();
assert_eq!(
&calls[6],
&command(&["git", "-C", KERNEL_TREE_PATH, "am", "--abort"])
);
assert_eq!(
&calls[7],
&command(&["git", "-C", KERNEL_TREE_PATH, "switch", "feature"])
);
}
}
}
4 changes: 2 additions & 2 deletions src/app/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
lore::application::handle::LoreApiHandle,
};

use apply::ApplyPatchsetRequest;
use apply::{AppliedPatchset, ApplyPatchsetRequest};
use reviewed_reply::{ReviewedReplyRequest, ReviewedReplyResult};

pub(crate) struct PatchsetActionService<'a> {
Expand All @@ -35,7 +35,7 @@ impl<'a> PatchsetActionService<'a> {
&self,
request: &ApplyPatchsetRequest,
config: &ConfigSnapshot,
) -> Result<String, String> {
) -> Result<AppliedPatchset, String> {
apply::apply_patchset(request, self.fs, self.shell, config)
}

Expand Down
3 changes: 3 additions & 0 deletions src/app/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ mod tests {
config::{ConfigHandle, ConfigState},
infrastructure::{file_system::MockFileSystemTrait, shell::MockShellTrait},
input::{event::InputEvent, handle::InputHandle, messages::InputMessage},
kw::history::MockKwHistoryStore,
lore::{
application::{
actor::LoreApiActor, cache::CacheTtl, handle::LoreApiHandle, service::LoreService,
Expand Down Expand Up @@ -232,6 +233,7 @@ mod tests {
shell: Box::new(MockShellTrait::new()),
fs: Box::new(MockFileSystemTrait::new()),
config: dummy_config_handle(),
kw_history: Arc::new(MockKwHistoryStore::new()),
},
}
}
Expand Down Expand Up @@ -333,6 +335,7 @@ mod tests {
Box::new(MockShellTrait::new()),
lore_api.clone(),
render.clone(),
Arc::new(MockKwHistoryStore::new()),
)
.expect("App::new must succeed");

Expand Down
4 changes: 4 additions & 0 deletions src/app/integration_tests/helpers/app_harness.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::sync::Arc;

use tokio::sync::mpsc;

use crate::{
app::App,
config::{ConfigHandle, ConfigState},
infrastructure::{file_system::MockFileSystemTrait, shell::MockShellTrait},
kw::history::MockKwHistoryStore,
lore::application::{cache::BootstrapLoreData, handle::LoreApiHandle},
render::handle::RenderHandle,
terminal::{handle::TerminalHandle, messages::TerminalMessage},
Expand Down Expand Up @@ -52,6 +55,7 @@ pub(crate) fn app_with_bootstrap_and_handles(
Box::new(MockShellTrait::new()),
lore_api,
render,
Arc::new(MockKwHistoryStore::new()),
)
.expect("minimal app should build")
}
Expand Down
Loading
Loading