Skip to content

Commit 18a3d4f

Browse files
committed
🚧 Utility to extract configuration into a new config file
Fixes: #63 Signed-off-by: Guyzmo <[email protected]>
1 parent 599dc37 commit 18a3d4f

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,20 @@ So you can run the tool as a git subcommand:
186186

187187
git hub clone guyzmo/git-repo
188188

189+
For those who like to keep all dotfiles in a git repository, it'd be horrendous to
190+
store tokens that offer access to your social accounts in a repository… And I'm not
191+
even talking about those who want to share your dotfiles. But don't worry, once
192+
it's all configured, you can fire up your [favorite editor](http://www.vim.org) and
193+
move all the `[gitrepo …]` sections into a new file, like `~/.gitconfig-repos`.
194+
195+
Your can run the following command to do this automagically:
196+
197+
python -m git_repo.extract_config
198+
199+
if you want to use another path, you can change the defaults:
200+
201+
python -m git_repo.extract_config ~/.gitconfig-repos ~/.gitconfig
202+
189203
### Development
190204

191205
For development, I like to use `buildout`, and the repository is already configured

git_repo/extract_config.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
import sys
5+
import git
6+
7+
def extract_gitrepo_sections(conf):
8+
return filter(lambda k: k.startswith('gitrepo'), conf.sections())
9+
10+
def extract_gitrepo_conf(gconf_old, gconf_new):
11+
if os.path.exists(gconf_new):
12+
return "✗ cannot execute, file already exists: {}".format(gconf_new)
13+
with git.config.GitConfigParser(gconf_old, read_only=False) as cold:
14+
with git.config.GitConfigParser(gconf_new, read_only=False) as cnew:
15+
sections = list(extract_gitrepo_sections(cold))
16+
# copy the sections to the new configuration file
17+
cnew.update({s: {k:v for k,v in cold.items(s)} for s in sections})
18+
cnew.write()
19+
# remove the sections from the old configuration file
20+
for section in sections:
21+
cold.remove_section(section)
22+
# insert path to the new config file in the old one
23+
cold.update({'include': {'path': os.path.abspath(gconf_new)}})
24+
print("🍻 git-repo configuration extracted to new file: {}".format(gconf_new))
25+
26+
if __name__ == '__main__':
27+
if '-h' in sys.argv or '--help' in sys.argv:
28+
sys.exit('Usage: {} [.gitconfig-repos] [.gitconfig]'.format(sys.argv[0]))
29+
sys.exit(extract_gitrepo_conf(
30+
gconf_old=os.path.expanduser(len(sys.argv) >= 3 and sys.argv[2] or '~/.gitconfig'),
31+
gconf_new=os.path.expanduser(len(sys.argv) >= 2 and sys.argv[1] or '~/.gitconfig-repos')
32+
))
33+
34+

0 commit comments

Comments
 (0)