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.
This commit is contained in:
Eyal Toledano
2025-06-13 18:56:09 -04:00
parent 32236a0bc5
commit be0bf18f41
11 changed files with 205 additions and 367 deletions

View File

@@ -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

View File

@@ -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=<tag>` - Copy tasks from specific tag
- `--from-branch` - Creates a new tag usin active git branch name (for `add-tag` only)
- `--description="<text>"` - Add custom tag descriptions
- Empty tag creation for fresh contexts

View File

@@ -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
}
}

View File

@@ -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"
},

View File

@@ -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
}
}
}

View File

@@ -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,7 +61,78 @@ export async function addTagDirect(args, log, context = {}) {
};
}
// Check required parameters
// 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: 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();
@@ -105,6 +181,7 @@ export async function addTagDirect(args, log, context = {}) {
message: `Successfully created tag "${result.tagName}"`
}
};
}
} catch (error) {
// Make sure to restore normal logging even if there's an error
disableSilentMode();

View File

@@ -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
},

View File

@@ -3715,6 +3715,10 @@ Examples:
'--copy-from <tag>',
'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 <text>', 'Optional description for the tag')
.action(async (tagName, options) => {
try {
@@ -3745,6 +3749,49 @@ Examples:
outputType: 'cli'
};
// Handle --from-branch option
if (options.fromBranch) {
const { createTagFromBranch } = await import(
'./task-manager/tag-management.js'
);
const gitUtils = await import('./utils/git-utils.js');
// 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,
@@ -3753,11 +3800,19 @@ Examples:
};
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}`));

View File

@@ -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) {
// Git workflow has been removed - return null to disable auto-switching
return null;
}
// Perform auto-switch
return await autoSwitchTagForBranch(

View File

@@ -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') {

View File

@@ -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)) {
// DISABLED: Automatic git workflow is too rigid and opinionated
// Users should explicitly use git-tag commands if they want integration
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}`);
}
}
/**
@@ -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)) {
// DISABLED: Automatic git workflow is too rigid and opinionated
// Users should explicitly use git-tag commands if they want integration
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}`);
}
}
}
/**