-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit.py
executable file
·64 lines (59 loc) · 1.87 KB
/
git.py
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
63
64
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Functions for finding files and dirs
tested with python 2.7
"""
import sys
import subprocess
import sh
import logging
import tools
logger = logging.getLogger("git")
logger.debug("loading git module")
def init(dir = '.', add_all = False):
"""
Initializes a new git repo
"""
with tools.DirHop(dir) as d:
sh.git.init()
if add_all:
sh.git.add('.')
sh.git.commit('-m', 'first commit')
def parse_git(attribute):
"""
Check the current git repo for one of the following items
attribute = "hash"
attribute = "hash_short"
attribute = "branch"
"""
command = None
if attribute == "hash":
command = ['git', 'rev-parse', 'HEAD']
elif attribute == "hash_short":
command = ['git', 'rev-parse', '--short', 'HEAD']
elif attribute == "branch":
command = ['git', 'rev-parse', '--abbrev-ref', 'HEAD']
if command != None:
try:
return(subprocess.check_output(command).strip()) # python 2.7+
except subprocess.CalledProcessError:
logger.error('Git branch is not configured. Exiting script.')
sys.exit()
def print_iter(iterable):
"""
basic printing of every item in an iterable object
"""
for item in iterable: logger.debug(item)
def validate_branch(allowed = ('master', 'production')):
try:
current_branch = parse_git(attribute = "branch")
if current_branch not in allowed:
logger.error("Current branch is not allowed! Branch is: {0}.".format(current_branch))
logger.error("Allowed branches are:")
for item in iterable: logger.error(item)
logger.error("Exiting...")
sys.exit()
except subprocess.CalledProcessError:
logger.error('Git branch is not configured. Exiting script.')
sys.exit()