Skip to content

Commit 3fddf30

Browse files
author
scott Chacon
committed
started the ruby-only command line git client
1 parent f1366b3 commit 3fddf30

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

bin/gitr

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env ruby
2+
3+
# This is a command line client that can do a number of read operations
4+
# on a git repository in pure ruby. This may be helpful if you have access
5+
# to a computer that has no C compiler but you want to do some git stuff
6+
# on it. It's also helpful for me to test Git stuff with.
7+
#
8+
# author : Scott Chacon (schacon@gmail.com)
9+
#
10+
# todo:
11+
# add --git-dir
12+
# add --log-file
13+
# add --help
14+
15+
#require 'lib/git'
16+
require 'rubygems'
17+
require 'git'
18+
require 'logger'
19+
20+
command = ARGV[0]
21+
22+
if !command
23+
puts 'You have to provide a command'
24+
puts 'usage: gitr (command) [args]'
25+
puts
26+
puts 'commands: log'
27+
puts ' log-shas'
28+
puts ' cat-file'
29+
puts ' rev-parse'
30+
puts ' branches'
31+
puts ' config'
32+
exit
33+
end
34+
35+
git_dir = ENV['GIT_DIR'] || '.git'
36+
@git = Git.bare(git_dir, :log => Logger.new(STDOUT))
37+
38+
case command
39+
when 'log'
40+
# gitr log
41+
@git.log.each do |l|
42+
puts 'commit ' + l.sha
43+
puts l.contents
44+
puts
45+
end
46+
when 'log-shas'
47+
# gitr log-shas
48+
puts @git.log
49+
when 'cat-file'
50+
# gitr cat-file
51+
puts @git.cat_file(ARGV[1])
52+
when 'rev-parse'
53+
# gitr rev-parse
54+
puts @git.revparse(ARGV[1])
55+
when 'branches'
56+
# gitr branches
57+
puts @git.branches
58+
when 'config'
59+
# gitr config
60+
@git.config.sort.each do |k,v|
61+
puts "#{k} : #{v}"
62+
end
63+
end
64+
65+
# gitr ls-tree
66+
# gitr pack-browse
67+
68+
# gitr diff / stats ?
69+
# output in yaml?

lib/git/base.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,10 @@ def revparse(objectish)
429429
self.lib.revparse(objectish)
430430
end
431431

432+
def cat_file(objectish)
433+
self.lib.object_contents(objectish)
434+
end
435+
432436
# returns the name of the branch the working directory is currently on
433437
def current_branch
434438
self.lib.branch_current

lib/git/branches.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,17 @@ def [](symbol)
4141
@branches[symbol.to_s]
4242
end
4343

44+
def to_s
45+
out = ''
46+
@branches.each do |k, b|
47+
if b.current
48+
out += "* " + b.to_s + "\n"
49+
else
50+
out += " " + b.to_s + "\n"
51+
end
52+
end
53+
out
54+
end
55+
4456
end
4557
end

lib/git/lib.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def full_log_commits(opts = {})
8181
sha = revparse(opts[:object] || branch_current || 'master')
8282
count = opts[:count] || 30
8383

84-
repo = Git::Raw::Repository.new(@git_dir)
84+
repo = get_raw_repo
8585
return process_commit_data(repo.log(sha, count))
8686
end
8787

@@ -182,7 +182,8 @@ def process_commit_data(data, sha = nil)
182182
end
183183

184184
def object_contents(sha)
185-
command('cat-file', ['-p', sha])
185+
#command('cat-file', ['-p', sha])
186+
get_raw_repo.cat_file(revparse(sha))
186187
end
187188

188189
def ls_tree(sha)

0 commit comments

Comments
 (0)