Module: GitCliPrompt::Commit

Includes:
CliPrompt, Logger, TR::CondUtils
Defined in:
lib/commit.rb

Defined Under Namespace

Classes: CommitError

Instance Method Summary collapse

Methods included from Logger

#logger

Methods included from CliPrompt

#method_missing

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class GitCliPrompt::CliPrompt

Instance Method Details

#commit(root, &block) ⇒ Object

Raises:



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
# File 'lib/commit.rb', line 15

def commit(root,&block)
 
  #logger.debug "Commit operation root : #{root}"
  ws = Gvcs::Workspace.new(root)
  raise CommitError, "Given path '#{File.expand_path(root)}' is not a VCS workspace" if not ws.is_workspace?

  begin

    modDir, modFiles = ws.modified_files
    newDir, newFiles = ws.new_files
    delDir, delFiles = ws.deleted_files
    stDir, stFiles = ws.staged_files

    pending = false
    pending = true if not_empty?(modDir) or not_empty?(modFiles) or not_empty?(newDir) or not_empty?(newFiles) or not_empty?(delDir) or not_empty?(delFiles)

    if not_empty?(stDir) or not_empty?(stFiles)
      say "  Item(s) already staged for commit: \n"
      stDir.each do |md|
        say "  #{md.path}"
      end
      stFiles.each do |md|
        say "  #{md.path}"
      end
    end

    if pending

      say "\n\n  Items could be add to version control:\n"
      cnt = 1
      [modDir, modFiles, newDir, newFiles, delDir, delFiles].each do |cont|
        cont.each do |md|
          say "  #{'%2s' % cnt}. #{md.to_s}\n"
          cnt += 1
        end
      end

    end

    puts

    if not pending
      pmt.say("\n  Workspace is clean. No changes or unstage files or directory is found.\n", color: :green) 
      "Workspace is clean. No changes or unstage files or directory is found"

    else

      ops = pmt.select("  Please select desired operation:") do |menu|
        menu.default 1

        menu.choice "  Add", :add if pending
        if not_empty?(stDir) or not_empty?(stFiles)
          menu.choice "  Remove file from staged", :remove_staged 
          menu.choice "  Commit staged", :commit
        end
        menu.choice "  Ignore changes", :ignore
        menu.choice "  Quit", :quit
      end

      case ops
      when :add
        choices = []
        choices += mark_modified_choice(modDir)
        choices += mark_modified_choice(modFiles)
        choices += mark_new_choice(newDir)
        choices += mark_new_choice(newFiles)
        choices += mark_del_choice(delDir)
        choices += mark_del_choice(delFiles)

        prompt_add_selection(ws, choices, &block)
        commit_staged(ws, &block)

      when :ignore
        pmt.ok "\n  Changes are ignored for this release", color: :yellow

      when :quit
        raise UserChangedMind

      when :remove_staged

        choices = []
        choices += stDir.map { |v| [v.path,v] }
        choices += stFiles.map { |v| [v.path,v] }

        sel = prompt_remove_staging(choices)
        ws.remove_from_staging(sel)

      when :commit

        commit_staged(ws, &block)

      end
    end


  rescue TTY::Reader::InputInterrupt
    ok "\n\n  Aborted"

  #rescue UserChangedMind
  #  ok "\n  Noted. Please retry the process again\n"

  #rescue UserSelectNone
  #  ok "\n  Nothing is selected. Process halted\n"

  #rescue Exception => ex
  #  error(ex.message)
  #  logger.error ex.message
  #  logger.error ex.backtrace.join('\n')

  end
end

#commit_staged(ws, &block) ⇒ Object



191
192
193
194
195
196
# File 'lib/commit.rb', line 191

def commit_staged(ws, &block)
  msg = prompt_commit_message(&block)
  output = ws.commit(msg)
  block.call(:changes_commited, msg, output) if block
  output
end

#confirm_selection(sel) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/commit.rb', line 164

def confirm_selection(sel)
  
  sel = [sel] if not sel.is_a?(Array)
  cs = []
  cnt = 1
  sel.each do |s|
    cs << "#{"%2s" % cnt.to_s}. #{s}"
    cnt += 1
  end

  yes?("  You've selected the following item(s). Proceed?\n#{cs.join("\n")}\n")

end

#prompt_add_selection(ws, choices, &block) ⇒ Object

Raises:



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/commit.rb', line 146

def prompt_add_selection(ws, choices, &block)
  
  sel = multi_select("  Please select which items to add to this commit session:") do |menu|
    choices.each do |c|
      menu.choice c[0], c[1]
    end
  end

  raise UserSelectNone, "  No selection of unversioned item(s) was made" if is_empty?(sel)
  if confirm_selection(sel)
    psel = vcs_items_to_path_array(sel)
    ws.add_to_staging(psel)
  else
    raise UserChangedMind, "  User aborted"
  end

end

#prompt_commit_message(&block) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/commit.rb', line 178

def prompt_commit_message(&block)

  if block
    defMsg = block.call(:default_commit_message)
  end

  msg = pmt.ask("  Commit Message :", required: true) do |m|
    m.default = defMsg if not_empty?(defMsg)
  end

  msg
end

#prompt_remove_staging(choices) ⇒ Object

Raises:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/commit.rb', line 127

def prompt_remove_staging(choices)
  
  sel = pmt.select("  Please select which item(s) to remove from staging:") do |menu|
    
    choices.each do |c|
      menu.choice c[0], c[1]
    end

  end

  raise UserSelectNone, "  No selection of staged item(s) were made" if is_empty?(sel)
  if confirm_selection(sel)
    sel
  else
    raise UserChangedMind, "  User aborted"
  end

end