Git command line scam notes

Recently, when writing the packaging pipeline, I encountered several pits, so I will summarize it a little.

Pull the remote branch if it exists, checkout if it does not exist

Full pull

1
2
3
4
5
6
7
8
9
10
11
12
git clone -b master "${git_url}" "${products_dir}"
cd "${products_dir}"
if [ "git branch -r | grep $branch" ]; then
echo "remote $branch existed"
git fetch --all
git switch -c "${branch}" "origin/${branch}"
git pull
else
echo "remote $branch not existed"
git checkout -b "${branch}"
git push -u origin "${branch}"
fi

There is a pit to pay attention to here, that is, you can’t use ‘–depth = 1’ when ** cloning, which will cause you to only pull the information of the branch in the future, and the other branches do not exist no matter how you check **

But sometimes our repository is too big, we just want to pull the latest version of the branch how to do, can pull success pull success, pull success to create a branch.

Sounds fine, but if you write directly, you will encounter problems, that is, the entire shell will exit if you fail to pull it, so is there a way to solve it?

There are two ways, the first is to use subprocess.

Subprocess tries to pull the latest version

1
2
3
4
5
6
7
8
9
git_clone=$(git clone--depth=1 -b "${branch}" "${git_url}" "${products_dir}")
if [[ -z ${git_clone} ]]; then
echo "remote $branch existed"
else
echo "remote $branch not existed"
git clone--depth=1 -b master "${git_url}" "${products_dir}"
git checkout -b "${branch}"
git push -u origin "${branch}"
fi

The internal part of '$ () ’ is subprocess, its failure will not cause the shell to exit directly

set

** Set set + e in the head of the shell to ensure that the failure does not exit, and then check whether the command is executed successfully **

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash 
set +e

git clone --depth=1 -b "$branch" "${git_url}" "${products_dir}"
if [[ $? -ne 0 ]]; then
echo "remote $branch not existed"
git clone--depth=1 -b master "${git_url}" "${products_dir}"
git checkout -b "${branch}"
git push -u origin "${branch}"
else
echo "remote $branch existed"
fi

‘$?’ means to get the execution result of the previous instruction. If the result is 0, it means success.

If the repository uses lfs, it is best to use git

That is to say, if your repository uses lfs, then try to use’git lfs clone ‘instead of’git clone’, the same is true for other commands

android:requestLegacyExternalStorage

If you encounter such an error during the packaging of the APK: ‘AAPT: error: attribute android: requestLegacyExternalStorage not found.’

This problem occurs when the project build.gradle - compileSdkVersion is configured with a version less than 29 and also configures the android: requestLegacyExternalStorage property in AndroidManifest.xml - application (whether it is configured by your own project or the third-party library).
This property is mainly used because of the storage permission adjustment of Android 10.0, a temporary solution launched by Google.

This blog describes two solutions: https://juejin.cn/post/6850418117093523464

However, these two methods do not apply to our situation, because we are directly generated APK, there is no way to pause the operation of the middle of the Gradle product, of course, we can say that the first package Gradle project out, and then build APK

However, Unity officially provides a method. We can define the’AndroidManifest.xml 'of the intermediate Gradle product. There are two types here. One is to define the unityLibrary, the other is to define the launcher, and we take the second

Then change the generated’Assets/Plugins/Android/LauncherManifest.xml 'to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<!-- GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN-->
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
xmlns:tools="http://schemas.android.com/tools"
android:installLocation="preferExternal">
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>

<application android:extractNativeLibs="true"
android:label="@string/app_name"
android:icon="@mipmap/app_icon"
tools:remove="android:requestLegacyExternalStorage"/>
</manifest>

Mainly add’tools: remove = “android: requestLegacyExternalStorage” 'in the application tag