Skip to content

Instantly share code, notes, and snippets.

@deeGraYve
Forked from jsanders/README.md
Created October 30, 2013 17:00
Show Gist options
  • Select an option

  • Save deeGraYve/7236185 to your computer and use it in GitHub Desktop.

Select an option

Save deeGraYve/7236185 to your computer and use it in GitHub Desktop.

Revisions

  1. @jsanders jsanders revised this gist Jun 21, 2012. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion git-old-branches
    Original file line number Diff line number Diff line change
    @@ -48,4 +48,5 @@ set -e
    git branch $branch_opts --merged $ref |
    tr -d '* ' |
    grep "${remote:-.}" |
    grep -v "^${ref}$"
    grep -v "^${ref}$" |
    grep -v "^smartgrid-v.*-maint$"
  2. Simon Chiang revised this gist Jun 21, 2012. 1 changed file with 10 additions and 5 deletions.
    15 changes: 10 additions & 5 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -34,9 +34,14 @@ Example output:
    $ git old-branches github/master | format_branch_deletes
    # schiang@comverge.com

    git push thinkerbot :30434977_fix_the_clock # a262408 (3 weeks ago) <schiang@comverge.com>
    git push thinkerbot :add_non_existant_methods # f47c5c9 (10 days ago) <schiang@comverge.com>
    git push thinkerbot :manual_requires # c9bcc3f (3 weeks ago) <schiang@comverge.com>
    git push thinkerbot :unnecessary_fsr # 54f4eb4 (9 days ago) <schiang@comverge.com>
    git push github :30434977_fix_the_clock # a262408 (3 weeks ago) <schiang@comverge.com>
    git push github :add_non_existant_methods # f47c5c9 (10 days ago) <schiang@comverge.com>
    git push github :manual_requires # c9bcc3f (3 weeks ago) <schiang@comverge.com>
    git push github :unnecessary_fsr # 54f4eb4 (9 days ago) <schiang@comverge.com>

    The idea is that you can check the delete commands and execute as needed.
    The idea is that you can check the delete commands and execute as needed. Note
    that once the remote branches are deleted your teammates may have to prune the
    deleted branches from their local copies:

    # use --dry-run option first as a precaution
    git remote prune github
  3. Simon Chiang revised this gist Jun 21, 2012. 2 changed files with 20 additions and 23 deletions.
    17 changes: 8 additions & 9 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -23,21 +23,20 @@ Now go to your project.
    # format branch delete commands according to user:
    git old-branches github/master | format_branch_deletes

    # filter branch delete commands by user:
    git old-branches github/master | format_branch_deletes doej

    Example output:

    $ git old-branches | format_branch_deletes
    # simon.a.chiang@gmail.com
    # schiang@comverge.com

    git branch -d remove_new_relic # 301a52b (4 hours ago)
    git branch -d do_a_great_thing # 301a52b (2 hours ago)
    git branch -d remove_new_relic # 301a52b (5 hours ago) <schiang@comverge.com>
    git branch -d something # 301a52b (5 hours ago) <schiang@comverge.com>

    $ git old-branches github/master | format_branch_deletes
    # simon.a.chiang@gmail.com
    # schiang@comverge.com

    git push github :remove_new_relic # 301a52b (4 hours ago)
    git push github :do_a_great_thing # 301a52b (2 hours ago)
    git push thinkerbot :30434977_fix_the_clock # a262408 (3 weeks ago) <schiang@comverge.com>
    git push thinkerbot :add_non_existant_methods # f47c5c9 (10 days ago) <schiang@comverge.com>
    git push thinkerbot :manual_requires # c9bcc3f (3 weeks ago) <schiang@comverge.com>
    git push thinkerbot :unnecessary_fsr # 54f4eb4 (9 days ago) <schiang@comverge.com>

    The idea is that you can check the delete commands and execute as needed.
    26 changes: 12 additions & 14 deletions format_branch_deletes
    Original file line number Diff line number Diff line change
    @@ -7,11 +7,18 @@ require 'ostruct'
    #############################################################################
    OptionParser.new do |opts|
    opts.banner = %{
    Usage: #{$0} [options] [USERS]
    Usage: #{$0} [options] [BRANCHES]
    Reads a list of branches from stdin and formats them for deletion based
    on the email of the last person to commit to that branch. Filters
    for the specified users, all users by default.
    Formats a list of branches for deletion. If no branches are listed then
    branches are read from stdin.
    Local branches are formatted like
    git branch -d BRANCH
    Remote branches (remote/branch) are formatted like:
    git push REMOTE :BRANCH
    Options:
    }.lstrip
    @@ -24,8 +31,6 @@ Options:

    end.parse!

    patterns = ARGV.dup
    ARGV.clear
    #############################################################################

    # read branches from stdin and sort by email
    @@ -35,7 +40,7 @@ while branch = gets

    logline = `git log --format="%ae,%h,%cr" -n1 '#{branch}'`
    email, sha, date = logline.strip.split(',')
    msg = "#{sha} (#{date})"
    msg = "#{sha} (#{date}) <#{email}>"

    if branch.include?('/')
    remote, branch = branch.split('/')
    @@ -46,13 +51,6 @@ while branch = gets
    branches_by_email[email] << [remote, branch, msg]
    end

    # filter branches by email
    unless patterns.empty?
    branches_by_email.delete_if do |email, branches|
    ! patterns.any? {|fragment| email.include?(fragment) }
    end
    end

    # print assignments
    template = ERB.new <<-EOF, nil, "<>"
    # <%= email %>
  4. Simon Chiang revised this gist Jun 21, 2012. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -28,11 +28,16 @@ Now go to your project.

    Example output:

    $ git old-branches | format_branch_deletes
    # simon.a.chiang@gmail.com

    git branch -d remove_new_relic # 301a52b (4 hours ago)
    git branch -d do_a_great_thing # 301a52b (2 hours ago)

    # john.doe@gmail.com
    $ git old-branches github/master | format_branch_deletes
    # simon.a.chiang@gmail.com

    git push github :remove_new_relic # 301a52b (4 hours ago)
    git push github :do_a_great_thing # 301a52b (2 hours ago)

    git branch -d what_was_that # 301a52b (10 days ago)
    The idea is that you can check the delete commands and execute as needed.
  5. Simon Chiang revised this gist Jun 21, 2012. 1 changed file with 38 additions and 0 deletions.
    38 changes: 38 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    Description
    ======================

    Find and delete old git branches that are no longer needed.

    Usage
    ======================

    Clone the repo and add to your path (just for ease of use):

    git clone git://gist.github.com/2891516.git gist-2891516
    cd gist-2891516
    export PATH="$PATH:$(pwd)"

    Now go to your project.

    # list branches that have been merged to the current branch
    git old-branches

    # list branches that have been merged to the remote:
    git old-branches github/master

    # format branch delete commands according to user:
    git old-branches github/master | format_branch_deletes

    # filter branch delete commands by user:
    git old-branches github/master | format_branch_deletes doej

    Example output:

    # simon.a.chiang@gmail.com

    git branch -d remove_new_relic # 301a52b (4 hours ago)
    git branch -d do_a_great_thing # 301a52b (2 hours ago)

    # john.doe@gmail.com

    git branch -d what_was_that # 301a52b (10 days ago)
  6. Simon Chiang revised this gist Jun 21, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions format_branch_deletes
    Original file line number Diff line number Diff line change
    @@ -59,9 +59,9 @@ template = ERB.new <<-EOF, nil, "<>"
    <% branches.sort.each do |(remote, branch, msg)| %>
    <% if remote.empty? %>
    git branch -d <%= branch.ljust(max) %> # <%= msg %>
    git branch -d <%= branch.ljust(max) %> # <%= msg %>
    <% else %>
    git push <%= remote %> :<%= branch.ljust(max) %> # <%= msg %>
    git push <%= remote %> :<%= branch.ljust(max) %> # <%= msg %>
    <% end %>
    <% end %>
  7. Simon Chiang revised this gist Jun 21, 2012. 1 changed file with 51 additions and 48 deletions.
    99 changes: 51 additions & 48 deletions format_branch_deletes
    Original file line number Diff line number Diff line change
    @@ -1,81 +1,84 @@
    #!/usr/bin/env ruby
    begin
    require 'optparse'
    require 'erb'
    require 'ostruct'
    require 'optparse'
    require 'erb'
    require 'ostruct'

    OptionParser.new do |opts|
    opts.banner = %{
    #############################################################################
    OptionParser.new do |opts|
    opts.banner = %{
    Usage: #{$0} [options] [USERS]
    Reads a list of branches from stdin and formats them for deletion based
    on the email of the last person to commit to that branch. Filters
    for the specified users, all users by default.
    Reads a list of branches from stdin and formats them for deletion based
    on the email of the last person to commit to that branch. Filters
    for the specified users, all users by default.
    Options:
    }.lstrip

    opts.on("-h", "--help", "print this help") do
    puts opts
    puts
    exit
    end

    end.parse!
    opts.on("-h", "--help", "print this help") do
    puts opts
    puts
    exit
    end

    patterns = ARGV.dup
    ARGV.clear
    end.parse!

    # read branches from stdin and sort by email
    branches_by_email = Hash.new {|hash, key| hash[key] = [] }
    while branch = gets
    branch.strip!
    patterns = ARGV.dup
    ARGV.clear
    #############################################################################

    logline = `git log --format="%ae,%h,%cr" -n1 '#{branch}'`
    email, sha, date = logline.strip.split(',')
    msg = "#{sha} (#{date})"
    # read branches from stdin and sort by email
    branches_by_email = Hash.new {|hash, key| hash[key] = [] }
    while branch = gets
    branch.strip!

    if branch.include?('/')
    remote, branch = branch.split('/')
    else
    remote = ""
    end
    logline = `git log --format="%ae,%h,%cr" -n1 '#{branch}'`
    email, sha, date = logline.strip.split(',')
    msg = "#{sha} (#{date})"

    branches_by_email[email] << [remote, branch, msg]
    if branch.include?('/')
    remote, branch = branch.split('/')
    else
    remote = ""
    end

    # filter branches by email
    unless patterns.empty?
    branches_by_email.delete_if do |email, branches|
    ! patterns.any? {|fragment| email.include?(fragment) }
    end
    branches_by_email[email] << [remote, branch, msg]
    end

    # filter branches by email
    unless patterns.empty?
    branches_by_email.delete_if do |email, branches|
    ! patterns.any? {|fragment| email.include?(fragment) }
    end
    end

    # print assignments
    template = ERB.new <<-EOF, nil, "<>"
    # print assignments
    template = ERB.new <<-EOF, nil, "<>"
    # <%= email %>
    <% branches.sort.each do |(remote, branch, msg)| %>
    <% if remote.empty? %>
    git branch -d <%= branch.ljust(max) %> # <%= msg %>
    git branch -d <%= branch.ljust(max) %> # <%= msg %>
    <% else %>
    git push <%= remote %> :<%= branch.ljust(max) %> # <%= msg %>
    git push <%= remote %> :<%= branch.ljust(max) %> # <%= msg %>
    <% end %>
    <% end %>
    EOF

    branches_by_email.keys.sort.each do |email|
    branches = branches_by_email[email]
    binding = OpenStruct.new(
    :email => email,
    :branches => branches,
    :max => branches.map {|r,b,m| b.length }.max
    ).send(:binding)
    branches_by_email.keys.sort.each do |email|
    branches = branches_by_email[email]
    binding = OpenStruct.new(
    :email => email,
    :branches => branches,
    :max => branches.map {|r,b,m| b.length }.max
    ).send(:binding)

    puts template.result(binding)
    end
    puts template.result(binding)
    end

    #############################################################################
    rescue(Errno::EPIPE)
    raise if $DEBUG
    if $!.class == Errno::EPIPE
  8. Simon Chiang revised this gist Jun 21, 2012. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions git-old-branches
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    #!/bin/bash
    ############################################################################
    # Lists merged branches locally or on a remote
    ############################################################################
    progname="${0##*/}"
    version="1.0"
    author="Simon Chiang"
  9. Simon Chiang renamed this gist Jun 21, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions assign_branch_deletes → format_branch_deletes
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ begin
    opts.banner = %{
    Usage: #{$0} [options] [USERS]
    Reads a list of branches from stdin and assigns them for deletion based
    Reads a list of branches from stdin and formats them for deletion based
    on the email of the last person to commit to that branch. Filters
    for the specified users, all users by default.
    @@ -53,7 +53,7 @@ Options:

    # print assignments
    template = ERB.new <<-EOF, nil, "<>"
    %% <%= email %>
    # <%= email %>
    <% branches.sort.each do |(remote, branch, msg)| %>
    <% if remote.empty? %>
  10. Simon Chiang revised this gist Jun 21, 2012. 1 changed file with 67 additions and 25 deletions.
    92 changes: 67 additions & 25 deletions assign_branch_deletes
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,58 @@
    #!/usr/bin/env ruby
    begin
    require 'optparse'
    require 'erb'
    require 'ostruct'

    require 'erb'
    require 'ostruct'
    OptionParser.new do |opts|
    opts.banner = %{
    Usage: #{$0} [options] [USERS]
    template = ERB.new <<-EOF, nil, "<>"
    Reads a list of branches from stdin and assigns them for deletion based
    on the email of the last person to commit to that branch. Filters
    for the specified users, all users by default.
    Options:
    }.lstrip

    opts.on("-h", "--help", "print this help") do
    puts opts
    puts
    exit
    end

    end.parse!

    patterns = ARGV.dup
    ARGV.clear

    # read branches from stdin and sort by email
    branches_by_email = Hash.new {|hash, key| hash[key] = [] }
    while branch = gets
    branch.strip!

    logline = `git log --format="%ae,%h,%cr" -n1 '#{branch}'`
    email, sha, date = logline.strip.split(',')
    msg = "#{sha} (#{date})"

    if branch.include?('/')
    remote, branch = branch.split('/')
    else
    remote = ""
    end

    branches_by_email[email] << [remote, branch, msg]
    end

    # filter branches by email
    unless patterns.empty?
    branches_by_email.delete_if do |email, branches|
    ! patterns.any? {|fragment| email.include?(fragment) }
    end
    end

    # print assignments
    template = ERB.new <<-EOF, nil, "<>"
    %% <%= email %>
    <% branches.sort.each do |(remote, branch, msg)| %>
    @@ -16,30 +65,23 @@ template = ERB.new <<-EOF, nil, "<>"
    EOF

    branches_by_email = Hash.new {|hash, key| hash[key] = [] }
    while branch = gets
    branch.strip!
    branches_by_email.keys.sort.each do |email|
    branches = branches_by_email[email]
    binding = OpenStruct.new(
    :email => email,
    :branches => branches,
    :max => branches.map {|r,b,m| b.length }.max
    ).send(:binding)

    logline = `git log --format="%ae,%h,%cr" -n1 '#{branch}'`
    email, sha, date = logline.strip.split(',')
    msg = "#{sha} (#{date})"
    puts template.result(binding)
    end

    if branch.include?('/')
    remote, branch = branch.split('/')
    rescue(Errno::EPIPE)
    raise if $DEBUG
    if $!.class == Errno::EPIPE
    exit 0
    else
    remote = ""
    puts "#{$!.message} (see '#{$0} --help')"
    exit 1
    end

    branches_by_email[email] << [remote, branch, msg]
    end

    branches_by_email.keys.sort.each do |email|
    branches = branches_by_email[email]
    binding = OpenStruct.new(
    :email => email,
    :branches => branches,
    :max => branches.map {|r,b,m| b.length }.max
    ).send(:binding)

    puts template.result(binding)
    end
  11. Simon Chiang renamed this gist Jun 21, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  12. Simon Chiang revised this gist Jun 21, 2012. 2 changed files with 45 additions and 35 deletions.
    45 changes: 45 additions & 0 deletions assign_branch_delete
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    #!/usr/bin/env ruby

    require 'erb'
    require 'ostruct'

    template = ERB.new <<-EOF, nil, "<>"
    %% <%= email %>
    <% branches.sort.each do |(remote, branch, msg)| %>
    <% if remote.empty? %>
    git branch -d <%= branch.ljust(max) %> # <%= msg %>
    <% else %>
    git push <%= remote %> :<%= branch.ljust(max) %> # <%= msg %>
    <% end %>
    <% end %>
    EOF

    branches_by_email = Hash.new {|hash, key| hash[key] = [] }
    while branch = gets
    branch.strip!

    logline = `git log --format="%ae,%h,%cr" -n1 '#{branch}'`
    email, sha, date = logline.strip.split(',')
    msg = "#{sha} (#{date})"

    if branch.include?('/')
    remote, branch = branch.split('/')
    else
    remote = ""
    end

    branches_by_email[email] << [remote, branch, msg]
    end

    branches_by_email.keys.sort.each do |email|
    branches = branches_by_email[email]
    binding = OpenStruct.new(
    :email => email,
    :branches => branches,
    :max => branches.map {|r,b,m| b.length }.max
    ).send(:binding)

    puts template.result(binding)
    end
    35 changes: 0 additions & 35 deletions make_emails.rb
    Original file line number Diff line number Diff line change
    @@ -1,35 +0,0 @@
    #!/usr/bin/env ruby

    require 'erb'
    require 'ostruct'

    template = ERB.new <<-EOF
    Hello Mr. or Mrs. "<%= names %>"
    These commands will delete branches that have already been merged into master. You were the last person to commit to the branch, so you probably know best whether or not the branch is still needed.
    <% branches.sort.each do |(remote, branch, sha)| %>
    git push git@github.com:comverge/IntelliSOURCE.git :<%= branch %>\t# <%= sha.strip %><% end %>
    Thanks!
    EOF

    emails={}
    while line=gets
    email, sha, ref, name = line.split(' ', 4)
    remote, branch = ref.split('/')

    emails[email] ||= {
    :names => name.strip,
    :branches => []
    }

    emails[email][:branches] << [remote, branch, sha]
    end

    emails.each do |email, data|
    data = OpenStruct.new(data)
    puts "%%"
    puts email
    puts "Old Branches on GitHub"
    puts template.result(data.send(:binding))
    end
  13. Simon Chiang revised this gist Jun 21, 2012. 1 changed file with 23 additions and 40 deletions.
    63 changes: 23 additions & 40 deletions git-old-branches
    Original file line number Diff line number Diff line change
    @@ -5,66 +5,49 @@
    progname="${0##*/}"
    version="1.0"
    author="Simon Chiang"
    usestr="usage: %s [-h] [-r remote] [ref]\n"
    usestr="usage: %s [-h] [reference_branch]
    Prints branches already merged to the reference branch (by default the
    current branch). If a remote branch is given, then branches for that
    remote are considered.
    "
    optstr=" %s %s\n"

    remote=""
    while getopts "hr:" opt
    while getopts "h" opt
    do
    case $opt in
    d ) delete="true" ;;
    h ) printf "$usestr" "$progname"
    printf "%s" "$hel"
    printf "options:\n"
    printf "$optstr" "-h" "prints this help"
    printf "$optstr" "-r" "operate on named remote"
    printf "\n"
    exit 0 ;;
    r ) remote="$OPTARG" ;;
    r ) remote="$OPTARG/" ;;
    \? ) printf "$usestr" "$progname"
    exit 2 ;;
    esac
    done
    shift $(($OPTIND - 1))

    ref="${2:-master}"

    ############################################################################
    git="git"
    git_branch="$git branch"
    branch_filter="."
    current_branch () {
    git branch | grep '*' | awk '{print $2}'
    }

    ref="${1:-$(current_branch)}"
    remote=$(printf "%s" "$ref" | sed -ne 's/\/.*//p')
    branch_opts=""

    if ! [ x"$remote" = x ]
    then
    git_branch="$git_branch -r"
    branch_filter="$remote/"
    then branch_opts="-r"
    fi

    ############################################################################
    set -e

    # list reverted commits
    reverted_commits () {
    $git log "$ref" |
    grep 'This reverts commit' |
    awk '{ print substr($4, 1, 7) }' |
    tr -d '.,'
    }

    # list branched merged to ref
    branches () {
    $git_branch -v | tr -d '*'
    }

    merged_branches () {
    $git_branch --merged "$ref" | tr -d '*'
    }

    # list sha+branches that have been merged,
    # minus any reverts and the ref commit itself
    join <(merged_branches) <(branches) |
    awk '{ print $2 " " $1 }' |
    grep "$branch_filter" |
    grep -v -e "^$($git log --format='%h' -n 1 "$ref")" |
    join -v1 - <(reverted_commits) |
    while read sha ref
    do git log -n1 --format="%ae %h $ref %an" $sha
    done
    git branch $branch_opts --merged $ref |
    tr -d '* ' |
    grep "${remote:-.}" |
    grep -v "^${ref}$"
  14. Simon Chiang revised this gist Jun 21, 2012. 2 changed files with 39 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion git-old-branches
    Original file line number Diff line number Diff line change
    @@ -64,4 +64,7 @@ join <(merged_branches) <(branches) |
    awk '{ print $2 " " $1 }' |
    grep "$branch_filter" |
    grep -v -e "^$($git log --format='%h' -n 1 "$ref")" |
    join -v1 - <(reverted_commits)
    join -v1 - <(reverted_commits) |
    while read sha ref
    do git log -n1 --format="%ae %h $ref %an" $sha
    done
    35 changes: 35 additions & 0 deletions make_emails.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #!/usr/bin/env ruby

    require 'erb'
    require 'ostruct'

    template = ERB.new <<-EOF
    Hello Mr. or Mrs. "<%= names %>"
    These commands will delete branches that have already been merged into master. You were the last person to commit to the branch, so you probably know best whether or not the branch is still needed.
    <% branches.sort.each do |(remote, branch, sha)| %>
    git push git@github.com:comverge/IntelliSOURCE.git :<%= branch %>\t# <%= sha.strip %><% end %>
    Thanks!
    EOF

    emails={}
    while line=gets
    email, sha, ref, name = line.split(' ', 4)
    remote, branch = ref.split('/')

    emails[email] ||= {
    :names => name.strip,
    :branches => []
    }

    emails[email][:branches] << [remote, branch, sha]
    end

    emails.each do |email, data|
    data = OpenStruct.new(data)
    puts "%%"
    puts email
    puts "Old Branches on GitHub"
    puts template.result(data.send(:binding))
    end
  15. Simon Chiang revised this gist Jun 20, 2012. 1 changed file with 15 additions and 10 deletions.
    25 changes: 15 additions & 10 deletions git-old-branches
    Original file line number Diff line number Diff line change
    @@ -42,21 +42,26 @@ fi
    set -e

    # list reverted commits
    $git log "$ref" |
    grep 'This reverts commit' |
    awk '{ print substr($4, 1, 7) }' |
    tr -d '.,' > reverts.tmp
    reverted_commits () {
    $git log "$ref" |
    grep 'This reverts commit' |
    awk '{ print substr($4, 1, 7) }' |
    tr -d '.,'
    }

    # list branched merged to ref
    $git_branch -v | tr -d '*' > branches.tmp
    $git_branch --merged "$ref" | tr -d '*' > merged.tmp
    branches () {
    $git_branch -v | tr -d '*'
    }

    merged_branches () {
    $git_branch --merged "$ref" | tr -d '*'
    }

    # list sha+branches that have been merged,
    # minus any reverts and the ref commit itself
    join merged.tmp branches.tmp |
    join <(merged_branches) <(branches) |
    awk '{ print $2 " " $1 }' |
    grep "$branch_filter" |
    grep -v -e "^$($git log --format='%h' -n 1 "$ref")" |
    join -v1 - reverts.tmp

    rm branches.tmp merged.tmp reverts.tmp
    join -v1 - <(reverted_commits)
  16. Simon Chiang revised this gist Jun 20, 2012. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions git-old-branches
    Original file line number Diff line number Diff line change
    @@ -5,11 +5,10 @@
    progname="${0##*/}"
    version="1.0"
    author="Simon Chiang"
    usestr="usage: %s [-h] [-r remote] [path_to_repo] [ref]\n"
    usestr="usage: %s [-h] [-r remote] [ref]\n"
    optstr=" %s %s\n"

    remote=""
    delete="false"
    while getopts "hr:" opt
    do
    case $opt in
    @@ -26,11 +25,10 @@ do
    done
    shift $(($OPTIND - 1))

    repo="${1:-.}"
    ref="${2:-master}"

    ############################################################################
    git="git --git-dir $repo/.git"
    git="git"
    git_branch="$git branch"
    branch_filter="."

  17. Simon Chiang revised this gist Jun 20, 2012. 1 changed file with 7 additions and 19 deletions.
    26 changes: 7 additions & 19 deletions git-old-branches
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,19 @@
    #!/bin/bash
    ############################################################################
    # Lists merged branches locally or on a remote
    ############################################################################
    progname="${0##*/}"
    version="1.0"
    author="Simon Chiang"
    usestr="usage: %s [-h] [path_to_repo] [ref]\n"
    usestr="usage: %s [-h] [-r remote] [path_to_repo] [ref]\n"
    optstr=" %s %s\n"

    remote=""
    delete="false"
    while getopts "hr:" opt
    do
    case $opt in
    d ) delete="true" ;;
    h ) printf "$usestr" "$progname"
    printf "$optstr" "-h" "prints this help"
    printf "$optstr" "-r" "operate on named remote"
    @@ -55,22 +59,6 @@ join merged.tmp branches.tmp |
    awk '{ print $2 " " $1 }' |
    grep "$branch_filter" |
    grep -v -e "^$($git log --format='%h' -n 1 "$ref")" |
    join -v1 - reverts.tmp

    rm branches.tmp merged.tmp reverts.tmp
    join -v1 - reverts.tmp

    # remove the branches
    # | awk '{print $2}' |
    # if [ x"$remote" = x ]
    # then
    # while read name
    # do $git branch -d "$name"
    # done
    # else
    # awk -F '/' '{print $2}' |
    # while read name
    # do $git push "$remote" ":$name"
    # done
    #
    # $git remote prune "$remote"
    # fi
    rm branches.tmp merged.tmp reverts.tmp
  18. Simon Chiang revised this gist Jun 20, 2012. 1 changed file with 5 additions and 7 deletions.
    12 changes: 5 additions & 7 deletions git-old-branches
    Original file line number Diff line number Diff line change
    @@ -3,18 +3,14 @@
    progname="${0##*/}"
    version="1.0"
    author="Simon Chiang"
    usestr="usage: %s [-h] [ref]\n"
    usestr="usage: %s [-h] [path_to_repo] [ref]\n"
    optstr=" %s %s\n"

    repo=`pwd`
    remote=""

    while getopts "d:hr:" opt
    while getopts "hr:" opt
    do
    case $opt in
    d ) repo="$OPTARG" ;;
    h ) printf "$usestr" "$progname"
    printf "$optstr" "-d" "path to repo"
    printf "$optstr" "-h" "prints this help"
    printf "$optstr" "-r" "operate on named remote"
    printf "\n"
    @@ -25,7 +21,9 @@ do
    esac
    done
    shift $(($OPTIND - 1))
    ref="${1:-master}"

    repo="${1:-.}"
    ref="${2:-master}"

    ############################################################################
    git="git --git-dir $repo/.git"
  19. Simon Chiang revised this gist Jun 20, 2012. 1 changed file with 70 additions and 14 deletions.
    84 changes: 70 additions & 14 deletions git-old-branches
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,78 @@
    #!/bin/bash
    set -e
    ############################################################################
    progname="${0##*/}"
    version="1.0"
    author="Simon Chiang"
    usestr="usage: %s [-h] [ref]\n"
    optstr=" %s %s\n"

    repo=`pwd`
    remote=""

    while getopts "d:hr:" opt
    do
    case $opt in
    d ) repo="$OPTARG" ;;
    h ) printf "$usestr" "$progname"
    printf "$optstr" "-d" "path to repo"
    printf "$optstr" "-h" "prints this help"
    printf "$optstr" "-r" "operate on named remote"
    printf "\n"
    exit 0 ;;
    r ) remote="$OPTARG" ;;
    \? ) printf "$usestr" "$progname"
    exit 2 ;;
    esac
    done
    shift $(($OPTIND - 1))
    ref="${1:-master}"

    branch=remotes/$1
    ############################################################################
    git="git --git-dir $repo/.git"
    git_branch="$git branch"
    branch_filter="."

    function reverted_commits () {
    git log | grep 'This reverts commit' | awk '{ print substr($4, 1, 7) }' | tr -d '.,'
    }
    if ! [ x"$remote" = x ]
    then
    git_branch="$git_branch -r"
    branch_filter="$remote/"
    fi

    ############################################################################
    set -e

    function branches () {
    git branch -av | grep "$1" | sed -e "s|$1/||" | awk '{ print $2 " " $1 }'
    }
    # list reverted commits
    $git log "$ref" |
    grep 'This reverts commit' |
    awk '{ print substr($4, 1, 7) }' |
    tr -d '.,' > reverts.tmp

    branches "$branch" | sort > branches.tmp
    reverted_commits | sort > reverts.tmp
    # list branched merged to ref
    $git_branch -v | tr -d '*' > branches.tmp
    $git_branch --merged "$ref" | tr -d '*' > merged.tmp

    # Remove branches that have been reverted
    join -v1 branches.tmp reverts.tmp > candidates.tmp
    # list sha+branches that have been merged,
    # minus any reverts and the ref commit itself
    join merged.tmp branches.tmp |
    awk '{ print $2 " " $1 }' |
    grep "$branch_filter" |
    grep -v -e "^$($git log --format='%h' -n 1 "$ref")" |
    join -v1 - reverts.tmp

    git log --format='%h' "$branch"/master | sort | join candidates.tmp -
    rm branches.tmp merged.tmp reverts.tmp

    rm branches.tmp reverts.tmp candidates.tmp
    # remove the branches
    # | awk '{print $2}' |
    # if [ x"$remote" = x ]
    # then
    # while read name
    # do $git branch -d "$name"
    # done
    # else
    # awk -F '/' '{print $2}' |
    # while read name
    # do $git push "$remote" ":$name"
    # done
    #
    # $git remote prune "$remote"
    # fi
  20. @thinkerbot thinkerbot created this gist Jun 7, 2012.
    22 changes: 22 additions & 0 deletions git-old-branches
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #!/bin/bash
    set -e

    branch=remotes/$1

    function reverted_commits () {
    git log | grep 'This reverts commit' | awk '{ print substr($4, 1, 7) }' | tr -d '.,'
    }

    function branches () {
    git branch -av | grep "$1" | sed -e "s|$1/||" | awk '{ print $2 " " $1 }'
    }

    branches "$branch" | sort > branches.tmp
    reverted_commits | sort > reverts.tmp

    # Remove branches that have been reverted
    join -v1 branches.tmp reverts.tmp > candidates.tmp

    git log --format='%h' "$branch"/master | sort | join candidates.tmp -

    rm branches.tmp reverts.tmp candidates.tmp