CS61B Gitlet Design
Gitlet Design Document
Name: Kai Huang
Classes and Data Structures
Commit
Represents a commit in gitlet. A series of commits can be seen as a linked list.
Fields
private String message
The message of this Commit.private long timestamp
Time at which a commit was created. Assigned by the constructor.private String parent
The parent commit of a commit object.private String secondParent
The second parent if merge.public TreeMap<String, String> reference
References to blobs.
Main
This is the entry point to our program. It takes in arguments from the command line and based on the command (the first element of the args
array) calls the corresponding command in Repository
which will actually execute the logic of the command. It also validates the arguments based on the command to ensure that enough and appropriate arguments were passed in.
Fields
This class has no fields and hence no associated state: it simply validates arguments and defers the execution to the Repository
class.
Repository
Fields
Local
public static File CWD = new File(System.getProperty("user.dir"))
Current working directorypublic static final File currentCWD = new File(System.getProperty("user.dir"))
A CWD that never change.public static File GITLET_DIR = join(CWD, ".gitlet")
.gitlet directory.public static File Stage_DIR = join(GITLET_DIR, "stage", "addition")
The stage for addition directory.public static File Removal_DIR = join(GITLET_DIR, "stage", "removal")
The stage for removal directory.public static File Commit_DIR = join(GITLET_DIR, "commit")
The commit directory.public static File Blob_DIR = join(GITLET_DIR, "blob")
The blob directory.public static File Head_DIR = join(GITLET_DIR, "refs", "heads")
The head directory.public static File Remote_DIR = join(GITLET_DIR, "refs", "remote")
The remote directory.public static File Config_DIR = join(GITLET_DIR, "config")
Record remote repository information: name and url.public static File HEAD = join(GITLET_DIR, "HEAD")
Store branch name in HEAD.public static HashSet<String> gitIgnore = new HashSet<>(Set.of(".gitignore", "delete_gitletDir.sh", "Makefile", "gitlet-design.md", "pom.xml"))
The gitIgnore file.
Remote
public static File remoteCWD
public static File remoteGITLET_DIR
If need,remoteGITLET_DIR
will benew File(readContentsAsString(join(Config_DIR, remoteName)))
.public static File remoteCommit_DIR
public static File remoteBlob_DIR
public static File remoteHead_DIR
public static final String remoteSeparator = "#"
Replace “/“ with “#” because filename can’t contain “/“, e.g. replace “R1/master” with “R1#master”.
Utils
This class contains helpful utility methods to read/write objects or String
contents from/to files, as well as reporting errors when they occur.
Fields
Only some private
fields to aid in the magic.
HelpMethod
The help methods to help achieve functions in Repository
class.
Fields
No fields.
Algorithms
init
- Check if
GITLET_DIR
exists. If exist, error. - Local directories and file make.
- Make initial commit.
new
instance ,serialize and sha1.- Write to
Commit_DIR
, filename: sha1-hash, content: serialization. - Write “master” in
HEAD
, write sha1-hash inCommit_DIR/master
add
…
Persistence
The directory structure looks like this:
1 | CWD <==== The current working directory |
The Repository
will set up all persistence.
Other
Super helpful commands in real Git
git hash-object <File>
//show the hash of the filegit cat-file -p <sha-1 hash>
//deserializegit ls-files -s
//Lists the status of all the files in the current Git repository and displays the metadata for those files. Specifically, it lists the files in the staging area and their related information.
Key sentences from a video
git add
Create a blob containing the content of the file.
git commit
- Write the commit’s content to the working copy.
- Write the commit’s content to the Index.
- Point HEAD at the thing that was checked out.
A commit is a set of changes.
git merge
- Generate the diff that combines the changes made by the receiver and giver.
- Apply the diff to the working copy.
- Apply the diff to the Index.
- Commit the updated Index.
- Point HEAD at the new commit.
when conflict:
- Generate the diff that combines the changes made by the receiver and giver.
- Apply the diff to the working copy.
- Apply the diff to the Index.
- The user resolves the conflicts in the working copy.
- The user resolves the conflicts in the Index.
- The user commits the merge.
git fetch
- Find the tip commit on the branch being fetched from the remote repository.
- Copy to the fetching repository the tip commit and the objects it depends on.
- Point the ref for the remote branch at the fetched commit.
- Point FETCH_HEAD at the fetched commit.
git pull
git fetch + git merge FETCH_HEAD
git clone
- Create the directory for the new repository.
- Move into the clone’s directory.
- Initialize the clone’s directory as a Git repository.
- Check out the branch that was checked out on the repository being cloned.
- Pull the branch that was checked out on the repository being cloned.
git clone [ ] [ ] --bare
A bare repository: You push to it and you pull from it but you never commit to it directly.
git push
- Copy the pushed branch’s tip commit and the objects it depends on to the remote directory.
- Point the pushed branch on the remote to the tip commit.
To be improved
- Use ppt to draw .gitlet file structure, merge cases, tesing cases.