GitHub上有一个名为platform_frameworks_base的Git存储库,其中包含部分Android源代码。
我编写了一个应用程序,该应用程序可以对该项目中的所有.aidl文件进行回复,因此它会在首次启动时下载所有文件。
到目前为止,我是通过从项目根目录下载Android.bp文件,从该文件中提取所有以.aidl结尾的文件路径,然后显式地逐一下载来完成的。
例如,如果我找到此文件路径:
media/java/android/media/IAudioService.aidl
我知道我可以这样下载:wget https://raw.githubusercontent.com/aosp-mirror/platform_frameworks_base/android-10.0.0_r47/media/java/android/media/IAudioService.aidl
直到Android 10(git标签:android-10.0.0_r47
)都可以正常工作。从Android 11开始(例如git标签:
android-11.0.0_r33
),文件路径使用加引号而不是完整路径。看到这个Android.bp。现在,它仅包含通配符/全局文件路径,例如:
media/java/**/*.aidl
location/java/**/*.aidl
等等...我当前的“解决方案”:
git clone --depth=1 -b android-11.0.0_r33 https://github.com/aosp-mirror/platform_frameworks_base.git
cat Android.bp | grep '\.aidl"' | cut -d'"' -f2
例如
shopt -s globstar && echo media/java/**/*.aidl
但是下载过程要花很长时间,因为该存储库包含超过 GB的二进制文件。即使我只是克隆了我关心的分支的最后一次提交。
现在我的实际问题是:
我如何才能下载我真正关心的
.aidl
文件? (理想情况下,无需解析GitHub中每个文件夹的HTML。)或者
如何在没有所有二进制文件的情况下下载/克隆存储库? (可能无法使用git吗?)
编辑:
我尝试使用GitHub API递归遍历所有目录,但是我立即收到API速率限制超出错误:
g_aidlFiles=""
# Recursively go through all directories and the paths to all found .aidl files in the global g_aidlFile variable
GetAidlFilesFromGithub() {
l_dirUrl="${1-}"
if [ "$l_dirUrl" == "" ]; then
echo "ERROR: Directory URL not provided in GetAidlFilesFromGithub"
exit 1
fi
echo "l_dirUrl: ${l_dirUrl}"
l_rawRes="$(curl -s -i $l_dirUrl)"
l_statusCode="$(echo "$l_rawRes" | grep HTTP | head -1 | cut -d' ' -f2)"
l_resBody="$(echo "$l_rawRes" | sed '1,/^\s*$/d')"
if [[ $l_statusCode == 4* ]] || [[ $l_statusCode == 5* ]]; then
echo "ERROR: Request failed!"
echo "Response status: $l_statusCode"
echo "Reponse body:"
echo "$l_resBody"
exit 1
fi
l_currentDirJson="$(echo "$l_resBody")"
if [ "$l_currentDirJson" == "" ]; then
echo "ERROR: l_currentDirJson is empty"
exit 1
fi
l_newAidlFiles="$(echo "$l_currentDirJson" | jq '.[] | select(.type=="file") | select(.path | endswith(".aidl")) | .path')"
if [ "$l_newAidlFiles" != "" ]; then
echo "l_newAidlFiles: ${l_newAidlFiles}"
g_aidlFiles="${g_aidlFiles}\n${l_newAidlFiles}"
fi
l_subDirUrls="$(echo "$l_currentDirJson" | jq '.[] | select(.type=="dir") | .url')"
if [ "$l_subDirUrls" != "" ]; then
echo "$l_subDirUrls" | while IFS= read -r l_subDirUrl ; do
(GetAidlFilesFromGithub "$l_subDirUrl")
done
else
echo "No subdirs found."
fi
}
GetAidlFilesFromGithub "https://api.github.com/repos/aosp-mirror/platform_frameworks_base/contents?ref=android-11.0.0_r33"
据我了解,我所有的用户都必须创建一个GitHub帐户并创建一个OAUTH密码以提高限制。这绝对不是我的选择。我希望我的应用程序易于使用。
最佳答案
不确定这是否是您想要的:
#!/usr/bin/env bash
get_github_file_list(){
local user=$1 repo=$2 branch=$3
curl -s "https://api.github.com/repos/$user/$repo/git/trees/$branch?recursive=1"
}
get_github_file_list aosp-mirror platform_frameworks_base android-11.0.0_r33 |\
jq -r '.tree|map(.path|select(test("\\.aidl")))[]'
关于bash - 从GitHub存储库克隆/下载特定文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66600692/