Module: Git::Si::Util

Included in:
SvnInterface
Defined in:
lib/git/si/util.rb

Instance Method Summary collapse

Instance Method Details

#add_all_svn_filesObject



229
230
231
232
233
234
# File 'lib/git/si/util.rb', line 229

def add_all_svn_files
  notice_message "Adding all files present in the svn repository."
  all_svn_files = Git::Si::SvnControl.parse_file_list( get_command_output( Git::Si::SvnControl.list_file_command ) )
  raise Git::Si::GitSiError.new("No files could be found in the svn repository.") if all_svn_files.empty?
  batch_add_files_to_git( all_svn_files )
end

#add_files_after_svn_update(updated_files) ⇒ Object



288
289
290
291
292
293
# File 'lib/git/si/util.rb', line 288

def add_files_after_svn_update( updated_files )
  notice_message "Updating mirror branch to match new data"
  # add updated files
  updated_files = Git::Si::SvnControl.parse_updated_files( updated_files )
  batch_add_files_to_git( updated_files )
end

#add_files_to_git(filenames) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/git/si/util.rb', line 40

def add_files_to_git( filenames )
  filenames.each do |filename|
    begin
      run_command( Git::Si::GitControl.add_command(filename) )
    rescue
      # errors here are not important enough to stop the whole process
    end
  end
end

#are_there_git_changes?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/git/si/util.rb', line 186

def are_there_git_changes?
  Git::Si::GitControl.are_there_changes?( get_command_output( Git::Si::GitControl.status_command ) )
end

#batch_add_files_to_git(filenames) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/git/si/util.rb', line 29

def batch_add_files_to_git( filenames )
  filenames.each_slice(10) do |batch|
    begin
      run_command( Git::Si::GitControl.add_command( batch ) )
    rescue
      # try to batch the files but add them individually if there is an error
      add_files_to_git( batch )
    end
  end
end

#configureObject



50
51
52
53
# File 'lib/git/si/util.rb', line 50

def configure
  Git::Si::SvnControl.svn_binary = options[:svn]
  Git::Si::GitControl.git_binary = options[:git]
end

#create_git_repositoryObject

Raises:



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/git/si/util.rb', line 190

def create_git_repository
  if File.exist? '.git'
    notice_message "Looks like a git repository already exists here."
    return false
  end
  notice_message "Initializing git repository"
  run_command(Git::Si::GitControl.init_command, {:allow_errors => true})
  raise Git::Si::GitError.new("Failed to initialize git repository. I'm not sure why. Check for any errors above.") unless did_last_command_succeed?
  add_all_svn_files()
  true
end

#create_gitignoreObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/git/si/util.rb', line 202

def create_gitignore
  # add externals to gitignore
  gitignore_patterns = Git::Si::GitIgnore.ignore_patterns
  gitignore_patterns += Git::Si::SvnControl.parse_external_repos( get_command_output( Git::Si::SvnControl.status_command ) )

  if not File.exist? '.gitignore'
    notice_message "Creating gitignore file."
    create_file('.gitignore', gitignore_patterns.join( "\n" ))
    run_command( Git::Si::GitControl.add_command('.gitignore') )
    return true
  end

  notice_message "Looks like a gitignore file already exists here."
  missing_patterns = Git::Si::GitIgnore.get_missing_lines_from( File.readlines( '.gitignore' ), gitignore_patterns )
  if not missing_patterns.empty?
    using_stderr do
      say "The .gitignore file is missing the following recommended patterns:\n#{missing_patterns.join( "\n" )}"
      if yes?("Do you want to add them? [Y/n] ", :green)
        append_to_file( '.gitignore', missing_patterns.join("\n") )
        run_command( Git::Si::GitControl.add_command('.gitignore') )
        return true
      end
    end
  end
  false
end

#create_mirror_branchObject



236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/git/si/util.rb', line 236

def create_mirror_branch
  begin
    run_command( Git::Si::GitControl.show_branch_command(get_mirror_branch) )
  rescue
    # no problem. It just means the branch does not exist.
  end
  if did_last_command_succeed?
    notice_message "Looks like the mirror branch already exists here."
  else
    notice_message "Creating mirror branch '#{get_mirror_branch}'."
    run_command( Git::Si::GitControl.create_branch_command(get_mirror_branch) )
  end
end

#debug(message) ⇒ Object



143
144
145
# File 'lib/git/si/util.rb', line 143

def debug(message)
  $stderr.puts message if options[:debug]
end

#delete_committed_branch(local_branch) ⇒ Object



295
296
297
298
299
300
# File 'lib/git/si/util.rb', line 295

def delete_committed_branch( local_branch )
  run_command( Git::Si::GitControl.checkout_command( 'master' ) )
  do_rebase_action
  run_command( Git::Si::GitControl.delete_branch_command( local_branch ) )
  success_message "branch '#{local_branch}' deleted!"
end

#delete_files_after_svn_update(updated_files) ⇒ Object



281
282
283
284
285
286
# File 'lib/git/si/util.rb', line 281

def delete_files_after_svn_update( updated_files )
  # delete deleted files.
  Git::Si::SvnControl.parse_deleted_files( updated_files ).each do |filename|
    run_command( Git::Si::GitControl.delete_command( filename ) )
  end
end

#did_last_command_succeed?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/git/si/util.rb', line 9

def did_last_command_succeed?
  $?.success?
end

#do_revisions_differObject



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

def do_revisions_differ
  last_fetched_version = get_svn_revision()
  last_rebased_version = get_git_si_revision()

  if ! last_fetched_version or ! last_rebased_version
    notice_message "Could not determine last git-si version information. This may be fine if you haven't used git-si before."
    return
  end

  debug "comparing last fetched revision #{last_fetched_version} and last rebased revision #{last_rebased_version}"

  if last_fetched_version > last_rebased_version
    raise Git::Si::VersionError.new("This branch is out-of-date (svn revision #{last_rebased_version}; svn is at #{last_fetched_version}). You should do a git si rebase or git si pull.")
  elsif last_fetched_version < last_rebased_version
    return true if ask("This branch is newer (svn revision #{last_rebased_version}) than svn (rev #{last_fetched_version}). That can happen when svn changes have been made directly and may be fine. Do you want to continue? [Y/n] ", :green) =~ /\s*^n/i
  end
end

#error_message(message) ⇒ Object



139
140
141
# File 'lib/git/si/util.rb', line 139

def error_message(message)
  $stderr.puts set_color message, :red
end

#get_command_output(command, options = {}) ⇒ Object



25
26
27
# File 'lib/git/si/util.rb', line 25

def get_command_output(command, options={})
  run_command(command, options.merge( capture: true ))
end

#get_git_si_revisionObject

Return the most recent svn revision number stored in git



56
57
58
59
# File 'lib/git/si/util.rb', line 56

def get_git_si_revision
  info = get_command_output(Git::Si::GitControl.log_command('--pretty=%B'))
  return Git::Si::GitControl.parse_last_svn_revision(info)
end

#get_local_branchObject

Raises:



74
75
76
77
78
79
# File 'lib/git/si/util.rb', line 74

def get_local_branch
  git_branches = get_command_output(Git::Si::GitControl.branch_command)
  local_branch = Git::Si::GitControl.parse_current_branch(git_branches)
  raise Git::Si::GitError.new("Could not find local branch name.") unless local_branch
  return local_branch
end

#get_mirror_branchObject



100
101
102
# File 'lib/git/si/util.rb', line 100

def get_mirror_branch
  return 'MIRRORBRANCH'
end

#get_svn_revisionObject

Return the most recent svn revision number



62
63
64
65
# File 'lib/git/si/util.rb', line 62

def get_svn_revision
  svn_info = get_command_output(Git::Si::SvnControl.info_command)
  return Git::Si::SvnControl.parse_last_revision(svn_info)
end

#get_svn_rootObject

Raises:



67
68
69
70
71
72
# File 'lib/git/si/util.rb', line 67

def get_svn_root
  svn_info = get_command_output(Git::Si::SvnControl.info_command, {:allow_errors => true})
  root_dir = Git::Si::SvnControl.parse_root_path(svn_info)
  raise Git::Si::SvnError.new("Could not find the svn root directory.") unless root_dir
  root_dir
end

#in_svn_root(&block) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/git/si/util.rb', line 81

def in_svn_root(&block)
  root_dir = get_svn_root
  notice_message "Changing directory to svn root: #{root_dir}"
  Dir.chdir(root_dir) do
    yield
  end
end

#is_file_in_git?(filename) ⇒ Boolean

Returns:

  • (Boolean)


268
269
270
# File 'lib/git/si/util.rb', line 268

def is_file_in_git?( filename )
  not get_command_output( Git::Si::GitControl.list_file_command( filename ) ).empty?
end

#notice_message(message) ⇒ Object



135
136
137
# File 'lib/git/si/util.rb', line 135

def notice_message(message)
  $stderr.puts set_color message, :yellow unless options[:quiet]
end

#on_local_branch(&block) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/git/si/util.rb', line 89

def on_local_branch(&block)
  begin
    in_svn_root do
      yield
    end
  rescue Git::Si::GitSiError => err
    error_message err
    exit false
  end
end

#on_mirror_branch(&block) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/git/si/util.rb', line 104

def on_mirror_branch(&block)
  local_branch = get_local_branch()
  run_command( Git::Si::GitControl.checkout_command(get_mirror_branch) )
  begin
    in_svn_root do
      yield
    end
  rescue Git::Si::GitSiError => err
    error_message err
    exit false
  ensure
    run_command( Git::Si::GitControl.checkout_command(local_branch) )
  end
end


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/git/si/util.rb', line 165

def print_colordiff(diff)
  debug "print_colordiff"
  if ! STDOUT.tty?
    debug "print_colordiff returning without colorizing"
    return say diff
  end
  debug "print_colordiff colorizing..."
  diff.each_line do |line|
    line.rstrip!
    case line
    when /^\+/, /^A/
      line = set_color line, :green
    when /^\-/, /^M/
      line = set_color line, :red
    when /^\?/
      line = set_color line, :yellow
    end
    say line
  end
end

#revert_files_to_svn_update(updated_files) ⇒ Object



272
273
274
275
276
277
278
279
# File 'lib/git/si/util.rb', line 272

def revert_files_to_svn_update( updated_files )
  notice_message "Reverting any local changes in mirror branch"
  # revert everything, but sometimes that doesn't work, so revert conflicts too.
  run_command( Git::Si::SvnControl.revert_command )
  Git::Si::SvnControl.parse_conflicted_files( updated_files ).each do |filename|
    run_command( Git::Si::SvnControl.revert_command( filename ) )
  end
end

#run_command(command, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/git/si/util.rb', line 13

def run_command(command, options={})
  output = ''
  debug "run_command `#{command}`, options: #{options}"
  if STDOUT.tty? and not @silent
    output = run(command, options)
  else
    output = run(command, options.update(verbose: false, capture: true))
  end
  raise Git::Si::ShellError.new("There was an error while trying to run the command: #{command}. Look above for any errors.") if not options[:allow_errors] and not did_last_command_succeed?
  return output
end

#stash_local_changesObject



250
251
252
253
254
255
256
257
258
259
# File 'lib/git/si/util.rb', line 250

def stash_local_changes
  on_local_branch do
    if are_there_git_changes?
      notice_message "Preserving uncommitted changed files"
      run_command( Git::Si::GitControl.stash_command )
      return true
    end
  end
  false
end

#success_message(message) ⇒ Object



131
132
133
# File 'lib/git/si/util.rb', line 131

def success_message(message)
  $stderr.puts set_color message, :green
end

#unstash_local_changes(did_stash_changes) ⇒ Object



261
262
263
264
265
266
# File 'lib/git/si/util.rb', line 261

def unstash_local_changes( did_stash_changes )
  if did_stash_changes
    notice_message "Restoring uncommitted changed files"
    run_command( Git::Si::GitControl.unstash_command )
  end
end

#using_stderr(&block) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/git/si/util.rb', line 119

def using_stderr(&block)
  old_stdout = $stdout
  $stdout = $stderr
  @silent = true
  begin
    yield
  ensure
    $stdout = old_stdout
    @silent = false
  end
end