def _delete_puppet_git_config(self, projectid, fqdn): repo_user = cfg.CONF[self.name].puppet_git_repo_user # set up repo object and rebase everything repo = self._get_repo() repo_path = repo.working_tree_dir repo.remotes.origin.pull(rebase=True) rolesfilepath = os.path.join(projectid, "%s.roles" % fqdn) if os.path.exists(os.path.join(repo_path, rolesfilepath)): repo.index.remove([rolesfilepath], working_tree=True) hierafilepath = os.path.join(projectid, "%s.yaml" % fqdn) if os.path.exists(os.path.join(repo_path, hierafilepath)): repo.index.remove([hierafilepath], working_tree=True) if repo.index.diff(repo.head.commit): LOG.debug("Removing instance-puppet entries") committer = git.Actor(repo_user, "*****@*****.**") author = git.Actor(repo_user, "*****@*****.**") message = "Deleted by designate during VM deletion" repo.index.commit(message, committer=committer, author=author) # and push self._git_push(repo) shutil.rmtree(repo.working_tree_dir)
def gitrepo(): """Fixture to create a test Git repository with several commits :return: temporary repository :rtype: :class:`git.Repo` """ gtmpdir = gittmpdir() result = {} repo = git.Repo.init(path=gtmpdir.strpath, mkdir=True) tux = git.Actor('Tux Penguin', '*****@*****.**') wilber = git.Actor("Wilber Gimp", '*****@*****.**') committers = [tux, wilber] with changedir(gtmpdir) as cwd: # Create two files local("fox.txt").write("The quick brown fox") local("README.txt").write("This is a test repository") repo.index.add(['fox.txt', 'README.txt']) repo.index.commit("First commit", committer=tux, author=tux) local("fox.txt").write("jumps over the lazy dog.", mode="a") repo.index.add(['fox.txt']) repo.index.commit("Add another line", committer=tux, author=tux) local("README.txt").write("==================", mode="a") repo.index.add(['README.txt']) repo.index.commit("Add equal sign line", committer=wilber, author=wilber) # Create some statistics result['commits'] = 3 result['committer_mails'] = {user.email for user in committers} return result, repo
def revert(self, revision, message="", author=None): actor = git.Actor(author[0], author[1]) try: self.repo.git.revert(revision, "--no-commit") except git.exc.GitCommandError: self.repo.git.revert("--abort") raise StorageError("Revert failed.") actor = git.Actor(author[0], author[1]) self.repo.index.commit(message, author=actor)
def _init_repository(self): _git = git.Repo.init(self.folder) # Always make sure there is a commit in the working tree, otherwise # HEAD is invalid, which results in other nasty problems. _git.index.commit( "Add: initial empty commit", author=git.Actor(GIT_USERNAME, GIT_EMAIL), committer=git.Actor(GIT_USERNAME, GIT_EMAIL), ) return _git
def repo_with_fixup(run_hopic): author = git.Actor('Joe Engineer', '*****@*****.**') commitargs = dict( author_date=_GIT_TIME, commit_date=_GIT_TIME, author=author, committer=author,) with git.Repo.init(run_hopic.toprepo, expand_vars=False) as repo: (run_hopic.toprepo / "hopic-ci-config.yaml").write_text('''\ version: bump: no phases: build: test: - echo "Build successful!" ''') repo.index.add(('hopic-ci-config.yaml',)) (run_hopic.toprepo / "README.md").write_text("Test\n") repo.index.add(('README.md',)) base_commit = repo.index.commit(message='feat: initial commit', **commitargs) # PR branch from just before the main branch's HEAD repo.head.reference = repo.create_head('merge-branch', base_commit) (run_hopic.toprepo / "README.md").write_text("Imagine smth useful here\n") repo.index.add(('README.md',)) repo.index.commit(message='docs: add useful documentation', **commitargs) (run_hopic.toprepo / "README.md").write_text("Imagine something useful here\n") repo.index.add(('README.md',)) repo.index.commit(message='fixup! docs: add useful documentation', **commitargs).hexsha yield repo
def test_clean_option_custom_command_is_run_before_default_command( capfd, run_hopic): author = git.Actor('Bob Tester', '*****@*****.**') commitargs = dict( author_date=_git_time, commit_date=_git_time, author=author, committer=author, ) with git.Repo.init(run_hopic.toprepo, expand_vars=False) as repo: (run_hopic.toprepo / 'hopic-ci-config.yaml').write_text('''\ clean: - touch dummy.txt ''') temp_test_file = 'random_file.txt' (run_hopic.toprepo / temp_test_file).write_text('''\ nothing to see here ''') repo.index.add(('hopic-ci-config.yaml', )) repo.index.commit(message='Initial commit', **commitargs) commit = list(repo.iter_commits('master', max_count=1))[0] (result, ) = run_hopic( ('--workspace', run_hopic.toprepo, 'checkout-source-tree', '--clean', '--target-remote', run_hopic.toprepo, '--target-ref', 'master'), ) assert result.exit_code == 0 assert not (run_hopic.toprepo / temp_test_file).is_file() assert not (run_hopic.toprepo / 'dummy.txt').is_file() assert commit.committed_date == ( run_hopic.toprepo / 'hopic-ci-config.yaml').stat().st_mtime
def update_feedstock(org_name, repo_name): name = repo_name[:-len("-feedstock")] with tmp_directory() as tmp_dir: feedstocks_url = ("https://{}@github.com/nwb-extensions/feedstocks.git" "".format(os.environ["FEEDSTOCKS_GH_TOKEN"])) feedstocks_repo = git.Repo.clone_from(feedstocks_url, tmp_dir) # Get the submodule feedstock_submodule = feedstocks_repo.create_submodule( name=name, path=os.path.join("feedstocks", name), url="https://github.com/{0}/{1}.git".format(org_name, repo_name), branch="master") # Update the feedstocks submodule feedstock_submodule.update(init=True, recursive=False, force=True, to_latest_revision=True) feedstocks_repo.git.add([".gitmodules", feedstock_submodule.path]) # Submit changes if feedstocks_repo.is_dirty(working_tree=False, untracked_files=True): author = git.Actor("nwb-extensions-admin", "nwbextensio" + "ns@" + "gm" + "ail.com") feedstocks_repo.index.commit( "Updated the {0} feedstock.".format(name), author=author, committer=author) feedstocks_repo.remote().pull(rebase=True) feedstocks_repo.remote().push()
def commit(project, branch): if current_user.username != get_branch_owner(project, branch): flash(_('You are not the owner of this branch'), 'error') return redirect(url_for('.view', project=project, branch=branch, filename='index.html')) merging = get_merging(project, branch) if merging: flash(_('You need to finish merging'), 'error') return redirect(url_for('.merge', project=project, branch=branch, other=merging['branch'])) user_repo_path = join('repos', project, branch) repo = git.Repo(join(user_repo_path, 'source')) form = MessageForm(request.form) if request.method == 'POST' and form.validate(): author = git.Actor(current_user.username, current_user.email) if len(form.message.data): message = form.message.data else: message = _('Some changes') repo.index.commit(message, author=author) origin = get_branch_origin(project, branch).name if branch != origin: git_api = repo.git git_api.push('origin', branch) flash(_('Page submitted to _%s') % origin, 'info') update_subtree(project, branch) flash('Change commited', 'info') return redirect(url_for('.branch', project=project, branch=branch)) menu = menu_bar(project, branch) diff = repo.git.diff('--cached') return render_template('commit.html', menu=menu, form=form, diff=diff)
def create_project(project, user): # create a repository repo_path = join('repos', project, 'master/source') os.makedirs(repo_path) git.Repo.init(repo_path) repo = git.Repo(repo_path) config_repo(repo, user.username, user.email) copyfile('application/empty_repo/source/index.rst', join(repo_path, 'index.rst')) copyfile('application/empty_repo/.gitignore', join(repo_path, '.gitignore')) repo.index.add(['index.rst', '.gitignore']) author = git.Actor(user.username, user.email) repo.index.commit(_('Initial commit'), author=author) # add project to database user_id = User.query.filter(User.username == user.username).first().id new_project = Project(project, user_id) db.session.add(new_project) # add master branch to database db.session.commit() project_id = Project.query.filter_by(name=project).first().id origin_id = 1 new_branch = Branch('master', project_id, origin_id, user_id) db.session.add(new_branch) db.session.commit() # updating branch's self reference new_branch = Branch.query.filter_by(project_id=project_id).first() new_branch.origin_id = new_branch.id db.session.commit() build(project, 'master')
def ExportToRepo(self, repo: git.Repo, targets: typing.List[str], src_files: typing.List[str], extra_files: typing.List[str], file_move_mapping: typing.Dict[str, str]) -> None: """Export the requested targets to the destination directory.""" # The timestamp for the export. timestamp = datetime.datetime.utcnow() # Export the git history. app.Log(1, 'Exporting git history for %s files', humanize.Commas(len(src_files))) for file in src_files: print(file) source_tree.ExportGitHistoryForFiles(self.git_repo, repo, src_files) # Make manual adjustments. exported_workspace = bazelutil.Workspace(pathlib.Path( repo.working_tree_dir)) self.CreatePythonRequirementsFileForTargets(exported_workspace, targets) self.CopyFilesToDestination(exported_workspace, extra_files) self.MoveFilesToDestination(exported_workspace, file_move_mapping) if not repo.is_dirty(untracked_files=True): return app.Log(1, 'Creating automated subtree export commit') repo.git.add('.') author = git.Actor(name='[Git export bot]', email='/dev/null') repo.index.commit(f'Automated subtree export at {timestamp.isoformat()}', author=author, committer=author, skip_hooks=True)
def __init__(self, repo_root, user, mail): self.__repo = git.Repo(path=repo_root) self.__user = git.Actor(user, mail) self.__root_path = repo_root self.__changes = set() self.__get_repo_changes()
def test_mixin_string_developer_data(): import git argument = '{"developer_data": [["a", "b"]]}' c = reporchestrator.mixin([argument]) assert c["developer_data"] == [["a", "b"]] assert set(c.keys()) == {"developer_data", "developers"} assert c["developers"][0] == git.Actor("a", "b")
def test_clean_option_custom_command_is_executed(capfd, tmp_path): author = git.Actor('Bob Tester', '*****@*****.**') commitargs = dict( author_date=_git_time, commit_date=_git_time, author=author, committer=author, ) toprepo = tmp_path / 'repo' std_out_message = 'test dummy' with git.Repo.init(str(toprepo), expand_vars=False) as repo: with (toprepo / 'hopic-ci-config.yaml').open('w') as f: f.write('''\ clean: - echo '%s' ''' % std_out_message) temp_test_file = 'random_file.txt' with (toprepo / temp_test_file).open('w') as f: f.write('''\ nothing to see here ''') repo.index.add(('hopic-ci-config.yaml',)) repo.index.commit(message='Initial commit', **commitargs) commit = list(repo.iter_commits('master', max_count=1))[0] result = run(('--workspace', str(toprepo), 'checkout-source-tree', '--clean', '--target-remote', str(toprepo), '--target-ref', 'master')) assert result.exit_code == 0 out, err = capfd.readouterr() sys.stdout.write(out) sys.stderr.write(err) clean_out = out.splitlines()[0] assert clean_out == std_out_message assert not (toprepo / temp_test_file).is_file() assert commit.committed_date == (toprepo / 'hopic-ci-config.yaml').stat().st_mtime
def test_default_clean_checkout_option(capfd, tmp_path): author = git.Actor('Bob Tester', '*****@*****.**') commitargs = dict( author_date=_git_time, commit_date=_git_time, author=author, committer=author, ) toprepo = tmp_path / 'repo' with git.Repo.init(str(toprepo), expand_vars=False) as repo: with (toprepo / 'hopic-ci-config.yaml').open('w') as f: f.write('''\ {} ''') temp_test_file = 'random_file.txt' with (toprepo / temp_test_file).open('w') as f: f.write('''\ nothing to see here ''') repo.index.add(('hopic-ci-config.yaml',)) repo.index.commit(message='Initial commit', **commitargs) commit = list(repo.iter_commits('master', max_count=1))[0] result = run(('--workspace', str(toprepo), 'checkout-source-tree', '--clean', '--target-remote', str(toprepo), '--target-ref', 'master')) assert result.exit_code == 0 assert not (toprepo / temp_test_file).is_file() assert commit.committed_date == (toprepo / 'hopic-ci-config.yaml').stat().st_mtime
def test_empty_file(tmp_path): # Change working directory cwd = os.getcwd() os.chdir(str(tmp_path)) # Create empty file file_name = str(tmp_path / "new-file") open(file_name, "a").close() # Get authors of empty, uncommitted file r = gitpython.Repo.init(tmp_path) repo_instance = repo.Repo() repo_instance.set_config(DEFAULT_CONFIG) # TODO: should throw an error? repo_instance.page(file_name) authors = repo_instance.get_authors() assert authors == [] # Get authors of empty but committed file r.index.add([file_name]) author = gitpython.Actor("Tim", "*****@*****.**") r.index.commit("initial commit", author=author) repo_instance.page(file_name) authors = repo_instance.get_authors() assert authors == [] os.chdir(cwd)
def __init__(self, name, user): self.name = name self.owner_id = user.id # create the master branch new_branch = application.branches.Branch('master', self, None, user) db.session.add(new_branch) db.session.commit() # updating branch's self reference new_branch.origin_id = new_branch.id db.session.commit() # create folder for resources os.makedirs(join('repos', name, '_resources')) os.makedirs(join('repos', name, '_resources/original')) os.makedirs(join('repos', name, '_resources/low_resolution')) os.makedirs(join('repos', name, '_resources/thumbnail')) # create the repository in the filesystem repo_path = join('repos', name, 'master/source') os.makedirs(repo_path) os.symlink( os.path.abspath(join('repos', name, '_resources', 'low_resolution')), os.path.abspath(join('repos', name, 'master/source/_resources/'))) git.Repo.init(repo_path) repo = git.Repo(repo_path) application.branches.config_repo(repo, user.username, user.email) copyfile('empty_repo/source/index.rst', join(repo_path, 'index.rst')) copyfile('empty_repo/.gitignore', join(repo_path, '.gitignore')) repo.index.add(['index.rst', '.gitignore']) author = git.Actor(user.username, user.email) repo.index.commit(_('Initial commit'), author=author) new_branch.build(timeout=30)
def commit(self, message, user, include=None): if include is not None: for file in include: self._be.index.add(file) author = git.Actor(user, user) self._be.index.commit(message=message, author=author)
def test_clean_option_home_annotations(capfd, run_hopic): author = git.Actor('Bob Tester', '*****@*****.**') commitargs = dict( author_date=_git_time, commit_date=_git_time, author=author, committer=author, ) home_path = os.path.expanduser("~") with git.Repo.init(run_hopic.toprepo, expand_vars=False) as repo: (run_hopic.toprepo / 'hopic-ci-config.yaml').write_text('''\ clean: - echo '$HOME' - echo '~' ''') repo.index.add(('hopic-ci-config.yaml', )) repo.index.commit(message='Initial commit', **commitargs) (result, ) = run_hopic( ('--workspace', run_hopic.toprepo, 'checkout-source-tree', '--clean', '--target-remote', run_hopic.toprepo, '--target-ref', 'master'), ) assert result.exit_code == 0 out, err = capfd.readouterr() sys.stdout.write(out) sys.stderr.write(err) clean_home_out = out.splitlines()[0] clean_tilde_out = out.splitlines()[1] assert clean_home_out == home_path assert clean_tilde_out == home_path
def spawn(cls, path): spawn = not osp.isdir(cls._git_dir(path)) repo = git.Repo.init(path=path) if spawn: author = git.Actor("Nobody", "*****@*****.**") repo.index.commit('Initial commit', author=author) return repo
def commit(self, **parms): if self.debug: deb.cm_debug.show(marker='debGit', message='Git Start Commit') self.git_repo.git.add('-A') committer = git.Actor("tacacsgui", "*****@*****.**") self.git_repo.index.commit(message='TacacsGUI', committer=committer) self.check(after=True)
def commit_map(self, old_commit, message, author, authored_date, author_tz_offset, committer, committed_date, committer_tz_offset): # keep author, update committer committer = git.Actor('The committer', '*****@*****.**') committed_date = time.time() return message, author, authored_date, author_tz_offset, committer, committed_date, committer_tz_offset
def init(self, path): spawn = not osp.isdir(GitWrapper._git_dir(path)) self.repo = git.Repo.init(path=path) if spawn: author = git.Actor("Nobody", "*****@*****.**") self.repo.index.commit('Initial commit', author=author) return self.repo
def create(self, request): update = request.data.get('update') file = request.data['file'] tracks = self.convert(file) repository = Repository.objects.get(title=request.data['repository'], owner=request.user) files_list = [] created_tracks = [] for track_title, score in tracks.items(): # Save file to disk file_path = os.path.join(repository.path_to_repo, track_title) files_list.append(file_path) with open(file_path, "w") as text_file: if isinstance(score, bytes): score = score.decode() print(score, file=text_file) if not update: created_tracks.append( Track.objects.create(title=track_title, repository=repository)) # Add files to commit index = repository.git_repository.index index.add(files_list) author = git.Actor(repository.owner.username, "*****@*****.**") commiter = git.Actor(request.user.username, "*****@*****.**") commit_message = request.data.get( 'message') or "Added tracks: {}".format(tracks.keys()) commit = index.commit(message=commit_message, author=author, committer=commiter) # Create Commit object Commit.objects.create(message=commit_message, commiter=request.user.username, repository=repository, hash=commit.hexsha) if update: tracks = Track.objects.filter( repository__owner=request.user, repository__title=request.data['repository']) serializer = self.get_serializer(list(tracks), many=True) else: serializer = self.get_serializer(created_tracks, many=True) return Response(serializer.data)
def test_model_plan_feature(): import git m = reporchestrator.Model() c = { "developers": [git.Actor("a", "b"), git.Actor("c", "d")], "repo_age_in_days": 1, "developer_strategy": "round-robin", "max_commits_per_branch": 3, "ticket_id_template": r"ACME-%d", } gen = reporchestrator.next_member(c) m = reporchestrator.model_plan_feature(c, m, gen) assert m.developer == git.Actor("a", "b") assert m.is_consistent() assert m.ticket == "ACME-1" assert 1 <= m.planned <= c["max_commits_per_branch"] m = reporchestrator.model_plan_feature(c, m, gen) assert m.developer == git.Actor("c", "d")
def try_add(self, info: Dict[str, Any]) -> bool: netkan = self.make_netkan(info) # Create .netkan file or quit if already there netkan_path = self.nk_repo.nk_path(netkan.get('identifier', '')) if netkan_path.exists(): # Already exists, we are done return True # Create branch branch_name = f"add/{netkan.get('identifier')}" try: self.nk_repo.git_repo.remotes.origin.fetch(branch_name) except git.GitCommandError: # *Shrug* pass if branch_name not in self.nk_repo.git_repo.heads: self.nk_repo.git_repo.create_head( branch_name, getattr( # type: ignore[arg-type] self.nk_repo.git_repo.remotes.origin.refs, branch_name, self.nk_repo.git_repo.remotes.origin.refs.master ) ) # Checkout branch self.nk_repo.git_repo.heads[branch_name].checkout() # Create file netkan_path.write_text(self.yaml_dump(netkan)) # Add netkan to branch self.nk_repo.git_repo.index.add([netkan_path.as_posix()]) # Commit self.nk_repo.git_repo.index.commit( ( f"Add {info.get('name')} from {info.get('site_name')}" f"\n\nThis is an automated commit on behalf of {info.get('username')}" ), author=git.Actor(info.get('username'), info.get('email')) ) # Push branch self.nk_repo.git_repo.remotes.origin.push( '{mod}:{mod}'.format(mod=branch_name)) # Create pull request if self.github_pr: self.github_pr.create_pull_request( title=f"Add {info.get('name')} from {info.get('site_name')}", branch=branch_name, body=self.PR_BODY_TEMPLATE.safe_substitute( defaultdict(lambda: '', info)), labels=['Pull request', 'Mod-request'], ) return True
def test_mkdocs_in_git_subdir(tmp_path): """ Sometimes `mkdocs.yml` is not in the root of the repo. We need to make sure things still work in this edge case. tmp_path/testproject website/ ├── docs/ └── mkdocs.yml """ testproject_path = tmp_path / "testproject" shutil.copytree( "tests/basic_setup/docs", str(testproject_path / "website" / "docs") ) shutil.copyfile( "tests/basic_setup/mkdocs.yml", str(testproject_path / "website" / "mkdocs.yml") ) cwd = os.getcwd() os.chdir(str(testproject_path)) # Create file file_name = str(testproject_path / "website" / "new-file") with open(file_name, "w") as the_file: the_file.write("Hello\n") # Create git repo and commit file r = gitpython.Repo.init(testproject_path) r.index.add([file_name]) author = gitpython.Actor("Tim", "*****@*****.**") r.index.commit("initial commit", author=author) # Test retrieving author repo_instance = repo.Repo() repo_instance.set_config(DEFAULT_CONFIG) repo_instance.page(file_name) authors = repo_instance.get_authors() assert len(authors) == 1 # We don't want to test datetime authors = util.page_authors(authors, file_name) authors[0]["last_datetime"] = None assert authors == [ { "name": "Tim", "email": "*****@*****.**", "last_datetime": None, "lines": 1, "lines_all_pages": 1, "contribution": "100.0%", "contribution_all_pages": "100.0%", } ] os.chdir(cwd)
def commit_and_push(): repo = git.Repo('.') index = repo.index index.add(['leanpkg.toml']) author = git.Actor('leanprover-community-bot', '*****@*****.**') index.commit('auto update dependencies', author=author, committer=author) print('Pushing commit to remote') repo.remote().push()
def __init__(self, repo_path: Path, ignores: Iterable[str] = []): self.actor: git.Actor = git.Actor("appcli", "root@localhost") self.repo_path = repo_path self.ignores = ignores self.repo = ( git.Repo(self.repo_path) if self.__repo_exists() else self.__initialise_git_repo() )
def __init__(self, repo_path: Path, ignores: Iterable[str] = []): self.actor: git.Actor = git.Actor("appcli", "root@localhost") # Get or create repo at this path to ensure it exists try: repo = git.Repo(repo_path) except git.InvalidGitRepositoryError: repo = self.__initialise_new_repo(repo_path, ignores) self.repo = repo
def test_commit_head(rc: ServerRepoCtx, client: flask.testing.FlaskClient): master_tree: shahex = _get_master_tree_hex(client) dummy: git.Actor = git.Actor('dummy', "*****@*****.**") commit: git.Commit = git.Commit.create_from_tree(rc.repo, master_tree, message="", author=dummy, committer=dummy) master: git.Reference = git.Reference(rc.repo, "refs/heads/master") master.set_commit(commit)