/
git_cwd_info
executable file
·44 lines (36 loc) · 1.16 KB
/
git_cwd_info
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/ruby
# -*- coding: utf-8 -*-
# The methods that get called more than once are memoized.
def git_repo_path
@git_repo_path ||= begin
relative_path = `git rev-parse --git-dir 2>/dev/null`.strip
File.expand_path(relative_path) unless relative_path.empty?
end
end
def in_git_repo
!git_repo_path.nil? &&
git_repo_path != '/.git' &&
git_repo_path != '~' &&
git_repo_path != "#{ENV['HOME']}/.git"
end
def git_parse_branch
@git_parse_branch ||= File.read("#{git_repo_path}/HEAD").strip.scan(/refs\/heads\/(.*)$/).flatten.first
end
def git_head_commit_id
`git rev-parse --short HEAD 2>/dev/null`.strip
end
def git_cwd_dirty
" %{\e[90m%}✗%{\e[0m%}" unless git_repo_path == '.' || `git ls-files -m`.strip.empty?
end
def rebasing_etc
if File.exists?(File.join(git_repo_path, 'BISECT_LOG'))
"+bisect"
elsif File.exists?(File.join(git_repo_path, 'MERGE_HEAD'))
"+merge"
elsif %w[rebase rebase-apply rebase-merge ../.dotest].any? {|d| File.exists?(File.join(git_repo_path, d)) }
"+rebase"
end
end
if in_git_repo
print " %{\e[34m%}#{git_parse_branch}%{\e[90m%}@%{\e[33m%}#{git_head_commit_id}%{\e[0m%}#{rebasing_etc}#{git_cwd_dirty}"
end