checkout: Save git config if checkout fails

Bug: b/314378562
Change-Id: Ic8f5c101c3a173dd15a0fa15861574af1b31f610
Reviewed-on: https://pigweed-review.googlesource.com/c/infra/recipes/+/187470
Pigweed-Auto-Submit: Rob Mohr <mohrr@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed-service-accounts.iam.gserviceaccount.com>
Presubmit-Verified: CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ted Pudlik <tpudlik@google.com>
diff --git a/recipe_modules/checkout/api.py b/recipe_modules/checkout/api.py
index 1420fe4..fc4fee9 100644
--- a/recipe_modules/checkout/api.py
+++ b/recipe_modules/checkout/api.py
@@ -1398,7 +1398,21 @@
                 self._repo(ctx)
 
             else:
-                self._git(ctx)
+                try:
+                    self._git(ctx)
+
+                # If the checkout failed save the git config. It might not be
+                # helpful, but it shouldn't hurt.
+                except self.m.step.StepFailure as exc:
+                    with self.m.context(cwd=ctx.root):
+                        with self.m.step.nest('git config'):
+                            self.m.git.config(
+                                '--list', '--local', step_name='local'
+                            )
+                            self.m.git.config(
+                                '--list', '--global', step_name='global'
+                            )
+                    raise
 
             if ctx.status:
                 for change in ctx.status.applied:
diff --git a/recipe_modules/checkout/tests/submodule.expected/submodule-repeated-nomatch.json b/recipe_modules/checkout/tests/submodule.expected/submodule-repeated-nomatch.json
index 47c6f0d..2c0d2c0 100644
--- a/recipe_modules/checkout/tests/submodule.expected/submodule-repeated-nomatch.json
+++ b/recipe_modules/checkout/tests/submodule.expected/submodule-repeated-nomatch.json
@@ -989,6 +989,65 @@
     ]
   },
   {
+    "cmd": [],
+    "name": "checkout pigweed.git config",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "git",
+      "config",
+      "--list",
+      "--local"
+    ],
+    "cwd": "[START_DIR]/co",
+    "luci_context": {
+      "realm": {
+        "name": "project:try"
+      },
+      "resultdb": {
+        "current_invocation": {
+          "name": "invocations/build:8945511751514863184",
+          "update_token": "token"
+        },
+        "hostname": "rdbhost"
+      }
+    },
+    "name": "checkout pigweed.git config.local",
+    "timeout": 300.0,
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "git",
+      "config",
+      "--list",
+      "--global"
+    ],
+    "cwd": "[START_DIR]/co",
+    "luci_context": {
+      "realm": {
+        "name": "project:try"
+      },
+      "resultdb": {
+        "current_invocation": {
+          "name": "invocations/build:8945511751514863184",
+          "update_token": "token"
+        },
+        "hostname": "rdbhost"
+      }
+    },
+    "name": "checkout pigweed.git config.global",
+    "timeout": 300.0,
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@"
+    ]
+  },
+  {
     "failure": {
       "failure": {},
       "humanReason": "change x:123456 (branch main) matches multiple submodules (foo1 (branch release), foo2 (branch dev)) but no branches match"
diff --git a/recipe_modules/checkout/tests/submodule.expected/submodule-repeated-twomatch.json b/recipe_modules/checkout/tests/submodule.expected/submodule-repeated-twomatch.json
index a9d1717..f6ccae7 100644
--- a/recipe_modules/checkout/tests/submodule.expected/submodule-repeated-twomatch.json
+++ b/recipe_modules/checkout/tests/submodule.expected/submodule-repeated-twomatch.json
@@ -989,6 +989,65 @@
     ]
   },
   {
+    "cmd": [],
+    "name": "checkout pigweed.git config",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "git",
+      "config",
+      "--list",
+      "--local"
+    ],
+    "cwd": "[START_DIR]/co",
+    "luci_context": {
+      "realm": {
+        "name": "project:try"
+      },
+      "resultdb": {
+        "current_invocation": {
+          "name": "invocations/build:8945511751514863184",
+          "update_token": "token"
+        },
+        "hostname": "rdbhost"
+      }
+    },
+    "name": "checkout pigweed.git config.local",
+    "timeout": 300.0,
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "git",
+      "config",
+      "--list",
+      "--global"
+    ],
+    "cwd": "[START_DIR]/co",
+    "luci_context": {
+      "realm": {
+        "name": "project:try"
+      },
+      "resultdb": {
+        "current_invocation": {
+          "name": "invocations/build:8945511751514863184",
+          "update_token": "token"
+        },
+        "hostname": "rdbhost"
+      }
+    },
+    "name": "checkout pigweed.git config.global",
+    "timeout": 300.0,
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@"
+    ]
+  },
+  {
     "failure": {
       "failure": {},
       "humanReason": "change x:123456 (branch main) matches multiple submodules (foo1 (branch main), foo2 (branch main)), but too many branches match"
diff --git a/recipe_modules/checkout/tests/submodule.expected/submodule-try-excluded-missing.json b/recipe_modules/checkout/tests/submodule.expected/submodule-try-excluded-missing.json
index 94c13b5..b534c23 100644
--- a/recipe_modules/checkout/tests/submodule.expected/submodule-try-excluded-missing.json
+++ b/recipe_modules/checkout/tests/submodule.expected/submodule-try-excluded-missing.json
@@ -558,6 +558,65 @@
     ]
   },
   {
+    "cmd": [],
+    "name": "checkout pigweed.git config",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "git",
+      "config",
+      "--list",
+      "--local"
+    ],
+    "cwd": "[START_DIR]/co",
+    "luci_context": {
+      "realm": {
+        "name": "project:ci"
+      },
+      "resultdb": {
+        "current_invocation": {
+          "name": "invocations/build:8945511751514863184",
+          "update_token": "token"
+        },
+        "hostname": "rdbhost"
+      }
+    },
+    "name": "checkout pigweed.git config.local",
+    "timeout": 300.0,
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "git",
+      "config",
+      "--list",
+      "--global"
+    ],
+    "cwd": "[START_DIR]/co",
+    "luci_context": {
+      "realm": {
+        "name": "project:ci"
+      },
+      "resultdb": {
+        "current_invocation": {
+          "name": "invocations/build:8945511751514863184",
+          "update_token": "token"
+        },
+        "hostname": "rdbhost"
+      }
+    },
+    "name": "checkout pigweed.git config.global",
+    "timeout": 300.0,
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@"
+    ]
+  },
+  {
     "failure": {
       "humanReason": "excluded submodule missing is not a submodule"
     },
diff --git a/recipe_modules/checkout/tests/submodule.expected/submodule-try-included-excluded.json b/recipe_modules/checkout/tests/submodule.expected/submodule-try-included-excluded.json
index f441f5f..4a1df8d 100644
--- a/recipe_modules/checkout/tests/submodule.expected/submodule-try-included-excluded.json
+++ b/recipe_modules/checkout/tests/submodule.expected/submodule-try-included-excluded.json
@@ -532,6 +532,65 @@
     ]
   },
   {
+    "cmd": [],
+    "name": "checkout pigweed.git config",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "git",
+      "config",
+      "--list",
+      "--local"
+    ],
+    "cwd": "[START_DIR]/co",
+    "luci_context": {
+      "realm": {
+        "name": "project:ci"
+      },
+      "resultdb": {
+        "current_invocation": {
+          "name": "invocations/build:8945511751514863184",
+          "update_token": "token"
+        },
+        "hostname": "rdbhost"
+      }
+    },
+    "name": "checkout pigweed.git config.local",
+    "timeout": 300.0,
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "git",
+      "config",
+      "--list",
+      "--global"
+    ],
+    "cwd": "[START_DIR]/co",
+    "luci_context": {
+      "realm": {
+        "name": "project:ci"
+      },
+      "resultdb": {
+        "current_invocation": {
+          "name": "invocations/build:8945511751514863184",
+          "update_token": "token"
+        },
+        "hostname": "rdbhost"
+      }
+    },
+    "name": "checkout pigweed.git config.global",
+    "timeout": 300.0,
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@"
+    ]
+  },
+  {
     "failure": {
       "humanReason": "cannot specify both included_submodules and excluded_submodules"
     },
diff --git a/recipe_modules/checkout/tests/submodule.expected/submodule-try-not-found.json b/recipe_modules/checkout/tests/submodule.expected/submodule-try-not-found.json
index 95712d9..af48e67 100644
--- a/recipe_modules/checkout/tests/submodule.expected/submodule-try-not-found.json
+++ b/recipe_modules/checkout/tests/submodule.expected/submodule-try-not-found.json
@@ -1040,6 +1040,65 @@
     ]
   },
   {
+    "cmd": [],
+    "name": "checkout pigweed.git config",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "git",
+      "config",
+      "--list",
+      "--local"
+    ],
+    "cwd": "[START_DIR]/co",
+    "luci_context": {
+      "realm": {
+        "name": "project:try"
+      },
+      "resultdb": {
+        "current_invocation": {
+          "name": "invocations/build:8945511751514863184",
+          "update_token": "token"
+        },
+        "hostname": "rdbhost"
+      }
+    },
+    "name": "checkout pigweed.git config.local",
+    "timeout": 300.0,
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "git",
+      "config",
+      "--list",
+      "--global"
+    ],
+    "cwd": "[START_DIR]/co",
+    "luci_context": {
+      "realm": {
+        "name": "project:try"
+      },
+      "resultdb": {
+        "current_invocation": {
+          "name": "invocations/build:8945511751514863184",
+          "update_token": "token"
+        },
+        "hostname": "rdbhost"
+      }
+    },
+    "name": "checkout pigweed.git config.global",
+    "timeout": 300.0,
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@"
+    ]
+  },
+  {
     "failure": {
       "humanReason": "could not find triggering changes in checkout"
     },