Last time we had created cmd_init(args) function. We add more functionality to cmd_init(args).

This was our code till last time.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import sys
import argparse

argparser = argparse.ArgumentParser(description="Another Content Tracker")
argsubparsers = argparser.add_subparsers(title="Commands", dest="command")
argsubparsers.required = True
argsp = argsubparsers.add_parser("init", help="Initialize a new, empty repository.")
argsp.add_argument("path", metavar="directory", nargs="?", default=".", help="Where to create the repository.")

def main(argv=sys.argv[1:]):
    args = argparser.parse_args(argv)
    if args.command == "init":
        cmd_init(args)
    

def cmd_init(args):
    print("Hello from init")
    

We had added an argument to your sub parser called path. If the value of path is not specified it would be ‘.’ current directory or it would whatever specified. This would help us

A repository in git is consist to two things a work tree where we have our files are there for version control and the git directory i.e. .git directory where Git stores its own data. In regular practice our work tree is a regular directory and git directory is child of that directory. Git supports all cases also our program would work in this case only.

For any command we run related to git there would be in git repo. So we create an object which would help us manage things. We would be working with lot of files we need to import another python module os.

So let’s create a class call GitRepository who’s object would give us worktree directory and .git directory. Later when we would create other commands into our code we can use this to check whether we are executing commands on a valid repository or not.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class GitRepository(object):
    """A git repository"""
    
    worktree = None
    gitdir = None
    conf = None
    
    def __init__(self, path):
        self.worktree = path
        self.gitdir = os.path.join(path, ".git")
    

We would create another function for our init command called `repo_create(path)` which would take our path argument from init.

1
2
3
4
5
6
7
8
def cmd_init(args):
	repo_create(args.path)
    
def repo_create(path):
	"""Create a new repository at path."""
	repo = GitRepository(path)
	print(repo.worktree, repo.gitdir)
    

Now when you ./wag init you would get something like . ./.git and if you do ./wag init test and you would get test test/.git. So this would help us in creating directories.

As we have worktree path and git directory path we can check if worktree exist, if it doesn’t then we can create a directory. This would be used for creating things with ./wag init test

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def repo_create(path):
	"""Create a new repository at path."""
	repo = GitRepository(path)
	if os.path.exists(repo.worktree): # We check whether there exists a path of worktree
		if not os.path.isdir(repo.worktree):# if path exists its a directory or not
			raise Exception("%s is not a directory!" % path)
		if os.listdir(repo.worktree):# if path exsits its empty or not
			raise Exception("%s is not empty!" % path)
	else:
		os.makedirs(repo.worktree) # Create directory as nothing exist with same path

In wag, we would only allow to init git if already existing repository is empty otherwise we won’t. Now we need to create .git directory and other directories inside it.