Re: [PATCH 00/12] Remove more index compatibility macros
- From
Eric Sunshine <sunshine@sunshineco.com>
- Date
- Jan 2, 2021, 06:12 UTC
- Message-ID
- <CAPig+cS-oDfCX-LaDEW2U+9Aa9VNZUPs54isODoHCv6HxAdZeQ@mail.gmail.com>
- In-Reply-To
- <pull.830.git.1609506428.gitgitgadget@gmail.com>
On Fri, Jan 1, 2021 at 8:09 AM Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com> wrote:
Show 8 quoted lines
> My strategy for update-index was to create static globals "repo" and > "istate" that point to the_repository and the_index, respectively. Then, I > was able to remove macros one-by-one without changing method prototypes > within the file. > > I had started trying to keep everything local to the method signatures, but > I hit a snag when reaching the command-line parsing callbacks, which I could > not modify their call signature. [...]
You should be able to do this, not by modifying the callback signature, but by taking advantage of the `extra` member of `struct option` which is available to callback functions or arbitrary use. If you need to access the index in a callback, then assign a `struct index_state *` to `extra`; likewise assign a `struct repository *` to `extra` to access the repository. If you need access to both the index and the repository, then just store the repository in `extra` since the repository has an `index` field.
You won't be able to use any of the canned OPT_FOO() macros to initialize an entry in the update-index.c `options[]` array which needs `extra`-initialization since the macros don't let you specify `extra`, but you can easily bypass the macro and initialize the `struct option` manually. (After all, the macros exist for convenience; they are not a hard requirement.)
Within the callback, extract the `repository` or `index_state` as you would any other field. For instance:
const struct repository *repo = opt->extra;
This should allow you to get rid of the globals introduced by patch [4/12] (assuming passing the index and repo arguments around everywhere doesn't get overly hairy).