git-fyncy 0.3.4 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,28 @@
1
+ require 'git-fyncy/utils'
2
+
3
+ module GitFyncy
4
+ class Remote
5
+ include Utils
6
+
7
+ def initialize(remote, path, rsync_args)
8
+ @remote = remote
9
+ @path = slashify path
10
+ @rsync_flags = rsync_args.join(" ")
11
+ end
12
+
13
+ def command(cmd)
14
+ puts cmd.length < 80 ? cmd : cmd[0...77] + '...'
15
+ system cmd
16
+ end
17
+
18
+ def scp(paths, rsync_args=[])
19
+ return if paths.empty?
20
+ command "rsync -zpR --checksum #{@rsync_flags} #{paths.to_a.join ' '} #{@remote}:#{@path}"
21
+ end
22
+
23
+ def ssh_rm(paths)
24
+ return if paths.empty?
25
+ command "ssh #{@remote} 'cd #{@path}; rm -f #{paths.to_a.join ' '}'"
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,97 @@
1
+ require 'git-fyncy/utils'
2
+
3
+ module GitFyncy
4
+ module Repo
5
+ extend Utils
6
+ GIT_CONFIG_NAME = "fyncy.remote"
7
+ DEFAULT_REMOTE = "origin".freeze
8
+
9
+
10
+ def self.get_stdout_str(cmd)
11
+ out = `#{cmd}`.chomp
12
+ out.empty? ? nil : out
13
+ end
14
+
15
+ def self.fyncy_remote_name_from_config
16
+ get_stdout_str "git config --get #{GIT_CONFIG_NAME}"
17
+ end
18
+
19
+ def self.configure_fyncy_remote(remote_name)
20
+ system "git config --add #{GIT_CONFIG_NAME} '#{remote_name}'"
21
+ end
22
+
23
+ def self.lookup_remote_url(remote_name)
24
+ get_stdout_str "git config --get remote.#{remote_name}.url"
25
+ end
26
+
27
+ def self.remote_defined?(remote_name)
28
+ system "git config --get remote.#{remote_name}.url"
29
+ end
30
+
31
+ TRAILING_GIT_REGEX = %r{^(.*)(s\.git/?$)}.freeze
32
+ def self.remove_trailing_git(str)
33
+ md = TRAILING_GIT_REGEX.match str
34
+ md ? md[1] : str
35
+ end
36
+
37
+ SSH_URL_REGEX = %r{ssh://(\w+@)?([\w\.]+)(:\d+)?([/\w\.]*)$}.freeze
38
+ SCP_REGEX = /^(\w+@)?([\.\w]+):(.*)$/.freeze
39
+
40
+ def self.host_and_path_for_remote(remote_name)
41
+ url = lookup_remote_url remote_name
42
+ return unless url
43
+
44
+ md = SSH_URL_REGEX.match url
45
+ if md
46
+ user = md[1]
47
+ host = md[2]
48
+ port = md[3]
49
+ if port && port[1..-1].to_i != 22
50
+ STDERR.puts "WARNING: git-fyncy does not currently support port numbers in remote names. Yeah, it is lame. Sorry. Please contribute to fix this!"
51
+ end
52
+ path = md[4]
53
+ else
54
+ md = SCP_REGEX.match url
55
+ unless md
56
+ pexit "Could not determine host and path from url (#{url}). To work with git-fyncy, the remote's url should be an ssh (i.e. start with \"ssh://\") or scp (i.e. USER@HOST:PATH) style url."
57
+ end
58
+ user = md[1]
59
+ host = md[2]
60
+ path = slashify remove_trailing_git md[3]
61
+ end
62
+
63
+ ["#{user}#{host}", path]
64
+ end
65
+
66
+ def self.prompt_user_for_remote_name
67
+ print "No remote specified for git fyncy. Enter the name of the remote to use (or press return to use the default, #{DEFAULT_REMOTE}):"
68
+ remote_name = gets.chomp
69
+ remote_name = DEFAULT_REMOTE if remote_name.empty?
70
+ if remote_defined?(remote_name)
71
+ puts
72
+ else
73
+ pexit "A remote by the name of #{remote_name} is not defined in this repo."
74
+ end
75
+
76
+ if configure_fyncy_remote remote_name
77
+ remote_name
78
+ else
79
+ pexit "Failed to set #{GIT_CONFIG_NAME} to #{remote_name}. Perhaps there was a permissions error?"
80
+ end
81
+ end
82
+
83
+ def self.host_and_path_from_current_repo
84
+ # Determine remote fyncy should use
85
+ remote_name = fyncy_remote_name_from_config
86
+ remote_name = prompt_user_for_remote_name unless remote_name
87
+ res = nil
88
+ res = host_and_path_for_remote remote_name if remote_name
89
+ res = host_and_path_for_remote DEFAULT_REMOTE unless res
90
+ res
91
+ end
92
+
93
+ def self.git_aware_files
94
+ `git ls-files -cmo --exclude-standard`.split("\n")
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,49 @@
1
+ require 'git-fyncy/utils'
2
+ require 'git-fyncy/repo'
3
+ require 'git-fyncy/remote'
4
+ require 'listen'
5
+
6
+ module GitFyncy
7
+ class Synchronizer
8
+ include Utils
9
+
10
+ def initialize(extra_rsync_args)
11
+ remote, path = Repo.host_and_path_from_current_repo
12
+ pexit 'A remote and path must be specified' unless remote && path
13
+ @remote = Remote.new remote, path, extra_rsync_args
14
+ end
15
+
16
+ def sync
17
+ @remote.scp Repo.git_aware_files
18
+ end
19
+
20
+ def listen
21
+ puts "GIT FYNCY: Listening @ #{Time.now.ctime}"
22
+ relpath = method :relative_path
23
+ files_to_remove = Set.new
24
+ begin
25
+ Listen.to!('.') do |modified, added, removed|
26
+ begin
27
+ self.sync
28
+ rel_removed = removed.map(&relpath)
29
+ files_to_remove.merge rel_removed
30
+ files_to_remove.clear if @remote.ssh_rm files_to_remove
31
+ rescue => e
32
+ puts e.inspect
33
+ end
34
+ end
35
+ rescue SignalException
36
+ exit 42
37
+ ensure
38
+ puts "\n"
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def relative_path(path)
45
+ path.slice! slashify(Dir.pwd)
46
+ path
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,12 @@
1
+ module GitFyncy
2
+ module Utils
3
+ def slashify(path)
4
+ path[-1] == '/' ? path : path + '/'
5
+ end
6
+
7
+ def pexit(error)
8
+ STDERR.puts error
9
+ exit 1
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module GitFyncy
2
- VERSION = "0.3.4"
2
+ VERSION = "0.5.0"
3
3
  end
data/lib/git-fyncy.rb CHANGED
@@ -1,72 +1,18 @@
1
1
  require "git-fyncy/version"
2
+ require "git-fyncy/synchronizer"
2
3
  require 'listen'
3
4
 
4
5
  module GitFyncy
5
- def self.slashify(path)
6
- path[-1] == '/' ? path : path + '/'
7
- end
8
-
9
- class Remote
10
- def initialize(remote, path)
11
- @remote = remote
12
- @path = GitFyncy.slashify path
13
- end
14
-
15
- def command(cmd)
16
- puts cmd.length < 80 ? cmd : cmd[0...77] + '...'
17
- system cmd
18
- end
19
-
20
- def scp(paths)
21
- return if paths.empty?
22
- command "rsync -zpR #{paths.to_a.join ' '} #{@remote}:#{@path}"
23
- end
24
-
25
- def ssh_rm(paths)
26
- return if paths.empty?
27
- command "ssh #{@remote} 'cd #{@path}; rm -f #{paths.to_a.join ' '}'"
28
- end
29
- end
30
-
31
- def self.relative_path(path)
32
- path.slice! GitFyncy.slashify(Dir.pwd)
33
- path
34
- end
35
-
36
- def self.git_aware_files
37
- `git ls-files -cmo --exclude-standard`.split("\n")
38
- end
39
-
40
- def self.pexit(error)
41
- puts error
42
- exit 1
43
- end
6
+ extend Utils
44
7
 
45
- def self.main(remote, path, working_dir=nil)
8
+ def self.main(*extra_rsync_args)
46
9
  working_dir ||= Dir.pwd
47
10
  Dir.chdir working_dir
48
- pexit 'A remote and path must be specified' unless remote && path
49
- remote = Remote.new remote, path
50
- remote.scp git_aware_files
51
- relpath = method :relative_path
52
11
 
53
- puts "GIT FYNCY #{Time.now.ctime}"
54
- files_to_remove = Set.new
55
- begin
56
- Listen.to!('.') do |modified, added, removed|
57
- begin
58
- remote.scp git_aware_files
59
- rel_removed = removed.map(&relpath)
60
- files_to_remove.merge rel_removed
61
- files_to_remove.clear if remote.ssh_rm files_to_remove
62
- rescue => e
63
- puts e.inspect
64
- end
65
- end
66
- rescue SignalException
67
- exit 42
68
- ensure
69
- puts "\n"
12
+ synchronizer = Synchronizer.new extra_rsync_args
13
+ unless synchronizer.sync
14
+ pexit "\nGIT FYNCY: First remote command failed, exiting"
70
15
  end
16
+ synchronizer.listen
71
17
  end
72
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git-fyncy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-29 00:00:00.000000000 Z
12
+ date: 2014-11-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: listen
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &21773540 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,12 +21,7 @@ dependencies:
21
21
  version: '1.3'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ~>
28
- - !ruby/object:Gem::Version
29
- version: '1.3'
24
+ version_requirements: *21773540
30
25
  description: The funky git aware syncer.
31
26
  email:
32
27
  - ryan@ryanmcg.com
@@ -44,6 +39,10 @@ files:
44
39
  - bin/git-fyncy
45
40
  - git-fyncy.gemspec
46
41
  - lib/git-fyncy.rb
42
+ - lib/git-fyncy/remote.rb
43
+ - lib/git-fyncy/repo.rb
44
+ - lib/git-fyncy/synchronizer.rb
45
+ - lib/git-fyncy/utils.rb
47
46
  - lib/git-fyncy/version.rb
48
47
  homepage: https://github.com/RyanMcG/git-fyncy
49
48
  licenses:
@@ -58,21 +57,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
58
57
  - - ! '>='
59
58
  - !ruby/object:Gem::Version
60
59
  version: '0'
61
- segments:
62
- - 0
63
- hash: -1348310610279710193
64
60
  required_rubygems_version: !ruby/object:Gem::Requirement
65
61
  none: false
66
62
  requirements:
67
63
  - - ! '>='
68
64
  - !ruby/object:Gem::Version
69
65
  version: '0'
70
- segments:
71
- - 0
72
- hash: -1348310610279710193
73
66
  requirements: []
74
67
  rubyforge_project:
75
- rubygems_version: 1.8.23
68
+ rubygems_version: 1.8.11
76
69
  signing_key:
77
70
  specification_version: 3
78
71
  summary: Want to sync the working directories of your git directory with a remote