def command(argv): initialized() if len(argv) < 1: help() sys.exit(1) ref = argv[0] match = re.match("(\w+)(~[1-9][0-9]*){0,1}", ref) if match is None: help() sys.exit(1) groups = match.groups() ref = groups[0] if ref == "HEAD": head = HEAD.from_file() ref = head.ref if groups[1] is None: prev_count = 0 else: prev_count = int(groups[1][1:]) head = HEAD.from_file() if head.sha1 is None: print >> sys.stderr, "Branch does not have the first commit" sys.exit(1) checkout_branch = Branch.from_file(ref) checkout_commit = Commit.from_file(checkout_branch.sha1text) for i in xrange(prev_count): if checkout_commit.parent is None: print >> sys.stderr, "Last commit doesn't have a parent" sys.exit() checkout_commit = Commit.from_file(checkout_commit.parent) staging = Index() staging.load(os.path.join(MYGIT_ROOTDIR, 'index')) target_commit_index = Index.from_tree(2, checkout_commit.treesha1) non_overwritable_files = staging.list_nonoverwritable_files_by_index( target_commit_index) if non_overwritable_files: print >> sys.stderr, "original file changed, you shouldn't write onto it %s" % non_overwritable_files sys.exit() current_head = HEAD.from_file() current_commit = Commit.from_file(current_head.sha1) current_index = Index.from_tree(2, current_commit.treesha1) staging.overwrite_repo(current_index, target_commit_index) target_commit_index.save(os.path.join(MYGIT_ROOTDIR, 'index')) new_head = HEAD() if not prev_count: new_head.ref = ref new_head.sha1 = checkout_commit.sha1 else: new_head.sha1 = checkout_commit.sha1 new_head.save()
def command(argv): initialized() if len(argv) != 1: help() sys.exit(1) if len(argv) == 1: branch_name = argv[0] branch = Branch.from_file(branch_name) if branch.sha1text is None: print >> sys.stderr, "Branch does not have the first commit" sys.exit(1) sha1text = branch.sha1text else: head = HEAD.from_file() sha1text = head.sha1 commit = Commit.from_file(sha1text) while commit is not None: print "hash: %s\ntree: %s\nauthor: %s\nmessage: %s\n" % ( commit.sha1, commit.treesha1, commit.author, commit.message) if commit.parent is not None: commit = Commit.from_file(commit.parent) else: commit = None
def command(argv): initialized() if len(argv) > 0: help() sys.exit(1) filename = os.path.join(MYGIT_ROOTDIR, 'index') index = Index() index.load(filename) head = HEAD.from_file() if head.ref is None: commitsha1 = head.sha1 else: branch = Branch.from_file(head.ref) commitsha1 = branch.sha1text commit = Commit.from_file(commitsha1) commitIndex = index.from_tree(2, commit.treesha1) removed, changed, added = index.differences(commitIndex) print "Added files:" print "\n".join(added) + "\n" print "Changed files:" print "\n".join(changed) + "\n" print "Removed files:" print "\n".join(removed) + "\n" fs_removed, fs_changed, fs_added = index.filesystem_differences() print "Changes not staged for commit" print "Added files:" print "\n".join(fs_added) + "\n" print "Changed files:" print "\n".join(fs_changed) + "\n" print "Removed files:" print "\n".join(fs_removed) + "\n" return ((removed, changed, added), (fs_removed, fs_changed, fs_added))
def command(argv): initialized() if len(argv) < 2 or argv[0] != '-m': help() sys.exit(1) message = argv[1] filename = os.path.join(MYGIT_ROOTDIR, 'index') index = Index() index.load(filename) sha1 = index.write_tree() head = HEAD.from_file() if head.ref is None: print >> sys.stderr, "the head is detached!" sys.exit(1) branch = Branch.from_file(head.ref) # parent will be fixed when branch is implemented commit = Commit(sha1, "Oguz", message, branch.sha1text) commitsha1 = commit.save() branch.sha1text = commitsha1 branch.save() index.save(filename) print commitsha1
def command(argv): initialized() if len(argv) > 0: help() sys.exit(1) filename = os.path.join(MYGIT_ROOTDIR, 'index') index = Index() index.load(filename) head = HEAD.from_file() if head.ref is None: commitsha1 = head.sha1 else: branch = Branch.from_file(head.ref) commitsha1 = branch.sha1text commit = Commit.from_file(commitsha1) commit_index = index.from_tree(2, commit.treesha1) for new_file in commit_index.new_files(index): with open(new_file, "r") as f: for line in unified_diff([""], f.readlines(), fromfile=new_file, tofile=new_file): print line removed_files, changed_files = commit_index.changed_files() for changed_file in changed_files: index_entry = next( (i for i in commit_index.index_entries if i.name == changed_file), None) blob = Blob.from_saved_blob(index_entry.sha1) with open(changed_file, "r") as f: for line in unified_diff(blob.content.split(), f.readlines(), fromfile=changed_file, tofile=changed_file): print line for removed_file in removed_files: index_entry = next( (i for i in commit_index.index_entries if i.name == removed_file), None) blob = Blob.from_saved_blob(index_entry.sha1) for line in unified_diff(blob.content.split(), [""], fromfile=removed_file, tofile=removed_file): print line
def create_branch(branch_name): head = HEAD.from_file() sha1text = head.sha1 branch = Branch(branch_name, sha1text=sha1text) branch.save()
def list_branches(): branches = Branch.list_branches() head = HEAD.from_file() for b in branches: print b, print "*" if head.ref == b else ""