git-garbage 1.1.17 → 1.1.19

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.
Files changed (2) hide show
  1. package/garbage.sh +44 -15
  2. package/package.json +2 -3
package/garbage.sh CHANGED
@@ -14,7 +14,19 @@ current="$(git rev-parse --abbrev-ref HEAD)"
14
14
 
15
15
  declare -a branches
16
16
 
17
- # merged
17
+ # Check if a branch was part of remote workflow (pushed at some point)
18
+ was_pushed() {
19
+ local branch="$1"
20
+ # Has remote tracking configured
21
+ git config --get "branch.$branch.remote" > /dev/null 2>&1 && return 0
22
+ # Has a remote tracking branch
23
+ git show-ref --verify --quiet "refs/remotes/origin/$branch" && return 0
24
+ # Has merge commits referencing this branch (PR workflow evidence)
25
+ [[ -n $(git log --oneline --grep="$branch" --merges -1 2>/dev/null) ]] && return 0
26
+ return 1
27
+ }
28
+
29
+ # merged (no was_pushed check: remote may be gone after PR merge)
18
30
  for branch in $(git for-each-ref --format "%(refname:short)" refs/heads/ --merged); do
19
31
  if [[ "$branch" != "$current" ]]; then
20
32
  branches+=("$branch")
@@ -23,30 +35,23 @@ done
23
35
 
24
36
  # squashed
25
37
  for branch in $(git for-each-ref --format "%(refname:short)" refs/heads/); do
26
- if [[ "$branch" != "$current" ]]; then
38
+ if [[ "$branch" != "$current" ]] && was_pushed "$branch"; then
27
39
  mergeBase=$(git merge-base "$current" "$branch")
28
40
  if [[ $(git cherry "$current" "$(git commit-tree "$(git rev-parse "$branch^{tree}")" -p "$mergeBase" -m _)") == "-"* ]]; then
29
- branches+=("$branch")
41
+ # Avoid duplicates
42
+ if [[ ! " ${branches[*]} " =~ " $branch " ]]; then
43
+ branches+=("$branch")
44
+ fi
30
45
  fi
31
46
  fi
32
47
  done
33
48
 
34
49
  # branches that were part of PR workflow (feature branches with remote deleted)
35
50
  for branch in $(git for-each-ref --format "%(refname:short)" refs/heads/); do
36
- if [[ "$branch" != "$current" ]]; then
37
- # Check if branch has commits not in main (indicating it was a feature branch)
51
+ if [[ "$branch" != "$current" ]] && was_pushed "$branch"; then
38
52
  if [[ $(git rev-list --count "$current..$branch" 2>/dev/null) -gt 0 ]]; then
39
- # Check if remote branch doesn't exist (indicating PR was closed and branch deleted)
40
53
  if ! git show-ref --verify --quiet "refs/remotes/origin/$branch"; then
41
- # Check if branch is already in our list to avoid duplicates
42
- branch_exists=false
43
- for existing_branch in "${branches[@]}"; do
44
- if [[ "$existing_branch" == "$branch" ]]; then
45
- branch_exists=true
46
- break
47
- fi
48
- done
49
- if [[ "$branch_exists" == false ]]; then
54
+ if [[ ! " ${branches[*]} " =~ " $branch " ]]; then
50
55
  branches+=("$branch")
51
56
  fi
52
57
  fi
@@ -54,6 +59,30 @@ for branch in $(git for-each-ref --format "%(refname:short)" refs/heads/); do
54
59
  fi
55
60
  done
56
61
 
62
+ # branches whose upstream tracking was deleted on remote
63
+ while IFS= read -r line; do
64
+ branch="${line%% *}"
65
+ if [[ "$branch" != "$current" ]] && [[ ! " ${branches[*]} " =~ " $branch " ]]; then
66
+ branches+=("$branch")
67
+ fi
68
+ done < <(git for-each-ref --format "%(refname:short) %(upstream:track)" refs/heads/ | grep '\[gone\]')
69
+
70
+ # branches with closed/merged PRs (requires gh CLI)
71
+ if command -v gh &> /dev/null && gh auth status &> /dev/null; then
72
+ pr_branches=$(gh pr list --state all --json headRefName,state \
73
+ --jq '[.[] | select(.state != "OPEN")] | .[].headRefName' \
74
+ --limit 500 2>/dev/null | sort -u)
75
+ if [[ -n "$pr_branches" ]]; then
76
+ for branch in $(git for-each-ref --format "%(refname:short)" refs/heads/); do
77
+ if [[ "$branch" != "$current" ]] && [[ ! " ${branches[*]} " =~ " $branch " ]]; then
78
+ if echo "$pr_branches" | grep -qxF "$branch"; then
79
+ branches+=("$branch")
80
+ fi
81
+ fi
82
+ done
83
+ fi
84
+ fi
85
+
57
86
  if [[ ${#branches[@]} -eq 0 ]]; then
58
87
  printf "\n Nothing to garbage."
59
88
  exit
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "git-garbage",
3
3
  "description": "Delete local git branches after deleting them on the remote repository.",
4
4
  "homepage": "https://github.com/Kikobeats/git-garbage",
5
- "version": "1.1.17",
5
+ "version": "1.1.19",
6
6
  "bin": {
7
7
  "git-garbage": "garbage.sh"
8
8
  },
@@ -27,7 +27,6 @@
27
27
  "devDependencies": {
28
28
  "@commitlint/cli": "latest",
29
29
  "@commitlint/config-conventional": "latest",
30
- "@ksmithut/prettier-standard": "latest",
31
30
  "ci-publish": "latest",
32
31
  "finepack": "latest",
33
32
  "git-authors-cli": "latest",
@@ -62,7 +61,7 @@
62
61
  },
63
62
  "nano-staged": {
64
63
  "*.js": [
65
- "prettier-standard",
64
+ "npx @kikobeats/prettier-standard",
66
65
  "standard --fix"
67
66
  ],
68
67
  "package.json": [