Skip to content

Commit f499b5d

Browse files
Small refactoring requested by Gianvito to enable re-use logic in other scripts.
1 parent 9a9bd95 commit f499b5d

File tree

1 file changed

+74
-70
lines changed

1 file changed

+74
-70
lines changed

bin/git-bc-show-eligible

Lines changed: 74 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -51,88 +51,92 @@ def find_unpicked(repo, from_commit, to_commit, since_commit, show_all):
5151
if since_commit and commit.id == since_commit.id:
5252
break
5353

54-
parser = argparse.ArgumentParser(description='Show commits, eligible for cherry-picking')
55-
parser.add_argument('branch', metavar='BRANCH', help='Name for the branch to check against',
56-
default='origin/master', nargs='?')
57-
parser.add_argument('target_branch', metavar='TARGET_BRANCH', help='Name for the target branch',
58-
default='HEAD', nargs='?')
59-
parser.add_argument('--since', metavar='COMMIT', help='Start checking since specified commit')
60-
parser.add_argument('--all', action='store_true', help='Show commits from all users')
61-
62-
top = find_toplevel()
63-
64-
if not top:
65-
print('The current folder is not a valid git repository')
66-
sys.exit(1)
67-
68-
args = parser.parse_args()
69-
repo = pygit2.Repository(top)
70-
71-
try:
72-
from_commit = repo.revparse_single(args.branch)
73-
except:
74-
print('Invalid branch %s' % args.branch)
75-
sys.exit(1)
76-
77-
try:
78-
to_commit = repo.revparse_single(args.target_branch)
79-
except:
80-
print('Invalid target branch %s' % args.target_branch)
81-
sys.exit(1)
82-
83-
if not repo.merge_base(from_commit.id, to_commit.id):
84-
print('%s and %s does not have common ancestor' % (args.branch, args.target_branch))
85-
sys.exit(1)
86-
87-
since_commit = None
88-
if args.since:
54+
def main():
55+
parser = argparse.ArgumentParser(description='Show commits, eligible for cherry-picking')
56+
parser.add_argument('branch', metavar='BRANCH', help='Name for the branch to check against',
57+
default='origin/master', nargs='?')
58+
parser.add_argument('target_branch', metavar='TARGET_BRANCH', help='Name for the target branch',
59+
default='HEAD', nargs='?')
60+
parser.add_argument('--since', metavar='COMMIT', help='Start checking since specified commit')
61+
parser.add_argument('--all', action='store_true', help='Show commits from all users')
62+
63+
top = find_toplevel()
64+
65+
if not top:
66+
print('The current folder is not a valid git repository')
67+
sys.exit(1)
68+
69+
args = parser.parse_args()
70+
repo = pygit2.Repository(top)
71+
8972
try:
90-
since_commit = repo.revparse_single(args.since)
73+
from_commit = repo.revparse_single(args.branch)
9174
except:
92-
print('Invalid since %s' % args.since)
75+
print('Invalid branch %s' % args.branch)
9376
sys.exit(1)
9477

95-
author_format_str = ' | %s <%s>'
96-
commit_format_str = '%s %s%s'
78+
try:
79+
to_commit = repo.revparse_single(args.target_branch)
80+
except:
81+
print('Invalid target branch %s' % args.target_branch)
82+
sys.exit(1)
83+
84+
if not repo.merge_base(from_commit.id, to_commit.id):
85+
print('%s and %s does not have common ancestor' % (args.branch, args.target_branch))
86+
sys.exit(1)
87+
88+
since_commit = None
89+
if args.since:
90+
try:
91+
since_commit = repo.revparse_single(args.since)
92+
except:
93+
print('Invalid since %s' % args.since)
94+
sys.exit(1)
95+
96+
author_format_str = ' | %s <%s>'
97+
commit_format_str = '%s %s%s'
98+
99+
if sys.stdout.isatty():
100+
author_format_str = ' \033[31m| %s <%s>\033[0m'
101+
commit_format_str = '\033[33m%s\033[0m %s%s'
97102

98-
if sys.stdout.isatty():
99-
author_format_str = ' \033[31m| %s <%s>\033[0m'
100-
commit_format_str = '\033[33m%s\033[0m %s%s'
103+
seen = ''
104+
groups = []
105+
current_group = []
106+
for commit in find_unpicked(repo, from_commit, to_commit, since_commit, args.all):
107+
author_info = ''
108+
if args.all:
109+
author_info = author_format_str % (commit.author.name, commit.author.email)
101110

102-
seen = ''
103-
groups = []
104-
current_group = []
105-
for commit in find_unpicked(repo, from_commit, to_commit, since_commit, args.all):
106-
author_info = ''
107-
if args.all:
108-
author_info = author_format_str % (commit.author.name, commit.author.email)
111+
commit_msg = commit.message[:commit.message.index('\n')]
112+
indent = commit_msg in seen
113+
seen += commit.message
109114

110-
commit_msg = commit.message[:commit.message.index('\n')]
111-
indent = commit_msg in seen
112-
seen += commit.message
115+
commit_string = (commit_format_str % (str(commit.id), commit_msg, author_info))
113116

114-
commit_string = (commit_format_str % (str(commit.id), commit_msg, author_info))
117+
if indent:
118+
current_group.append(commit_string)
119+
else:
120+
if current_group:
121+
groups.append(current_group)
122+
current_group = [commit_string]
115123

116-
if indent:
117-
current_group.append(commit_string)
118-
else:
119-
if current_group:
120-
groups.append(current_group)
121-
current_group = [commit_string]
124+
if current_group:
125+
groups.append(current_group)
122126

123-
if current_group:
124-
groups.append(current_group)
127+
for group in groups:
128+
merge = group[0]
129+
child_commits = group[1:-1]
130+
last_child_commit = group[-1] if len(group) > 1 else ''
125131

126-
for group in groups:
127-
merge = group[0]
128-
child_commits = group[1:-1]
129-
last_child_commit = group[-1] if len(group) > 1 else ''
132+
print(merge)
130133

131-
print(merge)
134+
for child in child_commits:
135+
print(" ├─ {}".format(child))
132136

133-
for child in child_commits:
134-
print(" ─ {}".format(child))
137+
if last_child_commit:
138+
print(" ─ {}".format(last_child_commit))
135139

136-
if last_child_commit:
137-
print(" └─ {}".format(last_child_commit))
138140

141+
if __name__ == '__main__':
142+
main()

0 commit comments

Comments
 (0)