-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-icon-emblems.py
122 lines (106 loc) · 4.09 KB
/
git-icon-emblems.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-
# Nautilus GIT Icon Emblems # started life as Nautilus SVN Icon Emblems though very little remains
# Version 1.2
# Copyright (c) 2008, Perberos <perberos@gmail.com>
# Gary Bishop <gb@cs.unc.edu>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import urllib
import gtk
import nautilus
from subprocess import Popen, PIPE
import time
import re
parentRE = re.compile(r'[./]+$')
if False:
fp = file('/tmp/gb.log', 'w')
import sys
sys.stdout = sys.stderr = fp
def dbprint(*args):
if fp:
fp.write(' '.join([str(arg) for arg in args]))
fp.write('\n')
fp.flush()
else:
def dbprint(*args):
pass
class GitFolderBranchProvider(nautilus.LocationWidgetProvider):
'''Display a label in Git controlled folders indicating the branches'''
def __init__(self):
pass
def get_widget(self, uri, window):
filename = urllib.url2pathname(uri[7:])
p = Popen(["git", "branch"], stdout=PIPE, cwd=filename)
output = p.communicate()[0]
if p.returncode != 0:
dbprint("not git dir")
return None
entry = gtk.Label()
entry.set_text(output.strip())
entry.show()
return entry
class GitIconEmblems(nautilus.InfoProvider):
def __init__(self):
dbprint("Initializing git-icons-emblems extension")
# initialize a simple cache
self.dirname = None
self.dirtime = time.time()
def update_file_info(self, file):
# get all values of path and filename
dbprint('update_file_info')
filename = urllib.url2pathname(file.get_uri()[7:])
dirname = os.path.dirname(filename)
basename = os.path.basename(filename)
if file.is_directory():
basename = basename + '/'
if os.path.exists(os.path.join(filename, '.git')):
file.add_emblem('GitOK')
dbprint(filename, dirname, basename)
# cache the output of git status for 10 seconds or until the folder changes
if self.dirname != dirname or time.time() - self.dirtime > 10:
self.dirname = dirname
self.dirtime = time.time()
self.files = None
p = Popen(["git", "status", "-s"], stdout=PIPE, cwd=dirname)
output = p.communicate()[0]
if p.returncode != 0:
dbprint("not git dir")
return
self.files = {}
for line in output.split('\n'):
stat = line[0:2]
fname = line[3:]
if parentRE.match(fname) and stat == '??':
self.files['..'] = stat
else:
self.files[fname] = stat
dbprint('files=', self.files)
if self.files is None: # not git controlled
return
stat = self.files.get(basename)
if stat is None:
if '..' in self.files:
file.add_emblem('GitUntracked')
elif stat == 'A ':
file.add_emblem('GitAdded') # added
elif stat == ' M':
file.add_emblem('GitModified') # modified
elif stat == 'AM':
file.add_emblem('GitAdded') # added
file.add_emblem('GitModified') # modified
elif stat == '??':
file.add_emblem('GitUntracked') # untracked
else:
file.add_emblem('GitConflict') # conflict or something I didn't account for