Fix retrieval of name in `pub global activate -sgit` (#3407)

diff --git a/lib/src/global_packages.dart b/lib/src/global_packages.dart
index cc0bfdf..82b1263 100644
--- a/lib/src/global_packages.dart
+++ b/lib/src/global_packages.dart
@@ -92,7 +92,7 @@
     String? path,
     String? ref,
   }) async {
-    var name = await cache.git.getPackageNameFromRepo(repo, cache);
+    var name = await cache.git.getPackageNameFromRepo(repo, ref, path, cache);
 
     // TODO(nweiz): Add some special handling for git repos that contain path
     // dependencies. Their executables shouldn't be cached, and there should
diff --git a/lib/src/source/git.dart b/lib/src/source/git.dart
index 8680608..3cfd32d 100644
--- a/lib/src/source/git.dart
+++ b/lib/src/source/git.dart
@@ -196,11 +196,13 @@
 
   /// Given a Git repo that contains a pub package, gets the name of the pub
   /// package.
-  Future<String> getPackageNameFromRepo(String repo, SystemCache cache) {
+  Future<String> getPackageNameFromRepo(
+      String repo, String? ref, String? path, SystemCache cache) {
     // Clone the repo to a temp directory.
     return withTempDir((tempDir) async {
       await _clone(repo, tempDir, shallow: true);
-      var pubspec = Pubspec.load(tempDir, cache.sources);
+      if (ref != null) await _checkOut(tempDir, ref);
+      var pubspec = Pubspec.load(p.join(tempDir, path), cache.sources);
       return pubspec.name;
     });
   }
diff --git a/test/global/activate/git_package_test.dart b/test/global/activate/git_package_test.dart
index 02d06db..ad3ff27 100644
--- a/test/global/activate/git_package_test.dart
+++ b/test/global/activate/git_package_test.dart
@@ -38,7 +38,7 @@
         'sub',
         [
           d.libPubspec('foo', '1.0.0'),
-          d.dir('bin', [d.file('foo.dart', "main() => print('1');")])
+          d.dir('bin', [d.file('sub.dart', "main() => print('1');")])
         ],
       ),
     ]).create();
@@ -46,8 +46,8 @@
       d.dir(
         'sub',
         [
-          d.libPubspec('foo', '2.0.0'),
-          d.dir('bin', [d.file('foo.dart', "main() => print('2');")])
+          d.libPubspec('sub', '2.0.0'),
+          d.dir('bin', [d.file('sub.dart', "main() => print('2');")])
         ],
       ),
     ]).commit();
@@ -55,8 +55,8 @@
       d.dir(
         'sub',
         [
-          d.libPubspec('foo', '3.0.0'),
-          d.dir('bin', [d.file('foo.dart', "main() => print('3');")])
+          d.libPubspec('sub', '3.0.0'),
+          d.dir('bin', [d.file('sub.dart', "main() => print('3');")])
         ],
       ),
     ]).commit();
@@ -72,19 +72,19 @@
       ],
       output: allOf(
         startsWith('Resolving dependencies...\n'
-            '+ foo 2.0.0 from git ..${p.separator}foo.git at'),
+            '+ sub 2.0.0 from git ..${p.separator}foo.git at'),
         // Specific revision number goes here.
         contains('in sub'),
         endsWith('Building package executables...\n'
-            'Built foo:foo.\n'
-            'Activated foo 2.0.0 from Git repository "..${p.separator}foo.git".'),
+            'Built sub:sub.\n'
+            'Activated sub 2.0.0 from Git repository "..${p.separator}foo.git".'),
       ),
     );
     await runPub(
       args: [
         'global',
         'run',
-        'foo',
+        'sub',
       ],
       output: contains('2'),
     );