Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_config_settings errors on blank lines returned by git config --list #603

Open
GavinMasterson opened this issue Oct 9, 2024 · 0 comments

Comments

@GavinMasterson
Copy link

Issue Description

When running git filter-repo, I get an error from the get_config_settings function.

Cause

The output of line 1556's subproc.check_output(git config --list.split(), cwd=repo_working_dir) is equivalent to:

output = b'''[email protected]
user.name=My Name

includeif.gitdir/i:~/work/.path=~/work/.gitconfig-work
[email protected]
user.name=My Name

credential.helper=cache
init.defaultbranch=main
includeif.hasconfig:remote.*.url:[email protected]:work/**.path=~/work/.gitconfig-work
credential.helper=cache
init.defaultbranch=main
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true'''

The conversion of the output object to the returned dict fails with the message:

Traceback (most recent call last):
File "/usr/bin/git-filter-repo", line 4151, in
main()
File "/usr/bin/git-filter-repo", line 4148, in main
filter.run()
File "/usr/bin/git-filter-repo", line 4068, in run
self._run_sanity_checks()
File "/usr/bin/git-filter-repo", line 2931, in _run_sanity_checks
self._config_settings = GitUtils.get_config_settings(target_working_dir)
File "/usr/bin/git-filter-repo", line 1563, in get_config_settings
return dict(line.split(b'=', maxsplit=1)
ValueError: dictionary update sequence element #2 has length 1; 2 is required

Potential Solution

I fixed the issue by modifying the function as follows:

@staticmethod
  def get_config_settings(repo_working_dir):
    output = ''
    try:
      output = subproc.check_output('git config --list'.split(),
                                    cwd=repo_working_dir)
    except subprocess.CalledProcessError as e: # pragma: no cover
      raise SystemExit('fatal: {}'.format(e))
    
    # Changes start here -->
    decoded_output = output.decode('utf-8')
    lines = [line for line in decoded_output.split('\n') if line.strip()]
    # FIXME: Ignores multi-valued keys, just let them overwrite for now
    return dict(line.split('=', maxsplit=1) for line in lines if '=' in line)

There are probably better ways to solve the issue that do not involve an opinionated decoding step, as I cannot tell what side-effects the decoding step may have in repos that are not UTF-8 encoded.

Summary

When running my desired git filter-repo ... command with this function, the command works as intended, and raises no error.

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

No branches or pull requests

1 participant