117
set _gitexec {}
118
set _reponame {}
119
set _iscygwin {}
120
+set _search_path {}
121
122
proc appname {} {
123
global _appname
129
if {$args eq {}} {
130
return $_gitdir
131
}
131
- return [eval [concat [list file join $_gitdir] $args]]
132
+ return [eval [list file join $_gitdir] $args]
133
}
134
135
proc gitexec {args} {
138
if {[catch {set _gitexec [git --exec-path]} err]} {
139
error "Git not installed?\n\n$err"
140
}
141
+ if {[is_Cygwin]} {
142
+ set _gitexec [exec cygpath \
143
+ --windows \
144
+ --absolute \
145
+ $_gitexec]
146
+ } else {
147
+ set _gitexec [file normalize $_gitexec]
148
+ }
149
}
150
if {$args eq {}} {
151
return $_gitexec
152
}
144
- return [eval [concat [list file join $_gitexec] $args]]
153
+ return [eval [list file join $_gitexec] $args]
154
}
155
156
proc reponame {} {
246
array unset global_config
247
if {$include_global} {
248
catch {
240
- set fd_rc [open "| git config --global --list" r]
249
+ set fd_rc [git_read config --global --list]
250
while {[gets $fd_rc line] >= 0} {
251
if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
252
if {[is_many_config $name]} {
262
263
array unset repo_config
264
catch {
256
- set fd_rc [open "| git config --list" r]
265
+ set fd_rc [git_read config --list]
266
while {[gets $fd_rc line] >= 0} {
267
if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
268
if {[is_many_config $name]} {
289
##
290
## handy utils
291
292
+proc _git_cmd {name} {
293
+ global _git_cmd_path
294
+
295
+ if {[catch {set v $_git_cmd_path($name)}]} {
296
+ switch -- $name {
297
+ --version -
298
+ --exec-path { return [list $::_git $name] }
299
+ }
300
+
301
+ set p [gitexec git-$name$::_search_exe]
302
+ if {[file exists $p]} {
303
+ set v [list $p]
304
+ } elseif {[is_Cygwin]} {
305
+ # On Cygwin git is a proper Cygwin program and knows
306
+ # how to properly restart the Cygwin environment and
307
+ # spawn its non-.exe support program.
308
+ #
309
+ set v [list $::_git $name]
310
+ } elseif {[is_Windows]
311
+ && $::_sh ne {}
312
+ && [file exists [gitexec git-$name]]} {
313
+ # Assume this is a UNIX shell script. We can
314
+ # probably execute it through a Bourne shell.
315
+ #
316
+ set v [list $::_sh [gitexec git-$name]]
317
+ } else {
318
+ error "No [gitexec git-$name]"
319
+ }
320
+ set _git_cmd_path($name) $v
321
+ }
322
+ return $v
323
+}
324
+
325
+proc _which {what} {
326
+ global env _search_exe _search_path
327
+
328
+ if {$_search_path eq {}} {
329
+ if {[is_Cygwin]} {
330
+ set _search_path [split [exec cygpath \
331
+ --windows \
332
+ --path \
333
+ --absolute \
334
+ $env(PATH)] {;}]
335
+ set _search_exe .exe
336
+ } elseif {[is_Windows]} {
337
+ set _search_path [split $env(PATH) {;}]
338
+ set _search_exe .exe
339
+ } else {
340
+ set _search_path [split $env(PATH) :]
341
+ set _search_exe {}
342
+ }
343
+ }
344
+
345
+ foreach p $_search_path {
346
+ set p [file join $p $what$_search_exe]
347
+ if {[file exists $p]} {
348
+ return [file normalize $p]
349
+ }
350
+ }
351
+ return {}
352
+}
353
+
354
proc git {args} {
284
- return [eval exec git $args]
355
+ set opt [list exec]
356
+
357
+ while {1} {
358
+ switch -- [lindex $args 0] {
359
+ --nice {
360
+ global _nice
361
+ if {$_nice ne {}} {
362
+ lappend opt $_nice
363
+ }
364
+ }
365
+
366
+ default {
367
+ break
368
+ }
369
+
370
+ }
371
+
372
+ set args [lrange $args 1 end]
373
+ }
374
+
375
+ set cmdp [_git_cmd [lindex $args 0]]
376
+ set args [lrange $args 1 end]
377
+
378
+ return [eval $opt $cmdp $args]
379
+}
380
+
381
+proc git_read {args} {
382
+ set opt [list |]
383
+
384
+ while {1} {
385
+ switch -- [lindex $args 0] {
386
+ --nice {
387
+ global _nice
388
+ if {$_nice ne {}} {
389
+ lappend opt $_nice
390
+ }
391
+ }
392
+
393
+ --stderr {
394
+ lappend args 2>@1
395
+ }
396
+
397
+ default {
398
+ break
399
+ }
400
+
401
+ }
402
+
403
+ set args [lrange $args 1 end]
404
+ }
405
+
406
+ set cmdp [_git_cmd [lindex $args 0]]
407
+ set args [lrange $args 1 end]
408
+
409
+ if {[catch {
410
+ set fd [open [concat $opt $cmdp $args] r]
411
+ } err]} {
412
+ if { [lindex $args end] eq {2>@1}
413
+ && $err eq {can not find channel named "1"}
414
+ } {
415
+ # Older versions of Tcl 8.4 don't have this 2>@1 IO
416
+ # redirect operator. Fallback to |& cat for those.
417
+ # The command was not actually started, so its safe
418
+ # to try to start it a second time.
419
+ #
420
+ set fd [open [concat \
421
+ $opt \
422
+ $cmdp \
423
+ [lrange $args 0 end-1] \
424
+ [list |& cat] \
425
+ ] r]
426
+ } else {
427
+ error $err
428
+ }
429
+ }
430
+ return $fd
431
+}
432
+
433
+proc git_write {args} {
434
+ set opt [list |]
435
+
436
+ while {1} {
437
+ switch -- [lindex $args 0] {
438
+ --nice {
439
+ global _nice
440
+ if {$_nice ne {}} {
441
+ lappend opt $_nice
442
+ }
443
+ }
444
+
445
+ default {
446
+ break
447
+ }
448
+
449
+ }
450
+
451
+ set args [lrange $args 1 end]
452
+ }
453
+
454
+ set cmdp [_git_cmd [lindex $args 0]]
455
+ set args [lrange $args 1 end]
456
+
457
+ return [open [concat $opt $cmdp $args] w]
458
}
459
460
proc load_current_branch {} {
491
return $m
492
}
493
494
+######################################################################
495
+##
496
+## find git
497
+
498
+set _git [_which git]
499
+if {$_git eq {}} {
500
+ catch {wm withdraw .}
501
+ error_popup "Cannot find git in PATH."
502
+ exit 1
503
+}
504
+set _nice [_which nice]
505
+set _sh [_which sh]
506
+
507
######################################################################
508
##
509
## version check
752
} else {
753
set rescan_active 1
754
ui_status {Refreshing file status...}
569
- set cmd [list git update-index]
570
- lappend cmd -q
571
- lappend cmd --unmerged
572
- lappend cmd --ignore-missing
573
- lappend cmd --refresh
574
- set fd_rf [open "| $cmd" r]
755
+ set fd_rf [git_read update-index \
756
+ -q \
757
+ --unmerged \
758
+ --ignore-missing \
759
+ --refresh \
760
+ ]
761
fconfigure $fd_rf -blocking 0 -translation binary
762
fileevent $fd_rf readable \
763
[list rescan_stage2 $fd_rf $after]
773
close $fd
774
}
775
590
- set ls_others [list | git ls-files --others -z \
591
- --exclude-per-directory=.gitignore]
776
+ set ls_others [list --exclude-per-directory=.gitignore]
777
set info_exclude [gitdir info exclude]
778
if {[file readable $info_exclude]} {
779
lappend ls_others "--exclude-from=$info_exclude"
785
786
set rescan_active 3
787
ui_status {Scanning for modified files ...}
603
- set fd_di [open "| git diff-index --cached -z [PARENT]" r]
604
- set fd_df [open "| git diff-files -z" r]
605
- set fd_lo [open $ls_others r]
788
+ set fd_di [git_read diff-index --cached -z [PARENT]]
789
+ set fd_df [git_read diff-files -z]
790
+ set fd_lo [eval git_read ls-files --others -z $ls_others]
791
792
fconfigure $fd_di -blocking 0 -translation binary -encoding binary
793
fconfigure $fd_df -blocking 0 -translation binary -encoding binary