Git Filters

Git filters are actions that can be applied to files before and after certain operations in the git workflow. One that is of particular interest to our workflow are clean and smudge filters. Those are filters applied before the staging operation and after the checkout operation respectively.

For our workflow, we are interested in changing the source of the flight_tools package in the Cargo.toml file from a git repository to a local path in our local workspace, but we want to keep the git path in the remote repository.

This can be achieved by keeping your local in a dirty state and making sure you never commit the change in location, but this is very error-prone and can make it very difficult to stage other changes to the package manifest file. In comparison, a filter can take care of all that repetitive manipulation automatically.

Setting up the filter

The filters first have to be defined in the git config file, either the package specific or the global config. Since this is a filter we'll want to apply to all the UFS packages, I recommend setting it up in the global config.

[filter "ft"]
  smudge = "sed '/[Dd]rone[Bb]ase/s/\\.git//g;s|git=\"https://github.com/[Dd]rone[Bb]ase|path=\"..|g;' "
  clean = "sed 's|path=\"\\.\\.|git=\"https://github.com/DroneBase|g;/DroneBase/s|"}|\\.git\"}|g' "

Adding this to the gitconfig will define a filter called ft that will use sed to find and replace strings. The smudge filter starts by replacing the .git suffix of the url if this is a dronebase repo, then replaces the whole git url with the local path. The clean filter does the reverse, replace the local path with the git url and adds the suffix if the line contains a dronebase url.

Using the filter

Once the filter is defined, it needs to be associated to certain files. This is achieved on a per-repo and per-file basis. In the repository, the .git/info/attributes file is used for this purpose.

Cargo.toml filter=ft

This will associate the ft filter to the Cargo.toml file. This will be needed for every repo and every file you want to associate filters.