Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#757: Settings in code repository #850

Merged
merged 36 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
84a29cf
did a little bit of everything, javadoc and tests missing
salimbouch Dec 5, 2024
f73b3fe
added documentation
salimbouch Dec 5, 2024
6923f71
tiny changes
salimbouch Dec 5, 2024
0e09cbe
Merge branch 'main' into settings-in-code-repo
salimbouch Dec 5, 2024
d4a5590
Update GitContextMock.java
salimbouch Dec 5, 2024
11f372d
Merge branch 'settings-in-code-repo' of https://github.com/salimbouch…
salimbouch Dec 5, 2024
5eb994f
Update GitContextMock.java
salimbouch Dec 5, 2024
cb46e0e
review changes
salimbouch Dec 9, 2024
0284234
review changes
salimbouch Dec 9, 2024
d8e4e88
moved save commit Id method to IdeContext
salimbouch Dec 9, 2024
d67de02
review changes
salimbouch Dec 11, 2024
094836e
removed unused import
salimbouch Dec 11, 2024
7523af6
fixed nls tests
salimbouch Dec 12, 2024
5b266fe
fixed tests
salimbouch Dec 12, 2024
5de9a0f
fixed nls test
salimbouch Dec 12, 2024
a7b5ec2
Merge branch 'main' into settings-in-code-repo
salimbouch Dec 12, 2024
cbf6cdc
fixed 826
salimbouch Dec 12, 2024
446475f
imports
salimbouch Dec 12, 2024
d112979
Update AbstractIdeContext.java
salimbouch Dec 12, 2024
ff61a2b
added check so that tests dont break
salimbouch Dec 12, 2024
845c9d9
Merge branch 'main' into settings-in-code-repo
salimbouch Dec 12, 2024
1334373
Merge branch 'main' into settings-in-code-repo
salimbouch Dec 13, 2024
44ead0c
reviewed changes
salimbouch Dec 15, 2024
532e318
Merge branch 'main' into settings-in-code-repo
hohwille Dec 16, 2024
7b55a39
review changes
salimbouch Dec 16, 2024
5324365
review changes
salimbouch Dec 16, 2024
e465bae
more elaboration on --code usage
salimbouch Dec 16, 2024
5ea6d95
Merge branch 'main' into settings-in-code-repo
hohwille Dec 20, 2024
a1a7238
Update GitContextImpl.java: constructive review
hohwille Dec 20, 2024
af9e828
Merge branch 'main' of https://github.com/devonfw/ideasy into setting…
salimbouch Jan 5, 2025
0ddbef6
adjusted changelog
salimbouch Jan 5, 2025
79bbd25
Merge branch 'settings-in-code-repo' of https://github.com/salimbouch…
salimbouch Jan 5, 2025
165174c
added javadoc
salimbouch Jan 7, 2025
80ba24e
Merge branch 'main' into settings-in-code-repo
hohwille Jan 10, 2025
37ac585
Merge branch 'main' into settings-in-code-repo
hohwille Jan 14, 2025
065f174
Merge branch 'main' into settings-in-code-repo
hohwille Jan 14, 2025
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.devonfw.tools.ide.commandlet;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
Expand Down Expand Up @@ -115,6 +116,9 @@ private void updateSettings() {
// here we do not use pullOrClone to prevent asking a pointless question for repository URL...
if (Files.isDirectory(settingsPath) && !this.context.getFileAccess().isEmptyDir(settingsPath)) {
step = this.context.newStep("Pull settings repository");
if (!Files.isDirectory(settingsPath.resolve(GitContext.GIT_FOLDER))) {
settingsPath = settingsPath.toRealPath();
}
gitContext.pull(settingsPath);
} else {
step = this.context.newStep("Clone settings repository");
Expand All @@ -132,7 +136,10 @@ private void updateSettings() {
}
gitContext.pullOrClone(GitUrl.of(repository), settingsPath);
}
gitContext.saveCurrentCommitId(settingsPath);
step.success("Successfully updated settings repository.");
} catch (IOException e) {
throw new IllegalStateException("Could not update the settings repository.");
hohwille marked this conversation as resolved.
Show resolved Hide resolved
} finally {
if (step != null) {
step.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.devonfw.tools.ide.commandlet;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.git.GitUrl;
import com.devonfw.tools.ide.io.FileAccess;
import com.devonfw.tools.ide.property.BooleanProperty;
import com.devonfw.tools.ide.property.FlagProperty;
import com.devonfw.tools.ide.property.StringProperty;

Expand All @@ -18,6 +23,10 @@ public class CreateCommandlet extends AbstractUpdateCommandlet {
/** {@link FlagProperty} for skipping the setup of git repositories */
public final FlagProperty skipRepositories;

public final FlagProperty codeRepositoryFlag;

public final StringProperty codeRepository;

/**
* The constructor.
*
Expand All @@ -28,6 +37,8 @@ public CreateCommandlet(IdeContext context) {
super(context);
this.newProject = add(new StringProperty("", true, "project"));
this.skipRepositories = add(new FlagProperty("--skip-repositories"));
this.codeRepositoryFlag = add(new FlagProperty("--code"));
this.codeRepository = add(new StringProperty("", false, ""));
add(this.settingsRepo);
}

Expand Down Expand Up @@ -58,13 +69,40 @@ public void run() {

initializeProject(newProjectPath);
this.context.setIdeHome(newProjectPath);
if (codeRepositoryFlag.isTrue()) {
String repoUrl = codeRepository.getValue();
if (repoUrl.isEmpty()) {
this.context.info("Please provide a repository URL after using --code");
} else {
initializeCodeRepository(repoUrl);
}
}
super.run();
hohwille marked this conversation as resolved.
Show resolved Hide resolved

if (this.skipRepositories.isTrue()) {
this.context.info("Skipping the cloning of project repositories as specified by the user.");
} else {
updateRepositories();
}
this.context.success("Successfully created new project '{}'.", newProjectName);

}

private void initializeCodeRepository(String repoUrl) {

// clone the given repository into IDE_HOME/workspaces/main
GitUrl gitUrl = GitUrl.of(repoUrl);
Path codeRepoPath = this.context.getWorkspacePath().resolve(gitUrl.getProjectName());
this.context.getGitContext().pullOrClone(gitUrl, codeRepoPath);
hohwille marked this conversation as resolved.
Show resolved Hide resolved

// check for settings folder and create symlink to IDE_HOME/settings
Path settingsFolder = codeRepoPath.resolve(IdeContext.FOLDER_SETTINGS);
if (Files.exists(settingsFolder)) {
this.context.getFileAccess().symlink(settingsFolder, this.context.getSettingsPath());
}

// create a file in IDE_HOME with the current local commit id
this.context.getGitContext().saveCurrentCommitId(codeRepoPath);
}

private void initializeProject(Path newInstancePath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ private ValidationResult applyAndRun(CliArguments arguments, Commandlet cmd) {
if (this.settingsPath != null) {
if (getGitContext().isRepositoryUpdateAvailable(this.settingsPath) ||
(getGitContext().fetchIfNeeded(this.settingsPath) && getGitContext().isRepositoryUpdateAvailable(this.settingsPath))) {
interaction("Updates are available for the settings repository. If you want to pull the latest changes, call ide update.");
interaction("Updates are available for the settings repository. If you want to apply the latest changes, call \"ide update\"");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public interface IdeContext extends IdeStartContext {

/** Legacy folder name used as compatibility fallback if {@link #FOLDER_TEMPLATES} does not exist. */
String FOLDER_LEGACY_TEMPLATES = "devon";
String SETTINGS_COMMIT_ID = ".commit.id";

/**
* @return {@code true} if {@link #isOfflineMode() offline mode} is active or we are NOT {@link #isOnline() online}, {@code false} otherwise.
Expand Down
2 changes: 2 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/git/GitContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public interface GitContext {
*/
boolean isRepositoryUpdateAvailable(Path repository);

void saveCurrentCommitId(Path repository);

/**
* Attempts a git pull and reset if required.
*
Expand Down
35 changes: 32 additions & 3 deletions cli/src/main/java/com/devonfw/tools/ide/git/GitContextImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.devonfw.tools.ide.git;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -51,12 +52,40 @@ public boolean fetchIfNeeded(Path repository, String remote, String branch) {
public boolean isRepositoryUpdateAvailable(Path repository) {

verifyGitInstalled();
String localCommitId = runGitCommandAndGetSingleOutput("Failed to get the local commit id.", repository, "rev-parse", "HEAD");
Path commitIdFile = this.context.getIdeHome().resolve(IdeContext.SETTINGS_COMMIT_ID);
String trackedCommitId = null;
if (Files.exists(commitIdFile)) {
try {
trackedCommitId = Files.readString(commitIdFile);
} catch (IOException e) {
return false;
}
}

String remoteCommitId = runGitCommandAndGetSingleOutput("Failed to get the remote commit id.", repository, "rev-parse", "@{u}");
if ((localCommitId == null) || (remoteCommitId == null)) {
if ((trackedCommitId == null) || (remoteCommitId == null)) {
return false;
}
return !localCommitId.equals(remoteCommitId);
return !trackedCommitId.equals(remoteCommitId);
}

@Override
public void saveCurrentCommitId(Path repository) {

if (!Files.exists(repository.resolve(GitContext.GIT_FOLDER))) {
try {
repository = repository.toRealPath();
} catch (IOException e) {
throw new IllegalStateException("Couldn't resolve settings to real path.", e);
}
hohwille marked this conversation as resolved.
Show resolved Hide resolved
}
String currentCommitId = runGitCommandAndGetSingleOutput("Failed to get current commit id.", repository, "rev-parse", "HEAD");
Path commitIdFile = this.context.getIdeHome().resolve(IdeContext.SETTINGS_COMMIT_ID);
try {
Files.writeString(commitIdFile, currentCommitId);
} catch (IOException e) {
throw new RuntimeException("Failed to save commit ID", e);
}
}

@Override
Expand Down
15 changes: 15 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/git/GitUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ public String toString() {
return this.url + "#" + this.branch;
}

/**
* Extracts the project name from an git URL.
* For URLs like "https://github.com/devonfw/ide-urls.git" returns "ide-urls"
*
* @return the project name without ".git" extension
*/
public String getProjectName() {
String path = this.url.substring(this.url.indexOf("://") + 3);
if (path.endsWith(".git")) {
path = path.substring(0, path.length() - 4);
}
String[] parts = path.split("/");
return parts[parts.length - 1];
}

/**
* @param gitUrl the {@link #toString() string representation} of a {@link GitUrl}. May contain a branch name as {@code «url»#«branch»}.
* @return the parsed {@link GitUrl}.
Expand Down