Skip to content

Instantly share code, notes, and snippets.

@quark-zju
Created December 1, 2014 01:48
Show Gist options
  • Select an option

  • Save quark-zju/fe164294fc478fdb9fff to your computer and use it in GitHub Desktop.

Select an option

Save quark-zju/fe164294fc478fdb9fff to your computer and use it in GitHub Desktop.

Revisions

  1. quark-zju created this gist Dec 1, 2014.
    41 changes: 41 additions & 0 deletions git-pushf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #!/usr/bin/env ruby


    # Find git root
    while !File.exists?('.git/config')
    Dir.chdir('..')
    raise 'git root not found' if Dir.pwd == '/'
    end

    args = ARGV
    if args.empty?
    args = File.read('.pushf').split rescue []
    end

    args.each do |arg|
    # Run git push
    hostname, path = arg.split(':')
    if !path || path.empty?
    path = File.basename(Dir.pwd)
    end

    if path.empty?
    puts 'unsupported'
    exit 1
    end

    if system 'git', 'push', '-f', "#{hostname}:#{path}"
    system 'git', 'push', '--tags', '-f', "#{hostname}:#{path}"
    else
    system "ssh #{hostname} '[ ! -e #{path}/.git ] && mkdir -p #{path} && cd #{path} && git init && git config receive.denyCurrentBranch ignore'"
    system 'git', 'push', '-f', "#{hostname}:#{path}"
    end

    head = `git rev-parse HEAD`.chomp

    system "ssh #{hostname} 'cd #{path} && echo #{head} > .git/HEAD && git reset --hard HEAD'"
    end

    if args.empty?
    puts 'git pushf host1[:path] [host2[:path]] ...'
    end