Skip to content

Commit 0ea8fd5

Browse files
committed
Optimize file staging by auto-selecting the single unstaged file when only one exists
1 parent f162704 commit 0ea8fd5

1 file changed

Lines changed: 57 additions & 50 deletions

File tree

‎apps/editor/src/utils/git-repository-utils.ts‎

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -97,66 +97,73 @@ export const prepare_staged_changes = async (
9797
staged_changes.length == 0 &&
9898
repository.state.workingTreeChanges.length > 0
9999
) {
100-
const items = repository.state.workingTreeChanges.map((change: any) => {
101-
const relative_path = path.relative(
102-
repository.rootUri.fsPath,
103-
change.uri.fsPath
104-
)
105-
const dir_name = path.dirname(relative_path)
106-
107-
return {
108-
label: path.basename(relative_path),
109-
description: dir_name == '.' ? '' : dir_name,
110-
picked: true,
111-
fsPath: change.uri.fsPath
112-
}
113-
})
114-
115-
items.sort((a, b) => {
116-
const label_diff = natural_sort(a.label, b.label)
117-
if (label_diff != 0) return label_diff
118-
return natural_sort(a.description || '', b.description || '')
119-
})
120-
121-
const selected = await new Promise<any[] | undefined>((resolve) => {
122-
const quick_pick = vscode.window.createQuickPick<any>()
123-
quick_pick.items = items
124-
quick_pick.selectedItems = items
125-
quick_pick.canSelectMany = true
126-
quick_pick.title = 'Unstaged Files'
127-
quick_pick.placeholder = 'Select files to commit'
128-
quick_pick.buttons = [
129-
{
130-
iconPath: new vscode.ThemeIcon('close'),
131-
tooltip: 'Close'
132-
}
133-
]
134-
135-
quick_pick.onDidTriggerButton((button) => {
136-
if (button.tooltip == 'Close') {
137-
resolve(undefined)
138-
quick_pick.hide()
100+
let files_to_stage: string[] = []
101+
102+
if (repository.state.workingTreeChanges.length == 1) {
103+
files_to_stage = [repository.state.workingTreeChanges[0].uri.fsPath]
104+
} else {
105+
const items = repository.state.workingTreeChanges.map((change: any) => {
106+
const relative_path = path.relative(
107+
repository.rootUri.fsPath,
108+
change.uri.fsPath
109+
)
110+
const dir_name = path.dirname(relative_path)
111+
112+
return {
113+
label: path.basename(relative_path),
114+
description: dir_name == '.' ? '' : dir_name,
115+
picked: true,
116+
fsPath: change.uri.fsPath
139117
}
140118
})
141119

142-
quick_pick.onDidAccept(() => {
143-
resolve(Array.from(quick_pick.selectedItems))
144-
quick_pick.hide()
120+
items.sort((a, b) => {
121+
const label_diff = natural_sort(a.label, b.label)
122+
if (label_diff != 0) return label_diff
123+
return natural_sort(a.description || '', b.description || '')
145124
})
146125

147-
quick_pick.onDidHide(() => {
148-
resolve(undefined)
149-
quick_pick.dispose()
126+
const selected = await new Promise<any[] | undefined>((resolve) => {
127+
const quick_pick = vscode.window.createQuickPick<any>()
128+
quick_pick.items = items
129+
quick_pick.selectedItems = items
130+
quick_pick.canSelectMany = true
131+
quick_pick.title = 'Unstaged Files'
132+
quick_pick.placeholder = 'Select files to commit'
133+
quick_pick.buttons = [
134+
{
135+
iconPath: new vscode.ThemeIcon('close'),
136+
tooltip: 'Close'
137+
}
138+
]
139+
140+
quick_pick.onDidTriggerButton((button) => {
141+
if (button.tooltip == 'Close') {
142+
resolve(undefined)
143+
quick_pick.hide()
144+
}
145+
})
146+
147+
quick_pick.onDidAccept(() => {
148+
resolve(Array.from(quick_pick.selectedItems))
149+
quick_pick.hide()
150+
})
151+
152+
quick_pick.onDidHide(() => {
153+
resolve(undefined)
154+
quick_pick.dispose()
155+
})
156+
157+
quick_pick.show()
150158
})
151159

152-
quick_pick.show()
153-
})
160+
if (!selected || selected.length == 0) {
161+
return null
162+
}
154163

155-
if (!selected || selected.length == 0) {
156-
return null
164+
files_to_stage = selected.map((item) => item.fsPath)
157165
}
158166

159-
const files_to_stage = selected.map((item) => item.fsPath)
160167
const file_args = files_to_stage
161168
.map((file: string) => `"${file.replace(/"/g, '\\"')}"`)
162169
.join(' ')

0 commit comments

Comments
 (0)