@@ -178,6 +178,92 @@ def check_for_updates(self, include_prereleases=False):
178178 'latest_version' : self .get_current_version (),
179179 'update_available' : False ,
180180 }
181+
182+ def install_update (self , target_version ):
183+ """Install update by pulling and checking out the specified version"""
184+ try :
185+ current_version = self .get_current_version ()
186+
187+ # Ensure we have a clean working directory
188+ result = subprocess .run (
189+ ['git' , 'status' , '--porcelain' ],
190+ capture_output = True ,
191+ text = True ,
192+ cwd = settings .BASE_DIR
193+ )
194+
195+ if result .stdout .strip ():
196+ return {
197+ 'success' : False ,
198+ 'error' : 'Working directory is not clean. Please commit or stash changes before updating.' ,
199+ 'current_version' : current_version ,
200+ }
201+
202+ # Fetch latest changes from remote
203+ result = subprocess .run (
204+ ['git' , 'fetch' , '--tags' ],
205+ capture_output = True ,
206+ text = True ,
207+ cwd = settings .BASE_DIR
208+ )
209+
210+ if result .returncode != 0 :
211+ return {
212+ 'success' : False ,
213+ 'error' : f'Failed to fetch updates: { result .stderr } ' ,
214+ 'current_version' : current_version ,
215+ }
216+
217+ # Checkout the target version
218+ tag_name = target_version if target_version .startswith ('v' ) else f'v{ target_version } '
219+ result = subprocess .run (
220+ ['git' , 'checkout' , tag_name ],
221+ capture_output = True ,
222+ text = True ,
223+ cwd = settings .BASE_DIR
224+ )
225+
226+ if result .returncode != 0 :
227+ # Try without 'v' prefix
228+ result = subprocess .run (
229+ ['git' , 'checkout' , target_version ],
230+ capture_output = True ,
231+ text = True ,
232+ cwd = settings .BASE_DIR
233+ )
234+
235+ if result .returncode != 0 :
236+ return {
237+ 'success' : False ,
238+ 'error' : f'Failed to checkout version { target_version } : { result .stderr } ' ,
239+ 'current_version' : current_version ,
240+ }
241+
242+ # Update version file if it exists
243+ version_file = settings .BASE_DIR / 'version.json'
244+ if version_file .exists ():
245+ try :
246+ with open (version_file , 'w' ) as f :
247+ json .dump ({'version' : target_version }, f , indent = 2 )
248+ except Exception as e :
249+ logger .warning (f"Could not update version file: { e } " )
250+
251+ new_version = self .get_current_version ()
252+
253+ return {
254+ 'success' : True ,
255+ 'message' : f'Successfully updated from { current_version } to { new_version } ' ,
256+ 'old_version' : current_version ,
257+ 'new_version' : new_version ,
258+ }
259+
260+ except Exception as e :
261+ logger .error (f"Error installing update: { e } " )
262+ return {
263+ 'success' : False ,
264+ 'error' : str (e ),
265+ 'current_version' : self .get_current_version (),
266+ }
181267
182268
183269# Global instance
0 commit comments