git-fyncy 0.6.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/git-fyncy CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'git-fyncy'
3
3
 
4
- GitFyncy::main(*ARGV)
4
+ GitFyncy.main(ARGV)
@@ -0,0 +1,11 @@
1
+ module GitFyncy
2
+ class Logger
3
+ def initialize(background)
4
+ @log = !(STDOUT.tty? && background)
5
+ end
6
+
7
+ def log(thing)
8
+ puts thing if @log
9
+ end
10
+ end
11
+ end
@@ -4,25 +4,32 @@ module GitFyncy
4
4
  class Remote
5
5
  include Utils
6
6
 
7
- def initialize(remote, path, rsync_args)
7
+ def initialize(logger, remote, path, rsync_args)
8
+ @logger = logger
8
9
  @remote = remote
9
10
  @path = slashify path
10
11
  @rsync_flags = rsync_args.join(" ")
12
+
13
+ # COMMANDS
14
+ @rm_cmd = "cd #{@path}; rm -f %{paths}"
15
+ @rm_cmd = "ssh #{@remote} '#{@rm_cmd}'" if @remote
16
+ path = @remote ? "#{@remote}:#{@path}" : @path
17
+ @rsync_cmd = "rsync -zpR --checksum #{@rsync_flags} %{paths} #{path}"
11
18
  end
12
19
 
13
- def command(cmd)
14
- puts cmd.length < 80 ? cmd : cmd[0...77] + '...'
20
+ def command(cmd, paths)
21
+ return if paths.empty?
22
+ cmd = cmd % {paths: paths.to_a.join(' ')}
23
+ @logger.log cmd
15
24
  system cmd
16
25
  end
17
26
 
18
- def scp(paths, rsync_args=[])
19
- return if paths.empty?
20
- command "rsync -zpR --checksum #{@rsync_flags} #{paths.to_a.join ' '} #{@remote}:#{@path}"
27
+ def rsync(paths)
28
+ command @rsync_cmd, paths
21
29
  end
22
30
 
23
- def ssh_rm(paths)
24
- return if paths.empty?
25
- command "ssh #{@remote} 'cd #{@path}; rm -f #{paths.to_a.join ' '}'"
31
+ def rm(paths)
32
+ command @rm_cmd, paths
26
33
  end
27
34
  end
28
35
  end
@@ -15,13 +15,13 @@ module GitFyncy
15
15
  end
16
16
 
17
17
  module ClassMethods
18
- [:remote_name, :url].each do |name|
18
+ GIT_CONFIG.each do |name, key|
19
19
  define_method("#{name}_from_config") do
20
- get_stdout_str "git config --get #{GIT_CONFIG.fetch(name)}"
20
+ get_stdout_str "git config --get #{key}"
21
21
  end
22
22
 
23
23
  define_method("configure_fyncy_#{name}") do |val|
24
- system "git config --add #{GIT_CONFIG.fetch(name)} '#{val}'"
24
+ system "git config --add #{key} '#{val}'"
25
25
  end
26
26
  end
27
27
  end
@@ -46,6 +46,11 @@ module GitFyncy
46
46
 
47
47
  def self.host_and_path_from_url(url)
48
48
  md = SSH_URL_REGEX.match url
49
+ user = nil
50
+ host = nil
51
+ remote = nil
52
+ path = url
53
+
49
54
  if md
50
55
  user = md[1]
51
56
  host = md[2]
@@ -56,15 +61,15 @@ module GitFyncy
56
61
  path = md[4]
57
62
  else
58
63
  md = SCP_REGEX.match url
59
- unless md
60
- 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."
64
+ if md
65
+ user = md[1]
66
+ host = md[2]
67
+ path = slashify remove_trailing_git md[3]
61
68
  end
62
- user = md[1]
63
- host = md[2]
64
- path = slashify remove_trailing_git md[3]
65
69
  end
66
70
 
67
- ["#{user}#{host}", path]
71
+ remote = "#{user}#{host}" if user && host
72
+ [remote, path]
68
73
  end
69
74
 
70
75
  def self.host_and_path_for_remote(remote_name)
@@ -77,14 +82,14 @@ module GitFyncy
77
82
  prompt = <<-EOS
78
83
  No remote configured. git fyncy looks under the fyncy section of your git
79
84
  config for a remote name or a url (i.e. at fyncy.remote and fyncy.url). If a
80
- remote name is specified, that remote's url be used with any ".git" suffix
81
- removed.
85
+ remote name is specified, that remote's url will be used with the ".git" suffix
86
+ removed if it exists.
82
87
 
83
88
  Enter a remote name to use (or press return to use #{DEFAULT_REMOTE}):
84
89
  EOS
85
90
  print prompt.chomp
86
91
 
87
- remote_name = gets.chomp
92
+ remote_name = STDIN.gets.chomp
88
93
  remote_name = DEFAULT_REMOTE if remote_name.empty?
89
94
  if remote_defined?(remote_name)
90
95
  puts
@@ -105,7 +110,7 @@ EOS
105
110
  return host_and_path_from_url url if url
106
111
 
107
112
  remote_name = remote_name_from_config
108
- remote_name = prompt_user_for_remote_name unless remote_name
113
+ remote_name = prompt_user_for_remote_name if STDIN.tty? && !remote_name
109
114
  res = nil
110
115
  res = host_and_path_for_remote remote_name if remote_name
111
116
  res = host_and_path_for_remote DEFAULT_REMOTE unless res
@@ -1,24 +1,30 @@
1
1
  require 'git-fyncy/utils'
2
2
  require 'git-fyncy/repo'
3
3
  require 'git-fyncy/remote'
4
+ require 'git-fyncy/logger'
4
5
  require 'listen'
5
6
 
6
7
  module GitFyncy
7
8
  class Synchronizer
8
9
  include Utils
9
10
 
10
- def initialize(extra_rsync_args)
11
+ def initialize(background, extra_rsync_args)
11
12
  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
13
+ pexit 'A remote and path must be specified' unless path
14
+ @background = background
15
+ @logger = Logger.new background
16
+ @remote = Remote.new @logger, remote, path, extra_rsync_args
14
17
  end
15
18
 
16
19
  def sync
17
- @remote.scp Repo.git_aware_files
20
+ @remote.rsync Repo.git_aware_files
18
21
  end
19
22
 
23
+ # Listen to file changes, forking to the background first if background is
24
+ # true.
20
25
  def listen
21
- puts "GIT FYNCY: Listening @ #{Time.now.ctime}"
26
+ daemonize if @background
27
+ @logger.log "GIT FYNCY: Listening @ #{Time.now.ctime}"
22
28
  relpath = method :relative_path
23
29
  files_to_remove = Set.new
24
30
  begin
@@ -27,20 +33,29 @@ module GitFyncy
27
33
  self.sync
28
34
  rel_removed = removed.map(&relpath)
29
35
  files_to_remove.merge rel_removed
30
- files_to_remove.clear if @remote.ssh_rm files_to_remove
36
+ files_to_remove.clear if @remote.rm files_to_remove
31
37
  rescue => e
32
- puts e.inspect
38
+ @logger.log e.inspect
33
39
  end
34
40
  end
35
41
  rescue SignalException
36
42
  exit 42
37
43
  ensure
38
- puts "\n"
44
+ @logger.log "\n"
39
45
  end
40
46
  end
41
47
 
42
48
  private
43
49
 
50
+ def daemonize
51
+ pid = fork
52
+ if pid # parent
53
+ File.write ".git-fyncy-pid", pid
54
+ Process.detach pid
55
+ exit 0
56
+ end
57
+ end
58
+
44
59
  def relative_path(path)
45
60
  path.slice! slashify(Dir.pwd)
46
61
  path
@@ -1,3 +1,3 @@
1
1
  module GitFyncy
2
- VERSION = "0.6.2"
2
+ VERSION = "0.7.0"
3
3
  end
data/lib/git-fyncy.rb CHANGED
@@ -1,23 +1,49 @@
1
1
  require "git-fyncy/version"
2
- require "git-fyncy/synchronizer"
2
+ require 'git-fyncy/synchronizer'
3
3
  require 'listen'
4
4
 
5
5
  module GitFyncy
6
6
  extend Utils
7
+ PID_FNAME = ".git-fyncy-pid".freeze
7
8
 
8
- def self.main(*extra_rsync_args)
9
- working_dir = `git rev-parse --show-toplevel`.chomp
9
+ def self.already_running?
10
+ File.exists? PID_FNAME
11
+ end
10
12
 
11
- if working_dir.empty?
12
- pexit "Must be in a git repository"
13
+ def self.kill
14
+ if already_running?
15
+ pid = File.read(PID_FNAME).to_i
16
+ if Process.kill "TERM", pid
17
+ File.delete PID_FNAME
18
+ else
19
+ pexit "Failed to kill process with id #{pid}"
20
+ end
21
+ else
22
+ pexit "No process to kill. No #{PID_FNAME} file."
13
23
  end
24
+ end
25
+
26
+ FLAGS = %w(--fork --kill).to_set.freeze
27
+ def self.main(args)
28
+ working_dir = `git rev-parse --show-toplevel`.chomp
29
+
30
+ pexit 'Must be in a git repository' if working_dir.empty?
14
31
 
15
32
  Dir.chdir working_dir
33
+ return kill if args.include? "--kill"
34
+ if already_running?
35
+ pexit "#{PID_FNAME} exists. Is git fyncy already running?"
36
+ end
16
37
 
17
- synchronizer = Synchronizer.new extra_rsync_args
38
+ rsync_args = args.reject { |arg| FLAGS.include? arg }
39
+ # Listen in the background if one of the args was '--fork'.
40
+ background = args.include? "--fork"
41
+
42
+ synchronizer = Synchronizer.new background, rsync_args
18
43
  unless synchronizer.sync
19
44
  pexit "\nGIT FYNCY: First remote command failed, exiting"
20
45
  end
46
+
21
47
  synchronizer.listen
22
48
  end
23
49
  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.6.2
4
+ version: 0.7.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: 2015-02-03 00:00:00.000000000 Z
12
+ date: 2015-03-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: listen
16
- requirement: &15747200 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,7 +21,12 @@ dependencies:
21
21
  version: '1.3'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *15747200
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
25
30
  description: The funky git aware syncer.
26
31
  email:
27
32
  - ryan@ryanmcg.com
@@ -39,6 +44,7 @@ files:
39
44
  - bin/git-fyncy
40
45
  - git-fyncy.gemspec
41
46
  - lib/git-fyncy.rb
47
+ - lib/git-fyncy/logger.rb
42
48
  - lib/git-fyncy/remote.rb
43
49
  - lib/git-fyncy/repo.rb
44
50
  - lib/git-fyncy/synchronizer.rb
@@ -65,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
71
  version: '0'
66
72
  requirements: []
67
73
  rubyforge_project:
68
- rubygems_version: 1.8.11
74
+ rubygems_version: 1.8.25
69
75
  signing_key:
70
76
  specification_version: 3
71
77
  summary: Want to sync the working directories of your git directory with a remote