Module: Bringit::Committing

Includes:
Merge
Included in:
Wrapper
Defined in:
lib/bringit/committing.rb,
lib/bringit/committing/merge.rb

Overview

Methods for committing. Use all these methods only mutexed with the git repository as the key.

Defined Under Namespace

Modules: Merge Classes: Error, HeadChangedError, InvalidPathError

Instance Method Summary collapse

Methods included from Merge

#add_merge_data, #conflict_hash, #conflict_on_update, #conflicts, #create_merging_commit, #create_user_commit, #diverged?, #merge, #merge_if_needed, #raise_head_changed_error, #with_temp_user_reference

Instance Method Details

#commit_multichange(options, previous_head_sha = nil) ⇒ Object

Apply multiple file changes to the repository

options should contain the following structure:

files: {
  [{content: 'Lorem ipsum...',
    path: 'documents/story.txt',
    action: :create},
   {content: 'New Lorem ipsum...',
    path: 'documents/old_story',
    action: :update},
   {content: 'New Lorem ipsum...',
    previous_path: 'documents/really_old_story.txt',
    path: 'documents/old_story',
    action: :rename_and_update},
   {path: 'documents/obsolet_story.txt',
    action: :remove},
   {path: 'documents/old_story',
    previus_path: 'documents/really_old_story.txt',
    action: :rename},
   {path: 'documents/secret',
    action: :mkdir}
  ]
  }
},
author: {
  email: '[email protected]',
  name: 'Test User',
  time: Time.now    # optional - default: Time.now
},
committer: {
  email: '[email protected]',
  name: 'Test User',
  time: Time.now    # optional - default: Time.now
},
commit: {
  message: 'Wow such commit',
  branch: 'master',    # optional - default: 'master'
  update_ref: false    # optional - default: true
}


218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/bringit/committing.rb', line 218

def commit_multichange(options, previous_head_sha = nil)
  commit_with(options, previous_head_sha) do |index|
    options[:files].each do |file|
      file_options = {}
      file_options[:file_path] = file[:path] if file[:path]
      file_options[:content] = file[:content] if file[:content]
      file_options[:encoding] = file[:encoding] if file[:encoding]
      case file[:action]
      when :create
        index.create(file_options)
      when :rename
        file_options[:previous_path] = file[:previous_path]
        file_options[:content] ||=
          blob(options[:commit][:branch], file[:previous_path]).data
        index.move(file_options)
      when :update
        index.update(file_options)
      when :rename_and_update
        previous_path = file[:previous_path]
        file_options[:previous_path] = previous_path
        index.move(file_options)
      when :remove
        index.delete(file_options)
      when :mkdir
        index.create_dir(file_options)
      end
    end
  end
end

#create_file(options, previous_head_sha = nil) ⇒ Object

Create a file in repository and return commit sha

options should contain the following structure:

file: {
  content: 'Lorem ipsum...',
  path: 'documents/story.txt'
},
author: {
  email: '[email protected]',
  name: 'Test User',
  time: Time.now    # optional - default: Time.now
},
committer: {
  email: '[email protected]',
  name: 'Test User',
  time: Time.now    # optional - default: Time.now
},
commit: {
  message: 'Wow such commit',
  branch: 'master',    # optional - default: 'master'
  update_ref: false    # optional - default: true
}


47
48
49
# File 'lib/bringit/committing.rb', line 47

def create_file(options, previous_head_sha = nil)
  commit_multichange(convert_options(options, :create), previous_head_sha)
end

#mkdir(path, options, previous_head_sha = nil) ⇒ Object

Create a new directory with a .gitkeep file. Creates all required nested directories (i.e. mkdir -p behavior)

options should contain the following structure:

author: {
  email: '[email protected]',
  name: 'Test User',
  time: Time.now    # optional - default: Time.now
},
committer: {
  email: '[email protected]',
  name: 'Test User',
  time: Time.now    # optional - default: Time.now
},
commit: {
  message: 'Wow such commit',
  branch: 'master',    # optional - default: 'master'
  update_ref: false    # optional - default: true
}


174
175
176
177
# File 'lib/bringit/committing.rb', line 174

def mkdir(path, options, previous_head_sha = nil)
  options[:file] = {path: path}
  commit_multichange(convert_options(options, :mkdir), previous_head_sha)
end

#remove_file(options, previous_head_sha = nil) ⇒ Object

Remove file from repository and return commit sha

options should contain the following structure:

file: {
  path: 'documents/story.txt'
},
author: {
  email: '[email protected]',
  name: 'Test User',
  time: Time.now    # optional - default: Time.now
},
committer: {
  email: '[email protected]',
  name: 'Test User',
  time: Time.now    # optional - default: Time.now
},
commit: {
  message: 'Remove FILENAME',
  branch: 'master'    # optional - default: 'master'
}


124
125
126
# File 'lib/bringit/committing.rb', line 124

def remove_file(options, previous_head_sha = nil)
  commit_multichange(convert_options(options, :remove), previous_head_sha)
end

#rename_and_update_file(options, previous_head_sha = nil) ⇒ Object

Change contents and path of a file in repository and return commit sha

options should contain the following structure:

file: {
  content: 'Lorem ipsum...',
  path: 'documents/story.txt',
  previous_path: 'documents/old_story.txt'
},
author: {
  email: '[email protected]',
  name: 'Test User',
  time: Time.now    # optional - default: Time.now
},
committer: {
  email: '[email protected]',
  name: 'Test User',
  time: Time.now    # optional - default: Time.now
},
commit: {
  message: 'Wow such commit',
  branch: 'master',    # optional - default: 'master'
  update_ref: false    # optional - default: true
}


100
101
102
# File 'lib/bringit/committing.rb', line 100

def rename_and_update_file(options, previous_head_sha = nil)
  commit_multichange(convert_options(options, :rename_and_update), previous_head_sha)
end

#rename_file(options, previous_head_sha = nil) ⇒ Object

Rename file from repository and return commit sha This does not change the file content.

options should contain the following structure:

file: {
  previous_path: 'documents/old_story.txt'
  path: 'documents/story.txt'
},
author: {
  email: '[email protected]',
  name: 'Test User',
  time: Time.now    # optional - default: Time.now
},
committer: {
  email: '[email protected]',
  name: 'Test User',
  time: Time.now    # optional - default: Time.now
},
commit: {
  message: 'Rename FILENAME',
  branch: 'master'    # optional - default: 'master'
}


151
152
153
# File 'lib/bringit/committing.rb', line 151

def rename_file(options, previous_head_sha = nil)
  commit_multichange(convert_options(options, :rename), previous_head_sha)
end

#update_file(options, previous_head_sha = nil) ⇒ Object

Change the contents of a file in repository and return commit sha

options should contain the following structure:

file: {
  content: 'Lorem ipsum...',
  path: 'documents/story.txt'
},
author: {
  email: '[email protected]',
  name: 'Test User',
  time: Time.now    # optional - default: Time.now
},
committer: {
  email: '[email protected]',
  name: 'Test User',
  time: Time.now    # optional - default: Time.now
},
commit: {
  message: 'Wow such commit',
  branch: 'master',    # optional - default: 'master'
  update_ref: false    # optional - default: true
}


73
74
75
# File 'lib/bringit/committing.rb', line 73

def update_file(options, previous_head_sha = nil)
  commit_multichange(convert_options(options, :update), previous_head_sha)
end