1 # git-gui index (add/remove) support
2 # Copyright (C) 2006, 2007 Shawn Pearce
4 proc _delete_indexlock
{} {
5 if {[catch {file delete
-- [gitdir index.lock
]} err
]} {
6 error_popup
[strcat
[mc
"Unable to unlock the index."] "\n\n$err"]
10 proc _close_updateindex
{fd
after} {
11 fconfigure $fd -blocking 1
12 if {[catch {close $fd} err
]} {
15 wm title
$w [strcat
"[appname] ([reponame]): " [mc
"Index Error"]]
16 wm geometry
$w "+[winfo rootx .]+[winfo rooty .]"
21 [mc
"Updating the Git index failed. A rescan will be automatically started to resynchronize git-gui."] \
26 button $w.buttons.
continue \
27 -text [mc
"Continue"] \
28 -command [list destroy $w]
29 pack $w.buttons.
continue -side right
-padx 5
30 button $w.buttons.unlock
\
31 -text [mc
"Unlock Index"] \
32 -command "destroy $w; _delete_indexlock"
33 pack $w.buttons.unlock
-side right
34 pack $w.buttons
-side bottom
-fill x
-pady 10 -padx 10
36 wm protocol
$w WM_DELETE_WINDOW
update
37 bind $w.buttons.
continue <Visibility
> "
39 focus $w.buttons.continue
54 proc update_indexinfo
{msg pathList
after} {
55 global update_index_cp
57 if {![lock_index
update]} return
60 set pathList
[lsort $pathList]
61 set totalCnt
[llength $pathList]
62 set batch
[expr {int
($totalCnt * .01) + 1}]
63 if {$batch > 25} {set batch
25}
65 $::main_status start
$msg [mc
"files"]
66 set fd
[git_write update-index
-z --index-info
]
73 fileevent $fd writable
[list \
74 write_update_indexinfo
\
83 proc write_update_indexinfo
{fd pathList totalCnt batch
after} {
84 global update_index_cp
85 global file_states current_diff_path
87 if {$update_index_cp >= $totalCnt} {
88 _close_updateindex
$fd $after
93 {$update_index_cp < $totalCnt && $i > 0} \
95 set path
[lindex $pathList $update_index_cp]
98 set s
$file_states($path)
99 switch -glob -- [lindex $s 0] {
107 set info [lindex $s 2]
108 if {$info eq
{}} continue
110 puts -nonewline $fd "$info\t[encoding convertto $path]\0"
111 display_file
$path $new
114 $::main_status update $update_index_cp $totalCnt
117 proc update_index
{msg pathList
after} {
118 global update_index_cp
120 if {![lock_index
update]} return
122 set update_index_cp
0
123 set pathList
[lsort $pathList]
124 set totalCnt
[llength $pathList]
125 set batch
[expr {int
($totalCnt * .01) + 1}]
126 if {$batch > 25} {set batch
25}
128 $::main_status start
$msg [mc
"files"]
129 set fd
[git_write update-index
--add --remove -z --stdin]
136 fileevent $fd writable
[list \
146 proc write_update_index
{fd pathList totalCnt batch
after} {
147 global update_index_cp
148 global file_states current_diff_path
150 if {$update_index_cp >= $totalCnt} {
151 _close_updateindex
$fd $after
156 {$update_index_cp < $totalCnt && $i > 0} \
158 set path
[lindex $pathList $update_index_cp]
161 switch -glob -- [lindex $file_states($path) 0] {
169 if {[file exists
$path]} {
178 puts -nonewline $fd "[encoding convertto $path]\0"
179 display_file
$path $new
182 $::main_status update $update_index_cp $totalCnt
185 proc checkout_index
{msg pathList
after} {
186 global update_index_cp
188 if {![lock_index
update]} return
190 set update_index_cp
0
191 set pathList
[lsort $pathList]
192 set totalCnt
[llength $pathList]
193 set batch
[expr {int
($totalCnt * .01) + 1}]
194 if {$batch > 25} {set batch
25}
196 $::main_status start
$msg [mc
"files"]
197 set fd
[git_write checkout-index
\
210 fileevent $fd writable
[list \
211 write_checkout_index
\
220 proc write_checkout_index
{fd pathList totalCnt batch
after} {
221 global update_index_cp
222 global file_states current_diff_path
224 if {$update_index_cp >= $totalCnt} {
225 _close_updateindex
$fd $after
230 {$update_index_cp < $totalCnt && $i > 0} \
232 set path
[lindex $pathList $update_index_cp]
234 switch -glob -- [lindex $file_states($path) 0] {
239 puts -nonewline $fd "[encoding convertto $path]\0"
240 display_file
$path ?_
245 $::main_status update $update_index_cp $totalCnt
248 proc unstage_helper
{txt paths
} {
249 global file_states current_diff_path
251 if {![lock_index begin-update
]} return
255 foreach path
$paths {
256 switch -glob -- [lindex $file_states($path) 0] {
261 lappend pathList
$path
262 if {$path eq
$current_diff_path} {
263 set after {reshow_diff
;}
268 if {$pathList eq
{}} {
274 [concat $after [list ui_ready
]]
278 proc do_unstage_selection
{} {
279 global current_diff_path selected_paths
281 if {[array size selected_paths
] > 0} {
283 {Unstaging selected files from commit
} \
284 [array names selected_paths
]
285 } elseif
{$current_diff_path ne
{}} {
287 [mc
"Unstaging %s from commit" [short_path
$current_diff_path]] \
288 [list $current_diff_path]
292 proc add_helper
{txt paths
} {
293 global file_states current_diff_path
295 if {![lock_index begin-update
]} return
299 foreach path
$paths {
300 switch -glob -- [lindex $file_states($path) 0] {
306 lappend pathList
$path
307 if {$path eq
$current_diff_path} {
308 set after {reshow_diff
;}
313 if {$pathList eq
{}} {
319 [concat $after {ui_status
[mc
"Ready to commit."]}]
323 proc do_add_selection
{} {
324 global current_diff_path selected_paths
326 if {[array size selected_paths
] > 0} {
328 {Adding selected files
} \
329 [array names selected_paths
]
330 } elseif
{$current_diff_path ne
{}} {
332 [mc
"Adding %s" [short_path
$current_diff_path]] \
333 [list $current_diff_path]
341 foreach path
[array names file_states
] {
342 switch -glob -- [lindex $file_states($path) 0] {
346 ?D
{lappend paths
$path}
349 add_helper
{Adding all changed files
} $paths
352 proc revert_helper
{txt paths
} {
353 global file_states current_diff_path
355 if {![lock_index begin-update
]} return
359 foreach path
$paths {
360 switch -glob -- [lindex $file_states($path) 0] {
365 lappend pathList
$path
366 if {$path eq
$current_diff_path} {
367 set after {reshow_diff
;}
374 # Split question between singular and plural cases, because
375 # such distinction is needed in some languages. Previously, the
376 # code used "Revert changes in" for both, but that can't work
377 # in languages where 'in' must be combined with word from
378 # rest of string (in diffrent way for both cases of course).
380 # FIXME: Unfortunately, even that isn't enough in some languages
381 # as they have quite complex plural-form rules. Unfortunately,
382 # msgcat doesn't seem to support that kind of string translation.
384 set n
[llength $pathList]
389 set query
[mc
"Revert changes in file %s?" [short_path
[lindex $pathList]]]
391 set query
[mc
"Revert changes in these %i files?" $n]
394 set reply
[tk_dialog \
396 "[appname] ([reponame])" \
399 [mc "Any unstaged changes will be permanently lost by the revert.
"]" \
403 [mc
"Revert Changes"] \
409 [concat $after [list ui_ready
]]
415 proc do_revert_selection
{} {
416 global current_diff_path selected_paths
418 if {[array size selected_paths
] > 0} {
420 [mc
"Reverting selected files"] \
421 [array names selected_paths
]
422 } elseif
{$current_diff_path ne
{}} {
424 [mc
"Reverting %s" [short_path
$current_diff_path]] \
425 [list $current_diff_path]
429 proc do_select_commit_type
{} {
430 global commit_type selected_commit_type
432 if {$selected_commit_type eq
{new
}
433 && [string match amend
* $commit_type]} {
435 } elseif
{$selected_commit_type eq
{amend
}
436 && ![string match amend
* $commit_type]} {
439 # The amend request was rejected...
441 if {![string match amend
* $commit_type]} {
442 set selected_commit_type new