Skip to content

Commit

Permalink
Fixes some issues in hound-search#76.
Browse files Browse the repository at this point in the history
1. Changes mini-templating to use {name} instead of ${name}.
2. Merges url-pattern into a single json property.
3. Makes excluded files page respect url pattern.
4. Introduces common.js file to share between pages.
  • Loading branch information
Kelly Norton committed Mar 2, 2015
1 parent 1cdf48e commit f643617
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 52 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ hound.sublime-workspace
data
.vagrant
pub/index.html
pub/excluded_files.html
config.json
28 changes: 28 additions & 0 deletions pub/assets/js/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// TODO(knorton): Use something to bundle this more intelligently and get this
// out of the global scope.

var ExpandVars = function(template, values) {
for (var name in values) {
template = template.replace('{' + name + '}', values[name]);
}
return template;
};

var UrlToRepo = function(repo, path, line) {
var url = repo.url.replace(/\.git$/, ''),
pattern = repo['url-pattern'],
anchor = line ? ExpandVars(pattern.anchor, { line : line }) : '';

// Hacky solution to fix _some more_ of the 404's when using SSH style URLs
var sshParts = /git@(.*):(.*)/i.exec(url);
if (sshParts) {
url = '//' + sshParts[1] + '/' + sshParts[2];
}

// I'm sure there is a nicer React/jsx way to do this:
return ExpandVars(pattern['base-url'], {
url : url,
path: path,
anchor: anchor
});
};
26 changes: 9 additions & 17 deletions pub/assets/js/excluded_files.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

var ExcludedRow = React.createClass({
render: function() {
var url = UrlToRepo(this.props.repo, this.props.file.Filename);
return (
<tr>
<td className="name">
<a href={this.props.baseUrl + this.props.file.Filename}>{this.props.file.Filename}</a>
<a href={url}>{this.props.file.Filename}</a>
</td>
<td className="reason">{this.props.file.Reason}</td>
</tr>
Expand All @@ -22,7 +23,7 @@ var ExcludedTable = React.createClass({

var rows = [];
this.props.files.forEach(function(file) {
rows.push(<ExcludedRow file={file} baseUrl={_this.props.baseUrl} />);
rows.push(<ExcludedRow file={file} repo={_this.props.repo} />);
});

return (
Expand Down Expand Up @@ -62,7 +63,7 @@ var RepoList = React.createClass({
var repos = [],
_this = this;
this.props.repos.forEach(function(repo){
repos.push(<RepoButton repo={repo} onRepoClick={_this.props.onRepoClick} currentRepo={_this.props.currentRepo} />);
repos.push(<RepoButton repo={repo} onRepoClick={_this.props.onRepoClick} currentRepo={_this.props.repo} />);
});

return (
Expand Down Expand Up @@ -91,24 +92,15 @@ var FilterableExcludedFiles = React.createClass({
return {
files: [],
repos: [],
currentRepo: '',
currentRepoUrl: ''
repo: null,
};
},

getUrlToRepo: function(repo) {
var info = this.state.repos[repo],
url = info.url.replace(/\.git$/, '');

return url + '/blob/master/';
},

onRepoClick: function(repo) {
var _this = this;
_this.setState({
searching: true,
currentRepo: repo,
currentRepoUrl: _this.getUrlToRepo(repo)
searching: true,
repo: this.state.repos[repo],
});
$.ajax({
url: '/api/v1/excludes',
Expand All @@ -132,8 +124,8 @@ var FilterableExcludedFiles = React.createClass({
<h1>Excluded Files</h1>

<div id="excluded_files" className="table-container">
<RepoList repos={Object.keys(this.state.repos)} onRepoClick={this.onRepoClick} currentRepo={this.state.currentRepo} />
<ExcludedTable files={this.state.files} searching={this.state.searching} baseUrl={this.state.currentRepoUrl} />
<RepoList repos={Object.keys(this.state.repos)} onRepoClick={this.onRepoClick} repo={this.state.repo} />
<ExcludedTable files={this.state.files} searching={this.state.searching} repo={this.state.repo} />
</div>
</div>
);
Expand Down
13 changes: 1 addition & 12 deletions pub/assets/js/hound.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,7 @@ var Model = {
},

UrlToRepo: function(repo, path, line) {
var info = this.repos[repo],
url = info.url.replace(/\.git$/, ''),
anc = line ? info.anchorpattern.replace('${line}', line) : '';

// Hacky solution to fix _some more_ of the 404's when using SSH style URLs
var sshParts = /git@(.*):(.*)/i.exec(url);
if (sshParts) {
url = '//' + sshParts[1] + '/' + sshParts[2];
}

// I'm sure there is a nicer React/jsx way to do this:
return info.urlpattern.replace('${url}', url).replace('${path}', path).replace('${anc}', anc);
return UrlToRepo(this.repos[repo], path, line);
}

};
Expand Down
10 changes: 6 additions & 4 deletions src/hound/cmds/houndd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"hound/api"
Expand All @@ -13,7 +14,6 @@ import (
"log"
"net/http"
"os"
"errors"
"os/exec"
"path"
"path/filepath"
Expand Down Expand Up @@ -186,7 +186,6 @@ func makeSearchers(

var s *searcher.Searcher


if useStaleIndex {
s, err = searcher.NewFromExisting(path, repo)
} else {
Expand Down Expand Up @@ -236,14 +235,17 @@ func runHttp(addr, root string, prod bool, cfg *config.Config, idx map[string]*s
template: "index.tpl.html",
dest: "index.html",
sources: []string{
"assets/js/common.js",
"assets/js/hound.js",
},
},
&content{
template: "excluded_files.tpl.html",
dest: "excluded_files.html",
sources: []string{
"assets/js/excluded_files.js"},
"assets/js/common.js",
"assets/js/excluded_files.js",
},
},
}

Expand Down Expand Up @@ -316,7 +318,7 @@ func main() {
}

formattedAddress := *flagAddr
if (0 == strings.Index(*flagAddr, ":")) {
if 0 == strings.Index(*flagAddr, ":") {
formattedAddress = "localhost" + *flagAddr
}
info_log.Printf("running server at http://%s...\n", formattedAddress)
Expand Down
57 changes: 38 additions & 19 deletions src/hound/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,53 @@ import (
const (
defaultMsBetweenPoll = 30000
defaultVcs = "git"
defaultUrlPattern = "${url}/blob/master/${path}${anc}"
defaultAnchorPattern = "#L${line}"
defaultBaseUrl = "{url}/blob/master/{path}{anchor}"
defaultAnchor = "#L{line}"
)

type UrlPattern struct {
BaseUrl string `json:"base-url"`
Anchor string `json:"anchor"`
}

type Repo struct {
Url string `json:"url"`
MsBetweenPolls int `json:"ms-between-poll"`
Vcs string `json:"vcs"`
UrlPattern string `json:"urlpattern"`
AnchorPattern string `json:"anchorpattern"`
Url string `json:"url"`
MsBetweenPolls int `json:"ms-between-poll"`
Vcs string `json:"vcs"`
UrlPattern *UrlPattern `json:"url-pattern"`
}

type Config struct {
DbPath string `json:"dbpath"`
Repos map[string]*Repo `json:"repos"`
}

// Populate missing config values with default values.
func initRepo(r *Repo) {
if r.MsBetweenPolls == 0 {
r.MsBetweenPolls = defaultMsBetweenPoll
}

if r.Vcs == "" {
r.Vcs = defaultVcs
}

if r.UrlPattern == nil {
r.UrlPattern = &UrlPattern{
BaseUrl: defaultBaseUrl,
Anchor: defaultAnchor,
}
} else {
if r.UrlPattern.BaseUrl == "" {
r.UrlPattern.BaseUrl = defaultBaseUrl
}

if r.UrlPattern.Anchor == "" {
r.UrlPattern.Anchor = defaultAnchor
}
}
}

func (c *Config) LoadFromFile(filename string) error {
r, err := os.Open(filename)
if err != nil {
Expand All @@ -47,18 +77,7 @@ func (c *Config) LoadFromFile(filename string) error {
}

for _, repo := range c.Repos {
if repo.MsBetweenPolls == 0 {
repo.MsBetweenPolls = defaultMsBetweenPoll
}
if repo.Vcs == "" {
repo.Vcs = defaultVcs
}
if repo.UrlPattern == "" {
repo.UrlPattern = defaultUrlPattern
}
if repo.AnchorPattern == "" {
repo.AnchorPattern = defaultAnchorPattern
}
initRepo(repo)
}

return nil
Expand Down

0 comments on commit f643617

Please sign in to comment.