Repository: ansible-collections/community.general

Issue:
git_config support for specifying a file (git config -f option)
<!--- Verify first that your feature was not already discussed on GitHub -->
<!--- Complete *all* sections as described, this form is processed automatically -->

##### SUMMARY
<!--- Describe the new feature/improvement briefly below -->
I'd like to see the git_config module support the -f option in git config to specify an explicit config file to modify.

##### ISSUE TYPE
- Feature Idea

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below, use your best guess if unsure -->
git_config

##### ADDITIONAL INFORMATION
<!--- Describe how the feature would be used, why it is needed and what it would solve -->
This is useful for tools that use gitconfig format such as Gerrit for configuration when you don't want to use a template.

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Add a setting to /var/gerrit/etc/gerrit.config
  community.general.git_config:
    name: container.heapLimit
    scope: file
    file: /var/gerrit/etc/gerrit.config
    value: 8g
```

<!--- HINT: You can also paste gist.github.com links for larger files -->



Discussion:
Files identified in the description:
* [`plugins/modules/source_control/git_config.py`](https://github.com/ansible-collections/community.general/blob/master/plugins/modules/source_control/git_config.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @djmattyg007 @mgedmin
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/plugins/modules/source_control/git_config.py b/plugins/modules/source_control/git_config.py
--- a/plugins/modules/source_control/git_config.py
+++ b/plugins/modules/source_control/git_config.py
@@ -27,7 +27,7 @@
 options:
   list_all:
     description:
-      - List all settings (optionally limited to a given I(scope))
+      - List all settings (optionally limited to a given I(scope)).
     type: bool
     default: 'no'
   name:
@@ -38,13 +38,19 @@
     description:
       - Path to a git repository for reading and writing values from a
         specific repo.
+  file:
+    description:
+      - Path to an adhoc git configuration file to be managed using the C(file) scope.
+    type: path
+    version_added: 2.0.0
   scope:
     description:
-      - Specify which scope to read/set values from. This is required
-        when setting config values. If this is set to local, you must
-        also specify the repo parameter. It defaults to system only when
-        not using I(list_all)=yes.
-    choices: [ "local", "global", "system" ]
+      - Specify which scope to read/set values from.
+      - This is required when setting config values.
+      - If this is set to C(local), you must also specify the C(repo) parameter.
+      - If this is set to C(file), you must also specify the C(file) parameter.
+      - It defaults to system only when not using I(list_all)=C(yes).
+    choices: [ "file", "local", "global", "system" ]
   state:
     description:
       - "Indicates the setting should be set/unset.
@@ -160,12 +166,16 @@ def main():
             list_all=dict(required=False, type='bool', default=False),
             name=dict(type='str'),
             repo=dict(type='path'),
-            scope=dict(required=False, type='str', choices=['local', 'global', 'system']),
+            file=dict(type='path'),
+            scope=dict(required=False, type='str', choices=['file', 'local', 'global', 'system']),
             state=dict(required=False, type='str', default='present', choices=['present', 'absent']),
-            value=dict(required=False)
+            value=dict(required=False),
         ),
         mutually_exclusive=[['list_all', 'name'], ['list_all', 'value'], ['list_all', 'state']],
-        required_if=[('scope', 'local', ['repo'])],
+        required_if=[
+            ('scope', 'local', ['repo']),
+            ('scope', 'file', ['file'])
+        ],
         required_one_of=[['list_all', 'name']],
         supports_check_mode=True,
     )
@@ -207,6 +217,10 @@ def main():
     if name:
         args.append(name)
 
+    if scope == 'file':
+        args.append('-f')
+        args.append(params['file'])
+
     if scope == 'local':
         dir = params['repo']
     elif params['list_all'] and params['repo']:
@@ -243,10 +257,10 @@ def main():
     if not module.check_mode:
         if unset:
             args.insert(len(args) - 1, "--" + unset)
-            cmd = ' '.join(args)
+            cmd = args
         else:
             new_value_quoted = shlex_quote(new_value)
-            cmd = ' '.join(args + [new_value_quoted])
+            cmd = args + [new_value_quoted]
         try:  # try using extra parameter from ansible-base 2.10.4 onwards
             (rc, out, err) = module.run_command(cmd, cwd=dir, ignore_invalid_cwd=False)
         except TypeError:


Test patch: