0

We have a team of 5 developers working on a hardware project. We have a user_config.h file containing #defines for each user's specific environment and preferences - eg what hardware version they're using, whether sound should be turned on etc.

Presently, the file is in our .gitignore file, which works well enough. Users can make changes to their settings that don't get pushed. It was previously tracked and has been rm --cached on master branch.

Occasionally we switch to an old branch that didn't have the file, and it gets deleted. The user then has to get someone else to email them a current user_config.h file. We'd like to stop this from happening.

Bonus points: What I'd really like is to have the file tracked, but ignore user changes. Occasionally, the project leads need to add settings to this file when new features are added, so we'd like those changes to be pushed to all users. And if a new user checks out the repo, we'd like them to get the "factory default" settings, which would also be good to change from time to time.

So, ideally, we want to ignore changes to any line of the file, but accept changes where lines are added or deleted. Preferably with the ability to temporarily turn this off so we can change the defaults, (even if this means editing the file directly in github or something like that), overwriting what's on the repo but not users' changes.

Is there any way in git that we can achieve all of this?

UStralian
  • 19
  • 4

2 Answers2

0

Simply don't track config.h.
You can version and track:

  • a config.h.tpl (template file with placeholder values).
  • a default value file, with the factory default setting
  • a script able to take a value file and a template file, and produce the config.h file (which remains untracked, private)
  • a .gitignore which ignores the resulting generated config.h file
  • a .gitattribute declaring a smudge content filter (see below)

Then you can keep outside your local clone a value file where each developer will have their own private values. If the script does not detect that file outside the repo, it would generate the config.h with the default value file.

That generation is automated through a content filter driver, using a .gitattributes declaration.

https://i.stack.imgur.com/tumAc.png
(image from "Customizing Git - Git Attributes" from "Pro Git book"))

Once you declare that content filer driver in your local config, it will automatically, on git checkout, generate your config.h file for you.
See a complete example in "Best practice - Git + Build automation - Keeping configs separate".

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
-1

Bonus points: What I'd really like is to have the file tracked, but ignore user changes

This is what git assume-unchanged is for

https://git-scm.com/docs/git-update-index

--[no-]assume-unchanged

When this flag is specified, the object names recorded for the paths are not updated.

Instead, this option sets/unsets the "assume unchanged" bit for the paths.

When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. If you want to change the working tree file, you need to unset the bit to tell Git. This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (e.g. cifs).

Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.


we'd like them to get the "factory default" settings, which would also be good to change from time to time

As explained above this is whats that will be done, you will be able too check in only if you choose to do so while other changes will be ignored.


So, ideally, we want to ignore changes to any line of the file, but accept changes where lines are added or deleted

use git partial add git add -p and choose what do you want to commit and whats not.

enter image description here

CodeWizard
  • 92,491
  • 19
  • 110
  • 133