-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_git_functions.py
64 lines (44 loc) · 1.48 KB
/
common_git_functions.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
# Contains all the common functions used to interact with your git repository
import subprocess
import time
import os
import config_manager as config
BASE_PATH = config.get('general', 'folder_path')
BASE_GIT = 'git -C ' + BASE_PATH
LOG_FILE = config.get('general', 'log_file_name')
# Adds all and commits all using git commit command
def commit():
execute(BASE_GIT + ' add . ')
execute(BASE_GIT + ' commit -m "date: ' + str(time.time()) + '"')
return
# Appends a given string to a file
def update_file(filename, string_to_append):
execute('echo "[' + str(time.time()) + '] -> ' + string_to_append + '" >> ' + BASE_PATH + '/' + filename)
return
# Creates an empty folder (if it doesn't exist) and initialize a git repo in it
def init_git_repo():
if not os.path.exists(BASE_PATH):
os.makedirs(BASE_PATH)
execute(BASE_GIT + ' init ')
update_file('master_file.txt', '[master] First commit on master')
commit()
return
# Resets the branch on master
def reset():
execute(BASE_GIT + ' checkout master')
return
# Creates a new branch
def create_branch(branch_name):
execute(BASE_GIT + ' branch ' + branch_name)
return
# Checks out on a new branch
def checkout(branch_name):
execute(BASE_GIT + ' checkout ' + branch_name)
return
# Executes and log a command
def execute(command):
with open(LOG_FILE, 'a') as log_file:
log_file.write(command + '\n')
subprocess.Popen(command, shell=True)
time.sleep(1)
return