| 1 | package App::Codit::Plugins::Git; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | App::Codit::Plugins::FileBrowser - plugin for App::Codit | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use vars qw( $VERSION ); | ||
| 12 | $VERSION = '0.21'; | ||
| 13 | |||
| 14 | use base qw( Tk::AppWindow::BaseClasses::Plugin ); | ||
| 15 | |||
| 16 | require Tk::Adjuster; | ||
| 17 | require Tk::DocumentTree; | ||
| 18 | require Tk::Menu; | ||
| 19 | use File::Basename; | ||
| 20 | |||
| 21 | use Config; | ||
| 22 | my $mswin = $Config{'osname'} eq 'MSWin32'; | ||
| 23 | |||
| 24 | my @contextmenu = ( | ||
| 25 | [ 'menu_normal', undef, '~Open all files', 'git_open_all', 'document-open'], | ||
| 26 | [ 'menu_normal', undef, '~Remove from project', 'git_remove_dialog', 'edit-delete'], | ||
| 27 | [ 'menu_separator', undef, 'c1'], | ||
| 28 | [ 'menu_normal', undef, '~Collapse all', 'git_collapse'], | ||
| 29 | [ 'menu_normal', undef, '~Expand all', 'git_expand'], | ||
| 30 | ); | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | Integrate Git into Codit. | ||
| 35 | |||
| 36 | =head1 DETAILS | ||
| 37 | |||
| 38 | This plugin will only load if the git executable is installed. | ||
| 39 | |||
| 40 | It adds a document list to the navigation panel with the git icon. | ||
| 41 | |||
| 42 | When a file is opened it will check if it belongs to a git repository. If it does | ||
| 43 | it will add the repository to the top menu list. If you select a repository | ||
| 44 | from that list all documents in that repository are loaded in the file list. | ||
| 45 | Selecting a document in the list will open it if it is not yet opened and select it. | ||
| 46 | |||
| 47 | When a file is closed it will check if there are any remaining documents in it's repository | ||
| 48 | are opened. If none are open it will unselect the repository and remove it from the | ||
| 49 | top menu list. | ||
| 50 | |||
| 51 | It has a context menu that pops with the right mouse button. You can quickly open all files in | ||
| 52 | the repository, or remove the current selected file from the repository. And you can collapse | ||
| 53 | and expand the list. | ||
| 54 | |||
| 55 | =head1 COMMANDS | ||
| 56 | |||
| 57 | Thi Git plugin adds the following commands to Codit. | ||
| 58 | |||
| 59 | =over 4 | ||
| 60 | |||
| 61 | =item B<git_collapse> | ||
| 62 | |||
| 63 | Collapses the git document tree and only opens the current selected document, if it is in | ||
| 64 | the current selected repository. | ||
| 65 | |||
| 66 | =item B<git_command> I<$project>, I<$commandstring> | ||
| 67 | |||
| 68 | Executes the git command in $commandstring for repository $project. | ||
| 69 | |||
| 70 | =item B<git_expand> | ||
| 71 | |||
| 72 | Epxands the git document tree. | ||
| 73 | |||
| 74 | =item B<git_open_all> | ||
| 75 | |||
| 76 | Opens all files in the current selected repository. | ||
| 77 | |||
| 78 | =item B<git_remove> I<?$name?> | ||
| 79 | |||
| 80 | If $name is not specified, $name is the selected document. | ||
| 81 | |||
| 82 | Closes $name, Removes it from the current selected repository | ||
| 83 | and deletes the file from disk. Use with care. | ||
| 84 | |||
| 85 | =item B<git_remove_dialog> | ||
| 86 | |||
| 87 | Same as git_remove but first asks nicely if you really want to do this. | ||
| 88 | |||
| 89 | =back | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub new { | ||
| 94 | my $class = shift; | ||
| 95 | my $self = $class->SUPER::new(@_); | ||
| 96 | return undef unless defined $self; | ||
| 97 | |||
| 98 | #load only if git command line is installed | ||
| 99 | my $git = `git -v`; | ||
| 100 | return undef unless $git =~/^git\sversion/; | ||
| 101 | |||
| 102 | $self->cmdHookAfter('doc_open', 'openDocAfter', $self); | ||
| 103 | $self->cmdHookAfter('doc_close', 'closeDocAfter', $self); | ||
| 104 | $self->cmdHookBefore('doc_select', 'selectDocBefore', $self); | ||
| 105 | $self->cmdConfig( | ||
| 106 | git_add => ['gitAdd', $self], | ||
| 107 | git_collapse => ['gitCollapse', $self], | ||
| 108 | git_command => ['gitCommand', $self], | ||
| 109 | git_expand => ['gitExpand', $self], | ||
| 110 | git_open_all => ['gitOpenAll', $self], | ||
| 111 | git_remove => ['gitRemove', $self], | ||
| 112 | git_remove_dialog => ['gitRemoveDialog', $self], | ||
| 113 | ); | ||
| 114 | |||
| 115 | $self->{PROJECTS} = {}; | ||
| 116 | |||
| 117 | my $page = $self->ToolLeftPageAdd('Git', 'git-icon', undef, 'Manage your projects', 250); | ||
| 118 | |||
| 119 | my $pframe = $page->Frame->pack(-fill => 'x'); | ||
| 120 | $pframe->Label(-text => 'Project:')->pack(-side => 'left'); | ||
| 121 | |||
| 122 | my $current = ''; | ||
| 123 | $self->{CURRENT} = \$current; | ||
| 124 | my $mb = $pframe->Menubutton( | ||
| 125 | -anchor => 'w', | ||
| 126 | -textvariable => \$current, | ||
| 127 | )->pack(-side => 'left', -expand => 1, -fill => 'x'); | ||
| 128 | |||
| 129 | my $menu = $mb->Menu(-tearoff => 0); | ||
| 130 | $mb->configure(-menu => $menu); | ||
| 131 | $self->{PMENU} = $menu; | ||
| 132 | |||
| 133 | my $nav = $self->extGet('Selector'); | ||
| 134 | my $gtree = $page->DocumentTree( | ||
| 135 | -entryselect => ['selectInternal', $self], | ||
| 136 | -diriconcall => ['GetDirIcon', $nav], | ||
| 137 | -fileiconcall => ['GetFileIcon', $nav], | ||
| 138 | -saveiconcall => ['GetSaveIcon', $nav], | ||
| 139 | )->pack(-expand => 1, -fill => 'both'); | ||
| 140 | my $stack =$self->extGet('MenuBar')->menuStack(@contextmenu); | ||
| 141 | $gtree->configure('-contextmenu', $stack); | ||
| 142 | $self->{TREE} = $gtree; | ||
| 143 | |||
| 144 | $self->after(10, ['doPostConfig', $self]); | ||
| 145 | return $self; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub closeDocAfter { | ||
| 149 | my ($self, $file) = @_; | ||
| 150 | if ($file ne 0) { | ||
| 151 | #does the file belong to a project? | ||
| 152 | my $project = $self->fileInAnyProject($file); | ||
| 153 | if (defined $project) { | ||
| 154 | #is any other file of this project open? | ||
| 155 | $self->projectRemove($project) unless $self->filesOpenInProject($project); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | sub doPostConfig { | ||
| 161 | my $self = shift; | ||
| 162 | my $mdi = $self->mdi; | ||
| 163 | my @list = $mdi->docFullList; | ||
| 164 | for (@list) { | ||
| 165 | my $doc = $_; | ||
| 166 | my $folder = $self->projectFind($doc); | ||
| 167 | if (defined $folder) { | ||
| 168 | my $name = $self->projectName($folder); | ||
| 169 | $self->projectAdd($name, $folder); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | $self->after(1000, ['navContextMenu', $mdi]); | ||
| 173 | } | ||
| 174 | |||
| 175 | sub fileInAnyProject { | ||
| 176 | my ($self, $file) = @_; | ||
| 177 | my @list = $self->projectList; | ||
| 178 | for (@list) { | ||
| 179 | my $project = $_; | ||
| 180 | return $project if $self->fileInProject($project, $file) | ||
| 181 | } | ||
| 182 | return undef | ||
| 183 | } | ||
| 184 | |||
| 185 | sub fileInProject { | ||
| 186 | my ($self, $project, $file) = @_; | ||
| 187 | my @files = $self->gitFileList($project); | ||
| 188 | my $term = quotemeta($file); | ||
| 189 | my @containing = grep { /^$term$/ } @files; | ||
| 190 | return 1 if @containing; | ||
| 191 | return '' | ||
| 192 | } | ||
| 193 | |||
| 194 | sub fileRoot { | ||
| 195 | my ($self, $file) = @_; | ||
| 196 | return '/' unless $mswin; | ||
| 197 | if ($file =~ /^([a-zA-Z]\:\\)/) { | ||
| 198 | return $1 | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | sub filesOpenInProject { | ||
| 203 | my ($self, $project) = @_; | ||
| 204 | my $mdi = $self->mdi; | ||
| 205 | my @g = $self->gitFileList($project); | ||
| 206 | my @open = (); | ||
| 207 | for (@g) { | ||
| 208 | push @open, $_ if $mdi->docExists($_); | ||
| 209 | } | ||
| 210 | return @open | ||
| 211 | } | ||
| 212 | |||
| 213 | sub fileStrip { | ||
| 214 | my ($self, $project, $file) = @_; | ||
| 215 | my $offset = length($self->projectFolder($project)) + 1; | ||
| 216 | $file = substr($file, $offset); | ||
| 217 | return $file; | ||
| 218 | } | ||
| 219 | |||
| 220 | sub gitAdd { | ||
| 221 | my $self = shift; | ||
| 222 | my $project = $self->projectCurrent; | ||
| 223 | return if $project eq ''; | ||
| 224 | |||
| 225 | my $doc = $self->mdi->docSelected; | ||
| 226 | return unless defined $doc; | ||
| 227 | return unless -e $doc; | ||
| 228 | return if $self->fileInProject($project, $doc); | ||
| 229 | |||
| 230 | $doc = $self->fileStrip($project, $doc); | ||
| 231 | $self->gitCommand($project, "add $doc"); | ||
| 232 | $self->projectRefresh; | ||
| 233 | } | ||
| 234 | |||
| 235 | sub gitCollapse { $_[0]->{TREE}->collapseAll } | ||
| 236 | |||
| 237 | sub gitCommand { | ||
| 238 | my ($self, $project, $command) = @_; | ||
| 239 | my $folder = $self->projectFolder($project); | ||
| 240 | return unless defined $folder; | ||
| 241 | return `cd $folder; git $command` | ||
| 242 | } | ||
| 243 | |||
| 244 | sub gitExpand { $_[0]->{TREE}->expandAll } | ||
| 245 | |||
| 246 | sub gitFileList { | ||
| 247 | my ($self, $project) = @_; | ||
| 248 | my $list = $self->gitCommand($project, 'ls-files'); | ||
| 249 | my $folder = $self->projectFolder($project); | ||
| 250 | my $sep = $self->{TREE}->cget('-separator'); | ||
| 251 | my @items = (); | ||
| 252 | while ($list =~ s/([^\n]*)\n//) { | ||
| 253 | my $file = "$folder$sep$1"; | ||
| 254 | push @items, $file if -e $file | ||
| 255 | } | ||
| 256 | return @items | ||
| 257 | } | ||
| 258 | |||
| 259 | sub gitOpenAll { | ||
| 260 | my $self = shift; | ||
| 261 | my $project = $self->projectCurrent; | ||
| 262 | return if $project eq ''; | ||
| 263 | my @list = $self->gitFileList($project); | ||
| 264 | my $mdi = $self->mdi; | ||
| 265 | $mdi->silentMode(1); | ||
| 266 | for (@list) { | ||
| 267 | next unless -T $_; | ||
| 268 | $self->cmdExecute('doc_open', $_) unless $mdi->docExists($_) | ||
| 269 | } | ||
| 270 | $mdi->silentMode(0); | ||
| 271 | } | ||
| 272 | |||
| 273 | sub gitRemove { | ||
| 274 | my ($self, $project, $name) = @_; | ||
| 275 | if ($self->fileInProject($project, $name)) { | ||
| 276 | $self->cmdExecute('doc_close', $name) if $self->mdi->docExists($name); | ||
| 277 | |||
| 278 | #remove project path from $name | ||
| 279 | $name = $self->fileStrip($project, $name); | ||
| 280 | |||
| 281 | $self->gitCommand($project, "rm -f $name"); | ||
| 282 | $self->projectRefresh; | ||
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | sub gitRemoveDialog { | ||
| 287 | my ($self, $project, $name) = @_; | ||
| 288 | $project = $self->projectCurrent unless defined $project; | ||
| 289 | return if $project eq ''; | ||
| 290 | unless (defined $name) { | ||
| 291 | my $tree = $self->{TREE}; | ||
| 292 | my $folder = $self->projectFolder($project); | ||
| 293 | my $sep = $tree->cget('-separator'); | ||
| 294 | ($name) = $tree->infoSelection; | ||
| 295 | $name = "$folder$sep$name" if defined $name; | ||
| 296 | } | ||
| 297 | if (defined $name) { | ||
| 298 | my $answer = $self->popDialog( | ||
| 299 | 'Removing from repository', | ||
| 300 | "Are you sure you want to remove\n$name?", | ||
| 301 | 'dialog-warning', | ||
| 302 | 'Yes', 'No', | ||
| 303 | ); | ||
| 304 | $self->cmdExecute('git_remove', $project, $name) if $answer eq 'Yes'; | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | sub openDocAfter { | ||
| 309 | my $self = shift; | ||
| 310 | for (@_) { | ||
| 311 | my $folder = $self->projectFind($_); | ||
| 312 | if (defined $folder) { | ||
| 313 | my $name = $self->projectName($folder); | ||
| 314 | $self->projectAdd($name, $folder); | ||
| 315 | } | ||
| 316 | } | ||
| 317 | } | ||
| 318 | |||
| 319 | sub prj { return $_[0]->{PROJECTS} } | ||
| 320 | |||
| 321 | sub projectAdd { | ||
| 322 | my ($self, $project, $folder) = @_; | ||
| 323 | return if $project eq ''; | ||
| 324 | return if $self->projectExists($project); | ||
| 325 | $self->prj->{$project} = $folder; | ||
| 326 | $self->{PMENU}->add('command', | ||
| 327 | -label => $project, | ||
| 328 | -command => ['projectSelect', $self, $project], | ||
| 329 | ); | ||
| 330 | } | ||
| 331 | |||
| 332 | sub projectCurrent { | ||
| 333 | my $self = shift; | ||
| 334 | my $cur = $self->{CURRENT}; | ||
| 335 | $$cur = shift if @_; | ||
| 336 | return $$cur | ||
| 337 | } | ||
| 338 | |||
| 339 | sub projectExists { | ||
| 340 | my ($self, $project) = @_; | ||
| 341 | return exists $self->prj->{$project}; | ||
| 342 | } | ||
| 343 | |||
| 344 | sub projectFind { | ||
| 345 | my ($self, $file) = @_; | ||
| 346 | return undef unless -e $file; | ||
| 347 | my $root = $self->fileRoot($file); | ||
| 348 | while ($file ne $root) { | ||
| 349 | $file = dirname($file); | ||
| 350 | my $git = "$file/.git"; | ||
| 351 | return $file if (-e $git) and (-d $git) | ||
| 352 | } | ||
| 353 | return undef | ||
| 354 | } | ||
| 355 | |||
| 356 | sub projectFolder { | ||
| 357 | my ($self, $project) = @_; | ||
| 358 | return $self->prj->{$project}; | ||
| 359 | } | ||
| 360 | |||
| 361 | sub projectList { | ||
| 362 | my $self = shift; | ||
| 363 | my $prj = $self->prj; | ||
| 364 | return sort keys %$prj; | ||
| 365 | } | ||
| 366 | |||
| 367 | sub projectName { | ||
| 368 | my ($self, $folder) = @_; | ||
| 369 | my $url = `cd $folder; git config --local remote.origin.url`; | ||
| 370 | if ($url =~ /\/([^\/]+)\.git$/) { | ||
| 371 | return $1 | ||
| 372 | } | ||
| 373 | } | ||
| 374 | |||
| 375 | sub projectRefresh { | ||
| 376 | my $self = shift; | ||
| 377 | |||
| 378 | my $tree = $self->{TREE}; | ||
| 379 | $tree->deleteAll; | ||
| 380 | |||
| 381 | my $cur = $self->projectCurrent; | ||
| 382 | return if $cur eq ''; | ||
| 383 | |||
| 384 | my @list = $self->gitFileList($cur); | ||
| 385 | my $size = @list; | ||
| 386 | my $count = 0; | ||
| 387 | $self->mdi->progressAdd('git load', 'Loading project', $size, \$count); | ||
| 388 | for (@list) { | ||
| 389 | $tree->entryAdd($_); | ||
| 390 | $count ++; | ||
| 391 | $self->pause(20); | ||
| 392 | } | ||
| 393 | $self->mdi->progressRemove('git load'); | ||
| 394 | } | ||
| 395 | |||
| 396 | sub projectRemove { | ||
| 397 | my ($self, $project) = @_; | ||
| 398 | return unless $self->projectExists($project); | ||
| 399 | my $prj = $self->prj; | ||
| 400 | |||
| 401 | #remove from the menu | ||
| 402 | my @p = $self->projectList; | ||
| 403 | my $size = @p; | ||
| 404 | my $menu = $self->{PMENU}; | ||
| 405 | |||
| 406 | for (0 .. $size - 1) { | ||
| 407 | if ($menu->entrycget($_, '-label') eq $project) { | ||
| 408 | $menu->delete($_); | ||
| 409 | last; | ||
| 410 | } | ||
| 411 | } | ||
| 412 | |||
| 413 | #unselect if this project is currently selected | ||
| 414 | $self->projectSelect('') if $self->projectCurrent eq $project; | ||
| 415 | |||
| 416 | #remove | ||
| 417 | delete $prj->{$project}; | ||
| 418 | } | ||
| 419 | |||
| 420 | sub projectSelect { | ||
| 421 | my ($self, $project) = @_; | ||
| 422 | $self->projectCurrent($project); | ||
| 423 | $self->projectRefresh; | ||
| 424 | } | ||
| 425 | |||
| 426 | sub selectDocBefore { | ||
| 427 | my ($self, $file) = @_; | ||
| 428 | $self->selectExternal($file); | ||
| 429 | } | ||
| 430 | |||
| 431 | sub selectExternal { | ||
| 432 | my ($self, $name) = @_; | ||
| 433 | my $cur = $self->projectCurrent; | ||
| 434 | return if $cur eq ''; | ||
| 435 | $self->{TREE}->entrySelect($name) if $self->fileInProject($cur, $name); | ||
| 436 | } | ||
| 437 | |||
| 438 | sub selectInternal { | ||
| 439 | my ($self, $name) = @_; | ||
| 440 | my $mdi = $self->mdi; | ||
| 441 | unless ($mdi->docExists($name)) { | ||
| 442 | if (-T $name) { | ||
| 443 | $self->cmdExecute('doc_open', $name); | ||
| 444 | } else { | ||
| 445 | $self->openURL($name) | ||
| 446 | } | ||
| 447 | } | ||
| 448 | $self->cmdExecute('doc_select', $name) if $mdi->docExists($name); | ||
| 449 | } | ||
| 450 | |||
| 451 | sub Unload { | ||
| 452 | my $self = shift; | ||
| 453 | $self->ToolLeftPageRemove('Git'); | ||
| 454 | for ( | ||
| 455 | 'git_add', | ||
| 456 | 'git_collapse', | ||
| 457 | 'git_command', | ||
| 458 | 'git_expand', | ||
| 459 | 'git_open_all', | ||
| 460 | 'git_remove', | ||
| 461 | 'git_remove_dialog', | ||
| 462 | ) { | ||
| 463 | $self->cmdRemove($_); | ||
| 464 | } | ||
| 465 | $self->cmdUnhookAfter('doc_close', 'closeDocAfter', $self); | ||
| 466 | $self->cmdUnhookAfter('doc_open', 'openDocAfter', $self); | ||
| 467 | $self->cmdUnhookBefore('doc_select', 'selectDocBefore', $self); | ||
| 468 | my $flag = $self->SUPER::Unload; | ||
| 469 | my $mdi = $self->mdi; | ||
| 470 | $self->after(100, ['navContextMenu', $mdi]); | ||
| 471 | return $flag; | ||
| 472 | } | ||
| 473 | |||
| 474 | =head1 LICENSE | ||
| 475 | |||
| 476 | Same as Perl. | ||
| 477 | |||
| 478 | =head1 AUTHOR | ||
| 479 | |||
| 480 | Hans Jeuken (hanje at cpan dot org) | ||
| 481 | |||
| 482 | =head1 BUGS AND CAVEATS | ||
| 483 | |||
| 484 | If you find any bugs, please report them here L<https://github.com/haje61/App-Codit/issues>. | ||
| 485 | |||
| 486 | =cut | ||
| 487 | |||
| 488 | |||
| 489 | |||
| 490 | |||
| 491 | 1; |