def initialize(init_args: InitArgs) -> None: install() global repo global native_blob_metrics, native_blob_visitor global diff_blob_metrics, diff_blob_visitor global native_tree_metrics, native_tree_visitor global dir_metrics, dir_visitor global diffdir_metrics, diffdir_visitor def get_metrics(superclass) -> Tuple: # type: ignore return tuple([ all_metrics[m]() for m in init_args.metrics if issubclass(all_metrics[m], superclass) ] + [ m() for m in init_args.custom_metrics if issubclass(m, superclass) ]) repo = Repository(init_args.repository) native_blob_metrics = get_metrics(NativeBlobMetric) native_blob_visitor = NativeBlobVisitor() diff_blob_metrics = get_metrics(DiffBlobMetric) diff_blob_visitor = DiffBlobVisitor(repo) native_tree_metrics = get_metrics(NativeTreeMetric) native_tree_visitor = NativeTreeVisitor() dir_metrics = get_metrics(DirMetric) dir_visitor = DirVisitor(repo) diffdir_metrics = get_metrics(DiffDirMetric) diffdir_visitor = DiffDirVisitor(repo)
def edit(self, new_markdown: str, user: User, edit_message: str) -> None: """Set the page's markdown, render its HTML, and commit the repo.""" if new_markdown == self.markdown: return self.markdown = new_markdown self.rendered_html = convert_markdown_to_safe_html(new_markdown) self.rendered_html = add_anchors_to_headings(self.rendered_html) self.last_edited_time = utc_now() repo = Repository(self.BASE_PATH) author = Signature(user.username, user.username) repo.index.read() repo.index.add(str(self.file_path.relative_to(self.BASE_PATH))) repo.index.write() # Prepend the group name and page path to the edit message - if you change the # format of this, make sure to also change the page-editing template to match edit_message = f"~{self.group.path}/{self.path}: {edit_message}" repo.create_commit( repo.head.name, author, author, edit_message, repo.index.write_tree(), [repo.head.target], )
def show_branches_refs(dir): repo = Repository(dir) branches_list = list(repo.branches) print(branches_list) all_refs = list(repo.references) print(all_refs) return branches_list
def get_changed_contrib_names() -> List[str]: """Get all changed files as compared to remote/main""" repo = Repository(root_path) main_branch = repo.lookup_branch('main') if main_branch is None: raise RuntimeError("Can't find `main` branch to compare to.") file_paths = set( itertools.chain.from_iterable( (patch.delta.old_file.path, patch.delta.new_file.path) for patch in repo.diff(a=main_branch))) changed_contribs = set() for filepath in file_paths: if '__pycache__' not in filepath: path_parents = (root_path / Path(filepath)).parents for contrib_path in contrib_paths: if contrib_path in path_parents: changed_contribs.add(contrib_path.name) if test_path in path_parents: for contrib_path in contrib_paths: if filepath.endswith(f'test_{contrib_path.parts[-1]}.py'): changed_contribs.add(contrib_path.name) return list(changed_contribs)
def classify_by_tag( path: str, start_tag: str, end_tag: Optional[str] = None, model: Optional[MLModel] = None ) -> List[str]: """Classify messages for the given repo based on tags.""" repo_path = os.path.join(path, ".git") if os.path.exists(repo_path): repo = Repository(repo_path) else: raise RepositoryNotFoundException start_tag = repo.revparse_single("refs/tags/" + start_tag) if end_tag is None: end_tag = repo.revparse_single("refs/heads/master") else: end_tag = repo.revparse_single("refs/tags/" + end_tag) orig_messages = [] walker = repo.walk(end_tag.id, GIT_SORT_TOPOLOGICAL) walker.hide(start_tag.id) for commit in walker: orig_messages.append(commit.message.lower()) return classify_messages(orig_messages, model)
def fetch_project(srcdir, project, git_remote_callback, fetch_remote, dry_run): try: repo = Repository(os.path.join(srcdir, project.workspace_path, ".git")) master_remote = get_origin(repo, project) head_branch = get_head_branch(repo) tracking_branch = head_branch.upstream if head_branch else None tracking_remote = repo.remotes[ tracking_branch. remote_name] if tracking_branch is not None else None if fetch_remote and master_remote is not None: if not dry_run: master_remote.fetch(callbacks=git_remote_callback) else: msg("@{cf}Fetching@|: %s\n" % escape(textify(master_remote.url))) if fetch_remote and tracking_remote is not None and ( master_remote is None or master_remote.name != tracking_remote.name): if not dry_run: tracking_remote.fetch(callbacks=git_remote_callback) else: msg("@{cf}Fetching@|: %s\n" % escape(textify(tracking_remote.url))) return "" except (GitError, AuthorizationFailed) as e: return str(e)
def get_docset_ids(config: Dict, manifest: Manifest, only: Optional[List[str]]) -> Dict[str, str]: """Obtain the ids for all requested docsets. Args: config: Cache configuration. manifest: Repository manifest. only: Only consider this docset (null to consider all) Returns: State. """ ids = dict() for entry in config: if only and entry["docset"] not in only: continue id = "" for name in entry["projects"]: p = next((p for p in manifest.projects if p.name == name), None) assert p, f"Project {name} not in manifest" repo = Repository(Path(p.topdir) / p.path) id += repo.revparse_single("HEAD").id.hex ids[entry["docset"]] = hashlib.sha256(id.encode("utf-8")).hexdigest() return ids
def __init__(self, path): self.path = abspath(path) + '/' # Open database self.path_data = '%s/database/' % self.path if not lfs.is_folder(self.path_data): error = '"%s" should be a folder, but it is not' % path raise ValueError, error # Open repository self.repo = Repository(self.path_data) # Read index try: tree = self.repo.head.peel(GIT_OBJ_TREE) self.repo.index.read_tree(tree.id) except: pass # Check git commiter try: _, _ = self.username, self.useremail except: print '=========================================' print 'ERROR: Please configure GIT commiter via' print ' $ git config --global user.name' print ' $ git config --global user.email' print '=========================================' raise
def test_incomplete_signature_does_not_crash_gitdata_classes(self): import tempfile with tempfile.TemporaryDirectory( prefix="tmprepo_") as tmp_repo_location: # change working directory os.chdir(tmp_repo_location) subprocess.run(['git', 'init'], cwd=tmp_repo_location) # create a file a single line in it filename = 'file.txt' with open(os.path.join(tmp_repo_location, filename), "w") as f: f.write("A single line of code\n") subprocess.run(['git', 'add', 'file.txt'], cwd=tmp_repo_location) # apparently it is not possible to create pygit.Signature with empty author's email (and name) # but commits with no author's email can be created via git subprocess.run([ 'git', 'commit', '-m "No-email author" --author "Author NoEmail <>"' ], cwd=tmp_repo_location) try: # Commit without author's email does not crash data fetch git_repository = Repository(tmp_repo_location) WholeHistory(git_repository) BlameData(git_repository) except Exception as e: self.fail(str(e))
def get_docset_props(docset: str, config: Dict, manifest: Manifest) -> Tuple[str, bool]: """Obtain the id and dirty status of the given docset. Args: docset: Docset name. config: Cache configuration. manifest: Repository manifest. Returns: Docset ID. """ for entry in config: if entry["docset"] != docset: continue id = "" dirty = False for name in entry["projects"]: p = next((p for p in manifest.projects if p.name == name), None) assert p, f"Project {name} not in manifest" repo = Repository(Path(p.topdir) / p.path) id += repo.revparse_single("HEAD").id.hex dirty = dirty or bool(repo.status()) return hashlib.sha256(id.encode("utf-8")).hexdigest(), dirty raise ValueError(f"Docset {docset} not in configuration file")
def test_blame_mapper(gitrepo): directory, g = gitrepo repo = Repository("%s/jool/.git" % directory) commit = repo.revparse_single('9aeaac94cda9eb7e32d6f861079ab793a0311983') mapper = BugMap() assert mapper.map(repo, 'c/d.py', 5, commit) == '903b2d2c65f291492f30467edc6442b74a78ab92'
def launch(self): # init repo self._info('Locating the repository..') try: repo = Repository(REPO_PATH) except GitError: self._error('Failed to locate the repository!') return # fetch self._info('Fetching repository..') try: repo.remotes['origin'].fetch() except GitError: self._error('Failed to fetch the repository!') # reset repository self._info('Resetting the repository..') try: repo.reset( repo.lookup_reference('refs/remotes/origin/master').target, GIT_RESET_HARD) except GitError: self._error('Failed to reset the repository!') # launch self._info('Launching..') try: subprocess.Popen(EXE_PATH, cwd=EXE_WORKDIR) except OSError: self._error('Failed to launch!') else: self.destroy()
def try_commit_and_push(name, version, bundle_version): repo = Repository("headers/.git") new_commit_message = name + " " + version + " (" + bundle_version + ")" # Already commited this version? for commit in repo.walk(repo.head.target, GIT_SORT_TIME | GIT_SORT_REVERSE): if commit.message == new_commit_message: return False index = repo.index index.add_all() index.write() # print(index.diff_to_workdir().stats.files_changed) # if index.diff_to_workdir().stats.files_changed == 0: # return False print("Commiting...") user = repo.default_signature tree = index.write_tree() ref = "refs/heads/master" repo.create_commit(ref, user, user, new_commit_message, tree, [repo.head.get_object().hex]) push(repo, ref) return True
def vgit_push(self, path, message, user_name_commiter, user_name_author, email_commiter, email_author, branch, user_name_pusher, user_passoword_pusher, log_cb): try: repo = Repository(path) index = repo.index reference = 'refs/heads/' + branch tree = index.write_tree() author = pygit2.Signature(user_name_author, email_author) commiter = pygit2.Signature(user_name_commiter, email_commiter) oid = repo.create_commit(reference, author, commiter, message, tree, [repo.head.target]) credentials = pygit2.UserPass(user_name_pusher, user_passoword_pusher) remo = repo.remotes["origin"] remo.credentials = credentials aux = remo.url repo.remotes.set_push_url(user_name_pusher, aux) callbacks = pygit2.RemoteCallbacks(credentials=credentials) remo.push([reference], callbacks=callbacks) except ValueError as err: log_cb(str(err)) return False except Exception as err: log_cb(str(err)) return False return True
def find_cloned_gitlab_projects(projects, srcdir, subdir=None): def repo_has_project_url(repo_urls, project): for _, url in iteritems(project.url): if url in repo_urls: return True return False base_path = srcdir if subdir is None else os.path.join(srcdir, subdir) result = [] foreign = [] for curdir, subdirs, files in os_walk(base_path, followlinks=True): if "CATKIN_IGNORE" in files: del subdirs[:] elif ".git" in subdirs: repo = Repository(os.path.join(curdir, ".git")) repo_urls = set() for r in repo.remotes: repo_urls.add(r.url) path = os.path.relpath(curdir, srcdir) for project in projects: if repo_has_project_url(repo_urls, project): assert project.workspace_path is None or project.workspace_path == path project.workspace_path = path for p in project.packages: p.project = project result.append(project) break else: foreign.append(os.path.relpath(curdir, srcdir)) del subdirs[:] else: subdirs = [s for s in subdirs if not s.startswith(".")] return result, foreign
def __main__(): parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument("-f", "--full", help="Replay all commits during preview", action="store_true") parser.add_argument("-r", "--reduce", help="Filter to only the branches in the command", action="store_true") args, unknown_args = parser.parse_known_args() command = "git " + ' '.join(unknown_args) validate() repo_name = os.getcwd() + "\\.git" branch_filterer = BranchFilterer() if args.reduce: branch_filterer = BasicBranchFilterer(unknown_args) repository = Repository(repo_name) chain_repo = ChainRepository(repository, branch_filterer) commands = ["git checkout " + chain_repo.head_name, command] LegendPrinter().print_legend() print("\nBefore `%s`" % command) ChainHierarchyPrinter(chain_repo.tree, chain_repo.head_name).print() print("\nAfter `%s`" % command) GitCommandPreviewer(chain_repo, not args.full, branch_filterer).preview_commands(commands) print()
def get_git_info(git_working_tree_dir): repository_path = discover_repository(git_working_tree_dir) assert repository_path is not None repo = Repository(repository_path) commits = list(repo.walk(repo.head.target, GIT_SORT_NONE)) head_commit = commits[0] diff = repo.diff() git_info = { 'head_commit': { 'hash': head_commit.hex, 'message': head_commit.message, 'author': head_commit.author.name }, 'branch': { 'name': repo.head.shorthand }, 'stats': { 'files_changed': diff.stats.files_changed, }, 'num_commits': len(commits) } return git_info
def classify_by_date( path: str, start: Optional[str] = None, end: Optional[str] = None, model: Optional[MLModel] = None ) -> List[str]: """Classify commits by date.""" start_time = 0 end_time = sys.maxsize if start is not None: start_time = int(time.mktime(datetime.datetime.strptime(start, "%Y-%m-%d").timetuple())) if end is not None: end_time = int(time.mktime(datetime.datetime.strptime(end, "%Y-%m-%d").timetuple())) repo_path = os.path.join(path, ".git") if os.path.exists(repo_path): repo = Repository(repo_path) else: raise RepositoryNotFoundException orig_messages = [] for commit in repo.walk(repo.head.target, GIT_SORT_TOPOLOGICAL): if start_time < commit.commit_time < end_time: orig_messages.append(commit.message.lower()) return classify_messages(orig_messages, model)
def save_experiment_config(self): with open(os.path.join(self.experiment_dir, 'parameters.txt'), 'w') as file: config_dict = vars(self.args) for k in vars(self.args): file.write(f"{k}={config_dict[k]} \n") repo = Repository('.') file.write(f"git-repo={repo.head.shorthand} \n")
def report(): max_name_length = 0 max_email_length = 0 authors = dict() repo = Repository('%s/.git' % find_toplevel(os.getcwd())) for commit in repo.walk(repo.head.target, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE): if commit.author.email not in authors.keys(): authors[commit.author.email] = dict() authors[commit.author.email]['author'] = commit.author authors[commit.author.email]['commits'] = 1 if len(commit.author.name) > max_name_length: max_name_length = len(commit.author.name) if len(commit.author.email) > max_email_length: max_email_length = len(commit.author.email) else: authors[commit.author.email]['commits'] += 1 print( 'Name'.ljust(max_name_length), '\t', 'Email'.ljust(max_email_length), '\t', 'Commits' ) print( '_' * max_name_length, '\t', '_' * max_email_length, '\t', '_' * 7 ) for email, author in authors.items(): print( author['author'].name.ljust(max_name_length), '\t', author['author'].email.ljust(max_email_length), '\t', author['commits'] )
def run(self, options, args): moduledir = Path(user_data_dir, "modules") if options.uninstall == True: for package in args: print(package[package.find("/") + 1:]) if os.path.exists( str(Path(moduledir, package[package.find("/") + 1:]))): shutil.rmtree(str( Path(moduledir, package[package.find("/") + 1:])), onerror=readonly_handler) else: print("\033[93m" + package[package.find("/") + 1:] + " not exists\033[0m") elif options.list == True: print(os.listdir(moduledir)) else: for package in args: print(package) if os.path.exists( str(Path(moduledir, package[package.find("/") + 1:]))): shutil.rmtree( str(Path(moduledir, package[package.find("/") + 1:]))) clone_repository( "https://github.com/" + package, str(Path(moduledir, package[package.find("/") + 1:]))) repo = Repository( str( Path( str( Path(moduledir, package[package.find("/") + 1:])), '.git'))) repo.init_submodules() repo.update_submodules()
def define_env(env): repo = Repository(".") if repo is not None: target = repo.head.shorthand env.variables["current_branch"] = target
def repo_at(root): try: repo = Repository(root + '.git') except _pygit2.GitError as err: print("Error: " + str(err)) return None return repo
def __main__(): colorama.init(autoreset=True) parser = argparse.ArgumentParser() parser.add_argument('branches_to_include', type=str, nargs='*', help='The branches you want to include in the map') parser.add_argument("-r", "--render", help="generate png of repo", action="store_true") parser.add_argument("-v", "--verbose", help="print logging information", action="store_true") args = parser.parse_args() Logger.enable_logging = args.verbose repo_name = get_repo_name() repository = Repository(repo_name) branch_filterer = get_branch_filterer(repository, args.branches_to_include) chain_repo = ChainRepository(repository, branch_filterer) printer = ChainHierarchyPrinter(chain_repo.tree, chain_repo.head_name) if (args.render): digraph = DigraphWrapper(chain_repo.tree.root) print(str(digraph)) LegendPrinter().print_legend() printer.print() print()
def parse_diffusion_features(pid, repo_path, branch, start, stop=-1): """ Function to extract diffusion features from a set of commits. """ repo = Repository(repo_path) head = repo.references.get(branch) commits = list( repo.walk(head.target, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE)) start = start - 1 if (start > 0) else start commits = commits[start:stop] if (stop != -1) else commits[start:] features = [[] for c in range(len(commits))] for i, commit in enumerate(tqdm(commits[1:], position=pid)): diff = repo.diff(commits[i], commit) patches = [p for p in diff] # Extract all different subsystems that have been modified modules = set([]) subsystems_mapping = {} entropy_change = 0 file_changes = [] total_change = 0 for patch in patches: # Skip binary files if patch.delta.is_binary: continue _, addition, deletions = patch.line_stats total_change = total_change + (addition + deletions) file_changes.append(addition + deletions) # Store all subsystems fpath = patch.delta.new_file.path subsystems = fpath.split('/')[:-1] root = subsystems_mapping for system in subsystems: if system not in root: root[system] = {} root = root[system] if subsystems > 0: modules.add(subsystems[0]) # Check how many subsystems that have been touched modified_systems = count_diffing_subsystems(subsystems_mapping) # Calculate the entropy for the commit entropy_change = count_entropy(file_changes, total_change) # Add all features features[i].append(str(commit.hex)) features[i].append(str(float(modified_systems))) features[i].append(str(float(len(modules)))) features[i].append(str(float(entropy_change))) RES[pid] = features
def set_repo(): try: repo = Repository('./repo') except: repo_url = 'https://github.com/rubik/radon.git' repo_path = './repo' repo = clone_repository(repo_url, repo_path) return repo
def set_repo(): try: repo = Repository('~/repo') except: repo_url = 'https://github.com/EntilZha/PyFunctional.git' repo_path = '~/repo' repo = clone_repository(repo_url, repo_path) return repo
def push_changes(repo_path: Path): """Push the repository at repo_path to the remote named origin.""" repo = Repository(repo_path) remote = repo.remotes['origin'] creds = UserPass('Technical27', GITHUB_TOKEN) callback = RemoteCallbacks(credentials=creds) remote.connect(callbacks=callback) remote.push(['refs/heads/master:refs/heads/master'], callbacks=callback)
def get_current_branch_name(self) -> str: """ :return: Current branch checked out in repo """ repo = Repository(self.git_repo_path) if repo.head_is_unborn or repo.head_is_detached: return "" return repo.head.name
def set_repo(): try: reposito = Repository('./repo') except: repository_url = 'https://github.com/robin70001/DFS1.git' repository_path = './repo' reposito = clone_repository(repository_url, repository_path) return reposito