git_local 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 162324f58ec0dcc7a42618d829adb745b1a9d168
4
+ data.tar.gz: 50bded75f0cccd73479ba51de0b670a5daed7862
5
+ SHA512:
6
+ metadata.gz: 29ba407c31aa08a995408f1c806be444bb4be050ebe4b0b1ada1894006f6fabef5ce37562cabf574ddfb4c1f5e930cba9e344b5e787984857332ec5c653d67cc
7
+ data.tar.gz: a0a948674d56f4b0b63b226daaf7ad0347160d8c4ba21250125a6a5487cb9a9add8b792f60a722711fd6abb09deb8e1b8943d4e6600f80689aa7ab96ff3480ac
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .DS_Store
11
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Galvanize Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # git_local
2
+
3
+ A lightweight git command line wrapper in Ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'git_local'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install git_local
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/git_local.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'git_local/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "git_local"
8
+ spec.version = GitLocal::VERSION
9
+ spec.authors = ["Galvanize Product"]
10
+ spec.email = ["dev@galvanize.com"]
11
+
12
+ spec.summary = "A lightweight git command line wrapper in Ruby"
13
+ spec.homepage = "https://github.com/Galvanize-IT/GitLocal"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "pry", "~> 0.10.4"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+
26
+ spec.required_ruby_version = '~> 2.0'
27
+ end
@@ -0,0 +1,38 @@
1
+ module GitLocal
2
+ class Object
3
+ attr_reader :path
4
+
5
+ class NotFound < StandardError
6
+ end
7
+
8
+ def initialize(path)
9
+ @path = path
10
+ end
11
+
12
+ def name
13
+ path.rindex("/") ? path[path.rindex("/") + 1..-1] : path
14
+ end
15
+
16
+ def read(max_lines = nil)
17
+ return contents if max_lines.nil?
18
+
19
+ File.foreach(path).first(max_lines).join
20
+ rescue StandardError => e
21
+ raise NotFound
22
+ end
23
+
24
+ def sha
25
+ Digest::SHA1.hexdigest("blob " + contents.length.to_s + "\0" + contents)
26
+ end
27
+
28
+ def size
29
+ File.size(path).to_f / 2**20
30
+ end
31
+
32
+ private
33
+
34
+ def contents
35
+ @contents ||= File.read(path)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,92 @@
1
+ module GitLocal
2
+ class Repository
3
+ class NotFound < StandardError
4
+ end
5
+
6
+ def initialize(org:, repo:, branch:, local_directory:)
7
+ @org = org
8
+ @repo = repo
9
+ @branch = branch
10
+ @local_directory = local_directory
11
+ end
12
+
13
+ def get
14
+ Dir.exist?(path) && new_commit_on_remote? ? pull : clone_and_checkout
15
+ end
16
+
17
+ def file_object(file_path)
18
+ GitLocal::Object.new(File.join(path, file_path))
19
+ end
20
+
21
+ def file_objects(file_path = nil)
22
+ repo_path = file_path.nil? ? path : File.join(path, file_path)
23
+ searchable_repo_path = repo_path.end_with?("/") ? repo_path : "#{repo_path}/"
24
+
25
+ Dir.glob("#{searchable_repo_path}*").each_with_object([]) do |filename, git_files|
26
+ next if %w(. ..).include?(filename) || File.extname(filename) == ".zip" || File.directory?(filename)
27
+
28
+ git_files << GitLocal::Object.new(filename)
29
+ end
30
+ end
31
+
32
+ def all_file_objects(file_path = nil, include_dirs = false)
33
+ repo_path = file_path.nil? ? path : File.join(path, file_path)
34
+ searchable_repo_path = repo_path.end_with?("/") ? repo_path : "#{repo_path}/"
35
+ Dir.glob("#{searchable_repo_path}**/*").each_with_object([]) do |filename, git_files|
36
+ if !File.directory?(filename) || include_dirs
37
+ git_files << GitLocal::Object.new(filename)
38
+ end
39
+ end
40
+ end
41
+
42
+ def local_path(file_path)
43
+ file_path.gsub("#{path}/", "")
44
+ end
45
+
46
+ def new_commit_on_remote?
47
+ popened_io = IO.popen("(cd #{path} && git rev-parse HEAD)")
48
+ head = popened_io.read.chomp.split("\n").last
49
+ Process.wait(popened_io.pid)
50
+
51
+ popened_io = IO.popen("(cd #{path} && git remote update && git rev-parse origin/#{branch})")
52
+ remote = popened_io.read.chomp
53
+ Process.wait(popened_io.pid)
54
+ raise NotFound unless $?.to_i == 0
55
+
56
+ remote != head
57
+ end
58
+
59
+ def path
60
+ @path ||= "#{local_directory}/#{org_repo_branch}"
61
+ end
62
+
63
+ private
64
+
65
+ attr_reader :org, :repo, :branch, :local_directory
66
+
67
+ def clone_and_checkout
68
+ FileUtils.makedirs(repo_path) unless Dir.exist?(repo_path)
69
+
70
+ popened_io = IO.popen("(cd #{repo_path} && git clone git@github.com:#{org_repo}.git --branch #{branch} --single-branch #{branch} && cd #{path})")
71
+ Process.wait(popened_io.pid)
72
+
73
+ raise NotFound unless $?.to_i == 0
74
+ end
75
+
76
+ def org_repo
77
+ @org_repo ||= "#{org}/#{repo}"
78
+ end
79
+
80
+ def org_repo_branch
81
+ @org_repo_branch ||= "#{org}/#{repo}/#{branch}"
82
+ end
83
+
84
+ def pull
85
+ Process.wait(IO.popen("(cd #{path} && git pull)").pid)
86
+ end
87
+
88
+ def repo_path
89
+ @repo_path ||= "#{local_directory}/#{org_repo}"
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,42 @@
1
+ require 'fileutils'
2
+
3
+ module GitLocal
4
+ module TestHelpers
5
+ def create_git_repository(org:, repo:, branch:, local_directory:, file_paths: [], size: nil)
6
+ repo_path = GitLocal::Repository.new(org: org, repo: repo, branch: branch, local_directory: local_directory).path
7
+
8
+ FileUtils.mkdir_p(repo_path)
9
+
10
+ file_paths.each do |file_path|
11
+ create_file("#{repo_path}/#{file_path}", size)
12
+ end
13
+ end
14
+
15
+ def write_local_git_file(org:, repo:, branch:, file_path:, file_contents:, local_directory:)
16
+ repo_path = GitLocal::Repository.new(org: org, repo: repo, branch: branch, local_directory: local_directory).path
17
+
18
+ file = File.open("#{repo_path}/#{file_path}", "w")
19
+ file.puts(file_contents)
20
+ file.close
21
+ end
22
+
23
+ def remove_all_repositories(path)
24
+ repositories = File.join(path, "**", "*")
25
+ FileUtils.rm_rf Dir.glob(repositories)
26
+ end
27
+
28
+ def create_file(path, size = nil)
29
+ dir = File.dirname(path)
30
+
31
+ FileUtils.mkdir_p(dir) unless File.directory?(dir)
32
+
33
+ File.new(path, "w")
34
+
35
+ return if size.nil?
36
+
37
+ File.open(path, "wb") do |f|
38
+ size.to_i.times { f.write(SecureRandom.random_bytes(2**20)) }
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module GitLocal
2
+ VERSION = "0.0.2"
3
+ end
data/lib/git_local.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "git_local/version"
2
+ require "git_local/repository"
3
+ require "git_local/object"
4
+ require "git_local/test_helpers"
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: git_local
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Galvanize Product
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.10.4
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.10.4
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - dev@galvanize.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - bin/setup
82
+ - git_local.gemspec
83
+ - lib/git_local.rb
84
+ - lib/git_local/object.rb
85
+ - lib/git_local/repository.rb
86
+ - lib/git_local/test_helpers.rb
87
+ - lib/git_local/version.rb
88
+ homepage: https://github.com/Galvanize-IT/GitLocal
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - "~>"
99
+ - !ruby/object:Gem::Version
100
+ version: '2.0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.6.11
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: A lightweight git command line wrapper in Ruby
112
+ test_files: []