threads / discuss / 45421

Is there a way to have a local version of a header file?

Subject: Is there a way to have a local version of a header file?

## tl;dr

11 messages between Mar 18, 2017 and Mar 19, 2017.

replies: 10people: 6as markdown or json

David Lang· Mar 18, 2017, 14:29 UTC · lore

for an embedded project built inside the Arduino IDE, (alternate firmware for a home automation project) there is a need to set a number of parameters that we really don't want in the main repo (wifi network IDs/passwords)

right now, we have these things set as #defines in a header file.
We need to distribute a base version of this file for new people to get started.

Is there any way to have git define a file in such a way that if it doesn't exist in the worktree it gets populated, but if it does exist it doesn't get overwritten? (as I type this, I'm thinking a trigger may work, but we need it to work on Linux, Windows and OSX)

Any thoughts on a sane way to handle this situation?
David Lang
Ævar Arnfjörð Bjarmason· Mar 18, 2017, 14:48 UTC · re: David Lang · lore

Re: Is there a way to have a local version of a header file?

On Sat, Mar 18, 2017 at 3:29 PM, David Lang <david@lang.hm> wrote:
Show 15 quoted lines
> for an embedded project built inside the Arduino IDE, (alternate firmware
> for a home automation project) there is a need to set a number of parameters
> that we really don't want in the main repo (wifi network IDs/passwords)
>
> right now, we have these things set as #defines in a header file.
>
> We need to distribute a base version of this file for new people to get
> started.
>
> Is there any way to have git define a file in such a way that if it doesn't
> exist in the worktree it gets populated, but if it does exist it doesn't get
> overwritten? (as I type this, I'm thinking a trigger may work, but we need
> it to work on Linux, Windows and OSX)
>
> Any thoughts on a sane way to handle this situation?
There's no sane way to do what you're describing without renaming the file.
But the sanest way to do this is to have a config.h.example
Then you have "/config.h" in the .gitignore file.

And you tell the users to copy the *.example file to *.h, and your program then includes the *.h file.

If you wanted to provide defaults you could just #include the config.h.example first, so #defines in the *.h file would clobber those in the *.example.

David Lang· Mar 18, 2017, 14:58 UTC · re: Ævar Arnfjörð Bjarmason · lore

Re: Is there a way to have a local version of a header file?

On Sat, 18 Mar 2017, Ævar Arnfjörð Bjarmason wrote:
Show 29 quoted lines
> On Sat, Mar 18, 2017 at 3:29 PM, David Lang <david@lang.hm> wrote:
>> for an embedded project built inside the Arduino IDE, (alternate firmware
>> for a home automation project) there is a need to set a number of parameters
>> that we really don't want in the main repo (wifi network IDs/passwords)
>>
>> right now, we have these things set as #defines in a header file.
>>
>> We need to distribute a base version of this file for new people to get
>> started.
>>
>> Is there any way to have git define a file in such a way that if it doesn't
>> exist in the worktree it gets populated, but if it does exist it doesn't get
>> overwritten? (as I type this, I'm thinking a trigger may work, but we need
>> it to work on Linux, Windows and OSX)
>>
>> Any thoughts on a sane way to handle this situation?
>
> There's no sane way to do what you're describing without renaming the file.
>
> But the sanest way to do this is to have a config.h.example
>
> Then you have "/config.h" in the .gitignore file.
>
> And you tell the users to copy the *.example file to *.h, and your
> program then includes the *.h file.
>
> If you wanted to provide defaults you could just #include the
> config.h.example first, so #defines in the *.h file would clobber
> those in the *.example.
That's what we currently have (user_config.h and user_config_override.h)

I was hoping to not have the situation where downloading and trying to compile will complain about a missing include file (if the users don't copy user_config_override_example.h to user_config_override.h) while letting us do a .gitignore on user_config_override.h

for many people using this project, this is the first time they have ever compiled anything, and we have the typical set of people not reading instructions :-/

Darn, I was hoping that the scenario of needing to have a config file provided in the repo, while not overwriting local changes to it was common enough that there were some tricks available. This is a little harder as the running code doesn't have a filesystem so we are limited to what we can do in the compiler and git (no makefile even, the Arduino folks consider that too complicated, it just slurps up all .ino files in a directory and compiles them)

Ævar Arnfjörð Bjarmason· Mar 18, 2017, 17:08 UTC · re: David Lang · lore

Re: Is there a way to have a local version of a header file?

On Sat, Mar 18, 2017 at 3:58 PM, David Lang <david@lang.hm> wrote:
Show 58 quoted lines
> On Sat, 18 Mar 2017, Ævar Arnfjörð Bjarmason wrote:
>
>> On Sat, Mar 18, 2017 at 3:29 PM, David Lang <david@lang.hm> wrote:
>>>
>>> for an embedded project built inside the Arduino IDE, (alternate firmware
>>> for a home automation project) there is a need to set a number of
>>> parameters
>>> that we really don't want in the main repo (wifi network IDs/passwords)
>>>
>>> right now, we have these things set as #defines in a header file.
>>>
>>> We need to distribute a base version of this file for new people to get
>>> started.
>>>
>>> Is there any way to have git define a file in such a way that if it
>>> doesn't
>>> exist in the worktree it gets populated, but if it does exist it doesn't
>>> get
>>> overwritten? (as I type this, I'm thinking a trigger may work, but we
>>> need
>>> it to work on Linux, Windows and OSX)
>>>
>>> Any thoughts on a sane way to handle this situation?
>>
>>
>> There's no sane way to do what you're describing without renaming the
>> file.
>>
>> But the sanest way to do this is to have a config.h.example
>>
>> Then you have "/config.h" in the .gitignore file.
>>
>> And you tell the users to copy the *.example file to *.h, and your
>> program then includes the *.h file.
>>
>> If you wanted to provide defaults you could just #include the
>> config.h.example first, so #defines in the *.h file would clobber
>> those in the *.example.
>
>
> That's what we currently have (user_config.h and user_config_override.h)
>
> I was hoping to not have the situation where downloading and trying to
> compile will complain about a missing include file (if the users don't copy
> user_config_override_example.h to user_config_override.h) while letting us
> do a .gitignore on user_config_override.h
>
> for many people using this project, this is the first time they have ever
> compiled anything, and we have the typical set of people not reading
> instructions :-/
>
> Darn, I was hoping that the scenario of needing to have a config file
> provided in the repo, while not overwriting local changes to it was common
> enough that there were some tricks available. This is a little harder as the
> running code doesn't have a filesystem so we are limited to what we can do
> in the compiler and git (no makefile even, the Arduino folks consider that
> too complicated, it just slurps up all .ino files in a directory and
> compiles them)

There might be some way I haven't thought of, in particular maybe you can use gitattributes to define a custom diff/merge driver that always reports no changes, or some ways to (ab)use the index to make git ignore any changes to the file.

Junio C Hamano· Mar 18, 2017, 18:05 UTC · re: Ævar Arnfjörð Bjarmason · lore

Re: Is there a way to have a local version of a header file?

Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:
> There might be some way I haven't thought of, in particular maybe you
> can use gitattributes to define a custom diff/merge driver that always
> reports no changes, or some ways to (ab)use the index to make git
> ignore any changes to the file.
Why does this have to be so difficult?

Ship a config.h.sample file, have a Makefile rule that is forced to run before any compilation happens that checks if config.h exists and then created it if missing by copying config.h.sample over, and then all other source files can include config.h without having to know anything about config.h.sample's existence.

Did I miss something?
David Lang· Mar 18, 2017, 22:40 UTC · re: Junio C Hamano · lore

Re: Is there a way to have a local version of a header file?

On Sat, 18 Mar 2017, Junio C Hamano wrote:
Show 16 quoted lines
> Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:
>
>> There might be some way I haven't thought of, in particular maybe you
>> can use gitattributes to define a custom diff/merge driver that always
>> reports no changes, or some ways to (ab)use the index to make git
>> ignore any changes to the file.
>
> Why does this have to be so difficult?
>
> Ship a config.h.sample file, have a Makefile rule that is forced to
> run before any compilation happens that checks if config.h exists
> and then created it if missing by copying config.h.sample over, and
> then all other source files can include config.h without having to
> know anything about config.h.sample's existence.
>
> Did I miss something?
There is no makefile with the arduino IDE/build system :-(
David Lang
Junio C Hamano· Mar 18, 2017, 23:11 UTC · re: David Lang · lore

Re: Is there a way to have a local version of a header file?

David Lang <david@lang.hm> writes:
Show 9 quoted lines
>> Ship a config.h.sample file, have a Makefile rule that is forced to
>> run before any compilation happens that checks if config.h exists
>> and then created it if missing by copying config.h.sample over, and
>> then all other source files can include config.h without having to
>> know anything about config.h.sample's existence.
>>
>> Did I miss something?
>
> There is no makefile with the arduino IDE/build system :-(

How does "the build system" you want to make it work with actually work? Is it incapable of "compiling" a "source file" into an "object file" that happens to be a text using an arbitrary "compiler"?

I was hoping that readers are imaginative enough to replace Makefile with whatever way things are normally built with when reading my message, and the reader can just replace "source file" with "config.h.sample", "compiler" with "test -f config.h || cat config.h.sample >config.h" and "object file" with "config.h".

Samuel Lijin· Mar 18, 2017, 23:22 UTC · re: Junio C Hamano · lore

Re: Is there a way to have a local version of a header file?

Arduino is basically a simplified/streamlined cross-compilation toolchain with very tightly coupled IDE integration.

I'd just provide a .sample and tell people what to do with it in the README. The alternative is to provide config.h as is and tell people to use "git update-index --assume-unchanged" immediately after cloning to ignore changes to the file, but this is prone to people accidentally committing credentials.

On Sat, Mar 18, 2017 at 6:11 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 22 quoted lines
> David Lang <david@lang.hm> writes:
>
>>> Ship a config.h.sample file, have a Makefile rule that is forced to
>>> run before any compilation happens that checks if config.h exists
>>> and then created it if missing by copying config.h.sample over, and
>>> then all other source files can include config.h without having to
>>> know anything about config.h.sample's existence.
>>>
>>> Did I miss something?
>>
>> There is no makefile with the arduino IDE/build system :-(
>
> How does "the build system" you want to make it work with actually
> work?  Is it incapable of "compiling" a "source file" into an
> "object file" that happens to be a text using an arbitrary
> "compiler"?
>
> I was hoping that readers are imaginative enough to replace Makefile
> with whatever way things are normally built with when reading my
> message, and the reader can just replace "source file" with
> "config.h.sample", "compiler" with "test -f config.h || cat
> config.h.sample >config.h" and "object file" with "config.h".
Johannes Sixt· Mar 19, 2017, 21:33 UTC · re: Samuel Lijin · lore

Re: Is there a way to have a local version of a header file?

Am 19.03.2017 um 00:22 schrieb Samuel Lijin:
Show 5 quoted lines
> I'd just provide a .sample and tell people what to do with it in the
> README. The alternative is to provide config.h as is and tell people
> to use "git update-index --assume-unchanged" immediately after cloning
> to ignore changes to the file, but this is prone to people
> accidentally committing credentials.

Please do not suggest to use --assume-unchanged. It is not intended for this purpose, and if used in the way you suggest, it will sooner or later bite back.

--assume-unchanged is a promise to Git that you do not change the file. If you break the promise, you get what you deserve ;)

-- Hannes
David Lang· Mar 19, 2017, 00:19 UTC · re: Junio C Hamano · lore

Re: Is there a way to have a local version of a header file?

On Sat, 18 Mar 2017, Junio C Hamano wrote:
Show 16 quoted lines
> David Lang <david@lang.hm> writes:
>
>>> Ship a config.h.sample file, have a Makefile rule that is forced to
>>> run before any compilation happens that checks if config.h exists
>>> and then created it if missing by copying config.h.sample over, and
>>> then all other source files can include config.h without having to
>>> know anything about config.h.sample's existence.
>>>
>>> Did I miss something?
>>
>> There is no makefile with the arduino IDE/build system :-(
>
> How does "the build system" you want to make it work with actually
> work?  Is it incapable of "compiling" a "source file" into an
> "object file" that happens to be a text using an arbitrary
> "compiler"?

It looks for all *.ino files (which need to contain C code) in the specified ("project") directory and compiles and links them all into one blob, it adds a smidge of code at the beginning to run setup() followed by loop(). It then dumps this blob (via serial/USB) into the flash of the device that will run it.

it's a very dumbed down system, designed for non-programmers to do trivial things (blink a few LEDs, etc) that's been pushed to far more sophisticated uses than it was ever designed for.

David Lang
Show 6 quoted lines
> I was hoping that readers are imaginative enough to replace Makefile
> with whatever way things are normally built with when reading my
> message, and the reader can just replace "source file" with
> "config.h.sample", "compiler" with "test -f config.h || cat
> config.h.sample >config.h" and "object file" with "config.h".
>
Jakub Narębski· Mar 18, 2017, 18:21 UTC · re: Ævar Arnfjörð Bjarmason · lore

Re: Is there a way to have a local version of a header file?

W dniu 18.03.2017 o 18:08, Ævar Arnfjörð Bjarmason pisze:
> There might be some way I haven't thought of, in particular maybe you
> can use gitattributes to define a custom diff/merge driver that always
> reports no changes, or some ways to (ab)use the index to make git
> ignore any changes to the file.

There is `git update-index --skip-worktree` (originally meant for sparse checkout), which you can use to kind of ignore changes to tracked file, in a safe way (though sometimes annoying, when it prevents stashing changes).

There is also an existing solution of a hook that prevents commiting files with passwords in them; I forgot the name...

HTH,
-- 
Jakub Narębski

← back to recent threads