-
Notifications
You must be signed in to change notification settings - Fork 0
/
gclient_scm.py
2003 lines (1756 loc) · 83.3 KB
/
gclient_scm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Gclient-specific SCM-specific operations."""
import collections
import contextlib
import errno
import json
import logging
import os
import platform
import posixpath
import re
import shutil
import sys
import tempfile
import threading
import traceback
import gclient_utils
import gerrit_util
import git_cache
import scm
import subprocess2
# TODO: Should fix these warnings.
# pylint: disable=line-too-long
class NoUsableRevError(gclient_utils.Error):
"""Raised if requested revision isn't found in checkout."""
class DiffFiltererWrapper(object):
"""Simple base class which tracks which file is being diffed and
replaces instances of its file name in the original and
working copy lines of the git diff output."""
index_string = None
original_prefix = "--- "
working_prefix = "+++ "
def __init__(self, relpath, print_func):
# Note that we always use '/' as the path separator to be
# consistent with cygwin-style output on Windows
self._relpath = relpath.replace("\\", "/")
self._current_file = None
self._print_func = print_func
def SetCurrentFile(self, current_file):
self._current_file = current_file
@property
def _replacement_file(self):
return posixpath.join(self._relpath, self._current_file)
def _Replace(self, line):
return line.replace(self._current_file, self._replacement_file)
def Filter(self, line):
if (line.startswith(self.index_string)):
self.SetCurrentFile(line[len(self.index_string):])
line = self._Replace(line)
else:
if (line.startswith(self.original_prefix)
or line.startswith(self.working_prefix)):
line = self._Replace(line)
self._print_func(line)
class GitDiffFilterer(DiffFiltererWrapper):
index_string = "diff --git "
def SetCurrentFile(self, current_file):
# Get filename by parsing "a/<filename> b/<filename>"
self._current_file = current_file[:(len(current_file) / 2)][2:]
def _Replace(self, line):
return re.sub("[a|b]/" + self._current_file, self._replacement_file,
line)
# SCMWrapper base class
class SCMWrapper(object):
"""Add necessary glue between all the supported SCM.
This is the abstraction layer to bind to different SCM.
"""
def __init__(self,
url=None,
root_dir=None,
relpath=None,
out_fh=None,
out_cb=None,
print_outbuf=False):
self.url = url
self._root_dir = root_dir
if self._root_dir:
self._root_dir = self._root_dir.replace('/', os.sep)
self.relpath = relpath
if self.relpath:
self.relpath = self.relpath.replace('/', os.sep)
if self.relpath and self._root_dir:
self.checkout_path = os.path.join(self._root_dir, self.relpath)
if out_fh is None:
out_fh = sys.stdout
self.out_fh = out_fh
self.out_cb = out_cb
self.print_outbuf = print_outbuf
def Print(self, *args, **kwargs):
kwargs.setdefault('file', self.out_fh)
if kwargs.pop('timestamp', True):
self.out_fh.write('[%s] ' % gclient_utils.Elapsed())
print(*args, **kwargs)
def RunCommand(self, command, options, args, file_list=None):
commands = [
'update', 'updatesingle', 'revert', 'revinfo', 'status', 'diff',
'pack', 'runhooks'
]
if not command in commands:
raise gclient_utils.Error('Unknown command %s' % command)
if not command in dir(self):
raise gclient_utils.Error(
'Command %s not implemented in %s wrapper' %
(command, self.__class__.__name__))
return getattr(self, command)(options, args, file_list)
@staticmethod
def _get_first_remote_url(checkout_path):
log = scm.GIT.YieldConfigRegexp(checkout_path, r'remote.*.url')
return next(log)[1]
def GetCacheMirror(self):
if getattr(self, 'cache_dir', None):
url, _ = gclient_utils.SplitUrlRevision(self.url)
return git_cache.Mirror(url)
return None
def GetActualRemoteURL(self, options):
"""Attempt to determine the remote URL for this SCMWrapper."""
# Git
if os.path.exists(os.path.join(self.checkout_path, '.git')):
actual_remote_url = self._get_first_remote_url(self.checkout_path)
mirror = self.GetCacheMirror()
# If the cache is used, obtain the actual remote URL from there.
if (mirror and mirror.exists() and mirror.mirror_path.replace(
'\\', '/') == actual_remote_url.replace('\\', '/')):
actual_remote_url = self._get_first_remote_url(
mirror.mirror_path)
return actual_remote_url
return None
def DoesRemoteURLMatch(self, options):
"""Determine whether the remote URL of this checkout is the expected URL."""
if not os.path.exists(self.checkout_path):
# A checkout which doesn't exist can't be broken.
return True
actual_remote_url = self.GetActualRemoteURL(options)
if actual_remote_url:
return (gclient_utils.SplitUrlRevision(actual_remote_url)[0].rstrip(
'/') == gclient_utils.SplitUrlRevision(self.url)[0].rstrip('/'))
# This may occur if the self.checkout_path exists but does not contain a
# valid git checkout.
return False
def _DeleteOrMove(self, force):
"""Delete the checkout directory or move it out of the way.
Args:
force: bool; if True, delete the directory. Otherwise, just move it.
"""
if force and os.environ.get('CHROME_HEADLESS') == '1':
self.Print('_____ Conflicting directory found in %s. Removing.' %
self.checkout_path)
gclient_utils.AddWarning('Conflicting directory %s deleted.' %
self.checkout_path)
gclient_utils.rmtree(self.checkout_path)
else:
bad_scm_dir = os.path.join(self._root_dir, '_bad_scm',
os.path.dirname(self.relpath))
try:
os.makedirs(bad_scm_dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
dest_path = tempfile.mkdtemp(prefix=os.path.basename(self.relpath),
dir=bad_scm_dir)
self.Print(
'_____ Conflicting directory found in %s. Moving to %s.' %
(self.checkout_path, dest_path))
gclient_utils.AddWarning('Conflicting directory %s moved to %s.' %
(self.checkout_path, dest_path))
shutil.move(self.checkout_path, dest_path)
class GitWrapper(SCMWrapper):
"""Wrapper for Git"""
name = 'git'
remote = 'origin'
@property
def cache_dir(self):
try:
return git_cache.Mirror.GetCachePath()
except RuntimeError:
return None
def __init__(self, url=None, *args, **kwargs):
"""Removes 'git+' fake prefix from git URL."""
if url and (url.startswith('git+http://')
or url.startswith('git+https://')):
url = url[4:]
SCMWrapper.__init__(self, url, *args, **kwargs)
filter_kwargs = {'time_throttle': 1, 'out_fh': self.out_fh}
if self.out_cb:
filter_kwargs['predicate'] = self.out_cb
self.filter = gclient_utils.GitFilter(**filter_kwargs)
self._running_under_rosetta = None
def GetCheckoutRoot(self):
return scm.GIT.GetCheckoutRoot(self.checkout_path)
def GetRevisionDate(self, _revision):
"""Returns the given revision's date in ISO-8601 format (which contains the
time zone)."""
# TODO(floitsch): get the time-stamp of the given revision and not just
# the time-stamp of the currently checked out revision.
return self._Capture(['log', '-n', '1', '--format=%ai'])
def _GetDiffFilenames(self, base):
"""Returns the names of files modified since base."""
return self._Capture(
# Filter to remove base if it is None.
list(
filter(
bool,
['-c', 'core.quotePath=false', 'diff', '--name-only', base])
)).split()
def diff(self, options, _args, _file_list):
_, revision = gclient_utils.SplitUrlRevision(self.url)
if not revision:
revision = 'refs/remotes/%s/main' % self.remote
self._Run(['-c', 'core.quotePath=false', 'diff', revision], options)
def pack(self, _options, _args, _file_list):
"""Generates a patch file which can be applied to the root of the
repository.
The patch file is generated from a diff of the merge base of HEAD and
its upstream branch.
"""
try:
merge_base = [self._Capture(['merge-base', 'HEAD', self.remote])]
except subprocess2.CalledProcessError:
merge_base = []
gclient_utils.CheckCallAndFilter(['git', 'diff'] + merge_base,
cwd=self.checkout_path,
filter_fn=GitDiffFilterer(
self.relpath,
print_func=self.Print).Filter)
def _Scrub(self, target, options):
"""Scrubs out all changes in the local repo, back to the state of target."""
quiet = []
if not options.verbose:
quiet = ['--quiet']
self._Run(['reset', '--hard', target] + quiet, options)
if options.force and options.delete_unversioned_trees:
# where `target` is a commit that contains both upper and lower case
# versions of the same file on a case insensitive filesystem, we are
# actually in a broken state here. The index will have both 'a' and
# 'A', but only one of them will exist on the disk. To progress, we
# delete everything that status thinks is modified.
output = self._Capture(
['-c', 'core.quotePath=false', 'status', '--porcelain'],
strip=False)
for line in output.splitlines():
# --porcelain (v1) looks like:
# XY filename
try:
filename = line[3:]
self.Print('_____ Deleting residual after reset: %r.' %
filename)
gclient_utils.rm_file_or_tree(
os.path.join(self.checkout_path, filename))
except OSError:
pass
def _FetchAndReset(self, revision, file_list, options):
"""Equivalent to git fetch; git reset."""
self._SetFetchConfig(options)
self._Fetch(options, prune=True, quiet=options.verbose)
revision = self._AutoFetchRef(options, revision)
self._Scrub(revision, options)
if file_list is not None:
files = self._Capture(['-c', 'core.quotePath=false',
'ls-files']).splitlines()
file_list.extend(
[os.path.join(self.checkout_path, f) for f in files])
def _DisableHooks(self):
hook_dir = os.path.join(self.checkout_path, '.git', 'hooks')
if not os.path.isdir(hook_dir):
return
for f in os.listdir(hook_dir):
if not f.endswith('.sample') and not f.endswith('.disabled'):
disabled_hook_path = os.path.join(hook_dir, f + '.disabled')
if os.path.exists(disabled_hook_path):
os.remove(disabled_hook_path)
os.rename(os.path.join(hook_dir, f), disabled_hook_path)
def _maybe_break_locks(self, options):
"""This removes all .lock files from this repo's .git directory, if the
user passed the --break_repo_locks command line flag.
In particular, this will cleanup index.lock files, as well as ref lock
files.
"""
if options.break_repo_locks:
git_dir = os.path.join(self.checkout_path, '.git')
for path, _, filenames in os.walk(git_dir):
for filename in filenames:
if filename.endswith('.lock'):
to_break = os.path.join(path, filename)
self.Print('breaking lock: %s' % (to_break, ))
try:
os.remove(to_break)
except OSError as ex:
self.Print('FAILED to break lock: %s: %s' %
(to_break, ex))
raise
def _download_topics(self, patch_rev, googlesource_url):
"""This method returns new patch_revs to process that have the same topic.
It does the following:
1. Finds the topic of the Gerrit change specified in the patch_rev.
2. Find all changes with that topic.
3. Append patch_rev of the changes with the same topic to the patch_revs
to process.
4. Returns the new patch_revs to process.
"""
patch_revs_to_process = []
# Parse the patch_rev to extract the CL and patchset.
patch_rev_tokens = patch_rev.split('/')
change = patch_rev_tokens[-2]
# Parse the googlesource_url.
tokens = re.search('//(.+).googlesource.com/(.+?)(?:\.git)?$',
googlesource_url)
if not tokens or len(tokens.groups()) != 2:
# googlesource_url is not in the expected format.
return patch_revs_to_process
# parse the gerrit host and repo out of googlesource_url.
host, repo = tokens.groups()[:2]
gerrit_host_url = '%s-review.googlesource.com' % host
# 1. Find the topic of the Gerrit change specified in the patch_rev.
change_object = gerrit_util.GetChange(gerrit_host_url, change)
topic = change_object.get('topic')
if not topic:
# This change has no topic set.
return patch_revs_to_process
# 2. Find all changes with that topic.
changes_with_same_topic = gerrit_util.QueryChanges(
gerrit_host_url, [('topic', topic), ('status', 'open'),
('repo', repo)],
o_params=['ALL_REVISIONS'])
for c in changes_with_same_topic:
if str(c['_number']) == change:
# This change is already in the patch_rev.
continue
self.Print('Found CL %d with the topic name %s' %
(c['_number'], topic))
# 3. Append patch_rev of the changes with the same topic to the
# patch_revs to process.
curr_rev = c['current_revision']
new_patch_rev = c['revisions'][curr_rev]['ref']
patch_revs_to_process.append(new_patch_rev)
# 4. Return the new patch_revs to process.
return patch_revs_to_process
def _ref_to_remote_ref(self, target_rev):
"""Helper function for scm.GIT.RefToRemoteRef with error checking.
Joins the results of scm.GIT.RefToRemoteRef into a string, but raises a
comprehensible error if RefToRemoteRef fails.
Args:
target_rev: a ref somewhere under refs/.
"""
tmp_ref = scm.GIT.RefToRemoteRef(target_rev, self.remote)
if not tmp_ref:
raise gclient_utils.Error(
'Failed to turn target revision %r in repo %r into remote ref' %
(target_rev, self.checkout_path))
return ''.join(tmp_ref)
def apply_patch_ref(self, patch_repo, patch_rev, target_rev, options,
file_list):
# type: (str, str, str, optparse.Values, Collection[str]) -> str
"""Apply a patch on top of the revision we're synced at.
The patch ref is given by |patch_repo|@|patch_rev|.
|target_rev| is usually the branch that the |patch_rev| was uploaded against
(e.g. 'refs/heads/main'), but this is not required.
We cherry-pick all commits reachable from |patch_rev| on top of the curret
HEAD, excluding those reachable from |target_rev|
(i.e. git cherry-pick target_rev..patch_rev).
Graphically, it looks like this:
... -> o -> [possibly already landed commits] -> target_rev
\
-> [possibly not yet landed dependent CLs] -> patch_rev
The final checkout state is then:
... -> HEAD -> [possibly not yet landed dependent CLs] -> patch_rev
After application, if |options.reset_patch_ref| is specified, we soft reset
the cherry-picked changes, keeping them in git index only.
Args:
patch_repo: The patch origin.
e.g. 'https://foo.googlesource.com/bar'
patch_rev: The revision to patch.
e.g. 'refs/changes/1234/34/1'.
target_rev: The revision to use when finding the merge base.
Typically, the branch that the patch was uploaded against.
e.g. 'refs/heads/main' or 'refs/heads/infra/config'.
options: The options passed to gclient.
file_list: A list where modified files will be appended.
"""
# Abort any cherry-picks in progress.
try:
self._Capture(['cherry-pick', '--abort'])
except subprocess2.CalledProcessError:
pass
base_rev = self.revinfo(None, None, None)
if not target_rev:
raise gclient_utils.Error(
'A target revision for the patch must be given')
if target_rev.startswith(('refs/heads/', 'refs/branch-heads')):
# If |target_rev| is in refs/heads/** or refs/branch-heads/**, try
# first to find the corresponding remote ref for it, since
# |target_rev| might point to a local ref which is not up to date
# with the corresponding remote ref.
remote_ref = self._ref_to_remote_ref(target_rev)
self.Print('Trying the corresponding remote ref for %r: %r\n' %
(target_rev, remote_ref))
if scm.GIT.IsValidRevision(self.checkout_path, remote_ref):
# refs/remotes may need to be updated to cleanly cherry-pick
# changes. See https://crbug.com/1255178.
self._Capture(['fetch', '--no-tags', self.remote, target_rev])
target_rev = remote_ref
elif not scm.GIT.IsValidRevision(self.checkout_path, target_rev):
# Fetch |target_rev| if it's not already available.
url, _ = gclient_utils.SplitUrlRevision(self.url)
mirror = self._GetMirror(url, options, target_rev, target_rev)
if mirror:
rev_type = 'branch' if target_rev.startswith(
'refs/') else 'hash'
self._UpdateMirrorIfNotContains(mirror, options, rev_type,
target_rev)
self._Fetch(options, refspec=target_rev)
patch_revs_to_process = [patch_rev]
if hasattr(options, 'download_topics') and options.download_topics:
patch_revs_to_process_from_topics = self._download_topics(
patch_rev, self.url)
patch_revs_to_process.extend(patch_revs_to_process_from_topics)
self._Capture(['reset', '--hard'])
for pr in patch_revs_to_process:
self.Print('===Applying patch===')
self.Print('Revision to patch is %r @ %r.' % (patch_repo, pr))
self.Print('Current dir is %r' % self.checkout_path)
self._Capture(['fetch', '--no-tags', patch_repo, pr])
pr = self._Capture(['rev-parse', 'FETCH_HEAD'])
if not options.rebase_patch_ref:
self._Capture(['checkout', pr])
# Adjust base_rev to be the first parent of our checked out
# patch ref; This will allow us to correctly extend `file_list`,
# and will show the correct file-list to programs which do `git
# diff --cached` expecting to see the patch diff.
base_rev = self._Capture(['rev-parse', pr + '~'])
else:
self.Print('Will cherrypick %r .. %r on top of %r.' %
(target_rev, pr, base_rev))
try:
if scm.GIT.IsAncestor(pr,
target_rev,
cwd=self.checkout_path):
if len(patch_revs_to_process) > 1:
# If there are multiple patch_revs_to_process then
# we do not want want to invalidate a previous patch
# so throw an error.
raise gclient_utils.Error(
'patch_rev %s is an ancestor of target_rev %s. This '
'situation is unsupported when we need to apply multiple '
'patch_revs: %s' %
(pr, target_rev, patch_revs_to_process))
# If |patch_rev| is an ancestor of |target_rev|, check
# it out.
self._Capture(['checkout', pr])
else:
# If a change was uploaded on top of another change,
# which has already landed, one of the commits in the
# cherry-pick range will be redundant, since it has
# already landed and its changes incorporated in the
# tree. We pass '--keep-redundant-commits' to ignore
# those changes.
self._Capture([
'cherry-pick', target_rev + '..' + pr,
'--keep-redundant-commits'
])
except subprocess2.CalledProcessError as e:
self.Print('Failed to apply patch.')
self.Print('Revision to patch was %r @ %r.' %
(patch_repo, pr))
self.Print('Tried to cherrypick %r .. %r on top of %r.' %
(target_rev, pr, base_rev))
self.Print('Current dir is %r' % self.checkout_path)
self.Print('git returned non-zero exit status %s:\n%s' %
(e.returncode, e.stderr.decode('utf-8')))
# Print the current status so that developers know what
# changes caused the patch failure, since git cherry-pick
# doesn't show that information.
self.Print(self._Capture(['status']))
try:
self._Capture(['cherry-pick', '--abort'])
except subprocess2.CalledProcessError:
pass
raise
if file_list is not None:
file_list.extend(self._GetDiffFilenames(base_rev))
latest_commit = self.revinfo(None, None, None)
if options.reset_patch_ref:
self._Capture(['reset', '--soft', base_rev])
return latest_commit
def check_diff(self, previous_commit, files=None):
# type: (str, Optional[List[str]]) -> bool
"""Check if a diff exists between the current commit and `previous_commit`.
Returns True if there were diffs or if an error was encountered.
"""
cmd = ['diff', previous_commit, '--quiet']
if files:
cmd += ['--'] + files
try:
self._Capture(cmd)
return False
except subprocess2.CalledProcessError as e:
# git diff --quiet exits with 1 if there were diffs.
if e.returncode != 1:
self.Print('git returned non-zero exit status %s:\n%s' %
(e.returncode, e.stderr.decode('utf-8')))
return True
def set_config(f):
def wrapper(*args):
return_val = f(*args)
if os.path.exists(os.path.join(args[0].checkout_path, '.git')):
# The config updates to the project are stored in this list
# and updated consecutively after the reads. The updates
# are done this way because `scm.GIT.GetConfig` caches
# the config file and `scm.GIT.SetConfig` evicts the cache.
# This ensures we don't interleave reads and writes causing
# the cache to set and unset consecutively.
config_updates = []
if scm.GIT.GetConfig(args[0].checkout_path,
'diff.ignoresubmodules') != 'dirty':
# If diff.ignoreSubmodules is not already set, set it to `all`.
config_updates.append(('diff.ignoreSubmodules', 'dirty'))
if scm.GIT.GetConfig(args[0].checkout_path,
'fetch.recursesubmodules') != 'off':
config_updates.append(('fetch.recurseSubmodules', 'off'))
if scm.GIT.GetConfig(args[0].checkout_path,
'push.recursesubmodules') != 'off':
# The default is off, but if user sets submodules.recurse to
# on, this becomes on too. We never want to push submodules
# for gclient managed repositories. Push, even if a no-op,
# will increase `git cl upload` latency.
config_updates.append(('push.recurseSubmodules', 'off'))
for update in config_updates:
scm.GIT.SetConfig(args[0].checkout_path,
update[0],
update[1],
modify_all=True)
return return_val
return wrapper
@set_config
def update(self, options, args, file_list):
"""Runs git to update or transparently checkout the working copy.
All updated files will be appended to file_list.
Raises:
Error: if can't get URL for relative path.
"""
if args:
raise gclient_utils.Error("Unsupported argument(s): %s" %
",".join(args))
url, deps_revision = gclient_utils.SplitUrlRevision(self.url)
revision = deps_revision
managed = True
if options.revision:
# Override the revision number.
revision = str(options.revision)
if revision == 'unmanaged':
# Check again for a revision in case an initial ref was specified
# in the url, for example bla.git@refs/heads/custombranch
revision = deps_revision
managed = False
if not revision:
# If a dependency is not pinned, track the default remote branch.
revision = scm.GIT.GetRemoteHeadRef(self.checkout_path, self.url,
self.remote)
if revision.startswith('origin/'):
revision = 'refs/remotes/' + revision
if managed and platform.system() == 'Windows':
self._DisableHooks()
printed_path = False
verbose = []
if options.verbose:
self.Print('_____ %s at %s' % (self.relpath, revision),
timestamp=False)
verbose = ['--verbose']
printed_path = True
revision_ref = revision
if ':' in revision:
revision_ref, _, revision = revision.partition(':')
if revision_ref.startswith('refs/branch-heads'):
options.with_branch_heads = True
mirror = self._GetMirror(url, options, revision, revision_ref)
if mirror:
url = mirror.mirror_path
remote_ref = scm.GIT.RefToRemoteRef(revision, self.remote)
if remote_ref:
# Rewrite remote refs to their local equivalents.
revision = ''.join(remote_ref)
rev_type = "branch"
elif revision.startswith('refs/'):
# Local branch? We probably don't want to support, since DEPS should
# always specify branches as they are in the upstream repo.
rev_type = "branch"
else:
# hash is also a tag, only make a distinction at checkout
rev_type = "hash"
# If we are going to introduce a new project, there is a possibility
# that we are syncing back to a state where the project was originally a
# sub-project rolled by DEPS (realistic case: crossing the Blink merge
# point syncing backwards, when Blink was a DEPS entry and not part of
# src.git). In such case, we might have a backup of the former .git
# folder, which can be used to avoid re-fetching the entire repo again
# (useful for bisects).
backup_dir = self.GetGitBackupDirPath()
target_dir = os.path.join(self.checkout_path, '.git')
if os.path.exists(backup_dir) and not os.path.exists(target_dir):
gclient_utils.safe_makedirs(self.checkout_path)
os.rename(backup_dir, target_dir)
# Reset to a clean state
self._Scrub('HEAD', options)
if (not os.path.exists(self.checkout_path) or
(os.path.isdir(self.checkout_path)
and not os.path.exists(os.path.join(self.checkout_path, '.git')))):
if mirror:
self._UpdateMirrorIfNotContains(mirror, options, rev_type,
revision)
try:
self._Clone(revision, url, options)
except subprocess2.CalledProcessError as e:
logging.warning('Clone failed due to: %s', e)
self._DeleteOrMove(options.force)
self._Clone(revision, url, options)
if file_list is not None:
files = self._Capture(
['-c', 'core.quotePath=false', 'ls-files']).splitlines()
file_list.extend(
[os.path.join(self.checkout_path, f) for f in files])
if mirror:
self._Capture(
['remote', 'set-url', '--push', 'origin', mirror.url])
if not verbose:
# Make the output a little prettier. It's nice to have some
# whitespace between projects when cloning.
self.Print('')
return self._Capture(['rev-parse', '--verify', 'HEAD'])
if mirror:
self._Capture(['remote', 'set-url', '--push', 'origin', mirror.url])
if not managed:
self._SetFetchConfig(options)
self.Print('________ unmanaged solution; skipping %s' %
self.relpath)
return self._Capture(['rev-parse', '--verify', 'HEAD'])
self._maybe_break_locks(options)
if mirror:
self._UpdateMirrorIfNotContains(mirror, options, rev_type, revision)
# See if the url has changed (the unittests use git://foo for the url,
# let that through).
current_url = scm.GIT.GetConfig(self.checkout_path,
f'remote.{self.remote}.url')
return_early = False
# TODO(maruel): Delete url != 'git://foo' since it's just to make the
# unit test pass. (and update the comment above)
# Skip url auto-correction if remote.origin.gclient-auto-fix-url is set.
# This allows devs to use experimental repos which have a different url
# but whose branch(s) are the same as official repos.
strp_url = url[:-4] if url.endswith('.git') else url
strp_current_url = current_url[:-4] if current_url.endswith(
'.git') else current_url
if (strp_current_url.rstrip('/') != strp_url.rstrip('/')
and url != 'git://foo' and scm.GIT.GetConfigBool(
self.checkout_path,
f'remote.{self.remote}.gclient-auto-fix-url')):
self.Print('_____ switching %s from %s to new upstream %s' %
(self.relpath, current_url, url))
if not (options.force or options.reset):
# Make sure it's clean
self._CheckClean(revision)
# Switch over to the new upstream
self._Run(['remote', 'set-url', self.remote, url], options)
if mirror:
if git_cache.Mirror.CacheDirToUrl(current_url.rstrip(
'/')) == git_cache.Mirror.CacheDirToUrl(
url.rstrip('/')):
# Reset alternates when the cache dir is updated.
with open(
os.path.join(self.checkout_path, '.git', 'objects',
'info', 'alternates'), 'w') as fh:
fh.write(os.path.join(url, 'objects'))
else:
# Because we use Git alternatives, our existing repository
# is not self-contained. It's possible that new git
# alternative doesn't have all necessary objects that the
# current repository needs. Instead of blindly hoping that
# new alternative contains all necessary objects, keep the
# old alternative and just append a new one on top of it.
with open(
os.path.join(self.checkout_path, '.git', 'objects',
'info', 'alternates'), 'a') as fh:
fh.write("\n" + os.path.join(url, 'objects'))
self._EnsureValidHeadObjectOrCheckout(revision, options, url)
self._FetchAndReset(revision, file_list, options)
return_early = True
else:
self._EnsureValidHeadObjectOrCheckout(revision, options, url)
if return_early:
return self._Capture(['rev-parse', '--verify', 'HEAD'])
cur_branch = self._GetCurrentBranch()
# Cases:
# 0) HEAD is detached. Probably from our initial clone.
# - make sure HEAD is contained by a named ref, then update.
# Cases 1-4. HEAD is a branch.
# 1) current branch is not tracking a remote branch
# - try to rebase onto the new hash or branch
# 2) current branch is tracking a remote branch with local committed
# changes, but the DEPS file switched to point to a hash
# - rebase those changes on top of the hash
# 3) current branch is tracking a remote branch w/or w/out changes, and
# no DEPS switch
# - see if we can FF, if not, prompt the user for rebase, merge, or stop
# 4) current branch is tracking a remote branch, but DEPS switches to a
# different remote branch, and a) current branch has no local changes,
# and --force: - checkout new branch b) current branch has local
# changes, and --force and --reset: - checkout new branch c) otherwise
# exit
# GetUpstreamBranch returns something like 'refs/remotes/origin/main'
# for a tracking branch or 'main' if not a tracking branch (it's based
# on a specific rev/hash) or it returns None if it couldn't find an
# upstream
if cur_branch is None:
upstream_branch = None
current_type = "detached"
logging.debug("Detached HEAD")
else:
upstream_branch = scm.GIT.GetUpstreamBranch(self.checkout_path)
if not upstream_branch or not upstream_branch.startswith(
'refs/remotes'):
current_type = "hash"
logging.debug(
"Current branch is not tracking an upstream (remote)"
" branch.")
elif upstream_branch.startswith('refs/remotes'):
current_type = "branch"
else:
raise gclient_utils.Error('Invalid Upstream: %s' %
upstream_branch)
self._SetFetchConfig(options)
# Fetch upstream if we don't already have |revision|.
if not scm.GIT.IsValidRevision(
self.checkout_path, revision, sha_only=True):
self._Fetch(options, prune=options.force)
if not scm.GIT.IsValidRevision(
self.checkout_path, revision, sha_only=True):
# Update the remotes first so we have all the refs.
remote_output = scm.GIT.Capture(['remote'] + verbose +
['update'],
cwd=self.checkout_path)
if verbose:
self.Print(remote_output)
revision = self._AutoFetchRef(options, revision)
# This is a big hammer, debatable if it should even be here...
if options.force or options.reset:
target = 'HEAD'
if options.upstream and upstream_branch:
target = upstream_branch
self._Scrub(target, options)
if current_type == 'detached':
# case 0
# We just did a Scrub, this is as clean as it's going to get. In
# particular if HEAD is a commit that contains two versions of the
# same file on a case-insensitive filesystem (e.g. 'a' and 'A'),
# there's no way to actually "Clean" the checkout; that commit is
# uncheckoutable on this system. The best we can do is carry forward
# to the checkout step.
if not (options.force or options.reset):
self._CheckClean(revision)
self._CheckDetachedHead(revision, options)
if self._Capture(['rev-list', '-n', '1', 'HEAD']) == revision:
self.Print('Up-to-date; skipping checkout.')
else:
# 'git checkout' may need to overwrite existing untracked files.
# Allow it only when nuclear options are enabled.
self._Checkout(
options,
revision,
force=(options.force and options.delete_unversioned_trees),
quiet=True,
)
if not printed_path:
self.Print('_____ %s at %s' % (self.relpath, revision),
timestamp=False)
elif current_type == 'hash':
# case 1
# Can't find a merge-base since we don't know our upstream. That
# makes this command VERY likely to produce a rebase failure. For
# now we assume origin is our upstream since that's what the old
# behavior was.
upstream_branch = self.remote
if options.revision or deps_revision:
upstream_branch = revision
self._AttemptRebase(upstream_branch,
file_list,
options,
printed_path=printed_path,
merge=options.merge)
printed_path = True
elif rev_type == 'hash':
# case 2
self._AttemptRebase(upstream_branch,
file_list,
options,
newbase=revision,
printed_path=printed_path,
merge=options.merge)
printed_path = True
elif remote_ref and ''.join(remote_ref) != upstream_branch:
# case 4
new_base = ''.join(remote_ref)
if not printed_path:
self.Print('_____ %s at %s' % (self.relpath, revision),
timestamp=False)
switch_error = (
"Could not switch upstream branch from %s to %s\n" %
(upstream_branch, new_base) +
"Please use --force or merge or rebase manually:\n" +
"cd %s; git rebase %s\n" % (self.checkout_path, new_base) +
"OR git checkout -b <some new branch> %s" % new_base)
force_switch = False
if options.force:
try:
self._CheckClean(revision)
# case 4a
force_switch = True
except gclient_utils.Error as e:
if options.reset:
# case 4b
force_switch = True
else:
switch_error = '%s\n%s' % (str(e), switch_error)
if force_switch:
self.Print("Switching upstream branch from %s to %s" %
(upstream_branch, new_base))
switch_branch = 'gclient_' + remote_ref[1]
self._Capture(['branch', '-f', switch_branch, new_base])
self._Checkout(options, switch_branch, force=True, quiet=True)
else:
# case 4c
raise gclient_utils.Error(switch_error)
else:
# case 3 - the default case
rebase_files = self._GetDiffFilenames(upstream_branch)
if verbose:
self.Print('Trying fast-forward merge to branch : %s' %
upstream_branch)
try:
merge_args = ['merge']
if options.merge:
merge_args.append('--ff')
else:
merge_args.append('--ff-only')
merge_args.append(upstream_branch)
merge_output = self._Capture(merge_args)
except subprocess2.CalledProcessError as e:
rebase_files = []
if re.search(b'fatal: Not possible to fast-forward, aborting.',
e.stderr):
if not printed_path:
self.Print('_____ %s at %s' % (self.relpath, revision),
timestamp=False)
printed_path = True
while True:
if not options.auto_rebase:
try:
action = self._AskForData(
'Cannot %s, attempt to rebase? '
'(y)es / (q)uit / (s)kip : ' %
('merge' if options.merge else
'fast-forward merge'), options)
except ValueError:
raise gclient_utils.Error('Invalid Character')
if options.auto_rebase or re.match(
r'yes|y', action, re.I):
self._AttemptRebase(upstream_branch,
rebase_files,
options,
printed_path=printed_path,
merge=False)
printed_path = True
break
if re.match(r'quit|q', action, re.I):
raise gclient_utils.Error(
"Can't fast-forward, please merge or "
"rebase manually.\n"
"cd %s && git " % self.checkout_path +
"rebase %s" % upstream_branch)
if re.match(r'skip|s', action, re.I):