Skip to content

Replace more instances of the_index and the_repository #379

Open
@dscho

Description

@dscho

Once upon a time, Git was intended to be just like all the other simplistic Unix utilities, a set of commands to be cobbled together by shell scripts. In those times, it was (deemed) okay to cut corners, using global variables all over the place, and leaving it to exit() to "clean up" allocated memory.

In the meantime, Git has matured a lot, and most of its code is in proper C, and quite some of that code has been prepared for more versatile use already, e.g. for in-process submodule handling, by avoiding global variables.

Two of those global variables are the_index and the_repository. The former represents "the" Git index (AKA staging area) while the latter represents the .git directory.

Ideally, we would have no code in libgit.a (i.e. the internal API functions) that access the_index or the_repository directly. Rather, they should be passed through as function parameters, much like object-oriented languages implicitly pass a this or self parameter.

The convention is to give the repository parameter the short-and-sweet name r, and the index parameter the pretty intuitive name index. A good example to follow is this commit: 90d3405

Note that it is not necessary to pass through both the index and the repository, at least not in general, as the_repository->index is a thing. Only in rare circumstances will a function want to work specifically on a temporary, or potentially temporary, index. Meaning: when adding a struct the_repository *r parameter to a function's signature that already has a struct index_state *index, the latter parameter can be removed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions