git-smart-clean 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +142 -0
- package/bin/cli.js +22 -0
- package/package.json +47 -0
- package/src/index.js +182 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 husseinjaafar27
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# git-smart-clean
|
|
2
|
+
|
|
3
|
+
๐งน Smart cleanup tool for merged git branches
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g git-smart-clean
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Basic Usage
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# Interactive mode
|
|
17
|
+
git-smart-clean
|
|
18
|
+
|
|
19
|
+
# Preview without deleting
|
|
20
|
+
git-smart-clean --dry-run
|
|
21
|
+
|
|
22
|
+
# Exclude specific branches
|
|
23
|
+
git-smart-clean --exclude "main,develop,staging"
|
|
24
|
+
|
|
25
|
+
# Only branches older than X days
|
|
26
|
+
git-smart-clean --older-than 90
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Options
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
git-smart-clean [options]
|
|
33
|
+
|
|
34
|
+
Options:
|
|
35
|
+
-V, --version output the version number
|
|
36
|
+
-d, --dry-run Preview without deleting
|
|
37
|
+
--older-than <days> Only branches older than X days
|
|
38
|
+
--exclude <branches> Branches to exclude (default: "main,master,develop")
|
|
39
|
+
-h, --help display help for command
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Features
|
|
43
|
+
|
|
44
|
+
โ
Interactive branch selection
|
|
45
|
+
โ
Dry-run mode for safety
|
|
46
|
+
โ
Shows branch age (committed X ago)
|
|
47
|
+
โ
Color-coded by age (red/yellow/green)
|
|
48
|
+
โ
Sorted by age (oldest first)
|
|
49
|
+
โ
Auto-excludes protected branches
|
|
50
|
+
โ
Beautiful terminal UI
|
|
51
|
+
โ
Safe confirmation prompts
|
|
52
|
+
|
|
53
|
+
## Examples
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
# Basic cleanup
|
|
57
|
+
git-smart-clean
|
|
58
|
+
|
|
59
|
+
# Preview first (recommended)
|
|
60
|
+
git-smart-clean --dry-run
|
|
61
|
+
|
|
62
|
+
# Only very old branches
|
|
63
|
+
git-smart-clean --older-than 180
|
|
64
|
+
|
|
65
|
+
# Exclude custom branches
|
|
66
|
+
git-smart-clean --exclude "main,staging,production,develop"
|
|
67
|
+
|
|
68
|
+
# Combine options
|
|
69
|
+
git-smart-clean --older-than 90 --exclude "main,dev" --dry-run
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## How It Works
|
|
73
|
+
|
|
74
|
+
1. **Scans** for merged branches in your current repository
|
|
75
|
+
2. **Excludes** protected branches (main, master, develop, and current branch)
|
|
76
|
+
3. **Shows** branch age based on last commit date
|
|
77
|
+
4. **Color codes** by age:
|
|
78
|
+
- ๐ด Red: Older than 3 months
|
|
79
|
+
- ๐ก Yellow: 1-3 months old
|
|
80
|
+
- ๐ข Green: Less than 1 month
|
|
81
|
+
5. **Lets you select** which branches to delete (all pre-selected)
|
|
82
|
+
6. **Confirms** before deleting
|
|
83
|
+
7. **Deletes** selected branches safely
|
|
84
|
+
|
|
85
|
+
## Screenshots
|
|
86
|
+
|
|
87
|
+
### Dry Run Mode
|
|
88
|
+
```
|
|
89
|
+
โ Found 5 merged branches
|
|
90
|
+
|
|
91
|
+
๐ DRY RUN - No branches will be deleted
|
|
92
|
+
|
|
93
|
+
โข feature/old-authentication (committed 2 months ago)
|
|
94
|
+
โข bugfix/payment-gateway-fix (committed 3 weeks ago)
|
|
95
|
+
โข hotfix/urgent-security-patch (committed 2 days ago)
|
|
96
|
+
|
|
97
|
+
Would delete 3 branches
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Interactive Mode
|
|
101
|
+
```
|
|
102
|
+
โ Found 5 merged branches
|
|
103
|
+
|
|
104
|
+
? Select branches to delete (Space to select, Enter to confirm):
|
|
105
|
+
>(*) feature/old-authentication (committed 2 months ago)
|
|
106
|
+
(*) bugfix/payment-gateway-fix (committed 3 weeks ago)
|
|
107
|
+
( ) hotfix/urgent-security-patch (committed 2 days ago)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Development
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
# Clone the repo
|
|
114
|
+
git clone https://github.com/husseinjaafar27/git-smart-clean.git
|
|
115
|
+
cd git-smart-clean
|
|
116
|
+
|
|
117
|
+
# Install dependencies
|
|
118
|
+
npm install
|
|
119
|
+
|
|
120
|
+
# Test locally
|
|
121
|
+
npm link
|
|
122
|
+
git-smart-clean --dry-run
|
|
123
|
+
|
|
124
|
+
# Run in another repo
|
|
125
|
+
cd ~/some-other-repo
|
|
126
|
+
git-smart-clean
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Contributing
|
|
130
|
+
|
|
131
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
132
|
+
|
|
133
|
+
## License
|
|
134
|
+
|
|
135
|
+
MIT ยฉ husseinjaafar27
|
|
136
|
+
|
|
137
|
+
## Support
|
|
138
|
+
|
|
139
|
+
If you find this tool helpful, please โญ star the repo!
|
|
140
|
+
|
|
141
|
+
Found a bug? [Open an issue](https://github.com/husseinjaafar27/git-smart-clean/issues)
|
|
142
|
+
```
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { program } from 'commander';
|
|
4
|
+
import { cleanBranches } from '../src/index.js';
|
|
5
|
+
|
|
6
|
+
program
|
|
7
|
+
.name('git-smart-clean')
|
|
8
|
+
.description('Smart cleanup tool for merged git branches')
|
|
9
|
+
.version('1.0.0')
|
|
10
|
+
.option('-d, --dry-run', 'Preview without deleting')
|
|
11
|
+
.option('--older-than <days>', 'Only branches older than X days', parseInt)
|
|
12
|
+
.option('--exclude <branches>', 'Branches to exclude (comma-separated)', 'main,master,develop')
|
|
13
|
+
.action(async (options) => {
|
|
14
|
+
try {
|
|
15
|
+
await cleanBranches(options);
|
|
16
|
+
} catch (error) {
|
|
17
|
+
console.error('Error:', error.message);
|
|
18
|
+
process.exit(1);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
program.parse();
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "git-smart-clean",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Smart cleanup tool for merged git branches",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"bin": {
|
|
8
|
+
"git-smart-clean": "./bin/cli.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"start": "node bin/cli.js"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"git",
|
|
15
|
+
"cli",
|
|
16
|
+
"branches",
|
|
17
|
+
"cleanup",
|
|
18
|
+
"developer-tools",
|
|
19
|
+
"git-branches",
|
|
20
|
+
"merged-branches",
|
|
21
|
+
"branch-management",
|
|
22
|
+
"git-tools"
|
|
23
|
+
],
|
|
24
|
+
"author": "husseinjaafar",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/husseinjaafar27/git-smart-clean.git"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/husseinjaafar27/git-smart-clean/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/husseinjaafar27/git-smart-clean#readme",
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"chalk": "^4.1.2",
|
|
36
|
+
"commander": "^14.0.3",
|
|
37
|
+
"inquirer": "^8.2.7",
|
|
38
|
+
"ora": "^9.3.0",
|
|
39
|
+
"simple-git": "^3.30.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"jest": "^30.2.0"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=16.0.0"
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import simpleGit from 'simple-git';
|
|
2
|
+
import inquirer from 'inquirer';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import ora from 'ora';
|
|
5
|
+
|
|
6
|
+
// Helper function to format relative time
|
|
7
|
+
function getRelativeTime(date) {
|
|
8
|
+
const now = new Date();
|
|
9
|
+
const diffMs = now - date;
|
|
10
|
+
const diffSeconds = Math.floor(diffMs / 1000);
|
|
11
|
+
const diffMinutes = Math.floor(diffSeconds / 60);
|
|
12
|
+
const diffHours = Math.floor(diffMinutes / 60);
|
|
13
|
+
const diffDays = Math.floor(diffHours / 24);
|
|
14
|
+
const diffWeeks = Math.floor(diffDays / 7);
|
|
15
|
+
const diffMonths = Math.floor(diffDays / 30);
|
|
16
|
+
const diffYears = Math.floor(diffDays / 365);
|
|
17
|
+
|
|
18
|
+
if (diffYears > 0) {
|
|
19
|
+
return `${diffYears} ${diffYears === 1 ? 'year' : 'years'} ago`;
|
|
20
|
+
} else if (diffMonths > 0) {
|
|
21
|
+
return `${diffMonths} ${diffMonths === 1 ? 'month' : 'months'} ago`;
|
|
22
|
+
} else if (diffWeeks > 0) {
|
|
23
|
+
return `${diffWeeks} ${diffWeeks === 1 ? 'week' : 'weeks'} ago`;
|
|
24
|
+
} else if (diffDays > 0) {
|
|
25
|
+
return `${diffDays} ${diffDays === 1 ? 'day' : 'days'} ago`;
|
|
26
|
+
} else if (diffHours > 0) {
|
|
27
|
+
return `${diffHours} ${diffHours === 1 ? 'hour' : 'hours'} ago`;
|
|
28
|
+
} else if (diffMinutes > 0) {
|
|
29
|
+
return `${diffMinutes} ${diffMinutes === 1 ? 'minute' : 'minutes'} ago`;
|
|
30
|
+
} else {
|
|
31
|
+
return 'just now';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Color code based on age
|
|
36
|
+
function getAgeColor(date) {
|
|
37
|
+
const now = new Date();
|
|
38
|
+
const diffDays = Math.floor((now - date) / (1000 * 60 * 60 * 24));
|
|
39
|
+
|
|
40
|
+
if (diffDays > 90) {
|
|
41
|
+
return chalk.red; // Red: older than 3 months
|
|
42
|
+
} else if (diffDays > 30) {
|
|
43
|
+
return chalk.yellow; // Yellow: 1-3 months
|
|
44
|
+
} else {
|
|
45
|
+
return chalk.green; // Green: less than 1 month
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function cleanBranches(options) {
|
|
50
|
+
const spinner = ora('Scanning branches...').start();
|
|
51
|
+
const git = simpleGit();
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
// Check if we're in a git repository
|
|
55
|
+
const isRepo = await git.checkIsRepo();
|
|
56
|
+
if (!isRepo) {
|
|
57
|
+
spinner.fail('Not a git repository');
|
|
58
|
+
process.exit(1);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Get current branch
|
|
62
|
+
const status = await git.status();
|
|
63
|
+
const currentBranch = status.current;
|
|
64
|
+
|
|
65
|
+
// Get merged branches
|
|
66
|
+
const branchSummary = await git.branch(['--merged']);
|
|
67
|
+
|
|
68
|
+
// Parse exclude list
|
|
69
|
+
const excludeBranches = options.exclude.split(',').map(b => b.trim());
|
|
70
|
+
excludeBranches.push(currentBranch);
|
|
71
|
+
|
|
72
|
+
// Filter branches
|
|
73
|
+
let mergedBranches = branchSummary.all.filter(branch => {
|
|
74
|
+
const cleanBranch = branch.replace('remotes/origin/', '').trim();
|
|
75
|
+
return !excludeBranches.includes(cleanBranch);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
if (mergedBranches.length === 0) {
|
|
79
|
+
spinner.succeed('No merged branches to clean up!');
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Get last commit date for each branch
|
|
84
|
+
spinner.text = 'Getting branch information...';
|
|
85
|
+
|
|
86
|
+
const branchesWithInfo = await Promise.all(
|
|
87
|
+
mergedBranches.map(async (branch) => {
|
|
88
|
+
try {
|
|
89
|
+
const log = await git.log([branch, '-1']);
|
|
90
|
+
const lastCommitDate = log.latest ? new Date(log.latest.date) : new Date();
|
|
91
|
+
const relativeTime = getRelativeTime(lastCommitDate);
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
name: branch,
|
|
95
|
+
date: lastCommitDate,
|
|
96
|
+
relativeTime: `committed ${relativeTime}`
|
|
97
|
+
};
|
|
98
|
+
} catch (error) {
|
|
99
|
+
return {
|
|
100
|
+
name: branch,
|
|
101
|
+
date: new Date(),
|
|
102
|
+
relativeTime: 'unknown'
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
})
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
// Sort by date (oldest first)
|
|
109
|
+
branchesWithInfo.sort((a, b) => a.date - b.date);
|
|
110
|
+
|
|
111
|
+
spinner.succeed(`Found ${chalk.bold(branchesWithInfo.length)} merged branches`);
|
|
112
|
+
|
|
113
|
+
// Dry run mode
|
|
114
|
+
if (options.dryRun) {
|
|
115
|
+
console.log(chalk.yellow('\n๐ DRY RUN - No branches will be deleted\n'));
|
|
116
|
+
|
|
117
|
+
branchesWithInfo.forEach(b => {
|
|
118
|
+
const ageColor = getAgeColor(b.date);
|
|
119
|
+
console.log(` โข ${chalk.gray(b.name.padEnd(40))} ${ageColor(`(${b.relativeTime})`)}`);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
console.log(chalk.yellow(`\nWould delete ${branchesWithInfo.length} branches`));
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Interactive selection
|
|
127
|
+
const { selectedBranches } = await inquirer.prompt([{
|
|
128
|
+
type: 'checkbox',
|
|
129
|
+
name: 'selectedBranches',
|
|
130
|
+
message: 'Select branches to delete (Space to select, Enter to confirm):',
|
|
131
|
+
choices: branchesWithInfo.map(b => {
|
|
132
|
+
const ageColor = getAgeColor(b.date);
|
|
133
|
+
const displayName = `${b.name.padEnd(40)} ${ageColor(`(${b.relativeTime})`)}`;
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
name: displayName,
|
|
137
|
+
value: b.name,
|
|
138
|
+
checked: true
|
|
139
|
+
};
|
|
140
|
+
}),
|
|
141
|
+
pageSize: 15
|
|
142
|
+
}]);
|
|
143
|
+
|
|
144
|
+
if (selectedBranches.length === 0) {
|
|
145
|
+
console.log(chalk.yellow('No branches selected. Exiting.'));
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Confirm deletion
|
|
150
|
+
const { confirm } = await inquirer.prompt([{
|
|
151
|
+
type: 'confirm',
|
|
152
|
+
name: 'confirm',
|
|
153
|
+
message: `Delete ${selectedBranches.length} branches?`,
|
|
154
|
+
default: false
|
|
155
|
+
}]);
|
|
156
|
+
|
|
157
|
+
if (!confirm) {
|
|
158
|
+
console.log(chalk.yellow('Cancelled. No branches deleted.'));
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Delete branches
|
|
163
|
+
console.log('');
|
|
164
|
+
const deleteSpinner = ora('Deleting branches...').start();
|
|
165
|
+
|
|
166
|
+
for (const branch of selectedBranches) {
|
|
167
|
+
try {
|
|
168
|
+
await git.deleteLocalBranch(branch, true);
|
|
169
|
+
console.log(chalk.green(` โ Deleted ${branch}`));
|
|
170
|
+
} catch (error) {
|
|
171
|
+
console.log(chalk.red(` โ Failed to delete ${branch}: ${error.message}`));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
deleteSpinner.succeed();
|
|
176
|
+
console.log(chalk.bold.green(`\nโ Successfully cleaned up ${selectedBranches.length} branches!\n`));
|
|
177
|
+
|
|
178
|
+
} catch (error) {
|
|
179
|
+
spinner.fail('Error occurred');
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
}
|