1
1
# Standard library
2
- import argparse
2
+ # import argparse
3
+ # Standard library
3
4
import logging
4
5
import os
5
6
from datetime import datetime , timezone
9
10
from pandas import PeriodIndex
10
11
11
12
13
+ class GitOperationError (Exception ):
14
+ def __init__ (self , message , exit_code ):
15
+ super ().__init__ (message )
16
+ self .exit_code = exit_code
17
+
18
+
12
19
def setup (current_file ):
13
20
# Set up logging
14
21
logging .basicConfig (
@@ -52,19 +59,19 @@ def log_paths(logger, paths):
52
59
logger .info (f"PATHS:{ paths_list } " )
53
60
54
61
55
- def fetch_and_merge (repo_path , branch = "fetch -automation" ):
62
+ def fetch_and_merge (repo_path , branch = "refine -automation" ):
56
63
try :
57
64
repo = Repo (repo_path )
58
65
origin = repo .remote (name = "origin" )
59
66
origin .fetch ()
60
67
repo .git .merge (f"origin/{ branch } " , allow_unrelated_histories = True )
61
68
logging .info (f"Fetched and merged latest changes from { branch } " )
62
69
except InvalidGitRepositoryError :
63
- logging . error (f"Invalid Git repository at { repo_path } " )
70
+ raise GitOperationError (f"Invalid Git repository at { repo_path } " , 2 )
64
71
except NoSuchPathError :
65
- logging . error (f"No such path: { repo_path } " )
72
+ raise GitOperationError (f"No such path: { repo_path } " , 3 )
66
73
except Exception as e :
67
- logging . error (f"Error during fetch and merge: { e } " )
74
+ raise GitOperationError (f"Error during fetch and merge: { e } " , 1 )
68
75
69
76
70
77
def add_and_commit (repo_path , message ):
@@ -77,9 +84,11 @@ def add_and_commit(repo_path, message):
77
84
repo .index .commit (message )
78
85
logging .info ("Changes committed" )
79
86
except InvalidGitRepositoryError :
80
- logging . error (f"Invalid Git repository at { repo_path } " )
87
+ raise GitOperationError (f"Invalid Git repository at { repo_path } " , 2 )
81
88
except NoSuchPathError :
82
- logging .error (f"No such path: { repo_path } " )
89
+ raise GitOperationError (f"No such path: { repo_path } " , 3 )
90
+ except Exception as e :
91
+ raise GitOperationError (f"Error during add and commit: { e } " , 1 )
83
92
84
93
85
94
def push_changes (repo_path ):
@@ -89,43 +98,45 @@ def push_changes(repo_path):
89
98
origin .push ()
90
99
logging .info ("Changes pushed" )
91
100
except InvalidGitRepositoryError :
92
- logging . error (f"Invalid Git repository at { repo_path } " )
101
+ raise GitOperationError (f"Invalid Git repository at { repo_path } " , 2 )
93
102
except NoSuchPathError :
94
- logging .error (f"No such path: { repo_path } " )
95
-
96
-
97
- def main ():
98
- parser = argparse .ArgumentParser (description = "Git operations script" )
99
- parser .add_argument (
100
- "--operation" ,
101
- type = str ,
102
- required = True ,
103
- help = "Operation to perform: fetch_and_merge, add_and_commit, push" ,
104
- )
105
- parser .add_argument ("--message" , type = str , help = "Commit message" )
106
- parser .add_argument (
107
- "--branch" ,
108
- type = str ,
109
- default = "fetch-automation" ,
110
- help = "Branch to fetch and merge from" ,
111
- )
112
- args = parser .parse_args ()
113
-
114
- repo_path = os .getcwd () # Assuming the script runs in the root of the repo
115
-
116
- if args .operation == "fetch_and_merge" :
117
- fetch_and_merge (repo_path , args .branch )
118
- elif args .operation == "add_and_commit" :
119
- if not args .message :
120
- raise ValueError (
121
- "Commit message is required for add_and_commit operation"
122
- )
123
- add_and_commit (repo_path , args .message )
124
- elif args .operation == "push" :
125
- push_changes (repo_path )
126
- else :
127
- raise ValueError ("Unsupported operation" )
128
-
129
-
130
- if __name__ == "__main__" :
131
- main ()
103
+ raise GitOperationError (f"No such path: { repo_path } " , 3 )
104
+ except Exception as e :
105
+ raise GitOperationError (f"Error during push changes: { e } " , 1 )
106
+
107
+
108
+ # def main():
109
+ # parser = argparse.ArgumentParser(description="Git operations script")
110
+ # parser.add_argument(
111
+ # "--operation",
112
+ # type=str,
113
+ # required=True,
114
+ # help="Operation to perform: fetch_and_merge, add_and_commit, push",
115
+ # )
116
+ # parser.add_argument("--message", type=str, help="Commit message")
117
+ # parser.add_argument(
118
+ # "--branch",
119
+ # type=str,
120
+ # default="refine-automation",
121
+ # help="Branch to fetch and merge from",
122
+ # )
123
+ # args = parser.parse_args()
124
+
125
+ # repo_path = os.getcwd() # Assuming the script runs in repo root
126
+
127
+ # if args.operation == "fetch_and_merge":
128
+ # fetch_and_merge(repo_path, args.branch)
129
+ # elif args.operation == "add_and_commit":
130
+ # if not args.message:
131
+ # raise ValueError(
132
+ # "Commit message is required for add_and_commit operation"
133
+ # )
134
+ # add_and_commit(repo_path, args.message)
135
+ # elif args.operation == "push":
136
+ # push_changes(repo_path)
137
+ # else:
138
+ # raise ValueError("Unsupported operation")
139
+
140
+
141
+ # if __name__ == "__main__":
142
+ # main()
0 commit comments