electron 的depot_tools 工具下载构建源码 nodejs写的
electron 的depot_tools 工具下载构建源码。这个工具是用nodejs写的,封装了chromium自身的depot_tools工具。非常方便易用。
主要是electron在下载完chromium源码后会打自己的patch。如果用chromium的gclient下载会麻烦,报patch conflicts。这个electron自己用nodejs写的工具就不会了。
参考自:https://github.com/electron/build-tools
1,预先安装:linux下:
git必须2.2版本以上。
1, 安装nodejs
sudo apt-get install -y nodejs 或者 yum install nodejs
node -v 看版本低
sudo npm i -g n 安装n工具
sudo n lts node更新到了最新
2,下载eletron构建工具:
npm i -g @electron/build-tools (总有权限问题,不用-g安装在了当前目录)
也可用yarn安装:sudo yarn global add @electron/build-tools
如若报错,须运行如下,解决nodejs缺失很多包:
git clone https://github.com/electron/build-tools ~/.electron_build_tools && (cd ~/.electron_build_tools && npm install)
3,安装python
sudo apt install python
windows上
1,可以用chocolate或者scoop安装。安装方法在 :https://classic.yarnpkg.com/en/docs/install/
安装chocolate
1,启动 powsershell 用管理员权限
2,运行 Get-ExecutionPolicy
如果返回Restricted
执行:
Set-ExecutionPolicy AllSigned or Set-ExecutionPolicy Bypass -Scope Process
3,运行下面命令会安装choco软件
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
4,测试安装成功否
choco
choco -?
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\xxx\AppData\Roaming
ChocolateyInstall=C:\ProgramData\chocolatey
ChocolateyLastPathUpdate=132366682081775967
2,
choco必须在cmd里面运行:
choco install git python nodejs yarn
refreshenv
安装 git,python,nodejs,yarn(linux下用)或者npm。
(git需要git.exe在路径上,而不是google的depot tools里面的git.bat。那样python就不能运行git了,需要加上shell=ture)
下载eletron构建工具:
npm i -g @electron/build-tools
需要安装glibc编译库在linux。如果报错:vpython src/buildtools/ensure_gn_version.py git_revision:e002e68a48d1c82648eadde2f6aafa20d08c36f2' in '/root/electron/e11'
`/root/electron/e11/src/buildtools/linux64/gn --version` returned 1:
/root/electron/e11/src/buildtools/linux64/gn: /lib64/libc.so.6: version `GLIBC_2.18' not found (required by /root/electron/e11/src/buildtools/linux64/gn)
解决方法
yum install gcc
wget http://ftp.gnu.org/gnu/glibc/glibc-2.18.tar.gz
tar zxf glibc-2.18.tar.gz
cd glibc-2.18/
mkdir build
cd build/
../configure --prefix=/usr
make -j4
make install
报错:确实bzip2:
yum search bzip2
yum install bzip2.x86_64
windows上需要安装vs 2019.
2, 源码下载并且编译构建- e init --root=~/electron
参数构建时才有用:
-i, --import <name> Import build settings from $root/src/electron/build/args/$import.gn (default: "testing")
--bootstrap 会全做,包括同步,构建。
如果想做多个分支版本:
`--root=~/electron/branch` (e.g. `~/electron-gn/master`)
喝咖啡去吧。
1,e init 这个会看目录下有没有chromium的depot_tools,没有的话会去下载。
实例: e init e11 --root ./e11
命令行参数有
1)给这次构建起个名字,如branch7,chocolate,随意。后面接release 或者 testing。以后切换构建用: e use branch7.
2)设定源码存放的根目录,多个构建tesing,debug,release可以重复用它。
示例:master-testing,master-release都是起的名字配置
命令执行后会生成配置文件,操作系统的环境变量可以放在json文件的env里面。(存放目录C:\Users\Administrator\.electron_build_tools\configs):
{
"goma": "none",
"root": "C:\\dev\\electron7",
"origin": {
"electron": "git@github.com:electron/electron.git",
"node": "git@github.com:electron/node.git"
},
"gen": {
"args": [
"import(\"//electron/build/args/testing.gn\")"
],
"out": "Testing"
},
"env": {
"CHROMIUM_BUILDTOOLS_PATH": "C:\\dev\\electron7\\src\\buildtools",
"GIT_CACHE_PATH": "C:\\Users\\Administrator\\.git_cache",
"DEPOT_TOOLS_WIN_TOOLCHAIN": 0 #只适用于windows。如果下载机器上没有装vs2019,这个值需要置1,下载谷歌内部的vs,只有内部人员才有权限使用。编译时置0,用自己本机的。
}
}
和depot_tools 使用目录有关的源码在 .electron_build_tools\src\下:
evm-config.js
\utils\depot-tools.js
DEPOT_TOOLS_WIN_TOOLCHAIN为1时,log输出:
[1/4] Resolving packages...
success Already up-to-date.
Done in 3.50s.
Hook 'vpython.bat -c 'import os, subprocess; os.chdir(os.path.join("src", "electron")); subprocess.check_call(["python", "script/lib/npx.py", "yarn@1.15.2", "install", "--frozen-lockfile"]);'' took 12.70 secs
________ running 'vpython.bat src/build/landmines.py' in 'C:\zhibin\electron9.2.1'
________ running 'vpython.bat src/third_party/depot_tools/update_depot_tools_toggle.py --disable' in 'C:\zhibin\electron9.2.1'
________ running 'vpython.bat src/tools/remove_stale_pyc_files.py src/android_webview/tools src/build/android src/gpu/gles2_conform_support src/infra src/ppapi src/printing src/third_party/blink/renderer/build/scripts src/third_party/blink/tools src/third_party/catapult src/tools' in 'C:\zhibin\electron9.2.1'
________ running 'vpython.bat src/buildtools/ensure_gn_version.py git_revision:5ed3c9cc67b090d5e311e4bd2aba072173e82db9' in 'C:\zhibin\electron9.2.1'
________ running 'vpython.bat src/build/vs_toolchain.py update --force' in 'C:\zhibin\electron9.2.1'
Windows toolchain out of date or doesn't exist, updating (Pro)...
current_hashes:
desired_hash: 6d205e765a23d3cbe0fcc8d1191ae406d8bf9c04
Extracting c:\users\administrator\appdata\local\temp\tmp8nmtsz\6d205e765a23d3cbe0fcc8d1191ae406d8bf9c04.zip...
Calculating hash of toolchain in vs_files\6d205e765a23d3cbe0fcc8d1191ae406d8bf9c04. Please wait...
Hook 'vpython.bat src/build/vs_toolchain.py update --force' took 686.70 secs
________ running 'vpython.bat src/tools/clang/scripts/update.py' in 'C:\zhibin\electron9.2.1'
Downloading https://commondatastorage.googleapis.com/chromium-browser-clang/Win/clang-n346557-4e0d9925-2.tgz .......... Done.
Copying C:\zhibin\electron9.2.1\src\third_party\depot_tools\win_toolchain\vs_files\6d205e765a23d3cbe0fcc8d1191ae406d8bf9c04\win_sdk\..\DIA SDK\bin\amd64\msdia140.dll to C:\zhibin\electron9.2.1\src\third_party\llvm-build\Release+Asserts\bin
Hook 'vpython.bat src/tools/clang/scripts/update.py' took 23.48 secs
________ running 'vpython.bat src/build/util/lastchange.py -o src/build/util/LASTCHANGE' in 'C:\zhibin\electron9.2.1'
Hook 'vpython.bat src/build/util/lastchange.py -o src/build/util/LASTCHANGE' took 10.01 secs
________ running 'vpython.bat src/build/util/lastchange.py -m GPU_LISTS_VERSION --revision-id-only --header src/gpu/config/gpu_lists_version.h' in 'C:\zhibin\electron9.2.1'
________ running 'vpython.bat src/build/util/lastchange.py -m SKIA_COMMIT_HASH -s src/third_party/skia --header src/skia/ext/skia_commit_hash.h' in 'C:\zhibin\electron9.2.1'
________ running 'vpython.bat src/third_party/depot_tools/download_from_google_storage.py --no_resume --no_auth --bucket chromium-clang-format -s src/buildtools/win/clang-format.exe.sha1' in 'C:\zhibin\electron9.2.1'
________ running 'vpython.bat src/third_party/depot_tools/download_from_google_storage.py --no_resume --no_auth --bucket chromium-browser-clang/rc -s src/build/toolchain/win/rc/win/rc.exe.sha1' in 'C:\zhibin\electron9.2.1'
________ running 'download_from_google_storage --no_resume --extract --no_auth --bucket chromium-fonts -s src/third_party/test_fonts/test_fonts.tar.gz.sha1' in 'C:\zhibin\electron9.2.1'
0> Downloading src/third_party/test_fonts/test_fonts.tar.gz@a22de844e32a3f720d219e3911c3da3478039f89...
0> Removed src/third_party/test_fonts/test_fonts...
0> Extracting 30 entries from src/third_party/test_fonts/test_fonts.tar.gz to src/third_party/test_fonts/test_fonts
Downloading 1 files took 68.306000 second(s)
Hook 'download_from_google_storage --no_resume --extract --no_auth --bucket chromium-fonts -s src/third_party/test_fonts/test_fonts.tar.gz.sha1' took 71.11 secs
________ running 'download_from_google_storage --no_auth --quiet --bucket chromium-webrtc-resources -d src/third_party/opus/tests/resources' in 'C:\zhibin\electron9.2.1'
________ running 'vpython.bat src/third_party/depot_tools/download_from_google_storage.py --no_resume --no_auth --num_threads=4 --bucket chromium-browser-clang/orderfiles -d src/chrome/build' in 'C:\zhibin\electron9.2.1'
________ running 'vpython.bat src/third_party/depot_tools/download_from_google_storage.py --no_resume --directory --recursive --no_auth --num_threads=16 --bucket chromium-apache-win32 src/third_party/apache-win32' in 'C:\zhibin\electron9.2.1'
Hook 'vpython.bat src/third_party/depot_tools/download_from_google_storage.py --no_resume --directory --recursive --no_auth --num_threads=16 --bucket chromium-apache-win32 src/third_party/apache-win32' took 16.20 secs
________ running 'vpython.bat src/third_party/depot_tools/download_from_google_storage.py --no_resume --no_auth -u --bucket v8-wasm-fuzzer -s src/v8/test/fuzzer/wasm_corpus.tar.gz.sha1' in 'C:\zhibin\electron9.2.1'
________ running 'vpython.bat src/third_party/depot_tools/download_from_google_storage.py --no_resume --no_auth --bucket chromium-nodejs/12.14.1 -s src/third_party/node/win/node.exe.sha1' in 'C:\zhibin\electron9.2.1'
________ running 'vpython.bat src/third_party/depot_tools/download_from_google_storage.py --no_resume --extract --no_auth --bucket chromium-nodejs -s src/third_party/node/node_modules.tar.gz.sha1' in 'C:\zhibin\electron9.2.1'
________ running 'vpython.bat src/third_party/depot_tools/download_from_google_storage.py --no_resume --no_auth --num_threads=4 --bucket chromium-tools-traffic_annotation -d src/tools/traffic_annotation/bin/win32' in 'C:\zhibin\electron9.2.1'
0> Downloading src/tools/traffic_annotation/bin/win32\traffic_annotation_auditor.exe...
Downloading 1 files took 55.001000 second(s)
Hook 'vpython.bat src/third_party/depot_tools/download_from_google_storage.py --no_resume --no_auth --num_threads=4 --bucket chromium-tools-traffic_annotation -d src/tools/traffic_annotation/bin/win32' took 63.31 secs
________ running 'vpython.bat src/third_party/depot_tools/download_from_google_storage.py --no_resume --no_auth --num_threads=4 --bucket chromium-binary-patching/zucchini_testdata --recursive -d src/components/zucchini' in 'C:\zhibin\electron9.2.1'
Hook 'vpython.bat src/third_party/depot_tools/download_from_google_storage.py --no_resume --no_auth --num_threads=4 --bucket chromium-binary-patching/zucchini_testdata --recursive -d src/components/zucchini' took 10.18 secs
________ running 'vpython.bat src/third_party/depot_tools/download_from_google_storage.py --no_resume --no_auth --bucket chromium-ads-detection -s src/third_party/subresource-filter-ruleset/data/UnindexedRules.sha1' in 'C:\zhibin\electron9.2.1'
0> Downloading src/third_party/subresource-filter-ruleset/data/UnindexedRules...
Downloading 1 files took 67.118000 second(s)
Hook 'vpython.bat src/third_party/depot_tools/download_from_google_storage.py --no_resume --no_auth --bucket chromium-ads-detection -s src/third_party/subresource-filter-ruleset/data/UnindexedRules.sha1' took 73.81 secs
________ running 'vpython.bat -vpython-spec src/.vpython -vpython-tool install' in 'C:\zhibin\electron9.2.1'
View Code
示例:
# making 'release' and 'testing' builds from master
$ e init master-testing --root=~/src/electron
Creating '~/src/electron'
New build config 'master-testing' created
Now using config 'master-testing'
$ e show current
master-testing
$ e init master-release --root=~/src/electron
INFO Root '~/src/electron' already exists.
INFO (OK if you are sharing $root between multiple build configs)
New build config 'master-release' created
Now using config 'master-release'
$ e show configs
* master-release
master-testing
$ e show current
master-release
$ e show root
~/src/electron
$ e use master-testicdng
Now using config 'master-testing'
$ e show current
master-testing
$ e show root
~/src/electron
报错下载链接找不到pywin32
用这个链接下载解决:https://github.com/mhammond/pywin32/releases/tag/b228。64位用pywin32-228.win-amd64-py3.8.exe。
- 2、e sync
e sync -vvvv --revision 11-x-y
等同gclient sync。同步源码,依赖。
目前不支持版本切换,下载其他分支只能这么做:
进入src/electron目录下:
$ git checkout x-y-z
$ e sync -vvvv
$ e build
直接用nodejs的 http-server,非常容易实现http的断点传输。用360浏览器等,直接下载,很快。
npm install -g i http-server
http-server electron-dir
下载想要的分支,用
git branch -vv
可以看到本地分支关联的是哪个远程分支。
修改 .gclient 里面的url:
https//github/electron/electron@7-3-x
删除掉已经下载的src目录。重新同步代码:
e sync -vvvv
可以看到是指定的分支下载了。
因为用的unmanaged,所以切换分支是不行的,以前unmanaged就丢掉了。
是不是需要先upstream到远程分支。再操作。
gclient参数同样可以加入
$ e sync
Running "gclient sync --with_branch_heads --with_tags" in '~/src/electron/src'
[sync output omitted]
-v 或者 -vvvv输出更多信息。
- e build [target] -v
- e build --gen 强制做gn操作,重新生成ninja项目
Target | Description |
breakpad | Builds the breakpad |
chromedriver | Builds the |
electron | Builds the Electron binary (Default) |
electron:dist | Builds the Electron binary and generates a dist zip file 打包。可以在e-build.js中加入自动删除zip文件和移除调试信息,缩减dist包到100m: function runStrip(config) {
|
mksnapshot | Builds the |
node:headers | Builds the node headers |
如果源码是从别人那里拷贝过来,需要设置:
rem set PATH=D:\dev\electron7\src\out\Testing;%PATH%
set DEPOT_TOOLS_DIR=D:\dev\electron-master\electron\depot_tools
set DEPOT_TOOLS_WIN_TOOLCHAIN=0
set GIT_CACHE_PATH=D:\\dev\\.git_cache
set depot-tools=D:\dev\electron-master\electron\depot_tools
set PATH=%depot-tools%\bootstrap-3_8_0_chromium_8_bin\python\bin\scripts;%PATH%
electron7编译用到pip,安装了pywin32
如果已经安装了sdk,按下图修改:
帮助:
e --helpor e help <cmd>
e命令类似git,mvn:
nvm | e | Description |
nvm ls | e show configs | 显示配置Show the available configurations |
nvm current | e show current | 显示版本Show which configuration is currently in use |
nvm use <name> | e use <name> | 切换分支Change which configuration is currently in use |
运行于调试:
Using Electron
After you've built Electron, it's time to use it!
Command | Description |
e start | Run the Electron build |
e node | Run the Electron build as Node |
e debug | Run the Electron build in a debugger |
e test | Run Electron's spec runner |
As usual, any extra args are passed along to the executable. For example, e node --version
will print out Electron's node version.
e debug
Runs your local Electron build inside of lldb or gdb.
$ uname
Linux
$ e debug
Reading symbols from /home/yourname/electron/gn/master/src/out/Testing/electron...
(gdb)
$ uname
Darwin
$ e debug
target create "/Users/yourname/electron-gn/src/out/Testing/Electron.app/Contents/MacOS/Electron"
(lldb)
e test
-g webRequest
Starts the local Electron build's test runner. Any extra args are passed along to the runner.
# run all tests
e test
# run main process tests
e test --runners=main
Possible extra arguments to pass:
-
--node
- Run Node.js' own tests with Electron in RUN_AS_NODE
mode. -
--runners=<main|remote|native>
- The set of tests to run, can be either main
, remote
, or native
.
单元测试一个用例,位于:D:\dev\electron\src\electron\spec-main\api-web-request-spec.ts
e test --runners=remote -g webRequest
e test -- -g webRequest
Getting Information
e show
shows information about the current build config.
Command | Description |
e show current | The name of the active build config |
e show configs | Lists all build configs |
e show env | Show environment variables injected by the active build config |
e show exe | The path of the built Electron executable |
e show root | The path of the root directory from |
e show src [name] | The path of the named (default: electron) source dir |
e show stats | Build statistics |
Example usage:
$ uname
Darwin
$ e show exe
/Users/username/electron-gn-root/src/out/Testing/Electron.app/Contents/MacOS/Electron
$ uname
Linux
$ e show exe
/home/username/electron-gn-root/src/out/Testing/electron
$ e show out
Testing
$ e show src
/home/username/electron-gn-root/src/electron
$ cd `e show src base` && pwd
/home/username/electron-gn-root/src/base
$ ripgrep --t h TakeHeapSnapshot `e show src`
e open <commit | issue | PR>
Opens the GitHub page for the specified commit, pull request, or issue.
For example, e open 0920d01
will find the commit with an abbreviated sha1 of 0920d01
, see that it's associated with pull request #23450, and open https://github.com/electron/electron/pull/23450 in your browser. Since you can pass in a pull request or issue number as well, e open 23450
would have the same effect.
e patches [patch-dir]
Exports patches to the desired patch folder in Electron source tree.
Valid patch directories can include:
-
node
-
v8
-
boringssl
-
chromium
-
perfetto
-
icu
Command | Source Directory | Patch Directory |
e patches node | | |
e patches chromium | | |
e patches boringssl | | |
e patches v8 | | |
e patches perfetto | | |
e patches icu | | |
应用patch:
git am ~/dev0/e9.2.1/patch/0001-pdf-viewer-feature-disabled.patch
有时候莫名其妙不成功,也没有冲突,检查:
git apply --check ~/dev0/e9.2.1/patch/0002-update-version-from-9.2.1-to-109.2.1.patch
git apply --stat ~/dev0/e9.2.1/patch/0002-update-version-from-9.2.1-to-109.2.1.patch
放弃:
git am --abort
改用:
git apply ~/dev0/e9.2.1/patch/0002-update-version-from-9.2.1-to-109.2.1.patch
git add xxx.file 或者 .
然后commit
Advanced Usage
Per-Session Active Configs
If you want your shell sessions to each have different active configs, try this in your ~/.profile
or ~/.zshrc
or ~/.bashrc
:
export EVM_CURRENT_FILE="$(mktemp --tmpdir evm-current.XXXXXXXX.txt)"
This will create per-shell temporary files in which he active config file can be changed with e use
.
C:\dev>e init --root=\dev\electron mymaster
WARN A fixable error has occurred
--> git config --global core.filemode must be set to false.
Do you want build-tools to try fix this for you? [y/n]: y
Running "git config --global core.filemode false"
WARN A fixable error has occurred
--> git config --global core.autocrlf must be set to false.
Do you want build-tools to try fix this for you? [y/n]: y
Running "git config --global core.autocrlf false"
WARN A fixable error has occurred
--> git config --global branch.autosetuprebase must be set to always.
Do you want build-tools to try fix this for you? [y/n]: y
Running "git config --global branch.autosetuprebase always"
WARN A fixable error has occurred
--> A required dependency "choco" could not be located, it probably has to be installed.
Do you want build-tools to try fix this for you? [y/n]: y
Running "powershell -Command Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"
WARN A fixable error has occurred
--> A required dependency "python" could not be located, it probably has to be installed.
Do you want build-tools to try fix this for you? [y/n]: y
Running "choco install python2 --yes"
WARN A fixable error has occurred
--> A required dependency "pywin32" could not be located, it probably has to be installed.
Do you want build-tools to try fix this for you? [y/n]: y
Running "choco install pywin32 --yes"
需要装nodejs对应版本的pywin32
C:\dev>e init --root=\dev\electron mymaster
Creating \dev\electron
Cloning "depot_tools" into C:\Users\Administrator\.electron_build_tools\third_party\depot_tools
Updating C:\Users\Administrator\.electron_build_tools\third_party\depot_tools
Running "cmd.exe /c C:\Users\Administrator\.electron_build_tools\third_party\depot_tools\update_depot_tools.bat"
Downloading CIPD client for windows-amd64 from https://chrome-infra-packages.appspot.com/client?platform=windows-amd64&version=git_revision:9f9afb5ef6ef9d4887e8aa2bb617dfdd798f8005...
Running "python C:\Users\Administrator\.electron_build_tools\third_party\depot_tools\gclient.py config --name src/electron --unmanaged https://github.com/electron/electron" in \dev\electron
Creating C:\Users\Administrator\.electron_build_tools\configs
New build config mymaster created in C:\Users\Administrator\.electron_build_tools\configs\evm.mymaster.json
Now using config mymaster
Electron 打包执行
electron_dist_zip.runtime_deps
./electron
resources.pak
chrome_100_percent.pak
chrome_200_percent.pak
locales/am.pak
locales/ar.pak
locales/bg.pak
locales/bn.pak
locales/ca.pak
locales/cs.pak
locales/da.pak
locales/de.pak
locales/el.pak
locales/en-GB.pak
locales/en-US.pak
locales/es.pak
locales/et.pak
locales/fa.pak
locales/fi.pak
locales/fil.pak
locales/fr.pak
locales/gu.pak
locales/he.pak
locales/hi.pak
locales/hr.pak
locales/hu.pak
locales/id.pak
locales/it.pak
locales/ja.pak
locales/kn.pak
locales/ko.pak
locales/lt.pak
locales/lv.pak
locales/ml.pak
locales/mr.pak
locales/ms.pak
locales/nb.pak
locales/nl.pak
locales/pl.pak
locales/pt-PT.pak
locales/ro.pak
locales/ru.pak
locales/sk.pak
locales/sl.pak
locales/sr.pak
locales/sv.pak
locales/sw.pak
locales/ta.pak
locales/te.pak
locales/th.pak
locales/tr.pak
locales/uk.pak
locales/vi.pak
locales/zh-CN.pak
locales/zh-TW.pak
locales/es-419.pak
locales/pt-BR.pak
resources/default_app.asar
v8_context_snapshot.bin
icudtl.dat
swiftshader/libGLESv2.so
swiftshader/libEGL.so
./libEGL.so
./libGLESv2.so
./libVkICD_mock_icd.so
angledata/VkICD_mock_icd.json
angledata/VkICD_mock_icd.json
./libffmpeg.so
swiftshader/libvk_swiftshader.so
natives_blob.bin
snapshot_blob.bin
natives_blob.bin
snapshot_blob.bin
LICENSE
LICENSES.chromium.html
version
./chrome_sandbox
View Code
添加sscache,加快以后编译速度:
function runGNGen1(config) {
depot.ensure();
const sscache = ' cc_wrapper = \\"D:/dev/electron7/src/electron/external_binaries/sccache\\" ';
const gnExec = os.platform() === 'win32' ? 'gn.bat' : 'gn';
const gn_args = config.gen.args.join(' ').replace(/\"/g, '\\"'); // gn parses this part -- inner quotes must be escaped
const exec = `${path.resolve(depot.path, gnExec)} gen "out/${
config.gen.out
}" --args="${gn_args} ${sscache}"`;
const opts = { cwd: path.resolve(config.root, 'src') };
depot.execSync(config, exec, opts);
}
将离线git 仓库连接上远程
取回源码文件和git cache文件(.git_cache)里面的。
以electron为例:
源码和.git_cache目录放好后,什么也别动,先做第一步:(否则总报错:error: object directory /root/.git_cache/chromium.googlesource.com-chromium-src/objects does not exist; check .git/objects/info/alternates)
1,修改src/electron/.git/objects/info/alternates文件,指向本地存放路径。
/home/admin/.git_cache/github.com-electron-electron/objects
2,如不想拷贝整个cache目录,至少需要上面那个目录下的pack底下的文件:
这个方案应该更好,以chromium为例:
直接将拷贝回的源码,用git看会是detached游离状态,因为是checkout的一个提交点。第一步先建立本地分支。第二步将分支提交到远程。这就是我们要的分支了。第三步,附带可以同步一下本地分支的tags和branch。否则远程库里面没有。
根本不用在远程还在建立一个分支取合并,太麻烦了。
如下示例:
在chromium的src目录下:
1,$ git remote remove origin
2,$ git remote add origin https://github.com/electron/electron 本地仓库中建立一个与远程仓库的别名,以便之后提交代码而不是每次都要输入远程仓库地址。git remote -v 查看本地与远程联系
git branch -vv 查看本地分支与远程的关联。可以看到当前分支处于游离状态:* (HEAD detached from 83.0.4103.122)。
3,git branch tem //tem是新建分支的名字
4,git checkout tem
git branch 查看新建分支情况。是刚才的游离分支commit id。
5,git push origin tem 将本地分支提交到远程,如果远程没有就创建一个同名的。
5.1 git push origin tem:master 或者指定提交到远程某个分支:将本地分支tem提交到远程origin上的master分支。
注意,虽然代码同步到了远程。但是tag并没有。本地分支的tag要比远程多。
不建议做merge,没用
$ git fetch 可以更新到远程所有分支
git checkout master 分支 这个是真正的目标分支
git merge tem 合并刚才tem分支到目标master分支。可能有冲突解决。
解决冲突:
使用tem分支
git checkout --theirs $codefile $codefile是具体的冲突文件,可以用 git status 查看到
git add $codefile
git merge --continue
重新关联track远程分支
1,$ git remote remove origin
2,$ git remote add origin https://github.com/electron/electron
3,$ git branch --set-upstream-to=origin/<branch> 或者将当前分支与远程已有分支关联. 这里没有写本地分支,默认现在分支。 origin/<branch>这个是远程分支
git branch --set-upstream-to=origin/my-remote-branch my-local-branch 将本地分支my-local-branch关联到了远程分支my-remote-branch
4,git fetch (git pull)
5,git push --set-upstream origin tistar-chrome-v9.2.1 将本地分支tistar-chrome-v9.2.1关联到远程同名分支并创建和push
5,git push --set-upstream origin mybranch:tistar-chrome-v9.2.1 将本地分支mybranch关联到远程分支tistar-chrome-v9.2.1 并 push
远程分支与本地分支同名,才能省略成 git push!!!!
git checkout 78.0.3904.130 检出的是tag,无法提交修改
git checkout -b branchElectron7 78.0.3904.130 为tag建立了本地分支
重新关联远程分支
1,克隆个本地副本,因为速度快: git clone coworker-machine:/path/to/repo
2,重新定向到真正的远程: git set-url origin https://chromium.googlesource.com/chromium/src.git
3,更新: git fetch
4,删除以前本地仓库没用的分支: git prune origin
多个远程仓库add后,直接切换同步与提交的代码:
git.exe fetch -v --progress "mygithub" 7-3-x
git.exe push --set-upstream --progress "mygithub" bit-7-3-x:7-3-x 输入用户名与密码
git强制覆盖本地分支,慎用!!!
git fetch --all
git reset --hard origin/master
git pull
备注:
git fetch 只是下载远程的库的内容,不做任何的合并
git reset 把HEAD指向刚刚下载的最新的版本
- git fetch origin newTestBranch:newTestBranch(newTestBranch 前面的分支为远程分支名,后面是本地分支名)
然后git branch -v 查看分支
确认能自己新建的newDevelop分支的存在 - git reflog show HEAD@{now} -10
这个命令会把HEAD指针所有的动作显示出来。从中可以清楚的看到,在指针中提交对应的commit id
找到需要恢复的commit ,记下前面的commit id
chromium打印二进制函数:
#include "net/base/hex_utils.h"
auto sss = base::StringPiece(info.content_disposition);
LOG(INFO) << net::HexDump(sss);
Git Cookbook
A collection of git recipes to do common git tasks.
See also Git Tips.
Contents
- Introduction
- Chromium-specific Git Extensions
- Excluding file(s) from git-cl, while preserving them for later use
- Method #1: Reset your current branch, and selectively commit files
- Method #2: Create a new branch, reset, then commit files to preserve
- Method #3: Cherry-pick changes into review branches
- Sharing code between multiple machines
- Reverting commits
- Retrieving, or diffing against an old file revision
- Reusing a Git mirror
Introduction
This is designed to be a cookbook for common command sequences/tasks relating to git, git-cl, and how they work with Chromium development. It might be a little light on explanations.
If you are new to git, or do not have much experience with a distributed version control system, you should also check out The Git Community Book for an overview of basic git concepts and general git usage. Knowing what git means by branches, commits, reverts, and resets (as opposed to what SVN means by them) will help make the following much more understandable.
Chromium-specific Git Extensions
Chromium ships a large number of git extensions in depot_tools. Some (like git cl
) are required for the Chromium development workflow, while others (like git map-branches
) are simple utilities to make your life easier. Please take a look at the full depot_tools tutorial, and at the extensive man pages for all the extensions.
Excluding file(s) from git-cl, while preserving them for later use
Since git-cl assumes that the diff between your current branch and its tracking branch is what should be used for the CL, the goal is to remove the unwanted files from the current branch, and preserve them in another branch.
Method #1: Reset your current branch, and selectively commit files
- git log
See the list of your commits. Find the hash of the last commit before your changes. - git reset --soft abcdef
where abcdef is the hash found in the step above. - git commit <files_for_this_cl> -m "files to upload"
commit the files you want included in the CL here. - git new-branch new_branch_name
Create a new branch for the files that you want to exclude. - git commit -a -m "preserved files"
Commit the rest of the files.
Method #2: Create a new branch, reset, then commit files to preserve
This method creates a new branch from your current one to preserve your changes. The commits on the new branch are undone, and then only the files you want to preserve are recommitted.
- git checkout -b new_branch_name
This preserves your old files. - git log
See the list of your commits. Find the hash of the last commit before your changes. - git reset --soft abcdef
Where abcdef is the hash found in the step above. - git commit <files_to_preserve> -m "preserved files"
Commit the found files into the new_branch_name
.
Then revert your files however you'd like in your old branch. The files listed in step 4 will be saved in new_branch_name
Method #3: Cherry-pick changes into review branches
If you are systematic in creating separate local commits for independent changes, you can make a number of different changes in the same client and then cherry-pick each one into a separate review branch.
- Make and commit a set of independent changes.
- git log
# see the hashes for each of your commits. - repeat checkout, cherry-pick, upload steps for each change1..n
- git new-branch review-changeN
Create a new review branch tracking origin - git cherry-pick <hash of change N>
- git cl upload
If a change needs updating due to review comments, you can go back to your main working branch, update the commit, and re-cherry-pick it into the review branch.
- git checkout <working branch>
- Make changes.
- If the commit you want to update is the most recent one:
- git commit --amend <files>
- If not:
- git commit <files>
- git rebase -i origin
# use interactive rebase to squash the new commit into the old one.
- git log
# observe new hash for the change - git checkout review-changeN
- git reset --hard
# remove the previous version of the change - cherry-pick <new hash of change N>
- git cl upload
Sharing code between multiple machines
Assume Windows computer named vista, and a Linux one named penguin. Prerequisite: both machines have git clones of the main git tree.
vista$ git remote add linux ssh://penguin/path/to/git/repo
vista$ git fetch linux
vista$ git branch -a # should show "linux/branchname"
vista$ git checkout -b foobar linux/foobar
vista$ hack hack hack; git commit -a
vista$ git push linux # push branch back to linux
penguin$ git reset --hard # update with new stuff in branch
Note that, by default, gclient sync
will update all remotes. If your other machine (i.e., penguin
in the above example) is not always available, gclient sync
will timeout and fail trying to reach it. To fix this, you may exclude your machine from being fetched by default:
vista$ git config --bool remote.linux.skipDefaultUpdate true
Reverting commits
The command git revert X
patches in the inverse of a particular commit. Using this command is one way of making a revert:
git checkout origin # start with trunk
git revert abcdef
git cl upload
Retrieving, or diffing against an old file revision
Git works in terms of commits, not files. Thus, working with the history of a single file requires modified version of the show and diff commands.
# Find the commit you want in the file's commit log.
git log path/to/file
# This prints out the file contents at commit 123abc.
git show 123abc:path/to/file
# Diff the current version against path/to/file against the version at
# path/to/file
git diff 123abc -- path/to/file
When invoking git show
or git diff
, the path/to/file
is not relative the the current directory. It must be the full path from the directory where the .git directory lives. This is different from invoking git log
which understands relative paths.
Reusing a Git mirror
If you have a nearby copy of a Git repo, you can quickly bootstrap your copy from that one then adjust it to point it at the real upstream one.
- Clone a nearby copy of the code you want: git clone coworker-machine:/path/to/repo
- Change the URL your copy fetches from to point at the real git repo: git set-url origin https://chromium.googlesource.com/chromium/src.git
- Update your copy: git fetch
- Delete any extra branches that you picked up in the initial clone: git prune origin
如果已经安装了sdk,按下图修改: