public
Description: probably a waste of time
Clone URL: git://github.com/claude/git_local.git
git_local / git_local.rb
100644 147 lines (126 sloc) 3.198 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!ruby
 
# Required Gems
%w(rubygems syntaxi grit sinatra yaml haml).each {|d| require d}
 
Syntaxi::wrap_at_column = 120
layout 'layout.haml'
get "/" do
  load_config
  load_repos
  haml :index
end
 
get "/history/:name" do
  @repo = get_repo(params[:name])
  haml :history
end
 
# TODO: need to figure out how to traverse commits
get "/history/:name/:id" do
  repo = get_repo(params[:name])
  commit = repo.commit(params[:id])
  diff_text = "[code lang=\"ruby\"]" + repo.diff(commit.parents[0], commit) + "[/code]"
  @formatted_text = Syntaxi.new(diff_text).process
  haml :diff
end
 
get "/tree/:name" do
  @tree = get_repo(params[:name]).tree
  haml :tree
end
 
get "/tree/:name/*" do
  path = request.path_info.gsub("/tree/#{params[:name]}/", '')
  if path.split('/').length >= 2
    new_tree = path.split('/').pop
    @tree = get_repo(params[:name]).tree(new_tree)
    @blob = get_repo(params[:name]).blob(new_tree)
    file_text = "[code lang=\"ruby\"]" + @blob.data + "[/code]"
    @formatted_text = Syntaxi.new(file_text).process
  else
    @tree = get_repo(params[:name]).tree
  end
   
  haml :tree
end
 
post "/create_repo_path" do
  create_repositories_path(params[:path])
  redirect '/'
end
 
post "/add_repo" do
  load_repos
  set_index
  load_config
  path = @config['path'] + '/' + underscorify(params[:name]) + '.git'
  @repo_to_create = { @repo => {'path' => path, 'name' => params[:name], 'formatted_name' => underscorify(params[:name])}}
  yaml_to_write
  File.open('config/repos.yml', 'w') do |w|
    YAML.dump(yaml_to_write, w)
  end
  init_new_git(path)
  redirect '/'
end
 
get '/stylesheet.css' do
  header 'Content-Type' => 'text/css; charset=utf-8'
  sass :stylesheet
end
 
helpers do
 
  def init_new_git(path)
    Grit::Repo.init_bare(path)
  end
 
  def yaml_to_write
    if @all_repos == false
      @repo_to_create
    else
      @all_repos.merge(@repo_to_create)
    end
  end
 
  # TODO: refactor above and below methods
  def config_to_write
    if @config == false
      @config_to_create
    else
      @config.merge(@config_to_create)
    end
  end
 
  def load_config
    if File.exists?("config/config.yml")
      @config = YAML::load(open("config/config.yml"))
    else
      @config = {}
    end
  end
 
  def create_repositories_path(path)
    load_config
    Dir.mkdir(path)
    @config_to_create = { 'path' => path }
    config_to_write
    File.open('config/config.yml', 'w') do |w|
      YAML.dump(config_to_write, w)
    end
  end
 
  def set_index
    if @all_repos == false
      @repo = 'repository_1'
    else
      @repo = 'repository_' + (@all_repos.size + 1).to_s
    end
  end
 
  def get_repo(name)
    load_repos
    @all_repos.each do |key,value|
      if name == value['formatted_name']
        @path = value['path']
      end
    end
    Grit::Repo.new(@path)
  end
 
  def load_repos
    if File.exists?("config/repos.yml")
      @all_repos = YAML::load(open("config/repos.yml"))
    else
      @all_repos = {}
    end
  end
 
  def all_repos_path
    config = YAML::load(open("config/config.yml"))
    config.each {|key,value| return value.to_s unless value.nil? }
  end
 
  def underscorify(params)
    params.gsub(/[^a-z0-9]+/i, '_').downcase
  end
end