#!/usr/bin/env python
#coding: utf-8

import sys

from util         import core
from util         import adjust
from util.git     import *
from argparse     import ArgumentParser, SUPPRESS

# usage
#  git ref b81fe395c0bf28c4be8                         -> ハッシュ[b81fe395c0bf28c4be8]の[hash値]を出力
#  git ref tag                                         -> タグ[tag]の[hash値]を出力
#  git ref -t b81fe395c0bf28c4be8                      -> ハッシュ[b81fe395c0bf28c4be8]の[type値]を出力
#  git ref b81fe395c0bf28c4be8 --file git-todo.py      -> ハッシュ[b81fe395c0bf28c4be8]の[git-todo.py]の[hash値]を出力
#  git ref b81fe395c0bf28c4be8 --cat-file git-todo.py  -> ハッシュ[b81fe395c0bf28c4be8]の[git-todo.py]の[中身]を出力
#  git ref --ls HEAD                                   -> コミット[HEAD]の[ls-tree -r]を表示
#  git ref --detail HEAD                               -> コミット[HEAD]の[git show]を表示

parser = ArgumentParser(prog="git ref",
            description="This script can show reference hash or files easyly.")

parser.add_argument("reference", action="store",
            help="Please set hash of reference.\
                  If you not set other options, \
                                    script show full hash value.")

parser.add_argument("-l", "--ls", action="store_true", default=SUPPRESS,
            help="Show all files with hash in commit.")

parser.add_argument("-t", "--type", action="store_true", default=SUPPRESS,
            help="Show type of hash.")

parser.add_argument("-f", "--file", action="store", default=SUPPRESS,
            help="Show file object hash in commit.")

parser.add_argument("-c", "--cat-file", action="store",
            dest="file", default=SUPPRESS,
            help="Show file contents.")

class ArgumentNamespace:

    def __setattr__(self, key, value):
        self.__dict__[key] = value

    def __repr__(self):
        return "ArgumentNamespace(%s)" % ", ".join(
                                [k+"='%s'"%v for k,v in vars(self).iteritems()])

    def is_only_ref(self):
        """Return True  if there is only reference argument"""

        if len(vars(self).keys()) == 1 and self.reference:
            return True

def check_ref(reference):
    """This function check reference whether it's valid ref, \
                              If it's valid ref, return reference"""

    try:
        # Run git rev-parse --verify [ref]
        verified = git("rev-parse", "--verify", reference)

    except:
        # If it is invalid, Call error
        parser.error("invalid reference.")

    return reference

def main(args):

    # User set reference only
    if args.is_only_ref():

        print git("rev-parse", check_ref(args.reference))
        return 0

    if args.ls:

        print git("ls-tree", "-r", check_ref(args.reference))
        return 0



            

if __name__ == "__main__":

    if len(sys.argv) == 1:
        parser.parse_args(["-h"])

    #args = parser.parse_args()
    args = parser.parse_args(namespace=ArgumentNamespace())
    print args

    sys.exit(main(args))


@@ -1233,34 +1233,16 @@
 e_true%22,
- default=SUPPRESS,
 %0A       
@@ -1347,34 +1347,16 @@
 e_true%22,
- default=SUPPRESS,
 %0A       
@@ -1439,34 +1439,16 @@
 %22store%22,
- default=SUPPRESS,
 %0A       
@@ -1553,50 +1553,20 @@
 re%22,
-%0A            dest=%22file%22, default=SUPPRESS
+ dest=%22file%22
 ,%0A  
@@ -2129,80 +2129,23 @@
 ef, 
-%5C%0A                              If it's valid ref, return referenc
+Return Tru
 e%22%22%22%0A
-%0A
     
@@ -2358,24 +2358,19 @@
  return 
-referenc
+Tru
 e%0A%0Adef m
@@ -2377,24 +2377,69 @@
 ain(args):%0A%0A
+    ref = args.reference%0A    check_ref(ref)%0A%0A
     # User s
@@ -2519,33 +2519,11 @@
 e%22, 
-check_ref(args.reference)
+ref
 )%0A  
@@ -2538,16 +2538,37 @@
 turn 0%0A%0A
+    # User set --ls %0A
     if a
@@ -2615,67 +2615,130 @@
 r%22, 
-check_ref(args.reference))%0A        return 0%0A%0A%0A%0A            
+ref)%0A        return 0%0A%0A    # User set --type%0A    if args.type:%0A%0A        print git(%22cat-file%22, %22-t%22, ref)%0A        return 0%0A
 %0A%0Aif

Remove default attribute of add_argument