<commit_before>var Codesearch = require('./codesearch.js');

var REPO = '/home/nelhage/code/linux-2.6/';
var REF  = 'refs/tags/v3.0';

var searcher = new Codesearch(REPO, [REF]);

var clients = {};

function Client(remote) {
  this.remote = remote;
  this.pending_search = null;
  this.last_search = null;
}

Client.prototype.new_search = function (str) {
  if (str === this.last_search)
    return;
  this.pending_search = str;
  if (searcher.readyState == 'ready') {
    this.dispatch_search();
  }
}

Client.prototype.dispatch_search = function() {
  if (this.pending_search !== null) {
    var start = new Date();
    this.last_search = this.pending_search;
    console.log('dispatching: %s...', this.pending_search)
    var search = searcher.search(this.pending_search);
    var remote = this.remote;
    this.pending_search = null;
    search.on('error', function (err) {
                if (remote.error)
                  remote.error(search.search, err)
              }.bind(this));
    search.on('match', function (match) {
                if (remote.match)
                  remote.match(search.search, match);
              });
    search.on('done', function () {
                if (remote.search_done)
                  remote.search_done(search.search, (new Date()) - start);
              });
  }
}


function Server(remote, conn) {
  clients[conn.id] = new Client(remote);
  this.new_search = function(str) {
    clients[conn.id].new_search(str);
  }
  conn.on('end', function() {
            delete clients[conn.id];
          });
}

searcher.on('ready', function () {
              Object.keys(clients).forEach(
                function (id) {
                  clients[id].dispatch_search();
                });
            });

<commit_msg>Use git-rev-parse to resolve the ref to search.<commit_after>var Codesearch = require('./codesearch.js'),
    execFile   = require('child_process').execFile;


var REPO = '/home/nelhage/code/linux-2.6/';
var REF  = 'v3.0';

var searcher = null;

var clients = {};

function Client(remote) {
  this.remote = remote;
  this.pending_search = null;
  this.last_search = null;
}

Client.prototype.new_search = function (str) {
  if (str === this.last_search)
    return;
  this.pending_search = str;
  if (searcher && searcher.readyState == 'ready') {
    this.dispatch_search();
  }
}

Client.prototype.dispatch_search = function() {
  if (this.pending_search !== null) {
    var start = new Date();
    this.last_search = this.pending_search;
    console.log('dispatching: %s...', this.pending_search)
    var search = searcher.search(this.pending_search);
    var remote = this.remote;
    this.pending_search = null;
    search.on('error', function (err) {
                if (remote.error)
                  remote.error(search.search, err)
              }.bind(this));
    search.on('match', function (match) {
                if (remote.match)
                  remote.match(search.search, match);
              });
    search.on('done', function () {
                if (remote.search_done)
                  remote.search_done(search.search, (new Date()) - start);
              });
  }
}

function Server(remote, conn) {
  clients[conn.id] = new Client(remote);
  this.new_search = function(str) {
    clients[conn.id].new_search(str);
  }
  conn.on('end', function() {
            delete clients[conn.id];
          });
}

execFile('git', ['rev-parse', REF], {
           cwd: REPO
         }, function (err, stdout, stderr) {
           if (err) throw err;
           console.log("Searching commit %s (%s)",
                       REF, stdout.trim());
           searcher = new Codesearch(REPO, [stdout.trim()]);
           searcher.on('ready', function () {
                         Object.keys(clients).forEach(
                           function (id) {
                             clients[id].dispatch_search();
                           });
                       });
         });

{'fraction_non_alphanumeric': 0.1486349848331648, 'fraction_numerical': 0.0020222446916076846, 'mean_word_length': 2.0986687548942835, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4578775', 'n_tokens_mistral': 1214, 'n_tokens_neox': 1205, 'n_words': 233}