Skip to content

Commit 847576f

Browse files
authored
Merge pull request #51 from devchat-ai/hotfix/git-staged-deletion-flag-#197
Fix staged file deletion flag display in commit workflow
2 parents be63f25 + d0fc2a4 commit 847576f

1 file changed

Lines changed: 32 additions & 22 deletions

File tree

commit/commit.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ def strip_file_name(file_name):
149149
# check wether filename is a directory
150150
if os.path.isdir(filename):
151151
continue
152-
modified_files.append(os.path.normpath(strip_file_name(filename)))
152+
modified_files.append((os.path.normpath(strip_file_name(filename)), status[1:2]))
153153
if status[0:1] == "M" or status[0:1] == "A" or status[0:1] == "D":
154-
staged_files.append(os.path.normpath(strip_file_name(filename)))
154+
staged_files.append((os.path.normpath(strip_file_name(filename)), status[0:1]))
155155
return modified_files, staged_files
156156

157157

@@ -167,10 +167,14 @@ def get_marked_files(modified_files, staged_files):
167167
List[str]: 用户选中的文件列表
168168
"""
169169
# Create two Checkbox instances for staged and unstaged files
170-
staged_checkbox = Checkbox(staged_files, [True] * len(staged_files))
170+
staged_files_show = [f'{file[1] if file[1]!="?" else "U"} {file[0]}' for file in staged_files]
171+
staged_checkbox = Checkbox(staged_files_show, [True] * len(staged_files_show))
171172

172-
unstaged_files = [file for file in modified_files if file not in staged_files]
173-
unstaged_checkbox = Checkbox(unstaged_files, [False] * len(unstaged_files))
173+
unstaged_files = [file for file in modified_files if file[1].strip() != ""]
174+
unstaged_files_show = [
175+
f'{file[1] if file[1]!="?" else "U"} {file[0]}' for file in unstaged_files
176+
]
177+
unstaged_checkbox = Checkbox(unstaged_files_show, [False] * len(unstaged_files_show))
174178

175179
# Create a Form with both Checkbox instances
176180
form_list = []
@@ -190,31 +194,37 @@ def get_marked_files(modified_files, staged_files):
190194
# Retrieve the selected files from both Checkbox instances
191195
staged_checkbox_selections = staged_checkbox.selections if staged_checkbox.selections else []
192196
unstaged_selections = unstaged_checkbox.selections if unstaged_checkbox.selections else []
193-
selected_staged_files = [staged_files[idx] for idx in staged_checkbox_selections]
194-
selected_unstaged_files = [unstaged_files[idx] for idx in unstaged_selections]
197+
selected_staged_files = [staged_files[idx][0] for idx in staged_checkbox_selections]
198+
selected_unstaged_files = [unstaged_files[idx][0] for idx in unstaged_selections]
195199

196-
# Combine the selections from both checkboxes
197-
selected_files = selected_staged_files + selected_unstaged_files
200+
return selected_staged_files, selected_unstaged_files
198201

199-
return selected_files
200202

201-
202-
def rebuild_stage_list(user_files):
203+
def rebuild_stage_list(staged_select_files, unstaged_select_files):
203204
"""
204205
根据用户选中文件,重新构建stage列表
205206
206207
Args:
207-
user_files: 用户选中的文件列表
208+
staged_select_files: 当前选中的已staged文件列表
209+
unstaged_select_files: 当前选中的未staged文件列表
208210
209211
Returns:
210212
None
211-
212213
"""
213-
# Unstage all files
214-
subprocess.check_output(["git", "reset"])
215-
# Stage all user_files
216-
for file in user_files:
217-
os.system(f'git add "{file}"')
214+
# 获取当前所有staged文件
215+
current_staged_files = subprocess.check_output(
216+
["git", "diff", "--name-only", "--cached"], text=True
217+
).splitlines()
218+
219+
# 添加unstaged_select_files中的文件到staged
220+
for file in unstaged_select_files:
221+
subprocess.check_output(["git", "add", file])
222+
223+
# 将不在staged_select_files中的文件从staged移除
224+
user_selected_files = staged_select_files + unstaged_select_files
225+
files_to_unstage = [file for file in current_staged_files if file not in user_selected_files]
226+
for file in files_to_unstage:
227+
subprocess.check_output(["git", "reset", file])
218228

219229

220230
def get_diff():
@@ -354,12 +364,12 @@ def main():
354364
print("There are no files to commit.", flush=True)
355365
sys.exit(0)
356366

357-
selected_files = get_marked_files(modified_files, staged_files)
358-
if not selected_files:
367+
staged_select_files, unstaged_select_files = get_marked_files(modified_files, staged_files)
368+
if not staged_select_files and not unstaged_select_files:
359369
print("No files selected, the commit has been aborted.")
360370
return
361371

362-
rebuild_stage_list(selected_files)
372+
rebuild_stage_list(staged_select_files, unstaged_select_files)
363373

364374
print(
365375
"Step 2/2: Review the commit message I've drafted for you. "

0 commit comments

Comments
 (0)