summaryrefslogtreecommitdiffstats
path: root/g-ed-it/docBar.py
blob: e7deb24437b27e0b1e99ed3c6faf4da51a97fcbc (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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
#-*- coding:utf-8 -*-

import gedit
import gtk

import subprocess

import os
import os.path

class DocBar (object):

	def __init__(self, tab, commitDialog):
		self.tab = tab
		self.child = self.tab.get_children()[0]
		self.tab.remove(self.child)
		self.vbox = gtk.VBox()
		
		self.commitDialog = commitDialog
		
		self.doc = self.tab.get_document()
		self.update_handler1 =  self.doc.connect("saved",self.doc_changed)
		self.update_handler2 =  self.doc.connect("loaded",self.doc_changed)
		
		self.docBar = gtk.HBox() 
		
		self.lbl_status = gtk.Label("État du fichier")
		
		self.btn_add = gtk.Button("add")
		self.btn_add.connect("clicked", self.add_file)
		
		self.btn_commit = gtk.Button("commit")
		self.btn_commit.connect("clicked", self.commit_file)
		
		self.btn_diff_head_index = gtk.Button("diff HEAD/INDEX")
		self.btn_diff_head_index.connect("clicked", self.diff_head_index)
		
		self.btn_diff_index_wt = gtk.Button("diff INDEX/WT")
		self.btn_diff_index_wt.connect("clicked", self.diff_index_wt)
		
		self.docBar.pack_start(self.btn_diff_head_index, False, False)
		self.docBar.pack_start(self.btn_diff_index_wt, False, False)
		self.docBar.pack_start(self.lbl_status, True, False)
		self.docBar.pack_start(self.btn_add, False, False)
		self.docBar.pack_start(self.btn_commit, False, False)
		
		self.vbox.pack_start(self.docBar, False, False)
		self.vbox.pack_start(self.child)
		
		self.tab.add(self.vbox)
		
		self.tab.show_all()
		
		self.getDocState()
		
	def deactivate(self):
		self.tab.remove(self.vbox)
		self.vbox.remove(self.child)
		self.tab.add(self.child)
		
		self.doc.disconnect(self.update_handler1)
		self.doc.disconnect(self.update_handler2)
		
		self.doc = None
		self.vbox = None
		self.child = None
		self.tab = None
		
	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,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
		self.setDocInfo()
		pass
	
	def setDocInfo(self):
		if not self.inGitDir :
			self.docBar.hide()
			return 
		self.docBar.show()
		self.btn_add.set_sensitive(self.index2WT!=None)
		self.btn_commit.set_sensitive(self.HEAD2index!=None)
		self.btn_diff_head_index.set_sensitive(self.HEAD2index!=None)
		self.btn_diff_index_wt.set_sensitive(self.index2WT!=None)
		if not self.iscached :
			self.lbl_status.set_label("Not cached")
			self.btn_add.set_sensitive(True)
		else:
			text = "HEAD -- "
			if self.HEAD2index == "A":
				text = text + "Added"
			elif self.HEAD2index == "C":
				text = text + "Copied"
			elif self.HEAD2index == "D":
				text = text + "Deleted"
			elif self.HEAD2index == "M":
				text = text + "Modified"
			elif self.HEAD2index == "R":
				text = text + "Renamed"
			elif self.HEAD2index == "T":
				text = text + "Type changed"
			elif self.HEAD2index == "U":
				text = text + "Unmerged"
			elif self.HEAD2index == "X":
				text = text + "Unknown"
			elif self.HEAD2index == "B":
				text = text + "pairing Broken"
			text = text + " --> INDEX -- "
			if self.index2WT == "A":
				text = text + "Added"
			elif self.index2WT == "C":
				text = text + "Copied"
			elif self.index2WT == "D":
				text = text + "Deleted"
			elif self.index2WT == "M":
				text = text + "Modified"
			elif self.index2WT == "R":
				text = text + "Renamed"
			elif self.index2WT == "T":
				text = text + "Type changed"
			elif self.index2WT == "U":
				text = text + "Unmerged"
			elif self.index2WT == "X":
				text = text + "Unknown"
			elif self.index2WT == "B":
				text = text + "pairing Broken"
			text = text + " --> WT"
			self.lbl_status.set_label(text)
		pass
	
	def doc_changed(self,doc,arg1):
		self.getDocState()
		
	def add_file(self,button):
		uri = self.doc.get_uri_for_display()
		subprocess.call(["git-add",os.path.basename(uri)],stdout=subprocess.PIPE,cwd=os.path.dirname(uri))
		self.getDocState()
		pass
		
	def commit_file(self,button):
		uri = self.doc.get_uri_for_display()
		self.commitDialog.show(uri,self)
		pass

	def diff_head_index(self,button):
		uri = self.doc.get_uri_for_display()
		subprocess.call(["git-diff","--cached",os.path.basename(uri)],cwd=os.path.dirname(uri))
		pass
	
	def diff_index_wt(self,button):
		uri = self.doc.get_uri_for_display()
		subprocess.call(["git-diff",os.path.basename(uri)],cwd=os.path.dirname(uri))
		pass