Commit Graph

5820 Commits

Author SHA1 Message Date
Nitzan Gur-Furman 903645835b PatchApplier: Check for existence of src/dest files before any operation
Change-Id: Ia3ec0ce1af65114b48669157a934f70f1e22fd37
Bug: Google b/271474227
2023-03-31 06:22:29 -04:00
Martin Fick c5617711a1 Revert "RefDirectory: Throw exception if CAS of packed ref list fails"
This reverts commit 9c33f7364d.

Reason for revert: This change was based on the false claim that the
packedrefs file lock is held while the CAS is being done, but it is
actually released before the CAS (the in memory lock is still held,
however that does not prevent external actors from updating the
packedrefs files and then another thread from subsequently re-reading it
and updating the in memory packedRefList). Although reverting this
change can cause the CAS to fail, it should not actually matter since
the failure would indicate that another thread has already updated the
in memory packedRefList to either the same version this thread was
trying to update it too, or to a more recent version. Either way,
failing the CAS is then appropriate and should not be problematic.

Although this change reverts the code in the RefDirectory class, it
keeps the "improvements" to the test so that it continues to pass
reliably. The reason for the quotes around the word "improvements" is
because I believe the test alteration actually dramatically changes the
intent of the test, and that the original intent of the test is
untestable with the GC and RefDirectory classes as is.

Change-Id: I3acee7527bb542996dcdfaddfb2bdb45ec444db5
Signed-off-by: Martin Fick <quic_mfick@quicinc.com>
2023-03-30 18:04:16 -04:00
Kaushik Lingarkar 5ae8d28faa RefDirectory.delete: Prevent failures when packed-refs is outdated
The in-memory copy of packed refs might be outdated by the time the
packed-refs lock is acquired, so ensure the one read from disk is
used after acquiring the lock to prevent commit packed-refs from
throwing an exception. As a side-effect, since this updates the
in-memory copy of packed-refs when it is re-read from disk, it can
prevent other callers needing to re-read if it had changed.

Change-Id: I724c866b330b397e955b5eb04b259eedd9911e93
Signed-off-by: Kaushik Lingarkar <quic_kaushikl@quicinc.com>
2023-03-30 22:32:50 +02:00
Kaushik Lingarkar 4f7627be24 RefDirectory.pack: Only rely on packed refs from disk
Since packed-refs is read from disk anyway, don't rely on the
in-memory copy as that is racy and if outdated, could result in
commit of pack-refs throwing an exception. This change also avoids
a possible unnecessary double read of packed-refs from disk.

Change-Id: I684a64991f53f8bdad58bbd248aae6522d11267d
Signed-off-by: Kaushik Lingarkar <quic_kaushikl@quicinc.com>
2023-03-30 22:32:50 +02:00
Kaushik Lingarkar 4652dd956e RefDirectory: Make pack() and commitPackRefs() void
There are no more callers (since Iae71cb3) of these methods that need
the returned value. These methods should not have been returning
anything in the first place as that can introduce bugs such as the
one described in Iae71cb3.

Change-Id: I1d083a91603da803a106cfb1506925a82c2ef809
Signed-off-by: Kaushik Lingarkar <quic_kaushikl@quicinc.com>
2023-03-30 22:32:50 +02:00
Kaushik Lingarkar 33c00f3347 Implement a snapshotting RefDirectory for use in request scope
Introduce a SnapshottingRefDirectory class which allows users to get
a snapshot of the ref database and use it in a request scope (for
example a Gerrit query) instead of having to re-read packed-refs
several times in a request.

This can potentially be further improved to avoid scanning/reading a
loose ref several times in a request. This would especially help
repeated lookups of a packed ref, where we check for the existence of
a loose ref each time.

Change-Id: I634b92877f819f8bf36a3b9586bbc1815108189a
Signed-off-by: Kaushik Lingarkar <quic_kaushikl@quicinc.com>
2023-03-30 22:25:53 +02:00
Kaushik Lingarkar 47f2f3613c PackedBatchRefUpdate: Ensure updates are applied on latest packed refs
In the window between refs being packed (via refDb.pack) and obtaining
updates (via applyUpdates), packed-refs may have been updated by another
actor and relying on the previously read contents may lead to losing the
updates done by the other actor. To help avoid this, read packed-refs
from disk to ensure we have the latest copy after it is locked and
before committing updates to it.

Bug: 581641
Change-Id: Iae71cb30830b307d0df929c9131911ee476c711c
Signed-off-by: Kaushik Lingarkar <quic_kaushikl@quicinc.com>
2023-03-30 22:25:52 +02:00
Thomas Wolf d33b9ab5d3 PatchApplier: missing @since, and minor formatting
Change-Id: I561ca2f522579571b29d3e6f35f24e201d1c1663
Signed-off-by: Thomas Wolf <twolf@apache.org>
2023-03-29 21:06:11 +02:00
Ronald Bhuleskar 738dacb7fb BasePackFetchConnection: support negotiationTip feature
By default, Git will report, to the server, commits reachable from all local refs to find common commits in an attempt to reduce the size of the to-be-received packfile. If specified with negotiation tip, Git will only report commits reachable from the given tips. This is useful to speed up fetches when the user knows which local ref is likely to have commits in common with the upstream ref being fetched.

When negotation-tip is on, use the wanted refs instead of all refs as source of the "have" list to send.

This is controlled by the `fetch.usenegotationtip` flag, false by default. This works only for programmatic fetches and there is no support for it yet in the CLI.

Change-Id: I19f8fe48889bfe0ece7cdf78019b678ede5c6a32
2023-03-28 14:29:54 -07:00
Matthias Sohn 23b9693a75 DirCache: support option index.skipHash
Support the new option index.skipHash which was introduced in git 2.40
[1]. If it is set to true skip computing the git index checksum. This
accelerates Git commands that manipulate the index, such as git add, git
commit, or git status. Instead of storing the checksum, write a trailing
set of bytes with value zero, indicating that the computation was
skipped.

Accept a skipped checksum consisting of 20 null bytes when reading the
index since the option could have been set to true at the time when the
index was written.

[1] https://git-scm.com/docs/git-config#Documentation/git-config.txt-indexskipHash

Bug: 581723
Change-Id: I28ebe44c5ca1cbcb882438665d686452a0c111b2
2023-03-28 23:16:08 +02:00
Han-Wen NIenhuys 5166ded098 Merge "Fix PatchApplier error handling." 2023-03-28 05:51:18 -04:00
Nitzan Gur-Furman 3a913a8c34 Fix PatchApplier error handling.
1. For general errors, throw IOException instead of wrapping them with
PatchApplyException. The wrapping was moved (back) to ApplyCommand.
2. For file specific errors, log the errors as part of
PatchApplier::Result.
3. Change applyPatch() to receive the parsed Patch object, so the caller
can decide how to handle parsing errors.

Background: this utility class was extracted from ApplyCommand on V6.4.0.
During the extraction, we left the exception wrapping by
PatchApplyException intact. This attitude made it harder for the callers to
distinguish between the actual error causes.

Change-Id: Ib0f2b5e97a13df2339d8b65f2fea1c819c161ac3
2023-03-28 11:18:08 +02:00
kylezhao 827849017d Ensure FileCommitGraph scans commit-graph file if it already exists
When commit-graph file already exists in the repository, a newly
created FileCommitGraph didn't scan CommitGraph until the file was
modified, resulting in wrong result.

Change-Id: Ic85676f2d3b6a88f3ae28d4065729926b6fb2f23
Signed-off-by: kylezhao <kylezhao@tencent.com>
2023-03-27 10:51:07 +02:00
Matthias Sohn 67fcf76b4b Merge branch 'stable-6.4' into stable-6.5
* stable-6.4:
  GC: Close File.lines stream

Change-Id: I7e3a4b3671e779fd62062c4e10d224f432e39b54
2023-03-23 09:07:33 +01:00
Matthias Sohn cd2dc85f31 Merge branch 'stable-6.3' into stable-6.4
* stable-6.3:
  GC: Close File.lines stream

Change-Id: I99455916d447f5dffed85e9a5c1d51b323f07a16
2023-03-23 09:07:09 +01:00
Matthias Sohn 137efda0ba Merge branch 'stable-6.2' into stable-6.3
* stable-6.2:
  GC: Close File.lines stream

Change-Id: Id93b1933a5ce1ede9eb388c9fd54a4b3749694bf
2023-03-23 09:06:43 +01:00
Matthias Sohn b118e7b4c4 Merge branch 'stable-6.1' into stable-6.2
* stable-6.1:
  GC: Close File.lines stream

Change-Id: Ia2be0b05ed860125a388b01d6c291832f08dd990
2023-03-23 09:06:16 +01:00
Matthias Sohn 5b16c8ae15 Merge branch 'stable-6.0' into stable-6.1
* stable-6.0:
  GC: Close File.lines stream

Change-Id: I2f9e6da5584a40bb4b4efed0b87ae456f119d757
2023-03-23 09:05:42 +01:00
Matthias Sohn 55164c43b9 Merge branch 'stable-5.13' into stable-6.0
* stable-5.13:
  GC: Close File.lines stream

Change-Id: Ib473750e5a3ad3d74b0cb41f25052890f50a975c
2023-03-23 09:04:50 +01:00
Xing Huang 3212c8fa38 GC: Close File.lines stream
From File#lines javadoc: The returned stream from File Lines
encapsulates a Reader. If timely disposal of file system resources is
required, the try-with-resources construct should be used to ensure
that the stream's close method is
invoked after the stream operations are completed.

Wrap File.lines with try-with-resources.

Change-Id: I82c6faa3ef1083f6c7e964f96e9540b4db18eee8
Signed-off-by: Xing Huang <xingkhuang@google.com>
(cherry picked from commit 172a207945)
2023-03-23 08:19:26 +01:00
Matthias Sohn 1d073e30d7 [errorprone] Suppress [Finally] warnings
In these cases we use Throwable#addSuppressed to ensure the exception
thrown in the catch block preceding the finally block throwing another
exception isn't lost.

Change-Id: I96e78a5c15238ab77ac90ca1901850ce19acfcd8
2023-03-02 23:33:27 +01:00
Pavel Salamon 0518a6b0c1 Change config pull.rebase=preserve to pull.rebase=merges
The native git option to preserve merge commits during rebase
has been changed from pull.rebase=preserve to pull.rebase=merges.

This changeset in jgit makes the same config change. The old "preserve"
option is no longer recognized and is replaced by new option called
"merges".

This makes jgit's rebase configuration compatible with native git
versions 2.34 and newer where the old "preserve" option has been
removed.

Change-Id: Ic07ff954e258115e76465a1593ef3259f4c418a3
2023-02-28 23:44:41 +01:00
Matthias Sohn 2d0b908048 BatchingProgressMonitor: expose time spent per task
Display elapsed time per task if enabled via
ProgressMonitor#showDuration or if system property or environment
variable GIT_TRACE_PERFORMANCE is set to "true". If both the system
property and the environment variable are set the system property takes
precedence.

E.g. using jgit CLI:

$ GIT_TRACE_PERFORMANCE=true jgit clone https://foo.bar/foobar
Cloning into 'foobar'...
remote: Counting objects: 1 [0.002s]
remote: Finding sources: 100% (15531/15531) [0.006s]
Receiving objects:      100% (169737/169737) [13.045s]
Resolving deltas:       100% (67579/67579) [1.842s]

Change-Id: I4d624e7858b286aeddbe7d4e557589986d73659e
2023-02-27 16:41:33 -05:00
Ivan Frade ca2c57b2ec PackWriter: offer to write an object-size index for the pack
PackWriter callers tell the writer what do the want to include in the
pack and invoke #writePack(). Afterwards, they can invoke #writeIndex()
to write the corresponding pack index.

Mirror this for the object-size index, adding a #writeObjectSizeIndex()
method.

Change-Id: Ic319975c72c239cd6488303f7d4cced797e6fe00
2023-02-24 12:56:33 -08:00
Matthias Sohn cfacc43b52 Fix formatting in GC#doGc
Change-Id: Ifa3adb66d4e0404bab4036d6b165d6c4dafe921a
2023-02-24 15:18:39 +01:00
Ivan Frade ad07196d60 PackExt: Define new extension for the object size index
Change-Id: I6bbaf43b4e6fb456ca0e9e0c6efcfeded0f94d6d
2023-02-23 09:32:20 -08:00
Matthias Sohn 176f17d05e Merge branch 'stable-6.4'
* stable-6.4:
  If tryLock fails to get the lock another gc has it
  Fix GcConcurrentTest#testInterruptGc
  Don't swallow IOException in GC.PidLock#lock
  Check if FileLock is valid before using or releasing it

Change-Id: Ia2797b44a60342eb9df53f0b3d674cba92a512fc
2023-02-22 21:06:41 +01:00
Matthias Sohn f4eda3360a Merge branch 'stable-6.3' into stable-6.4
* stable-6.3:
  If tryLock fails to get the lock another gc has it
  Fix GcConcurrentTest#testInterruptGc
  Don't swallow IOException in GC.PidLock#lock
  Check if FileLock is valid before using or releasing it

Change-Id: I5af34c92e423a651db53b4dc45ed844d5f39910d
2023-02-22 21:05:55 +01:00
Matthias Sohn 636f377e4e Merge branch 'stable-6.2' into stable-6.3
* stable-6.2:
  If tryLock fails to get the lock another gc has it
  Fix GcConcurrentTest#testInterruptGc
  Don't swallow IOException in GC.PidLock#lock
  Check if FileLock is valid before using or releasing it

Change-Id: I5b6b10622b61fde3f0f10455a74ae159a0b69082
2023-02-22 21:03:52 +01:00
Matthias Sohn 6cc741aa23 Merge branch 'stable-6.1' into stable-6.2
* stable-6.1:
  If tryLock fails to get the lock another gc has it
  Fix GcConcurrentTest#testInterruptGc
  Don't swallow IOException in GC.PidLock#lock
  Check if FileLock is valid before using or releasing it

Change-Id: I3ffe92566cc145053bb762f612dd96bc6d542c62
2023-02-22 21:03:22 +01:00
Matthias Sohn b526829fba Merge branch 'stable-6.0' into stable-6.1
* stable-6.0:
  If tryLock fails to get the lock another gc has it
  Fix GcConcurrentTest#testInterruptGc
  Don't swallow IOException in GC.PidLock#lock
  Check if FileLock is valid before using or releasing it

Change-Id: Idea23e555c024557d7e39a86efe25f609400b962
2023-02-22 21:02:47 +01:00
Matthias Sohn 238f1693f7 Merge branch 'stable-5.13' into stable-6.0
* stable-5.13:
  If tryLock fails to get the lock another gc has it
  Fix GcConcurrentTest#testInterruptGc
  Don't swallow IOException in GC.PidLock#lock
  Check if FileLock is valid before using or releasing it

Change-Id: I708d0936fa86b028e4da4e7e21f332f8b48ad293
2023-02-22 21:02:09 +01:00
Matthias Sohn d9f75e8bb2 If tryLock fails to get the lock another gc has it
Change-Id: Ifd3bbcc5e0591883b774d23256949a83010ea134
2023-02-22 20:38:43 +01:00
Matthias Sohn 49f5273867 Don't swallow IOException in GC.PidLock#lock
This broke the test GcConcurrentTest#testInterruptGc which expects
ClosedByInterruptException when the thread doing gc is interrupted.

Change-Id: I89e02fc37aceeccb04c20cfc5b71cb8fa21793d6
2023-02-22 19:27:30 +01:00
Matthias Sohn a6da439b47 Check if FileLock is valid before using or releasing it
Change-Id: I23ba67b61b9b03772f33a929c080c0d02b8c8652
2023-02-22 02:56:06 +01:00
Matthias Sohn e92212a5a0 Merge branch 'stable-6.4'
* stable-6.4:
  Use Java 11 ProcessHandle to get pid of the current process
  Acquire file lock "gc.pid" before running gc
  Silence API errors introduced by 9424052f

Change-Id: Ifa4e56b6ecca9305f3f1685e45450019bfc82e22
2023-02-22 01:29:32 +01:00
Matthias Sohn dcd6367391 Merge branch 'stable-6.3' into stable-6.4
* stable-6.3:
  Use Java 11 ProcessHandle to get pid of the current process
  Acquire file lock "gc.pid" before running gc
  Silence API errors introduced by 9424052f

Change-Id: Ic40dbab18616d8d9fe3820b9890c86652b80eb47
2023-02-22 01:28:27 +01:00
Matthias Sohn c70374e641 Merge branch 'stable-6.2' into stable-6.3
* stable-6.2:
  Use Java 11 ProcessHandle to get pid of the current process
  Acquire file lock "gc.pid" before running gc
  Silence API errors introduced by 9424052f

Change-Id: I53cf9675deac0b588048d8224216d2a7e8bd16ec
2023-02-22 01:27:50 +01:00
Matthias Sohn 628ca9bd6f Merge branch 'stable-6.1' into stable-6.2
* stable-6.1:
  Use Java 11 ProcessHandle to get pid of the current process
  Acquire file lock "gc.pid" before running gc
  Silence API errors introduced by 9424052f

Change-Id: I0562a4a224779ccf1e4cc1ff8f5a352e55ab220a
2023-02-22 01:27:16 +01:00
Matthias Sohn 4c111e59d0 Merge branch 'stable-6.0' into stable-6.1
* stable-6.0:
  Use Java 11 ProcessHandle to get pid of the current process
  Acquire file lock "gc.pid" before running gc
  Silence API errors introduced by 9424052f

Change-Id: Ib9a2419253ffcbc90874adbfdb8129fee3178210
2023-02-22 01:26:36 +01:00
Matthias Sohn 2a2a208fa1 Use Java 11 ProcessHandle to get pid of the current process
Change-Id: I790f218601c1d5e1b39c4101e3b2708e76b9d782
2023-02-22 01:06:06 +01:00
Matthias Sohn aa13d1daf5 Merge branch 'stable-5.13' into stable-6.0
* stable-5.13:
  Acquire file lock "gc.pid" before running gc
  Silence API errors introduced by 9424052f

Change-Id: Ibb5c46cb79377d2d2cd7d4586f31c86665d2851c
2023-02-22 01:00:26 +01:00
kylezhao d789fe2f4d UploadPack: use allow-any-sha1-in-want configuration
C git 2.11 supports setting the equivalent of RequestPolicy.ANY with
uploadpack.allowAnySHA1InWant[1]. Parse this into TransportConfig and
use it from UploadPack.

Add additional tests for [2] and this change.

We can execute "git clone --filter=blob:none --no-checkout" successfully
with config uploadPack.allowFilter is true. But when we checkout, the
git will fetch other missing objects required by the checkout(this is
why we need this config).

When both uploadPack.allowFilter and uploadPack.allowAnySHA1InWant are
true, jgit will support partial clone. If you are using an extremely
large monorepo, this feature can help. It allows users to work on an
incomplete repo which reduces disk usage.

[1] f8edeaa05d
[2] change Id39771a6e42d8082099acde11249306828a053c0

Bug: 573390
Change-Id: I8fe75f03bf1fea7c11e0d67c8637bd05dd1f9b89
Signed-off-by: kylezhao <kylezhao@tencent.com>
2023-02-21 09:11:21 +01:00
Matthias Sohn 8eee800fb1 Acquire file lock "gc.pid" before running gc
Git guards gc by locking a lock file "gc.pid" before starting execution.
The lock file contains the pid and hostname of the process holding the
lock. Git tries to kill the process holding that lock if the lock file
wasn't modified in the last 12 hours and was started from the same host.

Teach JGit to acquire this lock before running gc but skip execution if
another process already holds the lock. Killing the other process could
be undesired if it's a long running application.

If the lock file wasn't modified in the last 12 hours try to lock it and
run gc if locking succeeds.

Register a shutdown hook for the lock file to ensure it is cleaned up if
the process is gracefully killed.

Change-Id: I00b838dcbf4fb0d03863bf7a2cd86b743c6c6971
2023-02-21 00:18:33 +01:00
Matthias Sohn 5d5a0d5376 Externalize strings introduced in c9552aba
Change-Id: I81bb78344df61e6eb42622fcef6235d4da0ae052
2023-02-20 21:40:40 +01:00
Matthias Sohn fe64445c11 Merge branch 'stable-6.4'
* stable-6.4:
  Fix getPackedRefs to not throw NoSuchFileException
  Add pack options to preserve and prune old pack files
  Allow to perform PackedBatchRefUpdate without locking loose refs
  Document option "core.sha1Implementation" introduced in 59029aec

Change-Id: I36051c623fcd480aa80ed32b4e89f9bdd1b798e0
2023-02-20 21:29:30 +01:00
Matthias Sohn f8e6bcba48 Merge branch 'stable-6.3' into stable-6.4
* stable-6.3:
  Fix getPackedRefs to not throw NoSuchFileException
  Add pack options to preserve and prune old pack files
  Allow to perform PackedBatchRefUpdate without locking loose refs
  Document option "core.sha1Implementation" introduced in 59029aec

Change-Id: I1073098fb06eabafdb3c5e7fcf44d55b86a1b152
2023-02-20 21:01:38 +01:00
Matthias Sohn 6ea0e11869 Merge branch 'stable-6.2' into stable-6.3
* stable-6.2:
  Fix getPackedRefs to not throw NoSuchFileException
  Add pack options to preserve and prune old pack files
  Allow to perform PackedBatchRefUpdate without locking loose refs
  Document option "core.sha1Implementation" introduced in 59029aec

Change-Id: I765c7302ce84a6a9c28fdef29da2bfaa49477c6e
2023-02-20 20:59:14 +01:00
Ivan Frade 596c445af2 PackConfig: add entry for minimum size to index
The object size index can have up to #(blobs-in-repo) entries, taking
a relevant amount of memory. Let operators configure the threshold size
to include objects in the size index.

The index will include objects with size *at or above* this
value (with -1 for none). This is more effective for the
filter-by-size case.

Lowering the threshold adds more objects to the index. This improves
performance at the cost of memory/storage space. For the object-size
case, more calls will use the index instead of reading IO. For the
filter-by-size case, lower threshold means better granularity (if
ObjectReader#isSmallerThan is implemented based only on the index).

Change-Id: I6ccd9334adbbc2abf95fde51dbbfc85b8230ade0
2023-02-16 10:25:44 -08:00
Matthias Sohn d8155c137e Merge branch 'stable-6.1' into stable-6.2
* stable-6.1:
  Fix getPackedRefs to not throw NoSuchFileException
  Add pack options to preserve and prune old pack files
  Allow to perform PackedBatchRefUpdate without locking loose refs
  Document option "core.sha1Implementation" introduced in 59029aec

Change-Id: Id32683d5f506e082d39af269803bccee0280cc27
2023-02-16 16:59:56 +01:00
Matthias Sohn 07a9eb06ff Merge branch 'stable-6.0' into stable-6.1
* stable-6.0:
  Add pack options to preserve and prune old pack files
  Allow to perform PackedBatchRefUpdate without locking loose refs
  Document option "core.sha1Implementation" introduced in 59029aec

Change-Id: I876a38c2de8b7d5eaacd00e36b85599f88173221
2023-02-16 16:59:09 +01:00
Matthias Sohn c46eb91611 Merge branch 'stable-5.13' into stable-6.0
* stable-5.13:
  Add pack options to preserve and prune old pack files
  Allow to perform PackedBatchRefUpdate without locking loose refs
  Document option "core.sha1Implementation" introduced in 59029aec

Change-Id: I423f410578f5bbe178832b80fef8998a5372182c
2023-02-16 16:48:24 +01:00
Prudhvi Akhil Alahari 012cb77930 Fix getPackedRefs to not throw NoSuchFileException
Since Files.newInputStream is from java.nio package, it throws
java.nio.file.NoSuchFileException. This was missed in the change
I00da88e. Without this change, getPackedRefs fails with
NoSuchFileException when there is no packed-refs file in a project.

Change-Id: I93c202ddb73a0a5979af8e4d09e45f5645664b45
Signed-off-by: Prudhvi Akhil Alahari <quic_prudhvi@quicinc.com>
2023-02-16 16:44:12 +05:30
Ivan Frade c9552abaf3 PackObjectSizeIndex: interface and impl for the object-size index
Operations like "clone --filter=blob:limit=N" or the "object-info"
command need to read the size of the objects from the storage. An
index would provide those sizes at once rather than having to seek in
the packfile.

Introduce an interface for the Object-size index. This index returns
the inflated size of an object. Not all objects could be indexed (to
limit memory usage).

This implementation indexes only blobs (no trees, nor
commits) *above* certain size threshold (configurable). Lower
threshold adds more objects to the index, consumes more memory and
provides better performance. 0 means "all blobs" and -1 "disabled".

If we don't index everything, for the filter use case is more
efficient to index the biggest objects first: the set is small and
most objects are filtered by NOT being in the index. For the
object-size, the more objects in the index the better, regardless
their size. All together, it is more helpful to index above threshold.

Change-Id: I9ed608ac240677e199b90ca40d420bcad9231489
2023-02-14 11:50:29 -08:00
Ivan Frade 62d0e7be7c UInt24Array: Array of unsigned ints encoded in 3 bytes.
The object size index stores positions of objects in the main
index (when ordered by sha1). These positions are per-pack and usually
a pack has <16 million objects (there are exceptions but rather
rare). It could save some memory storing these positions in three bytes
instead of four. Note that these positions are sorted and always positive.

Implement a wrapper around a byte[] to access and search "ints" while
they are stored as unsigned 3 bytes.

Change-Id: Iaa26ce8e2272e706e35fe4cdb648fb6ca7591972
2023-02-14 10:19:12 -08:00
Ivan Frade 5b9ca7df42 PackIndex: expose the position of an object-id in the index
The primary index returns the offset in the pack for an
objectId. Internally it keeps the object-ids in lexicographical order,
but doesn't expose an API to find the position of an object-id in that
list. This is needed for the object-size index, that we want to store
as "position-in-idx, size".

Add a #findPosition(object-id) method to the PackIndex interface to
know where an object-id sits in the ordered list of ids in the pack.

Note that this index position is over the list of ordered object-ids,
while reverse-index position is over the list of objects in packed
order.

Change-Id: I89fa146599e347a26d3012d3477d7f5bbbda7ba4
2023-02-14 10:01:29 -08:00
Matthias Sohn 9424052f27 Add pack options to preserve and prune old pack files
Add the options
- pack.preserveOldPacks
- pack.prunePreserved

This allows to configure in git config if old packs should be preserved
during gc and pruned during the next gc.

The original implementation in 91132bb0 only allows to set these options
using the API.

Change-Id: I5b23ab4f317d12f5ccd234401419913e8263cc9a
2023-02-11 01:19:28 +01:00
Xing Huang df5b7959be DfsPackFile/DfsGC: Write commit graphs and expose in pack
JGit knows how to read/write commit graphs but the DFS stack is not
using it yet.

The DFS garbage collector generates a commit-graph with commits
reachable from any ref. The pack is stored as extra stream in the GC
pack. DfsPackFile mimicks how other indices are loaded storing the
reference in DFS cache.

Signed-off-by: Xing Huang <xingkhuang@google.com>
Change-Id: I3f94997377986d21a56b300d8358dd27be37f5de
2023-02-07 16:59:56 -05:00
Xing Huang eccae7cf0b ObjectReader: Allow getCommitGraph to throw IOException
ObjectReader#getCommitGraph doesn't report errors loading the
commit graph. The caller should be aware of the situation and
ultimately decide what to do.

Add IOException to ObjectReader#getCommitGraph signature. RevWalk
defaults to an empty commit-graph on IO errors.

Signed-off-by: Xing Huang <xingkhuang@google.com>
Change-Id: I38eeacff76c7f926b6dfb192d1e5916e40770024
2023-02-07 11:32:12 -05:00
Saša Živkov ed2cbd9e8a Allow to perform PackedBatchRefUpdate without locking loose refs
Add another newBatchUpdate method in the RefDirectory where we can
control if the created PackedBatchRefUpdate will lock the loose refs or
not.

This can be useful in cases when we run programs which have exclusive
access to a Git repository and we know that locking loose refs is
unnecessary and just a performance loss.

Change-Id: I7d0932eb1598a3871a2281b1a049021380234df9
(cherry picked from commit cb90ed0852)
2023-02-03 10:18:47 +01:00
Han-Wen NIenhuys a1fa0ee679 Merge "UploadPack: consume delimiter in object-info command" 2023-02-02 09:09:25 -05:00
Han-Wen NIenhuys f94ab7680c Merge "PatchApplier fix - init cache with provided tree" 2023-02-02 09:00:56 -05:00
Han-Wen Nienhuys 341116103e UploadPack: consume delimiter in object-info command
The 'size' packet line is an argument, so it
must be preceeded by a 0001 delimiter. See also git's
t5701-git-serve.sh test,

https://github.com/git/git/blob/8b8d9a2/t/t5701-git-serve.sh#L329

Without this fix, the server will choke on the delimiter line, saying
PackProtocolException: unexpected <empty string>

To test, I ran Gerrit locally with this fix

$ curl -X POST   -H 'git-protocol: version=2'   -H 'content-type:
application/x-git-upload-pack-request'   -H 'accept:
application/x-git-upload-pack-result'   --data
$'0018command=object-info\n00010009size\n0031oid
d38b1b92bdb2893eb4505667375563f2d6d4086b\n0000'
http://localhost:8080/git.git/git-upload-pack

=>

0008size0032d38b1b92bdb2893eb4505667375563f2d6d4086b 268590000


The same command completes identically on Gitlab (which supports the
object-info command)

$ curl -X POST   -H 'git-protocol: version=2'   -H 'content-type:
application/x-git-upload-pack-request'   -H 'accept:
application/x-git-upload-pack-result'   --data
$'0018command=object-info\n00010009size\n0031oid
d38b1b92bdb2893eb4505667375563f2d6d4086b\n0000'
https://gitlab.com/gitlab-org/git.git/git-upload-pack

=>

0008size0032d38b1b92bdb2893eb4505667375563f2d6d4086b 268590000

In this case, the blob is for the COPYING file in the Git source tree,
which is 26859 bytes long.

Change-Id: Ief4ce1eb9303a3b2479547d7950ef01c7c28f472
2023-02-02 08:47:35 -05:00
Nitzan Gur-Furman a399bd13b1 PatchApplier fix - init cache with provided tree
This change only affects inCore repositories.
Before this change, any file that wasn't part of the patch
wasn't read, and therefore wasn't part of the output tree.

Change-Id: I246ef957088f17aaf367143f7a0b3af0f8264ffb
Bug: Google b/267270348
2023-02-02 12:39:26 +01:00
Ivan Frade 8898d62dbc Merge "DfsReaderIoStats: Add Commit Graph fields into DfsReaderIoStats" 2023-02-01 18:06:56 -05:00
Matthias Sohn 8bd960bf2b Merge changes I343cc3cf,I9dedf61b
* changes:
  Avoid error-prone warning
  Fix unused exception error-prone warning
2023-02-01 16:52:37 -05:00
Han-Wen Nienhuys b30c75be40 Fix unused exception error-prone warning
Ignoring the exception seems intended in this case.

Change-Id: I9dedf61b9cb5a6ff39fb141dd5da19143f4f6978
2023-02-01 10:53:43 +01:00
Han-Wen Nienhuys 97e8b4cc71 UploadPack: advertise object-info command if enabled
Change-Id: Iad8e5b5f4fdd84bd275eb19ee0d01eb6986d79f2
2023-02-01 10:52:33 +01:00
Han-Wen NIenhuys 66b871b777 Merge "Move MemRefDatabase creation in a separate method." 2023-02-01 04:15:44 -05:00
Matthias Sohn 580cb13f21 Merge branch 'stable-6.4'
* stable-6.4:
  Shortcut during git fetch for avoiding looping through all local refs
  FetchCommand: fix fetchSubmodules to work on a Ref to a blob
  Silence API warnings introduced by I466dcde6
  Allow the exclusions of refs prefixes from bitmap
  PackWriterBitmapPreparer: do not include annotated tags in bitmap
  BatchingProgressMonitor: avoid int overflow when computing percentage
  Speedup GC listing objects referenced from reflogs
  FileSnapshotTest: Add more MISSING_FILE coverage

Change-Id: Id0ebfbd85eb815716383b9495eb7dd1f54cf4d74
2023-02-01 01:23:34 +01:00
Matthias Sohn ef010db594 Merge branch 'stable-6.3' into stable-6.4
* stable-6.3:
  Shortcut during git fetch for avoiding looping through all local refs
  FetchCommand: fix fetchSubmodules to work on a Ref to a blob
  Silence API warnings introduced by I466dcde6
  Allow the exclusions of refs prefixes from bitmap
  PackWriterBitmapPreparer: do not include annotated tags in bitmap
  BatchingProgressMonitor: avoid int overflow when computing percentage
  Speedup GC listing objects referenced from reflogs
  FileSnapshotTest: Add more MISSING_FILE coverage

Change-Id: Iefcf5d832bd0087c1027876f2200689e1150abce
2023-02-01 01:12:06 +01:00
Matthias Sohn 82e1362e07 Merge branch 'stable-6.2' into stable-6.3
* stable-6.2:
  Shortcut during git fetch for avoiding looping through all local refs
  FetchCommand: fix fetchSubmodules to work on a Ref to a blob
  Silence API warnings introduced by I466dcde6
  Allow the exclusions of refs prefixes from bitmap
  PackWriterBitmapPreparer: do not include annotated tags in bitmap
  BatchingProgressMonitor: avoid int overflow when computing percentage
  Speedup GC listing objects referenced from reflogs
  FileSnapshotTest: Add more MISSING_FILE coverage

Change-Id: I2ff386d9a096277360e6c7bd5535b49984620fb3
2023-02-01 01:10:56 +01:00
Matthias Sohn d8c02aec6a Merge branch 'stable-6.1' into stable-6.2
* stable-6.1:
  Shortcut during git fetch for avoiding looping through all local refs
  FetchCommand: fix fetchSubmodules to work on a Ref to a blob
  Silence API warnings introduced by I466dcde6
  Allow the exclusions of refs prefixes from bitmap
  PackWriterBitmapPreparer: do not include annotated tags in bitmap
  BatchingProgressMonitor: avoid int overflow when computing percentage
  Speedup GC listing objects referenced from reflogs
  FileSnapshotTest: Add more MISSING_FILE coverage

Change-Id: Iff2fba026b49463016015b2fae1a42cf76ee2dbb
2023-02-01 00:54:30 +01:00
Matthias Sohn b5de5ccb9e Merge branch 'stable-6.0' into stable-6.1
* stable-6.0:
  Shortcut during git fetch for avoiding looping through all local refs
  FetchCommand: fix fetchSubmodules to work on a Ref to a blob
  Silence API warnings introduced by I466dcde6
  Allow the exclusions of refs prefixes from bitmap
  PackWriterBitmapPreparer: do not include annotated tags in bitmap
  BatchingProgressMonitor: avoid int overflow when computing percentage
  Speedup GC listing objects referenced from reflogs
  FileSnapshotTest: Add more MISSING_FILE coverage

Change-Id: Ib5055f2f3b8a313c178d6f6c7c5630285ad5a726
2023-02-01 00:41:52 +01:00
Matthias Sohn da21265a14 Merge branch 'stable-5.13' into stable-6.0
* stable-5.13:
  Shortcut during git fetch for avoiding looping through all local refs
  FetchCommand: fix fetchSubmodules to work on a Ref to a blob
  Silence API warnings introduced by I466dcde6
  Allow the exclusions of refs prefixes from bitmap
  PackWriterBitmapPreparer: do not include annotated tags in bitmap
  BatchingProgressMonitor: avoid int overflow when computing percentage
  Speedup GC listing objects referenced from reflogs
  FileSnapshotTest: Add more MISSING_FILE coverage

Change-Id: I58ad4c210a5e7e5a1ba6b22315b04211c8909950
2023-02-01 00:33:20 +01:00
Luca Milanesio 21e902dd7f Shortcut during git fetch for avoiding looping through all local refs
The FetchProcess needs to verify that all the refs received point
to objects that are reachable from the local refs, which could be
very expensive but is needed to avoid missing objects exceptions
because of broken chains.

When the local repository has a lot of refs (e.g. millions) and the
client is fetching a non-commit object (e.g. refs/sequences/changes in
Gerrit) the reachability check on all local refs can be very expensive
compared to the time to fetch the remote ref.

Example for a 2M refs repository:
- fetching a single non-commit object: 50ms
- checking the reachability of local refs: 30s

A ref pointing to a non-commit object doesn't have any parent or
successor objects, hence would never need to have a reachability check
done. Skipping the askForIsComplete() altogether would save the 30s
time spent in an unnecessary phase.

Signed-off-by: Luca Milanesio <luca.milanesio@gmail.com>
Change-Id: I09ac66ded45cede199ba30f9e71cc1055f00941b
2023-02-01 00:07:45 +01:00
Matthias Sohn 7650832002 FetchCommand: fix fetchSubmodules to work on a Ref to a blob
FetchCommand#fetchSubmodules assumed that FETCH_HEAD can always be
parsed as a tree. This isn't true if it refers to a Ref referring to a
BLOB. This is e.g. used in Gerrit for Refs like refs/sequences/changes
which are used to implement sequences stored in git.

Change-Id: I414f5b7d9f2184b2d7d53af1dfcd68cccb725ca4
2023-01-31 23:52:20 +01:00
Luca Milanesio ad977f1572 Allow the exclusions of refs prefixes from bitmap
When running a GC.repack() against a repository with over one
thousands of refs/heads and tens of millions of ObjectIds,
the calculation of all bitmaps associated with all the refs
would result in an unreasonable big file that would take up to
several hours to compute.

Test scenario: repo with 2500 heads / 10M obj Intel Xeon E5-2680 2.5GHz
Before this change: 20 mins
After this change and 2300 heads excluded: 10 mins (90s for bitmap)

Having such a large bitmap file is also slow in the runtime
processing and have negligible or even negative benefits, because
the time lost in reading and decompressing the bitmap in memory
would not be compensated by the time saved by using it.

It is key to preserve the bitmaps for those refs that are mostly
used in clone/fetch and give the ability to exlude some refs
prefixes that are known to be less frequently accessed, even
though they may actually be actively written.

Example: Gerrit sandbox branches may even be actively
used and selected automatically because its commits are very
recent, however, they may bloat the bitmap, making it ineffective.

A mono-repo with tens of thousands of developers may have
a relatively small number of active branches where the
CI/CD jobs are continuously fetching/cloning the code. However,
because Gerrit allows the use of sandbox branches, the
total number of refs/heads may be even tens to hundred
thousands.

Change-Id: I466dcde69fa008e7f7785735c977f6e150e3b644
Signed-off-by: Luca Milanesio <luca.milanesio@gmail.com>
2023-01-31 17:14:09 -05:00
Dmitrii Filippov 0f3a3fde95 Move MemRefDatabase creation in a separate method.
The InMemoryRepository is used in tests (e.g. in gerrit tests) and it
can be useful to create a custom MemRefDatabase for some tests.

Change-Id: I6fbbbfe04400ea1edc988c8788c8eeb06ca8480a
2023-01-31 13:55:25 -05:00
Luca Milanesio e4529cd39c PackWriterBitmapPreparer: do not include annotated tags in bitmap
The annotated tags should be excluded from the bitmap associated
with the heads-only packfile. However, this was not happening
because of the check of exclusion of the peeled object instead
of the objectId to be excluded from the bitmap.

Sample use-case:

refs/heads/main
  ^
  |
 commit1 <-- commit2 <- annotated-tag1 <- tag1
  ^
  |
 commit0

When creating a bitmap for the above commit graph, before this
change all the commits are included (3 bitmaps), which is
incorrect, because all commits reachable from annotated tags
should not be included.

The heads-only bitmap should include only commit0 and commit1
but because PackWriterBitPreparer was checking for the peeled
pointer of tag1 to be excluded (commit2) which was not found in
the list of tags to exclude (annotated-tag1), the commit2 was
included, even if it wasn't reachable only from the head.

Add an additional check for exclusion of the original objectId
for allowing the exclusion of annotated tags and their pointed
commits. Add one specific test associated with an annotated tag
for making sure that this use-case is covered also.

Example repository benchmark for measuring the improvement:
# refs: 400k (2k heads, 88k tags, 310k changes)
# objects: 11M (88k of them are annotate tags)
# packfiles: 2.7G

Before this change:
GC time: 5h
clone --bare time: 7 mins

After this change:
GC time: 20 mins
clone --bare time: 3 mins

Bug: 581267
Signed-off-by: Luca Milanesio <luca.milanesio@gmail.com>
Change-Id: Iff2bfc6587153001837220189a120ead9ac649dc
2023-01-31 14:15:56 +01:00
Matthias Sohn 611412a055 BatchingProgressMonitor: avoid int overflow when computing percentage
When cloning huge repositories I observed percentage of object counts
turning negative. This happened if lastWork * 100 exceeded
Integer.MAX_VALUE.

Change-Id: Ic5f5cf5a911a91338267aace4daba4b873ab3900
2023-01-31 14:15:53 +01:00
Xing Huang 66ad43a6c7 DfsReaderIoStats: Add Commit Graph fields into DfsReaderIoStats
We are adding commit-graph loading to the DFS stack and the stats object doesn't have fields to track that.

This change replicates the stats of the primary index for the commit-graph.

Signed-off-by: Xing Huang <xingkhuang@google.com>
Change-Id: I4a657bed50083c4ae8bc9f059d4943d612ea2d49
2023-01-25 15:29:04 -06:00
Matthias Sohn cd3fc7a299 Speedup GC listing objects referenced from reflogs
GC needs to get a ReflogReader for all existing refs to list all objects
referenced from reflogs. The existing Repository#getReflogReader method
accepts the ref name and then resolves the Ref to create a ReflogReader.
GC calling that for a huge number of Refs one by one is very slow. GC
first gets all Refs in bulk and then calls getReflogReader for each of
them.

Fix this by adding another getReflogReader method to Repository which
accepts a Ref directly.

This speeds up running JGit gc on a mirror clone of the Gerrit
repository from 15:36 min to 1:08 min. The repository used in this test
had 45k refs, 275k commits and 1.2m git objects.

Change-Id: I474897fdc6652923e35d461c065a29f54d9949f4
2023-01-23 17:19:14 +01:00
Matthias Sohn a1901305b2 Merge branch 'stable-6.4'
* stable-6.4:
  Cache trustFolderStat/trustPackedRefsStat value per-instance
  Refresh 'objects' dir and retry if a loose object is not found

Change-Id: Iea8038dfde29ab988501469f86ee829e578a2fe8
2023-01-13 19:33:54 +01:00
Matthias Sohn 14300dd77b Merge branch 'stable-6.3' into stable-6.4
* stable-6.3:
  Cache trustFolderStat/trustPackedRefsStat value per-instance
  Refresh 'objects' dir and retry if a loose object is not found

Change-Id: I1db2b51ae8101f345d08235d4f3dc416bfcb42d5
2023-01-13 19:32:56 +01:00
Matthias Sohn 5bd2832134 Merge branch 'stable-6.2' into stable-6.3
* stable-6.2:
  Cache trustFolderStat/trustPackedRefsStat value per-instance
  Refresh 'objects' dir and retry if a loose object is not found

Change-Id: Ibc9bffab8c9ef9c39384b53c142d99878f7f3f98
2023-01-13 19:32:06 +01:00
Matthias Sohn 9eef6790cf Merge branch 'stable-6.1' into stable-6.2
* stable-6.1:
  Cache trustFolderStat/trustPackedRefsStat value per-instance
  Refresh 'objects' dir and retry if a loose object is not found

Change-Id: I9e876f72f735f58bf02c7862a3d8e657fc46a7b9
2023-01-13 19:31:18 +01:00
Nasser Grainawi 21b2aef0aa Cache trustFolderStat/trustPackedRefsStat value per-instance
Instead of re-reading the config every time the methods using these
values were called, cache the config value at the time of instance
construction. Caching the values improves performance for each of the
method calls. These configs are set based on the filesystem storing the
repository and unlikely to change while an application is running.

Change-Id: I1cae26dad672dd28b766ac532a871671475652df
Signed-off-by: Nasser Grainawi <quic_nasserg@quicinc.com>
2023-01-13 18:45:02 +01:00
Kaushik Lingarkar fed1a54935 Refresh 'objects' dir and retry if a loose object is not found
A new loose object may not be immediately visible on a NFS
client if it was created on another client. Refreshing the
'objects' dir and trying again can help work around the NFS
behavior.

Here's an E2E problem that this change can help fix. Consider
a Gerrit multi-primary setup with repositories based on NFS.
Add a new patch-set to an existing change and then immediately
fetch the new patch-set of that change. If the fetch is handled
by a Gerrit primary different that the one which created the
patch-set, then we sometimes run into a MissingObjectException
that causes the fetch to fail.

Bug: 581317
Change-Id: Iccc6676c68ef13a1e8b2ff52b3eeca790a89a13d
Signed-off-by: Kaushik Lingarkar <quic_kaushikl@quicinc.com>
2023-01-13 18:44:35 +01:00
kylezhao de7d06775c RevWalk: integrate commit-graph with commit parsing
RevWalk#createCommit() will inspect the commit-graph file to find the
specified object's graph position and then return a new RevCommitCG
instance.

RevCommitGC is a RevCommit with an additional "pointer" (the position)
to the commit-graph, so it can load the headers and metadata from there
instead of the pack. This saves IO access in walks where the body is not
needed (i.e. #isRetainBody is false and #parseBody is not invoked).

RevWalk uses automatically the commit-graph if available, no action
needed from callers. The commit-graph is fetched on first access from
the reader (that internally can keep it loaded and reuse it between
walks).

The startup cost of reading the entire commit graph is small. After
testing, reading a commit-graph with 1 million commits takes less than
50ms. If we use RepositoryCache, it will not be initialized util the
commit-graph is rewritten.

Bug: 574368
Change-Id: I90d0f64af24f3acc3eae6da984eae302d338f5ee
Signed-off-by: kylezhao <kylezhao@tencent.com>
2023-01-10 14:56:33 +08:00
Matthias Sohn 801a56b48a Merge branch 'stable-6.4'
* stable-6.4:
  Introduce core.trustPackedRefsStat config
  Fix documentation for core.trustFolderStat

Change-Id: I93ad0c49b70113134026364c9f647de89d948693
2023-01-06 22:09:55 +01:00
kylezhao 05e5e9907c GC: disable writing commit-graph for shallow repos
In shallow repos, GC writes to the commit-graph that shallow commits
do not have parents. This won't be true after a "git fetch --unshallow"
(and before another GC).

Do not write the commit-graph from shallow clones of a repo. The
commit-graph must have the real metadata of commits and that is not
available in a shallow view of the repo.

Change-Id: Ic9f2358ddaa607c74f4dbf289c9bf2a2f0af9ce0
Signed-off-by: kylezhao <kylezhao@tencent.com>
2023-01-06 13:13:13 -05:00
Matthias Sohn 6a35235d16 Merge branch 'stable-6.3' into stable-6.4
* stable-6.3:
  Introduce core.trustPackedRefsStat config
  Fix documentation for core.trustFolderStat

Change-Id: I18d9fc89c9ac1ef069dcefa7d7f992a28539ccf3
2023-01-05 16:09:58 +01:00
Matthias Sohn e4c2331af6 Merge branch 'stable-6.2' into stable-6.3
* stable-6.2:
  Introduce core.trustPackedRefsStat config
  Fix documentation for core.trustFolderStat

Change-Id: I48b6c095ac62dc859829d6fef45325accbb0a144
2023-01-05 16:05:14 +01:00
Matthias Sohn 62ed46da16 Merge branch 'stable-6.1' into stable-6.2
* stable-6.1:
  Introduce core.trustPackedRefsStat config
  Fix documentation for core.trustFolderStat

Change-Id: Ic78630f74c72624932a384eed52ef79ae1eff3e5
2023-01-05 15:55:19 +01:00
Kaushik Lingarkar 82b5aaf7e3 Introduce core.trustPackedRefsStat config
Currently, we always read packed-refs file when 'trustFolderStat'
is false. Introduce a new config 'trustPackedRefsStat' which takes
precedence over 'trustFolderStat' when reading packed refs. Possible
values for this new config are:

* always: Trust packed-refs file attributes
* after_open: Same as 'always', but refresh the file attributes of
              packed-refs before trusting it
* never: Always read the packed-refs file
* unset: Fallback to 'trustFolderStat' to determine if the file
  attributes of packed-refs can be trusted

Folks whose repositories are on NFS and have traditionally been
setting 'trustFolderStat=false' can now get some performance improvement
with 'trustPackedRefsStat=after_open' as it refreshes the file
attributes of packed-refs (at least on some NFS clients) before
considering it.

For example, consider a repository on NFS with ~500k packed-refs. Here
are some stats which illustrate the improvement with this new config
when reading packed refs on NFS:

trustFolderStat=true trustPackedRefsStat=unset: 0.2ms
trustFolderStat=false trustPackedRefsStat=unset: 155ms
trustFolderStat=false trustPackedRefsStat=after_open: 1.5ms

Change-Id: I00da88e4cceebbcf3475be0fc0011ff65767c111
Signed-off-by: Kaushik Lingarkar <quic_kaushikl@quicinc.com>
2023-01-05 15:52:36 +01:00
Matthias Sohn 8ef58089a8 RefDatabase: fix javadoc formatting
Change-Id: I547819ac380a0e6a88d05206ff171b69f46a8549
2023-01-04 23:51:30 +01:00
Matthias Sohn ddf1c1ed3c Pull up additionalRefsNames from RefDirectory to RefDatabase
This enables to reuse this constant in all RefDatabase implementations.

Change-Id: I13d8fb780de24f71e005b698965fb5bcdbf3c728
2023-01-04 23:51:30 +01:00
Matthias Sohn 70b436b1b2 Add TernarySearchTree
A ternary search tree is a type of tree where nodes are arranged in a
manner similar to a binary search tree, but with up to three children
rather than the binary tree's limit of two.

Each node of a ternary search tree stores a single character, a
reference to a value object and references to its three children named
equal kid, lo kid and hi kid. The lo kid pointer must point to a node
whose character value is less than the current node. The hi kid pointer
must point to a node whose character is greater than the current
node.[1] The equal kid points to the next character in the word. Each
node in a ternary search tree represents a prefix of the stored strings.
All strings in the middle subtree of a node start with that prefix.

Like other prefix trees, a ternary search tree can be used as an
associative map with the ability for incremental string search. Ternary
search trees are more space efficient compared to standard prefix trees,
at the cost of speed.

They allow efficient prefix search which is important to implement
searching refs by prefix in a RefDatabase.

Searching by prefix returns all keys if the prefix is an empty string.

Bug: 576165
Change-Id: If160df70151a8e1c1bd6716ee4968e4c45b2c7ac
2023-01-04 23:51:23 +01:00
kylezhao 414bfe05ff CommitGraph: teach ObjectReader to get commit-graph
FileRepository's ObjectReader#getCommitGraph will return commit-graph
when it exists and core.commitGraph is true.

DfsRepository is not supported currently.

Change-Id: I992d43d104cf542797e6949470e95e56de025107
Signed-off-by: kylezhao <kylezhao@tencent.com>
2023-01-04 14:50:38 +08:00
Ivan Frade 93ac99b52a Merge "CommitGraph: add commit-graph for FileObjectDatabase" 2023-01-03 14:56:53 -05:00
Thomas Wolf 9a6d602488 PatchApplier: fix handling of last newline in text patch
If the last line came from the patch, use the patch to determine whether
or not there should be a trailing newline. Otherwise use the old text.

Add test cases for
- no newline at end, last line not in patch hunk
- no newline at end, last line in patch hunk
- patch removing the last newline
- patch adding a newline at the end of file not having one

all for core.autocrlf false, true, and input.

Add a test case where the "no newline" indicator line is not the last
line of the last hunk. This can happen if the patch ends with removals
at the file end.

Bug: 581234
Change-Id: I09d079b51479b89400ad300d0662c1dcb50deab6
Also-by: Yuriy Mitrofanov <a2terminator@mail.ru>
Signed-off-by: Thomas Wolf <twolf@apache.org>
2022-12-26 11:51:25 +01:00
kylezhao 8a7348df69 CommitGraph: add commit-graph for FileObjectDatabase
This change makes JGit can read .git/objects/info/commit-graph file
and then get CommitGraph.

Loading a new commit-graph into memory requires additional time. After
testing, loading a copy of the Linux's commit-graph(1039139 commits)
is under 50ms.

Bug: 574368
Change-Id: Iadfdd6ed437945d3cdfdbe988cf541198140a8bf
Signed-off-by: kylezhao <kylezhao@tencent.com>
2022-12-23 13:06:06 +08:00
Thomas Wolf aeb74f63d4 Reformat PatchApplier and PatchApplierTest
Some lines were too long, unnecessary fully qualified class names,
and an assertEquals(actual, expected) when it should have been
assertEquals(expected, actual).

Change-Id: I3b3c46c963afe2fb82a79c1e93970e73778877e5
Signed-off-by: Thomas Wolf <twolf@apache.org>
2022-12-22 23:30:02 +01:00
Anna Papitto 6722f25d56 PackWriter#prepareBitmapIndex: add clarifying comments
New readers of #prepareBitmapIndex may be confused about the manual
memory management (hidden mutation and nulling out pointers).

Add two clarifying comments to help future readers.

Change-Id: I93cab1919066efda37e96c47667f6991f67e377e
2022-12-20 13:56:11 -05:00
Anna Papitto 9b7c3ac11f IO#readFully: provide overload that fills the full array
IO#readFully is often called with the intent to fill the destination
array from beginning to end. The redundant arguments for where to start
and stop filling are opportunities for bugs if specified incorrectly or
if not changed to match a changed array length.

Provide a overloaded method for filling the full destination array.

Change-Id: I964f18f4a061189cce1ca00ff0258669277ff499
Signed-off-by: Anna Papitto <annapapitto@google.com>
2022-12-19 10:26:41 -08:00
Anna Papitto 0d10ebe560 PackReverseIndex#findPosition: fix typo in method name
The package-private findPostion method has a type in it. The typo will
become more widespread when a file-based implementation class is
introduced.

Correct the spelling to findPosition before the file-based
implementation is introduced.

Change-Id: Ib285f5a3f9a333ace1782dae9b5d425505eb962a
Signed-off-by: Anna Papitto <annapapitto@google.com>
2022-12-16 16:48:43 -05:00
kylezhao b082c58e0f GC: Write commit-graph files when gc
If 'core.commitGraph' and 'gc.writeCommitGraph' are both true, then gc
will rewrite the commit-graph file when 'git gc' is run. Defaults to
false while the commit-graph feature matures.

Bug: 574368
Change-Id: Ic94cd69034c524285c938414610f2e152198e06e
Signed-off-by: kylezhao <kylezhao@tencent.com>
2022-12-16 11:11:45 -05:00
kylezhao 7016e2ddae CommitGraph: add core.commitGraph config
Change-Id: I3b5e735ebafba09ca18fd83da479c7950fa3ea8d
Signed-off-by: kylezhao <kylezhao@tencent.com>
2022-12-16 10:21:09 -05:00
Ivan Frade 6ea36794d1 Merge "Gc#deleteOrphans: avoid dependence on PackExt alphabetical ordering" 2022-12-16 08:20:24 -05:00
kylezhao 7b0f633b67 CommitGraph: implement commit-graph read
Git introduced a new file storing the topology and some metadata of
the commits in the repo (commitGraph). With this data, git can browse
commit history without parsing the pack, speeding up e.g.
reachability checks.

This change teaches JGit to read commit-graph-format file, following
the upstream format([1]).

JGit can read a commit-graph file from a buffered stream, which means
that we can provide this feature for both FileRepository and
DfsRepository.

[1] https://git-scm.com/docs/commit-graph-format/2.21.0

Bug: 574368
Change-Id: Ib5c0d6678cb242870a0f5841bd413ad3885e95f6
Signed-off-by: kylezhao <kylezhao@tencent.com>
2022-12-16 06:57:06 -05:00
Anna Papitto 5c6c374ff6 Gc#deleteOrphans: avoid dependence on PackExt alphabetical ordering
Deleting orphan files depends on .pack and .keep being reverse-sorted
to before the corresponding index files that could be orphans. The new
reverse index file extension (.rev) will break that frail dependency.

Rewrite Gc#deleteOrphans to avoid that dependence by tracking which pack
names have a .pack or .keep file and then deleting any index files that
without a corresponding one. This approach takes linear time instead of
the O(n logn) time needed for sorting.

Change-Id: If83c378ea070b8871d4b01ae008e7bf8270de763
Signed-off-by: Anna Papitto <annapapitto@google.com>
2022-12-15 11:54:11 -08:00
Jeremy T. Braun 514ebfdc7e WalkPushConnection: Sanitize paths given to transports
These paths are given to the underlying URI-based transports (s3, sftp,
http), all of which expect forward-slash as the path separator
character.

Change-Id: I3cbb5928c9531a4da4691411bd8ac248fdf47ef2
2022-12-15 12:53:19 -05:00
Matthias Sohn 61f4a036c6 Merge changes Iad832fe1,Icef9658c
* changes:
  CommitGraphWriter: fix UnusedException errorprone error
  Update jetty to 10.0.13
2022-12-14 04:50:54 -05:00
Ivan Frade 91587aebd0 GraphCommits: Remove unused getter by position
CommitGraphWriter uses the GraphCommits in for-each loops and doesn't
need the access by position anymore. This was a left-over from
https://git.eclipse.org/r/c/jgit/jgit/+/182832

Remove the unused method.

Change-Id: I39df9bfab2601d581705ddf4cea3c04ed4765ff9
2022-12-13 14:37:37 -08:00
Matthias Sohn 04245494bb CommitGraphWriter: fix UnusedException errorprone error
Errorprone run in the bazel build raised this exception:

org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/commitgraph/CommitGraphWriter.java:105:
error: [UnusedException] This catch block catches an exception and
re-throws another, but swallows the caught exception rather than setting
it as a cause. This can make debugging harder.
		} catch (InterruptedIOException e) {
		  ^
    (see https://errorprone.info/bugpattern/UnusedException)
  Did you mean 'throw new
IOException(JGitText.get().commitGraphWritingCancelled, e);'?

Change-Id: Iad832fe17955fc1e60e6a4902bc50fd9dca76b9d
2022-12-13 15:04:45 +01:00
Xing Huang 3a136d2000 PackExt: Add a commit graph extension.
There is no commit graph PackExt because the non-DFS stack is not writing using PackExt mechanism. The extension is needed in DFS to determine the stream to write the commit-graph.

Add a commit graph extension that matches the one in cgit
(https://git-scm.com/docs/commit-graph#_file_layout)
in preparation for adding DFS support for reading and writing commit graphs.

Change-Id: Id14eda9f116a319124981e0bcbc533928b1b5e8c
Signed-off-by: Xing Huang <xingkhuang@google.com>
2022-12-12 17:26:12 -05:00
Matthias Sohn e74f3855ad Merge "commitgraph package: fix exports/imports, add @since tag for new API" 2022-12-08 14:10:23 -05:00
Sergey ec7c61eac3 BatchRefUpdate: Consistent switch branches in ref update
The expression RefUpdate ru = newUpdate(cmd) is eagerly evaluated before the switch statement.
But it is not used in some switch cases and thus is calculated uselessly.

Move expression evaluation to the switch case where it is actually used.
After such a move, several cases became identical and thus were squashed.

Change-Id: Ifd1976f1c28378e092fb24d7ca9c415cba49f07f
2022-12-08 05:27:35 -05:00
Sergey 2b21d9bbb6 RefWriter#writePackedRefs: Remove a redundant "if" check
After checking the variable, the same variable was checked again inside
the "if" block, and after the first check, this variable does not
change. Remove the second unnecessary check.

Change-Id: I6a38e67073f7f93105575b8f415ad32d350af602
2022-12-08 10:59:58 +01:00
Matthias Sohn ebc1f7d65c commitgraph package: fix exports/imports, add @since tag for new API
Change-Id: I9175b1d796f91f5ba4e21d3418550ae451c054b0
2022-12-08 02:00:58 +01:00
kylezhao cf70e7cbe4 CommitGraph: implement commit-graph writer
Teach JGit to write a commit-graph formatted file by walking commit
graph from specified commit objects.

See: https://git-scm.com/docs/commit-graph-format/2.21.0

Bug: 574368
Change-Id: I34f9f28f8729080c275f86215ebf30b2d05af41d
Signed-off-by: kylezhao <kylezhao@tencent.com>
2022-12-06 20:34:46 +08:00
Dmitrii Filippov cb9f058f9b Fix crashes on rare combination of file names
The NameConflictTreeWalk class is used in merge for iterating over
entries in commits. The class uses a separate iterator for each
commit's tree. In rare cases it can incorrectly report the same entry
twice. As a result, duplicated entries are added to the merge result
and later jgit throws an exception when it tries to process merge
result.

The problem appears only when there is a directory-file conflict for
the last item in trees. Example from the bug:
Commit 1:
* subtree - file
* subtree-0 - file
Commit 2:
* subtree - directory
* subtree-0 - file
Here the names are ordered like this:
"subtree" file <"subtree-0" file < "subtree" directory.

The NameConflictTreeWalk handles similar cases correctly if there are
other files after subtree... in commits - this is processed in the
AbstractTreeIterator.min function. Existing code has a special
optimization for the case, when all trees are pointed to the same
entry name - it skips additional checks. However, this optimization
incorrectly skips checks if one of trees reached the end.

The fix processes a situation when some trees reached the end, while
others are still point to an entry.

bug: 535919
Change-Id: I62fde3dd89779fac282479c093400448b4ac5c86
2022-11-29 10:49:27 +01:00
Han-Wen NIenhuys 1d5a6c77a6 Merge "Fix crashes on rare combination of file names" 2022-11-28 09:34:46 -05:00
alerthak c14e6f5c6c [pgm] Add options --name-only, --name-status to diff, log, show
Change-Id: Ib218bd2ccbd7990feca4c35d8c8dc34d4a8291e6
2022-11-23 16:12:54 +01:00
Matthias Sohn 9fc9ae6de9 Fix warning about non-externalized String
Change-Id: I00b2132eac6d6ad2701d3f8151eb8c80ad6d679d
2022-11-20 20:30:45 +01:00
Matthias Sohn fe3908076e Merge branch 'stable-6.3'
* stable-6.3:
  Remove unused imports
  Suppress non-externalized String warnings
  Remove unused API problem filters
  Silence API errors
  Silence API errors
  Silence API warnings

Change-Id: I6778c8266bc7e918c943dcabf23aa230f4e998d5
2022-11-20 20:24:14 +01:00
Matthias Sohn 246981c850 Merge branch 'stable-6.2' into stable-6.3
* stable-6.2:
  Remove unused imports
  Suppress non-externalized String warnings
  Remove unused API problem filters
  Silence API errors
  Silence API errors
  Silence API warnings

Change-Id: I71aa9f890c5eb05849ad16a00b9974da5e51171e
2022-11-20 20:22:24 +01:00
Matthias Sohn 3301032c6a Suppress non-externalized String warnings
Change-Id: I95c61bd906bd3f7cdea9581e0ffa92ec607c6140
2022-11-20 20:16:28 +01:00
Matthias Sohn 71f50e52af Merge branch 'stable-6.1' into stable-6.2
* stable-6.1:
  Silence API errors
  Silence API errors
  Silence API warnings

Change-Id: Id3efa1b32710661cde63e93c5b8832785a805415
2022-11-20 20:10:37 +01:00
Matthias Sohn 97ad9bdae6 Merge branch 'stable-6.0' into stable-6.1
* stable-6.0:
  Silence API errors
  Silence API warnings

Change-Id: I2b8336652e60dec97666582cf9331c8505729473
2022-11-20 20:08:42 +01:00
Matthias Sohn 41b33a16b8 Silence API errors
Change-Id: Ie112b2099ea2125bc85863524e56f09ba4907373
2022-11-20 19:55:22 +01:00
Matthias Sohn 12f48276bd Merge branch 'stable-5.13' into stable-6.0
* stable-5.13:
  Silence API warnings

Change-Id: If5ab988a0e177c37b125e0b10625e506eeb2a74f
2022-11-20 19:54:44 +01:00
Matthias Sohn aa9f736c33 Silence API warnings
introduced by
- addition of configurable SHA1 implementation in 5.13.2
- 3-digit @since 5.9.1 annotations on GitServlet methods

Change-Id: If19853fcc5e3677e5b18e8e3fbbcd2773378dffc
2022-11-20 19:45:54 +01:00
Anna Papitto bd515f1c2a DfsBlockCache: Report IndexEventConsumer metrics for reverse indexes.
IndexEventConsumer metrics are reported per index PackExt and reverse
indexes did not have one, so they were not included.

Now that there is a REVERSE_INDEX PackExt, enable reporting
IndexEventConsumer metrics for reverse indexes.

Change-Id: Ia6a752f6eb8932a5b4ba45cc15cbc7e0786fd247
Signed-off-by: Anna Papitto <annapapitto@google.com>
2022-11-17 12:38:48 -05:00
Anna Papitto accacc27a1 DfsStreamKey: Replace ForReverseIndex to separate metrics.
Keys used for identifying reverse indexes in the DfsBlockCache use a
custom subclass ForReverseIndex because there was no PackExt for them.
This conflates BlockCacheMetrics for reverse indexes with those for
packs, since the key falls back onto 0 when there is no extension.

Replace the custom ForReverseIndex with a DfsStreamKey usage to bring
keys for the new REVERSE_INDEX extension in line with INDEX and BITMAP
and separate reverse index and pack BlockCacheMetrics.

Change-Id: I305e2c16d2a8cb2a824855ea92e0c9a9b188fce5
Signed-off-by: Anna Papitto <annapapitto@google.com>
2022-11-17 12:38:36 -05:00
Ivan Frade 5c033a98f6 Merge "PackExt: Add a reverse index extension." 2022-11-17 12:38:10 -05:00
Thomas Wolf 1c886d92f6 RawText.isBinary(): handle complete buffer correctly
Make sure we always get consistent results, whether or not we have the
full data in the buffer.

Change-Id: Ieb379a0c375ad3dd352e63ac2f23bda6ef16c215
Signed-off-by: Thomas Wolf <twolf@apache.org>
2022-11-16 15:17:19 -05:00
Matthias Sohn 0fb9d26eff Merge branch 'stable-6.3'
* stable-6.3:
  [benchmarks] Remove profiler configuration
  Add SHA1 benchmark
  [benchmarks] Set version of maven-compiler-plugin to 3.8.1
  Fix running JMH benchmarks
  Add option to allow using JDK's SHA1 implementation
  Fix API breakage caused by extracting WorkTreeUpdater
  Extract Exception -> HTTP status code mapping for reuse
  Don't handle internal git errors as an HTTP error
  Ignore IllegalStateException if JVM is already shutting down
  Allow to perform PackedBatchRefUpdate without locking loose refs

Change-Id: Ib58879be292c54a2a7f4936ac0986997985c822b
2022-11-16 10:15:30 +01:00
Matthias Sohn 1cd9a1f804 Merge branch 'stable-6.2' into stable-6.3
* stable-6.2:
  Extract Exception -> HTTP status code mapping for reuse
  Don't handle internal git errors as an HTTP error
  Allow to perform PackedBatchRefUpdate without locking loose refs

Change-Id: I562be0802efa231023c5f10e6461339b2d7fbacf
2022-11-16 10:14:13 +01:00
Matthias Sohn a24b22632f Merge branch 'stable-6.1' into stable-6.2
* stable-6.1:
  Extract Exception -> HTTP status code mapping for reuse
  Don't handle internal git errors as an HTTP error
  Allow to perform PackedBatchRefUpdate without locking loose refs

Change-Id: Icb321779184d20f3871e236fda1a3acba605a6da
2022-11-16 10:13:20 +01:00
Matthias Sohn 77e2f4bd27 Merge "Merge branch 'stable-6.0' into stable-6.1" into stable-6.1 2022-11-16 04:10:43 -05:00
Matthias Sohn f3e0e9d5a3 Merge branch 'stable-6.2' into stable-6.3
* stable-6.2:
  [benchmarks] Remove profiler configuration
  Add SHA1 benchmark
  [benchmarks] Set version of maven-compiler-plugin to 3.8.1
  Fix running JMH benchmarks
  Add option to allow using JDK's SHA1 implementation
  Ignore IllegalStateException if JVM is already shutting down

Change-Id: I9c1576011c11b4ff8f453d18d9e786cee59860fa
2022-11-16 09:56:08 +01:00
Matthias Sohn d588c2c9ad Merge branch 'stable-6.1' into stable-6.2
* stable-6.1:
  [benchmarks] Remove profiler configuration
  Add SHA1 benchmark
  [benchmarks] Set version of maven-compiler-plugin to 3.8.1
  Fix running JMH benchmarks
  Add option to allow using JDK's SHA1 implementation
  Ignore IllegalStateException if JVM is already shutting down

Change-Id: Ie433c46a01a0f33848d54ecf99b30a44ca01e286
2022-11-16 09:55:22 +01:00
Matthias Sohn 7f36943d0c Merge branch 'stable-6.0' into stable-6.1
* stable-6.0:
  [benchmarks] Remove profiler configuration
  Add SHA1 benchmark
  [benchmarks] Set version of maven-compiler-plugin to 3.8.1
  Fix running JMH benchmarks
  Add option to allow using JDK's SHA1 implementation
  Ignore IllegalStateException if JVM is already shutting down

Change-Id: I176419026c3f4fdd8ebd34c61468c1ec3482ff45
2022-11-16 09:54:28 +01:00
Anna Papitto ed99797970 PackExt: Add a reverse index extension.
There is no reverse index PackExt because the reverse index is not currently
written to a file. This prevents fine-grained performance reporting for reverse
indexes, which will be useful when introducing a reverse index file and
observing performance changes.

Add a reverse index extension that matches the one in cgit
(9bf691b78c/Documentation/gitformat-pack.txt (L302))
in preparation for adding a reverse index file while observing
performance before and after.

Change-Id: Iee53f1e01cf645a3c468892fcf97c8444f9a784a
Signed-off-by: Anna Papitto <annapapitto@google.com>
2022-11-15 15:30:50 -08:00
Matthias Sohn f1909615d3 Merge branch 'stable-5.13' into stable-6.0
* stable-5.13:
  [benchmarks] Remove profiler configuration
  Add SHA1 benchmark
  [benchmarks] Set version of maven-compiler-plugin to 3.8.1
  Fix running JMH benchmarks
  Add option to allow using JDK's SHA1 implementation
  Ignore IllegalStateException if JVM is already shutting down

Change-Id: I40105336f0b9e593a8a2c242a9557f854c274fdc
2022-11-16 00:15:17 +01:00
Matthias Sohn 59029aec30 Add option to allow using JDK's SHA1 implementation
The change If6da9833 moved the computation of SHA1 from the JVM's
JCE to a pure Java implementation with collision detection.
The extra security for public sites comes with a cost of slower
SHA1 processing compared to the native implementation in the JDK.

When JGit is used internally and not exposed to any traffic from
external or untrusted users, the extra cost of the pure Java SHA1
implementation can be avoided, falling back to the previous
native MessageDigest implementation.

Bug: 580310
Change-Id: Ic24c0ba1cb0fb6282b8ca3025ffbffa84035565e
2022-11-15 23:08:13 +01:00
Matthias Sohn 4d9e004f57 Fix API breakage caused by extracting WorkTreeUpdater
448052dc2e made WorkTreeUpdater package visible which breaks API for
subclasses of ResolveMerger since they cannot access WorkTreeUpdater.

Fix this by moving WorkTreeUpdater into ResolveMerger as a nested class
and mark it protected so that subclasses can use it.

Bug: 581049
Change-Id: I5a2c4953f8514dc0a1b8041c8e069d28370bb2eb
2022-11-15 15:18:38 +01:00
Eryk Szymanski bd2aced4a3 Use replace instead of replaceAll in toCleanString
This is from SonarLint (rule.java:S4348)
Regex patterns should not be created needlessly:

When String::replaceAll is used, the first argument should be a real
regular expression. If it’s not the case, String::replace does exactly
the same thing as String::replaceAll without the performance drawback of
the regex.

Change-Id: I00ba967ff4a27eeeb6fccf9373f6df2c94ecd823
2022-11-10 20:08:25 +01:00
Dmitrii Filippov 1e04046a6d Fix crashes on rare combination of file names
The NameConflictTreeWalk class is used in merge for iterating over
entries in commits. The class uses a separate iterator for each
commit's tree. In rare cases it can incorrectly report the same entry
twice. As a result, duplicated entries are added to the merge result
and later jgit throws an exception when it tries to process merge
result.

The problem appears only when there is a directory-file conflict for
the last item in trees. Example from the bug:
Commit 1:
* subtree - file
* subtree-0 - file
Commit 2:
* subtree - directory
* subtree-0 - file
Here the names are ordered like this:
"subtree" file <"subtree-0" file < "subtree" directory.

The NameConflictTreeWalk handles similar cases correctly if there are
other files after subtree... in commits - this is processed in the
AbstractTreeIterator.min function. Existing code has a special
optimization for the case, when all trees are pointed to the same
entry name - it skips additional checks. However, this optimization
incorrectly skips checks if one of trees reached the end.

The fix processes a situation when some trees reached the end, while
others are still point to an entry.

bug: 535919
Change-Id: I62fde3dd89779fac282479c093400448b4ac5c86
2022-11-03 14:09:56 -04:00
Josh Brown fe9aeb02e6 UploadPack: Receive and parse client session-id
Before this change JGit did not support the session-id capability
implemented by native Git in UploadPack. This change implements
advertising the capability from the server and parsing the session-id
received from the client during an UploadPack operation.

Enable the transfer.advertisesid config setting to advertise the
capability from the server. The client may send a session-id capability
in response. If received, the value from this is parsed and available
via the getClientSID method on the UploadPack object.

This change does not add the capability to send a session-id from the
JGit client.

https://git-scm.com/docs/gitprotocol-capabilities#_session_idsession_id

Change-Id: Ib1b6929ff1b3a4528e767925b5e5c44b5d18182f
Signed-off-by: Josh Brown <sjoshbrown@google.com>
2022-11-02 16:13:22 -04:00
Josh Brown 7b0a71a5e9 TransferConfig: Move reading advertisesid setting into TransferConfig
The config setting to enable advertising the session-id capability is
currently read in the ReceivePack class. This change moves it to a
common location in the TransferConfig class so that it can be reused
in other places like UploadPack. TransferConfig is also a more logical
place for the setting as it resides in the `transfer` config section.

Set the transfer.advertisesid setting to true to send the session-id
capability to the client.

Change-Id: If68ecb5e68b59f5c452a7992d02e3688b0a86747
Signed-off-by: Josh Brown <sjoshbrown@google.com>
2022-11-02 16:13:08 -04:00
Josh Brown e8068188f1 FirstWant: Parse client session-id if received.
In protocol V0 the client capabilities are appended to the first line.
Parsing session-id is currently only supported during a ReceivePack
operation. This change will parse the client session-id capability if
it has been sent by the client.

If the server sends the session-id capability to the client. The client
may respond with a session ID of its own. FirstWant.fromLine will now
parse the ID and make it available via the getClientSID method.

This change does not add support to send the session-id capability from
the server. The change is necessary to support session-id in UploadPack.

Change-Id: Id3fe44fdf9a72984ee3de9cf40cc4e71d434df4a
Signed-off-by: Josh Brown <sjoshbrown@google.com>
2022-11-02 20:12:03 +00:00
Josh Brown 93097f0018 ReceivePack: Receive and parse client session-id.
Before this change JGit did not support the session-id capability
implemented by native Git. This change implements advertising the
capability from the server and parsing the session-id received from
the client during a ReceivePack operation.

Enable the transfer.advertisesid config setting to advertise the
capability from the server. The client may send a session-id capability
in response. If received, the value from this is parsed and available
via the getClientSID method on the ReceivePack object. All capabilities
in the form `capability=value` are now split into key value pairs at the
first `=` character. This change replaces specific handling for the 
agent capability.

This change does not add advertisement or parsing to UploadPack. This
change also does not add the ability to send a session ID from the JGit
client.

https://git-scm.com/docs/protocol-v2/2.33.0#_session_idsession_id

Change-Id: I56fb115e843b11b27e128c4ac427b05d5ec129d0
Signed-off-by: Josh Brown <sjoshbrown@google.com>
2022-10-27 16:17:50 -04:00
Matthias Sohn 924491d4df Ignore IllegalStateException if JVM is already shutting down
Trying to register/unregister a shutdown hook when the JVM is already in
shutdown throws an IllegalStateException. Ignore this exception since we
can't do anything about it.

Bug: 580953
Change-Id: I8fc6fdd5585837c81ad0ebd6944430856556d90e
2022-10-27 20:31:58 +02:00
Saša Živkov cb90ed0852 Allow to perform PackedBatchRefUpdate without locking loose refs
Add another newBatchUpdate method in the RefDirectory where we can
control if the created PackedBatchRefUpdate will lock the loose refs or
not.

This can be useful in cases when we run programs which have exclusive
access to a Git repository and we know that locking loose refs is
unnecessary and just a performance loss.

Change-Id: I7d0932eb1598a3871a2281b1a049021380234df9
2022-10-26 16:51:08 +02:00
kylezhao ad9c217f49 PushCommand: allow users to disable use of bitmaps for push
Reachability bitmaps are designed to speed up the "counting objects"
phase of generating a pack during a clone or fetch. They are not
optimized for Git clients sending a small topic branch via "git push".
In some cases (see [1]), using reachability bitmaps during "git push"
can cause significant performance regressions.

Add PushCommand#setUseBitmaps(boolean) to allow users to tell "git push"
not to use bitmaps.

[1]: https://lore.kernel.org/git/87zhoz8b9o.fsf@evledraar.gmail.com/

Change-Id: I7fb7d26084ec63ddfa7249cf58abb85929b30e56
Signed-off-by: kylezhao <kylezhao@tencent.com>
2022-10-21 08:11:33 +02:00
Thomas Wolf 71af0d6a5c I/O redirection for the pre-push hook
Fix and complete the implementation of calling the pre-push hook.
Add the missing error stream redirect, and add the missing setters
in Transport and in PushCommand. In Transport, delay setting up a
PrePushHook such that it happens only on a push. Previously, the
hook was set up also for fetches.

Bug: 549246
Change-Id: I64a576dfc6b139426f05d9ea6654027ab805734e
Signed-off-by: Thomas Wolf <twolf@apache.org>
2022-10-20 23:34:56 +02:00
Ivan Frade 96236fdcb5 PackParser: populate full size of the PackedObjectInfos
We need the full size of the objects to populate the object-size index
of a pack. This size is not always the one encoded in the object header
in the pack (e.g. for deltas).

Populate the full size of PackedObjectInfos in the PackParser, which is
invoked when receiving a pack e.g. in a push.

Change-Id: I102c20901aefb5e85047e2e526c0d733f82ff74b
2022-10-18 11:19:21 -07:00
Ivan Frade 60206ea95f PackedObjectInfo: add the full size to the description
So we can create a size index later.

Change-Id: I9db47ced929fbf045fc37bead6449bbf5484d308
2022-10-18 11:19:21 -07:00
Ivan Frade b58ea5c6c9 ObjectReader: New #isNotLargerThan method
Partial clones filter the objects to send by size calling
ObjectReader#getObjectSize per object. This method reads the object from
storage to get the size, which can be expensive.

Offer a #isNotLargerThan method. The default implementation reads the
object, but subclasses can override it with more efficient lookups (e.g.
adding an index).

isNotLargerThan gives implementors more options to optimize than
getObjectIndex (e.g. can be implemented storing only object over certain
size).

Change-Id: Iefd4b1370cb9144f15cc0391286aeeb365e6ea87
2022-10-18 11:18:46 -07:00
Jörg Kubitz 2021ce3423 ObjectDirectory: avoid using File.getCanonicalPath()
On java 17 + Windows OS java.io.File.getCanonicalPath is a very slow
system call which uses most time during clone.

That is since JDK 12 the result of File.getCanonicalPath is not cached
anymore by default:
https://bugs.openjdk.java.net/browse/JDK-8207005

* Use toRealPath() to follow symbolic links also on windows.
* Cache the result.

Bug: 580568
Change-Id: I95f4f5b2babefd7210ee4740646230225ebf3788
2022-09-22 10:48:50 +02:00
Jörg Kubitz eb5124c74f AutoCRLFOutputStream: use BufferedOutputStream
This should improve performance of autocrlf checkout.

Bug: 580651
Change-Id: I2e2fe0273ac3c71fad50a575278234804ee28306
2022-09-20 10:00:17 +02:00
Thomas Wolf f71fcbf36b CloneCommand: set HEAD also when not checking out
CloneCommand, when setNoCheckout(true) was set, did not set HEAD.
With C git, "git clone --no-checkout" does.

Change-Id: Ief3df7e904ce90829a6345a6c3e9ee6a68486ab0
Signed-off-by: Thomas Wolf <twolf@apache.org>
2022-09-18 19:43:40 +02:00
Thomas Wolf 4f4204914c Pass on shallowSince only if not null
FetchCommand.setShallowSince() and Transport.setDeepenSince() require
a non-null argument.

Change-Id: I1c3a20be518374e380a4e90787ed834438da40ee
Signed-off-by: Thomas Wolf <twolf@apache.org>
2022-09-18 19:32:31 +02:00
Thomas Wolf ccad49a28c Fix wrong @since tag
PatchApplier was merged only in JGit 6.4.

Change-Id: Ica84d8184c1d3a1acce8beba9b076f6c32e749d7
Signed-off-by: Thomas Wolf <twolf@apache.org>
2022-09-18 17:02:51 +02:00
Han-Wen NIenhuys 21a497843c Merge "Split out ApplyCommand logic to PatchApplier class" 2022-09-15 04:16:48 -04:00
Nitzan Gur-Furman acde6c8f5b Split out ApplyCommand logic to PatchApplier class
PatchApplier now routes updates through the index. This has two
results:

* we can now execute patches in-memory.

* the JGit apply command will now always update the
index to match the working tree.

Change-Id: Id60a88232f05d0367787d038d2518c670cdb543f
Co-authored-by: Han-Wen Nienhuys <hanwen@google.com>
Co-authored-by: Nitzan Gur-Furman <nitzan@google.com>
2022-09-15 09:15:55 +02:00
kylezhao ac3bf7896e Fix typo in FetchV2Request javadoc
Change-Id: I905dda15e1b7f5e431816d3fbb01a1672e5fc786
Signed-off-by: kylezhao <kylezhao@tencent.com>
2022-09-14 15:49:39 +08:00
Thomas Wolf a8e683fef6 [merge] Fix merge conflicts with symlinks
Previous code would do a content merge on symlinks, and write the merge
result to the working tree as a file. C git doesn't do this; it leaves
a symlink in the working tree unchanged, or in a delete-modify conflict
it would check out "theirs".

Moreover, previous code would write the merge result to the link target,
not to the link. This would overwrite an existing link target, or fail
if the link pointed to a directory.

In link/file conflicts or file/link conflicts, C git always puts the
file into the working tree.

Change conflict handling accordingly. Add tests for all the conflict
cases.

Bug: 580347
Change-Id: I3cffcb4bcf8e336a85186031fff23f0c4b6ee19d
Signed-off-by: Thomas Wolf <twolf@apache.org>
2022-09-07 15:02:02 +02:00
Matthias Sohn 370d1170e8 Merge branch 'master' into stable-6.3
* master:
  Move WorkTreeUpdater to merge package
  WorkTreeUpdater: use DirCacheCheckout#StreamSupplier
  DirCacheCheckout#getContent: also take InputStream supplier
  WorkTreeUpdater: remove safeWrite option

Change-Id: I8be570dbc4ad0d0b46046b85cbda24c3adcba170
2022-09-06 15:58:47 +02:00
Matthias Sohn 57087e2b92 Merge changes I888f313f,I98de155c,I2efa9a6d,Ifa79dac2
* changes:
  Move WorkTreeUpdater to merge package
  WorkTreeUpdater: use DirCacheCheckout#StreamSupplier
  DirCacheCheckout#getContent: also take InputStream supplier
  WorkTreeUpdater: remove safeWrite option
2022-09-06 09:42:14 -04:00
Han-Wen Nienhuys 448052dc2e Move WorkTreeUpdater to merge package
This avoids making it public with the associated costs for backward
compatibility guarantees.

Change-Id: I888f313f3854deace8d4cd92f354a6ef0d3b5460
2022-09-06 14:48:13 +02:00
Han-Wen Nienhuys 6ed4c074ca WorkTreeUpdater: use DirCacheCheckout#StreamSupplier
This avoids having to introduce the StreamLoader bridging class.

Change-Id: I98de155c458745236df24d6323eabed5061e7f8c
2022-09-06 14:48:13 +02:00
Han-Wen Nienhuys 6d2bcb6043 DirCacheCheckout#getContent: also take InputStream supplier
This lets us use DirCacheCheckout for routines that want to write
files in the worktree that aren't available as a git object.

DirCacheCheckout#getContent takes a InputStream supplier rather than
InputStream: if filtering fails with IOException, the data is placed
unfiltered in the checkout. This means that the stream has to be read
again, from the start.

Use it in this way in ApplyCommand. This use is incorrect, though: the
same InputStream is returned twice, so if the read to be retried, the
stream will return 0 bytes. It doesn't really matter, because in
either case, the SHA1 will not match up, and the patch fails.

Change-Id: I2efa9a6da06806ff79b155032fe4b34be8fec09e
2022-09-06 14:38:41 +02:00
Matthias Sohn 7c4a5421cc Revert "Adds FilteredRevCommit that can overwrites its parents in the
DAG."

This reverts commit 6297491e8a.

This is done as a quick fix for the failure of egit tests caused by  the
introduction of FilteredRevCommit.

Bug: 580690
Change-Id: Ia6b651dd11b0a4b02d5e52247eb4bf13adf94e27
2022-09-06 10:40:26 +02:00
Matthias Sohn ee6334bccf Revert "Option to pass start RevCommit to be blamed on to the
BlameGenerator."

This reverts commit 5747bba48b.

This is done as a quick fix for the failure of egit tests caused by  the
introduction of FilteredRevCommit.

Bug: 580690
Change-Id: Ia0178bc2de4fc825a81207bbd7979bf3a386c955
2022-09-06 10:40:26 +02:00
Han-Wen Nienhuys 2778bb2cc9 WorkTreeUpdater: remove safeWrite option
This was added in Ideaefd5178 to anticipate on writing files for
ApplyCommand, but we are keeping WorkTreeUpdater private to the merge
package for now.

Change-Id: Ifa79dac245e60eb7a77eaea4cc1249222e347d38
2022-09-06 10:01:44 +02:00
Ivan Frade ca166a0c62 BaseSuperprojectWriter: report invalid paths as manifest errors
An invalid path in the manifest (e.g. '.') is reported by DirCache in a
runtime exception. In server context this becomes a 500 instead of a user error.

Wrap the runtime invalid path exception into a checked ManifestErrorException that
caller can handle.

Change-Id: I61a2104922765506ae232334891057bb06141d97
2022-08-23 12:10:27 -07:00
Han-Wen Nienhuys 25aceffdc5 ApplyCommand: fix ApplyResult#updatedFiles
On executing a copy, mark the destination as updated.

On executing a rename, mark both source and destination as updated.

Change-Id: Ied5b9b0e5a14eac59a06cdd0961e25e143f50ff0
2022-08-23 09:23:23 -04:00
Han-Wen Nienhuys 0887111ba5 WorkTreeUpdater: rename metadata maps
Change-Id: I8ff3803da8fc13377d11c2dc5523e9e32d0650cb
2022-08-23 05:13:43 -04:00
Han-Wen Nienhuys ddb1a0c3a2 WorkTreeUpdater#Result: hide data members
This is the standard across JGit.

Change-Id: Ie52ad7000d8725657b33dd4f3adcc05ab9666875
2022-08-23 04:24:37 -04:00
Han-Wen NIenhuys d718127a7e Merge "WorkTreeUpdater: re-format and clean-up" 2022-08-18 11:22:46 -04:00
Ronald Bhuleskar 1a2aa740c8 Add javadoc on RevCommit
Change-Id: Ib413154fe52983286cb6307862a7373af4fec6e8
2022-08-17 19:16:45 -04:00
Ronald Bhuleskar 5747bba48b Option to pass start RevCommit to be blamed on to the BlameGenerator.
This can allow passing a FilteredRevCommit which is the filtered list of
commit graph making it easier for Blame to work on. This can
significantly improve blame performance since blame can skip expensive
RevWalk.

Change-Id: Ie127cb710d004079e9f53a5802130afdb49a7de1
2022-08-17 13:50:34 -04:00
Thomas Wolf aaae9ecd57 WorkTreeUpdater: re-format and clean-up
Reformat using the standard JGit formatter settings. Clean-ups:

* Try to improve javadoc.
* Remove blindly copy-pasted "@since 6.1" annotations.
* Get rid of private method nonNullNonBareRepo(); it's not needed.
* Simplify method nonNullRepo(), and annotate as @NonNull.
* Rename setInCoreFileSizeLimit() to getInCoreFileSizeLimit().

Change-Id: Ib1797e7cf925d87554307468330971e8ab2e05e9
Signed-off-by: Thomas Wolf <twolf@apache.org>
Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
2022-08-17 18:44:21 +02:00
Ronald Bhuleskar 6297491e8a Adds FilteredRevCommit that can overwrites its parents in the DAG.
Change-Id: I1ea63a3b56074099688fc45d6a22943a8ae3c2ae
2022-08-16 21:32:25 -07:00
Matthias Sohn ab3799be37 Merge changes Ib6689f54,I3b5c22ee
* changes:
  Remove unused API problem filters
  Add missing @since tag for RevCommit#parents introduced in 61b4d105e4
2022-08-16 17:00:43 -04:00
Thomas Wolf b255eb0fb6 DirCacheCheckout: load WorkingTreeOptions only once
Previous code loaded the WorkingTreeOptions afresh for every single
file being checked out. This checked the git config (all three files,
repo, user and system config) for having been modified every time.

These checks can be costly, for instance on Windows, or if one of the
three config files is not on a local disk, or on an otherwise slow
storage.

Improve this by loading the options and thus checking the git config
only once before the checkout.

Bug: 579715
Change-Id: I21cd5a808f9d90b5ca2d022f91f0eeb8ca26091c
Signed-off-by: Thomas Wolf <twolf@apache.org>
2022-08-14 21:33:19 +02:00
Thomas Wolf 134ee334fb WorkTreeUpdater: Fix unclosed streams
1. A TemporaryBuffer.LocalFile must be destroyed to ensure the
   temporary file gets deleted on disk.
2. TemporaryBuffer.openInputStream() may be used only after
   TemporaryBuffer.close().
3. The caller of DirCacheCheckout.getContent() is responsible for
   closing the OutputStream!

Change-Id: I46abb0fba27656a1026858e5783fc60d4738a45e
Signed-off-by: Thomas Wolf <twolf@apache.org>
2022-08-14 21:33:18 +02:00
Matthias Sohn 1745131add Merge "Fix API errors caused by 23a71696cd" 2022-08-14 05:43:39 -04:00
Thomas Wolf d9c0629ffe Fix adding symlinks to the index when core.symlinks=false
With core.symlinks=false, symlinks are checked out as plain files.
When such a file is re-added to the index, and the index already
contains a symlink there, add the file as a symlink. Previous code
changed the index entry to a regular file.

Bug: 580412
Change-Id: I5497bedc3da89c8b10120b8077c56bc5b67cb791
Signed-off-by: Thomas Wolf <twolf@apache.org>
2022-08-13 15:14:15 +02:00
Matthias Sohn 4bec6d98b8 Add missing @since tag for RevCommit#parents introduced in 61b4d105e4
Change-Id: I3b5c22eea7d0af5464c7df7a7cd855ea08029853
2022-08-12 10:38:57 +02:00
Matthias Sohn 2417c9f0e4 Fix API errors caused by 23a71696cd
- add missing @since 6.3 for new protected field workTreeUpdater and new
  class WorkTreeUpdater
- suppress API errors caused by removing/adding protected fields and
  methods

We follow OSGi semantic versioning which allows breaking implementers in
minor versions which are e.g. subclassing a public class.

Change-Id: I28f0d7b4fdd9a1f0fbc6b137d6c68dda9fe3c11e
2022-08-12 10:37:09 +02:00
Han-Wen NIenhuys 4d6c6df108 Merge changes Ideaefd51,I6c347393
* changes:
  Reapply "Create util class for work tree updating in both filesystem and index."
  ResolveMerger: add coverage for inCore file => directory transition
2022-08-09 05:10:53 -04:00
Ronald Bhuleskar 8542bf790e Provide default shallowCommits getter and setter in ObjectDatabase
I649db9ae679ec2606cf7c530b040f8b6b93eb81a added a default implementation
for getShallowCommits and setShallowCommits to DfsObjDatabase, for the
convenience of any implementers that define subclasses. But we forgot
that some implementers inherit from ObjectDatabase directly instead.
Move the default getter and setter to the base class so that such
callers do not need source changes to unbreak their build.

This also lets us update the api_filters to reflect that this is no
longer an API-breaking change.

Change-Id: I5dcca462eb306e511e57907b7d9264d51b3f3014
2022-08-08 18:30:00 -04:00
Nitzan Gur-Furman 23a71696cd Reapply "Create util class for work tree updating in both filesystem and index."
This reverts commit 5709317f71.

Add a bugfix for deletions in ResolveMergers instantiated with just an
ObjectInserter as argument.

Original change description:

Create util class for work tree updating in both filesystem and index.

This class intends to make future support in index updating easier.

This class currently extracts some logic from ResolveMerger. Logic
related to StreamSupplier was copied from ApplyCommand, which will be
integrated in a following change.

Co-authored-by: Nitzan Gur-Furman <nitzan@google.com>
Co-authored-by: Han-Wen Nienhuys <hanwen@google.com>
Change-Id: Ideaefd51789a382a8b499d1ca7ae0146d032f48b
2022-08-08 18:38:23 +02:00
Jonathan Nieder 5709317f71 Revert "Create util class for work tree updating in both filesystem and index."
This reverts commit 5151b324f4. It is
producing NullPointerExceptions during merges, causing Gerrit's
acceptance tests to fail:

com.google.gerrit.extensions.restapi.RestApiException: Cannot rebase ps
[...]
	at com.google.gerrit.server.api.changes.RevisionApiImpl.rebase(RevisionApiImpl.java:280)
	at com.google.gerrit.acceptance.api.change.ChangeIT.rebaseChangeBase(ChangeIT.java:1584)
Caused by: com.google.gerrit.server.update.UpdateException: java.lang.NullPointerException: repository is required
	at com.google.gerrit.server.update.BatchUpdate.executeUpdateRepo(BatchUpdate.java:588)
[...]
Caused by: java.lang.NullPointerException: repository is required
	at org.eclipse.jgit.merge.Merger.nonNullRepo(Merger.java:128)
	at org.eclipse.jgit.merge.ResolveMerger.addDeletion(ResolveMerger.java:380)
	at org.eclipse.jgit.merge.ResolveMerger.processEntry(ResolveMerger.java:553)
	at org.eclipse.jgit.merge.ResolveMerger.mergeTreeWalk(ResolveMerger.java:1224)
	at org.eclipse.jgit.merge.ResolveMerger.mergeTrees(ResolveMerger.java:1174)
	at org.eclipse.jgit.merge.ResolveMerger.mergeImpl(ResolveMerger.java:299)
	at org.eclipse.jgit.merge.Merger.merge(Merger.java:233)
	at org.eclipse.jgit.merge.Merger.merge(Merger.java:186)
	at org.eclipse.jgit.merge.ThreeWayMerger.merge(ThreeWayMerger.java:96)
	at com.google.gerrit.server.change.RebaseChangeOp.rebaseCommit(RebaseChangeOp.java:360)

Change-Id: Idf63de81666d0df118d2d93c4f6e014e00dc05b8
2022-08-05 18:38:56 -04:00
Ronald Bhuleskar 05a2485075 Provide a default implementation for set/get shallowCommits on DfsObjDatabase
Jgit change https://git.eclipse.org/r/c/jgit/jgit/+/193329 adds an implementation for get/set shallow commits in ObjectDatabase. This failed gerrit's acceptance tests since there is no default implementation for them in DfsObjDatabase.

Change-Id: I649db9ae679ec2606cf7c530b040f8b6b93eb81a
2022-08-05 17:25:54 -04:00
Terry Parker 1c7b4a580f Merge "Revert "Option to pass start RevCommit to be blamed on to the BlameGenerator."" 2022-08-03 18:38:05 -04:00
Terry Parker 7f89b0fa16 Merge "Revert "Adds FilteredRevCommit that can overwrites its parents in the DAG."" 2022-08-03 18:37:57 -04:00
Ronald Bhuleskar e5e46bc536 Revert "Adds FilteredRevCommit that can overwrites its parents in the DAG."
This reverts commit ceb51a5e0e.

Reason for revert: The change in https://git.eclipse.org/r/c/jgit/jgit/+/194354 broke the egit test [1]. Calling c.getShortMessage() causes an NPE.

[1] https://ci.eclipse.org/egit/job/egit.gerrit/2711/

Change-Id: I411565b6eaa0bbb562cc1c8a355942ff09fd29bc
2022-08-03 17:16:34 -04:00
Ronald Bhuleskar 3d2f087932 Revert "Option to pass start RevCommit to be blamed on to the BlameGenerator."
This reverts commit 59e8bec6e7.

Reason for revert: The change in https://git.eclipse.org/r/c/jgit/jgit/+/194354 broke the egit test [1]. Calling c.getShortMessage() causes an NPE.

[1] https://ci.eclipse.org/egit/job/egit.gerrit/2711/

Change-Id: Iaf5feb35f4bb4c3487b04be15d1fe11376975523
2022-08-03 17:14:18 -04:00
Thomas Wolf 8184683f7e CleanCommand: fix prefix matching
String.startsWith() is not a valid test for file path prefixes:
directory "a" is _not_ a prefix of a file "ab", only of "a/b".

Add a proper Paths.isEqualOrPrefix() method and use it in CleanCommand.

Bug: 580478
Change-Id: I6863e6ba94a8ffba6561835cc57044a0945d2770
Signed-off-by: Thomas Wolf <twolf@apache.org>
2022-08-03 07:42:27 +02:00
Ronald Bhuleskar 59e8bec6e7 Option to pass start RevCommit to be blamed on to the BlameGenerator.
This can allow passing a FilteredRevCommit which is the filtered list of
commit graph making it easier for Blame to work on. This can
significantly improve blame performance since blame can skip expensive
RevWalk.

Change-Id: I5dab25301d6aef7df6a0bc25a4c553c730199272
2022-08-02 21:02:33 -04:00
Ronald Bhuleskar ceb51a5e0e Adds FilteredRevCommit that can overwrites its parents in the DAG.
Change-Id: I2df9843dde0f589f5fea6cedaaff52e313eea6de
2022-08-02 20:25:58 -04:00
Ronald Bhuleskar 61b4d105e4 Add the ability to override parents on RevCommit.
This makes RevCommit extensible to allow having different structure of
child-parent relationship. This change is a pre-requsite for having a
FilteredRevCommit that overrides parents from the RevCommit. That then
provides a cheaper way to walk over a subset of RevCommits instead of
an expensive way that applies filters while walking over selected
commits. Useful with Blame which works on a single file and that can be
made performant, if we know all the commits needed by the Blame
algorithm. So Blame algorithm can avoid walking over finding what
commits to blame on.

This change makes parents field on RevCommit private and exposes it
thrrough overrideable methods such as getParents, getParent at index,
getParentCount and setParents. All other files other than RevCommit are
updating the usages of accessing RevCommits parents.

Change-Id: I2d13b001c599cc4ebc92d1ab6e07b07acb3b7fe5
2022-08-02 10:50:16 -07:00
Dmitrii Filippov b544da795b Refactor NameConflictTreeWalk.fastMin method
Change-Id: Iac2e6f615463e18ddf788e6ddfe15ef023cac977
2022-08-01 13:31:26 -04:00
Dmitrii Filippov f8e229d569 Rename fastMinHasMatch to allTreesNamesMatchFastMinRef
Change-Id: I2d9165616650e9d44745c6848d2cf1045f53f33c
2022-08-01 13:31:07 -04:00
Han-Wen NIenhuys e030b106c5 Merge "Document TreeWalk#min()" 2022-08-01 13:07:29 -04:00
Han-Wen NIenhuys 688c83d93a Merge "NameConflictTreeWalk: respect git order on multi-tree iteration" 2022-08-01 11:25:10 -04:00
Han-Wen Nienhuys 8a3a6a99af Document TreeWalk#min()
Change-Id: I29a6c023929d8270a8cdd1e7f012817a06428f42
Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
2022-08-01 17:21:49 +02:00
Thomas Wolf eef4da5dac Use constants for git packet protocol line identifiers
Introduce named constants for packet line headers and use them instead
of direct string literals everywhere. This not only makes the code more
readable because we don't need NON-NLS markers, it also makes it more
robust since we can use the length of these constants instead of magic
numbers.

Change-Id: Ie4b7239e0b479a68a2dc23e6e05f25061d481a31
Signed-off-by: Thomas Wolf <twolf@apache.org>
2022-07-31 14:08:48 +02:00
Robin Müller 673007d529 ObjectDirectory: improve reading of shallow file
Use FileUtils.readWithRetries().

Change-Id: I5929184caca6b83a1ee87b462e541620bd68aa90
2022-07-31 14:08:48 +02:00
Robin Müller 207dd4c938 Fetch: add support for shallow
This adds support for shallow cloning. The CloneCommand and the
FetchCommand now have the new methods setDepth, setShallowSince and
addShallowExclude to tell the server that the client doesn't want to
download the complete history.

Bug: 475615
Change-Id: Ic80fb6efb5474543ae59be590ebe385bec21cc0d
2022-07-31 14:08:47 +02:00
Dmitrii Filippov 8584ac7048 NameConflictTreeWalk: respect git order on multi-tree iteration
The NameConflictTreeWalk class is used in 3-way merge for iterating over
entries in 3 different commits. The class provides information about a
current entry and a state of the entry in commits (e.g entry is file,
entry is directory, entry is missing). In rare cases, the tree walker
can mix information about entries with different name.

The problem appears, because git uses unusual sorting order for
files. Example (this is a simplified real-life example):
Commit 1:
* gradle.properties - file
* gradle - directory (with nested files)
*   gradle/file - file in gradle directory
Commit 2:
* gradle.properties - file
* no entry with the name gradle
Commit 3:
* gradle.properties - file
* gradle - file
Here the names are ordered like this:
"gradle" file <"gradle.properties" file < "gradle/file" file.

NameConflictTreeWalk iterator already have code for processing
git sorting order, however in the example above the code doesn't
work correctly. Before the fix, NameConflictTreeWalk returns:
#next()
"gradle - directory" | "gradle.properties" | "gradle - file" - which is
wrong. The expected result is
#next()
"gradle - directory | MISSED_FILE | "gradle - file"
#next()
"gradle.properties"|"gradle.properties"|"gradle.properties"

Ensure that the "matches" field of tree iterators (which contains the
current path) is kept in sync in the case above.

Change-Id: Ief5aa06d80b358f4080043c8694aa0fd7c60045b
Signed-off-by: Dmitrii Filippov <dmfilippov@google.com>
2022-07-29 19:35:22 +02:00
Han-Wen NIenhuys 800a4dc20c Merge "Create util class for work tree updating in both filesystem and index." 2022-07-25 09:44:28 -04:00
Nitzan Gur-Furman 5151b324f4 Create util class for work tree updating in both filesystem and index.
This class intends to make future support in index updating easier.

This class currently extracts some logic from ResolveMerger. Logic
related to StreamSupplier was copied from ApplyCommand, which will be
integrated in a following change.

Change-Id: I8dc5a582433fc9891038c628385d3970b5a8984b
2022-07-25 14:29:46 +03:00
Thomas Wolf a1ce9063fb Fix the handling of .git/info/exclude and core.excludesFile
The RootIgnoreNode in a WorkingTreeIterator must _not_ add the rules
from .git/info/exclude or from the file designated by git config
core.excludesFile to the list of rules read from the root .gitignore.
These really must be separate nodes in a hierarchy, otherwise the
precedence rules from [1] are violated and the outcome is not the
same as in C git.

[1] https://git-scm.com/docs/gitignore

Bug: 580381
Change-Id: I57802ba7bbbe4f183504c882b6c77a78cc3a9b99
Signed-off-by: Thomas Wolf <twolf@apache.org>
2022-07-15 21:00:16 +02:00
Matthias Sohn f6935d8cd2 Merge branch 'stable-6.2'
* stable-6.2:
  UploadPack: don't prematurely terminate timer in case of error
  Do not create reflog for remote tracking branches during clone
  UploadPack: do not check reachability of visible SHA1s
  Add missing package import javax.management to org.eclipse.jgit

Change-Id: Ia2280b848cea7bdfa5bc48356484e040d159845d
2022-07-06 17:02:58 +02:00
Matthias Sohn 34600e3ca2 Merge branch 'stable-6.1' into stable-6.2
* stable-6.1:
  UploadPack: don't prematurely terminate timer in case of error
  Do not create reflog for remote tracking branches during clone
  UploadPack: do not check reachability of visible SHA1s
  Add missing package import javax.management to org.eclipse.jgit

Change-Id: Ib3ff6a038e1b92a931a7f2624c19bbefd8cb95fd
2022-07-06 17:02:04 +02:00
Matthias Sohn d01376106a Merge branch 'stable-6.0' into stable-6.1
* stable-6.0:
  UploadPack: don't prematurely terminate timer in case of error
  Do not create reflog for remote tracking branches during clone
  UploadPack: do not check reachability of visible SHA1s
  Add missing package import javax.management to org.eclipse.jgit

Change-Id: I08734ee2c8f3296d908da6a29d53ed87c4b48eb2
2022-07-06 17:00:42 +02:00
Matthias Sohn 9f7d77b608 Merge branch 'stable-5.13' into stable-6.0
* stable-5.13:
  UploadPack: don't prematurely terminate timer in case of error
  Do not create reflog for remote tracking branches during clone
  UploadPack: do not check reachability of visible SHA1s
  Add missing package import javax.management to org.eclipse.jgit

Change-Id: I6db0a4d74399fde892eeec62efd2946f97547a5d
2022-07-06 16:59:30 +02:00
Simeon Andreev 1a364c49ec JGit blame very slow for large merge commits that rename files
Adjusted BlameGenerator to filter rename detection with the blame path.
This reduces the running time of the blame computation significantly,
for repositories with massive commits involving renames.

The filtered rename detection is made (internally) available with:
org.eclipse.jgit.internal.diff.FilteredRenameDetector

Bug: 578900
Change-Id: I6580004e81102d685081b8180da1587a35073d36
Signed-off-by: Simeon Andreev <simeon.danailov.andreev@gmail.com>
2022-07-01 16:10:13 +02:00
Matthias Sohn 035e0e23f2 UploadPack: don't prematurely terminate timer in case of error
In uploadWithExceptionPropagation don't prematurely terminate timer in
case of error to enable reporting it to the client. Expose a close
method so that callers can terminate it at the appropriate time.

If the timer is already terminated when trying to report it to the
client this failed with the error java.lang.IllegalStateException:
"Timer already terminated".

Bug: 579670
Change-Id: I95827442ccb0f9b1ede83630cf7c51cf619c399a
2022-06-30 14:45:31 +02:00
Matthias Sohn ca6b518432 Merge "Do not create reflog for remote tracking branches during clone" into stable-5.13 2022-06-26 15:36:03 -04:00
Luca Milanesio 4bb4693633 Do not create reflog for remote tracking branches during clone
When using JGit on a non-bare repository, the CloneCommand
it previously created local reflogs for all branches including remote
tracking ones, causing the generation of a potentially large
number of files on the local filesystem.

The creation of the remote-tracking branches (refs/remotes/*) during
clone is not an issue for the local filesystem because all of them are
stored in a single packed-refs file. However, the creation of a large
number of ref logs on a local filesystem IS an issue because it
may not be tuned or initialised in term of inodes to contain a very
large number of files.

When a user (or a CI system) performs the CloneCommand against
a potentially large repository (e.g., millions of branches), it is
interested in working or validating a single branch or tag and is
unlikely to work with all the remote-tracking branches.
The eager creation of a reflogs for all the remote-tracking branches is
not just a performance issue but may also compromise the ability to
use JGit for cloning a large repository.

The behaviour implemented in this change is also consistent with the
optimisation done in the C code-base [1].

We differentiate between clone and fetch commands using --branch
<initialBranch> option, that is only available in clone command,
and is set as HEAD per default.

[1] 58f233ce1e

Bug: 579805
Change-Id: I58d0d36a8a4ce42e0f59b8bf063747c4b81bd859
Signed-off-by: Luca Milanesio <luca.milanesio@gmail.com>
2022-06-25 12:09:01 +01:00
Luca Milanesio 66ace4b9af UploadPack: do not check reachability of visible SHA1s
When JGit needs to serve a Git client requesting SHA1s
during the want phase, it needs to make a full reachability
check from the advertised refs to the ones requested to
keep all objects in the correct scope of confidentiality
allowed by the avertised refs.

The check is also performed when the SHA1 corresponds to
one of the tips of the advertised refs which is a waste of
resources.

Example:

fetch> ref-prefix refs/heads/foo
fetch< 900505eb8ce8ced2a1757906da1b25c357b9654e refs/heads/foo
fetch< 0000
fetch> command=fetch
fetch> 0001
fetch> thin-pack
fetch> ofs-delta
fetch> want 900505eb8ce8ced2a1757906da1b25c357b9654e

The SHA1 in the want is the tip of refs/heads/foo and therefore
the full reachability check can be shortened and resolved more
quickly.

Change-Id: I49bd9e2464e0bd3bca2abf14c6e9df550d07383b
Signed-off-by: Luca Milanesio <luca.milanesio@gmail.com>
2022-06-25 06:45:05 -04:00
Thomas Wolf ba93cd36d8 Fix warnings about non-externalized string literals
Four occurrences in RefSpec marked with //$NON-NLS-<n>$.

Change-Id: Ie603a58c41357562f0739445e3fe77ca87a3eb54
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-06-23 20:34:35 +02:00
Simeon Andreev 0c32889ebc Run tests that checks araxis output only on Linux
Bug: 580189
Change-Id: Ieb14f5cf061fcb468b602c7f27e27b672e3b09e2
Signed-off-by: Simeon Andreev <simeon.danailov.andreev@gmail.com>
2022-06-17 14:39:48 +02:00
David Ostrovsky 87391ccee9 Fix DefaultCharset bug pattern flagged by error prone
See more details in: [1].

[1] https://errorprone.info/bugpattern/DefaultCharset
Change-Id: I3de0be57a2d74490a5b4e66801e9767b38f13bf9
2022-06-17 09:44:14 +02:00
Matthias Sohn d1e173ba40 Merge "Merge branch 'stable-6.2'" 2022-06-17 03:35:40 -04:00
Ronald Bhuleskar 7f4fa5655a Annotate the exception with the possible failure reason when Bitmaps are not enabled.
When bitmaps are not enabled the RevWalk is expensive. AllowFilter
provides an override to continue walking. PedestrianWalk can be
expensive and fails when the clone is a partial clone using
--filter=tree:0. This can only work when bitmaps are enabled.

Having a message to the exception improves debuggability of such cases.

Change-Id: Ie61de20a39a765e6f6f77e81b4c3bbda2eefbaf3
2022-06-15 16:42:39 -04:00
Matthias Sohn c67d951a0a Merge branch 'stable-6.2'
* stable-6.2:
  Prepare 5.13.2-SNAPSHOT builds
  JGit v5.13.1.202206130422-r
  AmazonS3: Add support for AWS API signature version 4

Change-Id: I13514595f3a710b0d6e3c2de62f6508b950d22f9
2022-06-15 17:40:31 +02:00
Matthias Sohn 58f5302e1d Merge branch 'stable-6.1' into stable-6.2
* stable-6.1:
  Prepare 5.13.2-SNAPSHOT builds
  JGit v5.13.1.202206130422-r
  AmazonS3: Add support for AWS API signature version 4

Change-Id: Id4965aacd4e2ea1e8575a2c1bd4845729db6049a
2022-06-15 17:39:52 +02:00
Matthias Sohn d0bc2b544a Merge branch 'stable-6.0' into stable-6.1
* stable-6.0:
  Prepare 5.13.2-SNAPSHOT builds
  JGit v5.13.1.202206130422-r
  AmazonS3: Add support for AWS API signature version 4

Change-Id: Ie9c38ab8033fe1283e8b444b6acd3f4298062bf3
2022-06-15 16:32:08 +02:00
Matthias Sohn d961bb6502 Merge branch 'stable-5.13' into stable-6.0
* stable-5.13:
  Prepare 5.13.2-SNAPSHOT builds
  JGit v5.13.1.202206130422-r
  AmazonS3: Add support for AWS API signature version 4

Change-Id: Ibd663a1d874d1aac274abc3dd44354fd99f64c39
2022-06-15 16:31:38 +02:00
eric.steele e9a5430c25 AmazonS3: Add support for AWS API signature version 4
Updating the AmazonS3 class to support AWS Signature version 4 because
version 2 is no longer supported in all AWS regions. The version can be
selected with the new 'aws.api.signature.version' property (defaults to
2 for backwards compatibility). When set to '4', the user must also
specify the AWS region via the 'region' property. The 'region' property
must match the region that the 'domain' property resolves to.

Bug: 579907
Change-Id: If289dbc6d0f57323cfeaac2624c4eb5028f78d13
2022-06-13 09:44:23 +02:00
Matthias Sohn 5868543f17 Fix typo in DiffTools#compare javadoc
Change-Id: Ib9f8453b3b948b098d433e1b1426c32b4b4f5af8
2022-06-13 00:58:47 +02:00
Matthias Sohn 4e2651e538 Merge branch 'stable-6.1' into stable-6.2
* stable-6.1:
  Fix connection leak for smart http connections

Change-Id: I4d38c62fa5416fd40b699de5b0ecfa03d498c21d
2022-06-07 11:36:46 +02:00
Matthias Sohn 1bd087a7f5 Merge branch 'stable-6.0' into stable-6.1
* stable-6.0:
  Fix connection leak for smart http connections

Change-Id: I8b7e643b87c0f788cdafb060a2c9e8b4ef6f7375
2022-06-07 11:36:11 +02:00
Matthias Sohn a96645a5f3 Merge branch 'stable-5.13' into stable-6.0
* stable-5.13:
  Fix connection leak for smart http connections

Change-Id: Ic851f2c4660ed761f5527e405b116b54da42fb7c
2022-06-07 11:35:13 +02:00
Matthias Sohn 5efd32e91d Merge branch 'stable-5.12' into stable-5.13
* stable-5.12:
  Fix connection leak for smart http connections

Change-Id: Id34f29c1b27a80c2b56c911cad7e3f64ef63af48
2022-06-07 11:34:25 +02:00
Matthias Sohn 8bb17e518f Merge branch 'stable-5.11' into stable-5.12
* stable-5.11:
  Fix connection leak for smart http connections

Change-Id: I6caabf4774ccf34706cef846c1087710f67e2ecd
2022-06-07 11:33:38 +02:00
Matthias Sohn c7335f32e9 Merge branch 'stable-5.10' into stable-5.11
* stable-5.10:
  Fix connection leak for smart http connections

Change-Id: I3885c6114caed897f762f5ce523d3b27288205b2
2022-06-07 10:53:24 +02:00
Matthias Sohn 85011b8b07 Merge branch 'stable-5.9' into stable-5.10
* stable-5.9:
  Fix connection leak for smart http connections

Change-Id: I5e7144b2f5cd850978220c476947001ae2debb8e
2022-06-07 10:42:22 +02:00
Saša Živkov 011c26ff36 Fix connection leak for smart http connections
SmartHttpPushConnection: close InputStream and OutputStream after
processing. Wrap IOExceptions which aren't TransportExceptions already
as a TransportException.

Also-By: Matthias Sohn <matthias.sohn@sap.com>
Change-Id: I8e11d899672fc470c390a455dc86367e92ef9076
2022-06-06 08:14:18 +02:00
Andre Bossert c32694e5ae Teach JGit to handle external diff/merge tools defined in .gitattributes
Adds API that allows UI to find (and handle) diff/merge tools, specific
for the given path. The assumption is that user can specify file type
specific diff/merge tools via gitattributes.

Bug: 552840
Change-Id: I1daa091e9afa542a9ebb5417853dff0452ed52dd
Signed-off-by: Mykola Zakharchuk <zakharchuk.vn@gmail.com>
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
Signed-off-by: Andre Bossert <andre.bossert@siemens.com>
2022-06-02 10:36:39 +02:00
Andre Bossert ff77d412a9 Adapt diff- and merge tool code for PGM and EGit usage
see: https://git-scm.com/docs/git-mergetool

* DiffTools and MergeTools
  * store FS, gitDir and workTree for usage without
git repository (for EGit preferences)
  * add getUserDefinedToolNames() and getPredefinedToolNames()
  * replace getToolNames() with getAllToolNames() that combines the two
lists and put default tool name (diff.tool or merge.tool) as first
element (for EGit preferences)
* FileElement: refactoring of getFile() and friends to have midName
(LOCAL, REMOTE etc.) always added to the temp file name (also for EGit)
* FileElement: added directory attribute that is used in getFile() to
return path with workDir as parent
* DiffTool and MergeTool
  * added errw.flush(), because sometimes stderr is not printed in case
of die()
  * print e.getMessage() always to stderr
* Moved toolname and prompt logic into managers
* Exported internal packages required for egit.ui

Bug: 356832
Change-Id: I71e7f4dc362169a7612ca4f6546a021bc4b2b5f4
Signed-off-by: Andre Bossert <andre.bossert@siemens.com>
Signed-off-by: Tim Neumann <Tim.Neumann@advantest.com>
2022-06-02 10:36:31 +02:00
Andre Bossert 973e955ead Add availability check of pre-defined tools
see: https://git-scm.com/docs/git-difftool
see: https://git-scm.com/docs/git-mergetool

* now all available tools are printed with "--tool-help"
* if no diff.tool or merge.tool is defined the first available
pre-defined tool is used

TODO:
- add mergetools to difftools --> extra change or merge to this
- return the exit-code of the tool to jgit / java runtime

Bug: 356832
Change-Id: I20fb04e71ced981f5625020f461bbac24e6cec70
Signed-off-by: Andre Bossert <andre.bossert@siemens.com>
2022-06-01 14:23:48 +02:00
Andrey Loskutov bb30be6b33 Merge "Add filtering with help of DirCacheCheckout.getContent()" 2022-05-30 12:42:20 -04:00
Andrey Loskutov 8c681aac50 Fixed since tags added for new merge constants
Bug: 356832
Change-Id: I70197522675de0e9b53399ee3d9ed3f10312ff6b
2022-05-30 16:34:30 +02:00
Andre Bossert e81085944f Add filtering with help of DirCacheCheckout.getContent()
see: https://git-scm.com/docs/git-mergetool

* refactoring of content (FileElement) handling
* now the temporary files are already filled with filtered content in
the calling classes (PGM), that can be used with EGit content too

TODO:
 * keep the temporaries when no change detected and the user answers no
to the question if the merge was successful

Bug: 356832
Change-Id: I86a0a052d059957d4d152c1bb94c262902c377d2
Signed-off-by: Andre Bossert <andre.bossert@siemens.com>
2022-05-30 13:28:32 +02:00
Matthias Sohn 29dce2e7be Merge branch 'stable-6.0' into stable-6.1
* stable-6.0:
  Remove stray files (probes or lock files) created by background threads

Change-Id: Iab21b9776c2ebecd4cad5d015028cadd4b6541db
2022-05-27 16:22:09 +02:00
Matthias Sohn 9612aae885 Merge branch 'stable-5.13' into stable-6.0
* stable-5.13:
  Remove stray files (probes or lock files) created by background threads

Change-Id: I7af1355a77f14995118145162f6bb8a4f1755f2b
2022-05-27 16:20:28 +02:00
Andrey Loskutov d2489ffa70 Merge "Add mergetool merge feature (execute external tool)" 2022-05-27 01:02:15 -04:00
Andrey Loskutov 5828ca1602 Merge "Add command line support for "git mergetool"" 2022-05-27 00:59:22 -04:00
Andrey Loskutov fde3e8b5d8 Merge "Add config reader for user-defined mergetools" 2022-05-27 00:58:42 -04:00
Andrey Loskutov 2b80dd968c Merge "Add difftool compare feature (execute external tool)" 2022-05-27 00:57:51 -04:00
James Z.M. Gao d67ac798f1 Remove stray files (probes or lock files) created by background threads
NOTE: port back from master branch.

On process exit, it was possible that the filesystem timestamp
resolution measurement left behind .probe files or even a lock file
for the jgit.config.

Ensure the SAVE_RUNNER is shut down when the process exits (via
System.exit() or otherwise). Move lf.lock() into the try-finally
block when saving the config file.

Delete .probe files on JVM shutdown -- they are created in daemon
threads that may terminate abruptly, not executing the "finally"
clause that normally removes these files.

Bug: 579445
Change-Id: Iaee2301eb14e6201406398a90228ad10cfea6098
2022-05-27 01:20:16 +02:00
Andre Bossert eaf4d500b8 Add mergetool merge feature (execute external tool)
see: https://git-scm.com/docs/git-mergetool

* implement mergetool merge function (execute external tool)
* add ExecutionResult and commandExecutionError to ToolException
* handle "base not present" case (empty or null base file path)
* handle deleted (rm) and modified (add) conflicts
* handle settings
 * keepBackup
 * keepTemporaries
 * writeToTemp

Bug: 356832
Change-Id: Id323c2fcb1c24d12ceb299801df8bac51a6d463f
Signed-off-by: Andre Bossert <andre.bossert@siemens.com>
2022-05-25 13:52:04 +02:00
Andre Bossert 8573435635 Add command line support for "git mergetool"
see: https://git-scm.com/docs/git-mergetool
see: https://git-scm.com/docs/git-config

* add command line support for "git mergetool"
  * add option handling for "--tool-help", "--tool=<mytool>",
"--[no-]prompt",  "--[no-]gui"
  * handle prompt
  * add MergeTools
  * add pre-defined mergetools
  * print merge actions --> no execute, will be done later

Bug: 356832
Change-Id: I6e505ffc3d03f75ecf4bba452a25d25dfcf5793f
Signed-off-by: Andre Bossert <andre.bossert@siemens.com>
2022-05-25 13:52:04 +02:00
Andre Bossert 24171b05f0 Add config reader for user-defined mergetools
see: https://git-scm.com/docs/git-mergetool
see: https://git-scm.com/docs/git-config

* add config reader for user-defined mergetools
  * merge.tool
  * merge.guitool
  * mergetool.prompt
  * mergetool.keepBackup
  * mergetool.keepTemporaries
  * mergetool.writeToTemp
  * mergetool.<tool>.path
  * mergetool.<tool>.cmd
  * mergetool.<tool>.trustExitCode

Bug: 356832
Change-Id: Ic4f07376630713d8e06cbad284d9c72c9ecc0405
Signed-off-by: Andre Bossert <andre.bossert@siemens.com>
2022-05-25 13:50:50 +02:00
Andre Bossert b63c2f39a1 Add difftool compare feature (execute external tool)
see: http://git-scm.com/docs/git-difftool

* add CommandExecutor that handles tool execution with help of "jgit.FS"
  * it handles tool execution with temporary created "command file" -->
for for all "command interpreters" and parameters with spaces etc.
  * using of external bash.exe at Windows (MinGW) if shell-script is
used as difftool command. It can be enabled with parameter
"jgit.usemsys2bash=auto" that checks if command contains ".sh" or
enabled / disabled with "jgit.usemsys2bash=true|false"
* added special handling for empty files (e.g. deleted, added etc.) that
are named "/dev/null"
* added creation and deletion of temporary files needed for compare
* added own Exception class for reporting to pgm / command line / EGit
* added prompt option handling before executing difftool
* reworked trustExitCode option for specific difftool and override for
all difftools from config and command line
* tested with command line options "--[no]-trust-exit-code",
"--tool=<toolname>", "--[no]-gui", --[no]-prompt
* ContentSource
  * added close() methods to close / cleanup used resources
(like ObjectReader TreeWalk etc.)
  * added isWorkingTreeSource() methods to check if file can be used
from working tree instead of copy from "ObjectLoader / ObjectReader" to
temporary file (fixes "difftool <commit> <commit>")

Bug: 356832
Change-Id: I5462fb6dbe4ecfd9da7c74117fce4070bbfd4d7a
Signed-off-by: Andre Bossert <andre.bossert@siemens.com>
Signed-off-by: Simeon Andreev <simeon.danailov.andreev@gmail.com>
2022-05-25 13:23:50 +02:00
Simon Sohrt ca62b3447b RewriteGenerator: Fully buffering of input is no longer necessary
Fully buffering by the previous generator of the input for the RewriteGenerator is no longer necessary.

Bug: 577948
Signed-off-by: Simon Sohrt <sohrt@his.de>
Change-Id: I59c7a7c7f3766e97627764608bc8dc733804274c
2022-05-25 05:49:28 -04:00
Fabio Ponciroli 4ac6e7b505 PackFileSnapshot: Resolve warnings by using "equals" instead of "=="
Errorprone raises the following warning: "[ReferenceEquality] Comparison
using reference equality instead of value equality"

Added Equality#isSameInstance util method to be reused in similar cases.

Change-Id: Ifc9885c9806dcafa1c5ee1351d3095bf1517dbf5
2022-05-20 20:12:29 +02:00
James Z.M. Gao 88d5f51e61 Remove stray files (probes or lock files) created by background threads
On process exit, it was possible that the filesystem timestamp
resolution measurement left behind .probe files or even a lock file
for the jgit.config.

Ensure the SAVE_RUNNER is shut down when the process exits (via
System.exit() or otherwise). Move lf.lock() into the try-finally
block when saving the config file.

Delete .probe files on JVM shutdown -- they are created in daemon
threads that may terminate abruptly, not executing the "finally"
clause that normally removes these files.

Bug: 579445
Change-Id: Ie27aca1fdaddfa487ebe072c3913d78355910df8
2022-05-15 19:41:24 +02:00
Thomas Wolf 563a7b5828 RefSpec: add missing @since
New API needs an @since 6.2 annotation.

Change-Id: I16b01ea820cc651605166acc13591b515541d06b
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-04-25 07:45:55 +02:00
Thomas Wolf 7f9cd7d2ba De-couple ServiceLoader calls from class loading
Use the holder pattern to de-couple the loading of super classes from
the ServiceLoader calls to set up global instances. This prevents
potential lock inversions.

Bug: 579550
Change-Id: Ie8284e4d6d680ddd4cc6a486bbefe8ed00266240
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-04-14 10:52:55 +02:00
Thomas Wolf 7e06e51750 CommitCommand: call GpgSigner.getDefault() only if indeed signing
If we're not signing the commit, we don't need the GpgSigner at all.

Change-Id: Ica35651c73d82b7604c8a67487b0d9474c908aae
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-04-14 10:52:55 +02:00
Thomas Wolf fbce3122e0 GpgSigner: prevent class lock inversion on the default signer
Don't store the default signer in a static field of the abstract
superclass GpgSigner. This many lead to a lock inversion on the class
initialization locks if there are concurrent loads of the GpgSigner
class and of one of its subclasses, and that subclass happens to be
the one the ServiceLoader wants to load.

Use the holder pattern to de-couple the loading of class GpgSigner
from the ServiceLoader call.

Bug: 579550
Change-Id: Ifac0ea0c8985a09fe0518d0dabc072fafd6db907
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-04-14 10:52:55 +02:00
yunjieli eca101fc05 Fetch: Introduce negative refspecs.
Implement negative refspecs in JGit fetch, following C Git. Git
supports negative refspecs in source only while this change supports
them in both source and destination.

If one branch is equal to any branch or matches any pattern in the
negative refspecs collection, the branch will not be fetched even if
it's in the toFetch collection.

With this feature, users can express more complex patterns during fetch.

Change-Id: Iaa1cd4de5c08c273e198b72e12e3dadae7be709f
Sign-off-by: Yunjie Li<yunjieli@google.com>
2022-04-13 10:21:20 -07:00
Fabio Ponciroli 98029e6758 BaseSearch: Suppress ReferenceEquality Warnings
Errorprone raises the following warning:
"[ReferenceEquality] Comparison using reference equality
instead of value equality"

Change-Id: Ib7542506dcdf5a0c7775a99a2c0972930d622879
2022-04-02 04:18:54 -04:00
Fabio Ponciroli 47a16f5f22 RewriteGenerator: Suppress ReferenceEquality Warnings
Errorprone raises the following warning:
"[ReferenceEquality] Comparison using reference equality
instead of value equality"

Change-Id: Idfe3f044730019fb49dccee25a0563c6fc01fc6a
2022-04-01 17:41:56 -04:00
Fabio Ponciroli cc8b2e627b PlotCommit: Suppress ReferenceEquality Warnings
Errorprone raises the following warning:
"[ReferenceEquality] Comparison using reference equality
instead of value equality"

Change-Id: Ia0dc2df68c77e40ff509a2c39568abce92525ee9
2022-04-01 17:27:42 -04:00
Fabio Ponciroli f1362e857e PlotCommitList: Suppress ReferenceEquality Warnings
Errorprone raises the following warning:
"[ReferenceEquality] Comparison using reference equality
instead of value equality"

A single pointer comparison is fine here.
A comparison with 'equals' would complicate the code and
also impact performance.

Change-Id: I6677dfc483acb95a64afbf433fd2b9460e408264
2022-04-01 17:07:19 -04:00
Thomas Wolf 8f02807164 Use git config core.commentChar
This concerns committing, creating merge conflict messages and creating
and editing squash messages. In a squash message, once the comment
character has been determined initially is always the first character.
Note that if core.commentChar=auto and there is a sequence of squashes,
it may be necessary to change the comment character when a new message
is added.

Bug: 579325
Change-Id: Idca19284a0240cd322e7512ea299a03658e1b2c1
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-03-26 19:53:37 +01:00
Thomas Wolf a187d12dd9 CommitConfig: add support for core.commentChar
Provide  access to the core.commentChar git config, and provide a
utility method to determine an unused comment character if the setting
is "auto".

Bug: 579325
Change-Id: I1ec7e4deffea6ac5929a8538a624d73bb59e4ecc
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-03-26 19:53:37 +01:00
Thomas Wolf a171360292 [push, lfs] Tell the pre-push hook whether the push is a dry run
This is a feature that does not exist in C git: an external pre-push
hook doesn't know whether the push is run as a dry run. But for
internal hooks written in Java it is entirely possible to give a hook
this information.

In JGit with its internal LFS implementation, this enables us to not
perform LFS uploads in a dry run. This is kind of important because
EGit frequently does a dry-run before doing the actual push to give the
user a way to review what would be pushed before it actually happens.

Doing an LFS upload of potentially huge files during a dry-run is
wasteful, makes the dry run not actually a dry run, and leads to
uploading the same file twice if followed by a real push.

Use the information in the LfsPrePushHook to only do the initial call
to the LFS server, but then skipping the actual upload if the push is
a dry run. That way, a failure to contact the LFS server leads to an
error in the dry run, as it should.

Bug: 546567
Change-Id: I155430f27c4979d91096ba72fd95c3775dd3f28b
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-03-23 18:55:31 -04:00
Matthias Sohn 30a137dfe5 Merge changes I5df54a92,Iecc61d1c
* changes:
  LsRemoteCommand: apply url.*.insteadOf
  Factor out URL replacement from RemoteConfig
2022-03-22 12:15:33 -04:00
Thomas Wolf 0ae37d4000 DirCacheCheckout: use a LinkedHashMap instead of HashMap
This guarantees that updates are checked out in git order, which
is important for LFS if a .lfsconfig file is used. That file comes
early in git order, and the LFS smudge filter will consider the
working tree version. To ensure that on branch switches the correct
version of that file is used, the checkout order must be stable and
should be the git order.

Change-Id: I20f6d11bf08558f9d5adfd2be71e36321460038c
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-03-19 21:41:55 +01:00
Thomas Wolf 7b1c8cf147 Re-try reading a file when there are concurrent writes
Git and JGit are very careful to replace git files atomically when
writing. The normal mechanism for this is to write to a temporary
file and then to rename it atomically to the final destination. This
works fine on POSIX-compliant systems, but on systems where renaming
may not be atomic, exceptions may be thrown if code tries to read
the file while the rename is still ongoing. This happens in particular
on Windows, where the typical symptom is that a FileNotFoundException
with message "The process cannot access the file because it is being
used by another process" is thrown, but file.isFile() == true at the
same time.

In FileBasedConfig, a re-try was already implemented for this case.
But the same problem can also occur in other places, for instance
in RefDirectory when reading loose or packed refs. Additionally,
JGit has similar re-tries when a stale NFS file handle is detected,
but that mechanism wasn't used consistently (only for git configs
and packed refs, but not for loose refs).

Factor out the general re-try mechanism for reading into a new method
FileUtils.readWithRetry() and use that in all three places. The
re-try parameters are hardcoded: at most 5 times for stale NFS handles,
and at most 5 times with increasing backoff delays (50, 100, 200, 400,
and 800ms) for the above concurrent write case.

Bug: 579116
Change-Id: If0c2ad367446d3c0f32b509274cf8e814aca12cf
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-03-19 21:33:51 +01:00
Thomas Wolf ac78c17523 [sideband] Ensure last bit of progress channel is written
If the last sideband progress message didn't end in \r or \n, there
may still be a buffered message at the end of a fetch or push. Ensure
that message gets written, too, even if it may be only partial.

Bug: 575629
Change-Id: I38edccb5cffb89e00e468480b43c7d951fb63e8e
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-03-19 12:13:37 +01:00
Thomas Wolf 2c1a8798ca LsRemoteCommand: apply url.*.insteadOf
If LsRemoteCommand is used without repository, the remote URI was not
translated. If the git user config contains e.g.

  [url "ssh://git@github.com/"]
    insteadOf = https://github.com/

and LsRemoteCommand is called with an HTTPS GitHub repository URL, the
command should actually rewrite this to an SSH URI and use the SSH
transport.

Actually this same problem may exist everywhere Transport is used with
an URIish instead of with a remote name. However, doing this translation
in Transport.open(URIish) and in Transport.open(Repository, URIish,
String) if no remote name is given would change the behavior and might
break assumptions made in existing clients. For instance, EGit's
PushOperation assumes that the URI obtained from PushResult.getURI()
was the same as was passed in to Transport.open(Repository, URIish).

URIs obtained from a RemoteConfig have this translation applied
transparently, and in Transport we cannot know for sure whether or
not a URI has already been translated, if needed. So doing this in
Transport might also lead to translating URIs twice.

Therefore this commit does the translation in LsRemoteCommand, where
we can be sure that it won't affect other use cases. If other cases
besides LsRemoteCommand are found where such a URI translation is
missing, it'll have to be done at higher levels, possibly even in
client code directly.

Bug: 544769
Change-Id: I5df54a925d30b55d98e21f37f2851fe79649b064
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-03-15 20:58:52 +01:00
Thomas Wolf c127a1a652 Factor out URL replacement from RemoteConfig
Add a new UrlConfig that encapsulates the basic URL replacement logic
for git configs url.<base>.insteadOf and pushInsteadof.

Change-Id: Iecc61d1c5e6089533552afa1d1e684ae72393b38
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-03-15 20:43:51 +01:00
Thomas Wolf f26ab4ebee [checkout] Use .gitattributes from the commit to be checked out
JGit used only one set of attributes constructed from the global and
info attributes, plus the attributes from working tree, index, and
HEAD.

These attributes must be used to determine whether the working tree is
dirty.

But for actually checking out a file, one must use the attributes from
global, info, and *the commit to be checked out*. Otherwise one may not
pick up definitions that are only in the .gitattributes of the commit
to be checked out or that are changed in that commit with respect to
the attributes currently in HEAD, the index, or the working tree.

Maintain in TreeWalk different Attributes per tree, and add operations
to determine EOL handling and smudge filters per tree.

Use the new methods in DirCacheCheckout and ResolveMerger. Note that
merging in JGit actually used the attributes from the base, not those
from ours, which looks dubious at least. It now uses those from ours,
and for checking out the ones from theirs.

The canBeContentMerged() determination was also done from the base
attributes, and is newly done from the ours attributes. Possibly this
should take into account all three attributes, and only if all three
agree the item can be content merged, a content merge should be
attempted? (What if the binary/text setting changes between base, ours,
or theirs?)

Also note that JGit attempts to perform content merges on non-binary
LFS files; there it used the filter attribute from base, too, even for
the ours and theirs versions. Newly it takes the filter attribute from
the correct tree. I'm not convinced doing content merges on potentially
huge files like LFS files is really a good idea.

Add tests in FilterCommandsTest and LfsGitTest to verify the behavior.

Open question: using index and working tree as fallback for the
attributes of ours (assuming it is HEAD) is OK. But does it also make
sense for base and theirs in merging?

Bug: 578707
Change-Id: I0bf433e9e3eb28479b6272e17c0666e175e67d08
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-03-07 18:45:25 +01:00
Matthias Sohn 72bba7bd53 Don't use final for method parameters
See https://wiki.eclipse.org/EGit/Contributor_Guide#Use_of_the_.22final.22_modifier

Change-Id: Idc6ed85483e381689e5085c4a1bacd75d26f5489
2022-03-06 18:52:59 +01:00
Thomas Wolf 8a2c769417 [push] support the "matching" RefSpecs ":" and "+:"
The implementation of push.default=matching was not correct.
It used the RefSpec "refs/heads/*:refs/heads/*", which would push
_all_ local branches. But "matching" must push only those local
branches for which a remote branch with the same name already exists
at the remote.

This RefSpec can be expanded only once the advertisement from the
remote has been received.

Enhance RefSpec so that ":" and "+:" can be represented. Introduce a
special RemoteRefUpdate for such a RefSpec; it must carry through the
fetch RefSpecs to be able to fill in the remote tracking updates as
needed. Implement the expansion in PushProcess.

Bug: 353405
Change-Id: I54a4bfbb0a6a7d77b9128bf4a9c951d6586c3df4
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-03-06 17:30:01 +01:00
Thomas Wolf 90df7c123e [push] Call the pre-push hook later in the push process
Call the pre-push hook only after having received the remote
advertisement and having determined rejections, like C git does.
Also similar to C git, don't pass rejected or up-to-date updates
to the pre-push hook.

Bug: 578852
Change-Id: I51d379ea7bd8234ec815f8f4a9fa325816f476cf
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-03-06 17:30:01 +01:00
Thomas Wolf 72ae234e79 IndexDiff: use tree filter also for SubmoduleWalk
The only uses of IndexDiff.setFilter() in JGit and EGit set a path
filter. Passing the filter on to the SubmoduleWalk gives the desired
result, which is consistent with command-line git.

Bug: 565251
Change-Id: I8eca1ed73eb1d237b8785f369352f72af9e0e168
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-03-06 11:11:26 +01:00
Matthias Sohn 6f175ea6c4 Describe: add support for core.abbrev config option
If core.abbrev is unset or "auto" estimate abbreviation length like C
git does:
- Estimate repository's object count by only considering packed objects,
  round up to next power of 2
- With the order of 2^len objects, we expect a collision at 2^(len/2).
  But we also care about hex chars, not bits, and there are 4 bits per
  hex. So all together we need to divide by 2; but we also want to round
  odd numbers up, hence adding one before dividing.
- For small repos use at least 7 hexdigits
- If object database fails to determine object count use 7 hexdigits as
  fallback

If it is set to "no" do not abbreviate object-ids.

Otherwise set it to the configured value capped to the range between 4
and length of an unabbreviated object-id.

Change-Id: I425f9724b69813dbb57872466bf2d2e1d6dc72c6
2022-03-02 19:29:48 +01:00
Matthias Sohn 9244c07d73 Add a typed config getter for integers confined to a range
Use Integer#MIN_VALUE to denote unset option.

Change-Id: I4d65f2434013111f25520c0ed2b9a9dc8123c6cf
2022-03-02 19:28:14 +01:00
David Ostrovsky 67097f5de4 PersonIdent: Add ctors that accept Instant in addition to Date
Error Prone is flagging Date-API as obsolete and recommends to migrate
to Instant and LocalDate. Given that more JGit users starting to migrate
to new Time API, offer ctors that accept Instant type and also add new
getter that returns when attribute as Instant type.

Change-Id: I64a36bf40f191495c6889c1dff314ede06848880
2022-03-02 16:38:44 +01:00
Fabio Ponciroli c543b8ee1a Make precedence more explicit
Errorprone was failing the build with: "[OperatorPrecedence]. Use
grouping parenthesis to make the operator precedence explicit"

Add parentheses to silence it.

Change-Id: I81f1f249e38fd2543f5412b3501b0179d0759f55
2022-03-02 16:24:44 +01:00
Matthias Sohn 85d8b31cb2 Cap describe abbrev option
- minimum is 4 [1]
- maximum is length of a full ObjectId

[1] https://git-scm.com/docs/git-config#Documentation/git-config.txt-coreabbrev

Change-Id: I145bde1a218f71b87b8d8260761dd0853770bb76
2022-03-02 09:56:03 +01:00
Matthias Sohn a2d5650b8f DescribeCommand: Add support for --abbrev=0
Setting --abbrev=0 suppresses long format and only shows the closest tag
[1].

[1] https://git-scm.com/docs/git-describe#Documentation/git-describe.txt---abbrevltngt

Change-Id: Ifcf4d7786dd0f0fb0315d8093fdb54384ed9d5f9
2022-03-02 09:56:03 +01:00
Sebastian Schuberth a7386ffe3a DescribeCommand: Support configuring the hash abbreviation
Bug: 537883
Signed-off-by: Sebastian Schuberth <sebastian.schuberth@bosch.io>
Change-Id: Ic52dcebc564bbb0d934cc3a6205704b7aeaee30e
2022-02-19 22:04:28 +01:00
Thomas Wolf 69ef598bd9 Simplify implementation of WorkingTreeIterator
All the filtering in WorkingTreeIterator is for check-in, i.e., clean
filtering. The implementation was in some parts too general, passing
around an OperationType. But since it's always CHECKIN_OP, that's not
actually necessary.

Change-Id: I73f8bc059e485a073e456962868f52b3a3db4fc1
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-02-16 18:51:28 +01:00
Thomas Wolf 63c1c6e4d8 Transport: load all refs only if push refspecs have wildcards
There is no need to load all refs if there are no wildcard push
refspecs. Load them lazily on the first wildcard refspec encountered
instead of loading them up-front.

Change-Id: I6d0e981f9ed4997dbdefeb7f83f37ff4f33e06a5
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
2022-02-14 10:45:15 +01:00