Skip to content

Commit

Permalink
started the ruby-only command line git client
Browse files Browse the repository at this point in the history
  • Loading branch information
scott Chacon committed Nov 23, 2007
1 parent f1366b3 commit 3fddf30
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
69 changes: 69 additions & 0 deletions bin/gitr
@@ -0,0 +1,69 @@
#!/usr/bin/env ruby

# This is a command line client that can do a number of read operations
# on a git repository in pure ruby. This may be helpful if you have access
# to a computer that has no C compiler but you want to do some git stuff
# on it. It's also helpful for me to test Git stuff with.
#
# author : Scott Chacon (schacon@gmail.com)
#
# todo:
# add --git-dir
# add --log-file
# add --help

#require 'lib/git'
require 'rubygems'
require 'git'
require 'logger'

command = ARGV[0]

if !command
puts 'You have to provide a command'
puts 'usage: gitr (command) [args]'
puts
puts 'commands: log'
puts ' log-shas'
puts ' cat-file'
puts ' rev-parse'
puts ' branches'
puts ' config'
exit
end

git_dir = ENV['GIT_DIR'] || '.git'
@git = Git.bare(git_dir, :log => Logger.new(STDOUT))

case command
when 'log'
# gitr log
@git.log.each do |l|
puts 'commit ' + l.sha
puts l.contents
puts
end
when 'log-shas'
# gitr log-shas
puts @git.log
when 'cat-file'
# gitr cat-file
puts @git.cat_file(ARGV[1])
when 'rev-parse'
# gitr rev-parse
puts @git.revparse(ARGV[1])
when 'branches'
# gitr branches
puts @git.branches
when 'config'
# gitr config
@git.config.sort.each do |k,v|
puts "#{k} : #{v}"
end
end

# gitr ls-tree
# gitr pack-browse

# gitr diff / stats ?
# output in yaml?
4 changes: 4 additions & 0 deletions lib/git/base.rb
Expand Up @@ -429,6 +429,10 @@ def revparse(objectish)
self.lib.revparse(objectish)
end

def cat_file(objectish)
self.lib.object_contents(objectish)
end

# returns the name of the branch the working directory is currently on
def current_branch
self.lib.branch_current
Expand Down
12 changes: 12 additions & 0 deletions lib/git/branches.rb
Expand Up @@ -41,5 +41,17 @@ def [](symbol)
@branches[symbol.to_s]
end

def to_s
out = ''
@branches.each do |k, b|
if b.current
out += "* " + b.to_s + "\n"
else
out += " " + b.to_s + "\n"
end
end
out
end

end
end
5 changes: 3 additions & 2 deletions lib/git/lib.rb
Expand Up @@ -81,7 +81,7 @@ def full_log_commits(opts = {})
sha = revparse(opts[:object] || branch_current || 'master')
count = opts[:count] || 30

repo = Git::Raw::Repository.new(@git_dir)
repo = get_raw_repo
return process_commit_data(repo.log(sha, count))
end

Expand Down Expand Up @@ -182,7 +182,8 @@ def process_commit_data(data, sha = nil)
end

def object_contents(sha)
command('cat-file', ['-p', sha])
#command('cat-file', ['-p', sha])
get_raw_repo.cat_file(revparse(sha))
end

def ls_tree(sha)
Expand Down

0 comments on commit 3fddf30

Please sign in to comment.