Claus Ibsen created CAMEL-27656:
-----------------------------------

             Summary: camel-git: global SystemReader, tag-vs-branch checkout, 
NPE, quadratic consumers, metadata
                 Key: CAMEL-27656
                 URL: https://issues.apache.org/jira/browse/CAMEL-27656
             Project: Camel
          Issue Type: Bug
          Components: camel-git
            Reporter: Claus Ibsen
             Fix For: 4.24.0


Found while improving the option descriptions of camel-git for CAMEL-23832 (PR 
(commit 
https://github.com/apache/camel/commit/edb1dee15d30e51d807ebff1256ac1d4bcf422d6)).
These are separate problems of the component; the descriptions commit only 
documents the current behavior.

h4. 1. gitConfigFile changes the JGit SystemReader of the whole JVM
{{RepositoryFactory.getRepository()}} calls {{SystemReader.setInstance()}}, 
which is a static, JVM-wide setting, and never restores it. After an endpoint 
with {{gitConfigFile}} starts, every JGit call in the JVM (other git endpoints, 
clones, commits) reads that config file. An endpoint without {{gitConfigFile}} 
that starts later sets the default reader back, also for the first endpoint. 
{{GitProducer.defineTargetBranchName()}} reads {{init.defaultBranch}} from 
{{SystemReader.getInstance()}}, so the merge target depends on which endpoint 
started last.
Proposed change: do not set the global instance; give the repository its own 
config (for example load the file as the base {{Config}} of the repository) and 
read {{init.defaultBranch}} from the repository config.

h4. 2. gitConfigFile with classpath: does not work inside a JAR, and http: 
leaves temp files
{{RepositoryFactory.resolveConfigFile()}} turns a {{classpath:}} resource into 
a {{File}} with {{URL.getFile()}}. For a resource inside a JAR this is a 
{{jar:...!/}} path that is not a file, and JGit's {{FileBasedConfig}} reads a 
missing file as an empty config, so the setting is silently ignored. A missing 
classpath resource fails with a NullPointerException. With {{http:}} or 
{{https:}}, {{getTempFileFromHttp()}} downloads the file to a new temp file at 
every producer or consumer start, and the file is never deleted.
Proposed change: read the resource with 
{{ResourceHelper.resolveMandatoryResourceAsInputStream()}} into a config 
({{Config.fromText()}}), with a clear error when it is missing, and no temp 
file.

h4. 3. Producer operations check out a tag instead of a branch with the same 
name
All producer operations except {{checkout}} (add, remove, commit, commitAll, 
status, log, push, pull, cherryPick, clean) call 
{{git.checkout().setName(branchName)}} with the short name. JGit's 
{{CheckoutCommand}} resolves the name with {{Repository.findRef()}}, which 
searches {{refs/tags/}} before {{refs/heads/}}. When a tag and a branch have 
the same name, the tag is checked out (detached HEAD), and a commit is then 
made on a detached HEAD instead of on the branch. {{doMerge()}} does the same 
for {{targetBranchName}}. {{doCheckout()}} was already fixed for this in 
CAMEL-26192.
Proposed change: check out {{Constants.R_HEADS + branchName}} in all these 
places, as {{doCheckout()}} does.

h4. 4. NullPointerException when no operation is given
{{GitProducer.process()}} does {{switch (operation)}}. When neither the 
{{operation}} option nor the {{CamelGitOperation}} header is set, {{operation}} 
is null and the switch throws a NullPointerException.
Proposed change: throw an IllegalArgumentException that says the operation 
option or the CamelGitOperation header must be set.

h4. 5. Commit consumer silently consumes all refs when branchName does not 
resolve
{{GitCommitConsumer.poll()}} uses {{log().add(id)}} only when 
{{repository.resolve(branchName)}} returns an id, and otherwise 
{{log().all()}}. A {{branchName}} with a typo, or of a branch that does not 
exist yet, makes the consumer deliver the commits of every branch and tag 
without a warning.
Proposed change: when {{branchName}} is set but does not resolve, log a WARN 
and deliver nothing (or fail at start), instead of falling back to all refs.

h4. 6. Consumers are quadratic in the number of commits, and their memory grows 
without bound
{{GitCommitConsumer}}, {{GitBranchConsumer}} and {{GitTagConsumer}} keep the 
consumed ids in an {{ArrayList}} and call {{contains()}} for every item of 
every poll. The commit consumer walks the whole history at every poll (500 ms 
by default), so a poll costs O(n²) for n commits; for a repository with tens of 
thousands of commits this is billions of comparisons per poll. The lists are 
never trimmed.
Proposed change: use a {{HashSet}} (ObjectId has a proper hashCode), and stop 
the commit walk at the commits already consumed.

h4. 7. targetBranchName declares defaultValue master, but the field is null
{{GitEndpoint.targetBranchName}} has {{defaultValue = "master"}} in 
{{@UriParam}}, but the field has no initializer. The effective default is 
{{init.defaultBranch}} of the user git config, and only then {{master}} 
({{GitProducer.defineTargetBranchName()}}). Tools and the docs show {{master}} 
as the default.
Proposed change: remove the {{defaultValue}}, as the description already 
explains the fallback.

h4. 8. depth has no defaultValue
{{GitEndpoint.depth}} is an {{int}} with default 0 (full clone), but its 
{{@UriParam}} has no {{defaultValue}}.
Proposed change: add {{defaultValue = "0"}}.

_Claude Code on behalf of davsclaus_



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to