Commit Graph

97100 Commits

Author SHA1 Message Date
Paul-Sebastian Ungureanu
2f4cc44d44 sha1-name.c: add get_oidf() which acts like get_oid()
Compared to `get_oid()`, `get_oidf()` has as parameters
a pointer to `object_id`, a printf format string and
additional arguments. This will help simplify the code
in subsequent commits.

Original-idea-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2019-05-13 22:36:39 +02:00
Johannes Schindelin
08f5629871 Start the merging-rebase to 7dcd3d86d4
In preparation for v2.22.0-rc0, we rearrange quite a few topics in this
branch thicket.

Apart from a reworded comment, this does not introduce any new change.

This commit starts the rebase of 7dcd3d86d4 to 7dcd3d86d4

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-13 21:22:22 +02:00
Johannes Schindelin
d4e5e1ea92 Merge pull request #2191 from dscho/add-p-fixups
A couple of fixups for add-i-in-c-status-and-help
2019-05-13 19:33:22 +02:00
Johannes Schindelin
c6746a4f41 fixup! Add a function to determine unique prefixes for a list of strings
Let's not walk off the end of the array. Also, avoid an early `malloc()`
in `add_prefix_entry()` (avoiding reuse of the same data structure for
lookup as for adding a new item), and strengthen the condition for the
bug condition.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-13 14:56:18 +02:00
Johannes Schindelin
f159d0dd18 fixup! built-in add -i: implement the main loop
Clarify the role of the "magic" `-1` and `-2` return values of the
`list_and_choose()` function.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-13 14:56:12 +02:00
Johannes Schindelin
e3538d8eb3 Merge pull request #2185 from dscho/fix-status-with-rebase-ir
Fix `git status`' display of `git rebase -ir`'s `label` commands
2019-05-10 22:55:48 +02:00
Johannes Schindelin
ec9eed5c88 status: fix display of rebase -ir's label command
The argument of a `label` command does *not* want to be turned into an
abbreviated SHA-1.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-10 21:32:17 +02:00
Johannes Schindelin
81a060a4d1 Merge pull request #2127 from dscho/fix-fsmonitor
Do query the fsmonitor again after the index has been discarded
2019-05-09 23:04:27 +02:00
Johannes Schindelin
912cfd3f9e fsmonitor: force a refresh after the index was discarded
With this change, the `index_state` struct becomes the new home for the
flag that says whether the fsmonitor hook has been run, i.e. it is now
per-index.

It also gets re-set when the index is discarded, fixing the bug
demonstrated by the "test_expect_failure" test added in the preceding
commit. In that case fsmonitor-enabled Git would miss updates under
certain circumstances, see that preceding commit for details.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-09 21:04:25 +02:00
Johannes Schindelin
328b4a2b06 fsmonitor: demonstrate that it is not refreshed after discard_index()
This one is tricky.

When `core.fsmonitor` is set, a `refresh_index()` will not perform a
full scan of files that might be modified, but will query the fsmonitor
and refresh only the ones that have been actually touched.

Due to implementation details, the fsmonitor is queried in
`refresh_cache_ent()`, but of course it only has to be queried once, so
we set a flag when we did that. But when the index was discarded, we did
not re-set that flag.

So far, this is only covered by our test suite when running with
GIT_TEST_FSMONITOR=$PWD/t7519/fsmonitor-all, and only due to the way the
built-in stash interacts with the recursive merge machinery.

Let's introduce a straight-forward regression test for this.

We simply extend the "read & discard index" loop in `test-tool
read-cache` to optionally refresh the index, report on a given file's
status, and then modify that file. Due to the bug described above, only
the first refresh will actually query the fsmonitor; subsequent loop
iterations will not.

This problem was reported by Ævar Arnfjörð Bjarmason.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-09 21:04:20 +02:00
Johannes Schindelin
2ac5f424d8 Merge pull request #2184 from dscho/address-coverity-report
Fix some issues reported by Coverity
2019-05-09 16:31:19 +02:00
Johannes Schindelin
32b5d74642 Merge branch 'address-coverity-reports'
Coverity pointed out a couple of bugs, and here are fixes for some of
them.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-09 14:51:29 +02:00
Johannes Schindelin
52ac616bd3 fixup! fscache: teach fscache to use NtQueryDirectoryFile
The return value of that function is not actually we are currently able
to translate to an `errno`-style value easily. So let's just not.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-09 14:51:29 +02:00
Johannes Schindelin
2a85ba50fe fixup! fscache: update fscache to be thread specific instead of global
We do fall back to not enabling the FSCache when we're out of
Thread-Local indexes, but we failed to unlock the mutex.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-09 14:51:29 +02:00
Johannes Schindelin
0ccd8caa6a fixup! Introduce helper to create symlinks that knows about index_state
We do not actually need to test for `state != NULL`, as
`state->delayed_checkout` had already been accessed before this.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-09 14:51:28 +02:00
Johannes Schindelin
ea7a305a45 squash! ci: also test with MS Visual C on Azure Pipelines
We specifically reduce the number of parallel links for MSVC, as RAM
usage is an issue with MSVC's parallel mode, manifested in the symptom:

	fatal error LNK1318: Unexpected PDB error; OK (0) ''

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-09 14:51:28 +02:00
Johannes Schindelin
28f28c03fd fixup! built-in add -p: offer a helpful error message when hunk navigation failed
Just an obvious fix for an obvious varargs mistake.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-09 14:50:57 +02:00
Johannes Schindelin
2a2d648516 sequencer: check return value of get_oid()
Just a little defensive coding.

Found by Coverity.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-09 14:44:14 +02:00
Johannes Schindelin
840b754ed4 bisect--helper: verify HEAD could be parsed before continuing
In 06f5608c14 (bisect--helper: `bisect_start` shell function partially
in C, 2019-01-02), we introduced a call to `get_oid()` and did not check
whether it succeeded before using its output.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-09 14:08:52 +02:00
Johannes Schindelin
d451e84f20 rebase -i: watch out for invalid upstream arguments
This was overlooked in 53bbcfbde7 (rebase -i: implement the main part
of interactive rebase as a builtin, 2018-09-27).

Found by Coverity.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-09 12:26:46 +02:00
Johannes Schindelin
fd442be344 rebase: replace incorrect logical negation by correct bitwise one
In bff014dac7 (builtin rebase: support the `verbose` and `diffstat`
options, 2018-09-04), we added a line that wanted to remove the
`REBASE_DIFFSTAT` bit from the flags, but it used an incorrect negation.

Found by Coverity.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-09 12:26:46 +02:00
Johannes Schindelin
65f1570624 Merge pull request #2175 from dscho/difftool-no-index
Allow `git difftool --no-index` to run outside of a worktree
2019-05-08 23:54:15 +02:00
Johannes Schindelin
a930c5a836 Merge pull request #2150 from dscho/add-p-gfw
Offer a built-in version of `git add -i` and `git add -p`
2019-05-08 21:30:53 +02:00
Johannes Schindelin
290a66692c Merge branch 'difftool-no-index-extra'
This patch addresses the segmentation faults in `git difftool --no-index
--dir-diff`: surprisingly, those two options don't make no sense
together.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-08 21:01:39 +02:00
Johannes Schindelin
860e6522f7 Merge branch 'difftool-no-index'
This fixes https://github.com/git-for-windows/git/issues/2123

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-08 21:01:38 +02:00
Johannes Schindelin
cac7f0a6c9 difftool --no-index: error out on --dir-diff (and don't crash)
In `--no-index` mode, we now no longer require a worktree nor a
repository. But some code paths in `difftool` expect those to be
present.

The most notable such code path is the `--dir-diff` one: we use the
existing checkout machinery to copy the files, and that machinery looks
up replacement refs, looks at alternate ODBs, wants to use the worktree
path, etc.

Rather than running into segmentation faults, let's die with an
informative error message.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-08 21:01:37 +02:00
Johannes Schindelin
89dfd5daf2 difftool: allow running outside Git worktrees with --no-index
As far as this developer can tell, the conversion from a Perl script to
a built-in caused the regression in the difftool that it no longer runs
outside of a Git worktree (with `--no-index`, of course).

It is a bit embarrassing that it took over two years after retiring the
Perl version to discover this regression, but at least we now know, and
can do something, about it.

This fixes https://github.com/git-for-windows/git/issues/2123

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-08 21:01:37 +02:00
Johannes Schindelin
9732ed19da parse-options: make OPT_ARGUMENT() more useful
`OPT_ARGUMENT()` is intended to keep the specified long option in `argv`
and not to do anything else.

However, it would make a lot of sense for the caller to know whether
this option was seen at all or not. For example, we want to teach `git
difftool` to work outside of any Git worktree, but only when
`--no-index` was specified.

Note: nothing in Git uses OPT_ARGUMENT(). Even worse, looking through
the commit history, one can easily see that nothing even
ever used it, apart from the regression test.

So not only do we make `OPT_ARGUMENT()` more useful, we are also about
to introduce its first real user!

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-08 21:01:29 +02:00
Johannes Schindelin
ee7778868f Merge branch 'built-in-stash-and-add-p'
Now that we have a built-in `git add -p` and a built-in `git stash`,
make them play nicely together.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-08 16:20:22 +02:00
Johannes Schindelin
d8850d4e39 Merge branch 'stash-p-corner-case'
This topic branch fixes a corner case that is amazingly common in this
developer's workflow: in a `git stash -p`, splitting a hunk and stashing
only part of it runs into a (known) bug where the partial hunk cannot be
applied in reverse.

It is one of those "good enough" fixes, not a full fix, though, as the
full fix would require a 3-way merge between `stash^` and the *worktree*
(not `HEAD`), with `stash` as merge base (i.e. a `git revert`, but on
top of the current worktree).

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-08 16:17:59 +02:00
Johannes Schindelin
b85a6f8009 stash -p: (partially) fix bug concerning split hunks
When trying to stash part of the worktree changes by splitting a hunk
and then only partially accepting the split bits and pieces, the user
is presented with a rather cryptic error:

	error: patch failed: <file>:<line>
	error: test: patch does not apply
	Cannot remove worktree changes

and the command would fail to stash the desired parts of the worktree
changes (even if the `stash` ref was actually updated correctly).

We even have a test case demonstrating that failure, carrying it for
four years already.

The explanation: when splitting a hunk, the changed lines are no longer
separated by more than 3 lines (which is the amount of context lines
Git's diffs use by default), but less than that. So when staging only
part of the diff hunk for stashing, the resulting diff that we want to
apply to the worktree in reverse will contain those changes to be
dropped surrounded by three context lines, but since the diff is
relative to HEAD rather than to the worktree, these context lines will
not match.

Example time. Let's assume that the file README contains these lines:

	We
	the
	people

and the worktree added some lines so that it contains these lines
instead:

	We
	are
	the
	kind
	people

and the user tries to stash the line containing "are", then the command
will internally stage this line to a temporary index file and try to
revert the diff between HEAD and that index file. The diff hunk that
`git stash` tries to revert will look somewhat like this:

	@@ -1776,3 +1776,4
	 We
	+are
	 the
	 people

It is obvious, now, that the trailing context lines overlap with the
part of the original diff hunk that the user did *not* want to stash.

Keeping in mind that context lines in diffs serve the primary purpose of
finding the exact location when the diff does not apply precisely (but
when the exact line number in the file to be patched differs from the
line number indicated in the diff), we work around this by reducing the
amount of context lines: the diff was just generated.

Note: this is not a *full* fix for the issue. Just as demonstrated in
t3701's 'add -p works with pathological context lines' test case, there
are ambiguities in the diff format. It is very rare in practice, of
course, to encounter such repeated lines.

The full solution for such cases would be to replace the approach of
generating a diff from the stash and then applying it in reverse by
emulating `git revert` (i.e. doing a 3-way merge). However, in `git
stash -p` it would not apply to `HEAD` but instead to the worktree,
which makes this non-trivial to implement as long as we also maintain a
scripted version of `add -i`.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-08 16:17:58 +02:00
Johannes Schindelin
9c4ed70beb t3904: fix incorrect demonstration of a bug
In 7e9e048661 (stash -p: demonstrate failure of split with mixed y/n,
2015-04-16), a regression test for a known breakage that was added to
the test script `t3904-stash-patch.sh` that demonstrated that splitting
a hunk and trying to stash only part of that split hunk fails (but
shouldn't).

As expected, it still fails, but for the wrong reason: once the bug is
fixed, we would expect stderr to show nothing, yet the regression test
expects stderr to show something.

Let's fix that by telling that regression test case to expect nothing to
be printed to stderr.

While at it, also drop the obvious left-over from debugging where the
regression test did not mind `git stash -p` to return a non-zero exit
status.

Of course, the regression test still fails, but this time for the
correct reason.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-08 16:17:58 +02:00
Johannes Schindelin
212565694d built-in stash: use the built-in git add -p if so configured
The scripted version of `git stash` called directly into the Perl script
`git-add--interactive.perl`, and this was faithfully converted to C.

However, we have a much better way to do this now: call `git add
--patch=<mode>`, which incidentally also respects the config setting
`add.interactive.useBuiltin`.

Let's do this.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-07 23:46:30 +02:00
Karsten Blees
954b915e0b mingw: Support git_terminal_prompt with more terminals
The `git_terminal_prompt()` function expects the terminal window to be
attached to a Win32 Console. However, this is not the case with terminal
windows other than `cmd.exe`'s, e.g. with MSys2's own `mintty`.

Non-cmd terminals such as `mintty` still have to have a Win32 Console
to be proper console programs, but have to hide the Win32 Console to
be able to provide more flexibility (such as being resizeable not only
vertically but also horizontally). By writing to that Win32 Console,
`git_terminal_prompt()` manages only to send the prompt to nowhere and
to wait for input from a Console to which the user has no access.

This commit introduces a function specifically to support `mintty` -- or
other terminals that are compatible with MSys2's `/dev/tty` emulation.

The most prominent user of `git_terminal_prompt()` is certainly
`git-remote-https.exe`. It is an interesting use case because both
`stdin` and `stdout` are redirected when Git calls said executable, yet
it still wants to access the terminal.

When running inside a `mintty`, the terminal is not accessible to the
`git-remote-https.exe` program, though, because it is a MinGW program
and the `mintty` terminal is not backed by a Win32 console.

To solve that problem, we simply call out to the shell -- which is an
*MSys2* program and can therefore access `/dev/tty`.

Helped-by: 마누엘 <nalla@hamal.uberspace.de>
Signed-off-by: Karsten Blees <blees@dcon.de>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-07 23:46:30 +02:00
Johannes Schindelin
36fd328ea7 re-revert tests: add a special setup where stash.useBuiltin is off
We temporarily reverted part of this commit to allow merging `add-p`
without conflicts. Now it is time to re-apply that part.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-07 23:25:09 +02:00
Johannes Schindelin
b6d244806d t3701: verify that we can add *lots* of files interactively
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-07 23:25:09 +02:00
Johannes Schindelin
b5af4f266b Merge remote-tracking branch 'dscho/add-p' into add-p-g4w
Let's test this for a while.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-07 23:25:09 +02:00
Johannes Schindelin
ab0301b0d6 Merge branch 'add-p-in-c-config-settings'
This is the final leg of the journey to a fully built-in `git add`: the
`git add -i` and `git add -p` modes were re-implemented in C, but they
lacked support for a couple of config settings.

The one that sticks out most is the `interactive.singleKey` setting: it
was not only particularly hard to get to work, especially on Windows. It
is also the setting that seems to be incomplete already in the Perl
version: while the name suggests that it applies to the main loop of
`git add --interactive`, or to the file selections in that command, it
does not. Only the `git add --patch` mode respects that setting.

As it is outside the purpose of the conversion of
`git-add--interactive.perl` to C, we will leave that loose end for some
future date.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-07 23:25:06 +02:00
Johannes Schindelin
8c242f909d Merge branch 'other-command-p-in-c'
At this stage on the journey to a fully built-in `git add`, we already
have everything we need, including the `--interactive` and `--patch`
options, as long as the `add.interactive.useBuiltin` setting is set to
`true` (kind of a "turned off feature flag", which it will be for a
while, until we get confident enough that the built-in version does the
job, and retire the Perl script).

However, the internal `add--interactive` helper is also used to back the
`--patch` option of `git stash`, `git reset` and `git checkout`.

This patch series brings them "online".

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-07 23:25:06 +02:00
Johannes Schindelin
ddc8a977fb Merge branch 'add-p-in-c'
Out of all the patch series on the journey to provide `git add
--interactive` and `git add --patch` in a built-in versions, this is the
big one, as can be expected from the fact that the `git add --patch`
functionality makes up over half of the 1,800+ lines of
`git-add--interactive.perl`.

The two patches that stick out are of course the ones to implement hunk
splitting and hunk editing: these operations are fundamentally more
complicated, and less obvious, than the rest of the operations.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-07 23:25:06 +02:00
Johannes Schindelin
15f3dba4da Merge branch 'add-i-fixes'
While re-implementing `git add -i` and `git add -p` in C, I tried to
make sure that there is test coverage for all of the features I convert
from Perl to C, to give me some confidence in the correctness from
running the test suite both with `GIT_TEST_ADD_I_USE_BUILTIN=true` and
with `GIT_TEST_ADD_I_USE_BUILTIN=false`.

However, I discovered that there are a couple of gaps. This patch series
intends to close them.

The first patch might actually not be considered a gap by some: it
basically removes the need for the `TTY` prerequisite in the `git add
-i` tests to verify that the output is colored all right. This change is
rather crucial for me, though: on Windows, where the conversion to a
built-in shows the most obvious benefits, there are no pseudo terminals
(yet), therefore `git.exe` cannot work with them (even if the MSYS2 Perl
interpreter used by Git for Windows knows about some sort of pty
emulation). And I *really* wanted to make sure that the colors work on
Windows, as I personally get a lot out of those color cues.

The patch series ends by addressing two issues that are not exactly
covering testing gaps:

- While adding a test case, I noticed that `git add -p` exited with
  *success* when it could not even generate a diff. This is so obviously
  wrong that I had to fix it right away (I noticed, actually, because my
  in-progress built-in `git add -p` failed, and the Perl version did
  not), and I used the same test case to verify that this is fixed once
  and for all.

- While working on covering those test gaps, I noticed a problem in an
  early version of the built-in version of `git add -p` where the `git
  apply --allow-overlap` mode failed to work properly, for little
  reason, and I fixed it real quick.

  It would seem that the `--allow-overlap` function is not only
  purposefully under-documented, but also purposefully under-tested,
  probably to discourage its use. I do not quite understand what I
  perceive to be Junio's aversion to that option, but I did not feel
  like I should put up a battle here, so I did not accompany this fix
  with a new test script.

  In the end, the built-in version of `git add -p` does not use the
  `--allow-overlap` function at all, anyway. Which should make everybody
  a lot happier.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-07 23:25:06 +02:00
Johannes Schindelin
5f0f8fd1d9 Merge branch 'add-i-in-c-all-except-patch'
This patch series implements the rest of the commands in `git add -i`'s
main loop: `update`, `revert`, `add_untracked`, `patch`, `diff`, and
`quit`. Apart from `quit`, these commands are all very similar in that
they first build a list of files, display it, let the user choose which
ones to act on, and then perform the action.

Note that the `patch` command is not actually converted to C, not
completely at least: the built-in version simply hands off to `git
add--interactive` after letting the user select which files to act on.

The reason for this omission is practicality. Out of the 1,800+ lines of
`git-add--interactive.perl`, over a thousand deal *just* with the `git
add -p` part. I did convert that functionality already (to be
contributed in a separate patch series), discovering that there is so
little overlap between the `git add --patch` part and the rest of `git
add --interactive` that I could put the former into a totally different
file: `add-patch.c`. Just a teaser ;-)

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-07 23:25:06 +02:00
Johannes Schindelin
779d4ddf4b ci: include the built-in git add -i in the linux-gcc job
This job runs the test suite twice, once in regular mode, and once with
a whole slew of `GIT_TEST_*` variables set.

Now that the built-in version of `git add --interactive` is
feature-complete, let's also throw `GIT_TEST_MULTI_PACK_INDEX` into that
fray.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-07 23:25:05 +02:00
Johannes Schindelin
a2d32c9879 Merge branch 'add-i-in-c-status-and-help'
The first part of the long journey to a fully built-in `git add -i`.

It reflects the part that was submitted a couple of times (see
https://github.com/gitgitgadget/git/pull/103) during the Outreachy
project by Slavica Đukić that continued the journey based on an initial
patch series by Daniel Ferreira.

This part only implements the `status` and the `help` part, like
Slavica's last iteration did, in the interest of making the review
remotely more reviewable. I fear that this attempt of making it a bit
more reviewable is pretty futile, as so many things changed. So I will
ask the reviewers for forgiveness: please be kind, and give this sort of
a fresh review.

I threw in a couple of major changes on top of that iteration, though:

- The original plan was to add a helper (`git add--helper`) that takes
  over more and more responsibility from the Perl script over the course
  of the conversion.

  This plan is no longer in effect, as I encountered a serious problem
  with that: the MSYS2 runtime used by the Perl interpreter which Git
  for Windows employs to run `git add -i` has a curious bug (that is
  safely outside the purview of this here patch series) where it fails
  to read from standard input after it spawned a non-MSYS2 program that
  reads from standard input. To keep my `git add -i` in a working state,
  I therefore adopted a different strategy:

  Just like `git difftool` was converted by starting with a built-in
  that did nothing but handing off to the scripted version, guarded by
  the (opt-in) `difftool.useBuiltin` config setting, I start this patch
  series by a built-in `add -i` that does nothing else but state that it
  is not implemented yet, guarded by the (opt-in)
  `add.interactive.useBuiltin` config setting.

  In contrast to the `git difftool` situation, it is quite a bit easier
  here, as we do not even have to rename the script to
  `git-legacy-add--interactive.perl`: the `add--interactive` command is
  an implementation detail that users are not even supposed to know
  about. Therefore, we can implement that road fork between the built-in
  and the scripted version in `builtin/add.c`, i.e. in the user-facing
  `git add` command.

  This will also naturally help with the transition to a fully built-in
  `git add -i`/`git add -p`, as we saw with the built-in `git rebase`
  how important it is for end users to have an escape hatch (and for
  that reason, tried our best to provide the same with the built-in `git
  stash`).

- The `help` command was actually not hooked up in `git add -i`, but was
  only available as a special option of the `git add--helper` command.
  As that command no longer exists, I kind of *had* to implement
  some way to let the built-in `git add -i` show the help text.

- The main loop of `git add -i` (i.e. the thing that lets you choose
  `status` or `help`) is now implemented (but only lists `status` and
  `help`, of course), as it makes use of that feature that took the main
  chunk of the Outreachy project: the function to determine unique
  prefixes of a list of strings.

- Speaking of the unique prefixes: the functionality to determine those
  is now encapsulated in the `prefix-map.c` file, and I also added a
  regression test.

- Speaking of the tests: I also implemented support for the environment
  variable `GIT_TEST_ADD_I_USE_BUILTIN`: by setting it, the test suite
  can be forced to use the built-in, or the Perl script, version of `git
  add -i`. Needless to say: by the end of this patch series, running the
  test suite with `GIT_TEST_ADD_I_USE_BUILTIN=true` will still result in
  a ton of test failures due to not-yet-implemented commands, but it
  will also demonstrate what *already* works.

- Since the main loop starts not only by showing the status, but
  refreshes the index before that, I added that, and I actually
  refactored that code into a new function
  (`repo_refresh_and_write_index()`), as it will be used a couple of
  times by the end of the complete conversion of `git add -i` into a
  built-in command.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-07 23:25:05 +02:00
Johannes Schindelin
89951d7fbd built-in add -p: handle Escape sequences more efficiently
When `interactive.singlekey = true`, we react immediately to keystrokes,
even to Escape sequences (e.g. when pressing a cursor key).

The problem with Escape sequences is that we do not really know when
they are done, and as a heuristic we poll standard input for half a
second to make sure that we got all of it.

While waiting half a second is not asking for a whole lot, it can become
quite annoying over time, therefore with this patch, we read the
terminal capabilities (if available) and extract known Escape sequences
from there, then stop polling immediately when we detected that the user
pressed a key that generated such a known sequence.

This recapitulates the remaining part of b5cc003253 (add -i: ignore
terminal escape sequences, 2011-05-17).

Note: We do *not* query the terminal capabilities directly. That would
either require a lot of platform-specific code, or it would require
linking to a library such as ncurses.

Linking to a library in the built-ins is something we try very hard to
avoid (we even kicked the libcurl dependency to a non-built-in remote
helper, just to shave off a tiny fraction of a second from Git's startup
time). And the platform-specific code would be a maintenance nightmare.

Even worse: in Git for Windows' case, we would need to query MSYS2
pseudo terminals, which `git.exe` simply cannot do (because it is
intentionally *not* an MSYS2 program).

To address this, we simply spawn `infocmp -L -1` and parse its output
(which works even in Git for Windows, because that helper is included in
the end-user facing installations).

This is done only once, as in the Perl version, but it is done only when
the first Escape sequence is encountered, not upon startup of `git add
-i`; This saves on startup time, yet makes reacting to the first Escape
sequence slightly more sluggish. But it allows us to keep the
terminal-related code encapsulated in the `compat/terminal.c` file.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-07 23:25:05 +02:00
Johannes Schindelin
8cf5fd62d8 built-in add -p: handle Escape sequences in interactive.singlekey mode
This recapitulates part of b5cc003253 (add -i: ignore terminal escape
sequences, 2011-05-17):

    add -i: ignore terminal escape sequences

    On the author's terminal, the up-arrow input sequence is ^[[A, and
    thus fat-fingering an up-arrow into 'git checkout -p' is quite
    dangerous: git-add--interactive.perl will ignore the ^[ and [
    characters and happily treat A as "discard everything".

    As a band-aid fix, use Term::Cap to get all terminal capabilities.
    Then use the heuristic that any capability value that starts with ^[
    (i.e., \e in perl) must be a key input sequence.  Finally, given an
    input that starts with ^[, read more characters until we have read a
    full escape sequence, then return that to the caller.  We use a
    timeout of 0.5 seconds on the subsequent reads to avoid getting stuck
    if the user actually input a lone ^[.

    Since none of the currently recognized keys start with ^[, the net
    result is that the sequence as a whole will be ignored and the help
    displayed.

Note that we leave part for later which uses "Term::Cap to get all
terminal capabilities", for several reasons:

1. it is actually not really necessary, as the timeout of 0.5 seconds
   should be plenty sufficient to catch Escape sequences,

2. it is cleaner to keep the change to special-case Escape sequences
   separate from the change that reads all terminal capabilities to
   speed things up, and

3. in practice, relying on the terminal capabilities is a bit overrated,
   as the information could be incomplete, or plain wrong. For example,
   in this developer's tmux sessions, the terminal capabilities claim
   that the "cursor up" sequence is ^[M, but the actual sequence
   produced by the "cursor up" key is ^[[A.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-07 23:25:05 +02:00
Johannes Schindelin
597ac6904d built-in add -p: respect the interactive.singlekey config setting
The Perl version of `git add -p` supports this config setting to allow
users to input commands via single characters (as opposed to having to
press the <Enter> key afterwards).

This is an opt-in feature because it requires Perl packages
(Term::ReadKey and Term::Cap, where it tries to handle an absence of the
latter package gracefully) to work. Note that at least on Ubuntu, that
Perl package is not installed by default (it needs to be installed via
`sudo apt-get install libterm-readkey-perl`), so this feature is
probably not used a whole lot.

In C, we obviously do not have these packages available, but we just
introduced `read_single_keystroke()` that is similar to what
Term::ReadKey provides, and we use that here.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-07 23:25:05 +02:00
Johannes Schindelin
09af4d86f7 terminal: add a new function to read a single keystroke
Typically, input on the command-line is line-based. It is actually not
really easy to get single characters (or better put: keystrokes).

We provide two implementations here:

- One that handles `/dev/tty` based systems as well as native Windows.
  The former uses the `tcsetattr()` function to put the terminal into
  "raw mode", which allows us to read individual keystrokes, one by one.
  The latter uses `stty.exe` to do the same, falling back to direct
  Win32 Console access.

  Thanks to the refactoring leading up to this commit, this is a single
  function, with the platform-specific details hidden away in
  conditionally-compiled code blocks.

- A fall-back which simply punts and reads back an entire line.

Note that the function writes the keystroke into an `strbuf` rather than
a `char`, in preparation for reading Escape sequences (e.g. when the
user hit an arrow key). This is also required for UTF-8 sequences in
case the keystroke corresponds to a non-ASCII letter.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-07 23:25:05 +02:00
Johannes Schindelin
8d5239f5fc terminal: accommodate Git for Windows' default terminal
Git for Windows' Git Bash runs in MinTTY by default, which does not have
a Win32 Console instance, but uses MSYS2 pseudo terminals instead.

This is a problem, as Git for Windows does not want to use the MSYS2
emulation layer for Git itself, and therefore has no direct way to
interact with that pseudo terminal.

As a workaround, use the `stty` utility (which is included in Git for
Windows, and which *is* an MSYS2 program, so it knows how to deal with
the pseudo terminal).

Note: If Git runs in a regular CMD or PowerShell window, there *is* a
regular Win32 Console to work with. This is not a problem for the MSYS2
`stty`: it copes with this scenario just fine.

Also note that we introduce support for more bits than would be
necessary for a mere `disable_echo()` here, in preparation for the
upcoming `enable_non_canonical()` function.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-07 23:25:05 +02:00
Johannes Schindelin
40410bae2b terminal: make the code of disable_echo() reusable
We are about to introduce the function `enable_non_canonical()`, which
shares almost the complete code with `disable_echo()`.

Let's prepare for that, by refactoring out that shared code.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-05-07 23:25:05 +02:00