@@ -63,25 +63,65 @@ def viz_path(self) -> str:
63
63
def viz_file_path (self ) -> str :
64
64
return os .path .join (self .viz_path , "graph.json" )
65
65
66
+ def _set_bot_email (self , git_cli : GitCLI ) -> None :
67
+ with git_cli .config_writer ("repository" ) as writer :
68
+ if not writer .has_section ("user" ):
69
+ writer .add_section ("user" )
70
+ writer .set ("user" , "email" , CODEGEN_BOT_EMAIL )
71
+
72
+ def _set_bot_username (self , git_cli : GitCLI ) -> None :
73
+ with git_cli .config_writer ("repository" ) as writer :
74
+ if not writer .has_section ("user" ):
75
+ writer .add_section ("user" )
76
+ writer .set ("user" , "name" , CODEGEN_BOT_NAME )
77
+
78
+ def _unset_bot_email (self , git_cli : GitCLI ) -> None :
79
+ with git_cli .config_writer ("repository" ) as writer :
80
+ if writer .has_option ("user" , "email" ):
81
+ writer .remove_option ("user" , "email" )
82
+
83
+ def _unset_bot_username (self , git_cli : GitCLI ) -> None :
84
+ with git_cli .config_writer ("repository" ) as writer :
85
+ if writer .has_option ("user" , "name" ):
86
+ writer .remove_option ("user" , "name" )
87
+
66
88
@cached_property
67
89
def git_cli (self ) -> GitCLI :
68
90
"""Note: this is recursive, may want to look out"""
69
91
git_cli = GitCLI (self .repo_path )
70
- has_username = False
71
- has_email = False
72
- with git_cli .config_reader (None ) as reader :
73
- if reader .has_option ("user" , "name" ):
74
- has_username = True
75
- if reader .has_option ("user" , "email" ):
76
- has_email = True
77
- with git_cli .config_writer ("repository" ) as writer :
78
- if not has_username or not has_email or self .bot_commit :
79
- if not writer .has_section ("user" ):
80
- writer .add_section ("user" )
81
- if not has_username or self .bot_commit :
82
- writer .set ("user" , "name" , CODEGEN_BOT_NAME )
83
- if not has_email or self .bot_commit :
84
- writer .set ("user" , "email" , CODEGEN_BOT_EMAIL )
92
+ username = None
93
+ user_level = None
94
+ email = None
95
+ email_level = None
96
+ levels = ["system" , "global" , "user" , "repository" ]
97
+ for level in levels :
98
+ with git_cli .config_reader (level ) as reader :
99
+ if reader .has_option ("user" , "name" ) and not username :
100
+ username = reader .get ("user" , "name" )
101
+ user_level = level
102
+ if reader .has_option ("user" , "email" ) and not email :
103
+ email = reader .get ("user" , "email" )
104
+ email_level = level
105
+ if self .bot_commit :
106
+ self ._set_bot_email (git_cli )
107
+ self ._set_bot_username (git_cli )
108
+ else :
109
+ # we need a username and email to commit, so if they're not set, set them to the bot's
110
+ # Case 1: username is not set: set it to the bot's
111
+ if not username :
112
+ self ._set_bot_username (git_cli )
113
+ # Case 2: username is set to the bot's at the repo level, but something else is set at the user level: unset it
114
+ elif username != CODEGEN_BOT_NAME and user_level != "repository" :
115
+ self ._unset_bot_username (git_cli )
116
+ # Case 3: username is only set at the repo level: do nothing
117
+ else :
118
+ pass # no-op to make the logic clearer
119
+ # Repeat for email
120
+ if not email :
121
+ self ._set_bot_email (git_cli )
122
+
123
+ elif email != CODEGEN_BOT_EMAIL and email_level != "repository" :
124
+ self ._unset_bot_email (git_cli )
85
125
return git_cli
86
126
87
127
@property
0 commit comments