Skip to content

Instantly share code, notes, and snippets.

@cyocun
Created August 18, 2015 08:50
Show Gist options
  • Save cyocun/b1f9822178bee071915b to your computer and use it in GitHub Desktop.
Save cyocun/b1f9822178bee071915b to your computer and use it in GitHub Desktop.

Revisions

  1. cyocun created this gist Aug 18, 2015.
    84 changes: 84 additions & 0 deletions new_gist_file.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    #!/usr/bin/ruby -Ku

    require 'fileutils'

    # つかいかた
    if ARGV.length == 0
    print "[Usage?] $ git_diff [from SHA] [to SHA]\n"
    exit(false)
    end

    # 引数が足りないよ
    if ARGV.length != 2
    print "[Error!] Arguments are not worth."
    exit(false)
    end

    # 引数をとるよ
    revFrom = ARGV[0]
    revTo = ARGV[1]
    dir = "./diff"

    # エクスポート先が確保できないよ
    if !File::exist?(dir) && !FileUtils.mkdir_p(dir, :mode => 0755)
    print "[Error!] Specified dirname is not exists & cannot mkdir.\n"
    exit(false)
    end

    # おしりにスラッシュ
    if !dir.match(/\/$/)
    dir += '/'
    end

    # git diffを実行
    print "[ exec ] git diff --name-status #{revFrom} #{revTo}\n"
    lines = `git diff --name-status #{revFrom} #{revTo}`
    dirName = dir + Time.now.strftime("%y%m%d") + '--' + revFrom + '_' + revTo;


    # 削除リスト
    killnote = []

    # 1行ずつ処理
    lines.each_line do |line|

    line = line.sub(/\n$/, '')
    status = line.slice!(/^[ADM]{1}\s+/).sub(/\s+/, '')

    # D のとき
    if status == 'D'
    killnote.push(line)
    # A, M のとき
    else
    from = line.sub(/\n$/, '')
    to = dirName + '/' + from
    dirname = File::dirname(to)

    # すでに存在するディレクトリは無視
    if File::exist?(dirname)
    #print "[notice] This dir(#{dirname}) was already created.\n"
    # いずれにも該当しなければ作成
    else
    # print "[ mkdir] #{dirname}\n"
    FileUtils.mkdir_p(dirname)
    end

    # ファイルをコピー
    if File::exist?(from)
    # print "[ copy ] #{from} => #{to}\n"
    FileUtils.cp(from, to)
    end

    end

    end

    # デスノート ...〆(.. )
    if killnote != []
    print "[remove] Please check '__REMOVE_US__.txt' and delete them.\n"
    F = open(dirName+'__REMOVE_US__.txt', 'w') do |f|
    f.puts killnote.join("\n")
    end
    end

    print "\n ---end process\n"