Fix main branch

Change-Id: I2ec18b2846aee8185b0f5d3222b07ce9259f9f57
diff --git a/server/agent/agent.go b/server/agent/agent.go
index 42a7024..6de4784 100644
--- a/server/agent/agent.go
+++ b/server/agent/agent.go
@@ -183,15 +183,6 @@
 		}
 	}
 
-	// Set up git user configuration
-	userConfig := git.UserConfig{
-		Name:  a.Config.GitUsername,
-		Email: a.Config.GitEmail,
-	}
-	if err := a.gitInterface.SetUserConfig(ctx, userConfig); err != nil {
-		return fmt.Errorf("failed to set git user config: %w", err)
-	}
-
 	// Check if remote origin exists, if not add it
 	remotes, err := a.gitInterface.ListRemotes(ctx)
 	if err != nil {
@@ -231,11 +222,33 @@
 
 	// Checkout to the specified branch
 	if a.Config.GitBranch != "" {
-		if err := a.gitInterface.Checkout(ctx, a.Config.GitBranch); err != nil {
-			// Try to create the branch if it doesn't exist
-			if err := a.gitInterface.CreateBranch(ctx, a.Config.GitBranch, ""); err != nil {
-				return fmt.Errorf("failed to create branch %s: %w", a.Config.GitBranch, err)
+		// First, check if we're already on the target branch
+		currentBranch, err := a.gitInterface.GetCurrentBranch(ctx)
+		if err != nil {
+			return fmt.Errorf("failed to get current branch: %w", err)
+		}
+
+		// Only checkout if we're not already on the target branch
+		if currentBranch != a.Config.GitBranch {
+			if err := a.gitInterface.Checkout(ctx, a.Config.GitBranch); err != nil {
+				errMsg := err.Error()
+
+				// Only create the branch if the error indicates it doesn't exist
+				if strings.Contains(errMsg, "did not match any file(s) known to git") ||
+					strings.Contains(errMsg, "not found") ||
+					strings.Contains(errMsg, "unknown revision") ||
+					strings.Contains(errMsg, "reference is not a tree") ||
+					strings.Contains(errMsg, "pathspec") ||
+					strings.Contains(errMsg, "fatal: invalid reference") {
+					if err := a.gitInterface.CreateBranch(ctx, a.Config.GitBranch, ""); err != nil {
+						return fmt.Errorf("failed to create branch %s: %w", a.Config.GitBranch, err)
+					}
+				} else {
+					return fmt.Errorf("failed to checkout branch %s: %w", a.Config.GitBranch, err)
+				}
 			}
+		} else {
+			log.Printf("Already on target branch: %s", a.Config.GitBranch)
 		}
 	}