Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3819a56

Browse files
committed
add logging to git-synth
1 parent 12a5454 commit 3819a56

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

‎git-synth‎

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
import click
44
import datetime
5-
import functools
5+
import logging
66
import os
77
import subprocess
88
import sys
99
import yaml
1010

1111
from pathlib import Path
1212

13+
LOG = logging.getLogger('git-synth')
14+
1315

1416
class Git:
1517
def __init__(self, repo):
@@ -22,8 +24,8 @@ class Git:
2224
return self._run_git('init', self.repo)
2325

2426
def _run_git(self, *args):
25-
out=subprocess.check_output(('git',) +args)
26-
return out
27+
LOG.debug('running with with %s', ' '.join(str(arg) forarginargs))
28+
return subprocess.check_output(('git',) +args)
2729

2830

2931
def Date(val):
@@ -36,12 +38,19 @@ def Date(val):
3638

3739

3840
@click.command()
39-
@click.option('-r', '--repo-path', default='testrepo', type=Path)
41+
@click.option('-r', '--repo-path', type=Path, required=True)
4042
@click.option('-n', '--name', default='demo')
4143
@click.option('-e', '--email', default='demo@example.com')
4244
@click.option('-d', '--date', type=Date)
45+
@click.option('-v', '--verbose', count=True)
4346
@click.argument('spec', type=click.File(mode='r'), default=sys.stdin)
44-
def main(repo_path, name, date, email, spec):
47+
def main(repo_path, name, date, email, verbose, spec):
48+
try:
49+
loglevel = ['WARNING', 'INFO', 'DEBUG'][verbose]
50+
except IndexError:
51+
loglevel = 'DEBUG'
52+
logging.basicConfig(level=loglevel)
53+
4554
if repo_path.is_dir():
4655
raise click.ClickException(f'directory {repo_path} already exists')
4756

@@ -75,40 +84,49 @@ def main(repo_path, name, date, email, spec):
7584
except StopIteration:
7685
if branches:
7786
current_branch = branches.pop()
78-
git('switch', current_branch)
87+
git('switch', '-q', current_branch)
7988
continue
8089

8190
if 'branch' in action:
91+
spec = action['branch']
92+
8293
if current_branch is not None:
8394
branches.append(current_branch)
8495

85-
branch =action['branch']
86-
git('switch', '-c', branch['name'])
87-
current_branch = branch['name']
88-
stack.append(iter(branch['actions']))
96+
LOG.info('creating branch %s', spec['name'])
97+
git('switch', '-qc', spec['name'])
98+
current_branch = spec['name']
99+
stack.append(iter(spec['actions']))
89100
continue
90101
elif 'commit' in action:
91-
commit = action['commit']
92-
print('commit', commit['message'])
93-
git('commit', '-m', commit['message'],
102+
spec = action['commit']
103+
LOG.info('commit with message "%s"', spec['message'])
104+
git('commit', '-m', spec['message'],
94105
'--allow-empty')
95106
elif 'file' in action:
96-
path = repo_path / action['file']['name']
107+
spec = action['file']
108+
path = repo_path / spec['name']
109+
LOG.info('creating file "%s"', spec['name'])
97110
with path.open('w') as fd:
98-
fd.write(action['file']['content'])
99-
git('add', action['file']['name'])
111+
fd.write(spec['content'])
112+
git('add', spec['name'])
100113
elif 'set' in action:
101-
if 'date' in action['set']:
102-
os.environ['GIT_AUTHOR_DATE'] = Date(action['set']['date']).isoformat()
103-
os.environ['GIT_COMMITTER_DATE'] = Date(action['set']['date']).isoformat()
104-
if 'name' in action['set']:
105-
os.environ['GIT_AUTHOR_NAME'] = action['set']['name']
106-
os.environ['GIT_COMMITTER_NAME'] = action['set']['name']
107-
if 'email' in action['set']:
108-
os.environ['GIT_AUTHOR_EMAIL'] = action['set']['email']
109-
os.environ['GIT_COMMITTER_EMAIL'] = action['set']['email']
114+
spec = action['set']
115+
if 'date' in spec:
116+
LOG.info('setting date to "%s"', spec['date'])
117+
os.environ['GIT_AUTHOR_DATE'] = Date(spec['date']).isoformat()
118+
os.environ['GIT_COMMITTER_DATE'] = Date(spec['date']).isoformat()
119+
if 'name' in spec:
120+
LOG.info('setting name to "%s"', spec['name'])
121+
os.environ['GIT_AUTHOR_NAME'] = spec['name']
122+
os.environ['GIT_COMMITTER_NAME'] = spec['name']
123+
if 'email' in spec:
124+
LOG.info('setting email to "%s"', spec['email'])
125+
os.environ['GIT_AUTHOR_EMAIL'] = spec['email']
126+
os.environ['GIT_COMMITTER_EMAIL'] = spec['email']
110127
elif 'setenv' in action:
111128
for k, v in action['setenv'].items():
129+
LOG.info('setting env var %s to "%s"', k, v)
112130
os.environ[k] = v
113131

114132

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /