Skip to content

Commit 62273d4

Browse files
committed
Commit the script
1 parent 7da171f commit 62273d4

File tree

4 files changed

+129
-1
lines changed

4 files changed

+129
-1
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,6 @@ venv.bak/
102102

103103
# mypy
104104
.mypy_cache/
105+
store.pckl
106+
store.pckl.example
107+
.DS_Store

LICENSE

100644100755
File mode changed.

README.md

100644100755
+13-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
# git-archive
2-
Create a zip archive of files from current commit to latest commit you zipped
2+
Create a zip archive of files from current commit to latest commit you zipped.
3+
4+
It required `git` module to be installed, `pip3 install git`
5+
6+
## How to use
7+
1. Clone this repo
8+
2. Copy the `git_archive` folder to your git repo folder
9+
3. Navigate to `git_archive folder` and run `python3 archive.py`
10+
4. Enter the full commit hash you want to start from
11+
12+
## Notice
13+
* The new archive created in sub directory inside git_archive folder
14+
* Use `--flush` to initalize the storage file, you will be asked to enter a commit hash again

git_archive/archive.py

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import git
2+
import os
3+
import pickle
4+
import sys
5+
import time
6+
import zipfile
7+
8+
# join arguments to search them
9+
argus = " ".join(sys.argv)
10+
# script variables
11+
config_file_name = 'store.pckl'
12+
13+
# handle the config file
14+
class pickleh():
15+
def get(self):
16+
if not os.path.isfile(config_file_name):
17+
self.create()
18+
f = open(config_file_name, 'rb')
19+
config = pickle.load(f)
20+
f.close()
21+
return config
22+
def store(self, attr, value):
23+
config = self.get(self)
24+
config[attr] = value
25+
f = open(config_file_name, 'wb')
26+
pickle.dump(config, f)
27+
f.close()
28+
return config
29+
def attr(self, attr):
30+
config = self.get(self)
31+
return config.get(attr, '')
32+
def destroy():
33+
os.remove(config_file_name)
34+
def create():
35+
f = open(config_file_name, 'wb')
36+
pickle.dump({}, f)
37+
f.close()
38+
39+
# check config keys existence
40+
class initConfig():
41+
def __init__(self):
42+
if argus.find('--flush') > -1:
43+
pickleh.destroy()
44+
pickleh.create()
45+
46+
# check config keys exist
47+
self.latestCommit()
48+
49+
def ret(self):
50+
return pickleh.get(pickleh)
51+
52+
def latestCommit(self):
53+
if len(pickleh.attr(pickleh, 'last_commit_zipped')) != 40:
54+
latest = input("Please enter a commit hash to compare from: ")
55+
if len(latest) != 40:
56+
print('Error: Hash should be 40 charchters length 👊')
57+
self.latestCommit()
58+
else:
59+
pickleh.store(pickleh, 'last_commit_zipped', latest)
60+
61+
# main function
62+
class gitAmrography():
63+
def __init__(self):
64+
global repo
65+
global g
66+
global git_path
67+
global config
68+
69+
c = initConfig()
70+
config = c.ret()
71+
git_path = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
72+
repo = git.Repo(git_path)
73+
g = git.Git(git_path)
74+
75+
def gitLatestCommit(self):
76+
return str(repo.heads.master.commit.tree)
77+
78+
def gitDiff(self, branch1, branch2):
79+
format = '--name-only'
80+
commits = []
81+
differ = g.diff('%s..%s' % (branch1, branch2), format).split("\n")
82+
for line in differ:
83+
if len(line):
84+
commits.append(line)
85+
return commits
86+
87+
def package(self):
88+
latestcommit = self.gitLatestCommit()
89+
commits = self.gitDiff(latestcommit, config['last_commit_zipped'])
90+
if len(commits) > 0:
91+
self.packagePrepare(commits, latestcommit)
92+
else:
93+
print('No changes to package ✅')
94+
95+
def packagePrepare(self, commits, latestcommit):
96+
new_archive_dir = "archives/" + str(int(time.time()))
97+
if not os.path.exists(new_archive_dir):
98+
os.makedirs(new_archive_dir)
99+
zipf = zipfile.ZipFile(new_archive_dir + '/archive.zip', 'w', zipfile.ZIP_DEFLATED)
100+
self.packageAdd(commits, zipf)
101+
zipf.close()
102+
pickleh.store(pickleh, 'last_commit_zipped', latestcommit)
103+
104+
def packageAdd(self, commits, ziph):
105+
for commit in commits:
106+
file = git_path + "/" + commit
107+
file = "../" + commit
108+
if os.path.isfile(file):
109+
ziph.write(file)
110+
111+
112+
archive = gitAmrography()
113+
archive.package()

0 commit comments

Comments
 (0)