16
16
import subprocess
17
17
import sys
18
18
from logging .handlers import RotatingFileHandler
19
- from typing import List , Optional
19
+ from typing import List , Optional , Tuple
20
20
21
21
import toml
22
22
@@ -106,25 +106,25 @@ def configure_logger(log_level: str) -> None:
106
106
logger .addHandler (console_handler )
107
107
108
108
109
- def get_pushed_refs () -> List [str ]:
109
+ def get_pushed_refs () -> List [Tuple [ str , str ] ]:
110
110
"""
111
111
Retrieves the list of refs being pushed.
112
112
113
113
Returns:
114
- List[str] : List of refs being pushed .
114
+ List[Tuple[ str, str]] : List of tuples containing local_ref and remote_sha .
115
115
"""
116
116
refs = []
117
117
try :
118
118
# Read from stdin the refs being pushed
119
119
for line in sys .stdin :
120
120
parts = line .strip ().split ()
121
- if len (parts ) >= 2 :
122
- local_ref , local_sha = parts [0 ], parts [ 1 ]
123
- refs .append (local_ref )
124
- logging .debug (f"Refs being pushed: { refs } " )
121
+ if len (parts ) >= 4 :
122
+ local_ref , local_sha , remote_ref , remote_sha = parts [: 4 ]
123
+ refs .append (( local_ref , remote_sha ) )
124
+ logger .debug (f"Refs being pushed: { refs } " )
125
125
return refs
126
126
except Exception as e :
127
- logging .error (f"Error reading refs from stdin: { e } " )
127
+ logger .error (f"Error reading refs from stdin: { e } " )
128
128
sys .exit (1 )
129
129
130
130
@@ -150,21 +150,21 @@ def get_upstream_branch() -> Optional[str]:
150
150
return None
151
151
152
152
153
- def get_commits_being_pushed (local_ref : str , remote_ref : str ) -> List [str ]:
153
+ def get_commits_being_pushed (remote_sha : str , local_sha : str ) -> List [str ]:
154
154
"""
155
- Retrieves the list of commit hashes being pushed for a given ref.
155
+ Retrieves the list of commit hashes being pushed for a given ref range .
156
156
157
157
Args:
158
- local_ref (str): The local ref being pushed .
159
- remote_ref (str): The remote ref being pushed to .
158
+ remote_sha (str): The remote SHA before the push .
159
+ local_sha (str): The local SHA being pushed.
160
160
161
161
Returns:
162
162
List[str]: List of commit hashes being pushed.
163
163
"""
164
164
try :
165
165
commits = (
166
166
subprocess .run (
167
- ["git" , "rev-list" , "--no-merges" , f"{ remote_ref } ..{ local_ref } " ],
167
+ ["git" , "rev-list" , "--no-merges" , f"{ remote_sha } ..{ local_sha } " ],
168
168
check = True ,
169
169
stdout = subprocess .PIPE ,
170
170
stderr = subprocess .PIPE ,
@@ -174,10 +174,10 @@ def get_commits_being_pushed(local_ref: str, remote_ref: str) -> List[str]:
174
174
.split ("\n " )
175
175
)
176
176
commits = [commit for commit in commits if commit ]
177
- logger .debug (f"Commits being pushed for { local_ref } ..{ remote_ref } : { commits } " )
177
+ logger .debug (f"Commits being pushed for { remote_sha } ..{ local_sha } : { commits } " )
178
178
return commits
179
179
except subprocess .CalledProcessError as e :
180
- logger .error (f"Error retrieving commits for { local_ref } ..{ remote_ref } : { e .stderr } " )
180
+ logger .error (f"Error retrieving commits for { remote_sha } ..{ local_sha } : { e .stderr } " )
181
181
sys .exit (1 )
182
182
183
183
@@ -350,13 +350,21 @@ def main() -> None:
350
350
logger .info ("No refs being pushed." )
351
351
return
352
352
353
- upstream_ref = get_upstream_branch ()
354
- if not upstream_ref :
355
- logger .error ("No upstream branch found. Aborting." )
356
- sys .exit (1 )
353
+ for local_ref , remote_sha in pushed_refs :
354
+ try :
355
+ local_sha = subprocess .run (
356
+ ["git" , "rev-parse" , local_ref ],
357
+ check = True ,
358
+ stdout = subprocess .PIPE ,
359
+ stderr = subprocess .PIPE ,
360
+ text = True ,
361
+ ).stdout .strip ()
362
+ logger .debug (f"Local SHA for { local_ref } : { local_sha } " )
363
+ except subprocess .CalledProcessError as e :
364
+ logger .error (f"Error retrieving local SHA for { local_ref } : { e .stderr } " )
365
+ continue
357
366
358
- for local_ref in pushed_refs :
359
- commits = get_commits_being_pushed (local_ref , upstream_ref )
367
+ commits = get_commits_being_pushed (remote_sha , local_sha )
360
368
if not commits :
361
369
logger .info (f"No new commits to process for { local_ref } ." )
362
370
continue
0 commit comments