clone_git_file 0.1.0

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: 637cbb2eda84c98bd8fb050abdf134fbcba36c68
4
+ data.tar.gz: f4b0ea2a891550970426d6b28569de407255a512
5
+ SHA512:
6
+ metadata.gz: 833662a9821dc6aee03abf872253ebd69a13631e33082bf3c6f001ef6ddb804a04615a99439050acdebdab93052998205beb5afae3703f0ad535e71fd29ce936
7
+ data.tar.gz: 9d349fae49b89876909032c5f55fe0655bd0aca96f5c06b9a0b0123363d609f095c81de5f23cbf7637717cc9d1f82415c8cfc1f9312dc8e6cbc674ba81976a10
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in clone_git_file.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Brandon Conway
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,36 @@
1
+ # Description
2
+
3
+ This gem will clone the repository containing a file, then open that file in
4
+ your chosen editor. If the repository already exists in the specified directory,
5
+ any uncommitted changes will be lost (`git reset HEAD --hard` will be run). The
6
+ repository will be pulled after resetting, to get the latest changes.
7
+
8
+ The editor that will be used is specified using the `EDITOR` environment
9
+ variable, which allows you to easily override your editor only for this command.
10
+
11
+ # Usage
12
+
13
+ At a minimum, you need to specify a target directory:
14
+
15
+ ```bash
16
+ TARGET_DIRECTORY=~/dev clone_git_file https://github.com/brandoncc/clone_git_file/blob/master/README.md
17
+ ```
18
+
19
+ That will clone this repository to `~/dev/clone_git_file` then open `README.md`
20
+ from the `master` branch.
21
+
22
+ If you want to override your default editor, just add `EDITOR=myeditor` to the
23
+ beginning. For example:
24
+
25
+ ```bash
26
+ EDITOR=mvim TARGET_DIRECTORY=~/dev clone_git_file https://github.com/brandoncc/clone_git_file/blob/master/README.md
27
+ ```
28
+
29
+ ### Bonus
30
+
31
+ You can also supply the repo url or a repo directory url, and that directory will be opened by your editor
32
+
33
+ # Limitations
34
+
35
+ Github is the only service that is current compatible. I would like to add more
36
+ services in the future.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "clone_git_file"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'clone_git_file/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "clone_git_file"
8
+ spec.version = CloneGitFile::VERSION
9
+ spec.authors = ["Brandon Conway"]
10
+ spec.email = ["brandoncc@gmail.com"]
11
+
12
+ spec.summary = "A small tool which allows you to easily clone and open a file from git services"
13
+ spec.homepage = "https://github.com/brandoncc/clone_git_file"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = ["clone_git_file"]
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.10"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "minitest"
32
+ spec.add_development_dependency "minitest-spec-context"
33
+ end
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'clone_git_file'
4
+
5
+ file = ARGV[0]
6
+ abort("You forgot the file!") unless file
7
+ abort("You haven't specified a target directory") unless ENV["TARGET_DIRECTORY"]
8
+ abort("Target directory doesn't exist") unless Dir.exists?(File.expand_path(ENV["TARGET_DIRECTORY"]))
9
+ abort("No editor set, please specify the EDITOR environment variable") unless ENV["EDITOR"]
10
+ abort("Sorry, github is the only service currently compatible") unless file.match(/github\.com/)
11
+
12
+ cloner = CloneGitFile::Cloner.new(file)
13
+ cloner.open_file
@@ -0,0 +1,65 @@
1
+ require 'ostruct'
2
+
3
+ module CloneGitFile
4
+ class GithubRepoParser
5
+ def initialize(file)
6
+ @file = file
7
+ end
8
+
9
+ def parse
10
+ abort("Error: That url is not valid") unless valid_url?
11
+ data = OpenStruct.new
12
+ data.repo_url = parse_repo_url
13
+ data.repo_name = parse_repo_name
14
+ data.github_username = parse_github_username
15
+ data.file_relative_path = parse_file_relative_path
16
+ data.branch_name = parse_branch_name
17
+ data
18
+ end
19
+
20
+ def valid_url?
21
+ !!@file.match(%r{.+github\.com/[^/]+/[^/]+})
22
+ end
23
+
24
+ private
25
+
26
+ def parse_github_username
27
+ @file.match(%r{.+github\.com/([^/]+)})[1]
28
+ end
29
+
30
+ def parse_repo_url
31
+ @file.match(%r{.+github\.com/[^/]+/[^/]+})[0]
32
+ end
33
+
34
+ def parse_repo_name
35
+ @file.match(%r{.+github\.com/[^/]+/([^/]+)})[1]
36
+ end
37
+
38
+ def parse_file_relative_path
39
+ path = @file.match(%r{#{Regexp.escape(parse_repo_url)}/(.+)$})[1]
40
+ path.sub!(%r{^(?:blob|tree)/[^/]+}, '') if has_branch?
41
+ path.sub!(%r{^/}, '')
42
+ path
43
+ rescue NoMethodError
44
+ ''
45
+ end
46
+
47
+ def has_branch?
48
+ is_branch? || is_tree?
49
+ end
50
+
51
+ def parse_branch_name
52
+ return nil unless has_branch?
53
+
54
+ @file.match(%r{#{Regexp.escape(parse_repo_url)}/(?:blob|tree)/([^/]+)})[1]
55
+ end
56
+
57
+ def is_branch?
58
+ @file.match(%r{#{Regexp.escape(parse_repo_url)}/blob/[^/]+/.+})
59
+ end
60
+
61
+ def is_tree?
62
+ @file.match(%r{#{Regexp.escape(parse_repo_url)}/tree/[^/]+$})
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module CloneGitFile
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,57 @@
1
+ require "clone_git_file/version"
2
+ require "clone_git_file/github_repo_parser"
3
+
4
+ module CloneGitFile
5
+ class Cloner
6
+ def initialize(file)
7
+ @file = file
8
+ end
9
+
10
+ def open_file
11
+ if Dir.exists?(File.expand_path(local_repo_path))
12
+ update_repo
13
+ else
14
+ clone_repo
15
+ end
16
+
17
+ launch_editor
18
+ end
19
+
20
+ private
21
+
22
+ def parsed_data
23
+ @parsed_data ||= GithubRepoParser.new(@file).parse
24
+ end
25
+
26
+ def local_repo_path
27
+ @local_repo_path ||= "#{ENV["TARGET_DIRECTORY"]}/#{parsed_data.github_username}/#{parsed_data.repo_name}"
28
+ end
29
+
30
+ def launch_editor
31
+ system("#{ENV["EDITOR"]} #{local_repo_path}/#{parsed_data.file_relative_path}")
32
+ end
33
+
34
+ def clone_repo
35
+ commands = ""
36
+ commands << "git clone #{parsed_data.repo_url} #{local_repo_path}"
37
+
38
+ if parsed_data.branch_name
39
+ commands << "\ncd #{local_repo_path}"
40
+ commands << "\ngit checkout #{parsed_data.branch_name}"
41
+ end
42
+
43
+ system(commands)
44
+ end
45
+
46
+ def update_repo
47
+ commands = ""
48
+
49
+ commands << "cd #{local_repo_path}"
50
+ commands << "\ngit reset HEAD --hard"
51
+ commands << "\ngit pull"
52
+ commands << "\ngit checkout #{parsed_data.branch_name}" if parsed_data.branch_name
53
+
54
+ system(commands)
55
+ end
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clone_git_file
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brandon Conway
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-13 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-spec-context
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - brandoncc@gmail.com
72
+ executables:
73
+ - clone_git_file
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - clone_git_file.gemspec
86
+ - exe/clone_git_file
87
+ - lib/clone_git_file.rb
88
+ - lib/clone_git_file/github_repo_parser.rb
89
+ - lib/clone_git_file/version.rb
90
+ homepage: https://github.com/brandoncc/clone_git_file
91
+ licenses:
92
+ - MIT
93
+ metadata:
94
+ allowed_push_host: https://rubygems.org
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.5.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: A small tool which allows you to easily clone and open a file from git services
115
+ test_files: []