Skip to content

Commit 1eef8f4

Browse files
authored
Merge pull request #122 from kellda/squash
Implement squash command
2 parents adbc740 + 143278a commit 1eef8f4

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

homu/main.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class PullReqState:
141141
num = 0
142142
priority = 0
143143
rollup = 0
144+
squash = False
144145
title = ''
145146
body = ''
146147
head_ref = ''
@@ -347,7 +348,7 @@ def get_test_on_fork_repo(self):
347348
def save(self):
348349
db_query(
349350
self.db,
350-
'INSERT OR REPLACE INTO pull (repo, num, status, merge_sha, title, body, head_sha, head_ref, base_ref, assignee, approved_by, priority, try_, rollup, delegate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', # noqa
351+
'INSERT OR REPLACE INTO pull (repo, num, status, merge_sha, title, body, head_sha, head_ref, base_ref, assignee, approved_by, priority, try_, rollup, squash, delegate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', # noqa
351352
[
352353
self.repo_label,
353354
self.num,
@@ -363,6 +364,7 @@ def save(self):
363364
self.priority,
364365
self.try_,
365366
self.rollup,
367+
self.squash,
366368
self.delegate,
367369
])
368370

@@ -723,6 +725,20 @@ def parse_commands(body, username, user_id, repo_label, repo_cfg, state,
723725

724726
state.save()
725727

728+
elif command.action == 'squash':
729+
if not _try_auth_verified():
730+
continue
731+
state.squash = True
732+
733+
state.save()
734+
735+
elif command.action == 'unsquash':
736+
if not _try_auth_verified():
737+
continue
738+
state.squash = False
739+
740+
state.save()
741+
726742
elif command.action == 'force' and realtime:
727743
if not _try_auth_verified():
728744
continue
@@ -1005,6 +1021,29 @@ def create_merge(state, repo_cfg, branch, logger, git_cfg,
10051021
desc = 'Auto-squashing failed'
10061022
comment = ''
10071023
ok = False
1024+
if state.squash:
1025+
try:
1026+
merge_base_sha = subprocess.check_output(
1027+
git_cmd(
1028+
'merge-base',
1029+
base_sha,
1030+
state.head_sha)).decode('ascii').strip()
1031+
git_editor = os.environ['GIT_EDITOR']
1032+
os.environ['GIT_EDITOR'] = "sed -i '2,/^$/s/^pick\b/s/'"
1033+
utils.logged_call(git_cmd(
1034+
'-c',
1035+
'user.name=' + git_cfg['name'],
1036+
'-c',
1037+
'user.email=' + git_cfg['email'],
1038+
'rebase',
1039+
'-i',
1040+
'--onto',
1041+
merge_base_sha, base_sha))
1042+
os.environ['GIT_EDITOR'] = git_editor
1043+
except subprocess.CalledProcessError:
1044+
desc = 'Squashing failed'
1045+
comment = ''
1046+
ok = False
10081047

10091048
if ok:
10101049
utils.logged_call(git_cmd('checkout', '-B', branch, base_sha))
@@ -1736,6 +1775,7 @@ def main():
17361775
priority INTEGER,
17371776
try_ INTEGER,
17381777
rollup INTEGER,
1778+
squash INTEGER,
17391779
delegate TEXT,
17401780
UNIQUE (repo, num)
17411781
)''')
@@ -1780,6 +1820,10 @@ def main():
17801820
db_query(db, 'SELECT treeclosed_src FROM repos LIMIT 0')
17811821
except sqlite3.OperationalError:
17821822
db_query(db, 'ALTER TABLE repos ADD COLUMN treeclosed_src TEXT')
1823+
try:
1824+
db_query(db, 'SELECT squash FROM pull LIMIT 0')
1825+
except sqlite3.OperationalError:
1826+
db_query(db, 'ALTER TABLE pull ADD COLUMN squash INT')
17831827

17841828
for repo_label, repo_cfg in cfg['repo'].items():
17851829
repo_cfgs[repo_label] = repo_cfg
@@ -1797,9 +1841,9 @@ def main():
17971841

17981842
db_query(
17991843
db,
1800-
'SELECT num, head_sha, status, title, body, head_ref, base_ref, assignee, approved_by, priority, try_, rollup, delegate, merge_sha FROM pull WHERE repo = ?', # noqa
1844+
'SELECT num, head_sha, status, title, body, head_ref, base_ref, assignee, approved_by, priority, try_, rollup, squash, delegate, merge_sha FROM pull WHERE repo = ?', # noqa
18011845
[repo_label])
1802-
for num, head_sha, status, title, body, head_ref, base_ref, assignee, approved_by, priority, try_, rollup, delegate, merge_sha in db.fetchall(): # noqa
1846+
for num, head_sha, status, title, body, head_ref, base_ref, assignee, approved_by, priority, try_, rollup, squash, delegate, merge_sha in db.fetchall(): # noqa
18031847
state = PullReqState(num, head_sha, status, db, repo_label, mergeable_que, gh, repo_cfg['owner'], repo_cfg['name'], repo_cfg.get('labels', {}), repos, repo_cfg.get('test-on-fork')) # noqa
18041848
state.title = title
18051849
state.body = body
@@ -1811,6 +1855,7 @@ def main():
18111855
state.priority = int(priority)
18121856
state.try_ = bool(try_)
18131857
state.rollup = rollup
1858+
state.squash = bool(squash)
18141859
state.delegate = delegate
18151860
builders = []
18161861
if merge_sha:

homu/parse_issue_comment.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ def rollup(cls, rollup_value):
7070
command.rollup_value = rollup_value
7171
return command
7272

73+
@classmethod
74+
def squash(cls):
75+
return cls('squash')
76+
77+
@classmethod
78+
def unsquash(cls):
79+
return cls('unsquash')
80+
7381
@classmethod
7482
def force(cls):
7583
return cls('force')
@@ -227,6 +235,12 @@ def parse_issue_comment(username, body, sha, botname, hooks=[]):
227235
rollup_value = WORDS_TO_ROLLUP[word]
228236
commands.append(IssueCommentCommand.rollup(rollup_value))
229237

238+
elif word == 'squash':
239+
commands.append(IssueCommentCommand.squash())
240+
241+
elif word == 'squash-':
242+
commands.append(IssueCommentCommand.unsquash())
243+
230244
elif word == 'force':
231245
commands.append(IssueCommentCommand.force())
232246

0 commit comments

Comments
 (0)