简体   繁体   中英

how to identify stale git branches using script

I need to find and list the branches that are

  • merged
  • not merged
  • merged but not deleted

from multiple projects and multiple repositories with single script from bit bucket and also the report should be printed in this format

project name: repo name: branch name: last commit date: author name:

I tried this in shell script

#!/bin/sh


  echo "Merged branches"
  for branch in `git branch -r --merged | grep -v HEAD`;
  do echo -e `git log --no-merges -n 1 --format="%ci, %cr, %an, %ae, "  $branch 
  | head -n 1` \\t$branch; done | sort -r

  echo ""
  echo "Not merged branches"
  for branch in `git branch -r --no-merged | grep -v HEAD`;
  do echo -e `git log --no-merges -n 1 --format="%ci, %cr, %an, %ae, " $branch | 
  head -n 1` \\t$branch; done | sort -r

by using these i can fetch only in that particular repo. how to list all the projects and repos and execute these git commands?

Bitbucket has some well written api documentation . Within this documentation one can find the resource: /repositories

As quoted from their documentation this endpoint:

Returns a paginated list of all public repositories. This endpoint also supports filtering and sorting of the results. See filtering and sorting for more details.

These are all public repositories you own. Within this response there should be a git url.

You can parse the json and retrieve all git urls of every repository and store them within a variabele. Then loop through the git repositories urls and clone each one of them by the command: git clone --recurse-submodules GIT_URL_HERE The --recurse-submodules option is used here since if there are any submodules we want to get them aswell!

After all repo's are cloned succesfully go into each directory by using the cd DIRECTORY command.

Within these directories one can list the branches.

  1. Listing all merged branches:
    1. git branch --merged
    2. lists branches merged into HEAD (ie tip of current branch)

  2. Listing all not merged branches:
    1. git branch --no-merged
    2. lists branches that have not been merged

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM