rgit 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ *.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rgit.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1 @@
1
+ Stupid Git clone with a PHP backend that i needed because I can't stand FTP while working on shared hostings.
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rgit'
4
+
5
+ rit = Rgit::Repo.new("empty")
6
+ if (Dir.exists? ".rit_empty")
7
+ Dir.chdir(".rit_empty")
8
+ conf = JSON.parse(File.read("rit.conf"))
9
+ rit.repo = conf["repo"]
10
+ Dir.chdir("../")
11
+ end
12
+ options = {}
13
+ OptionParser.new do |opts|
14
+ opts.banner = "Usage: rit [options]..."
15
+ opts.on('-d [arg]', '--define [arg]', 'Sets current repo') do |repo|
16
+ rit.repo = "empty"
17
+ rit.write_config "rit.conf","repo", repo
18
+ end
19
+ opts.on('-a [arg]', '--add [arg]', 'Adds files') do |pattern|
20
+ rit.add_files pattern
21
+ end
22
+ opts.on('-j', '--json', 'generates remote json.conf') do |what|
23
+ rit.generate
24
+ end
25
+ opts.on('-r [arg]','--repo [arg]',"Sets remote repo") do |repo|
26
+ rit.write_config "rit.conf","repo",repo
27
+ end
28
+ opts.on('-k [arg]','--key [arg]',"Sets key") do |key|
29
+ rit.write_config "rit.conf","key",key
30
+ end
31
+ opts.on('-c [arg]','--clean [arg]',"Clean files added to queue") do
32
+ rit.delete_queue
33
+ end
34
+ opts.on('-f','--force',"Forces file overwrite") do
35
+ options[:force] = true
36
+ end
37
+ opts.on('-u [arg]','--user [arg]',"Sets username") do |u|
38
+ rit.write_config "rit.conf","user",u
39
+ end
40
+ opts.on('-p [arg]','--push [arg]',"Pushes to repo") do |arg|
41
+ rit.upload_files(options)
42
+ end
43
+ opts.on('-g','--generate',"generate configuration (to upload in /.rit_keys)") do
44
+ rit.ziphp
45
+ end
46
+ opts.on('-s','--sync',"Downloads from remote repo") do
47
+ rit.sync
48
+ end
49
+ opts.on('-i [arg]','--init [arg]',"Creates empty repo") do |arg|
50
+ if FileTest::directory? ".rit_"+arg
51
+ puts "A .rit_#{arg} directory already exists"
52
+ exit
53
+ end
54
+ rit.create_repo arg
55
+ end
56
+ opts.on( '-h', '--help', 'Display this screen' ) do
57
+ puts opts
58
+ exit
59
+ end
60
+ end.parse!
61
+
@@ -0,0 +1,2 @@
1
+ deny from all
2
+
@@ -0,0 +1,109 @@
1
+ <?php
2
+
3
+ if (!isset($_POST["name"]) || !isset($_POST["key"]))
4
+ die("!!! You need to have a valid username:key combination in order to access this page");
5
+ elseif (!isset($_GET["action"]))
6
+ die("!!! You need to set an action");
7
+
8
+ $user = str_replace(array('/','.'),'',$_POST["name"]);
9
+ $ukey = str_replace(array('/','.'),'',$_POST["key"]);
10
+ if (!(file_exists(".rit_keys/".$user)))
11
+ die("!!! Such user doesn't exist");
12
+
13
+ $key = trim(file_get_contents(".rit_keys/".$user));
14
+ if ($key != $ukey)
15
+ die("!!! Wrong username:key combination");
16
+
17
+ function updateField($file,$key,$value) {
18
+ if (file_exists(".rit/".$file)) {
19
+ $prev = json_decode(file_get_contents(".rit/".$file),true);
20
+ $prev[$key] = $value;
21
+ }
22
+ else
23
+ $prev[$key] = $value;
24
+ file_put_contents(".rit/".$file,json_encode($prev));
25
+ }
26
+ function getField($file,$key) {
27
+ if (file_exists(".rit/".$file)) {
28
+ $prev = json_decode(file_get_contents(".rit/".$file),true);
29
+ if ($prev[$key] != "")
30
+ return $prev[$key];
31
+ else
32
+ return "404";
33
+ }
34
+ else
35
+ return "404";
36
+ }
37
+ function generateJSON() {
38
+ $path = $_POST["dir"]."/".$_POST["filename"];
39
+ updateField($_POST["repo"],$path,$_POST["last"]);
40
+ }
41
+ function handle_upload() {
42
+ $path = $_POST["dir"]."/".basename( $_FILES['rit']['name']);
43
+ updateField($_POST["repo"],$path,$_POST["last"]);
44
+ if (!file_exists($_POST["dir"]) && !is_dir($_POST["dir"]))
45
+ mkdir($_POST["dir"], 0, true);
46
+ if (file_exists($path))
47
+ unlink($path);
48
+ move_uploaded_file($_FILES['rit']['tmp_name'], $path);
49
+ }
50
+ function add_user() {
51
+ if(!isset($_POST["add_name"]) || !isset($_POST["add_key"]))
52
+ die("!!! You need to specify a new username and a new key");
53
+ file_put_contents(".rit_keys/".str_replace(array('/','.'),'',$_POST["add_name"]),str_replace(array('/','.'),'',$_POST["add_key"]));
54
+ }
55
+ function remove_user() {
56
+ if(!isset($_POST["rem_name"]))
57
+ die("!!! You need to specify a username to remove");
58
+ elseif (file_exists(".rit_keys/".str_replace(array('/','.'),'',$_POST["rem_name"])))
59
+ unlink(".rit_keys/".str_replace(array('/','.'),'',$_POST["rem_name"]));
60
+ else
61
+ die("No such user");
62
+ }
63
+
64
+ function downloadUpdate() {
65
+ $file = $_POST["file"];
66
+ if (file_exists($file)) {
67
+ header('Content-Description: File Transfer');
68
+ header('Content-Type: application/octet-stream');
69
+ header('Content-Disposition: attachment; filename='.basename($file));
70
+ header('Content-Transfer-Encoding: binary');
71
+ header('Expires: 0');
72
+ header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
73
+ header('Pragma: public');
74
+ header('Content-Length: ' . filesize($file));
75
+ ob_clean();
76
+ flush();
77
+ readfile($file);
78
+ exit;
79
+ }
80
+ }
81
+
82
+ function listFiles() {
83
+ echo file_get_contents(".rit/".$_POST["repo"]);
84
+ }
85
+
86
+ switch ($_GET["action"]) {
87
+ case 'add':
88
+ add_user();
89
+ break;
90
+ case 'upload':
91
+ handle_upload();
92
+ break;
93
+ case 'remove':
94
+ remove_user();
95
+ break;
96
+ case 'update':
97
+ downloadUpdate();
98
+ break;
99
+ case 'list':
100
+ listFiles();
101
+ break;
102
+ case 'generate':
103
+ generateJSON();
104
+ break;
105
+ default:
106
+ die("Such action doesn't exist");
107
+ }
108
+ ?>
109
+
@@ -0,0 +1,228 @@
1
+ require 'rgit/version'
2
+ require 'rest_client'
3
+ require 'json'
4
+ require 'optparse'
5
+ require 'find'
6
+ require 'fileutils'
7
+ require 'date'
8
+ require 'zipruby'
9
+
10
+
11
+ module Rgit
12
+ class Repo
13
+ FILE_DIR = File.expand_path(File.join('..', '..', 'files'), __FILE__)
14
+ attr_accessor :repo
15
+ def initialize(repo)
16
+ @repo = repo
17
+ RestClient.proxy = ENV['http_proxy']
18
+ end
19
+ # upload_files -> pushes files to repo
20
+ def upload_files(options)
21
+ Dir.chdir(".rit_"+@repo)
22
+ files = JSON.parse(File.read("queue.json"));
23
+ Dir.chdir("../")
24
+ i = 1
25
+ self.get_repo_files
26
+ puts "Pushing #{files.length} file(s)"
27
+ files.map! do |file|
28
+ puts "[#{i}/#{files.length}] "+file["dir"]+"/"+file["name"]
29
+ if (new_file? File.join(file["dir"],file["name"]))
30
+ puts "Newer version of "+file["dir"]+"/"+file["name"]+" available on the webserver, run rgit --sync"
31
+ print "Do you really want to overwrite a newer version of "+file["dir"]+"/"+file["name"]+"? (y/n)"
32
+ choice = gets.chop
33
+ (choice == "y")? allow = true : allow = false
34
+ else
35
+ allow = true
36
+ end
37
+ if (allow)
38
+ request = RestClient.post(read_config("rit.conf","repo")+"?action=upload",
39
+ {
40
+ :repo => @repo,
41
+ :dir => file["dir"],
42
+ :name => read_config("rit.conf","user"),
43
+ :key => read_config("rit.conf","key"),
44
+ :last => file["last"],
45
+ :rit => File.new(File.join(file["dir"],file["name"]),'rb')
46
+ }
47
+ );
48
+ end
49
+ i+=1
50
+ end
51
+ Dir.chdir(".rit_"+@repo)
52
+ File.delete("queue.json")
53
+ Dir.chdir("../")
54
+ end
55
+ #generate -> creates rit.conf
56
+ def generate
57
+ Dir.chdir(".rit_"+@repo)
58
+ files = JSON.parse(File.read("queue.json"));
59
+ Dir.chdir("../")
60
+ i = 1
61
+ puts "Adding to rit.conf #{files.length} file(s)"
62
+ files.map! do |file|
63
+ puts "[#{i}/#{files.length}] "+file["dir"]+"/"+file["name"]
64
+ request = RestClient.post(read_config("rit.conf","repo")+"?action=generate",
65
+ {
66
+ :repo => @repo,
67
+ :dir => file["dir"],
68
+ :name => read_config("rit.conf","user"),
69
+ :key => read_config("rit.conf","key"),
70
+ :last => file["last"],
71
+ :filename => file["name"]
72
+ }
73
+ );
74
+ i+=1
75
+ end
76
+ Dir.chdir(".rit_"+@repo)
77
+ File.delete("queue.json")
78
+ Dir.chdir("../")
79
+ end
80
+ # write_config -> updates config file (uses JSON)
81
+ def write_config file,key,value
82
+ Dir.chdir(".rit_"+@repo)
83
+ conf = JSON.parse(File.read(file))
84
+ conf[key] = value
85
+ f = File.new(file, "w+")
86
+ f.write(JSON.generate(conf))
87
+ Dir.chdir("../")
88
+ end
89
+ #read_config -> reads key from JSON config file
90
+ def read_config file,key
91
+ if !FileTest::directory? ".rit_"+@repo
92
+ puts "You need to create an empty repo first"
93
+ exit
94
+ end
95
+ Dir.chdir(".rit_"+@repo)
96
+ if File.file? file
97
+ conf = JSON.parse(File.read(file))
98
+ Dir.chdir("../")
99
+ return conf[key]
100
+ end
101
+ end
102
+ #get_files -> recursively finds file matching given regex
103
+ def get_files(dir, regex)
104
+ returnedFiles = []
105
+ i = 0
106
+ Find.find(@repo+"/"+dir) do |path|
107
+ dir, name = File.split(path)
108
+ if (!File.directory?(File.join(dir,name)) && (File.join(dir,name)) =~ regex)
109
+ returnedFiles << {:dir => dir, :name => name, :last => File.mtime(path)}
110
+ puts "Added file: #{dir}/#{name}"
111
+ i += 1
112
+ end
113
+ end
114
+ puts "Total: #{i} file(s)"
115
+ return returnedFiles
116
+ end
117
+ #new_file? -> check if server's file version is newer than the local one
118
+ def new_file? file_path
119
+ if !File.exist? file_path
120
+ return false
121
+ end
122
+ local_time = File.mtime(file_path)
123
+ if !self.read_config("remote.conf",file_path).nil?
124
+ remote_time = DateTime.parse(self.read_config("remote.conf",file_path)).to_time
125
+ return local_time < remote_time
126
+ else
127
+ return false
128
+ end
129
+ end
130
+ #get_repo_files -> gets file list from repo
131
+ def get_repo_files
132
+ puts "Downloading remote file list"
133
+ get_repo_conf = RestClient.post(self.read_config("rit.conf","repo")+'?action=update', {:name => self.read_config("rit.conf","user"), :key => self.read_config("rit.conf","key"), :file => File.join(".rit",@repo) }) { |resp, request, result, &block|
134
+ open(File.join(".rit_"+@repo,"remote.conf"), "wb") do |file|
135
+ file.write(resp.body)
136
+ end
137
+ }
138
+ end
139
+ #create_repo -> creates new repo
140
+ def create_repo key
141
+ if (!Dir.exists? ".rit_empty")
142
+ Dir.mkdir(".rit_empty")
143
+ Dir.chdir(".rit_empty")
144
+ normal = Hash.new()
145
+ normal["repo"]= key
146
+ normal["json_sucks"] = "not less than 2 vars"
147
+ f=File.open("rit.conf","w+")
148
+ f.write(JSON.generate(normal))
149
+ else
150
+ conf = JSON.parse(File.read("rit.conf"))
151
+ conf["repo"] = key
152
+ f = File.new("rit.conf", "w+")
153
+ f.write(JSON.generate(conf))
154
+ end
155
+ Dir.chdir("../")
156
+ Dir.mkdir(".rit_"+key)
157
+ Dir.mkdir(key)
158
+ Dir.chdir ".rit_"+key
159
+ data = Hash.new()
160
+ print "Username: "
161
+ data["user"] = gets.chop
162
+ print "Password: "
163
+ data["key"] = gets.chop
164
+ print "Repo: "
165
+ data["repo"] = gets.chop
166
+ f=File.open("rit.conf","w+")
167
+ f.write(JSON.generate(data))
168
+ Dir.chdir("../")
169
+ end
170
+ #add_files -> adds file to queue
171
+ def add_files pattern
172
+ Dir.chdir(".rit_"+@repo)
173
+ f = File.new("queue.json", "w+")
174
+ Dir.chdir("../")
175
+ f.write(JSON.generate(self.get_files(".",Regexp.new(pattern))))
176
+ end
177
+ #delete_queue -> deletes queue
178
+ def delete_queue
179
+ Dir.chdir(".rit_"+@repo)
180
+ File.delete("queue.json")
181
+ Dir.chdir("../")
182
+ end
183
+ #sync
184
+ def sync
185
+ self.get_repo_files
186
+ files = JSON.parse(File.read(File.join(".rit_"+@repo,"remote.conf")))
187
+ puts "syncing #{files.length} files"
188
+ i = 0
189
+ files.each do |key, value|
190
+ print "[#{i+1}/#{files.length}] #{key}"
191
+ if (!File.exist? key) || (new_file? key)
192
+ if !File.exist? key
193
+ file_path = File.split(key);
194
+ FileUtils.mkdir_p(file_path[0]);
195
+ end
196
+ req = RestClient.post(self.read_config("rit.conf","repo")+'?action=update', {:name => self.read_config("rit.conf","user"), :key => self.read_config("rit.conf","key"), :file => key }) { |resp, request, result, &block|
197
+ puts "\t\t*syncED*"
198
+ open(key, "wb") do |file|
199
+ file.write(resp.body)
200
+ end
201
+ }
202
+ else
203
+ puts "\t\t*UPDATED*"
204
+ end
205
+ i += 1
206
+ end
207
+ end
208
+ #ziphp -> creates zip for the webserver
209
+ def ziphp
210
+ if File.exist?("rgit_upload.zip")
211
+ File.unlink("rgit_upload.zip")
212
+ end
213
+ print "Insert username: "
214
+ name = gets
215
+ print "Insert password: "
216
+ pwd = gets
217
+ Zip::Archive.open('rgit_upload.zip', Zip::CREATE) do |ar|
218
+ ar.add_dir(".rit_keys")
219
+ ar.add_dir(".rit");
220
+ ar.add_file(File.join(FILE_DIR,"rit.php"))
221
+ ar.add_buffer(File.join(".rit_keys/",name.chop), pwd.chop)
222
+ ar.add_file(".rit_keys/.htaccess", File.join(FILE_DIR,".htaccess"))
223
+ ar.add_file(".rit/.htaccess", File.join(FILE_DIR,".htaccess"))
224
+ end
225
+ end
226
+ end
227
+ end
228
+
@@ -0,0 +1,3 @@
1
+ module Rgit
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rgit/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rgit"
7
+ s.version = Rgit::VERSION
8
+ s.authors = ["yawn"]
9
+ s.email = ["yawn.localhost@gmail.com"]
10
+ s.homepage = "http://github.com/MrYawn/Rgit"
11
+ s.summary = %q{stupid ruby git clone}
12
+ s.description = %q{git clone that manages website using a php backend}
13
+
14
+ s.rubyforge_project = "rgit"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ s.add_dependency "rest-client"
21
+ s.add_dependency "json"
22
+ s.add_dependency "zipruby"
23
+ end
24
+
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rgit
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 3
9
+ version: 0.0.3
10
+ platform: ruby
11
+ authors:
12
+ - yawn
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-07-07 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rest-client
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: json
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :runtime
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: zipruby
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ type: :runtime
58
+ version_requirements: *id003
59
+ description: git clone that manages website using a php backend
60
+ email:
61
+ - yawn.localhost@gmail.com
62
+ executables:
63
+ - rgit
64
+ extensions: []
65
+
66
+ extra_rdoc_files: []
67
+
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - README
72
+ - Rakefile
73
+ - bin/rgit
74
+ - files/.htaccess
75
+ - files/rit.php
76
+ - lib/rgit.rb
77
+ - lib/rgit/version.rb
78
+ - rgit.gemspec
79
+ has_rdoc: true
80
+ homepage: http://github.com/MrYawn/Rgit
81
+ licenses: []
82
+
83
+ post_install_message:
84
+ rdoc_options: []
85
+
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ requirements: []
105
+
106
+ rubyforge_project: rgit
107
+ rubygems_version: 1.3.7
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: stupid ruby git clone
111
+ test_files: []
112
+