this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #11 from HenrickTheBull/Updater-Enhancement

Updating updater to provide clarity on updates as.

authored by

Henrick and committed by
GitHub
537f90cb 59074183

+178 -4
+25
test-updater.js
··· 1 + /** 2 + * Test script for the updater's change statistics functionality 3 + */ 4 + const updater = require('./utils/updater'); 5 + 6 + // Log test title 7 + console.log('=== Testing Updater Change Statistics ==='); 8 + 9 + // Test with different commit ranges 10 + async function runTests() { 11 + // Test with the most recent commit 12 + await updater.testChangeStats(1); 13 + 14 + // Test with the last 3 commits if they exist 15 + await updater.testChangeStats(3); 16 + 17 + // For testing the actual update functionality: 18 + // Uncomment the following line to test the manual update with real fetching 19 + // const updateResult = await updater.manualUpdate(); 20 + // console.log('Update result:', updateResult ? 'Updates applied' : 'No updates available'); 21 + } 22 + 23 + runTests().then(() => { 24 + console.log('Tests completed'); 25 + });
+153 -4
utils/updater.js
··· 57 57 58 58 // Fetch latest changes from origin without merging 59 59 const fetchResult = await execAsync('git fetch origin main'); 60 - console.log('Fetch result:', fetchResult.stdout || 'No output'); 60 + if (fetchResult.stdout) { 61 + console.log(fetchResult.stdout); 62 + } 61 63 62 64 // Check if there are any changes to pull 63 65 const statusResult = await execAsync('git rev-list HEAD..origin/main --count'); ··· 66 68 if (changeCount > 0) { 67 69 console.log(`Found ${changeCount} new commit(s), pulling updates...`); 68 70 71 + // Save current HEAD for comparison after pull 72 + const oldHead = await execAsync('git rev-parse HEAD'); 73 + const oldHeadHash = oldHead.stdout.trim(); 74 + 69 75 // Pull the changes 70 76 const pullResult = await execAsync('git pull origin main'); 71 - console.log('Pull result:', pullResult.stdout); 77 + console.log(pullResult.stdout); 78 + 79 + // Get the new HEAD 80 + const newHead = await execAsync('git rev-parse HEAD'); 81 + const newHeadHash = newHead.stdout.trim(); 82 + 83 + // Show change statistics 84 + if (oldHeadHash !== newHeadHash) { 85 + const changeStats = await this.getChangeStats(oldHeadHash, newHeadHash); 86 + if (changeStats) { 87 + console.log(changeStats); 88 + } 89 + } 72 90 73 91 // Restart the bot using PM2 74 92 console.log('Restarting bot with PM2...'); ··· 83 101 } 84 102 85 103 /** 104 + * Get statistics about changes between current and updated code 105 + * @param {string} fromRef - Starting reference (e.g., HEAD) 106 + * @param {string} toRef - Ending reference (e.g., origin/main) 107 + * @returns {Promise<string>} Formatted string showing changes 108 + */ 109 + async getChangeStats(fromRef, toRef) { 110 + try { 111 + // Get the abbreviated hashes for display 112 + const shortFromRef = await execAsync(`git rev-parse --short ${fromRef}`); 113 + const shortToRef = await execAsync(`git rev-parse --short ${toRef}`); 114 + const fromRefShort = shortFromRef.stdout.trim(); 115 + const toRefShort = shortToRef.stdout.trim(); 116 + 117 + // Show what we're updating from/to 118 + let output = `Updating ${fromRefShort}..${toRefShort}\n`; 119 + 120 + // Add the fast-forward indicator that Git shows 121 + output += "Fast-forward\n"; 122 + 123 + // Use git diff --stat which shows the files changed and +/- indicators 124 + // Use --numstat to get the raw numbers for insertions and deletions 125 + const numstatResult = await execAsync(`git diff --numstat ${fromRef} ${toRef}`); 126 + const numstatLines = numstatResult.stdout.trim().split('\n').filter(Boolean); 127 + 128 + // Track total insertions and deletions 129 + let totalInsertions = 0; 130 + let totalDeletions = 0; 131 + 132 + // Process each line to format it like Git's output 133 + const fileChanges = []; 134 + for (const line of numstatLines) { 135 + const [insertions, deletions, path] = line.split('\t'); 136 + 137 + // Skip binary files or files with unusual output 138 + if (insertions === '-' || deletions === '-') continue; 139 + 140 + const insCount = parseInt(insertions, 10) || 0; 141 + const delCount = parseInt(deletions, 10) || 0; 142 + 143 + totalInsertions += insCount; 144 + totalDeletions += delCount; 145 + 146 + // Format file name with change counts 147 + const plusMinus = '+'.repeat(Math.min(insCount, 70)) + '-'.repeat(Math.min(delCount, 70)); 148 + const changeStats = `${plusMinus}`; 149 + 150 + // Calculate the proper width for the file path (similar to Git's output) 151 + const maxWidth = 80; // approximate width for console 152 + const reserved = 10; // space reserved for the stats part 153 + const width = Math.min(path.length, maxWidth - reserved - changeStats.length); 154 + const truncatedPath = path.length > width ? path.substring(0, width) : path; 155 + const paddedPath = truncatedPath.padEnd(width); 156 + 157 + fileChanges.push(`${paddedPath} | ${changeStats}`); 158 + } 159 + 160 + // Add each file's changes to the output 161 + fileChanges.forEach(fc => { 162 + output += ` ${fc}\n`; 163 + }); 164 + 165 + // Add the summary line 166 + const filesChanged = numstatLines.length; 167 + output += `\n ${filesChanged} file${filesChanged !== 1 ? 's' : ''} changed, `; 168 + output += `${totalInsertions} insertion${totalInsertions !== 1 ? 's' : ''}(+), `; 169 + output += `${totalDeletions} deletion${totalDeletions !== 1 ? 's' : ''}(-)`; 170 + 171 + return output; 172 + } catch (error) { 173 + console.error('Error getting change stats:', error); 174 + return ''; 175 + } 176 + } 177 + 178 + /** 86 179 * Check if an update is available (without applying it) 87 180 * @returns {Promise<boolean>} True if updates are available 88 181 */ ··· 104 197 */ 105 198 async manualUpdate() { 106 199 try { 107 - await execAsync('git fetch origin main'); 200 + // Fetch changes from remote 201 + const fetchResult = await execAsync('git fetch origin main'); 202 + if (fetchResult.stdout) { 203 + console.log(fetchResult.stdout); 204 + } 205 + 108 206 const statusResult = await execAsync('git rev-list HEAD..origin/main --count'); 109 207 const changeCount = parseInt(statusResult.stdout.trim(), 10); 110 208 111 209 if (changeCount > 0) { 210 + // Save current HEAD for comparison 211 + const oldHead = await execAsync('git rev-parse HEAD'); 212 + const oldHeadHash = oldHead.stdout.trim(); 213 + 214 + // Pull the changes 112 215 const pullResult = await execAsync('git pull origin main'); 113 - console.log('Manual update result:', pullResult.stdout); 216 + console.log(pullResult.stdout); 217 + 218 + // Get the new HEAD 219 + const newHead = await execAsync('git rev-parse HEAD'); 220 + const newHeadHash = newHead.stdout.trim(); 221 + 222 + // Show change statistics 223 + if (oldHeadHash !== newHeadHash) { 224 + const changeStats = await this.getChangeStats(oldHeadHash, newHeadHash); 225 + if (changeStats) { 226 + console.log(changeStats); 227 + } 228 + } 229 + 114 230 return true; 115 231 } 116 232 return false; 117 233 } catch (error) { 118 234 console.error('Error during manual update:', error); 119 235 return false; 236 + } 237 + } 238 + 239 + /** 240 + * Test function to display change statistics between commits 241 + * @param {number} [commitRange=1] - Number of commits to go back from HEAD 242 + * @returns {Promise<void>} 243 + */ 244 + async testChangeStats(commitRange = 1) { 245 + try { 246 + console.log(`Testing change statistics for the last ${commitRange} commit(s)...`); 247 + 248 + // Get the current HEAD 249 + const head = await execAsync('git rev-parse HEAD'); 250 + const headHash = head.stdout.trim(); 251 + 252 + // Get the commit before HEAD~n 253 + const prevHead = await execAsync(`git rev-parse HEAD~${commitRange}`); 254 + const prevHeadHash = prevHead.stdout.trim(); 255 + 256 + console.log(`Comparing changes between ${prevHeadHash.substring(0, 7)} and ${headHash.substring(0, 7)}`); 257 + 258 + // Get and display the change statistics 259 + const changeStats = await this.getChangeStats(prevHeadHash, headHash); 260 + if (changeStats) { 261 + console.log('\n--- Change Statistics ---'); 262 + console.log(changeStats); 263 + console.log('------------------------\n'); 264 + } else { 265 + console.log('No change statistics available'); 266 + } 267 + } catch (error) { 268 + console.error('Error testing change stats:', error); 120 269 } 121 270 } 122 271 }