def commit_all(repo_dir): run_command('git add --all', return_output=True, cwd=repo_dir) run_command( 'git commit -m "Initial commit"', return_output=True, cwd=repo_dir, )
def get_release_info(war, develop_branch, release_branch, release_tag): work_dir = environ['HOME'] + '/tmp_wildcat' try: release_info = 'META-INF/release.info' release_info_path = f'{work_dir}/{release_info}' makedirs(work_dir) run_command(f'jar xvf {realpath(war)}', cwd=work_dir, return_output=True) with open(f'{work_dir}/WEB-INF/classes/{release_info}', 'rt', encoding='utf-8') as f: print_release_info(f.read(), develop_branch, release_branch, release_tag) for jar in get_jars(work_dir): if exists(release_info_path): remove(release_info_path) run_command(f'jar xvf {jar} -x {release_info}', cwd=work_dir, return_output=True) if exists(release_info_path): with open(release_info_path, 'rt', encoding='utf-8') as f: print_release_info(f.read(), develop_branch, release_branch, release_tag) except OSError: print_exc() finally: rmtree(work_dir, ignore_errors=True)
def fork_repo(fork_project, branches, git_dir): try: for b in reversed(branches): run_command(f'git checkout {b}', return_output=True, cwd=git_dir) rc, original_origin = run_command('git remote get-url origin', return_output=True, cwd=git_dir) repo = original_origin.split('/')[-1][:-4] new_origin = '/'.join(original_origin.split( '/')[:-2]) + '/' + fork_project.lower() + '/' + repo + '.git' create_repo(fork_project, repo) run_command(f'git remote set-url origin {new_origin}', return_output=True, cwd=git_dir) run_command('git remote prune origin', return_output=True, cwd=git_dir) for b in branches: run_command(f'git push -u origin {b}', return_output=True, cwd=git_dir) set_repo_default_branch((fork_project, repo), branches[0]) print(f'{fork_project}/{repo}: {new_origin}') return (fork_project, repo), new_origin except OSError: # pragma: no cover print(f'Error forking {git_dir}') print_exc(file=sys.stdout)
def update_repo(branches, setup_branch, repo, repo_path): head = get_commit(repo_path) if head != [ branch['objectId'] for branch in branches[f"{repo['project_spec']}/{repo['name']}"] if branch['name'] == setup_branch ][0]: run_command( f'git fetch --all && git reset --hard origin/{setup_branch}', cwd=repo_path) return True return False
def update_repo(branches, setup_branch, repo, repo_path): head = get_commit(repo_path) if head != [ branch['latestCommit'] for branch in branches[(repo['project']['key'], repo['slug'])] if branch['displayId'] == setup_branch ][0]: run_command( f'git fetch --all && git reset --hard origin/{setup_branch}', cwd=repo_path) return True return False
def get_clone_url(git_dir): return_code, url = run_command( f'git --git-dir {git_dir} remote get-url origin', True, git_dir, False, ) if return_code: return run_command( f'git --git-dir {git_dir}/.git remote get-url origin', True, git_dir, )[1] else: return url
def create_and_push_to_dest_repo( branch, dest_repo, dest_repo_dir, ): dest_repo_origin = create_repo(dest_repo)['remoteUrl'] run_command(f'git remote add origin {dest_repo_origin}', return_output=True, cwd=dest_repo_dir) run_command( f'git push -u origin {branch}', return_output=True, cwd=dest_repo_dir, ) return dest_repo_origin
def create_symlinks(source, target): if not exists(target): makedirs(target) for main_file in [ f for f in glob(f'{source}/**/*.py', recursive=True) if has_main(f) and not is_excluded(f) ]: link_file = f'{target}/{map_to_link_name(main_file)}' if exists(link_file): print(f'{link_file} already exists') else: command = f'ln -s {main_file} {link_file}' print(command) run_command(command)
def get_emails(clone_dir, committer=False): log_format = '%ce' if committer else '%ae' rc, output = run_command( f'git log --format="{log_format}"', True, clone_dir, ) return set(output.split('\n'))
def create_and_push_to_dest_repo(dest_project, dest_repo, dest_branch, dest_repo_dir, webhook=None): dest_repo_origin = create_repo(dest_project, dest_repo)['links']['clone'][0]['href'] if webhook: enable_repo_web_hook([dest_project, dest_repo], webhook) run_command(f'git remote add origin {dest_repo_origin}', return_output=True, cwd=dest_repo_dir) run_command(f'git push -u origin {dest_branch}', return_output=True, cwd=dest_repo_dir) set_repo_default_branch((dest_project, dest_repo), dest_branch) return dest_repo_origin
def build(projects_dir): command = create_command(projects_dir) rc, output = run_command(command, return_output=True, cwd=projects_dir, check=False, timeout=None) return dict(build_result=read_build_result(projects_dir), output=output)
def setup(project, repo, src_branch, dest_project, dest_repo, dest_branch, work_dir): clone_url = [c for _, p, r, c in get_clone_urls([project]) if r == repo][0] dest_repo_dir = f'{work_dir}/{dest_project}/{dest_repo}' run_command(f'git clone -b {src_branch} {clone_url} {dest_repo_dir}') run_command('rm -rf .git', return_output=True, cwd=dest_repo_dir) run_command('git init', return_output=True, cwd=dest_repo_dir) if not dest_branch == 'master': run_command(f'git checkout -b {dest_branch}', return_output=True, cwd=dest_repo_dir) return clone_url, dest_repo_dir
def get_origin_remote_url(repo_dir, remote): command = 'git remote -v' try: rc, output = run_command(command, True, repo_dir) for line in output.split('\n'): if remote in line: return re.split(r'[\t ]+', line)[1] except CalledProcessError as cpe: print(cpe) return None
def apply_patch(repo, tag, patch_dir): print('Apply patch', tag, patch_dir) return [ run_command(cmd, True, repo) for cmd in [ f'git apply {patch_dir}/*', 'git add --all', f'git commit -m {tag}', f'git tag -a {tag} -m {tag}', ] ]
def build_multi_modules(multi_modules, repository_dir, settings_file, logback_file): success_coordinates = [] failure_coordinates = [] for mm in multi_modules: build_modules = mm['updated'] mvn_log = f'{mm["pom_dir"]}/mvn.log' command = create_command( build_modules, mvn_log, repository_dir, settings_file, logback_file, ) run_command(command, cwd=mm['pom_dir']) mm_success_coordinates, mm_failure_coordinates = create_build_result( mm['coordinates'], build_modules, mvn_log) success_coordinates += mm_success_coordinates failure_coordinates += mm_failure_coordinates return success_coordinates, failure_coordinates
def f(repo): rc, output = run_command(tag_cmd, return_output=True, cwd=repo.path, check=False) return { 'repo': repo, 'commit': get_commit(repo.path), 'tag': tag, 'success': rc == 0, 'output': output, }
def apply_patch(patch_repo, apply_repo, ref): print('Apply patch', ref, patch_repo, apply_repo, ref) rc, output = run_command(f'git checkout {ref}', True, patch_repo) print(output) synchronize_dirs(patch_repo, apply_repo) rc, output = run_command('git status', True, apply_repo) print(output) if 'nothing to commit' in output: rc, output = run_command(f'git tag -a {ref} -m {ref}', True, apply_repo) print(output) else: for cmd in [ 'git add --all', f'git commit -m {ref}', f'git tag -a {ref} -m {ref}', ]: rc, output = run_command(cmd, True, apply_repo) print(output) return ref
def analyze( log_dir, settings_file, sonar_plugin, sonar_url, sonar_auth_token, build, ): opts = f'-Dsonar.host.url={sonar_url} -Dsonar.login={sonar_auth_token}' log_path = f'{log_dir}/{build.name}.log' command = f'mvn -s {settings_file} {opts} {sonar_plugin}:sonar | tee {log_path}' return run_command(command, cwd=build.path, check=False, timeout=90), log_path, build
def create_patch(repo, since_tag, tag, output): patch_dir = f'{output}/{tag}' makedirs(patch_dir) cmd = ' '.join([ 'git log -p --reverse --pretty=email --full-index --binary', '--stat', '-m', '--first-parent' f'{since_tag + ".." if since_tag else ""}{tag}', f'> {patch_dir}/tag.patch' ]) print(cmd) rc, out = run_command(cmd, True, repo) return tag, patch_dir, out
def test_run_command(mock_run, return_output, expected): mock_run.return_value = MagicMock(returncode=0, stdout=b'response') assert expected == run_command('command', return_output, 'cwd', True, 1) assert [ call( 'command', check=True, cwd='cwd', shell=True, stderr=-2, stdout=-1, timeout=1, ) ] == mock_run.mock_calls
def setup(repo, branch, dest_repo, work_dir): clone_url, repo_dir = clone_repos( work_dir, repos=[repo], branch=branch, )[0] dest_repo_dir = f'{work_dir}/{dest_repo}' run_command(f'mv {repo_dir} {dest_repo_dir}') run_command('rm -rf .git', return_output=True, cwd=dest_repo_dir) run_command('git init', return_output=True, cwd=dest_repo_dir) if not branch == 'master': run_command( f'git checkout -b {branch}', return_output=True, cwd=dest_repo_dir, ) return clone_url, dest_repo_dir
def filter_branch(clone_dir, new_name, new_email, old_email=None): filter_branch_command = f'''git filter-branch --force --env-filter ' if [ "$GIT_COMMITTER_EMAIL" = "{old_email}" ] then export GIT_COMMITTER_NAME="{new_name}" export GIT_COMMITTER_EMAIL="{new_email}" fi if [ "$GIT_AUTHOR_EMAIL" = "{old_email}" ] then export GIT_AUTHOR_NAME="{new_name}" export GIT_AUTHOR_EMAIL="{new_email}" fi ' --tag-name-filter cat -- --branches --tags ''' if old_email else f'''git filter-branch --force --env-filter ' export GIT_COMMITTER_NAME="{new_name}" export GIT_COMMITTER_EMAIL="{new_email}" export GIT_AUTHOR_NAME="{new_name}" export GIT_AUTHOR_EMAIL="{new_email}" ' --tag-name-filter cat -- --branches --tags ''' return run_command(filter_branch_command, True, clone_dir)
def get_tags(repo, tag_pattern): return [ tag for tag in run_command('git tag', True, repo)[1].split('\n') if match(r'{}'.format(tag_pattern), tag) ]
def clone_repo(work_dir, setup_branch, repo): run_command( f'git clone {repo["links"]["clone"][0]["href"]} -b {setup_branch} ' + f'{work_dir}/{repo["project"]["key"]}/{repo["slug"]}') return True
def git(repo_dir, cmd, check=True): return run_command(cmd, return_output=True, cwd=repo_dir, check=check)
def get_dependency_tree(pom_file): code, output = run_command(f'mvn dependency:tree -f {pom_file}', True) return output
def switch_settings(ending): source = f'{environ["HOME"]}/.m2/settings_{ending}.xml' if not exists(source): raise ValueError(f'{source} does not exist') target = f'{environ["HOME"]}/.m2/settings.xml' return run_command(f'ln -sf {source} {target}', True)
def clone_repo(work_dir, setup_branch, repo, repo_path): run_command(f'git clone {repo["remoteUrl"]} -b {setup_branch} {repo_path}') return True
def get_branch(git_dir): return run_command('git rev-parse --abbrev-ref HEAD', True, git_dir)[1]
def get_refs(repo, ref_pattern): return [ ref for ref in run_command('git tag', True, repo)[1].split('\n') if match(r'{}'.format(ref_pattern), ref) ]