All files / src/services project-detection-service.ts

61.66% Statements 156/253
52% Branches 13/25
75% Functions 6/8
61.66% Lines 156/253

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 2531x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x                                               2x 2x 2x 2x 2x 2x 2x       2x 1x 1x 1x                                     1x 1x 1x 1x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 16x 16x                     16x 2x 2x 2x 2x 2x 2x 2x         2x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x             1x 1x 1x 1x 1x 1x 1x 1x 1x                     1x                                 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import * as crypto from 'crypto';
 
export interface ProjectInfo {
    
    localPath: string;
    
    remoteUrl?: string;
    
    
    //A unique hash identifying this project
    //This is generated from Git config or folder structure
    projectHash: string;
    
    //The name of the project (derived from folder name or repo name)
    projectName: string;
    
    isGitRepo: boolean;
}
 
//Detects the current project information from the active VS Code workspace
export function getCurrentProjectInfo(): ProjectInfo | null {
    const workspaceFolders = vscode.workspace.workspaceFolders;
    if (!workspaceFolders || workspaceFolders.length === 0) {
        return null;
    }
 
    // Use the first workspace folder as the primary project
    const primaryFolder = workspaceFolders[0];
    const localPath = primaryFolder.uri.fsPath;
    const projectName = path.basename(localPath);
 
    // Try to get Git information
    const gitInfo = getGitInfo(localPath);
    
    // Generate a unique project identifier
    const projectHash = generateProjectHash(localPath, gitInfo?.remoteUrl);
 
    return {
        localPath,
        remoteUrl: gitInfo?.remoteUrl,
        projectHash,
        projectName,
        isGitRepo: gitInfo?.isGitRepo || false
    };
}
 
 
//Extracts Git repository information from a local folder
function getGitInfo(folderPath: string): { remoteUrl?: string; isGitRepo: boolean } | null {
    try {
        const gitConfigPath = path.join(folderPath, '.git', 'config');
        
        // Check if .git directory exists
        if (!fs.existsSync(path.join(folderPath, '.git'))) {
            return { isGitRepo: false };
        }

        // If it's a Git worktree, read the .git file
        const gitPath = path.join(folderPath, '.git');
        let actualGitConfigPath = gitConfigPath;
        
        if (fs.statSync(gitPath).isFile()) {
            const gitFileContent = fs.readFileSync(gitPath, 'utf8');
            const match = gitFileContent.match(/gitdir: (.+)/);
            if (match) {
                const gitDir = match[1].trim();
                actualGitConfigPath = path.join(path.resolve(folderPath, gitDir), 'config');
            }
        }

        // Read Git config to find remote URL
        if (!fs.existsSync(actualGitConfigPath)) {
            return { isGitRepo: true };
        }

        const configContent = fs.readFileSync(actualGitConfigPath, 'utf8');
        
        // Look for the origin remote URL
        const remoteMatch = configContent.match(/\[remote "origin"\]\s*\n(?:[^\[]*\n)*?\s*url = (.+)/m);
        const remoteUrl = remoteMatch ? normalizeGitUrl(remoteMatch[1].trim()) : undefined;
 
        return {
            remoteUrl,
            isGitRepo: true
        };
    } catch (error) {
        console.warn('Error reading Git info:', error);
        return { isGitRepo: false };
    }
}
 
//Normalizes Git URLs to a standard format for comparison
 
function normalizeGitUrl(url: string): string {
    // Remove .git suffix if present
    let normalized = url.replace(/\.git$/, '');
    
    // Convert SSH to HTTPS format for consistency
    if (normalized.startsWith('git@')) {
        // Convert git@github.com:user/repo to https://github.com/user/repo
        normalized = normalized.replace(/^git@([^:]+):/, 'https://$1/');
    }
    
    // Ensure it starts with https://
    if (!normalized.startsWith('https://')) {
        // Handle other protocols by converting to https
        normalized = normalized.replace(/^[^:]+:\/\//, 'https://');
    }
    
    return normalized.toLowerCase();
}
 
 
//Generates a unique hash for the project based on available information
 
function generateProjectHash(localPath: string, remoteUrl?: string): string {
    const hash = crypto.createHash('sha256');
    
    if (remoteUrl) {
        // If we have a remote URL, use that as the primary identifier
        hash.update(normalizeGitUrl(remoteUrl));
    } else {
        // Fallback: use the project structure hash
        const structureHash = generateProjectStructureHash(localPath);
        hash.update(structureHash);
    }
    
    return hash.digest('hex').substring(0, 16); // First 16 characters for readability
}
 
/**
 * Generates a hash based on the project's file structure
 * This is used as a fallback when Git info is not available
 */
function generateProjectStructureHash(folderPath: string): string {
    try {
        const importantFiles = [
            'package.json',
            'composer.json',
            'pom.xml',
            'build.gradle',
            'requirements.txt',
            'Cargo.toml',
            'go.mod',
            '.gitignore'
        ];
        
        const existingFiles: string[] = [];
        const fileContents: string[] = [];
        
        for (const file of importantFiles) {
            const filePath = path.join(folderPath, file);
            if (fs.existsSync(filePath)) {
                existingFiles.push(file);
                try {
                    // Read first 1KB of each important file for fingerprinting
                    const content = fs.readFileSync(filePath, 'utf8').substring(0, 1024);
                    fileContents.push(content);
                } catch (error) {
                    // If we can't read the file, just include its name
                    console.warn(`Could not read ${file}:`, error);
                }
            }
        }
        
        // Create hash from folder name + existing files + partial contents
        const projectName = path.basename(folderPath);
        const combinedData = projectName + existingFiles.join(',') + fileContents.join('|');
        
        return crypto.createHash('sha256').update(combinedData).digest('hex');
    } catch (error) {
        console.warn('Error generating structure hash:', error);
        // Ultimate fallback: just use the folder name
        return crypto.createHash('sha256').update(path.basename(folderPath)).digest('hex');
    }
}
 
 
//Compares two project hashes to determine if they represent the same project
 
export function isSameProject(hash1: string, hash2: string): boolean {
    return hash1.toLowerCase() === hash2.toLowerCase();
}
 
 
//Validates if the current workspace matches the expected project
 
export function validateCurrentProject(expectedProjectHash: string, expectedRemoteUrl?: string): {
    isMatch: boolean;
    currentProject: ProjectInfo | null;
    reason?: string;
} {
    const currentProject = getCurrentProjectInfo();
    
    if (!currentProject) {
        return {
            isMatch: false,
            currentProject: null,
            reason: 'No workspace folder is currently open'
        };
    }
 
    // Enforce Git requirement for team projects
    if (!currentProject.isGitRepo || !currentProject.remoteUrl) {
        return {
            isMatch: false,
            currentProject,
            reason: 'Team functionality requires a Git repository with remote origin. Please clone the team\'s repository.'
        };
    }
    
    // Primary check: compare project hashes
    if (isSameProject(currentProject.projectHash, expectedProjectHash)) {
        return {
            isMatch: true,
            currentProject
        };
    }
    
    // Secondary check: if both have remote URLs, compare those
    if (currentProject.remoteUrl && expectedRemoteUrl) {
        const currentNormalized = normalizeGitUrl(currentProject.remoteUrl);
        const expectedNormalized = normalizeGitUrl(expectedRemoteUrl);
        
        if (currentNormalized === expectedNormalized) {
            return {
                isMatch: true,
                currentProject
            };
        }
    }
    
    return {
        isMatch: false,
        currentProject,
        reason: expectedRemoteUrl 
            ? `Current project (${currentProject.remoteUrl || 'local'}) doesn't match team project (${expectedRemoteUrl})`
            : `Project fingerprint doesn't match team project`
    };
}
 
 
//Gets a user-friendly description of the current project
 
export function getProjectDescription(project: ProjectInfo): string {
    if (project.remoteUrl) {
        return `${project.projectName} (${project.remoteUrl})`;
    }
    return `${project.projectName} (local folder: ${project.localPath})`;
}