How can I access the directories of a branch, after getting it ?
Even after
b = repo.get_branch(branch="XYZ") on downloading repo.get_contents(), I still get master branch contents.
Same issue here. Contents are the master branch version. Steps:
github = Github(api_key)
repo = github.get_repo(f"{org_name}/{repo_name}")
repo.get_branch(branch=branch)
file_contents = repo.get_contents(target)
Versions tested:
Workaround:
def get_sha_for_tag(repository, tag):
branches = repository.get_branches()
matched_branches = [match for match in branches if match.name == tag]
if matched_branches:
return matched_branches[0].commit.sha
tags = repository.get_tags()
matched_tags = [match for match in tags if match.name == tag]
if not matched_tags:
raise ValueError("No Tag or Branch exists with that name")
return matched_tags[0].commit.sha
github = Github(api_key)
repo = github.get_repo(f"{org_name}/{repo_name}")
sha = get_sha_for_tag(repo, branch)
file_contents = repo.get_contents(target, ref=sha)
@S-Dutta1 why did you close this? This should still be an open issue.
I thought the workaround you suggested was enough for the purpose, thus closed it. Opening it again.
I don't feel this is a bug.
repo.get_branch() only returns the branch object from GitHub, it doesn't change what branch the repository object is pointing to.
Since repo.get_contents() still operates on the repository itself, it's going to default to master, as the GitHub docs say.
Building on your example:
b = repo.get_branch(branch="XYZ")
file = repo.get_contents(path="foo/bar", ref=b.commit.sha)
Hi,
I am trying to download contents of a master branch using the same code snippet. However when I execute get_contents
contents = repository.get_contents(server_path, ref=sha)
I get error:
github.GithubException.UnknownObjectException: 404 {"message": "Not Found", "documentation_url": "https://docs.github.com/rest/reference/repos#get-repository-content"}
Please provide some pointer
just try like this
Hi,
I am trying to download contents of a master branch using the same code snippet. However when I execute get_contents
contents = repository.get_contents(server_path, ref=sha)I get error:
github.GithubException.UnknownObjectException: 404 {"message": "Not Found", "documentation_url": "https://docs.github.com/rest/reference/repos#get-repository-content"}
Please provide some pointer
just try like this it will give you default branch content
contents = repository.get_contents("")
Thank you. This worked. However now I get another exception:
Code snippet:
contents = repository.get_contents("")
for content in contents:
print("Processing %s" % content.path)
if content.type == 'dir':
print(content.path)
download_directory(repository, sha, content.path)
else:
try:
path = content.path
file_content = repository.get_contents(path, ref=sha)
file_data = base64.b64decode(file_content.content)
file_out = open(content.name, "w")
file_out.write(file_data)
file_out.close()
except (GithubException, IOError) as exc:
logging.error('Error processing %s: %s', content.path, exc)
Exception:
Traceback (most recent call last):
File "C:\JobSaga39\venv\Scripts\DumpGit.py", line 38, in
download_directory
download_directory(repository, sha, content.path)
File "C:\JobSaga39\venv\Scripts\DumpGit.py", line 38, in
download_directory
download_directory(repository, sha, content.path)
File "C:\JobSaga39\venv\Scripts\DumpGit.py", line 38, in
download_directory
download_directory(repository, sha, content.path)
[Previous line repeated 968 more times]
File "C:\JobSaga39\venv\Scripts\DumpGit.py", line 32, in
download_directory
contents = repository.get_contents("")
File "C:\JobSaga39\venv\lib\site-packages\github\Repository.py", line
1756, in get_contents
headers, data = self._requester.requestJsonAndCheck(
File "C:\JobSaga39\venv\lib\site-packages\github\Requester.py", line 316,
in requestJsonAndCheck
self.requestJson(
File "C:\JobSaga39\venv\lib\site-packages\github\Requester.py", line 408,
in requestJson
return self.__requestEncode(cnx, verb, url, parameters, headers, input,
encode)
File "C:\JobSaga39\venv\lib\site-packages\github\Requester.py", line 484,
in __requestEncode
status, responseHeaders, output = self.__requestRaw(
File "C:\JobSaga39\venv\lib\site-packages\github\Requester.py", line 511,
in __requestRaw
response = cnx.getresponse()
File "C:\JobSaga39\venv\lib\site-packages\github\Requester.py", line 108,
in getresponse
r = verb(
File "C:\JobSaga39\venv\lib\site-packages\requests\sessions.py", line
555, in get
return self.request('GET', url, *kwargs)
File "C:\JobSaga39\venv\lib\site-packages\requests\sessions.py", line
542, in request
resp = self.send(prep, *send_kwargs)
File "C:\JobSaga39\venv\lib\site-packages\requests\sessions.py", line
655, in send
r = adapter.send(request, *kwargs)
File "C:\JobSaga39\venv\lib\site-packages\requests\adapters.py", line
439, in send
resp = conn.urlopen(
File "C:\JobSaga39\venv\lib\site-packages\urllib3\connectionpool.py",
line 699, in urlopen
httplib_response = self._make_request(
File "C:\JobSaga39\venv\lib\site-packages\urllib3\connectionpool.py",
line 445, in _make_request
six.raise_from(e, None)
File "
File "C:\JobSaga39\venv\lib\site-packages\urllib3\connectionpool.py",
line 440, in _make_request
httplib_response = conn.getresponse()
File "C:\Program
Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\lib\http\client.py",
line 1345, in getresponse
response.begin()
File "C:\Program
Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\lib\http\client.py",
line 331, in begin
self.headers = self.msg = parse_headers(self.fp)
File "C:\Program
Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\lib\http\client.py",
line 225, in parse_headers
return email.parser.Parser(_class=_class).parsestr(hstring)
File "C:\Program
Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\lib\email\parser.py",
line 67, in parsestr
return self.parse(StringIO(text), headersonly=headersonly)
File "C:\Program
Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\lib\email\parser.py",
line 56, in parse
feedparser.feed(data)
File "C:\Program
Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\lib\email\feedparser.py",
line 176, in feed
self._call_parse()
File "C:\Program
Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\lib\email\feedparser.py",
line 180, in _call_parse
self._parse()
File "C:\Program
Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\lib\email\feedparser.py",
line 295, in _parsegen
if self._cur.get_content_maintype() == 'message':
File "C:\Program
Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\lib\email\message.py",
line 594, in get_content_maintype
ctype = self.get_content_type()
File "C:\Program
Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\lib\email\message.py",
line 578, in get_content_type
value = self.get('content-type', missing)
File "C:\Program
Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\lib\email\message.py",
line 471, in get
return self.policy.header_fetch_parse(k, v)
File "C:\Program
Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\lib\email_policybase.py",
line 316, in header_fetch_parse
return self._sanitize_header(name, value)
File "C:\Program
Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\lib\email_policybase.py",
line 287, in _sanitize_header
if _has_surrogates(value):
File "C:\Program
Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1264.0_x64__qbz5n2kfra8p0\lib\email\utils.py",
line 57, in _has_surrogates
s.encode()
RecursionError: maximum recursion depth exceeded while calling a Python
object
On Fri, Apr 30, 2021 at 6:31 AM rahulRB @.*> wrote:
just try like this
Hi,
I am trying to download contents of a master branch using the same code
snippet. However when I execute get_contents
contents = repository.get_contents(server_path, ref=sha)I get error:
github.GithubException.UnknownObjectException: 404 {"message": "Not
Found", "documentation_url": "
https://docs.github.com/rest/reference/repos#get-repository-content"}
Please provide some pointerjust try like this it will give you default branch content
contents = repository.get_contents("")—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/PyGithub/PyGithub/issues/1258#issuecomment-830002251,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ATYUAMVSZOXJP5OGSFRM4TDTLKBH5ANCNFSM4JAMXZRA
.
Most helpful comment
Same issue here. Contents are the master branch version. Steps:
github = Github(api_key)repo = github.get_repo(f"{org_name}/{repo_name}")repo.get_branch(branch=branch)file_contents = repo.get_contents(target)Versions tested:
Workaround: