forked from outlyerapp/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.py
More file actions
executable file
·62 lines (51 loc) · 1.35 KB
/
git.py
File metadata and controls
executable file
·62 lines (51 loc) · 1.35 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
from __future__ import print_function
import sys
import getopt
import re
import os
import subprocess
"""
Get the status of a git repo. Just change the WORKING_DIR.
"""
WORKING_DIR = '/update/my/path'
# Nagios return codes
STATE_OK = 0
STATE_WARNING = 1
STATE_CRITICAL = 2
STATE_UNKNOWN = 3
try:
opts, args = getopt.getopt(sys.argv[1:], "hg:", ["git-dir="])
except getopt.GetoptError:
print('git.py -g <git repo>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('check_git.py -g <git repo>')
print('Example: ./git.py -g /usr/local/git/reponame')
sys.exit()
elif opt in ("-g", "--git-dir"):
WORKING_DIR = arg
revlist = ['git', 'rev-list', 'HEAD..origin/master', '--count']
rc = 1000
output = ''
try:
rc = 0
output = subprocess.check_output(revlist, cwd=WORKING_DIR, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError, e:
rc = e.returncode
output = e.output
if rc == 0:
count = int(output)
if count > 5:
print("{} Commits available!".format(count))
sys.exit(STATE_CRITICAL)
elif count > 0:
print("{} Commits available!".format(count))
sys.exit(STATE_WARNING)
else:
print("OK. Git Repo is up to date with origin!")
sys.exit(STATE_OK)
elif rc > 0:
print(output, end='')
sys.exit(STATE_UNKNOWN)