Sha256: 5695576facf10ecad7bbda9d71300c1f496559a1c4d1fe40b597c9f450959db1

Contents?: true

Size: 1.52 KB

Versions: 33

Compression:

Stored size: 1.52 KB

Contents

module SDoc::GitHub
  def github_url(path)
    unless @github_url_cache.has_key? path
      @github_url_cache[path] = false
      file = RDoc::TopLevel.find_file_named(path)
      if file
        base_url = repository_url(path)
        if base_url
          sha1 = commit_sha1(path)
          if sha1
            relative_url = path_relative_to_repository(path)
            @github_url_cache[path] = "#{base_url}#{sha1}#{relative_url}"
          end
        end
      end
    end
    @github_url_cache[path]
  end
  
  protected
  
  def commit_sha1(path)
    name = File.basename(path)
    s = in_dir(File.join(basedir, File.dirname(path))) do
      `git log -1 --pretty=format:"commit %H" #{name}`
    end
    m = s.match(/commit\s+(\S+)/)
    m ? m[1] : false
  end
  
  def repository_url(path)
    s = in_dir(File.join(basedir, File.dirname(path))) do
      `git config --get remote.origin.url`
    end
    m = s.match(%r{github.com[/:](.*)\.git$})
    m ? "http://github.com/#{m[1]}/blob/" : false
  end

  def path_relative_to_repository(path)
    absolute_path = File.join(basedir, path)
    root = path_to_git_dir(File.dirname(absolute_path))
    absolute_path[root.size..absolute_path.size]
  end

  def path_to_git_dir(path)
    while !path.empty? && path != '.'
      if (File.exists? File.join(path, '.git')) 
        return path
      end
      path = File.dirname(path)
    end
    ''
  end  
  
  def in_dir(dir)
    pwd = Dir.pwd
    Dir.chdir dir
    return yield
  rescue Exception => e
    return ''
  ensure
    Dir.chdir pwd
  end
end

Version data entries

33 entries across 33 versions & 5 rubygems

Version Path
toy-sdoc-0.2.12.1 lib/sdoc/github.rb
toy-sdoc-0.2.13 lib/sdoc/github.rb
toy-sdoc-0.2.14.1 lib/sdoc/github.rb
toy-sdoc-0.2.15 lib/sdoc/github.rb
voloko-sdoc-0.2.1 lib/sdoc/github.rb
voloko-sdoc-0.2.10.1 lib/sdoc/github.rb
voloko-sdoc-0.2.10 lib/sdoc/github.rb
voloko-sdoc-0.2.11.1 lib/sdoc/github.rb
voloko-sdoc-0.2.11 lib/sdoc/github.rb
voloko-sdoc-0.2.12.1 lib/sdoc/github.rb
voloko-sdoc-0.2.12 lib/sdoc/github.rb
voloko-sdoc-0.2.13 lib/sdoc/github.rb
voloko-sdoc-0.2.14.1 lib/sdoc/github.rb
voloko-sdoc-0.2.14 lib/sdoc/github.rb
voloko-sdoc-0.2.2 lib/sdoc/github.rb
voloko-sdoc-0.2.3 lib/sdoc/github.rb
voloko-sdoc-0.2.4 lib/sdoc/github.rb
voloko-sdoc-0.2.5 lib/sdoc/github.rb
voloko-sdoc-0.2.6 lib/sdoc/github.rb
voloko-sdoc-0.2.7 lib/sdoc/github.rb