{"thread":{"id":"52528","subject":"bogus config file vs. 'git config --edit'","startedAt":"2019-12-27T11:04:38Z","lastAt":"2020-01-08T11:57:26Z","messageCount":2,"participants":["SZEDER Gábor","Jeff King"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"388967","messageId":"20191227110431.GC32750@szeder.dev","threadId":"52528","inReplyTo":null,"subject":"bogus config file vs. 'git config --edit'","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2019-12-27T11:04:31Z","receivedAt":"2019-12-27T11:04:38Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"Let's suppose I somehow ended up with a bogus config file:\n\n  $ tail -n2 .git/config \n  [section]\n          foo bar baz\n\nand now I try to rectify the situation, but I know that poking around\nin the .git directory is a no-no, so instead of 'vim .git/config' I\ntry:\n\n  $ git config --edit\n  fatal: bad config line 81 in file .git/config\n\nUh-oh.\n\nFurthermore, if I don't remember quite clearly the finer points of the\nsyntax of the config file, then I might want to look it up:\n\n  $ git help config\n  fatal: bad config line 81 in file .git/config\n\nI think bith 'git config --edit' and 'git help ...' should just work,\nno matter what nonsense might be in the config file, even if they then\nlaunch a different editor or pager than what are set in the\nconfiguration.\n\n"},{"id":"389468","messageId":"20200108115723.GA3381405@coredump.intra.peff.net","threadId":"52528","inReplyTo":"20191227110431.GC32750@szeder.dev","subject":"Re: bogus config file vs. 'git config --edit'","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2020-01-08T11:57:23Z","receivedAt":"2020-01-08T11:57:26Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Fri, Dec 27, 2019 at 12:04:31PM +0100, SZEDER Gábor wrote:\n\n> I think bith 'git config --edit' and 'git help ...' should just work,\n> no matter what nonsense might be in the config file, even if they then\n> launch a different editor or pager than what are set in the\n> configuration.\n\nAgreed. This has come up before (e.g., [1]) but I think doing it right\nwould need several changes:\n\n  - the config code is too eager to die on a syntax error; it should\n    return an error up the stack\n\n  - the stack looks like this when we first die():\n\n      #0  die (err=0x55555582436f \"%s\") at usage.c:165\n      #1  0x0000555555678ab6 in git_parse_source (fn=0x5555557719c4 <check_repo_format>, data=0x7fffffffe1e0, opts=0x0)\n          at config.c:849\n      #2  0x000055555567a9c5 in do_config_from (top=0x7fffffffdfc0, fn=0x5555557719c4 <check_repo_format>, \n          data=0x7fffffffe1e0, opts=0x0) at config.c:1546\n      #3  0x000055555567aab4 in do_config_from_file (fn=0x5555557719c4 <check_repo_format>, origin_type=CONFIG_ORIGIN_FILE, \n          name=0x555555913220 \".git/config\", path=0x555555913220 \".git/config\", f=0x5555559117c0, data=0x7fffffffe1e0, \n          opts=0x0) at config.c:1574\n      #4  0x000055555567ab80 in git_config_from_file_with_options (fn=0x5555557719c4 <check_repo_format>, \n          filename=0x555555913220 \".git/config\", data=0x7fffffffe1e0, opts=0x0) at config.c:1594\n      #5  0x000055555567abc5 in git_config_from_file (fn=0x5555557719c4 <check_repo_format>, \n          filename=0x555555913220 \".git/config\", data=0x7fffffffe1e0) at config.c:1603\n      #6  0x0000555555771e0b in read_repository_format (format=0x7fffffffe1e0, path=0x555555913220 \".git/config\")\n          at setup.c:523\n      #7  0x0000555555771bb5 in check_repository_format_gently (gitdir=0x555555911750 \".git\", candidate=0x7fffffffe1e0, \n          nongit_ok=0x7fffffffe2cc) at setup.c:460\n      #8  0x000055555577272f in setup_discovered_git_dir (gitdir=0x555555911750 \".git\", cwd=0x5555558c90b0 <cwd>, \n          offset=19, repo_fmt=0x7fffffffe1e0, nongit_ok=0x7fffffffe2cc) at setup.c:770\n      #9  0x0000555555773490 in setup_git_directory_gently (nongit_ok=0x7fffffffe2cc) at setup.c:1100\n      #10 0x00005555555706c8 in run_builtin (p=0x5555558bb980 <commands+576>, argc=2, argv=0x7fffffffe538) at git.c:416\n      #11 0x0000555555570baf in handle_builtin (argc=2, argv=0x7fffffffe538) at git.c:674\n      #12 0x00005555555711b2 in cmd_main (argc=2, argv=0x7fffffffe538) at git.c:842\n      #13 0x000055555563412e in main (argc=2, argv=0x7fffffffe538) at common-main.c:52\n\n     So we'd need to teach read_repository_format() to handle the error\n     and just assume we're not in a Git repo. But then how would \"git\n     config --edit\" realize it needs to edit the repo config file?\n\n  - assuming we get around the above problem, I suspect we'd run into\n    other things that want to read the config (e.g., loading default\n    config like the editor). We could be more lenient everywhere, but I\n    suspect that most of the other commands _do_ want to die on bogus\n    config (rather than do something unexpected). I wouldn't want to\n    change every git_config() call to handle errors, but maybe we could\n    have a lenient form and use it in just enough call-sites. Or maybe\n    we could detect early in git-config that we are in \"--edit\" mode,\n    and set a global for \"be lenient when reading the config\". I dunno.\n\nSo I think it's definitely do-able and would be much better, but it's\nprobably not entirely trivial.\n\n-Peff\n\n[1] https://lore.kernel.org/git/A5CDBB91-E889-4849-953A-2C1DB4A04513@gmail.com/\n"}]}