def get_branch_files(repository, check_upstream=False): if check_upstream and repository.github.upstream is not None: git.fetch( 'upstream', '--no-tags', '%s:refs/remotes/upstream/%s' % \ (repository.github.branch, repository.github.branch)) diff_output = git.diff( '--name-only', 'origin/%s..upstream/%s' % \ (repository.github.branch, repository.github.branch), strip=False) git.rebase('upstream/%s' % repository.github.branch) else: diff_output = '\n'.join([ line[3:] for line in git.status('status', '-s', strip=False) if len(line) > 3 ]) new_files = get_eligible_files(repository, diff_output, 'en') lstree_output = git.ls_tree('-r', '--name-only', repository.github.branch, strip=False) all_files = get_eligible_files(repository, lstree_output, 'en') return new_files, all_files
def _changed_files(self, commit): git = self.git changed_files = git.diff(commit + '^!', diff_filter='AM', name_only=True) changed_files = changed_files.split('\n') # Do not track the submodule -- causes issues with git blame if 'Testing/Data' in changed_files: changed_files.remove('Testing/Data') if len(changed_files) == 1 and not changed_files[0]: return None return tuple(changed_files)
def _was_fixed(self, hunks, followup): git = self.git followup_changed = git.diff(followup + '^!', diff_filter='M', name_only=True) followup_changed = followup_changed.split('\n') fixed_files = [] for changed in followup_changed: if changed in hunks: added_lines = set() for hh in hunks[changed]: added_lines.update(range(hh[0], hh[0] + hh[1])) try: blame = git.blame(followup + '^!', '--', changed, incremental=True, reverse=True) except GitCommandError: continue blame = blame.split('\n') first = blame[0].split() boundary = first[0] followup_deleted = set() followup_deleted.update( range(int(first[1]), int(first[1]) + int(first[3]))) blame = blame[1:] next_is_hunk = False for line in blame: if line.startswith('filename '): next_is_hunk = True continue if next_is_hunk: next_is_hunk = False line_split = line.split() if line_split[0] != boundary: continue followup_deleted.update( range(int(line_split[1]), int(line_split[1]) + int(line_split[3]))) if not followup_deleted.isdisjoint(added_lines): fixed_files.append(changed) if len(fixed_files) > 0: return tuple(fixed_files) return None
def _was_fixed(self, hunks, followup): git = self.git followup_changed = git.diff(followup + '^!', diff_filter='M', name_only=True) followup_changed = followup_changed.split('\n') fixed_files = [] for changed in followup_changed: if changed in hunks: added_lines = set() for hh in hunks[changed]: added_lines.update(range(hh[0], hh[0] + hh[1])) try: blame = git.blame(followup + '^!', '--', changed, incremental=True, reverse=True) except GitCommandError: continue blame = blame.split('\n') first = blame[0].split() boundary = first[0] followup_deleted = set() followup_deleted.update(range(int(first[1]), int(first[1]) + int(first[3]))) blame = blame[1:] next_is_hunk = False for line in blame: if line.startswith('filename '): next_is_hunk = True continue if next_is_hunk: next_is_hunk = False line_split = line.split() if line_split[0] != boundary: continue followup_deleted.update(range(int(line_split[1]), int(line_split[1]) + int(line_split[3]))) if not followup_deleted.isdisjoint(added_lines): fixed_files.append(changed) if len(fixed_files) > 0: return tuple(fixed_files) return None
def api_diff(project_id, sha1, old, new): lines = git.diff(get_db(), project_id, sha1, old, new) return jsonify(lines=lines)