git-sleep 0.0.2 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
data/bin/git-sleep CHANGED
@@ -10,15 +10,50 @@ help_text = "Available commands:
10
10
  git sleep authorize
11
11
  git sleep init"
12
12
 
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"
16
+
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
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?
30
+ end
31
+
32
+ end
33
+
34
+
13
35
  case ARGV.length
14
36
  when 0
15
37
  puts help_text
16
38
  when 1
17
39
  case ARGV.first.downcase.to_sym
18
40
  when :init
19
- current_path = File.expand_path(".")
20
- if Dir.entries(current_path).include?(".git")
41
+
42
+ if Dir.entries(CURRENT_PATH).include?(".git")
21
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"
52
+ else
53
+ # the file just doesn't exist
54
+ # this probably won't work because it won't be executable...?
55
+ install_hook()
56
+ end
22
57
  else
23
58
  puts "This is not a git repo!"
24
59
  end
@@ -27,12 +62,11 @@ when 1
27
62
  puts "what is your xid?"
28
63
  print "> "
29
64
  xid = $stdin.gets.chomp
30
- puts "what is your token?"
31
- print "> "
32
- token = $stdin.gets.chomp
33
- netrc.new_item_prefix = "\n# jawbone xid and sleepytime token\n"
34
- netrc["sleepytime.com"] = xid, token
65
+ netrc.new_item_prefix = "\n# jawbone xid\n"
66
+ netrc["sleepytime.com"] = xid, "no password"
35
67
  netrc.save
68
+ when :"-v"
69
+ puts "Git Sleep v#{GitSleep::VERSION}"
36
70
  else # unrecognized command
37
71
  puts help_text
38
72
  end
data/git-sleep.gemspec CHANGED
@@ -11,6 +11,7 @@ Gem::Specification.new do |git_sleep|
11
11
  git_sleep.files = Dir[
12
12
  './*.{md,gemspec}',
13
13
  './.gitignore',
14
+ './hooks/*',
14
15
  './bin/*',
15
16
  './lib/*'
16
17
  ]
data/hooks/pre-commit ADDED
@@ -0,0 +1,36 @@
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["sleepytime.com"].first
10
+
11
+ raise "Need to `git sleep authorize`" if xid.nil?
12
+
13
+ response = HTTParty.get(
14
+ "http://localhost:3000/api/need_sleep",
15
+ :body => {
16
+ :xid => xid
17
+ }
18
+ )
19
+
20
+ if response.response.code.to_i == 200
21
+ data = response.parsed_response
22
+ if data["can_commit"]
23
+ puts "Great job #{ENV['USER']}! You slept #{data['sleep24']} hours in the last 24 hours"
24
+ else
25
+ puts "You have only slept #{data["sleep24"]} hours in the last 24 hours!!"
26
+ puts "You'll have to git some sleep #{ENV["USER"]} before you can commit again"
27
+ puts "(if you're sure you want to commit right now, run: git commit --no-verify)"
28
+ exit(1)
29
+ end
30
+ elsif response.response.code.to_i == 401
31
+ puts "Must first authorize at #{GitSleep::OUR_SITE}"
32
+ puts "Then run `git sleep authorize`"
33
+ else
34
+ # could not communicate with our server
35
+ # uh oh!
36
+ end
data/lib/git-sleep.rb CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'httparty'
3
3
 
4
4
  module GitSleep
5
- VERSION = "0.0.2"
5
+ VERSION = "0.0.15"
6
6
  LAST_UPDATED = "2013-08-11"
7
7
  NETRC_PATH = File.expand_path('~/.netrc')
8
8
  OUR_SITE = "http://sleepytime.com"
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.0.2
4
+ version: 0.0.15
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -56,6 +56,7 @@ files:
56
56
  - ./README.md
57
57
  - ./git-sleep.gemspec
58
58
  - ./.gitignore
59
+ - ./hooks/pre-commit
59
60
  - ./bin/git-sleep
60
61
  - ./lib/git-sleep.rb
61
62
  - bin/git-sleep