-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitccpy.py
139 lines (101 loc) · 3.93 KB
/
gitccpy.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
import sys
import subprocess
import shutil
CURRENT_PATH = os.getcwd().replace("\\", "/")
COMMIT_ID = 'HEAD'
CONFIGS = {
'LOCAL_PREXFIX' : '',
'DESTINATION_PATH' : None
}
######################################## [read args]
if len(sys.argv) > 1:
params = sys.argv[1:]
COMMIT_ID = params[0]
######################################## [read .gitccpy]
if not os.path.isfile(CURRENT_PATH + '/' + '.gitccpy'):
print('.gitccpy not found!')
exit()
with open(CURRENT_PATH + '/' + '.gitccpy', 'r') as f:
for line in f.readlines():
kv = [s.strip() for s in line.split('=')]
CONFIGS[kv[0]] = kv[1]
if CONFIGS['DESTINATION_PATH'] == None:
print('DESTINATION_PATH is not defined in .gitccpy')
exit()
######################################## [functions]
copied_count = 0
def clear_destination(folder):
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
def cp(from_path, to_path):
if not os.path.isfile(from_path): return
os.makedirs(os.path.dirname(to_path), exist_ok=True)
f1 = open(from_path, 'rb')
f2 = open(to_path, 'wb')
f2.write(f1.read())
f1.close()
f2.close()
def copy_paths(paths):
global copied_count
for p in paths:
if (not p.startswith(CONFIGS['LOCAL_PREXFIX'])): continue
copy_from = CURRENT_PATH + '/' + p
copy_to = CONFIGS['DESTINATION_PATH'] + '/' + p.replace(CONFIGS['LOCAL_PREXFIX'], '')
print("copying...", copy_from)
cp(copy_from, copy_to)
copied_count += 1
def get_last_commit():
if not os.path.isfile(CURRENT_PATH + '/' + '.gitccpy.lastcommit'): return None
with open(CURRENT_PATH + '/' + '.gitccpy.lastcommit', 'r') as f:
return f.read()
def store_last_commit(id):
with open(CURRENT_PATH + '/' + '.gitccpy.lastcommit', 'w') as f:
f.write(id)
######################################## [read last commit if exists, if user not specified custom commit id]
from_last_commit = False
if COMMIT_ID == 'HEAD':
id = get_last_commit()
if not id == None:
COMMIT_ID = id
from_last_commit = True
######################################## [execute git rev-list] [for retrive all commit ids of range <given-commit-id>^..HEAD]
git_command = ['git', 'rev-list', COMMIT_ID + ('' if from_last_commit else '^') + '..HEAD', '--reverse']
result = subprocess.run(git_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if result.returncode == 0:
all_commit_ids = result.stdout.splitlines()
else:
print("Error executing git rev-list command:")
print(result.stderr)
exit()
if (len(all_commit_ids) == 0):
print("No new commits.")
exit()
print('New', len(all_commit_ids), 'commits there...')
######################################## [store last commit id]
store_last_commit(all_commit_ids[-1])
######################################## [execute git diff-tree for all commits]
changed_paths = []
for id in all_commit_ids:
print("Reading changes for commit:", id)
git_command = ['git', 'diff-tree', '--no-commit-id', '--name-only', '-r', id]
result = subprocess.run(git_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if result.returncode == 0:
changed_paths.extend(result.stdout.splitlines())
else:
print("Error executing git diff-tree command:")
print(result.stderr)
######################################## [copy all changes]
clear_destination(CONFIGS['DESTINATION_PATH'])
copy_paths(changed_paths)
if copied_count == 0:
print('No changes in ', CONFIGS['LOCAL_PREXFIX'])
else:
print(copied_count, "files copied into", CONFIGS['DESTINATION_PATH'])