|
6 | 6 | import logging |
7 | 7 |
|
8 | 8 | # typing ------------------------------------------------------------------- |
9 | | -from typing import TYPE_CHECKING, Union |
| 9 | +from typing import TYPE_CHECKING, Dict, Union |
10 | 10 |
|
11 | 11 | import git |
12 | 12 | from git.exc import InvalidGitRepositoryError |
@@ -167,6 +167,7 @@ def update( # type: ignore[override] |
167 | 167 |
|
168 | 168 | repo = self.repo |
169 | 169 | sms: "IterableList[Submodule]" = IterableList("name") |
| 170 | + fallback_branches: Dict[str, str] = {} |
170 | 171 |
|
171 | 172 | try: |
172 | 173 | # SETUP BASE COMMIT |
@@ -334,35 +335,25 @@ def update( # type: ignore[override] |
334 | 335 | # Rename the new remote back to what it was. |
335 | 336 | smr.rename(orig_name) |
336 | 337 |
|
337 | | - # Early on, we verified that the our current tracking |
338 | | - # branch exists in the remote. Now we have to ensure |
339 | | - # that the sha we point to is still contained in the new |
340 | | - # remote tracking branch. |
341 | | - if branch.name != ".": |
342 | | - smsha = sm.binsha |
343 | | - found = False |
344 | | - rref = smr.refs[branch.name] |
345 | | - for c in rref.commit.traverse(): |
346 | | - if c.binsha == smsha: |
347 | | - found = True |
348 | | - break |
349 | | - # END traverse all commits in search for sha |
350 | | - # END for each commit |
351 | | - |
352 | | - if not found: |
353 | | - # Adjust our internal binsha to use the one of the |
354 | | - # remote this way, it will be checked out in the |
355 | | - # next step. This will change the submodule relative |
356 | | - # to us, so the user will be able to commit the |
357 | | - # change easily. |
358 | | - _logger.warning( |
359 | | - "Current sha %s was not contained in the tracking\ |
360 | | - branch at the new remote, setting it the the remote's tracking branch", |
361 | | - sm.hexsha, |
362 | | - ) |
363 | | - sm.binsha = rref.commit.binsha |
364 | | - # END reset binsha |
365 | | - # END verify resolved tracking branch |
| 338 | + # The gitlink can be reachable from any branch on the new remote. |
| 339 | + if branch.name != "." and ( |
| 340 | + not smm.is_valid_object(sm.hexsha, "commit") |
| 341 | + or not smm.git._call_process_safe( |
| 342 | + "for_each_ref", |
| 343 | + "--format=%(refname)", |
| 344 | + f"--contains={sm.hexsha}", |
| 345 | + "--count=1", |
| 346 | + "--", |
| 347 | + f"refs/remotes/{orig_name}/", |
| 348 | + ) |
| 349 | + ): |
| 350 | + _logger.warning( |
| 351 | + "Gitlink %s is unavailable on the new remote, using the tip of %s", |
| 352 | + sm.hexsha, |
| 353 | + branch.name, |
| 354 | + ) |
| 355 | + sm.binsha = smr.refs[branch.name].commit.binsha |
| 356 | + fallback_branches[sm.name] = branch.name |
366 | 357 |
|
367 | 358 | # NOTE: All checkout is performed by the base |
368 | 359 | # implementation of update. |
@@ -453,6 +444,7 @@ def update( # type: ignore[override] |
453 | 444 | force=force_reset, |
454 | 445 | keep_going=keep_going, |
455 | 446 | no_fetch=no_fetch, |
| 447 | + _fallback_branch=fallback_branches.get(sm.name), |
456 | 448 | ) |
457 | 449 |
|
458 | 450 | # Update recursively depth first - question is which inconsistent state will |
|
0 commit comments