summaryrefslogtreecommitdiffstats
path: root/g-ed-it/docHelper.py
blob: e44ab2f8f2731638dba1acd89d69d5aedaaa04e4 (plain)
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
#!/usr/bin/env python
#-*- coding:utf-8 -*-

#    Copyright 2009 Matthieu Gautier

#    This file is part of g-ed-it.
#
#    g-ed-it 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
#    any later version.
#
#    g-ed-it 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 g-ed-it.  If not, see <http://www.gnu.org/licenses/>.

import os.path
import subprocess

import docBar

class DocHelper (object):
	def __init__(self, tab, window, gitAction):
		self.doc = tab.get_document()
		self.gitAction = gitAction
		self.tab = tab
		self.getDocState()
				
		self.update_handler1 =  self.doc.connect("saved",self.doc_changed)
		self.update_handler2 =  self.doc.connect("loaded",self.doc_changed)
		pass
	
	def deactivate(self):
		self.doc.disconnect(self.update_handler1)
		self.doc.disconnect(self.update_handler2)
		
	def update_ui(self):
		self.getDocState()
	
	def doc_changed(self,doc,arg1):
		self.getDocState()
		
	def getDocState(self):
		uri = self.doc.get_uri_for_display()
		cwd = os.path.dirname(uri)
		bname = os.path.basename(uri)
		
		self.inGitDir = False
		self.isCached = False
		self.HEAD2index = None
		self.index2WT = None
		
		if not self.doc.is_untitled():
			subPro = subprocess.Popen(["git-ls-files",os.path.basename(uri)],stdout=subprocess.PIPE,stderr=subprocess.STDOUT,cwd=cwd)
			statusStr = subPro.communicate()[0]
			if subPro.returncode == 0 :
				self.inGitDir = True
				if statusStr != "":
					self.isCached = True
					statusStr = subprocess.Popen(["git-diff","--cached","--name-status",os.path.basename(uri)],stdout=subprocess.PIPE,cwd=cwd).communicate()[0]
					if statusStr != "":
						status = statusStr[:-1].split()[0]
						self.HEAD2index = status
					statusStr = subprocess.Popen(["git-diff","--name-status",os.path.basename(uri)],stdout=subprocess.PIPE,cwd=cwd).communicate()[0]
					if statusStr != "":
						status = statusStr[:-1].split()[0]
						self.index2WT = status
		pass