def cmd_ls_tree(args): repo = rp.repo_find() obj = object.object_read( repo, object.object_find(repo, args.object, fmt=b'tree')) for item in obj.items: print("{0} {1} {2}\t{3}".format( "0" * (6 - len(item.mode)) + item.mode.decode("ascii"), object.object_read(repo, item.sha).fmt.decode("ascii"), item.sha, item.path.decode("ascii")))
def cmd_checkout(args): repo = rp.repo_find() obj = object.object_read(repo, object.object_find(repo, args.commit)) # If the object is a commit, grab its tree if obj.fmt == b'commit': obj = object.object_read(repo, obj.kvlm[b'tree'].decode("ascii")) # Verify that path is an empty directory if os.path.exists(args.path): if not os.path.isdir(args.path): raise Exception("Not a directory {0}!".format(args.path)) if os.listdir(args.path): raise Exception("Not empty {0}!".format(args.path)) else: os.makedirs(args.path) tree_checkout(repo, obj, os.path.realpath(args.path).encode())
def tree_checkout(repo, tree, path): for item in tree.items: obj = object.object_read(repo, item.sha) dest = os.path.join(path, item.path) if obj.fmt == b'tree': os.mkdir(dest) tree_checkout(repo, obj, dest) elif obj.fmt == b'blob': with open(dest, 'wb') as f: f.write(obj.blobdata)
def log_graphviz(repo, sha, seen): if sha in seen: return seen.add(sha) commit = object.object_read(repo, sha) assert (commit.fmt == b'commit') if not b'parent' in commit.kvlm.keys(): return parents = commit.kvlm[b'parent'] if type(parents) != list: parents = [parents] for p in parents: p = p.decode("ascii") print("c_{0} -> c_{1};".format(sha, p)) log_graphviz(repo, p, seen)
def cat_file(repo, obj, fmt=None): obj = object.object_read(repo, object.object_find(repo, obj, fmt=fmt)) sys.stdout.buffer.write(obj.serialize())