# -*- coding: utf-8 -*-
from git import *
import time
import subprocess
import os

"""Find a this day's the last commit.
    
Args:
    item - git commit's data

Returns:
    this day in last commit data

"""
def find_last_commit_time(items):
    commit_day = []
    commit_time = []
    uniq_commit_days = {}
    uniq_commit_data = {}
    for item in items:
        commit_time = str(time.strftime("%Y%m%d%H%M%S", time.gmtime(item.committed_date)))
        if((commit_time[0:8] in uniq_commit_days)== False):
            uniq_commit_days[commit_time[0:8]] = commit_time
        
        # If this is a latest date, then update to date.
        if(uniq_commit_days[commit_time[0:8]] <= commit_time):
            uniq_commit_days[commit_time[0:8]] = commit_time
            uniq_commit_data[uniq_commit_days[commit_time[0:8]]] = item.hexsha

    return uniq_commit_data

"""Checkout and Copy
    
    Args:
    uniq_commit_data
    repo
    git
    your_repository
    cp_dest_path
    
    """
def checkout_and_copy(uniq_commit_data, repo, git, your_repository, cp_dest_path, repository_name):

    
    for i in uniq_commit_data:
        key = i
        time.sleep(1)
        print("Process to ... " + str(i))
        git.checkout(uniq_commit_data.get(key), b=str(i))
        cp_cmd = "cp -r " + your_repository + " " + cp_dest_path
        subprocess.check_output(cp_cmd.strip().split(" "))
        print("Success Copy : " + str(cp_cmd))
        time.sleep(1)
        mkdir_cmd = "mkdir " + cp_dest_path + "/" + str(i)
        subprocess.check_output(mkdir_cmd.strip().split(" "))
        print("Success mkdir : " + str(mkdir_cmd))
        time.sleep(1)
        mv_cmd = "mv " + cp_dest_path + "/" +  repository_name + " " + cp_dest_path + "/" + str(i)
        subprocess.check_output(mv_cmd.strip().split(" "))
        print("Success Move : " + str(mv_cmd))
        time.sleep(1)
        # Branch change a 'master'
        git.checkout('master')
        # Delete this process's branch
        repo.delete_head(str(i),'-D')
        print("Success create dir : " + str(i))

def main():

    your_repository = os.getcwd() + "" # Your Repository(absolute path) is here. -> Example : os.getcwd() + "/test/your_repository"
    cp_dest_path = os.getcwd() + "" # Copy destination path(absolute path) is here. -> Example : os.getcwd() + "/test/copy_dest_path"
    repository_name = "repositry name is here" # TODO -> This is a Tentative correspondence.

    repo = Repo(your_repository)
    uniq_commit_days = {}
    uniq_commit_data = find_last_commit_time(repo.iter_commits('master', max_count=100))
    git = repo.git


    print("Daily Git Collection Vehicle start...")

    checkout_and_copy(uniq_commit_data, repo, git, your_repository, cp_dest_path, repository_name)

    print("Daily Git Collection Vehicle end!!")


if __name__ == '__main__':
    main()
@@ -889,20 +889,16 @@
 py%0A    %0A
-    
 Args:%0A  
@@ -966,24 +966,44 @@
 p_dest_path%0A
+    repository_name%0A
     %0A    %22%22%22
@@ -2096,16 +2096,73 @@
 main():%0A
+    # When using a Absolute path, delete to %22os.getcwd()%22
 %0A    you
@@ -2191,20 +2191,22 @@
 wd() + %22
+/
 %22 
-# 
++ %22
 Your Rep
@@ -2213,23 +2213,23 @@
 ository(
-absolut
+Relativ
 e path) 
@@ -2228,35 +2228,35 @@
 e path) is here.
- -%3E
+%22 #
  Example : os.ge
@@ -2246,39 +2246,26 @@
 # Example : 
-os.getcwd() + 
 %22
+.
 /test/your_r
@@ -2309,20 +2309,22 @@
 wd() + %22
+/
 %22 
-# 
++ %22
 Copy des
@@ -2341,15 +2341,15 @@
 ath(
-absolut
+Relativ
 e pa
@@ -2360,19 +2360,19 @@
 is here.
- -%3E
+%22 #
  Example
@@ -2374,31 +2374,18 @@
 ample : 
-os.getcwd() + 
 %22
+.
 /test/co

update a comment.