Permalink
Browse files

add basic doc db structure and task to fill them from disk

  • Loading branch information...
1 parent 809c700 commit d04f24a68684defd255969655202224a92f198a5 @schacon schacon committed Mar 8, 2012
View
@@ -5,3 +5,4 @@
require File.expand_path('../config/application', __FILE__)
Gitscm::Application.load_tasks
+
View
@@ -0,0 +1,3 @@
+class Doc < ActiveRecord::Base
+ has_many :doc_versions
+end
View
@@ -0,0 +1,3 @@
+class DocFile < ActiveRecord::Base
+ has_many :doc_versions
+end
@@ -0,0 +1,5 @@
+class DocVersion < ActiveRecord::Base
+ belongs_to :doc
+ belongs_to :version
+ belongs_to :doc_file
+end
View
@@ -0,0 +1,6 @@
+class Version < ActiveRecord::Base
+ validates :name, :uniqueness => true
+
+ has_many :doc_versions
+ has_many :docs, :through => :doc_versions
+end
View
@@ -1,58 +1,5 @@
Gitscm::Application.routes.draw do
- # The priority is based upon order of creation:
- # first created -> highest priority.
+ get "site/index"
- # Sample of regular route:
- # match 'products/:id' => 'catalog#view'
- # Keep in mind you can assign values other than :controller and :action
-
- # Sample of named route:
- # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
- # This route can be invoked with purchase_url(:id => product.id)
-
- # Sample resource route (maps HTTP verbs to controller actions automatically):
- # resources :products
-
- # Sample resource route with options:
- # resources :products do
- # member do
- # get 'short'
- # post 'toggle'
- # end
- #
- # collection do
- # get 'sold'
- # end
- # end
-
- # Sample resource route with sub-resources:
- # resources :products do
- # resources :comments, :sales
- # resource :seller
- # end
-
- # Sample resource route with more complex sub-resources
- # resources :products do
- # resources :comments
- # resources :sales do
- # get 'recent', :on => :collection
- # end
- # end
-
- # Sample resource route within a namespace:
- # namespace :admin do
- # # Directs /admin/products/* to Admin::ProductsController
- # # (app/controllers/admin/products_controller.rb)
- # resources :products
- # end
-
- # You can have the root of your site routed with "root"
- # just remember to delete public/index.html.
- # root :to => 'welcome#index'
-
- # See how all your routes lay out with "rake routes"
-
- # This is a legacy wild controller route that's not recommended for RESTful applications.
- # Note: This route will make all actions in every controller accessible via GET requests.
- # match ':controller(/:action(/:id))(.:format)'
+ root :to => 'site#index'
end
@@ -0,0 +1,12 @@
+class CreateVersions < ActiveRecord::Migration
+ def change
+ create_table :versions do |t|
+ t.string :name
+ t.string :commit_sha
+ t.string :tree_sha
+ t.datetime :committed
+ t.timestamps
+ end
+ add_index :versions, [:name]
+ end
+end
@@ -0,0 +1,9 @@
+class CreateDocFiles < ActiveRecord::Migration
+ def change
+ create_table :doc_files do |t|
+ t.string :name
+ t.timestamps
+ end
+ add_index :doc_files, [:name]
+ end
+end
@@ -0,0 +1,11 @@
+class CreateDocs < ActiveRecord::Migration
+ def change
+ create_table :docs do |t|
+ t.text :blob_sha
+ t.text :plain
+ t.text :html
+ t.timestamps
+ end
+ add_index :docs, [:blob_sha]
+ end
+end
@@ -0,0 +1,10 @@
+class CreateDocVersions < ActiveRecord::Migration
+ def change
+ create_table :doc_versions do |t|
+ t.belongs_to :version
+ t.belongs_to :doc
+ t.belongs_to :doc_file
+ t.timestamps
+ end
+ end
+end
View
@@ -10,6 +10,43 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 0) do
+ActiveRecord::Schema.define(:version => 20120308160013) do
+
+ create_table "doc_files", :force => true do |t|
+ t.string "name"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "doc_files", ["name"], :name => "index_doc_files_on_name"
+
+ create_table "doc_versions", :force => true do |t|
+ t.integer "version_id"
+ t.integer "doc_id"
+ t.integer "doc_file_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ create_table "docs", :force => true do |t|
+ t.text "blob_sha"
+ t.text "plain"
+ t.text "html"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "docs", ["blob_sha"], :name => "index_docs_on_blob_sha"
+
+ create_table "versions", :force => true do |t|
+ t.string "name"
+ t.string "commit_sha"
+ t.string "tree_sha"
+ t.datetime "committed"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "versions", ["name"], :name => "index_versions_on_name"
end
View
@@ -0,0 +1,54 @@
+# fill in the db from a local git clone
+task :preindex => :environment do
+ dir = ENV["GIT_REPO"]
+ Dir.chdir(dir) do
+ # find all tags
+ tags = `git tag | grep v1`.strip.split("\n")
+ tags = tags.select { |tag| tag =~ /v\d(\.\d)+$/ } # just get release tags
+
+ # for each tag, get a date and a list of file/shas
+ tags.each do |tag|
+ stag = Version.where(:name => tag).first_or_create
+
+ # extract metadata
+ commit_sha = `git rev-parse #{tag}`.chomp
+ tree_sha = `git rev-parse #{tag}^{tree}`.chomp
+ tagger = `git cat-file commit #{tag} | grep committer`.chomp.split(' ')
+ tz = tagger.pop
+ ts = tagger.pop
+ ts = Time.at(ts.to_i)
+
+ # save metadata
+ puts "#{tag}: #{ts}, #{commit_sha[0, 8]}, #{tree_sha[0, 8]}"
+ stag.commit_sha = commit_sha
+ stag.tree_sha = tree_sha
+ stag.committed = ts
+ stag.save
+
+ # find all the doc entries
+ entries = `git ls-tree #{tag}:Documentation`.strip.split("\n")
+ tree = entries.map do |e|
+ mode, type, sha, path = e.split(' ')
+ [path, sha, type]
+ end
+ tree = tree.select { |t| t.first =~ /.*\.txt/ }
+
+ puts "Found #{tree.size} entries"
+
+ tree.each do |entry|
+ path, sha, type = entry
+ path = path.gsub('.txt', '').gsub('v', '')
+ file = DocFile.where(:name => path).first_or_create
+ doc = Doc.where(:blob_sha => sha).first_or_create
+ if !doc.plain
+ content = `git cat-file blob #{sha}`.chomp
+ doc.plain = content
+ doc.save
+ end
+ DocVersion.where(:version_id => stag.id, :doc_id => doc.id, :doc_file_id => file.id).first_or_create
+ end
+
+ end
+ end
+end
+
Oops, something went wrong.

0 comments on commit d04f24a

Please sign in to comment.