git-sleep 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
 
8
8
  `gem install git-sleep`
9
9
 
10
- to be used in conjunction with [website goes here](#)
10
+ to be used in conjunction with [gitsleep.com](http://www.gitsleep.com)
11
11
 
12
12
  ## usage:
13
13
 
@@ -11,24 +11,22 @@ help_text = "Available commands:
11
11
  git sleep init"
12
12
 
13
13
  CURRENT_PATH = File.expand_path(".")
14
- SAMPLE_FILE_NAME = "#{CURRENT_PATH}/.git/hooks/pre-commit.sample"
15
- NON_SAMPLE_FILE_NAME = "#{CURRENT_PATH}/.git/hooks/pre-commit"
14
+ PATH_TO_THIS_REPOS_HOOKS = "#{CURRENT_PATH}/.git/hooks"
16
15
 
17
- def install_hook
18
-
19
- path_to_hook = File.expand_path("../../hooks/pre-commit", __FILE__)
20
- hook_code = File.read(path_to_hook)
21
-
22
- File.open(SAMPLE_FILE_NAME, "w") do |f|
23
- f.write hook_code
16
+ def install_hook(event)
17
+ hook_code = File.read(File.expand_path("../../hooks/#{event}", __FILE__))
18
+ path_to_new_hook = File.expand_path("#{PATH_TO_THIS_REPOS_HOOKS}/#{event}")
19
+ if File.exists? path_to_new_hook
20
+ puts "You already have a #{event} hook and we don't want to overwrite it..."
21
+ print "Is it okay? [y/n] "
22
+ ok = $stdin.gets.chomp
23
+ return unless ok == "y"
24
24
  end
25
- successful_rename = File.rename SAMPLE_FILE_NAME, NON_SAMPLE_FILE_NAME
26
- if successful_rename.zero?
27
- puts "Successfully installed hook. Enjoy your new sense of balance."
28
- else
29
- puts "Sorry, the hook failed to install..." # will this ever happen?
25
+ File.open(path_to_new_hook, "w") do |f|
26
+ f.write hook_code
30
27
  end
31
-
28
+ `chmod a+x #{path_to_new_hook}`
29
+ puts "Hook installed and made executable"
32
30
  end
33
31
 
34
32
 
@@ -38,32 +36,35 @@ when 0
38
36
  when 1
39
37
  case ARGV.first.downcase.to_sym
40
38
  when :init
41
-
42
- if Dir.entries(CURRENT_PATH).include?(".git")
43
- puts "This is a git repo, so I'll now install the hook"
44
-
45
- if File.exists? SAMPLE_FILE_NAME
46
- install_hook()
47
- elsif File.exists? NON_SAMPLE_FILE_NAME
48
- puts "You already have a pre-commit hook and we don't want to overwrite it..."
49
- print "Is it okay? [y/n] "
50
- ok = $stdin.gets.chomp
51
- install_hook() if ok == "y"
39
+
40
+ if !Dir.entries(CURRENT_PATH).include? ".git"
41
+ puts "This is not a git repo. Would you like to initialize one?"
42
+ print "[y/n] "
43
+ ok = $stdin.gets.chomp
44
+ if ok == "y"
45
+ `git init`
52
46
  else
53
- # the file just doesn't exist
54
- # this probably won't work because it won't be executable...?
55
- install_hook()
47
+ exit 0
56
48
  end
57
- else
58
- puts "This is not a git repo!"
59
49
  end
50
+
51
+ puts "Install the pre-commit hook that prevents commits when you haven't sleep enough?"
52
+ print "Is it okay? [y/n] "
53
+ ok = $stdin.gets.chomp
54
+ install_hook("pre-commit") if ok == "y"
55
+
56
+ puts "Install the post-commit hook that adds git notes about your sleep data?"
57
+ print "Is it okay? [y/n] "
58
+ ok = $stdin.gets.chomp
59
+ install_hook("post-commit") if ok =="y"
60
+
60
61
  when :authorize
61
62
  puts "Visit #{GitSleep::OUR_SITE} to get the necessary information"
62
63
  puts "what is your xid?"
63
64
  print "> "
64
65
  xid = $stdin.gets.chomp
65
66
  netrc.new_item_prefix = "\n# jawbone xid\n"
66
- netrc["gitsleep.com"] = xid, "no password"
67
+ netrc["gitsleep.com"] = xid, "no_password"
67
68
  netrc.save
68
69
  when :"-v"
69
70
  puts "Git Sleep v#{GitSleep::VERSION}"
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'netrc'
5
+ require 'git-sleep'
6
+ require 'httparty'
7
+
8
+ netrc = Netrc.read(GitSleep::NETRC_PATH)
9
+ xid = netrc["gitsleep.com"].first
10
+
11
+ raise "Need to `git sleep authorize`" if xid.nil?
12
+
13
+
14
+ begin
15
+ response = HTTParty.get(
16
+ "http://www.gitsleep.com/api/need_sleep",
17
+ :body => {
18
+ :xid => xid
19
+ }
20
+ )
21
+ rescue Exception => e
22
+ puts "Can't connect to gitsleep.com"
23
+ exit 0
24
+ end
25
+
26
+ if response.response.code.to_i == 200
27
+ data = response.parsed_response
28
+
29
+ `git notes --ref=gitsleep add -m "On #{data['sleep24']} hours sleep"`
30
+ remotes = `git remote`.split("\n")
31
+ remotes.each do |remote|
32
+ `git push #{remote} refs/notes/* 2> /dev/null`
33
+ end
34
+
35
+ elsif response.response.code.to_i == 401
36
+ puts "Must first authorize at #{GitSleep::OUR_SITE}"
37
+ puts "Then run `git sleep authorize`"
38
+ else
39
+ # could not communicate with our server
40
+ # uh oh!
41
+ end
42
+
@@ -10,12 +10,19 @@ xid = netrc["gitsleep.com"].first
10
10
 
11
11
  raise "Need to `git sleep authorize`" if xid.nil?
12
12
 
13
- response = HTTParty.get(
14
- "http://www.gitsleep.com/api/need_sleep",
15
- :body => {
16
- :xid => xid
17
- }
18
- )
13
+ begin
14
+ response = HTTParty.get(
15
+ "http://www.gitsleep.com/api/need_sleep",
16
+ :body => {
17
+ :xid => xid
18
+ }
19
+ )
20
+ rescue Exception => e
21
+ puts "Can't connect to gitsleep.com"
22
+ exit 0
23
+ end
24
+
25
+
19
26
 
20
27
  if response.response.code.to_i == 200
21
28
  data = response.parsed_response
@@ -1,6 +1,6 @@
1
1
  module GitSleep
2
- VERSION = "0.1.0"
3
- LAST_UPDATED = "2013-08-19"
2
+ VERSION = "0.1.1"
3
+ LAST_UPDATED = "2013-08-20"
4
4
  NETRC_PATH = File.expand_path('~/.netrc')
5
5
  OUR_SITE = "http://www.gitsleep.com"
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git-sleep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-08-19 00:00:00.000000000 Z
14
+ date: 2013-08-20 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: httparty
@@ -58,6 +58,7 @@ files:
58
58
  - ./README.md
59
59
  - ./git-sleep.gemspec
60
60
  - ./.gitignore
61
+ - ./hooks/post-commit
61
62
  - ./hooks/pre-commit
62
63
  - ./bin/git-sleep
63
64
  - ./lib/git-sleep.rb