Skip to content

Commit d929741

Browse files
author
Jeremy Silver
committed
Simplify flattener
1 parent 3c1353c commit d929741

File tree

1 file changed

+10
-50
lines changed

1 file changed

+10
-50
lines changed

flatten.py

+10-50
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,7 @@
1616
DEVELOP = "develop-"
1717
DEVELOP_DEFAULT = "all develop branches"
1818

19-
DIFF_FORMAT = """
2019

21-
You can download a zip of this exercise [here](https://github.com/udacity/ud843-QuakeReport/archive/{number}-Exercise-{name}.zip), \
22-
and a zip of the solution [here](https://github.com/udacity/ud843-QuakeReport/archive/{number}-Solution-{name}.zip). \
23-
Also, you can find a visual summary of the solution [here](https://github.com/udacity/ud843-QuakeReport/compare/\
24-
{number}-Exercise-{name}...{number}-Solution-{name}).
25-
26-
"""
2720

2821

2922
def flatten(repo_dir, target_dir, student, develop_branches, remove_branches, links):
@@ -32,33 +25,16 @@ def flatten(repo_dir, target_dir, student, develop_branches, remove_branches, li
3225
if develop_branches == DEVELOP_DEFAULT:
3326
develop_branches = [branch for branch in repo.branches if DEVELOP in branch.name]
3427

35-
if remove_branches:
36-
remove_local_branches(repo, student, develop_branches)
37-
38-
flat = len(develop_branches) == 1
39-
40-
# print develop_branches
28+
remove_local_branches(repo, student, develop_branches)
4129

4230
try:
4331
temp_dir = tempfile.mkdtemp()
44-
try:
45-
# current_branch = repo.active_branch
46-
# print "Stashing"
47-
# repo.git.stash()
48-
49-
for develop in develop_branches:
50-
to_temp_dir(repo, repo_dir, develop, temp_dir, flat, links)
51-
if links:
52-
insert_diff_links(temp_dir)
53-
54-
copy_snapshots(repo, student, temp_dir, target_dir)
55-
finally:
56-
pass
57-
# if current_branch:
58-
# repo.git.checkout(current_branch)
59-
# print "Popping"
60-
# if repo.git.stash("list"):
61-
# repo.git.stash("pop")
32+
33+
34+
for develop in develop_branches:
35+
to_temp_dir(repo, repo_dir, develop, temp_dir)
36+
37+
copy_snapshots(repo, student, temp_dir, target_dir)
6238
finally:
6339
if os.path.exists(temp_dir):
6440
shutil.rmtree(temp_dir)
@@ -74,7 +50,7 @@ def remove_local_branches(repo, student, develop_branches):
7450
repo.git.branch(branch.name, "-D")
7551

7652

77-
def to_temp_dir(repo, repo_dir, develop, temp_dir, flat, links):
53+
def to_temp_dir(repo, repo_dir, develop, temp_dir):
7854
for rev in repo.git.rev_list(develop).split("\n"):
7955
commit = repo.commit(rev)
8056
branch_name = clean_commit_message(commit.message)
@@ -87,21 +63,12 @@ def to_temp_dir(repo, repo_dir, develop, temp_dir, flat, links):
8763
repo.git.checkout(commit)
8864
print "Saving snapshot of:", branch_name
8965
repo.git.clean("-fdx")
90-
if flat:
91-
target_dir = os.path.join(temp_dir, branch_name)
92-
else:
93-
folder_name = develop.name.split("-",1)[1]
94-
target_dir = os.path.join(temp_dir, folder_name, branch_name)
66+
folder_name = develop.name.split("-",1)[1]
67+
target_dir = os.path.join(temp_dir, folder_name, branch_name)
9568

9669
shutil.copytree(repo_dir, target_dir,
9770
ignore=shutil.ignore_patterns(*IGNORE_PATTERNS))
9871

99-
if links:
100-
with open(os.path.join(target_dir, "README.md"), "a") as readme:
101-
print branch_name
102-
number, _, name = branch_name.split("-")
103-
readme.write(DIFF_FORMAT.format(number=number, name=name))
104-
10572

10673
def clean_commit_message(message):
10774
first_line = message.split("\n")[0]
@@ -110,13 +77,6 @@ def clean_commit_message(message):
11077
return safe_message[:MAX_LENGTH] if len(safe_message) > MAX_LENGTH else safe_message
11178

11279

113-
def insert_diff_links(temp_dir):
114-
for item in os.listdir(temp_dir):
115-
number, _, name = item.split("-")
116-
with open(os.path.join(temp_dir, item, "README.md"), "a") as readme:
117-
readme.write(DIFF_FORMAT.format(number=number, name=name))
118-
119-
12080
def copy_snapshots(repo, student, temp_dir, target_dir):
12181
if target_dir == os.getcwd():
12282
repo.git.checkout(student)

0 commit comments

Comments
 (0)