2
2
3
3
import click
4
4
import datetime
5
- import functools
5
+ import logging
6
6
import os
7
7
import subprocess
8
8
import sys
9
9
import yaml
10
10
11
11
from pathlib import Path
12
12
13
+ LOG = logging .getLogger ('git-synth' )
14
+
13
15
14
16
class Git :
15
17
def __init__ (self , repo ):
@@ -22,8 +24,8 @@ class Git:
22
24
return self ._run_git ('init' , self .repo )
23
25
24
26
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 ) for arg in args ) )
28
+ return subprocess . check_output (( 'git' ,) + args )
27
29
28
30
29
31
def Date (val ):
@@ -36,12 +38,19 @@ def Date(val):
36
38
37
39
38
40
@click .command ()
39
- @click .option ('-r' , '--repo-path' , default = 'testrepo' , type = Path )
41
+ @click .option ('-r' , '--repo-path' , type = Path , required = True )
40
42
@click .option ('-n' , '--name' , default = 'demo' )
41
43
@click .option ('-e' , '--email' , default = 'demo@example.com' )
42
44
@click .option ('-d' , '--date' , type = Date )
45
+ @click .option ('-v' , '--verbose' , count = True )
43
46
@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
+
45
54
if repo_path .is_dir ():
46
55
raise click .ClickException (f'directory { repo_path } already exists' )
47
56
@@ -75,40 +84,49 @@ def main(repo_path, name, date, email, spec):
75
84
except StopIteration :
76
85
if branches :
77
86
current_branch = branches .pop ()
78
- git ('switch' , current_branch )
87
+ git ('switch' , '-q' , current_branch )
79
88
continue
80
89
81
90
if 'branch' in action :
91
+ spec = action ['branch' ]
92
+
82
93
if current_branch is not None :
83
94
branches .append (current_branch )
84
95
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' ]))
89
100
continue
90
101
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' ],
94
105
'--allow-empty' )
95
106
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' ])
97
110
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' ])
100
113
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' ]
110
127
elif 'setenv' in action :
111
128
for k , v in action ['setenv' ].items ():
129
+ LOG .info ('setting env var %s to "%s"' , k , v )
112
130
os .environ [k ] = v
113
131
114
132
0 commit comments