Blame CONTRIBUTING

Packit 5756e2
Guidelines for Contributing
Packit 5756e2
===========================
Packit 5756e2
Packit Service a1bd4f
Packit 5756e2
Coding Standard
Packit 5756e2
---------------
Packit 5756e2
Packit Service a1bd4f
The formatting uses clang-format with clang 11.0. Run
Packit Service a1bd4f
`./contrib/scripts/nm-code-format.sh -i` to reformat the code
Packit Service a1bd4f
or call `clang-format` yourself.
Packit Service a1bd4f
You may also call `./contrib/scripts/nm-code-format-container.sh`
Packit Service a1bd4f
which runs a Fedora 33 container using podman.
Packit Service a1bd4f
You are welcome to not bother and open a merge request with
Packit Service a1bd4f
wrong formatting, but note that we then will automatically adjust
Packit Service a1bd4f
your contribution before merging.
Packit Service a1bd4f
Packit Service a1bd4f
The automatic reformatting was done by commit 328fb90f3e0d4e35975aff63944ac0412d7893a5.
Packit Service a1bd4f
Use `--ignore-rev` option or `--ignore-revs-file .git-blame-ignore-revs` to ignore
Packit Service a1bd4f
the reformatting commit with git-blame:
Packit Service a1bd4f
Packit Service a1bd4f
```
Packit Service a1bd4f
$ git config --add 'blame.ignoreRevsFile' '.git-blame-ignore-revs'
Packit Service a1bd4f
```
Packit 5756e2
Packit Service a1bd4f
Since our coding style is entirely automated, the following are just
Packit Service a1bd4f
some details of the style we use:
Packit 5756e2
Packit Service a1bd4f
* Indent with 4 spaces. (_no_ tabs).
Packit Service a1bd4f
Packit Service a1bd4f
* Have no space between the function name and the opening '('.
Packit Service a1bd4f
  - GOOD: `g_strdup(x)`
Packit Service a1bd4f
  - BAD:  `g_strdup (x)`
Packit 5756e2
Packit 5756e2
* C-style comments
Packit Service a1bd4f
  - GOOD: `f(x);  /* comment */`
Packit Service a1bd4f
  - BAD:  `f(x);  // comment`
Packit 5756e2
Packit 5756e2
* Keep assignments in the variable declaration area pretty short.
Packit Service a1bd4f
  - GOOD: `MyObject *object;`
Packit Service a1bd4f
  - BAD:  `MyObject *object = complex_and_long_init_function(arg1, arg2, arg3);`
Packit 5756e2
Packit 5756e2
* 80-cols is a guideline, don't make the code uncomfortable in order to fit in
Packit 5756e2
  less than 80 cols.
Packit 5756e2
Packit 5756e2
* Constants are CAPS_WITH_UNDERSCORES and use the preprocessor.
Packit Service a1bd4f
  - GOOD: `#define MY_CONSTANT 42`
Packit Service a1bd4f
  - BAD:  `static const unsigned myConstant = 42;`
Packit Service a1bd4f
Packit 5756e2
Packit 5756e2
Legal
Packit 5756e2
-----
Packit 5756e2
Packit 5756e2
NetworkManager is partly licensed under terms of GNU Lesser General Public License
Packit 5756e2
version 2 or later (LGPL-2.1+). That is for example the case for libnm.
Packit 5756e2
For historical reasons, the daemon itself is licensed under terms of GNU General
Packit 5756e2
Public License, version 2 or later (GPL-2.0+). See the license comment in the source
Packit 5756e2
files.
Packit 5756e2
Note that all new contributions to NetworkManager MUST be made under terms of
Packit 5756e2
LGPL-2.1+, that is also the case for parts that are currently licensed GPL-2.0+.
Packit 5756e2
The reason for that is that we might eventually relicense everything as LGPL and
Packit 5756e2
new contributions already must agree with that future change.
Packit Service a1bd4f
For more details see [RELICENSE.md](RELICENSE.md).
Packit Service a1bd4f
Packit 5756e2
Packit 5756e2
Assertions in NetworkManager code
Packit 5756e2
---------------------------------
Packit 5756e2
Packit 5756e2
There are different kind of assertions. Use the one that is appropriate.
Packit 5756e2
Packit 5756e2
1) g_return_*() from glib. This is usually enabled in release builds and
Packit 5756e2
  can be disabled with G_DISABLE_CHECKS define. This uses g_log() with
Packit 5756e2
  G_LOG_LEVEL_CRITICAL level (which allows the program to continue,
Packit 5756e2
  unless G_DEBUG=fatal-criticals or G_DEBUG=fatal-warnings is set). As such,
Packit 5756e2
  this is usually the preferred way for assertions that are supposed to be
Packit 5756e2
  enabled by default.
Packit 5756e2
Packit 5756e2
  Optimally, after a g_return_*() failure the program can still continue. This is
Packit 5756e2
  also the reason why g_return_*() is preferable over g_assert().
Packit 5756e2
  For example, that is often not given for functions that return a GError, because
Packit 5756e2
  g_return_*() will return failure without setting the error output. That often leads
Packit 5756e2
  to a crash immidiately after, because the caller requires the GError to be set.
Packit 5756e2
  Make a reasonable effort so that an assertion failure may allow the process
Packit 5756e2
  to proceed. But don't put too much effort in it. After all, it's an assertion
Packit 5756e2
  failure that is not supposed to happen either way.
Packit 5756e2
Packit 5756e2
2) nm_assert() from NetworkManager. This is disabled by default in release
Packit 5756e2
  builds, but enabled if you build --with-more-assertions. See "WITH_MORE_ASSERTS"
Packit 5756e2
  define. This is preferred for assertions that are expensive to check or
Packit 5756e2
  nor necessary to check frequently. It's also for conditions that can easily
Packit 5756e2
  verified to be true and where future refactoring is unlikley to break that
Packit 5756e2
  condition.
Packit 5756e2
  Use this deliberately and assume it is removed from production builds.
Packit 5756e2
Packit 5756e2
3) g_assert() from glib. This is used in unit tests and commonly enabled
Packit 5756e2
  in release builds. It can be disabled with G_DISABLE_ASSERT assert
Packit 5756e2
  define. Since this results in a hard crash on assertion failure, you
Packit 5756e2
  should almost always prefer g_return_*() over this (except in unit tests).
Packit 5756e2
Packit 5756e2
4) assert() from <assert.h>. It is usually enabled in release builds and
Packit 5756e2
  can be disabled with NDEBUG define. Don't use it in NetworkManager,
Packit 5756e2
  it's basically like g_assert().
Packit 5756e2
Packit 5756e2
5) g_log() from glib. These are always compiled in, depending on the logging level
Packit 5756e2
  these are assertions too. G_LOG_LEVEL_ERROR aborts the program, G_LOG_LEVEL_CRITICAL
Packit 5756e2
  logs a critical warning (like g_return_*(), see G_DEBUG=fatal-criticals)
Packit 5756e2
  and G_LOG_LEVEL_WARNING logs a warning (see G_DEBUG=fatal-warnings).
Packit 5756e2
  G_LOG_LEVEL_DEBUG level is usually not printed, unless G_MESSAGES_DEBUG environment
Packit 5756e2
  is set.
Packit 5756e2
  In general, avoid using g_log() in NetworkManager. We have nm-logging instead
Packit 5756e2
  which logs to syslog/systemd-journald.
Packit 5756e2
  From a library like libnm it might make sense to log warnings (if someting
Packit 5756e2
  is really wrong) or debug messages. But better don't. If it's important,
Packit 5756e2
  find a way to report the notification via the API to the caller. If it's
Packit 5756e2
  not important, keep silent.
Packit 5756e2
  In particular, don't use levels G_LOG_LEVEL_CRITICAL and G_LOG_LEVEL_WARNING because
Packit 5756e2
  these are effectively assertions and we want to run with G_DEBUG=fatal-warnings.
Packit 5756e2
Packit 5756e2
6) g_warn_if_*() from glib. These are always compiled in and log a G_LOG_LEVEL_WARNING
Packit 5756e2
  warning. Don't use this.
Packit 5756e2
Packit 5756e2
7) G_TYPE_CHECK_INSTANCE_CAST() from glib. Unless building with "WITH_MORE_ASSERTS",
Packit 5756e2
  we set G_DISABLE_CAST_CHECKS. This means, cast macros like NM_DEVICE(ptr)
Packit 5756e2
  translate to plain C pointer casts. Use such cast macros deliberately, in production
Packit 5756e2
  code they are cheap, with more asserts enabled the check that the pointer type is
Packit 5756e2
  suitable.
Packit 5756e2
Packit 5756e2
Of course, every assertion failure is a bug, and calling it must have no side effects.
Packit 5756e2
Packit 5756e2
Theoretically, you are welcome to disable G_DISABLE_CHECKS and G_DISABLE_ASSERT
Packit 5756e2
in production builds. In practice, nobody tests such a configuration, so beware.
Packit 5756e2
Packit 5756e2
For testing, you also want to run NetworkManager with environment variable
Packit 5756e2
G_DEBUG=fatal-warnings to crash upon G_LOG_LEVEL_CRITICAL and G_LOG_LEVEL_WARNING
Packit 5756e2
g_log() message. NetworkManager won't use these levels for regular logging
Packit 5756e2
but for assertions.
Packit 5756e2
Packit 5756e2
Packit 5756e2
Git Notes (refs/notes/bugs)
Packit 5756e2
---------------------------
Packit 5756e2
Packit 5756e2
There are special notes to annotate git commit messages with information
Packit 5756e2
about "Fixes" and "cherry picked from". Annotating the history is useful
Packit 5756e2
if it was not done initially because our scripts can make use of it.
Packit 5756e2
Packit 5756e2
The notes it are called "refs/notes/bugs".
Packit 5756e2
Packit 5756e2
So configure:
Packit 5756e2
Packit Service a1bd4f
```
Packit Service a1bd4f
$ git config --add 'remote.origin.fetch' 'refs/notes/bugs:refs/notes/bugs'
Packit Service a1bd4f
$ git config --add 'notes.displayref' 'refs/notes/bugs'
Packit Service a1bd4f
```
Packit 5756e2
Packit 5756e2
For example, set notes with
Packit 5756e2
Packit Service a1bd4f
```
Packit Service a1bd4f
$ git notes --ref refs/notes/bugs add -m "(cherry picked from $COMMIT_SHA)" HEAD
Packit Service a1bd4f
```
Packit 5756e2
Packit 5756e2
You should see the notes in git-log output as well.
Packit 5756e2
Packit 5756e2
To resync our local notes use:
Packit 5756e2
Packit Service a1bd4f
```
Packit Service a1bd4f
$ git fetch origin refs/notes/bugs:refs/notes/bugs -f
Packit Service a1bd4f
```