Class: Fugit::IndexList
- Inherits:
-
Panel
- Object
- Panel
- Fugit::IndexList
- Defined in:
- lib/fugit/index_list.rb
Instance Method Summary collapse
-
#initialize(parent) ⇒ IndexList
constructor
A new instance of IndexList.
- #on_click(event) ⇒ Object
- #on_double_click(event) ⇒ Object
- #on_menu_request(event) ⇒ Object
- #on_menu_revert_changes(event) ⇒ Object
- #on_menu_stage_file(event) ⇒ Object
- #process_staging(file, change, status) ⇒ Object
- #set_diff(file, change, status) ⇒ Object
- #update_tree ⇒ Object
Constructor Details
#initialize(parent) ⇒ IndexList
Returns a new instance of IndexList.
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 |
# File 'lib/fugit/index_list.rb', line 6 def initialize(parent) super(parent, ID_ANY) @index = TreeCtrl.new(self, ID_ANY, nil, nil, TR_MULTIPLE|TR_HIDE_ROOT|TR_FULL_ROW_HIGHLIGHT|TR_NO_LINES) imagelist = ImageList.new(16, 16) imagelist << get_icon("asterisk_yellow.png") imagelist << get_icon("tick.png") imagelist << get_icon("script_add.png") imagelist << get_icon("script_edit.png") imagelist << get_icon("script_delete.png") imagelist << get_icon("script.png") @index.set_image_list(imagelist) root = @index.add_root("root") @unstaged = @index.append_item(root, "Unstaged", 0) @staged = @index.append_item(root, "Staged", 1) @index.set_item_bold(@unstaged) @index.set_item_bold(@staged) = Menu.new = .append('Stage file') = .append('Revert changes') = Menu.new = .append('Unstage file') (, :on_menu_stage_file) (, :on_menu_revert_changes) (, :on_menu_stage_file) (@index.get_id, :on_menu_request) box = BoxSizer.new(VERTICAL) box.add(@index, 1, EXPAND) self.set_sizer(box) evt_tree_sel_changed(@index.get_id, :on_click) evt_tree_item_activated(@index.get_id, :on_double_click) evt_tree_item_collapsing(@index.get_id) {|event| event.veto} (:refresh) {update_tree if is_shown_on_screen} (:branch_checkout) {update_tree if is_shown_on_screen} (:commit_saved, :update_tree) (:index_changed, :update_tree) (:tab_switch, :update_tree) (:exiting) {self.hide} # Things seem to run smoother if we hide before destruction update_tree end |
Instance Method Details
#on_click(event) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/fugit/index_list.rb', line 116 def on_click(event) #~ @staged.deselect(-1) # Clear the other box's selection i = event.get_item return if i == 0 || !self.enabled? if i == @unstaged || i == @staged || @index.get_selections.size != 1 (:diff_clear) else set_diff(*@index.get_item_data(i)) end end |
#on_double_click(event) ⇒ Object
130 131 132 133 134 135 |
# File 'lib/fugit/index_list.rb', line 130 def on_double_click(event) i = event.get_item unless i == @unstaged || i == @staged process_staging(*@index.get_item_data(i)) end end |
#on_menu_request(event) ⇒ Object
160 161 162 163 164 165 166 167 168 |
# File 'lib/fugit/index_list.rb', line 160 def (event) i = event.get_item = nil unless [@root, @staged, @unstaged].include?(i) = @index.get_item_data(i) .enable([1] != :new) @index.([2] == :staged ? : ) end end |
#on_menu_revert_changes(event) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/fugit/index_list.rb', line 174 def (event) @confirm_revert ||= MessageDialog.new(self, "Are you sure you want to revert these changes?\nThe changes will be lost, this cannot be undone.", "Confirm revert", YES_NO|ICON_EXCLAMATION) if @confirm_revert.show_modal == ID_YES diff = `git diff -- "#{@menu_data[0]}"` diff_file = File.join(Dir.pwd, ".git", "fugit_partial.diff") File.open(diff_file, "wb") {|f| f << diff} # Write out in binary mode to preserve newlines, otherwise git freaks out `git apply --reverse .git/fugit_partial.diff` File.delete(diff_file) (:index_changed) end end |
#on_menu_stage_file(event) ⇒ Object
170 171 172 |
# File 'lib/fugit/index_list.rb', line 170 def (event) process_staging(*) if end |
#process_staging(file, change, status) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/fugit/index_list.rb', line 188 def process_staging(file, change, status) case status when :unstaged case change when :deleted `git rm --cached "#{file}" 2>&1` else `git add "#{file}" 2>&1` end when :staged `git reset "#{file}" 2>&1` end (:index_changed) end |
#set_diff(file, change, status) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/fugit/index_list.rb', line 137 def set_diff(file, change, status) case status when :unstaged case change when :new if File.directory?(file) (:diff_raw, "Git repo\n\nNote that staging with fugit will stage the files inside the repo,\nit will *not* add a git-submodule.") else val = File.read(file) (:diff_raw, val) end when :modified, :deleted val = `git diff -- "#{file}"` (:diff_set, val, :unstaged) else (:diff_clear) end when :staged val = `git diff --cached -- "#{file}"` (:diff_set, val, :staged) end end |
#update_tree ⇒ Object
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 |
# File 'lib/fugit/index_list.rb', line 56 def update_tree() self.disable others = `git ls-files --others --exclude-standard` deleted = `git ls-files --deleted` modified = `git ls-files --modified` staged = `git ls-files --stage` last_commit = `git ls-tree -r HEAD` committed = {} last_commit.split("\n").map do |line| (info, file) = line.split("\t") sha = info.match(/[a-f0-9]{40}/)[0] committed[file] = sha end deleted = deleted.split("\n") staged = staged.split("\n").map do |line| (info, file) = line.split("\t") sha = info.match(/[a-f0-9]{40}/)[0] [file, sha] end committed.each_pair do |file, sha| staged << [file, ""] unless staged.assoc(file) end staged.reject! {|file, sha| committed[file] == sha} @index.hide selection = @index.get_selections.map {|i| @index.get_item_data(i)} @index.delete_children(@unstaged) @index.delete_children(@staged) others.split("\n").each {|file| @index.append_item(@unstaged, file, 2, -1, [file, :new, :unstaged])} modified.split("\n").each {|file| @index.append_item(@unstaged, file, 3, -1, [file, :modified, :unstaged]) unless deleted.include?(file)} deleted.each {|file| @index.append_item(@unstaged, file, 4, -1, [file, :deleted, :unstaged])} staged.each {|file, sha| @index.append_item(@staged, file, 5, -1, [file, :modified, :staged])} @index.sort_children(@unstaged) @index.sort_children(@staged) @index.select_item(@unstaged, false) @index.select_item(@staged, false) to_select = [] @index.each {|i| to_select << i if selection.include?(@index.get_item_data(i))} to_select.each {|i| @index.select_item(i)} if to_select.size == 1 set_diff(*@index.get_item_data(to_select[0])) else (:diff_clear) end @index. @index.ensure_visible(to_select.empty? ? @unstaged : to_select[0]) @index.set_scroll_pos(HORIZONTAL, 0) @index.show self.enable self.set_focus unless to_select.size == 1 end |