From 070381bb456b25f3e867e9b23b78911190c6b369 Mon Sep 17 00:00:00 2001 From: scott Chacon Date: Sun, 11 Nov 2007 11:32:57 -0800 Subject: added remove and reset --- EXAMPLES | 17 +++++-- lib/git/base.rb | 13 ++++++ lib/git/lib.rb | 18 +++++++ lib/git/status.rb | 4 ++ tests/units/test_index_ops.rb | 106 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 tests/units/test_index_ops.rb diff --git a/EXAMPLES b/EXAMPLES index 0105611..22aab6c 100644 --- a/EXAMPLES +++ b/EXAMPLES @@ -79,18 +79,27 @@ g.config('user.email', 'email@email.com') g.add('.') g.add([file1, file2]) + +g.remove('file.txt') +g.remove(['file.txt', 'file2.txt']) g.commit('message') g.commit_all('message') - -***** IMPLEMENTED ***** - -g.remove('file.txt').and_file +g = Git.clone(repo, 'myrepo') +Dir.chdir('myrepo') do + new_file('test-file', 'blahblahblah') + g.status.untracked.each do |file| + puts file.blob(:index).contents + end +end g.reset # defaults to HEAD g.reset_hard(Git::Commit) + +***** IMPLEMENTED ***** + g.branch('new_branch') g.branch('new_branch').delete diff --git a/lib/git/base.rb b/lib/git/base.rb index 9be9fec..46c75eb 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -141,6 +141,19 @@ module Git self.lib.add(path) end + def remove(path = '.', opts = {}) + self.lib.remove(path, opts) + end + + def reset(commitish = nil, opts = {}) + self.lib.reset(commitish, opts) + end + + def reset_hard(commitish = nil, opts = {}) + opts = {:hard => true}.merge(opts) + self.lib.reset(path, opts) + end + def commit(message, opts = {}) self.lib.commit(message, opts) end diff --git a/lib/git/lib.rb b/lib/git/lib.rb index d857253..bc40e0c 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -212,11 +212,29 @@ module Git command('add', path) end + def remove(path = '.', opts = {}) + path = path.join(' ') if path.is_a?(Array) + + arr_opts = ['-f'] # overrides the up-to-date check by default + arr_opts << ['-r'] if opts[:recursive] + arr_opts << path + + command('rm', arr_opts) + end + def commit(message, opts = {}) arr_opts = ["-m '#{message}'"] arr_opts << '-a' if opts[:add_all] command('commit', arr_opts) end + + def reset(commit, opts = {}) + arr_opts = [] + arr_opts << '--hard' if opts[:hard] + arr_opts << commit.to_s if commit + command('reset', arr_opts) + end + private diff --git a/lib/git/status.rb b/lib/git/status.rb index f738644..7dcd284 100644 --- a/lib/git/status.rb +++ b/lib/git/status.rb @@ -18,6 +18,10 @@ module Git def added @files.select { |k, f| f.type == 'A' } end + + def deleted + @files.select { |k, f| f.type == 'D' } + end def untracked @files.select { |k, f| f.untracked } diff --git a/tests/units/test_index_ops.rb b/tests/units/test_index_ops.rb new file mode 100644 index 0000000..b3946b8 --- /dev/null +++ b/tests/units/test_index_ops.rb @@ -0,0 +1,106 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../test_helper' + +class TestIndexOps < Test::Unit::TestCase + + def setup + set_file_paths + @git = Git.open(@wdir) + end + + def test_add + in_temp_dir(false) do |path| + g = Git.clone(@wbare, 'new') + Dir.chdir('new') do + assert_equal('100644', g.status['example.txt'].mode_index) + + new_file('test-file', 'blahblahblah') + assert(g.status.untracked.assoc('test-file')) + + g.add + assert(g.status.added.assoc('test-file')) + assert(!g.status.untracked.assoc('test-file')) + assert(!g.status.changed.assoc('example.txt')) + + append_file('example.txt', 'hahahaha') + assert(g.status.changed.assoc('example.txt')) + + g.add + assert(g.status.changed.assoc('example.txt')) + + g.commit('my message') + assert(!g.status.changed.assoc('example.txt')) + assert(!g.status.added.assoc('test-file')) + assert(!g.status.untracked.assoc('test-file')) + assert_equal('hahahaha', g.status['example.txt'].blob.contents) + end + end + end + + def test_add_array + in_temp_dir do |path| + g = Git.clone(@wbare, 'new') + Dir.chdir('new') do + + new_file('test-file1', 'blahblahblah1') + new_file('test-file2', 'blahblahblah2') + assert(g.status.untracked.assoc('test-file1')) + + g.add(['test-file1', 'test-file2']) + assert(g.status.added.assoc('test-file1')) + assert(g.status.added.assoc('test-file1')) + assert(!g.status.untracked.assoc('test-file1')) + + g.commit('my message') + assert(!g.status.added.assoc('test-file1')) + assert(!g.status.untracked.assoc('test-file1')) + assert_equal('blahblahblah1', g.status['test-file1'].blob.contents) + end + end + end + + def test_remove + in_temp_dir do |path| + g = Git.clone(@wbare, 'remove_test') + Dir.chdir('remove_test') do + assert(g.status['example.txt']) + g.remove('example.txt') + assert(g.status.deleted.assoc('example.txt')) + g.commit('deleted file') + assert(!g.status['example.txt']) + end + end + end + + def test_reset + in_temp_dir do |path| + g = Git.clone(@wbare, 'reset_test') + Dir.chdir('reset_test') do + new_file('test-file1', 'blahblahblah1') + new_file('test-file2', 'blahblahblah2') + assert(g.status.untracked.assoc('test-file1')) + + g.add(['test-file1', 'test-file2']) + assert(!g.status.untracked.assoc('test-file1')) + + g.reset + assert(g.status.untracked.assoc('test-file1')) + assert(!g.status.added.assoc('test-file1')) + end + end + end + + def new_file(name, contents) + File.open(name, 'w') do |f| + f.puts contents + end + end + + def append_file(name, contents) + File.open(name, 'a') do |f| + f.puts contents + end + end + +end \ No newline at end of file -- cgit