From be0bf18f41ea934e5d0fb2ebd1ad990b8bba593b Mon Sep 17 00:00:00 2001 From: Eyal Toledano Date: Fri, 13 Jun 2025 18:56:09 -0400 Subject: [PATCH] feat(git-workflow): Simplify git integration with --from-branch option - Remove automatic git workflow and branch-tag switching - we are not ready for it yet - Add --from-branch option to add-tag command for manual tag creation from git branch - Remove git workflow configuration from config.json and assets - Disable automatic tag switching functions in git-utils.js - Add createTagFromBranch function for branch-based tag creation - Support both CLI and MCP interfaces for --from-branch functionality - Fix ES module imports in git-utils.js and utils.js - Maintain user control over tag contexts without forced automation The simplified approach allows users to create tags from their current git branch when desired, without the complexity and rigidity of automatic branch-tag synchronization. Users maintain full control over their tag contexts while having convenient tools for git-based workflows when needed. --- .changeset/rotten-files-clap.md | 13 - .changeset/six-cups-see.md | 1 + .taskmaster/config.json | 4 - .taskmaster/state.json | 4 +- assets/config.json | 7 - .../src/core/direct-functions/add-tag.js | 167 ++++++++--- mcp-server/src/tools/add-tag.js | 7 + scripts/modules/commands.js | 71 ++++- .../modules/task-manager/tag-management.js | 13 +- scripts/modules/utils.js | 9 - scripts/modules/utils/git-utils.js | 276 +----------------- 11 files changed, 205 insertions(+), 367 deletions(-) delete mode 100644 .changeset/rotten-files-clap.md diff --git a/.changeset/rotten-files-clap.md b/.changeset/rotten-files-clap.md deleted file mode 100644 index 014a25ae..00000000 --- a/.changeset/rotten-files-clap.md +++ /dev/null @@ -1,13 +0,0 @@ ---- -"task-master-ai": minor ---- - -Add an experimental automatic git branch-tag integration for seamless multi-context development - -- **Automatic Tag Creation**: System now automatically creates empty tags when switching to new git branches -- **Branch-Tag Mapping**: Maintains mapping between git branches and task contexts for seamless workflow -- **Auto-Switch on Branch Change**: Task context automatically switches when you change git branches (when git workflow is enabled) -- **Isolated Task Contexts**: Each branch gets its own clean task context, preventing merge conflicts and enabling parallel development -- **Configuration Support**: Git workflow features can be enabled/disabled via `.taskmaster/config.json` -- **Zero Migration Impact**: Existing projects continue working unchanged with automatic migration to "master" tag -- **ES Module Compatibility**: Fixed git-utils module to work properly with ES module architecture diff --git a/.changeset/six-cups-see.md b/.changeset/six-cups-see.md index d865b249..a36824c0 100644 --- a/.changeset/six-cups-see.md +++ b/.changeset/six-cups-see.md @@ -38,6 +38,7 @@ Task Master's multi-context capabilities are now fully exposed through the MCP s **Tag Creation Options:** - `--copy-from-current` - Copy tasks from currently active tag - `--copy-from=` - Copy tasks from specific tag +- `--from-branch` - Creates a new tag usin active git branch name (for `add-tag` only) - `--description=""` - Add custom tag descriptions - Empty tag creation for fresh contexts diff --git a/.taskmaster/config.json b/.taskmaster/config.json index 6e51b320..99b02b07 100644 --- a/.taskmaster/config.json +++ b/.taskmaster/config.json @@ -30,9 +30,5 @@ "bedrockBaseURL": "https://bedrock.us-east-1.amazonaws.com", "azureBaseURL": "https://your-endpoint.azure.com/", "defaultTag": "master" - }, - "tags": { - "enabledGitworkflow": true, - "autoSwitchTagWithBranch": true } } diff --git a/.taskmaster/state.json b/.taskmaster/state.json index f30bfdff..1104888c 100644 --- a/.taskmaster/state.json +++ b/.taskmaster/state.json @@ -1,6 +1,6 @@ { - "currentTag": "v017-adds", - "lastSwitched": "2025-06-13T22:24:34.119Z", + "currentTag": "master", + "lastSwitched": "2025-06-13T22:44:52.009Z", "branchTagMapping": { "v017-adds": "v017-adds" }, diff --git a/assets/config.json b/assets/config.json index 2aff9988..56d67d0c 100644 --- a/assets/config.json +++ b/assets/config.json @@ -29,12 +29,5 @@ "ollamaBaseURL": "http://localhost:11434/api", "azureOpenaiBaseURL": "https://your-endpoint.openai.azure.com/", "bedrockBaseURL": "https://bedrock.us-east-1.amazonaws.com" - }, - "tags": { - "autoSwitchOnBranch": false, - "gitIntegration": { - "enabled": false, - "autoSwitchTagWithBranch": false - } } } diff --git a/mcp-server/src/core/direct-functions/add-tag.js b/mcp-server/src/core/direct-functions/add-tag.js index acce7d26..c1f6bd39 100644 --- a/mcp-server/src/core/direct-functions/add-tag.js +++ b/mcp-server/src/core/direct-functions/add-tag.js @@ -3,7 +3,10 @@ * Direct function implementation for creating a new tag */ -import { createTag } from '../../../../scripts/modules/task-manager/tag-management.js'; +import { + createTag, + createTagFromBranch +} from '../../../../scripts/modules/task-manager/tag-management.js'; import { enableSilentMode, disableSilentMode @@ -17,6 +20,7 @@ import { createLogWrapper } from '../../tools/utils.js'; * @param {string} args.name - Name of the new tag to create * @param {boolean} [args.copyFromCurrent=false] - Whether to copy tasks from current tag * @param {string} [args.copyFromTag] - Specific tag to copy tasks from + * @param {boolean} [args.fromBranch=false] - Create tag name from current git branch * @param {string} [args.description] - Optional description for the tag * @param {string} [args.tasksJsonPath] - Path to the tasks.json file (resolved by tool) * @param {string} [args.projectRoot] - Project root path @@ -31,6 +35,7 @@ export async function addTagDirect(args, log, context = {}) { name, copyFromCurrent = false, copyFromTag, + fromBranch = false, description, projectRoot } = args; @@ -56,55 +61,127 @@ export async function addTagDirect(args, log, context = {}) { }; } - // Check required parameters - if (!name || typeof name !== 'string') { - log.error('Missing required parameter: name'); + // Handle --from-branch option + if (fromBranch) { + log.info('Creating tag from current git branch'); + + // Import git utilities + const gitUtils = await import( + '../../../../scripts/modules/utils/git-utils.js' + ); + + // Check if we're in a git repository + if (!(await gitUtils.isGitRepository(projectRoot))) { + log.error('Not in a git repository'); + disableSilentMode(); + return { + success: false, + error: { + code: 'NOT_GIT_REPO', + message: 'Not in a git repository. Cannot use fromBranch option.' + } + }; + } + + // Get current git branch + const currentBranch = await gitUtils.getCurrentBranch(projectRoot); + if (!currentBranch) { + log.error('Could not determine current git branch'); + disableSilentMode(); + return { + success: false, + error: { + code: 'NO_CURRENT_BRANCH', + message: 'Could not determine current git branch.' + } + }; + } + + // Prepare options for branch-based tag creation + const branchOptions = { + copyFromCurrent, + copyFromTag, + description: + description || `Tag created from git branch "${currentBranch}"` + }; + + // Call the createTagFromBranch function + const result = await createTagFromBranch( + tasksJsonPath, + currentBranch, + branchOptions, + { + session, + mcpLog, + projectRoot + }, + 'json' // outputFormat - use 'json' to suppress CLI UI + ); + + // Restore normal logging disableSilentMode(); + return { - success: false, - error: { - code: 'MISSING_PARAMETER', - message: 'Tag name is required and must be a string' + success: true, + data: { + branchName: result.branchName, + tagName: result.tagName, + created: result.created, + mappingUpdated: result.mappingUpdated, + message: `Successfully created tag "${result.tagName}" from git branch "${result.branchName}"` + } + }; + } else { + // Check required parameters for regular tag creation + if (!name || typeof name !== 'string') { + log.error('Missing required parameter: name'); + disableSilentMode(); + return { + success: false, + error: { + code: 'MISSING_PARAMETER', + message: 'Tag name is required and must be a string' + } + }; + } + + log.info(`Creating new tag: ${name}`); + + // Prepare options + const options = { + copyFromCurrent, + copyFromTag, + description + }; + + // Call the createTag function + const result = await createTag( + tasksJsonPath, + name, + options, + { + session, + mcpLog, + projectRoot + }, + 'json' // outputFormat - use 'json' to suppress CLI UI + ); + + // Restore normal logging + disableSilentMode(); + + return { + success: true, + data: { + tagName: result.tagName, + created: result.created, + tasksCopied: result.tasksCopied, + sourceTag: result.sourceTag, + description: result.description, + message: `Successfully created tag "${result.tagName}"` } }; } - - log.info(`Creating new tag: ${name}`); - - // Prepare options - const options = { - copyFromCurrent, - copyFromTag, - description - }; - - // Call the createTag function - const result = await createTag( - tasksJsonPath, - name, - options, - { - session, - mcpLog, - projectRoot - }, - 'json' // outputFormat - use 'json' to suppress CLI UI - ); - - // Restore normal logging - disableSilentMode(); - - return { - success: true, - data: { - tagName: result.tagName, - created: result.created, - tasksCopied: result.tasksCopied, - sourceTag: result.sourceTag, - description: result.description, - message: `Successfully created tag "${result.tagName}"` - } - }; } catch (error) { // Make sure to restore normal logging even if there's an error disableSilentMode(); diff --git a/mcp-server/src/tools/add-tag.js b/mcp-server/src/tools/add-tag.js index 93365280..886d1a25 100644 --- a/mcp-server/src/tools/add-tag.js +++ b/mcp-server/src/tools/add-tag.js @@ -32,6 +32,12 @@ export function registerAddTagTool(server) { .string() .optional() .describe('Specific tag to copy tasks from'), + fromBranch: z + .boolean() + .optional() + .describe( + 'Create tag name from current git branch (ignores name parameter)' + ), description: z .string() .optional() @@ -69,6 +75,7 @@ export function registerAddTagTool(server) { name: args.name, copyFromCurrent: args.copyFromCurrent, copyFromTag: args.copyFromTag, + fromBranch: args.fromBranch, description: args.description, projectRoot: args.projectRoot }, diff --git a/scripts/modules/commands.js b/scripts/modules/commands.js index 4a44a495..2c403a55 100644 --- a/scripts/modules/commands.js +++ b/scripts/modules/commands.js @@ -3715,6 +3715,10 @@ Examples: '--copy-from ', 'Copy tasks from the specified tag to the new tag' ) + .option( + '--from-branch', + 'Create tag name from current git branch (ignores tagName argument)' + ) .option('-d, --description ', 'Optional description for the tag') .action(async (tagName, options) => { try { @@ -3745,19 +3749,70 @@ Examples: outputType: 'cli' }; - // Regular tag creation - const createOptions = { - copyFromCurrent: options.copyFromCurrent || false, - copyFromTag: options.copyFrom, - description: options.description - }; + // Handle --from-branch option + if (options.fromBranch) { + const { createTagFromBranch } = await import( + './task-manager/tag-management.js' + ); + const gitUtils = await import('./utils/git-utils.js'); - await createTag(tasksPath, tagName, createOptions, context, 'text'); + // Check if we're in a git repository + if (!(await gitUtils.isGitRepository(projectRoot))) { + console.error( + chalk.red( + 'Error: Not in a git repository. Cannot use --from-branch option.' + ) + ); + process.exit(1); + } + + // Get current git branch + const currentBranch = await gitUtils.getCurrentBranch(projectRoot); + if (!currentBranch) { + console.error( + chalk.red('Error: Could not determine current git branch.') + ); + process.exit(1); + } + + // Create tag from branch + const branchOptions = { + copyFromCurrent: options.copyFromCurrent || false, + copyFromTag: options.copyFrom, + description: + options.description || + `Tag created from git branch "${currentBranch}"` + }; + + await createTagFromBranch( + tasksPath, + currentBranch, + branchOptions, + context, + 'text' + ); + } else { + // Regular tag creation + const createOptions = { + copyFromCurrent: options.copyFromCurrent || false, + copyFromTag: options.copyFrom, + description: options.description + }; + + await createTag(tasksPath, tagName, createOptions, context, 'text'); + } // Handle auto-switch if requested if (options.autoSwitch) { const { useTag } = await import('./task-manager/tag-management.js'); - await useTag(tasksPath, tagName, {}, context, 'text'); + const finalTagName = options.fromBranch + ? (await import('./utils/git-utils.js')).sanitizeBranchNameForTag( + await (await import('./utils/git-utils.js')).getCurrentBranch( + projectRoot + ) + ) + : tagName; + await useTag(tasksPath, finalTagName, {}, context, 'text'); } } catch (error) { console.error(chalk.red(`Error creating tag: ${error.message}`)); diff --git a/scripts/modules/task-manager/tag-management.js b/scripts/modules/task-manager/tag-management.js index b964a15b..65a80e35 100644 --- a/scripts/modules/task-manager/tag-management.js +++ b/scripts/modules/task-manager/tag-management.js @@ -1235,7 +1235,7 @@ async function createTagFromBranch( // Import git utilities const { sanitizeBranchNameForTag, isValidBranchForTag } = await import( - '../../utils/git-utils.js' + '../utils/git-utils.js' ); // Create a consistent logFn object regardless of context @@ -1341,7 +1341,7 @@ async function autoSwitchTagForBranch( isGitRepository, sanitizeBranchNameForTag, isValidBranchForTag - } = await import('../../utils/git-utils.js'); + } = await import('../utils/git-utils.js'); // Create a consistent logFn object regardless of context const logFn = mcpLog || { @@ -1475,13 +1475,8 @@ async function checkAndAutoSwitchTag(projectRoot, tasksPath, context = {}) { const rawConfig = fs.readFileSync(configPath, 'utf8'); const config = JSON.parse(rawConfig); - // Check if git workflow is enabled - const gitWorkflowEnabled = config.tags?.enabledGitworkflow || false; - const autoSwitchEnabled = config.tags?.autoSwitchTagWithBranch || false; - - if (!gitWorkflowEnabled || !autoSwitchEnabled) { - return null; - } + // Git workflow has been removed - return null to disable auto-switching + return null; // Perform auto-switch return await autoSwitchTagForBranch( diff --git a/scripts/modules/utils.js b/scripts/modules/utils.js index b5d9dbab..76c30946 100644 --- a/scripts/modules/utils.js +++ b/scripts/modules/utils.js @@ -524,15 +524,6 @@ function migrateConfigJson(configPath) { modified = true; } - // Add tags section if missing - if (!config.tags) { - config.tags = { - enabledGitworkflow: false, - autoSwitchTagWithBranch: false - }; - modified = true; - } - if (modified) { fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf8'); if (process.env.TASKMASTER_DEBUG === 'true') { diff --git a/scripts/modules/utils/git-utils.js b/scripts/modules/utils/git-utils.js index 7021d9d4..eba5ee03 100644 --- a/scripts/modules/utils/git-utils.js +++ b/scripts/modules/utils/git-utils.js @@ -285,134 +285,9 @@ async function checkAndAutoSwitchGitTag(projectRoot, tasksPath) { throw new Error('projectRoot is required for checkAndAutoSwitchGitTag'); } - try { - // Only proceed if we have a valid project root - if (!fs.existsSync(projectRoot)) { - return; - } - - // Read configuration to check if git workflow is enabled - const configPath = path.join(projectRoot, '.taskmaster', 'config.json'); - if (!fs.existsSync(configPath)) { - return; // No config, git workflow disabled - } - - const rawConfig = fs.readFileSync(configPath, 'utf8'); - const config = JSON.parse(rawConfig); - - // Check if git workflow features are enabled - const gitWorkflowEnabled = config.tags?.enabledGitworkflow || false; - const autoSwitchEnabled = config.tags?.autoSwitchTagWithBranch || false; - - if (!gitWorkflowEnabled || !autoSwitchEnabled) { - return; // Git integration disabled - } - - // Check if we're in a git repository - if (!(await isGitRepository(projectRoot))) { - return; // Not a git repo - } - - // Get current git branch - const currentBranch = await getCurrentBranch(projectRoot); - if (!currentBranch || !isValidBranchForTag(currentBranch)) { - return; // No valid branch for tag creation - } - - // Determine expected tag name from branch - const expectedTag = sanitizeBranchNameForTag(currentBranch); - - // Get current tag from state.json - const statePath = path.join(projectRoot, '.taskmaster', 'state.json'); - let currentTag = 'master'; // Default fallback - if (fs.existsSync(statePath)) { - try { - const rawState = fs.readFileSync(statePath, 'utf8'); - const state = JSON.parse(rawState); - currentTag = state.currentTag || 'master'; - } catch (error) { - // Use default if state reading fails - } - } - - // If we're already on the correct tag, nothing to do - if (currentTag === expectedTag) { - return; - } - - // Check if the expected tag exists by reading tasks.json - if (!fs.existsSync(tasksPath)) { - return; // No tasks file to work with - } - - const rawTasksData = fs.readFileSync(tasksPath, 'utf8'); - const tasksData = JSON.parse(rawTasksData); - const tagExists = tasksData[expectedTag]; - - if (tagExists) { - // Tag exists, switch to it - console.log( - `Auto-switching to tag "${expectedTag}" for branch "${currentBranch}"` - ); - - // Update current tag in state.json - let state = {}; - if (fs.existsSync(statePath)) { - const rawState = fs.readFileSync(statePath, 'utf8'); - state = JSON.parse(rawState); - } - - state.currentTag = expectedTag; - state.lastSwitched = new Date().toISOString(); - - // Ensure branchTagMapping exists and update it - if (!state.branchTagMapping) { - state.branchTagMapping = {}; - } - state.branchTagMapping[currentBranch] = expectedTag; - - fs.writeFileSync(statePath, JSON.stringify(state, null, 2), 'utf8'); - } else { - // Tag doesn't exist, create it automatically - console.log( - `Auto-creating tag "${expectedTag}" for branch "${currentBranch}"` - ); - - // Create the tag with a descriptive name - const newTagData = { - tasks: [], - metadata: { - created: new Date().toISOString(), - updated: new Date().toISOString(), - description: `Automatically created from git branch "${currentBranch}"` - } - }; - - // Add the new tag to the tasks data - tasksData[expectedTag] = newTagData; - fs.writeFileSync(tasksPath, JSON.stringify(tasksData, null, 2), 'utf8'); - - // Update branch-tag mapping - let state = {}; - if (fs.existsSync(statePath)) { - const rawState = fs.readFileSync(statePath, 'utf8'); - state = JSON.parse(rawState); - } - - state.currentTag = expectedTag; - state.lastSwitched = new Date().toISOString(); - - if (!state.branchTagMapping) { - state.branchTagMapping = {}; - } - state.branchTagMapping[currentBranch] = expectedTag; - - fs.writeFileSync(statePath, JSON.stringify(state, null, 2), 'utf8'); - } - } catch (error) { - // Silently fail - don't break normal operations - console.debug(`Git auto-switch failed: ${error.message}`); - } + // DISABLED: Automatic git workflow is too rigid and opinionated + // Users should explicitly use git-tag commands if they want integration + return; } /** @@ -427,148 +302,9 @@ function checkAndAutoSwitchGitTagSync(projectRoot, tasksPath) { return; // Can't proceed without project root } - try { - // Only proceed if we have a valid project root - if (!fs.existsSync(projectRoot)) { - return; - } - - // Read configuration to check if git workflow is enabled - const configPath = path.join(projectRoot, '.taskmaster', 'config.json'); - if (!fs.existsSync(configPath)) { - return; // No config, git workflow disabled - } - - const rawConfig = fs.readFileSync(configPath, 'utf8'); - const config = JSON.parse(rawConfig); - - // Check if git workflow features are enabled - const gitWorkflowEnabled = config.tags?.enabledGitworkflow || false; - const autoSwitchEnabled = config.tags?.autoSwitchTagWithBranch || false; - - if (!gitWorkflowEnabled || !autoSwitchEnabled) { - return; // Git integration disabled - } - - // Check if we're in a git repository (synchronously) - if (!isGitRepositorySync(projectRoot)) { - return; // Not a git repo - } - - // Get current git branch (synchronously) - const currentBranch = getCurrentBranchSync(projectRoot); - if (!currentBranch || !isValidBranchForTag(currentBranch)) { - return; // No valid branch for tag creation - } - - // Determine expected tag name from branch - const expectedTag = sanitizeBranchNameForTag(currentBranch); - - // Get current tag from state.json - const statePath = path.join(projectRoot, '.taskmaster', 'state.json'); - let currentTag = 'master'; // default - if (fs.existsSync(statePath)) { - try { - const rawState = fs.readFileSync(statePath, 'utf8'); - const state = JSON.parse(rawState); - currentTag = state.currentTag || 'master'; - } catch (error) { - // Use default if state.json is corrupted - } - } - - // If we're already on the correct tag, nothing to do - if (currentTag === expectedTag) { - return; - } - - // Check if the expected tag exists in tasks.json - let tasksData = {}; - if (fs.existsSync(tasksPath)) { - try { - const rawTasks = fs.readFileSync(tasksPath, 'utf8'); - tasksData = JSON.parse(rawTasks); - } catch (error) { - return; // Can't read tasks file - } - } - - const tagExists = tasksData[expectedTag]; - - if (tagExists) { - // Tag exists, switch to it - console.log( - `Auto-switching to tag "${expectedTag}" for branch "${currentBranch}"` - ); - - // Update current tag in state.json - let state = {}; - if (fs.existsSync(statePath)) { - try { - const rawState = fs.readFileSync(statePath, 'utf8'); - state = JSON.parse(rawState); - } catch (error) { - state = {}; // Start fresh if corrupted - } - } - - state.currentTag = expectedTag; - state.lastSwitched = new Date().toISOString(); - - // Ensure branchTagMapping exists and update it - if (!state.branchTagMapping) { - state.branchTagMapping = {}; - } - state.branchTagMapping[currentBranch] = expectedTag; - - fs.writeFileSync(statePath, JSON.stringify(state, null, 2), 'utf8'); - } else { - // Tag doesn't exist, create it automatically - console.log( - `Auto-creating tag "${expectedTag}" for branch "${currentBranch}"` - ); - - // Create the tag with a descriptive name - const newTagData = { - tasks: [], - metadata: { - created: new Date().toISOString(), - updated: new Date().toISOString(), - description: `Automatically created from git branch "${currentBranch}"` - } - }; - - // Add the new tag to the tasks data - tasksData[expectedTag] = newTagData; - fs.writeFileSync(tasksPath, JSON.stringify(tasksData, null, 2), 'utf8'); - - // Update branch-tag mapping and current tag - let state = {}; - if (fs.existsSync(statePath)) { - try { - const rawState = fs.readFileSync(statePath, 'utf8'); - state = JSON.parse(rawState); - } catch (error) { - state = {}; // Start fresh if corrupted - } - } - - state.currentTag = expectedTag; - state.lastSwitched = new Date().toISOString(); - - if (!state.branchTagMapping) { - state.branchTagMapping = {}; - } - state.branchTagMapping[currentBranch] = expectedTag; - - fs.writeFileSync(statePath, JSON.stringify(state, null, 2), 'utf8'); - } - } catch (error) { - // Silently fail - don't break normal operations - if (process.env.TASKMASTER_DEBUG === 'true') { - console.debug(`Git auto-switch failed: ${error.message}`); - } - } + // DISABLED: Automatic git workflow is too rigid and opinionated + // Users should explicitly use git-tag commands if they want integration + return; } /**