Skip to content

Commit dd7ad71

Browse files
authored
[CP-Stable] Unset GIT_DIR and other variables before updating (flutter#166772)
Cherry picks flutter#165818 `update_engine_version.{ps1|sh}` needs to operate on a foreign repository. When flutter is run in a git-hook, these environment variables will override our git calls location and corrupt the install. Cherry pick notes: The files are diverging a bit; but the guts of the change were clean (ps1|sh) and the tests just needed some care.
1 parent 6806f6b commit dd7ad71

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ INTERNAL NOTE
2727
## Flutter 3.29 Changes
2828

2929
### [3.29.3](https://github.com/flutter/flutter/releases/tag/3.29.3)
30+
- [flutter/165818](https://github.com/flutter/flutter/pull/165818) - Unset `GIT_DIR` to enable flutter tool calls in githooks.
3031
- [flutter/163421](https://github.com/flutter/flutter/issues/163421) - Impeller,
3132
Android, Fixes Android Emulator crash when navigating to routes with backdrop
3233
blurs.

bin/internal/update_engine_version.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
$ErrorActionPreference = "Stop"
1818

19+
# When called from a submodule hook; these will override `git -C dir`
20+
$env:GIT_DIR = $null
21+
$env:GIT_INDEX_FILE = $null
22+
$env:GIT_WORK_TREE = $null
23+
1924
$progName = Split-Path -parent $MyInvocation.MyCommand.Definition
2025
$flutterRoot = (Get-Item $progName).parent.parent.FullName
2126

bin/internal/update_engine_version.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
set -e
1919

20+
# When called from a submodule hook; these will override `git -C dir`
21+
unset GIT_DIR
22+
unset GIT_INDEX_FILE
23+
unset GIT_WORK_TREE
24+
2025
# Allow overriding the intended engine version via FLUTTER_PREBUILT_ENGINE_VERSION.
2126
#
2227
# This is for systems, such as Github Actions, where we know ahead of time the

dev/tools/test/update_engine_version_test.dart

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ void main() {
2929
}
3030
}
3131

32-
io.ProcessResult run(String executable, List<String> args) {
32+
io.ProcessResult run(String executable, List<String> args, {String? workingPath}) {
3333
print('Running "$executable ${args.join(" ")}"');
3434
final io.ProcessResult result = io.Process.runSync(
3535
executable,
3636
args,
3737
environment: environment,
38-
workingDirectory: testRoot.root.absolute.path,
38+
workingDirectory: workingPath ?? testRoot.root.absolute.path,
3939
includeParentEnvironment: false,
4040
);
4141
if (result.exitCode != 0) {
@@ -92,11 +92,68 @@ void main() {
9292
run('git', <String>['commit', '-m', 'tracking engine.version']);
9393
}
9494

95-
void setupRemote({required String remote}) {
96-
run('git', <String>['remote', 'add', remote, testRoot.root.path]);
97-
run('git', <String>['fetch', remote]);
95+
void setupRemote({required String remote, String? rootPath}) {
96+
run('git', <String>[
97+
'remote',
98+
'add',
99+
remote,
100+
rootPath ?? testRoot.root.path,
101+
], workingPath: rootPath);
102+
run('git', <String>['fetch', remote], workingPath: rootPath);
98103
}
99104

105+
void initGitRepoWithBlankInitialCommit({String? workingPath}) {
106+
run('git', <String>['init', '--initial-branch', 'master'], workingPath: workingPath);
107+
run('git', <String>[
108+
'config',
109+
'--local',
110+
'user.email',
111+
112+
], workingPath: workingPath);
113+
run('git', <String>['config', '--local', 'user.name', 'Test User'], workingPath: workingPath);
114+
run('git', <String>['add', '.'], workingPath: workingPath);
115+
run('git', <String>[
116+
'commit',
117+
'--allow-empty',
118+
'-m',
119+
'Initial commit',
120+
], workingPath: workingPath);
121+
}
122+
123+
group('GIT_DIR', () {
124+
late Directory externalGit;
125+
late String externalHead;
126+
setUp(() {
127+
externalGit = localFs.systemTempDirectory.createTempSync('GIT_DIR_test.');
128+
initGitRepoWithBlankInitialCommit(workingPath: externalGit.path);
129+
setupRemote(remote: 'upstream', rootPath: externalGit.path);
130+
131+
externalHead =
132+
(run('git', <String>['rev-parse', 'HEAD'], workingPath: externalGit.path).stdout
133+
as String)
134+
.trim();
135+
});
136+
137+
test('un-sets environment variables', () {
138+
// Needs to happen before GIT_DIR is set
139+
initGitRepoWithBlankInitialCommit();
140+
setupRemote(remote: 'upstream');
141+
142+
environment['GIT_DIR'] = '${externalGit.path}/.git';
143+
environment['GIT_INDEX_FILE'] = '${externalGit.path}/.git/index';
144+
environment['GIT_WORK_TREE'] = externalGit.path;
145+
146+
runUpdateEngineVersion();
147+
148+
final String engineStamp = testRoot.binInternalEngineVersion.readAsStringSync().trim();
149+
expect(engineStamp, isNot(equals(externalHead)));
150+
});
151+
152+
tearDown(() {
153+
externalGit.deleteSync(recursive: true);
154+
});
155+
});
156+
100157
group('if FLUTTER_PREBUILT_ENGINE_VERSION is set', () {
101158
setUp(() {
102159
environment['FLUTTER_PREBUILT_ENGINE_VERSION'] = '123abc';

0 commit comments

Comments
 (0)