gitview: annotation support
List files modifed as a part of the commit in the diff window Support annotation of the file listed in the diff window Support history browsing in the annotation window. Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
Aneesh Kumar K.V committed
Apr 19, 2007 at 22:26 UTC
2c9750cc8b902a55669183e05533207dd7ec71fd
1 file changed
+256
-4
contrib/gitview/gitview
+256
-4
@@ -10,7 +10,8 @@ GUI browser for git repository
10
This program is based on bzrk by Scott James Remnant <scott@ubuntu.com>
11
"""
12
__copyright__ = "Copyright (C) 2006 Hewlett-Packard Development Company, L.P."
13
-__author__ = "Aneesh Kumar K.V <aneesh.kumar@hp.com>"
13
+__copyright__ = "Copyright (C) 2007 Aneesh Kumar K.V <aneesh.kumar@gmail.com"
14
+__author__ = "Aneesh Kumar K.V <aneesh.kumar@gmail.com>"
15
16
17
import sys
@@ -24,6 +25,7 @@ import gobject
25
import cairo
26
import math
27
import string
28
+import fcntl
29
30
try:
31
import gtksourceview
@@ -337,6 +339,186 @@ class Commit:
339
fp.close()
340
return diff
341
342
+class AnnotateWindow:
343
+ """Annotate window.
344
+ This object represents and manages a single window containing the
345
+ annotate information of the file
346
+ """
347
+
348
+ def __init__(self):
349
+ self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
350
+ self.window.set_border_width(0)
351
+ self.window.set_title("Git repository browser annotation window")
352
+
353
+ # Use two thirds of the screen by default
354
+ screen = self.window.get_screen()
355
+ monitor = screen.get_monitor_geometry(0)
356
+ width = int(monitor.width * 0.66)
357
+ height = int(monitor.height * 0.66)
358
+ self.window.set_default_size(width, height)
359
+
360
+ def add_file_data(self, filename, commit_sha1, line_num):
361
+ fp = os.popen("git cat-file blob " + commit_sha1 +":"+filename)
362
+ i = 1;
363
+ for line in fp.readlines():
364
+ line = string.rstrip(line)
365
+ self.model.append(None, ["HEAD", filename, line, i])
366
+ i = i+1
367
+ fp.close()
368
+
369
+ # now set the cursor position
370
+ self.treeview.set_cursor(line_num-1)
371
+ self.treeview.grab_focus()
372
+
373
+ def _treeview_cursor_cb(self, *args):
374
+ """Callback for when the treeview cursor changes."""
375
+ (path, col) = self.treeview.get_cursor()
376
+ commit_sha1 = self.model[path][0]
377
+ commit_msg = ""
378
+ fp = os.popen("git cat-file commit " + commit_sha1)
379
+ for line in fp.readlines():
380
+ commit_msg = commit_msg + line
381
+ fp.close()
382
+
383
+ self.commit_buffer.set_text(commit_msg)
384
+
385
+ def _treeview_row_activated(self, *args):
386
+ """Callback for when the treeview row gets selected."""
387
+ (path, col) = self.treeview.get_cursor()
388
+ commit_sha1 = self.model[path][0]
389
+ filename = self.model[path][1]
390
+ line_num = self.model[path][3]
391
+
392
+ window = AnnotateWindow();
393
+ fp = os.popen("git rev-parse "+ commit_sha1 + "~1")
394
+ commit_sha1 = string.strip(fp.readline())
395
+ fp.close()
396
+ window.annotate(filename, commit_sha1, line_num)
397
+
398
+ def data_ready(self, source, condition):
399
+ while (1):
400
+ try :
401
+ buffer = source.read(8192)
402
+ except:
403
+ # resource temporary not available
404
+ return True
405
+
406
+ if (len(buffer) == 0):
407
+ gobject.source_remove(self.io_watch_tag)
408
+ source.close()
409
+ return False
410
+
411
+ for buff in buffer.split("\n"):
412
+ annotate_line = re.compile('^([0-9a-f]{40}) (.+) (.+) (.+)$')
413
+ m = annotate_line.match(buff)
414
+ if not m:
415
+ annotate_line = re.compile('^(filename) (.+)$')
416
+ m = annotate_line.match(buff)
417
+ if not m:
418
+ continue
419
+ filename = m.group(2)
420
+ else:
421
+ self.commit_sha1 = m.group(1)
422
+ self.source_line = int(m.group(2))
423
+ self.result_line = int(m.group(3))
424
+ self.count = int(m.group(4))
425
+ #set the details only when we have the file name
426
+ continue
427
+
428
+ while (self.count > 0):
429
+ # set at result_line + count-1 the sha1 as commit_sha1
430
+ self.count = self.count - 1
431
+ iter = self.model.iter_nth_child(None, self.result_line + self.count-1)
432
+ self.model.set(iter, 0, self.commit_sha1, 1, filename, 3, self.source_line)
433
+
434
+
435
+ def annotate(self, filename, commit_sha1, line_num):
436
+ # verify the commit_sha1 specified has this filename
437
+
438
+ fp = os.popen("git ls-tree "+ commit_sha1 + " -- " + filename)
439
+ line = string.strip(fp.readline())
440
+ if line == '':
441
+ # pop up the message the file is not there as a part of the commit
442
+ fp.close()
443
+ dialog = gtk.MessageDialog(parent=None, flags=0,
444
+ type=gtk.MESSAGE_WARNING, buttons=gtk.BUTTONS_CLOSE,
445
+ message_format=None)
446
+ dialog.set_markup("The file %s is not present in the parent commit %s" % (filename, commit_sha1))
447
+ dialog.run()
448
+ dialog.destroy()
449
+ return
450
+
451
+ fp.close()
452
+
453
+ vpan = gtk.VPaned();
454
+ self.window.add(vpan);
455
+ vpan.show()
456
+
457
+ scrollwin = gtk.ScrolledWindow()
458
+ scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
459
+ scrollwin.set_shadow_type(gtk.SHADOW_IN)
460
+ vpan.pack1(scrollwin, True, True);
461
+ scrollwin.show()
462
+
463
+ self.model = gtk.TreeStore(str, str, str, int)
464
+ self.treeview = gtk.TreeView(self.model)
465
+ self.treeview.set_rules_hint(True)
466
+ self.treeview.set_search_column(0)
467
+ self.treeview.connect("cursor-changed", self._treeview_cursor_cb)
468
+ self.treeview.connect("row-activated", self._treeview_row_activated)
469
+ scrollwin.add(self.treeview)
470
+ self.treeview.show()
471
+
472
+ cell = gtk.CellRendererText()
473
+ cell.set_property("width-chars", 10)
474
+ cell.set_property("ellipsize", pango.ELLIPSIZE_END)
475
+ column = gtk.TreeViewColumn("Commit")
476
+ column.set_resizable(True)
477
+ column.pack_start(cell, expand=True)
478
+ column.add_attribute(cell, "text", 0)
479
+ self.treeview.append_column(column)
480
+
481
+ cell = gtk.CellRendererText()
482
+ cell.set_property("width-chars", 20)
483
+ cell.set_property("ellipsize", pango.ELLIPSIZE_END)
484
+ column = gtk.TreeViewColumn("File Name")
485
+ column.set_resizable(True)
486
+ column.pack_start(cell, expand=True)
487
+ column.add_attribute(cell, "text", 1)
488
+ self.treeview.append_column(column)
489
+
490
+ cell = gtk.CellRendererText()
491
+ cell.set_property("width-chars", 20)
492
+ cell.set_property("ellipsize", pango.ELLIPSIZE_END)
493
+ column = gtk.TreeViewColumn("Data")
494
+ column.set_resizable(True)
495
+ column.pack_start(cell, expand=True)
496
+ column.add_attribute(cell, "text", 2)
497
+ self.treeview.append_column(column)
498
+
499
+ # The commit message window
500
+ scrollwin = gtk.ScrolledWindow()
501
+ scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
502
+ scrollwin.set_shadow_type(gtk.SHADOW_IN)
503
+ vpan.pack2(scrollwin, True, True);
504
+ scrollwin.show()
505
+
506
+ commit_text = gtk.TextView()
507
+ self.commit_buffer = gtk.TextBuffer()
508
+ commit_text.set_buffer(self.commit_buffer)
509
+ scrollwin.add(commit_text)
510
+ commit_text.show()
511
+
512
+ self.window.show()
513
+
514
+ self.add_file_data(filename, commit_sha1, line_num)
515
+
516
+ fp = os.popen("git blame --incremental -- " + filename + " " + commit_sha1)
517
+ flags = fcntl.fcntl(fp.fileno(), fcntl.F_GETFL)
518
+ fcntl.fcntl(fp.fileno(), fcntl.F_SETFL, flags | os.O_NONBLOCK)
519
+ self.io_watch_tag = gobject.io_add_watch(fp, gobject.IO_IN, self.data_ready)
520
+
521
+
522
class DiffWindow:
523
"""Diff window.
524
This object represents and manages a single window containing the
@@ -355,6 +537,7 @@ class DiffWindow:
537
height = int(monitor.height * 0.66)
538
self.window.set_default_size(width, height)
539
540
+
541
self.construct()
542
543
def construct(self):
@@ -371,10 +554,12 @@ class DiffWindow:
554
vbox.pack_start(menu_bar, expand=False, fill=True)
555
menu_bar.show()
556
557
+ hpan = gtk.HPaned()
558
+
559
scrollwin = gtk.ScrolledWindow()
560
scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
561
scrollwin.set_shadow_type(gtk.SHADOW_IN)
377
- vbox.pack_start(scrollwin, expand=True, fill=True)
562
+ hpan.pack1(scrollwin, True, True)
563
scrollwin.show()
564
565
if have_gtksourceview:
@@ -388,11 +573,77 @@ class DiffWindow:
573
self.buffer = gtk.TextBuffer()
574
sourceview = gtk.TextView(self.buffer)
575
576
+
577
sourceview.set_editable(False)
578
sourceview.modify_font(pango.FontDescription("Monospace"))
579
scrollwin.add(sourceview)
580
sourceview.show()
581
582
+ # The file hierarchy: a scrollable treeview
583
+ scrollwin = gtk.ScrolledWindow()
584
+ scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
585
+ scrollwin.set_shadow_type(gtk.SHADOW_IN)
586
+ scrollwin.set_size_request(20, -1)
587
+ hpan.pack2(scrollwin, True, True)
588
+ scrollwin.show()
589
+
590
+ self.model = gtk.TreeStore(str, str, str)
591
+ self.treeview = gtk.TreeView(self.model)
592
+ self.treeview.set_search_column(1)
593
+ self.treeview.connect("cursor-changed", self._treeview_clicked)
594
+ scrollwin.add(self.treeview)
595
+ self.treeview.show()
596
+
597
+ cell = gtk.CellRendererText()
598
+ cell.set_property("width-chars", 20)
599
+ column = gtk.TreeViewColumn("Select to annotate")
600
+ column.pack_start(cell, expand=True)
601
+ column.add_attribute(cell, "text", 0)
602
+ self.treeview.append_column(column)
603
+
604
+ vbox.pack_start(hpan, expand=True, fill=True)
605
+ hpan.show()
606
+
607
+ def _treeview_clicked(self, *args):
608
+ """Callback for when the treeview cursor changes."""
609
+ (path, col) = self.treeview.get_cursor()
610
+ specific_file = self.model[path][1]
611
+ commit_sha1 = self.model[path][2]
612
+ if specific_file == None :
613
+ return
614
+ elif specific_file == "" :
615
+ specific_file = None
616
+
617
+ window = AnnotateWindow();
618
+ window.annotate(specific_file, commit_sha1, 1)
619
+
620
+
621
+ def commit_files(self, commit_sha1, parent_sha1):
622
+ self.model.clear()
623
+ add = self.model.append(None, [ "Added", None, None])
624
+ dele = self.model.append(None, [ "Deleted", None, None])
625
+ mod = self.model.append(None, [ "Modified", None, None])
626
+ diff_tree = re.compile('^(:.{6}) (.{6}) (.{40}) (.{40}) (A|D|M)\s(.+)$')
627
+ fp = os.popen("git diff-tree -r --no-commit-id " + parent_sha1 + " " + commit_sha1)
628
+ while 1:
629
+ line = string.strip(fp.readline())
630
+ if line == '':
631
+ break
632
+ m = diff_tree.match(line)
633
+ if not m:
634
+ continue
635
+
636
+ attr = m.group(5)
637
+ filename = m.group(6)
638
+ if attr == "A":
639
+ self.model.append(add, [filename, filename, commit_sha1])
640
+ elif attr == "D":
641
+ self.model.append(dele, [filename, filename, commit_sha1])
642
+ elif attr == "M":
643
+ self.model.append(mod, [filename, filename, commit_sha1])
644
+ fp.close()
645
+
646
+ self.treeview.expand_all()
647
648
def set_diff(self, commit_sha1, parent_sha1, encoding):
649
"""Set the differences showed by this window.
@@ -406,6 +657,7 @@ class DiffWindow:
657
fp = os.popen("git diff-tree -p " + parent_sha1 + " " + commit_sha1)
658
self.buffer.set_text(unicode(fp.read(), encoding).encode('utf-8'))
659
fp.close()
660
+ self.commit_files(commit_sha1, parent_sha1)
661
self.window.show()
662
663
def save_menu_response(self, widget, string):
@@ -425,7 +677,7 @@ class DiffWindow:
677
class GitView:
678
""" This is the main class
679
"""
428
- version = "0.8"
680
+ version = "0.9"
681
682
def __init__(self, with_diff=0):
683
self.with_diff = with_diff
@@ -590,7 +842,7 @@ class GitView:
842
dialog = gtk.AboutDialog()
843
dialog.set_name("Gitview")
844
dialog.set_version(GitView.version)
593
- dialog.set_authors(["Aneesh Kumar K.V <aneesh.kumar@hp.com>"])
845
+ dialog.set_authors(["Aneesh Kumar K.V <aneesh.kumar@gmail.com>"])
846
dialog.set_website("http://www.kernel.org/pub/software/scm/git/")
847
dialog.set_copyright("Use and distribute under the terms of the GNU General Public License")
848
dialog.set_wrap_license(True)