-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfummy.py
101 lines (77 loc) · 2.56 KB
/
fummy.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
"""
Author: ikosenn
This is a program to eliminate stale git branches.
It checks last commits and based on the staleness threshold
eliminates all stale branches
Another CL function is provided to eliminate all available branches.
You can also remove all branches that have already been merged to
the main branch
"""
import os
from datetime import datetime
import click
from sarge import capture_stdout
import pytz
from dateutil.parser import parse
DEFAULT_BRANCH = 'master'
# helper functions
def get_time_difference(time):
"""
Computes the difference with todays time
"""
timezone = "Africa/Nairobi"
branch_time = parse(time)
current_time = datetime.now(pytz.timezone(timezone))
diff_days = (current_time - branch_time)
return diff_days.days
def cwd(path):
os.chdir(path)
@click.command()
@click.option(
'--threshold', '-t',
default=10,
prompt='What number of days should the threshold be? [10 days]')
@click.option(
'branches', '--branch', '-b', default=DEFAULT_BRANCH,
prompt='What branches should be excluded? [master]', multiple=True)
@click.option(
'--path', '-p', prompt='File path to the git repo?',
type=click.Path(exists=True))
def fummy(threshold, branches, path):
cwd(path)
all_branches = capture_stdout('git branch')
# remove spaces and any blank spaces
temp = all_branches.stdout.text.replace(
'*', '').replace(' ', '').split('\n')
for branch in temp:
if branch and branch not in branches:
click.echo('Processing branch: {}'.format(branch))
p = capture_stdout(
'git show {} --format="%cI" --no-patch'.format(branch))
diff_days = get_time_difference(p.stdout.text)
if diff_days > threshold:
click.echo('Deleting {}'.format(branch))
p = capture_stdout(
'git branch -D {}'.format(branch))
click.echo(p.stdout.text)
@click.command()
@click.option('--filename', type=click.Path(exists=True))
@click.option('--default', '-d', default=DEFAULT_BRANCH)
def kill_merged(default):
"""
Start by checking out to the master branch and then finding out the
branches already merged to master and eliminating the buggage
"""
# git branch --merged master
pass
@click.group()
def cli():
"""
Command Line Interface tools loader for ``fummy``
These utilities help with deleting git branches older than the specified
period
"""
pass
cli.add_command(fummy)
if __name__ == '__main__':
cli()