git-gui / lib / database.tclon commit Merge git://git.kernel.org/pub/scm/gitk/gitk (196821f)
   1# git-gui object database management support
   2# Copyright (C) 2006, 2007 Shawn Pearce
   3
   4proc do_stats {} {
   5        set fd [git_read count-objects -v]
   6        while {[gets $fd line] > 0} {
   7                if {[regexp {^([^:]+): (\d+)$} $line _ name value]} {
   8                        set stats($name) $value
   9                }
  10        }
  11        close $fd
  12
  13        set packed_sz 0
  14        foreach p [glob -directory [gitdir objects pack] \
  15                -type f \
  16                -nocomplain -- *] {
  17                incr packed_sz [file size $p]
  18        }
  19        if {$packed_sz > 0} {
  20                set stats(size-pack) [expr {$packed_sz / 1024}]
  21        }
  22
  23        set w .stats_view
  24        toplevel $w
  25        wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
  26
  27        label $w.header -text [mc "Database Statistics"]
  28        pack $w.header -side top -fill x
  29
  30        frame $w.buttons -border 1
  31        button $w.buttons.close -text [mc Close] \
  32                -default active \
  33                -command [list destroy $w]
  34        button $w.buttons.gc -text [mc "Compress Database"] \
  35                -default normal \
  36                -command "destroy $w;do_gc"
  37        pack $w.buttons.close -side right
  38        pack $w.buttons.gc -side left
  39        pack $w.buttons -side bottom -fill x -pady 10 -padx 10
  40
  41        frame $w.stat -borderwidth 1 -relief solid
  42        foreach s {
  43                {count           {mc "Number of loose objects"}}
  44                {size            {mc "Disk space used by loose objects"} { KiB}}
  45                {in-pack         {mc "Number of packed objects"}}
  46                {packs           {mc "Number of packs"}}
  47                {size-pack       {mc "Disk space used by packed objects"} { KiB}}
  48                {prune-packable  {mc "Packed objects waiting for pruning"}}
  49                {garbage         {mc "Garbage files"}}
  50                } {
  51                set name [lindex $s 0]
  52                set label [eval [lindex $s 1]]
  53                if {[catch {set value $stats($name)}]} continue
  54                if {[llength $s] > 2} {
  55                        set value "$value[lindex $s 2]"
  56                }
  57
  58                label $w.stat.l_$name -text "$label:" -anchor w
  59                label $w.stat.v_$name -text $value -anchor w
  60                grid $w.stat.l_$name $w.stat.v_$name -sticky we -padx {0 5}
  61        }
  62        pack $w.stat -pady 10 -padx 10
  63
  64        bind $w <Visibility> "grab $w; focus $w.buttons.close"
  65        bind $w <Key-Escape> [list destroy $w]
  66        bind $w <Key-Return> [list destroy $w]
  67        wm title $w [append "[appname] ([reponame]): " [mc "Database Statistics"]]
  68        tkwait window $w
  69}
  70
  71proc do_gc {} {
  72        set w [console::new {gc} [mc "Compressing the object database"]]
  73        console::chain $w {
  74                {exec git pack-refs --prune}
  75                {exec git reflog expire --all}
  76                {exec git repack -a -d -l}
  77                {exec git rerere gc}
  78        }
  79}
  80
  81proc do_fsck_objects {} {
  82        set w [console::new {fsck-objects} \
  83                [mc "Verifying the object database with fsck-objects"]]
  84        set cmd [list git fsck-objects]
  85        lappend cmd --full
  86        lappend cmd --cache
  87        lappend cmd --strict
  88        console::exec $w $cmd
  89}
  90
  91proc hint_gc {} {
  92        set object_limit 8
  93        if {[is_Windows]} {
  94                set object_limit 1
  95        }
  96
  97        set objects_current [llength [glob \
  98                -directory [gitdir objects 42] \
  99                -nocomplain \
 100                -tails \
 101                -- \
 102                *]]
 103
 104        if {$objects_current >= $object_limit} {
 105                set objects_current [expr {$objects_current * 250}]
 106                set object_limit    [expr {$object_limit    * 250}]
 107                if {[ask_popup \
 108                        [mc "This repository currently has approximately %i loose objects.
 109
 110To maintain optimal performance it is strongly recommended that you compress the database when more than %i loose objects exist.
 111
 112Compress the database now?" $objects_current $object_limit]] eq yes} {
 113                        do_gc
 114                }
 115        }
 116}