11import logging
2- import os
32from contextlib import asynccontextmanager
43
54from fastapi import FastAPI
65
6+ from codegen .configs .models .codebase import DefaultCodebaseConfig
77from codegen .git .configs .constants import CODEGEN_BOT_EMAIL , CODEGEN_BOT_NAME
88from codegen .git .repo_operator .repo_operator import RepoOperator
99from codegen .git .schemas .enums import SetupOption
@@ -47,11 +47,12 @@ async def lifespan(server: FastAPI):
4747 runner .op .git_cli .git .config ("user.email" , CODEGEN_BOT_EMAIL )
4848 runner .op .git_cli .git .config ("user.name" , CODEGEN_BOT_NAME )
4949
50- # Parse the codebase
50+ # Parse the codebase with sync enabled
5151 logger .info (f"Starting up fastapi server for repo_name={ repo_config .name } " )
5252 server_info .warmup_state = WarmupState .PENDING
53- await runner .warmup ()
54- server_info .synced_commit = runner .commit .hexsha
53+ codebase_config = DefaultCodebaseConfig .model_copy (update = {"sync_enabled" : True })
54+ await runner .warmup (codebase_config = codebase_config )
55+ server_info .synced_commit = runner .op .head_commit .hexsha
5556 server_info .warmup_state = WarmupState .COMPLETED
5657
5758 except Exception :
@@ -73,27 +74,24 @@ def health() -> ServerInfo:
7374
7475@app .post (RUN_FUNCTION_ENDPOINT )
7576async def run (request : RunFunctionRequest ) -> CodemodRunResult :
76- # TODO: Sync graph to whatever changes are in the repo currently
77-
78- # Run the request
77+ _save_uncommitted_changes_and_sync ()
7978 diff_req = GetDiffRequest (codemod = Codemod (user_code = request .codemod_source ))
8079 diff_response = await runner .get_diff (request = diff_req )
8180 if request .commit :
82- if _should_skip_commit (request .function_name ):
83- logger .info (f"Skipping commit because only changes to { request .function_name } were made" )
84- elif commit_sha := runner .codebase .git_commit (f"[Codegen] { request .function_name } " ):
81+ if commit_sha := runner .codebase .git_commit (f"[Codegen] { request .function_name } " , exclude_paths = [".codegen/*" ]):
8582 logger .info (f"Committed changes to { commit_sha .hexsha } " )
8683 return diff_response .result
8784
8885
89- def _should_skip_commit (function_name : str ) -> bool :
90- changed_files = runner .op .get_modified_files (runner .commit )
91- if len (changed_files ) != 1 :
92- return False
86+ def _save_uncommitted_changes_and_sync () -> None :
87+ if commit := runner .codebase .git_commit ("[Codegen] Save uncommitted changes" , exclude_paths = [".codegen/*" ]):
88+ logger .info (f"Saved uncommitted changes to { commit .hexsha } " )
9389
94- file_path = changed_files [0 ]
95- if not file_path .startswith (".codegen/codemods/" ):
96- return False
90+ cur_commit = runner .op .head_commit
91+ if cur_commit != runner .codebase .ctx .synced_commit :
92+ logger .info (f"Syncing codebase to head commit: { cur_commit .hexsha } " )
93+ runner .codebase .sync_to_commit (target_commit = cur_commit )
94+ else :
95+ logger .info ("Codebase is already synced to head commit" )
9796
98- changed_file_name = os .path .splitext (os .path .basename (file_path ))[0 ]
99- return changed_file_name == function_name .replace ("-" , "_" )
97+ server_info .synced_commit = cur_commit .hexsha
0 commit comments