From 907b03949bab53e2f7d55094100a71abd22f23e7 Mon Sep 17 00:00:00 2001 From: scott Chacon Date: Sun, 11 Nov 2007 16:01:23 -0800 Subject: added tagging --- lib/git/base.rb | 44 ++++++++++++++++++++++++++++++++++++++++++++ lib/git/branch.rb | 2 +- lib/git/lib.rb | 42 ++++++++++++++++++++++++++++++++++++++++-- lib/git/object.rb | 34 +++++++++++++++++++++++++++++----- lib/git/remote.rb | 25 +++++++++++++++++++++++-- 5 files changed, 137 insertions(+), 10 deletions(-) (limited to 'lib/git') diff --git a/lib/git/base.rb b/lib/git/base.rb index 0b837cb..ffd8a2b 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -84,6 +84,11 @@ module Git @index end + def chdir + Dir.chdir(dir.path) do + yield dir.path + end + end #g.config('user.name', 'Scott Chacon') # sets value #g.config('user.email', 'email@email.com') # sets value @@ -128,6 +133,10 @@ module Git Git::Branch.new(self, branch_name) end + def remote(remote_name = 'origin') + Git::Remote.new(self, remote_name) + end + def lib Git::Lib.new(self) @@ -172,11 +181,46 @@ module Git self.lib.checkout(branch, opts) end + def fetch(remote = 'origin') + self.lib.fetch(remote) + end + def merge(branch, message = 'merge') self.lib.merge(branch, message) end + def pull(remote = 'origin', branch = 'master', message = 'origin pull') + fetch(remote) + merge(branch, message) + end + + def remotes + self.lib.remotes.map { |r| Git::Remote.new(self, r) } + end + + def add_remote(name, url, opts = {}) + if url.is_a?(Git::Base) + url = url.repo.path + end + self.lib.remote_add(name, url, opts) + Git::Remote.new(self, name) + end + + def tags + self.lib.tags.map { |r| tag(r) } + end + + def tag(tag_name) + Git::Object.new(self, tag_name, true) + end + + def add_tag(tag_name) + self.lib.tag(tag_name) + tag(tag_name) + end + # convenience methods + def revparse(objectish) self.lib.revparse(objectish) diff --git a/lib/git/branch.rb b/lib/git/branch.rb index 1d4b64c..4b4ecfc 100644 --- a/lib/git/branch.rb +++ b/lib/git/branch.rb @@ -73,7 +73,7 @@ module Git end def to_s - @name + @full end private diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 9b3922e..93490db 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -264,6 +264,43 @@ module Git command('merge', arr_opts) end + def remote_add(name, url, opts = {}) + arr_opts = ['add'] + arr_opts << '-f' if opts[:with_fetch] + arr_opts << name + arr_opts << url + + command('remote', arr_opts) + end + + # this is documented as such, but seems broken for some reason + # i'll try to get around it some other way later + def remote_remove(name) + command('remote', ['rm', name]) + end + + def remotes + command_lines('remote') + end + + def tags + command_lines('tag') + end + + def tag(tag) + command('tag', tag) + end + + + def fetch(remote) + command('fetch', remote.to_s) + end + + def tag_sha(tag_name) + command('show-ref', ['--tags', '-s', tag_name]) + end + + private @@ -278,19 +315,20 @@ module Git path = @git_work_dir || @git_dir || @path Dir.chdir(path) do opts = opts.to_a.join(' ') + git_cmd = "git #{cmd} #{opts}" out = `git #{cmd} #{opts} 2>&1`.chomp #puts path #puts "gd: #{@git_work_dir}" #puts "gi: #{@git_index_file}" #puts "pp: #{@path}" - #puts "git #{cmd} #{opts}" + #puts git_cmd #puts out #puts if $?.exitstatus > 0 if $?.exitstatus == 1 && out == '' return '' end - raise Git::GitExecuteError.new(out) + raise Git::GitExecuteError.new(git_cmd + out.to_s) end out end diff --git a/lib/git/object.rb b/lib/git/object.rb index f100597..a7d7c17 100644 --- a/lib/git/object.rb +++ b/lib/git/object.rb @@ -1,4 +1,8 @@ module Git + + class GitTagNameDoesNotExist< StandardError + end + class Object class AbstractObject @@ -64,14 +68,34 @@ module Git end end - + class Tag < AbstractObject + attr_accessor :name + + def initialize(base, sha, name) + super(base, sha) + @name = name + end + + def setup + @type = 'tag' + end + end + class << self # if we're calling this, we don't know what type it is yet # so this is our little factory method - def new(base, objectish) - sha = base.lib.revparse(objectish) - type = base.lib.object_type(sha) - + def new(base, objectish, is_tag = false) + if is_tag + sha = base.lib.tag_sha(objectish) + if sha == '' + raise Git::GitTagNameDoesNotExist.new(objectish) + end + return Tag.new(base, sha, objectish) + else + sha = base.lib.revparse(objectish) + type = base.lib.object_type(sha) + end + klass = case type when /blob/: Blob diff --git a/lib/git/remote.rb b/lib/git/remote.rb index 6956d47..e72a7f6 100644 --- a/lib/git/remote.rb +++ b/lib/git/remote.rb @@ -1,7 +1,7 @@ module Git class Remote < Path - attr_accessor :name, :url, :fetch + attr_accessor :name, :url, :fetch_opts @base = nil @@ -10,7 +10,28 @@ module Git config = @base.lib.config_remote(name) @name = name @url = config['url'] - @fetch = config['fetch'] + @fetch_opts = config['fetch'] + end + + def remove + @base.remote_remove(@name) + end + + def fetch + @base.fetch(@name) + end + + # merge this remote locally + def merge(branch = 'master') + @base.merge("#{@name}/#{branch}") + end + + def branch(branch = 'master') + Git::Branch.new(@base, "#{@name}/#{branch}") + end + + def remove + @base.lib.remote_remove(@name) end def to_s -- cgit