Class: Git

Inherits:
Object
  • Object
show all
Defined in:
lib/git-flattr.rb

Class Method Summary collapse

Class Method Details

.command(cmd, opts = []) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/git-flattr.rb', line 12

def command cmd, opts = []
  opts = [opts].flatten.map {|s| escape(s) }.join(' ')
  git_cmd = "git #{cmd} #{opts} 2>&1"
  result = `#{git_cmd}`.chomp
  if $?.to_i == 0
    return result
  else
    return false
  end
end

.commit(sha) ⇒ Object



41
42
43
44
# File 'lib/git-flattr.rb', line 41

def commit sha
  #git log -1 --format=%H 53bebbe
  command("log", ["-1", "--format=%H", sha])
end

.config(name) ⇒ Object



28
29
30
# File 'lib/git-flattr.rb', line 28

def config name
  command('config', ['--get', name])
end

.current_repo_github?Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/git-flattr.rb', line 36

def current_repo_github?
  origin = config 'remote.origin.url'
  github_repo? origin
end

.escape(s) ⇒ Object



23
24
25
26
# File 'lib/git-flattr.rb', line 23

def escape(s)
  escaped = s.to_s.gsub('\'', '\'\\\'\'')
  %Q{"#{escaped}"}
end

.github_repo?(url) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/git-flattr.rb', line 53

def github_repo? url

  if url.match %r{^https://[-\w]+@github\.com}
    match = url.match(%r{^https://[-\w]+@github\.com/([-\w]+)/([-\.\w]+)\.git$})
  elsif url.match %r{^git@github\.com:}
    match = url.match(%r{^git@github\.com:([-\w]+)/([-\.\w]+)\.git$})
  elsif url.match %r{^git://github\.com/}
    match = url.match(%r{^git://github\.com/([-\w]+)/([-\.\w]+)\.git$})
  elsif url.match %r{^https://github\.com/}
    match = url.match(%r{^https://github\.com/([-\w]+)/([-\.\w]+)\.git$})
  end

  if !match.nil?
    @github_url = "https://github.com/#{match[1]}/#{match[2]}"
    return true
  else
    @github_url = nil
    return false
  end
end

.github_urlObject



46
47
48
49
50
51
# File 'lib/git-flattr.rb', line 46

def github_url
  origin = config 'remote.origin.url'
  if github_repo? origin
    return @github_url
  end
end

.set_config(name, value) ⇒ Object



32
33
34
# File 'lib/git-flattr.rb', line 32

def set_config name, value
  command('config', ['--global', '--add', name, value])
end

.valid?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/git-flattr.rb', line 74

def valid?

  git_opts = {
    :dir    => "#{Dir.pwd}/.git",
    :config => "#{Dir.pwd}/.git/config"
  }

  unless File.exists?(git_opts[:dir])
    error "Don't seem to be a git repository"
  end

  unless File.exists?(git_opts[:config])
    error "Git .config file not found"
  end

  unless Git.current_repo_github?
    error "Not a GitHub repository"
  end
end