Skip to content

add preservation of comment lines#20

Open
wififreedom wants to merge 1 commit intoopenwrt:masterfrom
wififreedom:preserve_comments
Open

add preservation of comment lines#20
wififreedom wants to merge 1 commit intoopenwrt:masterfrom
wififreedom:preserve_comments

Conversation

@wififreedom
Copy link
Copy Markdown

@wififreedom wififreedom commented Mar 31, 2026

This pull request implements preservation of comments in configuration files.

Comment lines before an entry, and the comment at the end of an entry are considered to be associated with that entry.

Example:

# this comment line is associated with section2

  # this comment line is associated with section2
config type section2 # this comment is associated with section 2
# this comment line is associated with opt
    
        # this comment line is associated with opt
        option opt 'val' # this comment is associated with opt
    
      # this comment line is not associated with any entry

Wih option -D, uci works as before and strips comments. The following commands have enhanced behaviour:

  • uci import preserves comments of unchanged configuration entries, and imports entries with their associated comments
  • uci export exports entries with their associated comments
  • uci 'commit preserves comments of unchanged configuration entries when committing changes with uci commands 'add', 'delete', 'set', 'add_list', and/or 'del_list'.
  • uci get '<config>.<section>' '#' shows the section comment
  • uci get '<config>.<section>.<option> '#' shows the option comment
  • uci set '<config>.<section>=<value>' '<comment>' sets the section value and comment
  • uci set '<config>.<section>.option=<value>' '<comment>' sets the option value and comment
  • uci add_list '<config>.<section>.option=<value> '<comment>' adds a list entry with value and comment
  • uci changes also shows comments
  • uci show does not show comments
  • uci batch supports the above commands

Features:

  • Comments are auto-indented if preserved, set or added.
  • The new delta file format is both backward compatible and forward compatible. Older uci versions interpret the addition in the new format as comment and ignores it.
  • Comments at the end of the file are not preserved because they do not belong to an entry.

I've added lots of shunit2 tests to verify the implementation.

An example of a result: the above example after and option 'opt2' has been added:


# this comment line is associated with section2
# this comment line is associated with section2
config type section2 # this comment is associated with section 2
        # this comment line is associated with opt
        # this comment line is associated with opt
        option opt 'val' # this comment is associated with opt
        # comment line for new option 'opt2'
        option opt2 'val2'

Comment thread file.c Outdated
@GeorgeSapkin
Copy link
Copy Markdown
Member

You'll need to look into the submission guidelines regarding commit formatting, etc.

@wififreedom wififreedom marked this pull request as ready for review April 2, 2026 14:16
@wififreedom wififreedom force-pushed the preserve_comments branch 3 times, most recently from 92f0747 to bf0a598 Compare April 3, 2026 11:21
@wififreedom wififreedom marked this pull request as draft April 4, 2026 09:06
@wififreedom wififreedom force-pushed the preserve_comments branch 7 times, most recently from 56181fc to 0cb0626 Compare April 6, 2026 22:06
@wififreedom wififreedom marked this pull request as ready for review April 6, 2026 22:33
@wififreedom wififreedom force-pushed the preserve_comments branch 2 times, most recently from f1c8b3c to db733c0 Compare April 11, 2026 05:19
This pull request implements preservation of comments in configuration
files.

Comment lines before an entry, and the comment at the end of an entry
are considered to be associated with that entry.

Example:

  # this comment line is associated with section

    # this comment line is associated with section
  config type 'section' # this comment is associated with section
  # this comment line is associated with opt

          # this comment line is associated with opt
          option opt 'val' # this comment is associated with opt

        # this comment line is not associated with any entry

Wih option -D, uci works as before and strips comments.

The following commands have enhanced behaviour:
  * uci import
    preserves comments of unchanged configuration entries, and imports
    entries with their associated comments
  * uci export
    exports entries with their associated comments
  * uci commit
    preserves comments of unchanged configuration entries when
    committing changes with uci commands 'add', 'delete', 'set',
    'add_list', and/or 'del_list'.
  * uci get '<config>.<section>' '#'
    shows the section comment
  * uci get '<config>.<section>.<option> '#'
    shows the option comment
  * uci set '<config>.<section>=<value>' '<comment>'
    sets the section value and comment
  * uci set '<config>.<section>.option=<value>' '<comment>'
    sets the option value and comment
  * uci add_list '<config>.<section>.option=<value> '<comment>'
    adds a list entry with value and comment
  * uci changes
    also shows comments
  * uci show
    does not show comments
  * uci batch
    supports the above commands

Features:
  * Comments are auto-indented if preserved, set or added.
  * The new delta file format is both backward compatible and forward
    compatible. Older uci versions interpret the addition in the new
    format as comment and ignore it.
  * Comments at the end of the file are not preserved because they do
    not belong to an entry.

An example of a result: the above example after and option 'opt2' has
been added with a comment:

  # this comment line is associated with section2
  # this comment line is associated with section2
  config type section2 # this comment is associated with section 2
          # this comment line is associated with opt
          # this comment line is associated with opt
          option opt 'val' # this comment is associated with opt
          # comment line for new option 'opt2'
          option opt2 'val2'

Signed-off-by: Bastiaan Stougie <wififreedom2026@protonmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants