/
GitRepository.java
1729 lines (1526 loc) · 58.3 KB
/
GitRepository.java
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) 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.copybara.git;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.copybara.util.CommandRunner.DEFAULT_TIMEOUT;
import static com.google.copybara.util.CommandRunner.NO_INPUT;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.StandardSystemProperty;
import com.google.common.base.Strings;
import com.google.common.base.Verify;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableRangeSet;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Range;
import com.google.common.flogger.FluentLogger;
import com.google.copybara.authoring.Author;
import com.google.copybara.authoring.AuthorParser;
import com.google.copybara.authoring.InvalidAuthorException;
import com.google.copybara.exception.CannotResolveRevisionException;
import com.google.copybara.exception.EmptyChangeException;
import com.google.copybara.exception.RepoException;
import com.google.copybara.exception.ValidationException;
import com.google.copybara.git.GitCredential.UserPassword;
import com.google.copybara.util.BadExitStatusWithOutputException;
import com.google.copybara.util.CommandOutput;
import com.google.copybara.util.CommandOutputWithStatus;
import com.google.copybara.util.CommandRunner;
import com.google.copybara.util.FileUtil;
import com.google.copybara.util.RepositoryUtil;
import com.google.copybara.shell.Command;
import com.google.copybara.shell.CommandException;
import com.google.re2j.Matcher;
import com.google.re2j.Pattern;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;
/**
* A class for manipulating Git repositories
*/
public class GitRepository {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private static final java.util.regex.Pattern SPACES = java.util.regex.Pattern.compile("( |\t)+");
// TODO(malcon): Make this generic (Using URIish.java)
private static final Pattern FULL_URI = Pattern.compile(
"([a-z][a-z0-9+-]+@[a-zA-Z0-9_.-]+(:.+)?|^[a-z][a-z0-9+-]+://.*)$");
private static final Pattern LS_TREE_ELEMENT = Pattern.compile(
"([0-9]{6}) (commit|tag|tree|blob) ([a-f0-9]{40})\t(.*)");
private static final Pattern LS_REMOTE_OUTPUT_LINE = Pattern.compile("([a-f0-9]{40})\t(.+)");
private static final Pattern SHA1_PATTERN = Pattern.compile("[a-f0-9]{6,40}");
private static final Pattern FAILED_REBASE = Pattern.compile("Failed to merge in the changes");
private static final ImmutableList<Pattern> REF_NOT_FOUND_ERRORS =
ImmutableList.of(
Pattern.compile("pathspec '(.+)' did not match any file"),
Pattern.compile(
"ambiguous argument '(.+)': unknown revision or path not in the working tree"));
private static final Pattern FETCH_CANNOT_RESOLVE_ERRORS =
Pattern.compile(
""
// When fetching a ref like 'refs/foo' fails.
+ "(fatal: [Cc]ouldn't find remote ref"
// When fetching a SHA-1 ref
+ "|no such remote ref"
// New output for fetching (git version 2.17)
+ "|fatal: no matching remote head"
// Local fetch for a SHA-1 fails
+ "|upload-pack: not our ref"
// Gerrit when fetching
+ "|ERR want .+ not valid)");
private static final Pattern NO_GIT_REPOSITORY =
Pattern.compile("does not appear to be a git repository");
/**
* Label to be used for marking the original revision id (Git SHA-1) for migrated commits.
*/
public static final String GIT_ORIGIN_REV_ID = "GitOrigin-RevId";
// Git exits with 128 in several circumstances. For example failed rebase.
private static final ImmutableRangeSet<Integer> NON_CRASH_ERROR_EXIT_CODES =
ImmutableRangeSet.<Integer>builder().add(
Range.closed(1, 10)).add(Range.singleton(128)).build();
/**
* We cannot control the repo storage location, but we can limit the number of characters of the
* repo folder name.
*/
private static final int DEFAULT_MAX_LOG_LINES = -1;
/**
* The location of the {@code .git} directory. The is also the value of the {@code --git-dir}
* flag.
*/
private final Path gitDir;
@Nullable
private final Path workTree;
private final boolean verbose;
private final GitEnvironment gitEnv;
private final Duration fetchTimeout;
private static final Map<Character, StatusCode> CHAR_TO_STATUS_CODE =
Arrays.stream(StatusCode.values())
.collect(Collectors.toMap(StatusCode::getCode, Function.identity()));
protected GitRepository(
Path gitDir, @Nullable Path workTree, boolean verbose, GitEnvironment gitEnv,
Duration fetchTimeout) {
this.gitDir = checkNotNull(gitDir);
this.workTree = workTree;
this.verbose = verbose;
this.gitEnv = checkNotNull(gitEnv);
this.fetchTimeout = checkNotNull(fetchTimeout);
}
/** Creates a new repository in the given directory. The new repo is not bare. */
public static GitRepository newRepo(boolean verbose, Path path, GitEnvironment gitEnv,
Duration fetchTimeout) {
return new GitRepository(path.resolve(".git"), path, verbose, gitEnv, fetchTimeout);
}
/** Create a new bare repository */
public static GitRepository newBareRepo(Path gitDir, GitEnvironment gitEnv, boolean verbose,
Duration fetchTimeout) {
return new GitRepository(gitDir, /*workTree=*/ null, verbose, gitEnv, fetchTimeout);
}
/**
* Get the version of git that will be used for running migrations. Returns empty if git cannot be
* found.
*
* @param gitEnv
*/
private static Optional<String> version(GitEnvironment gitEnv) {
try {
String version =
executeGit(
Paths.get(StandardSystemProperty.USER_DIR.value()),
ImmutableList.of("version"),
gitEnv,
/*verbose=*/ false)
.getStdout();
return Optional.of(version);
} catch (CommandException e) {
return Optional.empty();
}
}
/**
* Validate that a refspec is valid.
*
* @throws InvalidRefspecException if the refspec is not valid
*/
static void validateRefSpec(GitEnvironment gitEnv, Path cwd, String refspec)
throws InvalidRefspecException {
try {
executeGit(
cwd,
ImmutableList.of("check-ref-format", "--allow-onelevel", "--refspec-pattern", refspec),
gitEnv,
/*verbose=*/ false);
} catch (CommandException e) {
Optional<String> version = version(gitEnv);
throw new InvalidRefspecException(
version
.map(s -> "Invalid refspec: " + refspec)
.orElseGet(
() ->
String.format("Cannot find git binary at '%s'", gitEnv.resolveGitBinary())));
}
}
/**
* Fetch a reference from a git url.
*
* <p>Note that this method doesn't support fetching refspecs that contain local ref path
* locations. IOW
* "refs/foo" is allowed but not "refs/foo:remote/origin/foo". Wildcards are also not allowed.
*/
public GitRevision fetchSingleRef(String url, String ref)
throws RepoException, ValidationException {
if (ref.contains(":") || ref.contains("*")) {
throw new CannotResolveRevisionException("Fetching refspecs that"
+ " contain local ref path locations or wildcards is not supported. Invalid ref: " + ref);
}
// This is not strictly necessary for some Git repos that allow fetching from any sha1 ref, like
// servers configured with 'git config uploadpack.allowReachableSHA1InWant true'. Unfortunately,
// Github doesn't support it. So what we do is fetch the default refspec (see the comment
// below) and hope the sha1 is reachable from heads.
// If we fail to find the SHA-1 with that fetch we fetch the SHA-1 directly and hope the server
// allows to download it.
if (isSha1Reference(ref)) {
fetch(url, /*prune=*/false, /*force=*/true, ImmutableList.of());
try {
return resolveReferenceWithContext(ref, /*contextRef=*/ref, url);
} catch (RepoException | CannotResolveRevisionException ignore) {
// Ignore, the fetch below will attempt using the SHA-1.
}
}
fetch(url, /*prune=*/false, /*force=*/true, ImmutableList.of(ref));
return resolveReferenceWithContext("FETCH_HEAD", /*contextRef=*/ref, url);
}
/**
* Fetch zero or more refspecs in the local repository
*
* @param url remote git repository url
* @param prune if remotely non-present refs should be deleted locally
* @param force force updates even for non fast-forward updates
* @param refspecs a set refspecs in the form of 'foo' for branches, 'refs/some/ref' or
* 'refs/foo/bar:refs/bar/foo'.
* @return the set of fetched references and what action was done ( rejected, new reference,
* updated, etc.)
*/
public FetchResult fetch(String url, boolean prune, boolean force, Iterable<String> refspecs)
throws RepoException, ValidationException {
List<String> args = Lists.newArrayList("fetch", validateUrl(url));
args.add("--verbose");
// This shows progress in the log if not attached to a terminal
args.add("--progress");
if (prune) {
args.add("-p");
}
if (force) {
args.add("-f");
}
for (String ref : refspecs) {
createRefSpec(ref);
args.add(ref);
}
ImmutableMap<String, GitRevision> before = showRef();
CommandOutputWithStatus output = gitAllowNonZeroExit(NO_INPUT, args, fetchTimeout);
if (output.getTerminationStatus().success()) {
ImmutableMap<String, GitRevision> after = showRef();
return new FetchResult(before, after);
}
if (output.getStderr().isEmpty()
|| FETCH_CANNOT_RESOLVE_ERRORS.matcher(output.getStderr()).find()) {
throw new CannotResolveRevisionException("Cannot find references: " + refspecs);
} else if (NO_GIT_REPOSITORY.matcher(output.getStderr()).find()) {
throw new CannotResolveRevisionException(
String.format("Invalid Git repository: %s. Error: %s", url, output.getStderr()));
} else if (output.getStderr().contains(
"Server does not allow request for unadvertised object")) {
throw new CannotResolveRevisionException(
String.format("%s: %s", url, output.getStderr().trim()));
} else if (output.getStderr().contains("Repository not found")) {
throw new ValidationException(String.format("%s: %s", url, output.getStderr()));
} else {
throw throwUnknownGitError(output, args);
}
}
/**
* Create a refspec from a string
*/
public Refspec createRefSpec(String ref) throws ValidationException {
// Validate refspec
return Refspec.create(gitEnv, gitDir, ref);
}
@CheckReturnValue
public LogCmd log(String referenceExpr) {
return LogCmd.create(this, referenceExpr);
}
@CheckReturnValue
public PushCmd push() {
return new PushCmd(this, /*url=*/null, ImmutableList.of(), /*prune=*/false);
}
/**
* Runs a git ls-remote from the current directory for a repository url. Assumes the path to the
* git binary is already set. You don't have to be in a git repository to run this command. Does
* not work with remote names.
*
* @param url - see <repository> in git help ls-remote
* @param refs - see <refs> in git help ls-remote
* @param gitEnv - determines where the Git binaries are
* @param maxLogLines - Limit log lines to the number specified. -1 for unlimited
* @return - a map of refs to sha1 from the git ls-remote output.
*/
public static Map<String, String> lsRemote(
String url, Collection<String> refs, GitEnvironment gitEnv, int maxLogLines)
throws RepoException {
ImmutableMap.Builder<String, String> result = ImmutableMap.builder();
List<String> args;
try {
args = Lists.newArrayList("ls-remote", validateUrl(url));
} catch (ValidationException e) {
throw new RepoException("Invalid url: " + url, e);
}
args.addAll(refs);
CommandOutputWithStatus output;
try {
output = executeGit(FileSystems.getDefault().getPath("."), args, gitEnv, false, maxLogLines);
} catch (BadExitStatusWithOutputException e) {
throw new RepoException(
String.format("Error running ls-remote for '%s' and refs '%s': Exit code %s, Output:\n%s",
url, refs, e.getOutput().getTerminationStatus().getExitCode(),
e.getOutput().getStderr()), e);
} catch (CommandException e) {
throw new RepoException(
String.format("Error running ls-remote for '%s' and refs '%s'", url, refs), e);
}
if (output.getTerminationStatus().success()) {
for (String line : Splitter.on('\n').split(output.getStdout())) {
if (line.isEmpty()) {
continue;
}
Matcher matcher = LS_REMOTE_OUTPUT_LINE.matcher(line);
if (!matcher.matches()) {
throw new RepoException("Unexpected format for ls-remote output: " + line);
}
result.put(matcher.group(2), matcher.group(1));
}
}
return result.build();
}
/**
* Same as {@link #lsRemote(String, Collection, GitEnvironment, int)} but using this repository
* environment
*/
public Map<String, String> lsRemote(String url, Collection<String> refs) throws RepoException {
return lsRemote(url, refs, gitEnv, /*maxlogLines*/ -1);
}
@CheckReturnValue
static String validateUrl(String url) throws RepoException, ValidationException {
RepositoryUtil.validateNotHttp(url);
if (FULL_URI.matcher(url).matches()) {
return url;
}
// Support local folders
if (Files.isDirectory(Paths.get(url))) {
return url;
}
throw new RepoException(String.format("URL '%s' is not valid", url));
}
/**
* Execute show-ref git command in the local repository and returns a map from reference name to
* GitReference(SHA-1).
*/
private ImmutableMap<String, GitRevision> showRef(Iterable<String> refs)
throws RepoException {
ImmutableMap.Builder<String, GitRevision> result = ImmutableMap.builder();
CommandOutput commandOutput = gitAllowNonZeroExit(NO_INPUT,
ImmutableList.<String>builder().add("show-ref").addAll(refs).build(),
DEFAULT_TIMEOUT);
if (!commandOutput.getStderr().isEmpty()) {
throw new RepoException(String.format(
"Error executing show-ref on %s git repo:\n%s", getGitDir(), commandOutput.getStderr()));
}
for (String line : Splitter.on('\n').split(commandOutput.getStdout())) {
if (line.isEmpty()) {
continue;
}
List<String> strings = Splitter.on(' ').splitToList(line);
Preconditions.checkState(strings.size() == 2
&& SHA1_PATTERN.matcher(strings.get(0)).matches(), "Cannot parse line: '%s'", line);
// Ref -> SHA1
result.put(strings.get(1), new GitRevision(this, strings.get(0)));
}
return result.build();
}
/**
* Execute show-ref git command in the local repository and returns a map from reference name to
* GitReference(SHA-1).
*/
ImmutableMap<String, GitRevision> showRef() throws RepoException {
return showRef(ImmutableList.of());
}
String mergeBase(String commit1, String commit2) throws RepoException {
return simpleCommand("merge-base", commit1, commit2).getStdout().trim();
}
boolean isAncestor(String ancestor, String commit) throws RepoException {
CommandOutputWithStatus result =
gitAllowNonZeroExit(
NO_INPUT,
ImmutableList.of("merge-base", "--is-ancestor", "--", ancestor, commit),
DEFAULT_TIMEOUT);
if (result.getTerminationStatus().success()) {
return true;
}
if (result.getTerminationStatus().getExitCode() == 1) {
return false;
}
throw new RepoException("Error executing git merge-base --is-ancestor:\n" + result.getStderr());
}
/**
* Returns an instance equivalent to this one but with a different work tree. This does not
* initialize or alter the given work tree.
*/
public GitRepository withWorkTree(Path newWorkTree) {
return new GitRepository(this.gitDir, newWorkTree, this.verbose, this.gitEnv, fetchTimeout);
}
/**
* The Git work tree - in a typical Git repo, this is the directory containing the {@code .git}
* directory. Returns {@code null} for bare repos.
*/
@Nullable
public Path getWorkTree() {
return workTree;
}
public Path getGitDir() {
return gitDir;
}
/**
* Can be overwriten to add custom behavior.
*/
protected String runPush(PushCmd pushCmd) throws RepoException, ValidationException {
List<String> cmd = Lists.newArrayList("push");
// This shows progress in the log if not attached to a terminal
cmd.add("--progress");
if (pushCmd.prune) {
cmd.add("--prune");
}
if (pushCmd.url != null) {
cmd.add(validateUrl(pushCmd.url));
for (Refspec refspec : pushCmd.refspecs) {
cmd.add(refspec.toString());
}
}
return simpleCommand(cmd.toArray(new String[0])).getStderr();
}
/**
* An add command bound to the repo that can be configured and then executed with {@link #run()}.
*/
public class AddCmd {
private final boolean force;
private final boolean all;
private final Iterable<String> files;
private AddCmd(boolean force, boolean all, Iterable<String> files) {
this.force = force;
this.all = all;
this.files = checkNotNull(files);
}
/** Force the add */
@CheckReturnValue
public AddCmd force() {
return new AddCmd(/*force=*/true, all, files);
}
/** Add all the unstagged files to the index */
@CheckReturnValue
public AddCmd all() {
Preconditions.checkState(Iterables.isEmpty(files), "'all' and passing files is incompatible");
return new AddCmd(force, /*all=*/true, files);
}
/** Configure the files to add to the index */
@CheckReturnValue
public AddCmd files(Iterable<String> files) {
Preconditions.checkState(!all, "'all' and passing files is incompatible");
return new AddCmd(force, /*all=*/false, files);
}
/** Configure the files to add to the index */
@CheckReturnValue
public AddCmd files(String... files) {
return files(ImmutableList.copyOf(files));
}
/** Run the git command */
public void run() throws RepoException {
List<String> params = Lists.newArrayList("add");
if (force) {
params.add("-f");
}
if (all) {
params.add("--all");
}
params.add("--");
Iterables.addAll(params, files);
git(getCwd(), addGitDirAndWorkTreeParams(params));
}
}
/**
* Create a git add command that can be configured before execution.
*/
@CheckReturnValue
public AddCmd add() {
return new AddCmd(/*force*/false, /*all*/false, /*files*/ImmutableSet.of());
}
/**
* Get a field from a configuration {@code configFile} relative to {@link #getWorkTree()}.
*
* <p>If {@code configFile} is null it uses configuration (local or global).
* TODO(malcon): Refactor this to work similar to LogCmd.
*/
@Nullable
private String getConfigField(String field, @Nullable String configFile) throws RepoException {
ImmutableList.Builder<String> params = ImmutableList.builder();
params.add("config");
if (configFile != null) {
params.add("-f", configFile);
}
params.add("--get");
params.add(field);
CommandOutputWithStatus out = gitAllowNonZeroExit(NO_INPUT, params.build(),
DEFAULT_TIMEOUT);
if (out.getTerminationStatus().success()) {
return out.getStdout().trim();
} else if (out.getTerminationStatus().getExitCode() == 1 && out.getStderr().isEmpty()) {
return null;
}
throw new RepoException("Error executing git config:\n" + out.getStderr());
}
/**
* Resolves a git reference to the SHA-1 reference
*/
public String parseRef(String ref) throws RepoException, CannotResolveRevisionException {
// Runs rev-list on the reference and remove the extra newline from the output.
CommandOutputWithStatus result = gitAllowNonZeroExit(
NO_INPUT, ImmutableList.of("rev-list", "-1", ref, "--"), DEFAULT_TIMEOUT);
if (!result.getTerminationStatus().success()) {
throw new CannotResolveRevisionException("Cannot find reference '" + ref + "'");
}
String sha1 = result.getStdout().trim();
Verify.verify(SHA1_PATTERN.matcher(sha1).matches(), "Should be resolved to a SHA-1: %s", sha1);
return sha1;
}
boolean refExists(String ref) throws RepoException {
try {
parseRef(ref);
return true;
} catch (CannotResolveRevisionException e) {
return false;
}
}
public void rebase(String newBaseline) throws RepoException, RebaseConflictException {
CommandOutputWithStatus output = gitAllowNonZeroExit(
NO_INPUT, ImmutableList.of("rebase", checkNotNull(newBaseline)),
DEFAULT_TIMEOUT);
if (output.getTerminationStatus().success()) {
return;
}
if (FAILED_REBASE.matcher(output.getStderr()).find()) {
throw new RebaseConflictException(
"Conflict detected while rebasing " + workTree + " to " + newBaseline
+ ". Git output was:\n" + output.getStdout());
}
throw new RepoException(output.getStderr());
}
/**
* Checks out the given ref in the repo, quietly and throwing away local changes.
*/
public CommandOutput forceCheckout(String ref) throws RepoException {
return simpleCommand("checkout", "-q", "-f", checkNotNull(ref));
}
// DateTimeFormatter.ISO_OFFSET_DATE_TIME might include subseconds, but Git's ISO8601 format does
// not deal with subseconds (see https://git-scm.com/docs/git-commit#git-commit-ISO8601).
// We still want to stick to the default ISO format in Git, but don't add the subseconds.
private static final DateTimeFormatter ISO_OFFSET_DATE_TIME_NO_SUBSECONDS =
DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ssZ");
// The effective bytes that can be used for command-line arguments is ~128k. Setting an arbitrary
// max for the description of 64k
private static final int ARBITRARY_MAX_ARG_SIZE = 64_000;
public void commit(String author, ZonedDateTime timestamp, String message)
throws RepoException, ValidationException {
commit(checkNotNull(author), /*amend=*/false, checkNotNull(timestamp), checkNotNull(message));
}
// TODO(malcon): Create a CommitCmd object builder
public void commit(@Nullable String author, boolean amend, @Nullable ZonedDateTime timestamp,
String message)
throws RepoException, ValidationException {
if (isEmptyStaging() && !amend) {
throw new EmptyChangeException("Migration of the revision resulted in an empty change. "
+ "Is the change already migrated?");
}
ImmutableList.Builder<String> params = ImmutableList.<String>builder().add("commit");
if (author != null) {
params.add("--author", author);
}
if (timestamp != null) {
params.add("--date", timestamp.format(ISO_OFFSET_DATE_TIME_NO_SUBSECONDS));
}
if (amend) {
params.add("--amend");
}
Path descriptionFile = null;
try {
if (message.getBytes(StandardCharsets.UTF_8).length > ARBITRARY_MAX_ARG_SIZE) {
descriptionFile = getCwd().resolve(UUID.randomUUID().toString() + ".desc");
Files.write(descriptionFile, message.getBytes(StandardCharsets.UTF_8));
params.add("-F", descriptionFile.toAbsolutePath().toString());
} else {
params.add("-m", message);
}
git(getCwd(), addGitDirAndWorkTreeParams(params.build()));
} catch (IOException e) {
throw new RepoException(
"Could not commit change: Failed to write file " + descriptionFile, e);
} finally {
try {
if (descriptionFile != null) {
Files.deleteIfExists(descriptionFile);
}
} catch (IOException e) {
logger.atWarning().log("Could not delete description file: %s", descriptionFile);
}
}
}
/**
* Check if staging is empty. That means that a commit would fail with EmptyCommitException.
*/
private boolean isEmptyStaging() throws RepoException {
CommandOutput status = simpleCommand("diff", "--staged", "--stat");
return status.getStdout().trim().isEmpty();
}
public List<StatusFile> status() throws RepoException {
CommandOutput output = git(getCwd(),
addGitDirAndWorkTreeParams(ImmutableList.of("status", "--porcelain")));
ImmutableList.Builder<StatusFile> builder = ImmutableList.builder();
for (String line : Splitter.on('\n').split(output.getStdout())) {
if (line.isEmpty()) {
continue;
}
// Format 'XY file (-> file)?'
List<String> split = Splitter.on(" -> ").limit(2).splitToList(line.substring(3));
String fileName;
String newFileName;
if (split.size() == 1) {
fileName = split.get(0);
newFileName = null;
} else {
fileName = split.get(0);
newFileName = split.get(1);
}
builder.add(
new StatusFile(fileName, newFileName, toStatusCode(line.charAt(0)),
toStatusCode(line.charAt(1))));
}
return builder.build();
}
private StatusCode toStatusCode(char c) {
return checkNotNull(CHAR_TO_STATUS_CODE.get(c),
"Cannot find status code for '%s'", c);
}
/**
* Find submodules information for the current repository.
*
* @param currentRemoteUrl remote url associated with the repository. It will be used to
* resolve relative URLs (for example: url = ../foo).
*/
Iterable<Submodule> listSubmodules(String currentRemoteUrl) throws RepoException {
ImmutableList.Builder<Submodule> result = ImmutableList.builder();
String rawOutput = simpleCommand("submodule--helper", "list").getStdout();
for (String line : Splitter.on('\n').split(rawOutput)) {
if (line.isEmpty()) {
continue;
}
List<String> fields = Splitter.on(SPACES).splitToList(line);
String submoduleName = fields.get(3);
if (Strings.isNullOrEmpty(submoduleName)) {
throw new RepoException("Empty submodule name for " + line);
}
String path = getSubmoduleField(submoduleName, "path");
if (path == null) {
throw new RepoException("Path is required for submodule " + submoduleName);
}
String url = getSubmoduleField(submoduleName, "url");
if (url == null) {
throw new RepoException("Url is required for submodule " + submoduleName);
}
String branch = getSubmoduleField(submoduleName, "branch");
if (branch == null) {
branch = "master";
} else if (branch.equals(".")) {
branch = "HEAD";
}
FileUtil.checkNormalizedRelative(path);
// If the url is relative, construct a url using the parent module remote url.
if (url.startsWith("../")) {
url = siblingUrl(currentRemoteUrl, submoduleName, url.substring(3));
} else if (url.startsWith("./")) {
url = siblingUrl(currentRemoteUrl, submoduleName, url.substring(2));
}
try {
result.add(new Submodule(validateUrl(url), submoduleName, branch, path));
} catch (ValidationException e) {
throw new RepoException("Invalid url: " + url, e);
}
}
return result.build();
}
ImmutableList<TreeElement> lsTree(GitRevision reference, String treeish)
throws RepoException {
ImmutableList.Builder<TreeElement> result = ImmutableList.builder();
String stdout = simpleCommand("ls-tree", reference.getSha1(), "--", treeish).getStdout();
for (String line : Splitter.on('\n').split(stdout)) {
if (line.isEmpty()) {
continue;
}
Matcher matcher = LS_TREE_ELEMENT.matcher(line);
if (!matcher.matches()) {
throw new RepoException("Unexpected format for ls-tree output: " + line);
}
// We ignore the mode for now
GitObjectType objectType = GitObjectType.valueOf(matcher.group(2).toUpperCase());
String sha1 = matcher.group(3);
String path = matcher.group(4)
// Per ls-tree documentation. Replace those escaped characters.
.replace("\\\\", "\\").replace("\\t", "\t").replace("\\n", "\n");
result.add(new TreeElement(objectType, sha1, path));
}
return result.build();
}
private String siblingUrl(String currentRemoteUrl, String submoduleName, String relativeUrl)
throws RepoException {
int idx = currentRemoteUrl.lastIndexOf('/');
if (idx == -1) {
throw new RepoException(String.format(
"Cannot find the parent url for '%s'. But git submodule '%s' is"
+ " configured with url '%s'", currentRemoteUrl, submoduleName, relativeUrl));
}
return currentRemoteUrl.substring(0, idx) + "/" + relativeUrl;
}
private String getSubmoduleField(String submoduleName, String field) throws RepoException {
return getConfigField("submodule." + submoduleName + "." + field, ".gitmodules");
}
private Path getCwd() {
return workTree != null ? workTree : gitDir;
}
private List<String> addGitDirAndWorkTreeParams(Iterable<String> argv) {
Preconditions.checkState(Files.isDirectory(gitDir),
"git repository dir '%s' doesn't exist or is not a directory", gitDir);
List<String> allArgv = Lists.newArrayList("--git-dir=" + gitDir);
if (workTree != null) {
allArgv.add("--work-tree=" + workTree);
}
Iterables.addAll(allArgv, argv);
return allArgv;
}
public GitRepository init() throws RepoException {
try {
Files.createDirectories(gitDir);
if (workTree != null) {
Files.createDirectories(workTree);
}
} catch (IOException e) {
throw new RepoException("Cannot create directories: " + e.getMessage(), e);
}
if (workTree != null && workTree.resolve(".git").equals(gitDir)) {
git(workTree, ImmutableList.of("init", "."));
} else {
git(gitDir, ImmutableList.of("init", "--bare"));
}
return this;
}
public GitRepository withCredentialHelper(String credentialHelper)
throws RepoException {
git(gitDir, ImmutableList.of("config", "--local", "credential.helper",
checkNotNull(credentialHelper)));
return this;
}
public UserPassword credentialFill(String url) throws RepoException, ValidationException {
return new GitCredential(gitEnv.resolveGitBinary(), Duration.ofMinutes(1), gitEnv)
.fill(gitDir, url);
}
/**
* Runs a {@code git} command with the {@code --git-dir} and (if non-bare) {@code --work-tree}
* args set, and returns the {@link CommandOutput} if the command execution was successful.
*
* <p>Git commands usually write to stdout, but occasionally they write to stderr. It's
* responsibility of the client to consume the output from the correct source.
*
* <p>WARNING: Please consider creating a higher level function instead of calling this method.
* At some point we will deprecate.
*
* @param argv the arguments to pass to {@code git}, starting with the sub-command name
*/
public CommandOutput simpleCommand(String... argv) throws RepoException {
return git(getCwd(), addGitDirAndWorkTreeParams(Arrays.asList(argv)));
}
CommandOutput simpleCommandNoRedirectOutput(String... argv) throws RepoException {
Iterable<String> params = addGitDirAndWorkTreeParams(Arrays.asList(argv));
try {
// Use maxLoglines 0 and verbose=false to avoid redirection
return executeGit(getCwd(), params, gitEnv, /*verbose*/ false, /*maxLoglines*/ 0);
} catch (BadExitStatusWithOutputException e) {
CommandOutputWithStatus output = e.getOutput();
for (Pattern error : REF_NOT_FOUND_ERRORS) {
Matcher matcher = error.matcher(output.getStderr());
if (matcher.find()) {
throw new RepoException(
"Cannot find reference '" + matcher.group(1) + "'");
}
}
throw throwUnknownGitError(output, params);
} catch (CommandException e) {
throw new RepoException("Error executing 'git': " + e.getMessage(), e);
}
}
/**
* Execute git apply.
*
* @param index if true we pass --index to the git command
* @throws RebaseConflictException if it cannot apply the change.
*/
public void apply(byte[] stdin, boolean index) throws RepoException, RebaseConflictException {
CommandOutputWithStatus output = gitAllowNonZeroExit(stdin,
index ? ImmutableList.of("apply", "--index") : ImmutableList.of("apply"),
DEFAULT_TIMEOUT);
if (output.getTerminationStatus().success()) {
return;
}
if (output.getTerminationStatus().getExitCode() == 1) {
throw new RebaseConflictException("Couldn't apply patch:\n" + output.getStderr());
}
throw new RepoException("Couldn't apply patch:\n" + output.getStderr());
}
/**
* Invokes {@code git} in the directory given by {@code cwd} against this repository and returns
* the {@link CommandOutput} if the command execution was successful.
*
* <p>Git commands usually write to stdout, but occasionally they write to stderr. It's
* responsibility of the client to consume the output from the correct source.
*
* @param cwd the directory in which to execute the command
* @param params the argv to pass to Git, excluding the initial {@code git}
*/
public CommandOutput git(Path cwd, String... params) throws RepoException {
return git(cwd, Arrays.asList(params));
}
/**
* Invokes {@code git} in the directory given by {@code cwd} against this repository and returns
* the {@link CommandOutput} if the command execution was successful.
*
* <p>Git commands usually write to stdout, but occasionally they write to stderr. It's
* responsibility of the client to consume the output from the correct source.
*
* <p>See also {@link #git(Path, String[])}.
*
* @param cwd the directory in which to execute the command
* @param params params the argv to pass to Git, excluding the initial {@code git}
*/
private CommandOutput git(Path cwd, Iterable<String> params) throws RepoException {
try {
return executeGit(cwd, params, gitEnv, verbose);
} catch (BadExitStatusWithOutputException e) {
CommandOutputWithStatus output = e.getOutput();
for (Pattern error : REF_NOT_FOUND_ERRORS) {
Matcher matcher = error.matcher(output.getStderr());
if (matcher.find()) {
throw new RepoException(
"Cannot find reference '" + matcher.group(1) + "'");
}
}
throw throwUnknownGitError(output, params);
} catch (CommandException e) {
throw new RepoException("Error executing 'git': " + e.getMessage(), e);
}
}
private RepoException throwUnknownGitError(
CommandOutputWithStatus output, Iterable<String> params) throws RepoException {
throw new RepoException(
String.format(
"Error executing 'git %s'(exit code %d). Stderr: %s\n",
Joiner.on(' ').join(params),
output.getTerminationStatus().getExitCode(),
output.getStderr()));
}
/**
* Execute git allowing non-zero exit codes. This will only allow program non-zero exit codes
* (0-10. The upper bound is arbitrary). And will still fail for exit codes like 127 (Command not
* found).
*/
private CommandOutputWithStatus gitAllowNonZeroExit(byte[] stdin, Iterable<String> params,
Duration defaultTimeout)
throws RepoException {
try {
List<String> allParams = new ArrayList<>();
allParams.add(gitEnv.resolveGitBinary());
allParams.addAll(addGitDirAndWorkTreeParams(params));
Command cmd =
new Command(
Iterables.toArray(allParams, String.class),