Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cli/src/main/java/com/devonfw/tools/ide/git/GitContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ default void reset(Path repository, String branch) {

/**
* @param repository the {@link Path} to the folder where the git repository is located.
* @return the name of the default origin.
* @return the {@link List} with the names of all configured remotes (e.g. "origin"), or an empty {@link List} if the repository has no remote configured.
*/
String determineRemote(Path repository);
List<String> getRemotes(Path repository);

/**
* Saves the current git commit ID of a repository to a file given as an argument.
Expand Down
22 changes: 14 additions & 8 deletions cli/src/main/java/com/devonfw/tools/ide/git/GitContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ public void pullOrClone(GitUrl gitUrl, Path repository) {
Objects.requireNonNull(gitUrl);
if (Files.isDirectory(repository.resolve(GIT_FOLDER))) {
// checks for remotes
String remote = determineRemote(repository);
if (remote == null) {
if (getRemotes(repository).isEmpty()) {
String message = repository + " is a local git repository with no remote - if you did this for testing, you may continue...\n"
+ "Do you want to ignore the problem and continue anyhow?";
this.context.askToContinue(message);
Expand Down Expand Up @@ -258,14 +257,16 @@ public void fetch(Path repository, String remote, String branch) {
if (branch == null) {
branch = determineCurrentBranch(repository);
}
if (remote == null) {
remote = determineRemote(repository);
if ((remote == null) && (branch != null)) {
// the remote to fetch from is the one the current branch is tracking (see also "git rev-parse @{u}")
remote = getOptionalGitConfigValue(repository, "branch." + branch + ".remote");
}
String effectiveRemote = Objects.requireNonNullElse(remote, "origin");

ProcessResult result = runGitCommand(repository, ProcessMode.DEFAULT_CAPTURE, "fetch", Objects.requireNonNullElse(remote, "origin"), branch);
ProcessResult result = runGitCommand(repository, ProcessMode.DEFAULT_CAPTURE, "fetch", effectiveRemote, branch);

if (!result.isSuccessful()) {
LOG.warn("Git fetch for '{}/{} failed.'.", remote, branch);
LOG.warn("Git fetch for '{}/{} failed.'.", effectiveRemote, branch);
}
}

Expand All @@ -276,9 +277,14 @@ public String determineCurrentBranch(Path repository) {
}

@Override
public String determineRemote(Path repository) {
public List<String> getRemotes(Path repository) {

return runGitCommandAndGetSingleOutput("Failed to determine current origin of git repository.", repository, "remote");
ProcessResult result = runGitCommand(repository, ProcessMode.DEFAULT_CAPTURE, ProcessErrorHandling.NONE, "remote");
if (!result.isSuccessful()) {
LOG.warn("Failed to determine the remotes of git repository {}.", repository);
return List.of();
}
return result.getOut();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public void clone(GitUrl gitUrl, Path repository) {
}

@Override
public String determineRemote(Path repository) {
public List<String> getRemotes(Path repository) {

return DEFAULT_REMOTE;
return List.of(DEFAULT_REMOTE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ public String determineCurrentBranch(Path repository) {
}

@Override
public String determineRemote(Path repository) {
return DEFAULT_REMOTE;
public List<String> getRemotes(Path repository) {
return List.of(DEFAULT_REMOTE);
}

/**
Expand Down
43 changes: 41 additions & 2 deletions cli/src/test/java/com/devonfw/tools/ide/git/GitContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,45 @@ void testRunGitPullWithoutForce(@TempDir Path tempDir) {
assertThat(tempDir.resolve(GitContext.GIT_FOLDER).resolve("update")).hasContent(this.processContext.getNow().toString());
}

/**
* Runs a simulated git pull on a repository with multiple remotes configured and checks that the pull is performed instead of asking the user to continue.
* See <a href="https://github.com/devonfw/IDEasy/issues/840">issue #840</a>.
*
* @param tempDir a {@link TempDir} {@link Path}.
*/
@Test
void testRunGitPullWithMultipleRemotes(@TempDir Path tempDir) {

// arrange
String gitRepoUrl = "https://github.com/test";
IdeTestContext context = newGitContext(tempDir);
this.processContext.addOutputMessage(new OutputMessage(false, "origin"));
this.processContext.addOutputMessage(new OutputMessage(false, "upstream"));
FileAccess fileAccess = new FileAccessImpl(context);
fileAccess.mkdirs(tempDir.resolve(GitContext.GIT_FOLDER));
// act
context.getGitContext().pullOrClone(GitUrl.of(gitRepoUrl), tempDir);
// assert
assertThat(tempDir.resolve(GitContext.GIT_FOLDER).resolve("update")).hasContent(this.processContext.getNow().toString());
}

/**
* Runs a simulated git pull on a repository without any remote and checks that the user is asked whether to continue.
*
* @param tempDir a {@link TempDir} {@link Path}.
*/
@Test
void testRunGitPullWithoutRemoteAsksToContinue(@TempDir Path tempDir) {

// arrange
String gitRepoUrl = "https://github.com/test";
IdeTestContext context = newGitContext(tempDir);
FileAccess fileAccess = new FileAccessImpl(context);
fileAccess.mkdirs(tempDir.resolve(GitContext.GIT_FOLDER));
// act + assert (no answers are configured, so asking a question fails)
assertThrows(IllegalStateException.class, () -> context.getGitContext().pullOrClone(GitUrl.of(gitRepoUrl), tempDir));
}

/**
* Runs a git pull with force mode, creates temporary files to simulate a proper cleanup.
*
Expand Down Expand Up @@ -186,7 +225,7 @@ void testRunGitPullWithForceStartsCleanup(@TempDir Path tempDir) {
@Test
void testGitRepoIsRecognizedCorrectly(@TempDir Path tempDir) {
String gitRepoUrl = "https://github.com/test";

IdeTestContext context = newGitContext(tempDir);
GitContext gitContext = context.getGitContext();

Expand All @@ -199,7 +238,7 @@ void testGitRepoIsRecognizedCorrectly(@TempDir Path tempDir) {
void testNormalDirIsNoRepo(@TempDir Path tempDir) {
IdeTestContext context = newGitContext(tempDir);
GitContext gitContext = context.getGitContext();

FileAccess fileAccess = context.getFileAccess();
fileAccess.mkdirs(tempDir.resolve("new-folder"));

Expand Down