def clone(source, target=None, bare=False, checkout=None, outstream=sys.stdout): """Clone a local or remote git repository. :param source: Path or URL for source repository :param target: Path to target repository (optional) :param bare: Whether or not to create a bare repository :param outstream: Optional stream to write progress to :return: The new repository """ if checkout is None: checkout = (not bare) if checkout and bare: raise ValueError("checkout and bare are incompatible") client, host_path = get_transport_and_path(source) if target is None: target = host_path.split("/")[-1] if not os.path.exists(target): os.mkdir(target) if bare: r = Repo.init_bare(target) else: r = Repo.init(target) remote_refs = client.fetch(host_path, r, determine_wants=r.object_store.determine_wants_all, progress=outstream.write) r["HEAD"] = remote_refs["HEAD"] if checkout: outstream.write('Checking out HEAD') index.build_index_from_tree(r.path, r.index_path(), r.object_store, r["HEAD"].tree) return r
def commit_style(repo: Repo, format_rule_name: str) -> Tuple[str, str]: """ Call bash script which commit all changes to `style_name` branch and checkout master back. :param repo: Repo instance to the repository for which style were applied. :param format_rule_name: Applied format rule name. :return: Two commit hashes: where style was applied and where style was disrupt. """ def commit(repo: Repo, msg: str) -> str: """Commit everything.""" for tree_path, entry in repo.open_index().items(): full_path = os.path.join(repo.path.encode(), tree_path) blob = blob_from_path_and_stat(full_path, os.lstat(full_path)) if blob.id != entry.sha: repo.stage(tree_path) return repo.do_commit(msg.encode(), b"Source{d} ML Team <*****@*****.**>") repopath = repo.path base = repo.head() branch_create(repopath, format_rule_name, force=True) update_head(repopath, format_rule_name) style_commit_sha = commit(repo, format_rule_name) build_index_from_tree(repo.path, repo.index_path(), repo.object_store, repo[base].tree) revert_style_commit_sha = commit(repo, "Revert " + format_rule_name) update_head(repopath, b"master") return style_commit_sha.decode(), revert_style_commit_sha.decode()
def test_clone_then_push_data(self): self.test_push_data_branch() shutil.rmtree(self.temp_d) local_repo = repo.Repo.init(self.temp_d, mkdir=True) tcp_client = client.TCPGitClient(self.server_address, port=self.port) remote_refs = tcp_client.fetch(self.fakerepo, local_repo) files = (os.path.join(self.temp_d, 'testfile'), os.path.join(self.temp_d, 'testfile2')) local_repo["HEAD"] = remote_refs["refs/heads/master"] indexfile = local_repo.index_path() tree = local_repo["HEAD"].tree index.build_index_from_tree(local_repo.path, indexfile, local_repo.object_store, tree) for f in files: self.assertEqual(os.path.isfile(f), True) def determine_wants(*args): return {"refs/heads/master": local_repo.refs["HEAD"]} os.mkdir(os.path.join(self.temp_d, "test")) files = ('testfile11', 'testfile22', 'test/testfile33') i = 0 for f in files: file(os.path.join(self.temp_d, f), 'w').write("DATA %s" % i) i += 1 local_repo.stage(files) local_repo.do_commit('Test commit', 'fbo@localhost', ref='refs/heads/master') tcp_client.send_pack("/fakerepo", determine_wants, local_repo.object_store.generate_pack_contents)
def branch_did_change(self,sender): # set head to branch repo=self._get_repo() branch=self.view['branch'].text if branch==repo.active_branch: return if branch in self.branch_iterator(): self._repo().refs.set_symbolic_ref('HEAD', 'refs/heads/'+branch) self.unstage_all() self.refresh() console.hud_alert('branch') elif branch in self._repo(): indexfile = repo.repo.index_path() tree = repo.repo[str(branch)].tree build_index_from_tree(repo.repo.path, indexfile, repo.repo.object_store, tree) #self._repo().refs.set_symbolic_ref('HEAD', branch) #self.unstage_all() #self.refresh() console.hud_alert('commitish') else: #todo: prompt to confirm self.create_branch()
def init(pod, theme_url, force=False): if '//' not in theme_url or ':' not in theme_url: repo_url = THEME_REPO_URL.format(theme_url) else: repo_url = theme_url git_client = client.HttpGitClient(repo_url) logging.info('Initializing with {}...'.format(repo_url)) temp_repo = MemoryRepo() temp_repo.clone(git_client) tree = temp_repo['refs/heads/master'].tree # Build the tree into a temp directory. temp_root = tempfile.mkdtemp() local_repo = repo.Repo.init(temp_root) index_file = local_repo.index_path() index.build_index_from_tree(local_repo.path, index_file, temp_repo.object_store, tree) shutil.rmtree(os.path.join(temp_root, '.git')) # Automatically enable "force" for empty directories. if pod.list_dir('/') == []: force = True try: _copy_files_to_pod(temp_root, pod, force=force) except OSError as e: if 'File exists' in str(e): text = ('{} already exists and is not empty. Delete the directory before' ' proceeding or use --force.') logging.info(text.format(pod.root)) return logging.info('Pod ready to go: {}'.format(pod.root))
def pull(repo, remote_location, refs_path, outstream=sys.stdout, errstream=sys.stderr): """ Pull from remote via dulwich.client :param repo: Path to repository :param remote_location: Location of the remote :param refs_path: relative path to the fetched refs :param outstream: A stream file to write to output :param errstream: A stream file to write to errors """ # Open the repo r = open_repo(repo) client, path = get_transport_and_path(remote_location) remote_refs = client.fetch(path, r, progress=errstream.write) r['HEAD'] = remote_refs[refs_path] # Perform 'git checkout .' - syncs staged changes indexfile = r.index_path() tree = r["HEAD"].tree index.build_index_from_tree(r.path, indexfile, r.object_store, tree)
def DoDeploy(self, ref): global x2ProfilerApp self.repo = x2ProfilerApp.repo self.refs = self.repo.get_refs() o = self.repo[ref] while o.type_name == 'tag': type_name, sha = o._get_object() o = self.repo.get_object(sha) if not o.type_name == 'commit': raise ValueError('Unable to find the tagged commit!') # We can only do a clean checkout, so clenaup self.RmAllProfiles(x2ProfilerApp.x2swProfilesPath) # Dulwich can't handle detached head, so use a temp branch as a workaround self.repo.refs.set_symbolic_ref('HEAD', 'refs/heads/temp') self.repo['HEAD'] = o.id build_index_from_tree(self.repo.path, self.repo.index_path(), self.repo.object_store, o.tree) # Make the deployment folder (if not there) and checkout files into it if not os.path.isdir(x2ProfilerApp.x2swProfilesTgtPath): os.makedirs(x2ProfilerApp.x2swProfilesTgtPath) else: # Cleanup the deployment destination self.RmAllProfiles(x2ProfilerApp.x2swProfilesTgtPath) build_index_from_tree(x2ProfilerApp.x2swProfilesTgtPath, self.repo.index_path(), self.repo.object_store, o.tree)
def checkout(repo_path='.'): repo = Repo(repo_path) indexfile = repo.index_path() obj_sto = repo.object_store tree_id = repo[repo.head()].tree build_index_from_tree(repo_path, indexfile, obj_sto, tree_id) return [obj_sto.iter_tree_contents(tree_id)]
def DoDeploy(self, ref): global x2ProfilerApp self.repo = x2ProfilerApp.repo self.refs = self.repo.get_refs() o = self.repo[ref] while o.type_name == "tag": type_name, sha = o._get_object() o = self.repo.get_object(sha) if not o.type_name == "commit": raise ValueError("Unable to find the tagged commit!") # We can only do a clean checkout, so clenaup self.RmAllProfiles(x2ProfilerApp.x2swProfilesPath) # Dulwich can't handle detached head, so use a temp branch as a workaround self.repo.refs.set_symbolic_ref("HEAD", "refs/heads/temp") self.repo["HEAD"] = o.id build_index_from_tree(self.repo.path, self.repo.index_path(), self.repo.object_store, o.tree) # Make the deployment folder (if not there) and checkout files into it if not os.path.isdir(x2ProfilerApp.x2swProfilesTgtPath): os.makedirs(x2ProfilerApp.x2swProfilesTgtPath) else: # Cleanup the deployment destination self.RmAllProfiles(x2ProfilerApp.x2swProfilesTgtPath) build_index_from_tree(x2ProfilerApp.x2swProfilesTgtPath, self.repo.index_path(), self.repo.object_store, o.tree)
def _dulwich_checkout(self, _repo): """ Perform 'git checkout .' - syncs staged changes """ indexfile = _repo.index_path() tree = _repo["HEAD"].tree index.build_index_from_tree(_repo.path, indexfile, _repo.object_store, tree)
def clone(self, target_path, mkdir=True, bare=False, origin="origin"): """Clone this repository. :param target_path: Target path :param mkdir: Create the target directory :param bare: Whether to create a bare repository :param origin: Base name for refs in target repository cloned from this repository :return: Created repository as `Repo` """ if not bare: target = self.init(target_path, mkdir=mkdir) else: target = self.init_bare(target_path) self.fetch(target) target.refs.import_refs("refs/remotes/" + origin, self.refs.as_dict("refs/heads")) target.refs.import_refs("refs/tags", self.refs.as_dict("refs/tags")) try: target.refs.add_if_new("refs/heads/master", self.refs["refs/heads/master"]) except KeyError: pass # Update target head head, head_sha = self.refs._follow("HEAD") target.refs.set_symbolic_ref("HEAD", head) target["HEAD"] = head_sha if not bare: # Checkout HEAD to target dir from dulwich.index import build_index_from_tree build_index_from_tree(target.path, target.index_path(), target.object_store, target["HEAD"].tree) return target
def init(pod, theme_url, force=False): if '//' not in theme_url or ':' not in theme_url: repo_url = THEME_REPO_URL.format(theme_url) else: repo_url = theme_url git_client = client.HttpGitClient(repo_url) logging.info('Initializing with {}...'.format(repo_url)) temp_repo = MemoryRepo() temp_repo.clone(git_client) tree = temp_repo['refs/heads/master'].tree # Build the tree into a temp directory. temp_root = tempfile.mkdtemp() local_repo = repo.Repo.init(temp_root) index_file = local_repo.index_path() index.build_index_from_tree(local_repo.path, index_file, temp_repo.object_store, tree) shutil.rmtree(os.path.join(temp_root, '.git')) # Automatically enable "force" for empty directories. if pod.list_dir('/') == []: force = True try: _copy_files_to_pod(temp_root, pod, force=force) except OSError as e: if 'File exists' in str(e): text = ( '{} already exists and is not empty. Delete the directory before' ' proceeding or use --force.') logging.info(text.format(pod.root)) return logging.info('Pod ready to go: {}'.format(pod.root))
def test_git_dir(self): if os.name != "posix": self.skipTest("test depends on POSIX shell") repo_dir = tempfile.mkdtemp() repo = Repo.init(repo_dir) self.addCleanup(shutil.rmtree, repo_dir) # Populate repo filea = Blob.from_string("file a") filee = Blob.from_string("d") tree = Tree() tree[".git/a"] = (stat.S_IFREG | 0o644, filea.id) tree["c/e"] = (stat.S_IFREG | 0o644, filee.id) repo.object_store.add_objects([(o, None) for o in [filea, filee, tree]]) build_index_from_tree(repo.path, repo.index_path(), repo.object_store, tree.id) # Verify index entries index = repo.open_index() self.assertEqual(len(index), 1) # filea apath = os.path.join(repo.path, ".git", "a") self.assertFalse(os.path.exists(apath)) # filee epath = os.path.join(repo.path, "c", "e") self.assertTrue(os.path.exists(epath)) self.assertReasonableIndexEntry(index["c/e"], stat.S_IFREG | 0o644, 1, filee.id) self.assertFileContents(epath, "d")
def test_describe_rule_ordinal(self): init_test_repo_path = str(Path(__file__).parent / "test_repo.tar.xz") init_repo_files = {"test_repo": {"test_file.js": "test\n\n"}} generate_smoke.js_format_rules = {"test": ("init", "test")} with tempfile.TemporaryDirectory(prefix="benchmark-test") as testdir: self.assertEqual( 0, generate_smoke.generate_smoke_entry(init_test_repo_path, testdir, force=True)) self.assertEqual( set(os.listdir(testdir)) - {"index.csv"}, set(init_repo_files.keys())) for init_repo in init_repo_files: repo = Repo(os.path.join(testdir, init_repo)) repo.hooks.clear() # Speed up dulwich by ~25% self.assertEqual(set(init_repo_files[init_repo]), set(os.listdir(repo.path)) - {".git"}) walker = repo.get_graph_walker((b"refs/heads/test", )) next(walker) # Skip head commit. We need HEAD~1 before_head = next(walker) build_index_from_tree(repo.path, repo.index_path(), repo.object_store, repo[before_head].tree) for filename in init_repo_files[init_repo]: self.assertEqual(init_repo_files[init_repo][filename], (Path(testdir) / init_repo / filename).read_text())
def test_no_decode_encode(self): repo_dir = tempfile.mkdtemp() repo_dir_bytes = repo_dir.encode(sys.getfilesystemencoding()) self.addCleanup(shutil.rmtree, repo_dir) with Repo.init(repo_dir) as repo: # Populate repo file = Blob.from_string(b'foo') tree = Tree() latin1_name = u'À'.encode('latin1') utf8_name = u'À'.encode('utf8') tree[latin1_name] = (stat.S_IFREG | 0o644, file.id) tree[utf8_name] = (stat.S_IFREG | 0o644, file.id) repo.object_store.add_objects( [(o, None) for o in [file, tree]]) build_index_from_tree( repo.path, repo.index_path(), repo.object_store, tree.id) # Verify index entries index = repo.open_index() latin1_path = os.path.join(repo_dir_bytes, latin1_name) self.assertTrue(os.path.exists(latin1_path)) utf8_path = os.path.join(repo_dir_bytes, utf8_name) self.assertTrue(os.path.exists(utf8_path))
def test_git_dir(self): repo_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, repo_dir) with Repo.init(repo_dir) as repo: # Populate repo filea = Blob.from_string(b'file a') filee = Blob.from_string(b'd') tree = Tree() tree[b'.git/a'] = (stat.S_IFREG | 0o644, filea.id) tree[b'c/e'] = (stat.S_IFREG | 0o644, filee.id) repo.object_store.add_objects([(o, None) for o in [filea, filee, tree]]) build_index_from_tree(repo.path, repo.index_path(), repo.object_store, tree.id) # Verify index entries index = repo.open_index() self.assertEqual(len(index), 1) # filea apath = os.path.join(repo.path, '.git', 'a') self.assertFalse(os.path.exists(apath)) # filee epath = os.path.join(repo.path, 'c', 'e') self.assertTrue(os.path.exists(epath)) self.assertReasonableIndexEntry(index[b'c/e'], stat.S_IFREG | 0o644, 1, filee.id) self.assertFileContents(epath, b'd')
def clone(repo_url, ref=None, folder=None, rep=None): if ref is None: ref = 'refs/heads/master' logger.debug("clone repo_url={0}, ref={1}".format(repo_url, ref)) if not rep: if folder is None: folder = tempfile.mkdtemp() else: os.mkdir(folder) logger.debug("folder = {0}".format(folder)) rep = Repo.init(folder) client, relative_path = get_transport_and_path(repo_url) logger.debug("client={0}".format(client)) remote_refs = client.fetch(relative_path, rep) for k, v in remote_refs.iteritems(): try: rep.refs.add_if_new(k, v) except: pass if ref.startswith('refs/tags'): ref = rep.ref(ref) if isinstance(rep[ref], Tag): rep['HEAD'] = rep[ref].object[1] else: rep['HEAD'] = rep[ref] indexfile = rep.index_path() tree = rep["HEAD"].tree index.build_index_from_tree(rep.path, indexfile, rep.object_store, tree) logger.debug("done") return rep, folder
def _CheckoutLatestVersion(target_dir, url): """Pull tags and checkout the latest version of the target directory. Args: target_dir: (str) Directory name. url: (str) Git repository URL. Raises: errors.HangupException: Hangup during communication to a remote repository. """ local_repo = repo.Repo(target_dir) try: # We don't get the tags with a clone or a fetch, so we have to get them # after the fact. client_wrapper = WrapClient(url) local_repo = repo.Repo(target_dir) tag, revision = _PullTags(local_repo, client_wrapper, target_dir) log.info('Checking out revision [%s] of [%s] into [%s]', tag, url, target_dir) try: # Checkout the specified revision of the runtime definition from git. index.build_index_from_tree(local_repo.path, local_repo.index_path(), local_repo.object_store, revision.tree) except (IOError, OSError, WindowsError) as ex: raise InvalidTargetDirectoryError( 'Unable to checkout directory {0}: {1}'.format(target_dir, ex.message)) finally: local_repo.close()
def checkout(repo, ref=None): """Checkout the commit from a given ref to the working directory The equivalent of :code:`git checkout` Parameters ---------- repo : dulwich.repo.Repo ref : str Returns ------- list """ if ref is None: ref = repo.head() else: ref = bytes(full_ref(repo, ref), encoding='ascii') if isinstance(repo[ref], Tag): sha = repo[ref].object[1] tree_id = repo[sha].tree else: tree_id = repo[ref].tree index = repo.index_path() build_index_from_tree(repo.path, index, repo.object_store, tree_id) return [repo.object_store.iter_tree_contents(tree_id)]
def pull(repo, remote_location, refs_path, config=None, opener=None, outstream=sys.stdout, errstream=sys.stderr): """Pull from remote via dulwich.client :param repo: Path to repository :param remote_location: Location of the remote :param refs_path: relative path to the fetched refs :param config: Optional config object :param opener: Custom urllib2 opener for http(s) transport; primarily used for authentication :param outstream: A stream file to write to output :param errstream: A stream file to write to errors """ # Open the repo r = open_repo(repo) client, path = get_transport_and_path(remote_location, **{'config': config} if config else {}) if type(client) is HttpGitClient and opener: client.opener = opener remote_refs = client.fetch(path, r, progress=errstream.write) r['HEAD'] = remote_refs[refs_path] # Perform 'git checkout .' - syncs staged changes indexfile = r.index_path() tree = r["HEAD"].tree index.build_index_from_tree(r.path, indexfile, r.object_store, tree)
def clone(repo_url, ref=None, folder=None): is_commit = False if ref is None: ref = "refs/heads/master" elif not ref.startswith("refs/"): is_commit = True logger.debug("clone repo_url={0}, ref={1}".format(repo_url, ref)) if folder is None: folder = tempfile.mkdtemp() logger.debug("folder = {0}".format(folder)) rep = Repo.init(folder) client, relative_path = get_transport_and_path(repo_url) logger.debug("client={0}".format(client)) remote_refs = client.fetch(relative_path, rep) for k, v in remote_refs.iteritems(): try: rep.refs.add_if_new(k, v) except: pass if is_commit: rep["HEAD"] = rep.commit(ref) else: rep["HEAD"] = remote_refs[ref] indexfile = rep.index_path() tree = rep["HEAD"].tree index.build_index_from_tree(rep.path, indexfile, rep.object_store, tree) logger.debug("done") return rep, folder
def clone(url, path=None): client, target = dulwich.client.get_transport_and_path( url) #c , 'user/repo' if not path: if target.endswith('.git'): target = target[:-4] path = target path = F.mkdir(path) ls = F.ls(path) if len(ls) > 1: #skip .git return py.No('Not empty dir:' + path, ls) if len(ls) == 1: r = dulwich.repo.Repo(path) else: r = dulwich.repo.Repo.init(path) print(U.stime(), 'fetching url...') remote_refs = client.fetch(url, r) r[b"HEAD"] = remote_refs[b"HEAD"] print(U.stime(), 'build_index_from_tree ...') index.build_index_from_tree(r.path, r.index_path(), r.object_store, r[b'HEAD'].tree) r.close() return path, r
def test_symlink(self): repo_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, repo_dir) with Repo.init(repo_dir) as repo: # Populate repo filed = Blob.from_string(b'file d') filee = Blob.from_string(b'd') tree = Tree() tree[b'c/d'] = (stat.S_IFREG | 0o644, filed.id) tree[b'c/e'] = (stat.S_IFLNK, filee.id) # symlink repo.object_store.add_objects([(o, None) for o in [filed, filee, tree]]) build_index_from_tree(repo.path, repo.index_path(), repo.object_store, tree.id) # Verify index entries index = repo.open_index() # symlink to d epath = os.path.join(repo.path, 'c', 'e') self.assertTrue(os.path.exists(epath)) self.assertReasonableIndexEntry( index[b'c/e'], stat.S_IFLNK, 0 if sys.platform == 'win32' else 1, filee.id) self.assertFileContents(epath, 'd', symlink=True)
def __pull_beq(repo, local_dir): ''' pulls the git repo but does not use dulwich pull as it has file lock issues on windows ''' from dulwich import porcelain, index with porcelain.open_repo_closing(local_dir) as local_repo: remote_refs = porcelain.fetch(local_repo, repo) local_repo[b"HEAD"] = remote_refs[b"refs/heads/master"] index_file = local_repo.index_path() tree = local_repo[b"HEAD"].tree index.build_index_from_tree(local_repo.path, index_file, local_repo.object_store, tree)
def test_nonempty(self): if os.name != "posix": self.skipTest("test depends on POSIX shell") repo_dir = tempfile.mkdtemp() repo = Repo.init(repo_dir) self.addCleanup(shutil.rmtree, repo_dir) # Populate repo filea = Blob.from_string("file a") fileb = Blob.from_string("file b") filed = Blob.from_string("file d") filee = Blob.from_string("d") tree = Tree() tree["a"] = (stat.S_IFREG | 0o644, filea.id) tree["b"] = (stat.S_IFREG | 0o644, fileb.id) tree["c/d"] = (stat.S_IFREG | 0o644, filed.id) tree["c/e"] = (stat.S_IFLNK, filee.id) # symlink repo.object_store.add_objects([(o, None) for o in [filea, fileb, filed, filee, tree]]) build_index_from_tree(repo.path, repo.index_path(), repo.object_store, tree.id) # Verify index entries index = repo.open_index() self.assertEqual(len(index), 4) # filea apath = os.path.join(repo.path, "a") self.assertTrue(os.path.exists(apath)) self.assertReasonableIndexEntry(index["a"], stat.S_IFREG | 0o644, 6, filea.id) self.assertFileContents(apath, "file a") # fileb bpath = os.path.join(repo.path, "b") self.assertTrue(os.path.exists(bpath)) self.assertReasonableIndexEntry(index["b"], stat.S_IFREG | 0o644, 6, fileb.id) self.assertFileContents(bpath, "file b") # filed dpath = os.path.join(repo.path, "c", "d") self.assertTrue(os.path.exists(dpath)) self.assertReasonableIndexEntry(index["c/d"], stat.S_IFREG | 0o644, 6, filed.id) self.assertFileContents(dpath, "file d") # symlink to d epath = os.path.join(repo.path, "c", "e") self.assertTrue(os.path.exists(epath)) self.assertReasonableIndexEntry(index["c/e"], stat.S_IFLNK, 1, filee.id) self.assertFileContents(epath, "d", symlink=True) # Verify no extra files self.assertEqual([".git", "a", "b", "c"], sorted(os.listdir(repo.path))) self.assertEqual(["d", "e"], sorted(os.listdir(os.path.join(repo.path, "c"))))
def pull(self, wire, url, apply_refs=True, refs=None, update_after=False): if url != 'default' and '://' not in url: client = LocalGitClient(url) else: url_obj = url_parser(url) o = self._build_opener(url) url, _ = url_obj.authinfo() client = HttpGitClient(base_url=url, opener=o) repo = self._factory.repo(wire) determine_wants = repo.object_store.determine_wants_all if refs: def determine_wants_requested(references): return [references[r] for r in references if r in refs] determine_wants = determine_wants_requested try: remote_refs = client.fetch(path=url, target=repo, determine_wants=determine_wants) except NotGitRepository as e: log.warning( 'Trying to fetch from "%s" failed, not a Git repository.', url) # Exception can contain unicode which we convert raise exceptions.AbortException(e)(repr(e)) # mikhail: client.fetch() returns all the remote refs, but fetches only # refs filtered by `determine_wants` function. We need to filter result # as well if refs: remote_refs = {k: remote_refs[k] for k in remote_refs if k in refs} if apply_refs: # TODO: johbo: Needs proper test coverage with a git repository # that contains a tag object, so that we would end up with # a peeled ref at this point. for k in remote_refs: if k.endswith(PEELED_REF_MARKER): log.debug("Skipping peeled reference %s", k) continue repo[k] = remote_refs[k] if refs and not update_after: # mikhail: explicitly set the head to the last ref. repo['HEAD'] = remote_refs[refs[-1]] if update_after: # we want to checkout HEAD repo["HEAD"] = remote_refs["HEAD"] index.build_index_from_tree(repo.path, repo.index_path(), repo.object_store, repo["HEAD"].tree) return remote_refs
def testUpdate(self, event): src = "https://github.com/aaroncox/test.git" client, path = dulwich.client.get_transport_and_path(src) target = self.config['destination'] + "\\test" r = Repo(target) remote_refs = client.fetch(src, r) r["HEAD"] = remote_refs["HEAD"] index.build_index_from_tree(r.path, r.index_path(), r.object_store, r['head'].tree) for pack in r.object_store.packs: pack.close()
def test_nonempty(self): repo_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, repo_dir) with Repo.init(repo_dir) as repo: # Populate repo filea = Blob.from_string(b'file a') fileb = Blob.from_string(b'file b') filed = Blob.from_string(b'file d') tree = Tree() tree[b'a'] = (stat.S_IFREG | 0o644, filea.id) tree[b'b'] = (stat.S_IFREG | 0o644, fileb.id) tree[b'c/d'] = (stat.S_IFREG | 0o644, filed.id) repo.object_store.add_objects([ (o, None) for o in [filea, fileb, filed, tree] ]) build_index_from_tree(repo.path, repo.index_path(), repo.object_store, tree.id) # Verify index entries index = repo.open_index() self.assertEqual(len(index), 3) # filea apath = os.path.join(repo.path, 'a') self.assertTrue(os.path.exists(apath)) self.assertReasonableIndexEntry(index[b'a'], stat.S_IFREG | 0o644, 6, filea.id) self.assertFileContents(apath, b'file a') # fileb bpath = os.path.join(repo.path, 'b') self.assertTrue(os.path.exists(bpath)) self.assertReasonableIndexEntry(index[b'b'], stat.S_IFREG | 0o644, 6, fileb.id) self.assertFileContents(bpath, b'file b') # filed dpath = os.path.join(repo.path, 'c', 'd') self.assertTrue(os.path.exists(dpath)) self.assertReasonableIndexEntry(index[b'c/d'], stat.S_IFREG | 0o644, 6, filed.id) self.assertFileContents(dpath, b'file d') # Verify no extra files self.assertEqual(['.git', 'a', 'b', 'c'], sorted(os.listdir(repo.path))) self.assertEqual(['d'], sorted(os.listdir(os.path.join(repo.path, 'c'))))
def checkout(rep, ref=None): if ref is None: ref = 'refs/heads/master' elif ref.startswith('refs/tags'): ref = rep.ref(ref) if isinstance(rep[ref], Tag): rep['HEAD'] = rep[ref].object[1] else: rep['HEAD'] = rep.refs[ref] indexfile = rep.index_path() tree = rep["HEAD"].tree index.build_index_from_tree(rep.path, indexfile, rep.object_store, tree) return rep.path
def test_nonempty(self): repo_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, repo_dir) with Repo.init(repo_dir) as repo: # Populate repo filea = Blob.from_string(b'file a') fileb = Blob.from_string(b'file b') filed = Blob.from_string(b'file d') tree = Tree() tree[b'a'] = (stat.S_IFREG | 0o644, filea.id) tree[b'b'] = (stat.S_IFREG | 0o644, fileb.id) tree[b'c/d'] = (stat.S_IFREG | 0o644, filed.id) repo.object_store.add_objects([(o, None) for o in [filea, fileb, filed, tree]]) build_index_from_tree(repo.path, repo.index_path(), repo.object_store, tree.id) # Verify index entries index = repo.open_index() self.assertEqual(len(index), 3) # filea apath = os.path.join(repo.path, 'a') self.assertTrue(os.path.exists(apath)) self.assertReasonableIndexEntry(index[b'a'], stat.S_IFREG | 0o644, 6, filea.id) self.assertFileContents(apath, b'file a') # fileb bpath = os.path.join(repo.path, 'b') self.assertTrue(os.path.exists(bpath)) self.assertReasonableIndexEntry(index[b'b'], stat.S_IFREG | 0o644, 6, fileb.id) self.assertFileContents(bpath, b'file b') # filed dpath = os.path.join(repo.path, 'c', 'd') self.assertTrue(os.path.exists(dpath)) self.assertReasonableIndexEntry(index[b'c/d'], stat.S_IFREG | 0o644, 6, filed.id) self.assertFileContents(dpath, b'file d') # Verify no extra files self.assertEqual(['.git', 'a', 'b', 'c'], sorted(os.listdir(repo.path))) self.assertEqual(['d'], sorted(os.listdir(os.path.join(repo.path, 'c'))))
def checkout(rep, ref=None): is_commit = False if ref is None: ref = "refs/heads/master" elif not ref.startswith("refs/"): is_commit = True if is_commit: rep["HEAD"] = rep.commit(ref) else: rep["HEAD"] = rep.refs[ref] indexfile = rep.index_path() tree = rep["HEAD"].tree index.build_index_from_tree(rep.path, indexfile, rep.object_store, tree) return rep.path
def checkout(rep, ref=None): is_commit = False if ref is None: ref = 'refs/heads/master' elif not ref.startswith('refs/'): is_commit = True if is_commit: rep['HEAD'] = rep.commit(ref) else: rep['HEAD'] = rep.refs[ref] indexfile = rep.index_path() tree = rep["HEAD"].tree index.build_index_from_tree(rep.path, indexfile, rep.object_store, tree) return rep.path
def checkout_branch(self, branch='master'): git.update_head(self, branch) self.clean() co_ref = b'HEAD' repo_path = self.path from dulwich.repo import Repo from dulwich.index import build_index_from_tree repo = Repo(repo_path) indexfile = repo.index_path() obj_sto = repo.object_store tree_id = repo[co_ref].tree build_index_from_tree(repo_path, indexfile, obj_sto, tree_id) x = list(obj_sto.iter_tree_contents(tree_id)) x = [obj_sto.iter_tree_contents(tree_id)]
def reset(repo, mode, committish="HEAD"): """Reset current HEAD to the specified state. :param repo: Path to repository :param mode: Mode ("hard", "soft", "mixed") """ if mode != "hard": raise ValueError("hard is the only mode currently supported") r = open_repo(repo) indexfile = r.index_path() tree = r[committish].tree index.build_index_from_tree(r.path, indexfile, r.object_store, tree)
def clone_tmp(repo_url, branch='master', base_dir="/tmp/repos/"): logger.debug("clone repo_url={0}, branch={1}".format(repo_url, branch)) folder = tempfile.mkdtemp(dir=base_dir) logger.debug("folder = {0}".format(folder)) rep = Repo.init(folder) client, relative_path = get_transport_and_path(repo_url) logger.debug("client={0}".format(client)) remote_refs = client.fetch(relative_path, rep) rep['HEAD'] = remote_refs['refs/heads/{0}'.format(branch)] indexfile = rep.index_path() tree = rep["HEAD"].tree index.build_index_from_tree(rep.path, indexfile, rep.object_store, tree) logger.debug("done") return folder
def test_empty(self): repo_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, repo_dir) with Repo.init(repo_dir) as repo: tree = Tree() repo.object_store.add_object(tree) build_index_from_tree(repo.path, repo.index_path(), repo.object_store, tree.id) # Verify index entries index = repo.open_index() self.assertEqual(len(index), 0) # Verify no files self.assertEqual(['.git'], os.listdir(repo.path))
def _checkout_tree(self, tree): return build_index_from_tree( self.repo.path, self.repo.index_path(), self.repo.object_store, tree )
def reset_index(self, tree=None): """Reset the index back to a specific tree. :param tree: Tree SHA to reset to, None for current HEAD tree. """ from dulwich.index import ( build_index_from_tree, validate_path_element_default, validate_path_element_ntfs, ) if tree is None: tree = self[b'HEAD'].tree config = self.get_config() honor_filemode = config.get_boolean(b'core', b'filemode', os.name != "nt") if config.get_boolean(b'core', b'core.protectNTFS', os.name == "nt"): validate_path_element = validate_path_element_ntfs else: validate_path_element = validate_path_element_default return build_index_from_tree( self.path, self.index_path(), self.object_store, tree, honor_filemode=honor_filemode, validate_path_element=validate_path_element)
def _build_tree(self): from dulwich.index import build_index_from_tree config = self.get_config() honor_filemode = config.get_boolean('core', 'filemode', os.name != "nt") return build_index_from_tree(self.path, self.index_path(), self.object_store, self['HEAD'].tree, honor_filemode=honor_filemode)
def clone(self, wire, url, deferred, valid_refs, update_after_clone): remote_refs = self.fetch(wire, url, apply_refs=False) repo = self._factory.repo(wire) if isinstance(valid_refs, list): valid_refs = tuple(valid_refs) for k in remote_refs: # only parse heads/tags and skip so called deferred tags if k.startswith(valid_refs) and not k.endswith(deferred): repo[k] = remote_refs[k] if update_after_clone: # we want to checkout HEAD repo["HEAD"] = remote_refs["HEAD"] index.build_index_from_tree(repo.path, repo.index_path(), repo.object_store, repo["HEAD"].tree)
def checkout(repo_path='.', co_ref='HEAD'): """ Checkout a reference from a Git repository :param repo_path: <str> path of repository :param co_ref: <str> name of checkout reference :return entries: <TreeEntry> list of named tuples """ # TODO: add all checkout options repo = Repo(repo_path) indexfile = repo.index_path() obj_sto = repo.object_store # TODO: catch if not a reference tree_id = repo[co_ref].tree # TODO: error out if unstaged or uncommited files build_index_from_tree(repo_path,indexfile,obj_sto,tree_id) return [obj_sto.iter_tree_contents(tree_id)]
def checkout_ref(self) -> None: """ Checkout a specific ref from the repo """ try: from dulwich.index import build_index_from_tree except ImportError as exc: raise ImportError( "Unable to import dulwich, please ensure you have installed the git extra" ) from exc build_index_from_tree( self.repo.path, self.repo.index_path(), self.repo.object_store, self.get_tree_id_for_branch_or_tag(), )
def test_no_decode_encode(self): repo_dir = tempfile.mkdtemp() repo_dir_bytes = os.fsencode(repo_dir) self.addCleanup(shutil.rmtree, repo_dir) with Repo.init(repo_dir) as repo: # Populate repo file = Blob.from_string(b'foo') tree = Tree() latin1_name = u'À'.encode('latin1') latin1_path = os.path.join(repo_dir_bytes, latin1_name) utf8_name = u'À'.encode('utf8') utf8_path = os.path.join(repo_dir_bytes, utf8_name) tree[latin1_name] = (stat.S_IFREG | 0o644, file.id) tree[utf8_name] = (stat.S_IFREG | 0o644, file.id) repo.object_store.add_objects([(o, None) for o in [file, tree]]) try: build_index_from_tree(repo.path, repo.index_path(), repo.object_store, tree.id) except OSError as e: if e.errno == 92 and sys.platform == 'darwin': # Our filename isn't supported by the platform :( self.skipTest('can not write filename %r' % e.filename) else: raise except UnicodeDecodeError: # This happens e.g. with python3.6 on Windows. # It implicitly decodes using utf8, which doesn't work. self.skipTest('can not implicitly convert as utf8') # Verify index entries index = repo.open_index() self.assertIn(latin1_name, index) self.assertIn(utf8_name, index) self.assertTrue(os.path.exists(latin1_path)) self.assertTrue(os.path.exists(utf8_path))
def test_git_submodule_exists(self): repo_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, repo_dir) with Repo.init(repo_dir) as repo: filea = Blob.from_string(b'file alalala') subtree = Tree() subtree[b'a'] = (stat.S_IFREG | 0o644, filea.id) c = Commit() c.tree = subtree.id c.committer = c.author = b'Somebody <*****@*****.**>' c.commit_time = c.author_time = 42342 c.commit_timezone = c.author_timezone = 0 c.parents = [] c.message = b'Subcommit' tree = Tree() tree[b'c'] = (S_IFGITLINK, c.id) os.mkdir(os.path.join(repo_dir, 'c')) repo.object_store.add_objects( [(o, None) for o in [tree]]) build_index_from_tree(repo.path, repo.index_path(), repo.object_store, tree.id) # Verify index entries index = repo.open_index() self.assertEqual(len(index), 1) # filea apath = os.path.join(repo.path, 'c/a') self.assertFalse(os.path.exists(apath)) # dir c cpath = os.path.join(repo.path, 'c') self.assertTrue(os.path.isdir(cpath)) self.assertEqual(index[b'c'][4], S_IFGITLINK) # mode self.assertEqual(index[b'c'][8], c.id) # sha
def test_git_submodule_exists(self): repo_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, repo_dir) with Repo.init(repo_dir) as repo: filea = Blob.from_string(b'file alalala') subtree = Tree() subtree[b'a'] = (stat.S_IFREG | 0o644, filea.id) c = Commit() c.tree = subtree.id c.committer = c.author = b'Somebody <*****@*****.**>' c.commit_time = c.author_time = 42342 c.commit_timezone = c.author_timezone = 0 c.parents = [] c.message = b'Subcommit' tree = Tree() tree[b'c'] = (S_IFGITLINK, c.id) os.mkdir(os.path.join(repo_dir, 'c')) repo.object_store.add_objects( [(o, None) for o in [tree]]) build_index_from_tree( repo.path, repo.index_path(), repo.object_store, tree.id) # Verify index entries index = repo.open_index() self.assertEqual(len(index), 1) # filea apath = os.path.join(repo.path, 'c/a') self.assertFalse(os.path.exists(apath)) # dir c cpath = os.path.join(repo.path, 'c') self.assertTrue(os.path.isdir(cpath)) self.assertEqual(index[b'c'][4], S_IFGITLINK) # mode self.assertEqual(index[b'c'][8], c.id) # sha
def test_no_decode_encode(self): repo_dir = tempfile.mkdtemp() repo_dir_bytes = repo_dir.encode(sys.getfilesystemencoding()) self.addCleanup(shutil.rmtree, repo_dir) with Repo.init(repo_dir) as repo: # Populate repo file = Blob.from_string(b'foo') tree = Tree() latin1_name = u'À'.encode('latin1') latin1_path = os.path.join(repo_dir_bytes, latin1_name) utf8_name = u'À'.encode('utf8') utf8_path = os.path.join(repo_dir_bytes, utf8_name) tree[latin1_name] = (stat.S_IFREG | 0o644, file.id) tree[utf8_name] = (stat.S_IFREG | 0o644, file.id) repo.object_store.add_objects( [(o, None) for o in [file, tree]]) try: os.path.exists(latin1_path) except UnicodeDecodeError: # This happens e.g. with python3.6 on Windows. # It implicitly decodes using utf8, which doesn't work. self.skipTest('can not implicitly convert as utf8') build_index_from_tree( repo.path, repo.index_path(), repo.object_store, tree.id) # Verify index entries index = repo.open_index() self.assertIn(latin1_name, index) self.assertIn(utf8_name, index) self.assertTrue(os.path.exists(latin1_path)) self.assertTrue(os.path.exists(utf8_path))