Skip to content

Commit 545ccf7

Browse files
committed
Renamed JsGit.Repo to JsGit.MemoryRepo
1 parent bce5107 commit 545ccf7

File tree

7 files changed

+118
-114
lines changed

7 files changed

+118
-114
lines changed

lib/git.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -4007,6 +4007,10 @@ JsGit.Repo = function() {
40074007
remotes[name] = new JsGit[JsGit.REMOTE_TYPE](this, name, url)
40084008
}
40094009

4010+
that.addRemoteObject = function(name, remote) {
4011+
remotes[name] = remote
4012+
}
4013+
40104014
that.makeAndAddObject = function(sha, type, content) {
40114015
objects[sha] = JsGit.objects.make(sha, type, content)
40124016
return objects[sha]
@@ -4056,9 +4060,11 @@ JsGit.GithubProxyRepo = function(username, reponame, password) {
40564060
return repo
40574061
}
40584062

4059-
4060-
4061-
4063+
JsGit.LocalRepo = function(dir) {
4064+
var repo = new JsGit.Repo()
4065+
repo.addRemoteObject("local", new JsGit.LocalRemote())
4066+
return repo
4067+
}
40624068

40634069

40644070

@@ -4649,6 +4655,7 @@ JsGit.objects = {
46494655

46504656

46514657

4658+
46524659
JsGit.Diff = function(file1, file2, options) {
46534660
this.lines1 = file1.split("\n")
46544661
this.lines2 = file2.split("\n")

lib/jsgit.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11

2+
/* Main object */
23
JsGit = {
34
OBJECT_TYPES: ["tag", "commit", "tree", "blob"],
45
REMOTE_TYPE: "HttpRemote",
56

7+
// Print an error either to the console if in node, or to div#jsgit-errors
8+
// if in the client.
69
handleError: function(message) {
710
if (jsGitInNode) {
811
console.log(message)
@@ -12,6 +15,7 @@ JsGit = {
1215
}
1316
},
1417

18+
// Turn an array of bytes into a String
1519
bytesToString: function(bytes) {
1620
var result = "";
1721
var i;

lib/jsgit/cli.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ JsGit.Cli = function(args) {
33
this.args = args
44
this.run = function() {
55
console.log("current dir: " + process.cwd())
6-
var repo = new JsGit.LocalRepo()
6+
var repo = new JsGit.Repo()
77
console.log(repo)
88
}
99
}

lib/jsgit/repo.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

2-
JsGit.Repo = function() {
2+
JsGit.MemoryRepo = function(dir) {
33
var refs = {}
44
var that = {}
55
var remotes = {}
66
var objects = {}
7-
7+
88
that.getRef = function(refname) {
99
return refs[refname]
1010
}
@@ -95,21 +95,13 @@ JsGit.Repo = function() {
9595

9696
// Helper for creating Repos for a Github proxy
9797
JsGit.GithubProxyRepo = function(username, reponame, password) {
98-
var repo = new JsGit.Repo()
98+
var repo = new JsGit.MemoryRepo()
9999
var githubCredentials = "password=" + encodeURI(password) + "&username=" + username
100100
var remoteUrl = "http://localhost:3000/" + username + "/" + reponame + ".git?server=" + encodeURI("https://github.com") + "&" + githubCredentials
101101
repo.addRemote("origin", remoteUrl)
102102
return repo
103103
}
104104

105-
JsGit.LocalRepo = function(dir) {
106-
var repo = new JsGit.Repo()
107-
repo.addRemoteObject("local", new JsGit.LocalRemote())
108-
return repo
109-
}
110-
111-
112-
113105

114106

115107

lib/repo-viewer/repo-viewer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ RepoViewer = {
388388
RepoViewer.clearRefs()
389389
RepoViewer.clearCommits()
390390
RepoViewer.clearErrors()
391-
var repo = new JsGit.Repo()
391+
var repo = new JsGit.MemoryRepo()
392392
RepoViewer.repo = repo
393393
console.log("creating repo with origin: " + uri)
394394
// if (uri.indexOf("//github.com")) {

proxy/config.ru

+93-92
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,82 @@
11
require 'pp'
22
require "net/https"
33

4-
class GithubForwarder
5-
def initialize(app)
6-
@app = app
7-
end
8-
9-
def call(env)
10-
puts
11-
path = env["REQUEST_PATH"]
12-
query_string = env["QUERY_STRING"]
13-
puts "from: #{env["REQUEST_URI"]}"
14-
if path =~ /\/github/ or query_string =~ /server=http:\/\/github/
15-
if path =~ /\/github\//
16-
new_uri = "https://github.com" + path.gsub("/github", "") + "?" + query_string
17-
else
18-
params = {}
19-
query_string.split("&").each do |pair_string|
20-
pair = pair_string.split("=")
21-
params[pair[0]] = pair[1] ? URI.decode(pair[1]) : ""
22-
end
23-
username = params.delete("username")
24-
password = params.delete("password")
25-
server = params.delete("server")
26-
27-
new_uri = "#{server}#{path}"
28-
params.each do |key, value|
29-
new_uri += "?" unless new_uri =~ /(\?|&)$/
30-
new_uri += key + "=" + URI.encode(value)
31-
end
32-
end
33-
req = Rack::Request.new(env)
34-
method = req.request_method.downcase
35-
method[0..0] = method[0..0].upcase
36-
37-
puts "forwarding to: #{new_uri}"
38-
new_uri = URI.parse(new_uri)
39-
40-
sub_request = Net::HTTP.const_get(method).new("#{new_uri.path}#{"?" if new_uri.query}#{new_uri.query}")
41-
if sub_request.request_body_permitted? and req.body
42-
body = req.body.read
43-
sub_request.body = body
44-
puts "body : #{body.inspect}"
45-
sub_request.content_length = req.content_length
46-
sub_request.content_type = req.content_type
47-
if (req.content_type).include?("application/x-git-upload-pack-request")
48-
sub_request.content_type = "application/x-git-upload-pack-request"
49-
end
50-
end
51-
52-
sub_request["X-Forwarded-For"] = (req.env["X-Forwarded-For"].to_s.split(/, +/) + [req.env['REMOTE_ADDR']]).join(", ")
53-
sub_request["Accept-Encoding"] = req.accept_encoding
54-
sub_request["Referer"] = req.referer
55-
session = Net::HTTP.new(new_uri.host, new_uri.port)
56-
if new_uri.scheme == "https"
57-
session.use_ssl = true
58-
end
59-
# sub_request.basic_auth(username, password)
60-
sub_response = session.start do |http|
61-
http.request(sub_request)
62-
end
63-
64-
headers = {}
65-
sub_response.each_header do |k,v|
66-
headers[k] = v unless k.to_s =~ /cookie|content-length|transfer-encoding/i
67-
end
68-
body = sub_response.read_body
69-
p body[0..200]
70-
File.open("response_body.bin", "w") {|fout| fout.print body}
71-
puts "done"
72-
[sub_response.code.to_i, headers, [body]]
73-
else
74-
@app.call(env)
75-
end
76-
end
77-
end
78-
79-
use GithubForwarder
4+
# class GithubForwarder
5+
# def initialize(app)
6+
# @app = app
7+
# end
8+
#
9+
# def call(env)
10+
# puts
11+
# path = env["REQUEST_PATH"]
12+
# query_string = env["QUERY_STRING"]
13+
# puts "from: #{env["REQUEST_URI"]}"
14+
# if path =~ /\/github/ or query_string =~ /server=http:\/\/github/
15+
# if path =~ /\/github\//
16+
# new_uri = "https://github.com" + path.gsub("/github", "") + "?" + query_string
17+
# else
18+
# params = {}
19+
# query_string.split("&").each do |pair_string|
20+
# pair = pair_string.split("=")
21+
# params[pair[0]] = pair[1] ? URI.decode(pair[1]) : ""
22+
# end
23+
# username = params.delete("username")
24+
# password = params.delete("password")
25+
# server = params.delete("server")
26+
#
27+
# new_uri = "#{server}#{path}"
28+
# params.each do |key, value|
29+
# new_uri += "?" unless new_uri =~ /(\?|&)$/
30+
# new_uri += key + "=" + URI.encode(value)
31+
# end
32+
# end
33+
# req = Rack::Request.new(env)
34+
# method = req.request_method.downcase
35+
# method[0..0] = method[0..0].upcase
36+
#
37+
# puts "forwarding to: #{new_uri}"
38+
# new_uri = URI.parse(new_uri)
39+
#
40+
# sub_request = Net::HTTP.const_get(method).new("#{new_uri.path}#{"?" if new_uri.query}#{new_uri.query}")
41+
# if sub_request.request_body_permitted? and req.body
42+
# body = req.body.read
43+
# sub_request.body = body
44+
# puts "body : #{body.inspect}"
45+
# sub_request.content_length = req.content_length
46+
# sub_request.content_type = req.content_type
47+
# if (req.content_type).include?("application/x-git-upload-pack-request")
48+
# sub_request.content_type = "application/x-git-upload-pack-request"
49+
# end
50+
# end
51+
#
52+
# sub_request["X-Forwarded-For"] = (req.env["X-Forwarded-For"].to_s.split(/, +/) + [req.env['REMOTE_ADDR']]).join(", ")
53+
# sub_request["Accept-Encoding"] = req.accept_encoding
54+
# sub_request["Referer"] = req.referer
55+
# session = Net::HTTP.new(new_uri.host, new_uri.port)
56+
# if new_uri.scheme == "https"
57+
# session.use_ssl = true
58+
# end
59+
# # sub_request.basic_auth(username, password)
60+
# sub_response = session.start do |http|
61+
# http.request(sub_request)
62+
# end
63+
#
64+
# headers = {}
65+
# sub_response.each_header do |k,v|
66+
# headers[k] = v unless k.to_s =~ /cookie|content-length|transfer-encoding/i
67+
# end
68+
# body = sub_response.read_body
69+
# p body[0..200]
70+
# File.open("response_body.bin", "w") {|fout| fout.print body}
71+
# puts "done"
72+
# [sub_response.code.to_i, headers, [body]]
73+
# else
74+
# @app.call(env)
75+
# end
76+
# end
77+
# end
78+
#
79+
# use GithubForwarder
8080

8181
def concat_js
8282
load_file = File.read(File.dirname(__FILE__) + "/../lib/jsgit-client.js")
@@ -87,27 +87,28 @@ def concat_js
8787
end
8888
total_js = js.join("\n\n")
8989
File.open("../lib/git.js", "w") {|f| f.puts total_js }
90-
[200, {"Content-Type" => "text/javascript"}, total_js]
90+
[200, {"Content-Type" => "text/javascript"}, [total_js]]
9191
end
9292

9393
run proc { |env|
94-
if env["REQUEST_URI"] == "/favicon.ico"
95-
return [404, {}, ""]
96-
end
97-
if env["REQUEST_URI"] == "/git.js"
98-
return concat_js
99-
end
100-
ext = env["REQUEST_URI"].split(".").last
101-
case ext
102-
when "js"
103-
content_type = "text/javascript"
104-
when "css"
105-
content_type = "text/css"
94+
if env["PATH_INFO"] == "/favicon.ico"
95+
[404, {"Content-Type" => "image/png"}, ""]
96+
elsif env["PATH_INFO"] == "/git.js"
97+
concat_js
10698
else
107-
content_type = "text/html"
99+
ext = env["PATH_INFO"].split(".").last
100+
p ext
101+
case ext
102+
when "js"
103+
content_type = "text/javascript"
104+
when "css"
105+
content_type = "text/css"
106+
else
107+
content_type = "text/html"
108+
end
109+
p env["REQUEST_PATH"]
110+
[200, {"Content-Type" => content_type}, [File.read(File.dirname(__FILE__) + "/../" + env["PATH_INFO"])]]
108111
end
109-
110-
[200, {"Content-Type" => content_type}, [File.read(File.dirname(__FILE__) + "/../" + env["REQUEST_URI"])]]
111112
}
112113

113114

test/repo_test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
require('../lib/jsgit-server')
22

3-
exports.Repo = {
3+
exports.MemoryRepo = {
44
testCreate: function(test) {
5-
var repo = new JsGit.Repo()
5+
var repo = new JsGit.MemoryRepo()
66
test.done()
77
},
88

99
testEmpty: function(test) {
10-
var repo = new JsGit.Repo()
10+
var repo = new JsGit.MemoryRepo()
1111
test.equal(repo.getRefs().length, 0)
1212
test.equal(repo.getRemotes().length, 0)
1313
test.done()
1414
},
1515

1616
testAddRef: function(test) {
17-
var repo = new JsGit.Repo()
17+
var repo = new JsGit.MemoryRepo()
1818
test.deepEqual(repo.getRefs(), [])
1919

2020
repo.addRef("refs/heads/master", "yurrffff")
@@ -24,7 +24,7 @@ exports.Repo = {
2424
},
2525

2626
testAddRemote: function(test) {
27-
var repo = new JsGit.Repo()
27+
var repo = new JsGit.MemoryRepo()
2828
test.deepEqual(repo.getRemotes(), [])
2929

3030
repo.addRemote("origin", "http://www.yahoo.com/")
@@ -35,7 +35,7 @@ exports.Repo = {
3535
},
3636

3737
testMakeAndAddObject: function(test) {
38-
var repo = new JsGit.Repo()
38+
var repo = new JsGit.MemoryRepo()
3939
test.deepEqual(repo.objectCount(), 0)
4040

4141
repo.makeAndAddObject("asdfasdf", "blob", "Hello World!")

0 commit comments

Comments
 (0)