Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

git add -i: add a rudimentary version in C (supporting only status and help so far) #170

Closed
wants to merge 9 commits into from

Conversation

dscho
Copy link
Member

@dscho dscho commented Mar 28, 2019

This is the first leg on the long journey to a fully built-in git add -i (next up: parts 2, 3, 4, 5, and 6). Note: the latter PRs are not necessarily up to date, and will be re-targeted to the appropriate branches in https://github.com/gitster/git as soon as Junio picks them up.

This here patch series reflects the part that was submitted a couple of times (see #103) during the Outreachy project by Slavica Ðukic that continued the journey based on an initial patch series by Daniel Ferreira.

It only implements the status and the help part, in the interest of making the review remotely more reviewable.

As I am a heavy user of git add -p myself and use a patched version for several months already (it is so nice to not suffer over one second startup until the MSYS2 Perl finally shows me anything, instead it feels instantaneous), I integrated these patch series into Git for Windows already, as an opt-in feature guarded by the config variable add.interactive.useBuiltin (and Git for Windows' installer knows to detect this version and offer the option in the graphical user interface).

Changes since v6:

  • Fixed a potential buffer overrun when parsing numbers/number ranges.

Changes since v5:

  • Reworded two commit messages.
  • Clarified code that does not affect patch_mode.
  • Restricted scope of the very local variable endp.

Changes since v4:

  • Rebased onto current master to make use of Thomas Gummerer's repo_refresh_and_write_index() as well as to avoid merge conflicts with Eric Wong's work on struct hashmap.
  • Instead of rolling a dedicated data struct to simulate a Trie, we now use string-list extensively (an unsorted copy and a sorted one, the latter to determine unique prefixes). This had massive ramifications on the rest of the patches... For example, the struct command_item structure no longer contains the name field, but is intended to be a util in a string_list.
  • Changed the commit messages and author lines to reflect Slavica's name correctly.
  • Touched up a couple commit messages.

Changes since v3:

  • Rebased to v2.23.0 to reduce friction.
  • free_diffstat_info() is now made public as well, and used, to avoid a memory leak.
  • Prepared the patches for ew/hashmap (which is strict about the hashmap entries' type in hashmap_entry_init() and friends).
  • The private data types have been moved from prefix-map.h to prefix-map.c.
  • A lot of int types were converted to more appropriate size_t in prefix-map.c.
  • A misleading parameter name list was renamed to the correct array.
  • The code comment above find_unique_prefixes() was (hopefully) improved.
  • The run_help() function's signature now reflects that most of the parameters are actually unused.

Changes since v2:

  • Rebased to master to avoid merge conflicts.
  • Renumbered the prefix-map test to avoid conflicts with two patch series that are currently in-flight in pu.

Changes since v1:

  • The config machinery was reworked completely, to not use a callback to git_config(), but instead to query the config via the repo_config_get_*() functions. This also prevents a future "Huh???" moment: the internal add --interactive API accepts a parameter of type struct repository *r, but the previous configuration did not use that to query the config (and could in the future be a repository other than the_repository).

  • As a consequence, the color sequences are no longer stored in file-local variables, but passed around via a struct.

  • Instead of using the magical constant -2 to quit the main loop, it is now defined as LIST_AND_CHOOSE_QUIT (and likewise, LIST_AND_CHOOSE_ERROR is defined as -1 and used where appropriate).

  • Improved the add_prefix_item() function by avoiding buffer overruns, not reusing the struct that is used for lookup also for adding the new item, and by strengthening the bug check.

Cc: Jeff Hostetler git@jeffhostetler.com, Jeff King peff@peff.net

@dscho dscho changed the title Add i in c status and help git add -i: add a rudimentary version in C (supporting only status and help so far) Mar 28, 2019
@dscho dscho force-pushed the add-i-in-c-status-and-help branch 6 times, most recently from 11991db to 3d02c5b Compare April 3, 2019 09:40
add-interactive.c Outdated Show resolved Hide resolved
add-interactive.c Outdated Show resolved Hide resolved
prefix-map.c Outdated Show resolved Hide resolved
prefix-map.c Outdated Show resolved Hide resolved
@dscho
Copy link
Member Author

dscho commented Apr 10, 2019

@slavicaDj thank you for your review! Most helpful.

@dscho dscho force-pushed the add-i-in-c-status-and-help branch from 3d02c5b to 481e331 Compare April 10, 2019 15:03
@dscho
Copy link
Member Author

dscho commented Apr 10, 2019

/submit

@gitgitgadget
Copy link

gitgitgadget bot commented Apr 10, 2019

Submitted as pull.170.git.gitgitgadget@gmail.com

@slavicaDj
Copy link

@slavicaDj thank you for your review! Most helpful.

You're welcome! Thank you.

Makefile Outdated Show resolved Hide resolved
@dscho dscho force-pushed the add-i-in-c-status-and-help branch 2 times, most recently from ddb913e to 266dbf2 Compare May 13, 2019 13:01
@dscho
Copy link
Member Author

dscho commented May 13, 2019

/submit

@gitgitgadget
Copy link

gitgitgadget bot commented May 13, 2019

Submitted as pull.170.v2.git.gitgitgadget@gmail.com

@gitgitgadget
Copy link

gitgitgadget bot commented Nov 14, 2019

This patch series was integrated into pu via git@bc2831e.

dscho and others added 5 commits November 14, 2019 15:34
The reason why we did not start with the main loop to begin with is that
it is the first user of `list_and_choose()`, which uses the `list()`
function that we conveniently introduced for use by the `status`
command.

In contrast to the Perl version, in the built-in interactive `add`, we
will keep the `list()` function (which only displays items) and the
`list_and_choose()` function (which uses `list()` to display the items,
and only takes care of the "and choose" part) separate.

The `list_and_choose()` function, as implemented in
`git-add--interactive.perl` knows a few more tricks than the function we
introduce in this patch:

- There is a flag to let the user select multiple items.

- In multi-select mode, the list of items is prefixed with a marker
  indicating what items have been selected.

- Initially, for each item a unique prefix is determined (if there
  exists any within the given parameters), and shown in the list, and
  accepted as a shortcut for the selection.

These features will be implemented in the C version later.

This patch does not add any new main loop command, of course, the
built-in `git add -i` still only supports the `status` command. The
remaining commands to follow over the course of the next commits.

To accommodate for listing the commands in columns, preparing for the
commands that will be implemented over the course of the next
patches/patch series, we teach the `list()` function to do precisely
that.

Note that we only have a prompt ending in a single ">" at this stage;
later commits will add commands that display a double ">>" to indicate
that the user is in a different loop than the main one.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Just like in the Perl script `git-add--interactive.perl`, for each
command a unique prefix is determined (if there exists any within the
given parameters), and shown in the list, and accepted as a shortcut for
the command.

To determine the unique prefixes, as well as to look up the command in
question, we use a copy of the list and sort it.

While this might seem like overkill for a single command, it will make
much more sense when all the commands are implemented, and when we reuse
the same logic to present a list of files to edit, with convenient
unique prefixes.

At the start of the development of this patch series, a dedicated data
structure was introduced that imitated the Trie that the Perl version
implements. However, this was deemed overkill, and we now simply sort
the list before determining the length of the unique prefixes by looking
at each item's neighbor. As a bonus, we now use the same sorted list to
perform a binary search using the user-provided prefix as search key.

Original-patch-by: Slavica Đukić <slawica92@hotmail.com>
Helped-by: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
With this change, we print out the same colored help text that the
Perl-based `git add -i` prints in the main loop when question mark is
entered.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
The error messages as well as the unique prefixes are colored in `git
add -i` by default; We need to do the same in the built-in version.

Signed-off-by: Slavica Đukić <slawica92@hotmail.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This imitates the code to show the help text from the Perl script
`git-add--interactive.perl` in the built-in version.

To make sure that it renders exactly like the Perl version of `git add
-i`, we also add a test case for that to `t3701-add-interactive.sh`.

Signed-off-by: Slavica Đukić <slawica92@hotmail.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
@dscho dscho force-pushed the add-i-in-c-status-and-help branch from 7fda762 to ccc6fb4 Compare November 14, 2019 14:37
@dscho
Copy link
Member Author

dscho commented Nov 15, 2019

/submit

@gitgitgadget
Copy link

gitgitgadget bot commented Nov 15, 2019

@gitgitgadget
Copy link

gitgitgadget bot commented Nov 18, 2019

This patch series was integrated into pu via git@13df35f.

@gitgitgadget
Copy link

gitgitgadget bot commented Nov 19, 2019

This patch series was integrated into pu via git@b99a171.

@gitgitgadget
Copy link

gitgitgadget bot commented Nov 20, 2019

This patch series was integrated into pu via git@0d718c6.

@gitgitgadget
Copy link

gitgitgadget bot commented Nov 20, 2019

This patch series was integrated into pu via git@cea9447.

@gitgitgadget
Copy link

gitgitgadget bot commented Nov 21, 2019

This patch series was integrated into pu via git@cb15e0a.

@gitgitgadget
Copy link

gitgitgadget bot commented Nov 21, 2019

This patch series was integrated into pu via git@b15d70c.

@gitgitgadget
Copy link

gitgitgadget bot commented Nov 21, 2019

This patch series was integrated into next via git@caefa55.

@gitgitgadget gitgitgadget bot added the next label Nov 21, 2019
@gitgitgadget
Copy link

gitgitgadget bot commented Nov 22, 2019

This patch series was integrated into pu via git@a6bf991.

@gitgitgadget
Copy link

gitgitgadget bot commented Nov 25, 2019

This patch series was integrated into pu via git@9544a70.

@gitgitgadget
Copy link

gitgitgadget bot commented Nov 27, 2019

This patch series was integrated into pu via git@a1eb0ff.

@gitgitgadget
Copy link

gitgitgadget bot commented Dec 2, 2019

This patch series was integrated into pu via git@964dd06.

@gitgitgadget
Copy link

gitgitgadget bot commented Dec 3, 2019

This patch series was integrated into pu via git@dbb878f.

@gitgitgadget
Copy link

gitgitgadget bot commented Dec 5, 2019

This patch series was integrated into pu via git@f7998d9.

@gitgitgadget
Copy link

gitgitgadget bot commented Dec 5, 2019

This patch series was integrated into next via git@f7998d9.

@gitgitgadget
Copy link

gitgitgadget bot commented Dec 5, 2019

This patch series was integrated into master via git@f7998d9.

@gitgitgadget gitgitgadget bot added the master label Dec 5, 2019
@gitgitgadget gitgitgadget bot closed this Dec 5, 2019
@gitgitgadget
Copy link

gitgitgadget bot commented Dec 5, 2019

Closed via f7998d9.

@dscho dscho deleted the add-i-in-c-status-and-help branch December 5, 2019 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants