1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//! Validation functions for worktree merge operations
//!
//! This module contains pure validation functions that check preconditions,
//! verify merge success, and validate command results for worktree operations.
//! All functions in this module are stateless and focus on validation logic
//! without performing I/O operations.
//!
//! # Responsibilities
//!
//! - Merge precondition validation
//! - Claude command result verification
//! - Merge completion checks
//! - Permission denial detection
//! - Branch merge status validation
//!
//! # Design Principles
//!
//! Functions in this module follow functional programming principles:
//! - Pure functions that take inputs and return outputs
//! - No side effects or I/O operations
//! - Stateless validation logic
//! - Clear error messages with context
use Result;
/// Determine if merge should proceed based on commit count between branches
///
/// A merge should proceed when there are commits in the worktree branch that
/// are not in the target branch (commit count != "0").
///
/// # Arguments
///
/// * `commit_count` - String representation of the number of commits ahead
///
/// # Returns
///
/// * `true` if merge should proceed (has commits to merge)
/// * `false` if no commits to merge
///
/// # Examples
///
/// ```
/// use prodigy::worktree::manager_validation::should_proceed_with_merge;
///
/// assert!(should_proceed_with_merge("5"));
/// assert!(!should_proceed_with_merge("0"));
/// ```
/// Validate Claude command execution result
///
/// Checks if the Claude command execution was successful and provides
/// detailed error information if it failed.
///
/// # Arguments
///
/// * `result` - The execution result from Claude command
///
/// # Returns
///
/// * `Ok(())` if execution was successful
/// * `Err` with details if execution failed
///
/// # Errors
///
/// Returns error if:
/// - Command execution failed (success = false)
/// - Error output indicates failure
/// Validate that merge was successful by checking branch merge status
///
/// Verifies that the worktree branch has been successfully merged into the
/// target branch by checking if it appears in the list of merged branches.
/// Provides specific error messages for permission denial cases.
///
/// # Arguments
///
/// * `worktree_branch` - Name of the worktree branch that should be merged
/// * `target_branch` - Name of the target branch (e.g., main or master)
/// * `merged_branches` - Output from git showing merged branches
/// * `merge_output` - Output from the merge command (for error diagnosis)
///
/// # Returns
///
/// * `Ok(())` if branch is successfully merged
/// * `Err` with detailed explanation if merge failed
///
/// # Errors
///
/// Returns error if:
/// - Branch is not found in merged branches list
/// - Permission was denied during merge
/// - Merge was aborted or failed silently
/// Check if command output indicates permission was denied
///
/// Analyzes command output to detect if the operation failed due to
/// permission denial, which requires user intervention.
///
/// # Arguments
///
/// * `output` - Command output text to analyze
///
/// # Returns
///
/// * `true` if output contains permission denial indicators
/// * `false` otherwise
///
/// # Examples
///
/// ```
/// use prodigy::worktree::manager_validation::is_permission_denied;
///
/// assert!(is_permission_denied("Error: permission denied"));
/// assert!(is_permission_denied("Please grant permission to proceed"));
/// assert!(!is_permission_denied("Merge completed successfully"));
/// ```
/// Check if a branch has been merged into a target branch
///
/// Determines if the specified branch appears in the list of branches
/// that have been merged into the target.
///
/// # Arguments
///
/// * `branch` - Branch name to check
/// * `merged_branches_output` - Output from `git branch --merged target`
///
/// # Returns
///
/// * `true` if branch is in the merged list
/// * `false` otherwise
///
/// # Examples
///
/// ```
/// use prodigy::worktree::manager_validation::check_if_branch_merged;
///
/// let output = " feature-123\n bugfix-456\n* main\n";
/// assert!(check_if_branch_merged("feature-123", output));
/// assert!(!check_if_branch_merged("feature-999", output));
/// ```