Sha256: 88352cd0f276d3343af14fa9eb99eea472d46fef2a568a8a65151b1b88b9177b

Contents?: true

Size: 1.56 KB

Versions: 12

Compression:

Stored size: 1.56 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 have_git?
    @have_git = system('git --version > /dev/null 2>&1') if @have_git.nil?
    @have_git
  end

  def commit_sha1(path)
    return false unless have_git?
    name = File.basename(path)
    s = Dir.chdir(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)
    return false unless have_git?
    s = Dir.chdir(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
end

Version data entries

12 entries across 12 versions & 3 rubygems

Version Path
wpdoc-0.2.22 lib/wpdoc/github.rb
sdoc-0.3.4 lib/sdoc/github.rb
sdoc-0.3.3 lib/sdoc/github.rb
sdoc-0.3.2 lib/sdoc/github.rb
sdoc-0.3.1 lib/sdoc/github.rb
sdoc-0.3.0 lib/sdoc/github.rb
morph-sdoc-0.2.21 lib/sdoc/github.rb
wpdoc-0.2.21 lib/wpdoc/github.rb
wpdoc-0.2.20 lib/wpdoc/github.rb
sdoc-0.2.20 lib/sdoc/github.rb
sdoc-0.2.19 lib/sdoc/github.rb
sdoc-0.2.18 lib/sdoc/github.rb