clone_git_file 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 32fb5dd7523cde6334e4ec6f5373555f8423ecd0
4
- data.tar.gz: 51fd364c11b7bd60c9ade03fc98d07421428bebd
3
+ metadata.gz: a6a43dd0a36bfccd6e5c32722a835ecc6ceeac55
4
+ data.tar.gz: d1fd69615798975cc626c7a135e25d802d75a437
5
5
  SHA512:
6
- metadata.gz: 7be4d9be3dba8d5a90617b70c5dbb1cb0c86d85b7b7dcb92ff530a153ded7d9413a2e5aa7cf80b9205330fa1ad856e4b0437853f5e31ec78273bd2a559c3dba8
7
- data.tar.gz: 4c665b58515d5de912aee26fb0899c841541e9ec15410a8cd8d474d84fade71f3e2dfeb31e860ab1e7febe1db122f3088248bd8417d2467421291c3c08719369
6
+ metadata.gz: 0ab9272637734ae7ae03b0ab59bf19914308af732378ab0f30f6f47ba2b959840121627af3da9d53b050b60d5fd6d6a94eb14f5558957e04013351bea58f2872
7
+ data.tar.gz: 93cbe3fa002925024e92131bdafbf18feae483e1587fbcedf15d72bbb2b64b0b04dacca4141d0dd393564a413db941b70036c548492b35c2d42ea5d342116fd3
data/README.md CHANGED
@@ -18,28 +18,37 @@ This gem allows you to replace these commands:
18
18
 
19
19
  with:
20
20
 
21
- `cgf https://github.com/author/repo/file.rb` (assuming you setup the alias below)
21
+ `ogf https://github.com/author/repo/file.rb` (if you choose to setup an alias)
22
22
 
23
23
  #### Long version
24
24
 
25
- This gem will clone the repository containing a file, then open that file in
26
- your chosen editor. If the repository already exists in the specified directory,
27
- any uncommitted changes will be lost (`git reset HEAD --hard` will be run). The
28
- repository will be pulled after resetting, to get the latest changes.
25
+ This gem will clone the repository containing a file, optionally opening that
26
+ file in your chosen editor. If the repository already exists in the specified
27
+ directory, any uncommitted changes will be lost (`git reset HEAD --hard` will
28
+ be run). The repository will be pulled after resetting, to get the latest
29
+ changes.
29
30
 
30
31
  The editor that will be used is specified using the `EDITOR` environment
31
32
  variable, which allows you to easily override your editor only for this command.
32
33
 
33
34
  # Usage
34
35
 
35
- At a minimum, you need to specify a target directory:
36
+ At a minimum, you need to specify a target directory and url:
36
37
 
37
38
  ```bash
38
39
  TARGET_DIRECTORY=~/dev clone_git_file https://github.com/brandoncc/clone_git_file/blob/master/README.md
39
40
  ```
40
41
 
41
- That will clone this repository to `~/dev/clone_git_file` then open `README.md`
42
- from the `master` branch.
42
+ That will clone this repository to `~/dev/clone_git_file`, then switch to the
43
+ `master` branch. A message will then be shown giving you that location.
44
+
45
+ There are command switches available as follows:
46
+
47
+ | Switch | Description |
48
+ | --- | --- |
49
+ | -o, <nobr>--open</nobr> | Open the file in the specified editor after cloning |
50
+ | -t, <nobr>--terminal</nobr> | Output the command which would be used to open the file in your editor, as text in the terminal. This is useful for piping into other commands. Since Ruby `system` method executions are run in a child process, this is useful for opening the file in your editor in the current shell (more details in the bonus section). This is equivalent to `-ot`. |
51
+ | -s, <nobr>--silent</nobr> | Suppress messages from the cloning process. If you want to pipe the output of `-t` into a command, you should also use this. |
43
52
 
44
53
  If you want to override your default editor, just add `EDITOR=myeditor` to the
45
54
  beginning. For example:
@@ -50,14 +59,21 @@ EDITOR=mvim TARGET_DIRECTORY=~/dev clone_git_file https://github.com/brandoncc/c
50
59
 
51
60
  ### Bonus
52
61
 
53
- * You can also supply the repo url or a repo directory url, and that directory will be opened by your editor
54
- * Setup an alias to make using the gem easy. For example, here is mine (zsh):
62
+ * You can also supply the repo url or a repo subdirectory url, and that directory will be opened by your editor.
63
+ * Setup an alias to make using the gem easy. For example, here are mine (zsh):
55
64
 
56
65
  ```bash
57
66
  alias cgf="TARGET_DIRECTORY=~/dev EDITOR=mvim clone_git_file $1"
67
+ alias ogf="TARGET_DIRECTORY=~/dev EDITOR=mvim clone_git_file -ts $1"
58
68
  ```
69
+
70
+ `cgf` clones, then prints a message with the location of the cloned file/directory.
71
+
72
+ `ogf` prints the commands to open the file/directory in my editor. I then use `source <(...)` to pipe the printed command to `source` which runs it in my current shell. This fixes some weird things that happen when opening the editor in a child process, and also allows the current directory of my current shell to be changed (instead of the child process shell which will be lost when you close the editor).
73
+
74
+ Unfortunately `$1` doesn't get passed into the nested command, so the `source <()` usage can't be in the alias (still looking for a solution to this if anybody has one), so I used TextExpander to setup a snippet which handles that part, with the final command I run being `source <(ogf https://github.com/brandoncc/clone_git_file/blob/master/README.md)`.
59
75
 
60
76
  # Limitations
61
77
 
62
- Github is the only service that is current compatible. I would like to add more
63
- services in the future.
78
+ * Github is the only service that is currently compatible. I would like to add more services in the future.
79
+ * `-s, --silent` is only available on operating systems which can direct output to `/dev/null`, which means it is unavailable on Windows.
data/exe/clone_git_file CHANGED
@@ -1,12 +1,67 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'clone_git_file'
4
+ require 'optparse'
5
+ require 'rbconfig'
4
6
 
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("No editor set, please specify the EDITOR environment variable") unless ENV["EDITOR"]
9
- abort("Sorry, github is the only service currently compatible") unless file.match(/github\.com/)
7
+ options = {}
10
8
 
11
- cloner = CloneGitFile::Cloner.new(file)
9
+ OptionParser.new do |opts|
10
+ opts.banner = "\nA utility for easily cloning from git services\n"
11
+
12
+ opts.on("-o", "--open", "Open in editor after cloning") do
13
+ options[:open_in_editor] = true
14
+ end
15
+
16
+ opts.on("-s", "--silent", "Suppress messages from cloning process") do
17
+ options[:silent] = true
18
+ end
19
+
20
+ opts.on("-t", "--terminal", "Output editor run command to the terminal",
21
+ "instead of executing it (same as -ot)") do
22
+ options[:output_run_command_to_terminal] = true
23
+ end
24
+
25
+ opts.on("-h", "--help", "Displays help") do
26
+ puts opts
27
+ exit
28
+ end
29
+ end.parse!
30
+
31
+ def verify_url_was_provided(url)
32
+ abort("You must specify a url to clone") unless url
33
+ end
34
+
35
+ def verify_target_directory_was_provided
36
+ abort("You haven't specified a target directory") unless ENV["TARGET_DIRECTORY"]
37
+ end
38
+
39
+ def verify_url_is_github(url)
40
+ abort("Sorry, github is the only service currently compatible") unless url.match(/github\.com/)
41
+ end
42
+
43
+ def verify_editor_is_set_when_necessary(options)
44
+ if options[:open_in_editor] || options[:output_run_command_to_terminal]
45
+ unless ENV["EDITOR"]
46
+ abort("No editor set, please specify the EDITOR environment variable")
47
+ end
48
+ end
49
+ end
50
+
51
+ def verify_silent_is_available
52
+ abort("Silent mode is not available on Windows") if windows?
53
+ end
54
+
55
+ def windows?
56
+ RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
57
+ end
58
+
59
+ url = ARGV[0]
60
+ verify_url_was_provided(url)
61
+ verify_target_directory_was_provided
62
+ verify_url_is_github(url)
63
+ verify_editor_is_set_when_necessary(options)
64
+ verify_silent_is_available if options[:silent]
65
+
66
+ cloner = CloneGitFile::Cloner.new(url, options)
12
67
  cloner.open_file
@@ -3,8 +3,9 @@ require "clone_git_file/github_repo_parser"
3
3
 
4
4
  module CloneGitFile
5
5
  class Cloner
6
- def initialize(file)
6
+ def initialize(file, options = {})
7
7
  @file = file
8
+ @options = options
8
9
  end
9
10
 
10
11
  def open_file
@@ -14,7 +15,12 @@ module CloneGitFile
14
15
  clone_repo
15
16
  end
16
17
 
17
- launch_editor
18
+ if @options[:open_in_editor] ||
19
+ @options[:output_run_command_to_terminal]
20
+ launch_editor
21
+ else
22
+ print_clone_location
23
+ end
18
24
  end
19
25
 
20
26
  private
@@ -28,7 +34,22 @@ module CloneGitFile
28
34
  end
29
35
 
30
36
  def launch_editor
31
- system("#{ENV["EDITOR"]} #{local_repo_path}/#{parsed_data.file_relative_path}")
37
+ commands = ""
38
+ file_path = "#{local_repo_path}/#{parsed_data.file_relative_path}"
39
+
40
+ # change into the directory so that relative file loads will work
41
+ commands << "cd #{File.dirname(file_path)}"
42
+ commands << "\n#{ENV["EDITOR"]} #{file_path}"
43
+
44
+ if @options[:output_run_command_to_terminal]
45
+ puts(commands)
46
+ else
47
+ system(commands)
48
+ end
49
+ end
50
+
51
+ def print_clone_location
52
+ puts "Cloned to: #{local_repo_path}/#{parsed_data.file_relative_path}"
32
53
  end
33
54
 
34
55
  def clone_repo
@@ -40,6 +61,8 @@ module CloneGitFile
40
61
  commands << "\ngit checkout #{parsed_data.branch_name}"
41
62
  end
42
63
 
64
+ commands = make_commands_silent(commands) if @options[:silent]
65
+
43
66
  system(commands)
44
67
  end
45
68
 
@@ -50,8 +73,15 @@ module CloneGitFile
50
73
  commands << "\ngit reset HEAD --hard"
51
74
  commands << "\ngit pull"
52
75
  commands << "\ngit checkout #{parsed_data.branch_name}" if parsed_data.branch_name
76
+ commands = make_commands_silent(commands) if @options[:silent]
53
77
 
54
78
  system(commands)
55
79
  end
80
+
81
+ def make_commands_silent(commands)
82
+ cmds = commands.split("\n")
83
+
84
+ cmds.map { |c| "#{c} &> /dev/null" }.join("\n")
85
+ end
56
86
  end
57
87
  end
@@ -1,3 +1,3 @@
1
1
  module CloneGitFile
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clone_git_file
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Conway
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-18 00:00:00.000000000 Z
11
+ date: 2015-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler