diff --git a/java/com/google/copybara/git/GitDestination.java b/java/com/google/copybara/git/GitDestination.java index 37d1c19ad..b5fa96f89 100644 --- a/java/com/google/copybara/git/GitDestination.java +++ b/java/com/google/copybara/git/GitDestination.java @@ -549,7 +549,11 @@ public ImmutableList write(TransformResult transformResult, console.progress("Git Destination: Adding all files"); try (ProfilerTask ignored = generalOptions.profiler().start("add_files")) { - alternate.add().force().all().run(); + if(generalOptions.isForced()){ + alternate.add().force().all().run(); + }else{ + alternate.add().force().renormalize().all().run(); + } } console.progress("Git Destination: Excluding files"); diff --git a/java/com/google/copybara/git/GitRepository.java b/java/com/google/copybara/git/GitRepository.java index ea2c6f831..14fcb765e 100644 --- a/java/com/google/copybara/git/GitRepository.java +++ b/java/com/google/copybara/git/GitRepository.java @@ -1004,13 +1004,16 @@ public void abortCherryPick() throws RepoException { public class AddCmd { private final boolean force; + private final boolean renormalize; private final boolean all; private final Iterable files; @Nullable private final String pathSpecFromFile; - private AddCmd(boolean force, boolean all, Iterable files, String pathSpecFromFile) { + + private AddCmd(boolean force, boolean all, Iterable files, String pathSpecFromFile, boolean renormalize) { this.force = force; this.all = all; + this.renormalize = renormalize; this.files = checkNotNull(files); this.pathSpecFromFile = pathSpecFromFile; } @@ -1018,7 +1021,13 @@ private AddCmd(boolean force, boolean all, Iterable files, String pathSp /** Force the add */ @CheckReturnValue public AddCmd force() { - return new AddCmd(/*force=*/ true, all, files, pathSpecFromFile); + return new AddCmd(/*force=*/ true, all, files, pathSpecFromFile, renormalize); + } + + /** Renormalize the add */ + @CheckReturnValue + public AddCmd renormalize() { + return new AddCmd(/*force=*/ true, all, files, pathSpecFromFile, true); } /** Add all the unstagged files to the index */ @@ -1027,7 +1036,7 @@ public AddCmd all() { Preconditions.checkState(Iterables.isEmpty(files), "'all' and passing files is incompatible"); Preconditions.checkState( pathSpecFromFile == null, "'all' and pathSpecFromFile is incompatible"); - return new AddCmd(force, /*all=*/ true, files, pathSpecFromFile); + return new AddCmd(force, /*all=*/ true, files, pathSpecFromFile, renormalize); } /** Configure the files to add to the index */ @@ -1036,7 +1045,7 @@ public AddCmd files(Iterable files) { Preconditions.checkState(!all, "'all' and passing files is incompatible"); Preconditions.checkState( pathSpecFromFile == null, "'pathSpecFromFile' and passing files is incompatible"); - return new AddCmd(force, /*all=*/ false, files, pathSpecFromFile); + return new AddCmd(force, /*all=*/ false, files, pathSpecFromFile, renormalize); } /** Configure the files to add to the index */ @@ -1045,7 +1054,7 @@ public AddCmd pathSpecFromFile(String pathSpecFromFile) { Preconditions.checkState(!all, "'pathSpecFromFile' and passing files is incompatible"); Preconditions.checkState( Iterables.isEmpty(files), "'pathSpecFromFile' and passing files is incompatible"); - return new AddCmd(force, /*all=*/ false, files, pathSpecFromFile); + return new AddCmd(force, /*all=*/ false, files, pathSpecFromFile, renormalize); } /** Configure the files to add to the index */ @@ -1063,7 +1072,9 @@ public void run() throws RepoException { if (all) { params.add("--all"); } - + if (renormalize) { + params.add("--renormalize"); + } if (pathSpecFromFile != null) { params.add("--pathspec-from-file=" + pathSpecFromFile); } @@ -1078,7 +1089,7 @@ public void run() throws RepoException { */ @CheckReturnValue public AddCmd add() { - return new AddCmd(/*force*/ false, /*all*/ false, /*files*/ ImmutableSet.of(), null); + return new AddCmd(/*force*/ false, /*all*/ false, /*files*/ ImmutableSet.of(), null, false); } /**