Skip to content

Commit d634042

Browse files
Merge pull request swiftlang#33075 from nate-chandler/update-checkout/fallback-to-checkout-revision
[Update Checkout] Fall back to revision if branch checkout fails.
2 parents a2d648f + 2ef8f1b commit d634042

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

utils/update_checkout/tests/scheme_mock.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ def setup_mock_remote(base_dir):
119119
with open(get_config_path(base_dir), 'w') as f:
120120
json.dump(base_config, f)
121121

122+
return (LOCAL_PATH, REMOTE_PATH)
123+
122124

123125
BASEDIR_ENV_VAR = 'UPDATECHECKOUT_TEST_WORKSPACE_DIR'
124126
CURRENT_FILE_DIR = os.path.dirname(os.path.abspath(__file__))
@@ -147,7 +149,7 @@ def __init__(self, *args, **kwargs):
147149

148150
def setUp(self):
149151
create_dir(self.source_root)
150-
setup_mock_remote(self.workspace)
152+
(self.local_path, self.remote_path) = setup_mock_remote(self.workspace)
151153

152154
def tearDown(self):
153155
teardown_mock_remote(self.workspace)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# ===--- test_clone.py ----------------------------------------------------===#
2+
#
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See https:#swift.org/LICENSE.txt for license information
9+
# See https:#swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
#
11+
# ===----------------------------------------------------------------------===#
12+
13+
import os
14+
15+
from scheme_mock import call_quietly
16+
17+
from . import scheme_mock
18+
19+
WORKTREE_NAME = "feature"
20+
21+
22+
def path_for_worktree(workspace_path, worktree_name):
23+
return os.path.join(workspace_path, worktree_name)
24+
25+
26+
def setup_worktree(workspace_path, local_path, worktree_name):
27+
worktree_path = path_for_worktree(workspace_path, worktree_name)
28+
os.makedirs(worktree_path)
29+
for project in os.listdir(local_path):
30+
local_project_path = os.path.join(local_path, project)
31+
worktree_project_path = os.path.join(worktree_path, project)
32+
call_quietly(['git',
33+
'-C', local_project_path,
34+
'worktree', 'add', worktree_project_path])
35+
36+
37+
def teardown_worktree(workspace_path, local_path, worktree_name):
38+
worktree_path = path_for_worktree(workspace_path, worktree_name)
39+
for project in os.listdir(local_path):
40+
local_project_path = os.path.join(local_path, project)
41+
worktree_project_path = os.path.join(worktree_path, project)
42+
call_quietly(['git',
43+
'-C', local_project_path,
44+
'worktree', 'remove', worktree_project_path])
45+
46+
47+
class WorktreeTestCase(scheme_mock.SchemeMockTestCase):
48+
49+
def __init__(self, *args, **kwargs):
50+
super(WorktreeTestCase, self).__init__(*args, **kwargs)
51+
52+
def test_worktree(self):
53+
self.call([self.update_checkout_path,
54+
'--config', self.config_path,
55+
'--source-root', self.worktree_path,
56+
'--scheme', 'master'])
57+
58+
def setUp(self):
59+
super(WorktreeTestCase, self).setUp()
60+
self.worktree_path = os.path.join(self.workspace, WORKTREE_NAME)
61+
setup_worktree(self.workspace, self.local_path, WORKTREE_NAME)
62+
63+
def tearDown(self):
64+
teardown_worktree(self.workspace, self.local_path, WORKTREE_NAME)
65+
super(WorktreeTestCase, self).tearDown()

utils/update_checkout/update_checkout/update_checkout.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,15 @@ def update_single_repository(pool_args):
159159
if checkout_target:
160160
shell.run(['git', 'status', '--porcelain', '-uno'],
161161
echo=False)
162-
shell.run(['git', 'checkout', checkout_target], echo=True)
162+
try:
163+
shell.run(['git', 'checkout', checkout_target], echo=True)
164+
except Exception as originalException:
165+
try:
166+
result = shell.run(['git', 'rev-parse', checkout_target])
167+
revision = result[0].strip()
168+
shell.run(['git', 'checkout', revision], echo=True)
169+
except Exception as e:
170+
raise originalException
163171

164172
# It's important that we checkout, fetch, and rebase, in order.
165173
# .git/FETCH_HEAD updates the not-for-merge attributes based on

0 commit comments

Comments
 (0)