-
Notifications
You must be signed in to change notification settings - Fork 143
/
pruner.py
executable file
·39 lines (29 loc) · 964 Bytes
/
pruner.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
#!/usr/bin/env python
from os import chdir
from os.path import join, exists
from aos_cd_jobs.common import JOBS_DIRECTORY, initialize_repo
def prunable_remote_refs(repo):
candidates = []
for ref in repo.remotes.origin.refs:
if not (ref.remote_head == 'HEAD' or ref.remote_head == 'master'):
candidates.append(ref)
return candidates
def remote_ref_needs_pruning(ref):
return not exists(jenkinsfile_for_ref(ref))
def jenkinsfile_for_ref(ref):
return join(
ref.repo.working_dir,
JOBS_DIRECTORY,
ref.remote_head,
'Jenkinsfile'
)
def prune_remote_ref(ref):
ref.repo.remotes[ref.remote_name].push(':' + ref.remote_head)
def prune_remote_refs(repo):
for ref in prunable_remote_refs(repo):
if remote_ref_needs_pruning(ref):
prune_remote_ref(ref)
if __name__ == '__main__':
repo = initialize_repo()
chdir(repo.working_dir)
prune_remote_refs(repo)