Version Control System. Stage 3/4

Log & commit

Report a typo

Description

In this stage, you will implement two key commands. commit will allow a user to save file changes; log will allow viewing of the commit history. The goal is to simulate the basic functionality of a VCS like Git, where file changes are saved as "commits" and a history of these commits can be viewed.

The commit command allows the user to save changes made to a file. Each commit needs to have a unique commit ID. This ID can be generated using a cryptographic hash (e.g., SHA-1) based on the file contents or timestamp. The commit should only be created if there are changes in the files since the last commit. This can be determined by comparing the hash of the current state with the hash of the last committed version.

If a file/files has changed, the system will:

  • Create a new directory inside vcs/commits/ named after the unique commit ID;

  • Copy the current version of all tracked files into this new directory;

  • Log the commit ID and the commit message in a vcs/log.txt file;

If none of the files have changed since the last commit, the system will display a message like "Nothing to commit." and no new commit will be created.

The log command allows the user to view the history of commits. The commit history will be stored in the vcs/log.txt file, where each entry contains:

  • The commit ID;

  • The commit message;

When the log command is executed, the system will display the commit history in reverse order (showing the most recent commit first).

Git may seem quite complicated. If you want to learn more, watch the video explanation by GitLab.

Objectives

Implement the following commands:

  1. commit:

    • This command must be passed to the program along with a commit message (e.g., commit "Initial commit"). Else, print an error message;

    • Save all changes made to the tracked files. Each commit must be assigned a unique ID, which can be generated using a cryptographic hash (e.g., SHA-1) based on the current timestamp or file contents;

    • If there are no changes since the last commit, display a message like "Nothing to commit";

    • You don't need to optimize the storage of changes; simply copy the current version of the tracked files to the commit folder every time a commit is made.

  2. log:

    • This command should display all the commits in reverse order (most recent commit first).

    • The commit history is stored in vcs/log.txt, where each entry includes the commit ID and the commit message.

Do not create file1.txt, file2.txt and untracked_file.txt programmatically. These are examples of the files that a user of your version control system will work with.

Examples

The greater-than symbol followed by a space (> ) represents the user input. Note that it's not part of the input.

Example 1: the log argument

No commits yet.

This is the directory tree. Don't output it.

.
├── vcs
│   ├── commits
│   ├── config.txt
│   ├── index.txt
│   └── log.txt
├── file1.txt
├── file2.txt
└── untracked_file.txt

Example 2: the commit "Added several lines of code to the file1.txt" argument

Changes are committed.

This is the directory tree. Don't output it.

.
├── vcs
│   ├── commits
│   │   └── 0b4f05fcd3e1dcc47f58fed4bb189196f99da89a
│   │       ├── file1.txt
│   │       └── file2.txt
│   ├── config.txt
│   ├── index.txt
│   └── log.txt
├── file1.txt
├── file2.txt
└── untracked_file.txt

Example 3: the log argument

commit 0b4f05fcd3e1dcc47f58fed4bb189196f99da89a
Author: John
Added several lines of code to the file1.txt

Example 4: the commit "Changed several lines of code in the file2.txt" argument

Changes are committed.

Example 5: the log argument

commit 2853da19f31cfc086cd5c40915253cb28d5eb01c
Author: John
Changed several lines of code in the file2.txt

commit 0b4f05fcd3e1dcc47f58fed4bb189196f99da89a
Author: John
Added several lines of code to the file1.txt

This is the directory tree. Don't output it.

.
├── vcs
│   ├── commits
│   │   ├── 2853da19f31cfc086cd5c40915253cb28d5eb01c
│   │   │   ├── file1.txt
│   │   │   └── file2.txt
│   │   └── 0b4f05fcd3e1dcc47f58fed4bb189196f99da89a
│   │       ├── file1.txt
│   │       └── file2.txt
│   ├── config.txt
│   ├── index.txt
│   └── log.txt
├── file1.txt
├── file2.txt
└── untracked_file.txt

Example 6: the commit "Files were not changed" argument

Nothing to commit.

Example 7: the commit argument

Message was not passed.
Write a program
IDE integration
Checking the IDE status
___

Create a free account to access the full topic