@@ -161,6 +161,31 @@ def get_stat(self, filename):
161
161
return ", " .join (stat .split ("\n " )[1 ].split ("," )[1 :])
162
162
return None
163
163
164
+ def get_project_branch (self ):
165
+ branch = ""
166
+ project_name = self .get_project_name ()
167
+ if project_name :
168
+ branch += project_name + "/"
169
+ branch += self .get_branch ()
170
+ return branch
171
+
172
+ def check_branch_name (self , branch ):
173
+ return True
174
+
175
+ def get_branch_list (self ):
176
+ b_list = execute ("git branch --list" , self .dir ).split ("\n " )
177
+ def clean_branch_name (branch_name ):
178
+ return str (branch_name ).lstrip ("*" ).strip ()
179
+ return list (map (clean_branch_name , b_list ))
180
+
181
+ def update_branch (self , branch ):
182
+ branches = self .get_branch_list ()
183
+ if branch in branches :
184
+ execute ("git checkout {}" .format (branch ), self .dir )
185
+ else :
186
+ execute ("git checkout -b {0}" .format (branch ), self .dir )
187
+
188
+
164
189
class NautilusPropertyPage (Gtk .Grid ):
165
190
"""Property page main widget class."""
166
191
def __init__ (self , git ):
@@ -209,8 +234,9 @@ class NautilusLocation(Gtk.InfoBar):
209
234
_popover = None
210
235
_diff_button = None
211
236
212
- def __init__ (self , git ):
237
+ def __init__ (self , git , window ):
213
238
Gtk .InfoBar .__init__ (self )
239
+ self ._window = window
214
240
self ._git = git
215
241
self .set_message_type (Gtk .MessageType .QUESTION )
216
242
self .show ()
@@ -229,16 +255,11 @@ def _build_widgets(self):
229
255
image .show ()
230
256
container .attach (image , 0 , 0 , 1 , 1 )
231
257
232
- label = Gtk .Label ()
233
- project = self ._git .get_project_name ()
234
- branch = ""
235
- if project :
236
- branch = "{0}/" .format (project )
237
- branch += self ._git .get_branch ()
238
-
239
- label .set_text (branch )
240
- label .show ()
241
- container .attach (label , 1 , 0 , 1 , 1 )
258
+ branch_button = Gtk .Button ()
259
+ branch_button .set_label (self ._git .get_project_branch ())
260
+ branch_button .connect ("clicked" , self ._update_branch )
261
+ branch_button .show ()
262
+ container .attach (branch_button , 1 , 0 , 1 , 1 )
242
263
self .get_content_area ().add (container )
243
264
244
265
status = self ._git .get_status ()
@@ -254,13 +275,17 @@ def _build_widgets(self):
254
275
255
276
self .get_action_area ().pack_end (button , False , False , 0 )
256
277
278
+ def _update_branch (self , button ):
279
+ commit = BranchWidget (self ._git , self ._window )
280
+
257
281
@staticmethod
258
282
def _build_status_widget (status ):
259
283
"""Build a widget, contains a counter of modified/added/removed files."""
260
284
i = 0
261
285
grid = Gtk .Grid ()
262
286
grid .set_row_spacing (3 )
263
287
grid .set_column_spacing (3 )
288
+ grid .set_valign (Gtk .Align .CENTER )
264
289
grid .show ()
265
290
for _status in status :
266
291
if int (status [_status ]) > 0 :
@@ -408,6 +433,89 @@ def _build_main(self):
408
433
box .show ()
409
434
self .add (box )
410
435
436
+
437
+ class BranchWidget (Gtk .Window ):
438
+
439
+ def __init__ (self , git , window ):
440
+ self ._git = git
441
+ Gtk .Window .__init__ (self , Gtk .WindowType .POPUP )
442
+ # Header Bar
443
+ self ._build_headerbar ()
444
+
445
+ self .set_position (Gtk .WindowPosition .CENTER )
446
+ self .set_titlebar (self .hb )
447
+ self .set_default_size (350 , 100 )
448
+ self .set_transient_for (window )
449
+ self .set_modal (True )
450
+ self .set_resizable (False )
451
+ self .set_border_width (18 )
452
+ self ._build_main_widget ()
453
+
454
+ self .show_all ()
455
+
456
+ def _build_headerbar (self ):
457
+ self .hb = Gtk .HeaderBar ()
458
+ self .hb .set_title (self ._git .get_project_branch ())
459
+ # self.hb.set_show_close_button(True)
460
+
461
+ self .apply = Gtk .Button ()
462
+ self .apply .set_label (_ ("Apply" ))
463
+ self .apply .get_style_context ().add_class ("suggested-action" )
464
+ self .apply .connect ("clicked" , self .update_branch )
465
+ self .apply .set_sensitive (False )
466
+ self .apply .show ()
467
+ self .hb .pack_end (self .apply )
468
+
469
+ self .cancel = Gtk .Button ()
470
+ self .cancel .set_label (_ ("Cancel" ))
471
+ self .cancel .connect ("clicked" , self .close_window )
472
+ self .cancel .show ()
473
+ self .hb .pack_start (self .cancel )
474
+
475
+ def _build_main_widget (self ):
476
+ grid = Gtk .Grid ()
477
+ branches = self ._git .get_branch_list ()
478
+ current_branch = self ._git .get_branch ()
479
+ self .branch_entry = Gtk .ComboBoxText .new_with_entry ()
480
+ self .branch_entry .set_entry_text_column (0 )
481
+ i = 0
482
+ for branch in branches :
483
+ if branch == current_branch :
484
+ active_id = i
485
+ self .branch_entry .append_text (branch )
486
+ i += 1
487
+ self .branch_entry .set_active (active_id )
488
+ self .branch_entry .connect ("changed" , self ._validate_branch_name )
489
+ self .branch_entry .show ()
490
+ grid .set_halign (Gtk .Align .CENTER )
491
+ grid .add (self .branch_entry )
492
+ grid .show ()
493
+ self .add (grid )
494
+
495
+ def _validate_branch_name (self , entry ):
496
+ branch = entry .get_active_text ().strip ()
497
+ valid = True
498
+ if branch == self ._git .get_branch () or not branch :
499
+ valid = False
500
+ else :
501
+ valid = self ._git .check_branch_name (branch )
502
+
503
+ self .apply .set_sensitive (valid )
504
+ if valid :
505
+ entry .get_style_context ().remove_class ("error" )
506
+ else :
507
+ entry .get_style_context ().add_class ("error" )
508
+
509
+
510
+ def update_branch (self , * args ):
511
+ branch = self .branch_entry .get_active_text ().strip ()
512
+ self ._git .update_branch (branch )
513
+ self .close_window ()
514
+ # Todo : refresh the window if possible?
515
+
516
+ def close_window (self , * args ):
517
+ self .destroy ()
518
+
411
519
class NautilusGitLocationWidget (GObject .GObject , Nautilus .LocationWidgetProvider ):
412
520
"""Location widget extension."""
413
521
def __init__ (self ):
@@ -420,7 +528,7 @@ def get_widget(self, uri, window):
420
528
self .window = window
421
529
if is_git (uri ):
422
530
git = Git (uri )
423
- widget = NautilusLocation (git )
531
+ widget = NautilusLocation (git , self . window )
424
532
return widget
425
533
else :
426
534
return None
0 commit comments