You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2022/11/04 12:37:05 UTC

[GitHub] [camel] orpiske commented on a diff in pull request #8661: [CAMEL-18646] Provide custom configuration

orpiske commented on code in PR #8661:
URL: https://github.com/apache/camel/pull/8661#discussion_r1013967297


##########
components/camel-git/src/main/docs/git-component.adoc:
##########
@@ -83,5 +83,17 @@ from("git:///tmp/testRepo?type=commit")
     .to(....)
 ---------------------------------------
 
+== Custom config file
+By default camel-git will load .gitconfig file from user home folder. You

Review Comment:
   Use quotes to force the formatting to recognize this as a file. For instance, to make it like this `.gitconfig`.



##########
components/camel-git/src/main/java/org/apache/camel/component/RepositoryFactory.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.camel.component;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.camel.component.git.GitEndpoint;
+import org.apache.camel.util.ObjectHelper;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+import org.eclipse.jgit.util.SystemReader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class RepositoryFactory {
+
+    private static final Logger LOG = LoggerFactory.getLogger(RepositoryFactory.class);
+    private static final SystemReader DEFAULT_INSTANCE;
+
+    static {
+        DEFAULT_INSTANCE = SystemReader.getInstance();
+    }
+
+    private RepositoryFactory() {
+    }
+
+    public static Repository of(GitEndpoint endpoint) throws IOException {
+        if (ObjectHelper.isNotEmpty(endpoint.getGitConfigFile())) {
+            return fromPathToConfigFile(endpoint, endpoint.getGitConfigFile());
+        }
+        return getRepository(endpoint, DEFAULT_INSTANCE);
+    }
+
+    private static Repository fromPathToConfigFile(GitEndpoint endpoint, String pathToConfigFile) throws IOException {
+        if (ObjectHelper.isEmpty(pathToConfigFile)) {
+            throw new IllegalArgumentException("Path to git config file must be supplied");
+        }
+        File gitConfigFile;
+        if (pathToConfigFile.startsWith("/")) { //load from system
+            gitConfigFile = new File(pathToConfigFile);
+        } else { //load from resources
+            gitConfigFile = new File(endpoint.getClass().getClassLoader().getResource(pathToConfigFile).getFile());
+        }
+        return getRepository(endpoint, new CustomConfigSystemReader(gitConfigFile));
+    }
+
+    private static Repository getRepository(GitEndpoint endpoint, SystemReader instance) throws IOException {
+        FileRepositoryBuilder builder = new FileRepositoryBuilder();
+        try {
+            SystemReader.setInstance(instance);
+            // scan environment GIT_* variables
+            return builder.setGitDir(new File(endpoint.getLocalPath(), ".git")).readEnvironment()
+                    .findGitDir() // scan up the file system tree
+                    .build();
+        } catch (IOException e) {
+            LOG.error("There was an error, cannot open {} repository", endpoint.getLocalPath());
+            throw e;

Review Comment:
   Maybe describe the error that caused the problem. Something like: `LOG.error("There was an error opening the {} repository: {}", endpoint.getLocalPath(), e.getMessage);` 
   
   Or ... just wrap the exception. 



##########
components/camel-git/src/main/java/org/apache/camel/component/RepositoryFactory.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.camel.component;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.camel.component.git.GitEndpoint;
+import org.apache.camel.util.ObjectHelper;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+import org.eclipse.jgit.util.SystemReader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class RepositoryFactory {
+
+    private static final Logger LOG = LoggerFactory.getLogger(RepositoryFactory.class);
+    private static final SystemReader DEFAULT_INSTANCE;
+
+    static {
+        DEFAULT_INSTANCE = SystemReader.getInstance();
+    }
+
+    private RepositoryFactory() {
+    }
+
+    public static Repository of(GitEndpoint endpoint) throws IOException {
+        if (ObjectHelper.isNotEmpty(endpoint.getGitConfigFile())) {
+            return fromPathToConfigFile(endpoint, endpoint.getGitConfigFile());
+        }
+        return getRepository(endpoint, DEFAULT_INSTANCE);
+    }
+
+    private static Repository fromPathToConfigFile(GitEndpoint endpoint, String pathToConfigFile) throws IOException {
+        if (ObjectHelper.isEmpty(pathToConfigFile)) {
+            throw new IllegalArgumentException("Path to git config file must be supplied");
+        }
+        File gitConfigFile;
+        if (pathToConfigFile.startsWith("/")) { //load from system

Review Comment:
   Wouldn't this break on Windows? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org