Name:Qiu Wenqiang
The main functionality that Gitlet supports is:
-
Saving the contents of entire directories of files. In Gitlet, this is called committing, and the saved contents themselves are called commits.
-
Restoring a version of one or more files or entire commits. In Gitlet, this is called checking out those files or that commit.
-
Viewing the history of your backups. In Gitlet, you view this history in something called the log.
-
Maintaining related sequences of commits, called branches.
-
Merging changes made in one branch into another.
This is the main logic of Gitlet will live. This file will handle all the actual Gitlet commands by reading/writing from/to the correct file, setting up persistence and additional error checking.
It will also be responsible for setting up all persistence within Gitlet.
This includes creating the .gitlet folder as well as folder and file where
we will store Commit objects and other metadata.
-
public static fs::path CWD = fs::current_path()The Current Working Directory. -
public static fs::path GITLET_DIR = CWD / ".gitlet"The hidden.gitletdirectory. This is where all the state ofRepositorywill be stored. -
public static fs::path HEAD = GITLET_DIR / "HEAD"The file contains persistence data of the front of current branch of Gitlet. -
public static fs::path INDEX = GITLET_DIR / "INDEX"The file contains persistence data of the staging area. -
public static fs::path COMMIT_DIR = GITLET_DIR / "commits"The folder holds the file of persistence data of Commit objects. -
public static fs::path BLOB_DIR = GITLET_DIR / "blobs"The folder holds the file of persistence data of Blob objects. -
public static fs::path TREE = GITLET_DIR / "TREE"The file containing the persistence data of commit tree. -
public static fs::path CONFIG = GITLET_DIR / "CONFIG"Configure file where info of remote repos are saved -
private static Repository::Tree tree = Tree();An instantiated object of class Tree to manage Commit objects. -
private static StagingArea Repository::stagingArea = StagingArea();An instantiated object of class StagingArea to manage added info and removed info. -
private static RemoteRepo Repository::repo = RemoteRepo();An instantiated object of class RemoteRepo containing info of the added remote repo.
This is a place containing staged files(for addition or removal) after command add
and command rm.
-
private unordered_map<string, string> m_addition;For the files to be added. -
private unordered_map<string, string> m_removal;For the files to be removed.
The saved contents of entire directories of files. Commit is a combination of
log messages, other metadata (date, message...), a reference of the current tree,
and references to parent commits.
-
private Date m_timestampdate when Commit object is constructed. -
private string m_message;annotation of the current commit. -
private string m_parent_ref;a parent reference -
private string m_second_parent_ref;second parent reference(for merges) -
private string m_commit_ref;The reference of current Commit object, an SHA-1 value produced by using the metadata(combination of only timestamp and log message), the mapping of names to references, and the parent reference. -
private unordered_map<string, string> m_map;a map of file names to references to their class Blob objects.
The Blob class is where contents of files are saved. A single file might
correspond to multiple blobs: each being tracked in a different commit.
-
private string m_filename;The file name in the working directory. -
private string m_content;The saved content of a file. -
private string m_blob_ref;The Reference of a class Blob object and filename of the file in local repo.
Directory structures mapping names to reference to blob and other trees(subdirectories).
-
private string m_head;The reference to the front of the active branch. -
private string m_active_branch;The active branch name. -
private unordered_map<string, string> m_branches;All branches and the references to their own front commit(branchName : headRef)
This header file contains helpful utility methods to read/write objects or string contents from/to files, as well as reporting errors when they occur.
None
This class will create a unique string of SHA-1 value according to the input strings.
This class will record info of time specified and provides specified format of string of time.
-
public static void init()Initialize all directory structure of .gitlet; Initialize all static variables in class Repository and complete their data persistence; Complete first commit with message "initial commit" and add it to the tree; -
public static void add(string fileName)Add the file name and its reference to the staging area if it is not tracked in the head of current branch. -
public static void commit(string message)Create a new commit. The commit will get the same snapshot from the head of current branch which will become the parent of the commit. Add all staged files to the commit and delete all removed files from the commit if their exist. Add the commit to the current branch and the commit become the new head of current branch. -
public static void checkout(string doubleDash, string fileName)checkout -- fileName Check the existence of the file in the head of current branch. Read the file from the head commit and write the file to the working directory. -
public static void checkout(string commitRef, string doubleDash, string fileName)checkout commitID -- fileName Check the existence of the target commit. Check the existence of the target file in that commit. Read the file from the commit and write it to the working directory. -
public static void checkout(string branchName)checkout branchName Check the existence of the branch. Write all files in the head of the branch to the working directory, remove all files that are untracked by the head. -
public static void rm(string fileName)check if the file is staged for addition or tracked in the current head commit; untrack the file from staging area; remove file in current directory if it has been tracked in the current head -
public static void log()Starting at the current head commit, display information about each commit backwards along the commit tree until the initial commit, following the first parent commit links, ignoring any second parents found in merge commits. -
public static void globalLog()Like log, except displays information about all commits ever made. -
public static void find(string commitMessage)Prints out the reference ids of all commits that have the given commit message, one per line. -
public static void status()displays what branches currently exist, and marks the current branch with a *; displays all staged files; displays all removed files; displays files modified but not staged; displays files untracked; -
public static void branch(string branchName)create a new branch if it does not exist. -
public static void rmBranch(string branchName)remove branch specified if it exists or it isn't the current active branch. -
public static void reset(string commitRef)read commit object according to the commit reference if it exists; set the commit as new head of current branch; check out the current branch -
private static string getTheSplitPoint(Commit currentHead, Commit givenHead)get set of all parents of current head; iterate parents of given head backward, starting from given head; get the split point commit as long as the parent of given head occurs in the parents set above first time. -
public static void merge(string branchName)error check; get the split point of current branch and given branch;
any files that have been modified in the given branch since the split point, but not modified in the current branch since the split point should be changed to their versions in the given branch;
any files that have been modified in the current branch but not in the given branch since the split point should stay as they are;
any files that have been modified in both the current and given branch in the same way are left unchanged by the merge;
any files that were not present at the split point and are present only in the current branch should remain as they are;
any files that were not present at the split point and are present only in the given branch should be checked out and staged;
any files present at the split point, unmodified in the current branch, and absent in the given branch should be removed (and untracked);
any files present at the split point, unmodified in the given branch, and absent in the current branch should remain absent;
any files modified in different ways in the current and given branches are in conflict;
set current head as first parent, given head as second parent;
add new merged commit to the current branch.
-
std::string sha1(std::initializer_list<std::string> lst)Return a unique string value of SHA-1 according to the input strings. -
std::string readContent(std::string& file_name)check if the file is a regular file read its content to a string return the string -
std::string readContent(fs::path& file_name)same as above -
void writeContents(std::string& file_name, std::initializer_list<std::string> lst)Write all contents in lst to the file, creating or overwriting it as needed. -
void writeContents(fs::path& file_name, std::initializer_list<std::string> lst)same as above -
void create_file(const fs::path& p) -
template <typename T> void readObject(const std::string& file_name, T& object)Serialize an object and write it into target file. The function serialization is supposed by the Boost Libraries. -
template <typename T> void writeObject(const std::string& file_name, const T& object)Read data from target file and deserialize the data to the target object. The function deserialization is supported by the Boost Libraries. -
std::list<std::string> plainFilenamesIn(const fs::path& dir_name)Returns a list of the names of all plain files in the target directory, in lexicographic order. Returns null if DIR does not denote a directory. -
std::fstream join(const std::string& first, std::initializer_list<std::string> lst)Return the concatenation of FIRST and OTHERS into a file stream. -
void message(const std::string& msg, std::initializer_list<std::string> lst)Print a message composed from MSG and LST, followed by a newline.
CWD/
- .gitlet/ -- top level folder for all persistence data
- blobs/ -- folder for persistence data of Blob objects
- blob1 -- a single Blob object stored to a file
- blob2
- ...
- blobN
- commits/ -- folder for persistence data of Commit objects
- subdiretory1/ -- folder name is first six characters of SHA-1 ref of commit object
- commit1 -- file name is the rest charaters of SHA-1 ref of the commit object
- subdiretory2/
- commit2
...
- subdiretoryN/
- commitN
- HEAD -- the front of current branch of Gitlet
- INDEX -- file containing persistence data of Staging Area
- TREE -- file containing persistence data of commit tree
- config - file recording name and location of remote repo
The Repository class will set up all persistence. It will:
- Create
.gitletfolder if it doesn't exist. - Create
blobsfolder if it doesn't exist. - Create
commitsfolder if it doesn't exist. - Create
HEADfile if it doesn't exist. - Create
INDEXfile if it doesn't exist.
The Blob class will handle the serialization of Blob objects.
-
public void readFromOriginalFile();Read the contents of the file in CWD usingUtils.readContentAsString()method. -
public void writeToOriginalFile();Writethis.contentto the file in CWD named.gitlet/blobs/this.fileID -
public void readFromBlobFile();Read from the file named.gitlet/blobs/this.fileIDand convert it intoBlobobject usingUtils.readObject();method. -
public void writeToBlobFile();Write an entireBlobobject into the file named.gitlet/blobs/this.fileIDusingUtils.writeObject()method. (Actually, blob file is not allowed to be modified, so the calling of the function is just only one time.)
The Commit class will handle the serialization of Commit objects.
-
public void readFromFile();Read from the filejoin(COMMIT_DIR, fileID)and convert the data intoCommitobject usingUtils.readObject()method. -
public void writeToFile();Writethisto the filejoin(COMMIT_DIR, fileID)usingUtils.writeObject()method.
The StagingArea class will handle the serialization of StagingArea objects.
-
public void readFromFile();Read from the file.gitlet/INDEXand convert the data intoStagingAreaobject usingUtils.readObject()method. -
public void writeToFile();Writethisto the file.gitlet/INDEXusingUtils.writeObject()method.
-
public void readFromFile()Check out the existence of fileRepository.INDEX, if not, return; Read object from fileRepository.INDEXbyUtils.h::readObject()method and update member variablesadditionandremovalof current object. -
public void writeToFile()Write current object into fileRepository.INDEXcreated if not exist byUtils.h::writeObject()method.