Skip to content

Commit d4fb76a

Browse files
authored
[camera_android_camerax] Correct pre-push skill version validation logic (flutter#12371)
Corrects the version and merge conflict validation logic in the `pre-push` skill to compare the local branch against the upstream branch. If the merge conflict check fails, then the skill immediately reports that the code is not ready to push now. Also, adds an eval to ensure this update works as expected. Result of running it: | Eval ID | Scenario | Expected Outcome | Result | Justification | | :---: | :--- | :--- | :---: | :--- | | **1** | Behind Upstream (`setup_behind_upstream.sh`) | Detect branch is behind `upstream/main`, stop immediately without updating release info | **PASS** | Checked merge-base against `upstream/main`, halted immediately with `# NO, you are not ready to push`, and left `pubspec.yaml` and `CHANGELOG.md` unmodified. | | **2** | Behind Upstream with Conflicts (`setup_behind_upstream_conflict.dart`) | Detect that the branch is behind the upstream repository | **PASS** | Detected that the branch is behind the upstream repository, Ran git merge-tree and detected merge conflicts (CONFLICT (content): Merge conflict in .ci/flutter_master.version), Stopped immediately without running subsequent checks, Output # NO, you are not ready to push. and provided actionable instructions on how to resolve conflicts before pushing Ideally flutter/packages#12369 lands first so I can update the references of origin to upstream. ---- Follow up from one-shot attempt flutter/packages#12302. See go/flutter-project-one-shot for more information on the project. ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 0ba9a82 commit d4fb76a

4 files changed

Lines changed: 315 additions & 10 deletions

File tree

‎packages/camera/camera_android_camerax/.agents/skills/pre-push-skill/SKILL.md‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ You must verify that there are actually changed files to test.
2929
Command to run:
3030

3131
```bash
32-
git fetch origin main
33-
git diff --name-only origin/main...HEAD | grep '^packages/camera/camera_android_camerax'
32+
UPSTREAM_PREPUSH=$(git remote -v | grep 'flutter/packages' | head -n 1 | awk '{print $1}')
33+
UPSTREAM_PREPUSH=${UPSTREAM_PREPUSH:-upstream}
34+
git fetch $UPSTREAM_PREPUSH main
35+
git diff --name-only $UPSTREAM_PREPUSH/main...HEAD | grep '^packages/camera/camera_android_camerax'
3436
```
3537

3638
If this command outputs nothing,
@@ -51,15 +53,23 @@ is an ancestor of your current `HEAD`.
5153
Command to run:
5254

5355
```bash
54-
git fetch origin main
55-
git merge-base --is-ancestor origin/main HEAD
56+
UPSTREAM_PREPUSH=$(git remote -v | grep 'flutter/packages' | head -n 1 | awk '{print $1}')
57+
UPSTREAM_PREPUSH=${UPSTREAM_PREPUSH:-upstream}
58+
git fetch $UPSTREAM_PREPUSH main
59+
git merge-base --is-ancestor $UPSTREAM_PREPUSH/main HEAD
5660
```
5761

58-
If this command fails (exits with a non-zero code),
59-
the branch is behind `origin/main`.
60-
The code is NOT ready to push.
61-
The latest changes from `main` must be pulled first,
62-
and then merge conflicts must be resolved.
62+
If this command fails (exits with a non-zero code), the branch is behind upstream.
63+
In this case, check if there are actual merge conflicts by simulating a merge:
64+
65+
```bash
66+
git merge-tree HEAD $UPSTREAM_PREPUSH/main
67+
```
68+
69+
If the `merge-tree` command fails (exits with a non-zero code), there are merge conflicts.
70+
**STOP IMMEDIATELY.** Do not proceed to any subsequent checks and do not run `update-release-info`. Output `# NO, you are not ready to push`, inform the user that there are merge conflicts, and provide the command to reproduce the merge (`git merge $UPSTREAM_PREPUSH/main`).
71+
72+
If the branch is behind but there are no merge conflicts, you may proceed with the remaining checks. However, you must warn the user that they are behind and should pull/merge `$UPSTREAM_PREPUSH/main` before pushing, and you should still avoid running `update-release-info` automatically to prevent creating potential conflicts.
6373

6474
## 4. Check Unit Tests Pass
6575

‎packages/camera/camera_android_camerax/.agents/skills/pre-push-skill/evals/evals.json‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,44 @@
5858
},
5959
{
6060
"id": 5,
61+
"prompt": "First, run 'dart .agents/skills/pre-push-skill/evals/test_data/setup_behind_upstream_no_conflict.dart'. Then, run the pre-push skill.",
62+
"expected_chat_output": [
63+
"The agent warned the user that the branch should be pulled or merged before pushing.",
64+
"The agent reported that the code is ready to push (# YES, you are ready to push!)."
65+
],
66+
"expected_repo_state": [
67+
"The agent detected that the branch is behind the upstream main branch.",
68+
"The agent verified that there are no merge conflicts with the upstream main branch.",
69+
"The agent ran the unit tests check.",
70+
"The agent ran the publish check (Version and CHANGELOG updates).",
71+
"The agent ran the license headers check.",
72+
"The agent ran the required documentation check.",
73+
"The agent ran the added tests check.",
74+
"The agent did not run update-release-info.",
75+
"pubspec.yaml was not modified.",
76+
"CHANGELOG.md was not modified."
77+
],
78+
"agent_config": "reidbaker-agent"
79+
},
80+
{
81+
"id": 6,
82+
"prompt": "First, run 'dart .agents/skills/pre-push-skill/evals/test_data/setup_behind_upstream_conflict.dart'. Then, run the pre-push skill.",
83+
"expected_chat_output": [
84+
"The agent reported that the code is not ready to push (# NO, you are not ready to push).",
85+
"The agent told the user that there were merge conflicts and the command to reproduce the merge."
86+
],
87+
"expected_repo_state": [
88+
"The agent detected that the branch is behind the upstream main branch.",
89+
"The agent detected merge conflicts with the upstream main branch.",
90+
"The agent stopped immediately at the merge conflict check without running subsequent checks.",
91+
"The agent did not run update-release-info.",
92+
"pubspec.yaml was not modified.",
93+
"CHANGELOG.md was not modified."
94+
],
95+
"agent_config": "reidbaker-agent"
96+
},
97+
{
98+
"id": 7,
6199
"prompt": "First, run 'dart run .agents/skills/pre-push-skill/evals/test_data/setup_user_facing_docs_change.dart'. Then, run the /pre-push skill. Do not attempt to fix any issues you find, only report the output of the skill.",
62100
"expected_chat_output": [
63101
"Agent explicitly states that the code is not ready to push.",
@@ -70,4 +108,4 @@
70108
"agent_config": "reidbaker-agent"
71109
}
72110
]
73-
}
111+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// ignore_for_file: avoid_print
6+
7+
import 'dart:io';
8+
9+
/// Detects the remote name pointing to the main flutter/packages repository.
10+
Future<String> getUpstreamRemote(String workingDirectory) async {
11+
final ProcessResult result = await Process.run('git', <String>[
12+
'remote',
13+
'-v',
14+
], workingDirectory: workingDirectory);
15+
if (result.exitCode != 0) {
16+
return 'upstream'; // fallback
17+
}
18+
final stdout = result.stdout.toString();
19+
for (final String line in stdout.split('\n')) {
20+
if (line.contains('flutter/packages')) {
21+
final List<String> parts = line.split(RegExp(r'\s+'));
22+
if (parts.isNotEmpty) {
23+
return parts.first;
24+
}
25+
}
26+
}
27+
return 'upstream'; // fallback
28+
}
29+
30+
/// Finds the root directory of the git repository.
31+
Future<Directory> getRepoRoot(String workingDirectory) async {
32+
final ProcessResult result = await Process.run('git', <String>[
33+
'rev-parse',
34+
'--show-toplevel',
35+
], workingDirectory: workingDirectory);
36+
if (result.exitCode != 0) {
37+
return Directory(workingDirectory).parent.parent.parent;
38+
}
39+
return Directory(result.stdout.toString().trim());
40+
}
41+
42+
/// Checks out a branch that is behind upstream/main, makes a local commit that
43+
/// conflicts with the latest changes on upstream/main, and verifies that
44+
/// pre-push-skill detects the conflict and stops immediately.
45+
void main() async {
46+
// The root of the packages directory.
47+
final Directory packageDir = Directory(
48+
Platform.script.toFilePath(),
49+
).parent.parent.parent.parent.parent.parent;
50+
51+
final Directory repoRoot = await getRepoRoot(packageDir.path);
52+
final String upstream = await getUpstreamRemote(packageDir.path);
53+
print('Detected upstream remote: $upstream');
54+
55+
// 1. Fetch upstream main with depth of at least 10
56+
print('Fetching $upstream main...');
57+
final ProcessResult fetchResult = await Process.run('git', <String>[
58+
'fetch',
59+
'--depth=10',
60+
upstream,
61+
'main',
62+
], workingDirectory: packageDir.path);
63+
if (fetchResult.exitCode != 0) {
64+
print('Failed to fetch upstream main: ${fetchResult.stderr}');
65+
exit(1);
66+
}
67+
68+
// 2. Find a modified file in recent commits of upstream/main that exists in the repo
69+
print('Finding modified file in recent upstream commits...');
70+
String? fileToConflict;
71+
for (var i = 1; i <= 5; i++) {
72+
final ProcessResult diffResult = await Process.run('git', <String>[
73+
'diff',
74+
'--name-only',
75+
'--diff-filter=M',
76+
'$upstream/main~$i..$upstream/main',
77+
], workingDirectory: repoRoot.path);
78+
if (diffResult.exitCode == 0) {
79+
final List<String> lines = diffResult.stdout
80+
.toString()
81+
.split('\n')
82+
.map((String line) => line.trim())
83+
.where((String line) => line.isNotEmpty)
84+
.toList();
85+
for (final line in lines) {
86+
final file = File('${repoRoot.path}/$line');
87+
if (file.existsSync()) {
88+
fileToConflict = line;
89+
break;
90+
}
91+
}
92+
if (fileToConflict != null) {
93+
break;
94+
}
95+
}
96+
}
97+
98+
if (fileToConflict == null) {
99+
print('No modified file found in recent upstream commits. Cannot guarantee conflict.');
100+
exit(1);
101+
}
102+
print('Selected file to conflict: $fileToConflict');
103+
104+
// 3. Checkout a new temporary branch 'eval_behind_upstream_conflict' starting 1 commit behind upstream/main
105+
print('Checking out eval_behind_upstream_conflict...');
106+
final ProcessResult checkoutResult = await Process.run('git', <String>[
107+
'checkout',
108+
'-B',
109+
'eval_behind_upstream_conflict',
110+
'$upstream/main~1',
111+
], workingDirectory: packageDir.path);
112+
if (checkoutResult.exitCode != 0) {
113+
print('Failed to checkout: ${checkoutResult.stderr}');
114+
exit(1);
115+
}
116+
117+
// 4. Modify the file to cause a conflict by overwriting it
118+
final conflictFile = File('${repoRoot.path}/$fileToConflict');
119+
if (!conflictFile.existsSync()) {
120+
print('File does not exist: ${conflictFile.path}');
121+
exit(1);
122+
}
123+
await conflictFile.writeAsString('// CONFLICTING CHANGE\n// CONFLICT LINE 2\n');
124+
125+
// Also modify a file in camera_android_camerax so the skill detects changed files in this package
126+
final packageFile = File('${packageDir.path}/lib/src/camerax_library.dart');
127+
await packageFile.writeAsString('\n// Eval comment\n', mode: FileMode.append);
128+
129+
// 5. Commit the changes
130+
print('Committing...');
131+
final ProcessResult addResult = await Process.run('git', <String>[
132+
'add',
133+
conflictFile.path,
134+
packageFile.path,
135+
], workingDirectory: packageDir.path);
136+
if (addResult.exitCode != 0) {
137+
print('Failed to git add: ${addResult.stderr}');
138+
exit(1);
139+
}
140+
141+
final ProcessResult commitResult = await Process.run('git', <String>[
142+
'-c',
143+
'user.name=Author',
144+
'-c',
145+
146+
'commit',
147+
'-m',
148+
'eval: conflicting local commit',
149+
], workingDirectory: packageDir.path);
150+
if (commitResult.exitCode != 0) {
151+
print('Failed to commit: ${commitResult.stderr}');
152+
exit(1);
153+
}
154+
155+
print('Setup complete.');
156+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// ignore_for_file: avoid_print
6+
7+
import 'dart:io';
8+
9+
/// Detects the remote name pointing to the main flutter/packages repository.
10+
Future<String> getUpstreamRemote(String workingDirectory) async {
11+
final ProcessResult result = await Process.run('git', <String>[
12+
'remote',
13+
'-v',
14+
], workingDirectory: workingDirectory);
15+
if (result.exitCode != 0) {
16+
return 'upstream'; // fallback
17+
}
18+
final stdout = result.stdout.toString();
19+
for (final String line in stdout.split('\n')) {
20+
if (line.contains('flutter/packages')) {
21+
final List<String> parts = line.split(RegExp(r'\s+'));
22+
if (parts.isNotEmpty) {
23+
return parts.first;
24+
}
25+
}
26+
}
27+
return 'upstream'; // fallback
28+
}
29+
30+
/// Checks out a branch that is behind upstream/main by 1 commit, makes a local commit,
31+
/// and sets up the branch to test that pre-push-skill detects the branch is behind upstream,
32+
/// verifies there are no merge conflicts, proceeds with checks without running update-release-info,
33+
/// and reports that the branch needs to be updated before pushing.
34+
void main() async {
35+
// The root of the packages directory.
36+
final Directory packageDir = Directory(
37+
Platform.script.toFilePath(),
38+
).parent.parent.parent.parent.parent.parent;
39+
final dartFilePath = '${packageDir.path}/lib/src/camerax_library.dart';
40+
41+
final String upstream = await getUpstreamRemote(packageDir.path);
42+
print('Detected upstream remote: $upstream');
43+
44+
// 1. Fetch upstream main with depth of at least 2 to ensure upstream/main~1 is available
45+
print('Fetching $upstream main...');
46+
final ProcessResult fetchResult = await Process.run('git', <String>[
47+
'fetch',
48+
'--depth=2',
49+
upstream,
50+
'main',
51+
], workingDirectory: packageDir.path);
52+
if (fetchResult.exitCode != 0) {
53+
print('Failed to fetch: ${fetchResult.stderr}');
54+
exit(1);
55+
}
56+
57+
// 2. Checkout a new temporary branch 'eval_behind_upstream_no_conflict' starting 1 commit behind upstream/main
58+
print('Checking out eval_behind_upstream_no_conflict...');
59+
final ProcessResult checkoutResult = await Process.run('git', <String>[
60+
'checkout',
61+
'-B',
62+
'eval_behind_upstream_no_conflict',
63+
'$upstream/main~1',
64+
], workingDirectory: packageDir.path);
65+
if (checkoutResult.exitCode != 0) {
66+
print('Failed to checkout: ${checkoutResult.stderr}');
67+
exit(1);
68+
}
69+
70+
// 3. Make a local change and commit it so our HEAD is behind upstream/main and diverged
71+
print('Making local change...');
72+
final dartFile = File(dartFilePath);
73+
await dartFile.writeAsString('\n// Eval comment\n', mode: FileMode.append);
74+
75+
// 4. Commit
76+
print('Committing...');
77+
final ProcessResult addResult = await Process.run('git', <String>[
78+
'add',
79+
dartFilePath,
80+
], workingDirectory: packageDir.path);
81+
if (addResult.exitCode != 0) {
82+
print('Failed to git add: ${addResult.stderr}');
83+
exit(1);
84+
}
85+
86+
final ProcessResult commitResult = await Process.run('git', <String>[
87+
'-c',
88+
'user.name=Author',
89+
'-c',
90+
91+
'commit',
92+
'-m',
93+
'eval: local commit while behind upstream/main',
94+
], workingDirectory: packageDir.path);
95+
if (commitResult.exitCode != 0) {
96+
print('Failed to commit: ${commitResult.stderr}');
97+
exit(1);
98+
}
99+
100+
print('Setup complete.');
101+
}

0 commit comments

Comments
 (0)