This article is merely a primer; P

I am working in Shopee. If you don’t like to work overtime, please consider it

Refuse 996, then come to Shopee, treatment work life balance two no: www.v2ex.com/t/672561#re…

Naive Git

International Labor Day, also known as “May Day”, “International Workers’ Day” (International Workers’ Day or May Day), is a national holiday in more than 80 countries in the world. It’s on May 1 every year. It is a holiday shared by working people all over the world.

In October 1884, the United States and Canada’s eight international and national worker groups, held a rally in Chicago, the United States, decided to hold a general strike on May 1, 1886, forcing capitalists to implement the eight-hour working day system. The day finally came. On May 1, 1886, the 350 thousand workers shutdown of more than 20 thousand enterprises in the United States took to the streets, held a huge demonstration, all kinds of color of skin, the workers of each kind of work carried out a general strike together. In Chicago alone, 45,000 workers took to the streets. As a result, major industries of the United States were paralyzed, trains were frozen, stores were silent, and warehouses were closed and sealed.

In 1866, the First International Geneva Conference put forward the slogan of the eight-hour work day. [4] May 1, 1886, Chicago, the United States as the center, held in the United States about 350,000 people to participate in the large-scale strike and demonstrations, demonstrators asked to improve working conditions, the implementation of the eight-hour working system. On May 3, 1886, the Chicago government sent the police to suppress, shot and killed two people, and the situation expanded. On May 4, the striking workers held a protest in Haymarket Square. As unidentified people threw bombs at the police, the police opened fire, successively four workers and seven policemen died. It was called the Haymarket Riot or The Haymarket Massacre. In subsequent sentencing, eight anarchists were charged with murder, four were hanged, and one committed suicide in prison.

To commemorate this great labor movement and the subsequent sentencing of the protest, workers’ protests were held around the world. These activities became the forerunner of international Labor Day.

In July 1889, the organization of Engels held the second international founding conference announced that the annual May 1 as the international Labor Day.

Victory was finally achieved after a bitter and bloody struggle. In commemoration of the workers’ movement, July 14, 1889, the socialist Congress convened by the national Marxist, opened in Paris, France. On the general assembly, the representatives agreed: May 1 as the common festival of the international proletariat. The decision received an immediate positive response from workers around the world. On May 1, 1890, the working class in Europe and the United States took the lead in taking to the streets, holding a grand demonstration and assembly to fight for legal rights and interests. From then on, every time this day the world’s working people to assembly, parade, to celebrate, and the public holiday.

May 1st International Labor Day, write a simple Git together!

How does Git work?

Git is a distributed version-control system for tracking changes in source code during software development.

If you don’t know how git works, you can use three axes git add. git commit; Git push: Keep track of file changes, use commit as a tag, and synchronize with remote servers.

Tracking file changes

If you’re developing Git, after you initialize a repository folder, you need to keep track of all the files you need to track in order to record possible changes later. The easiest way to do this is to make a copy of everything.

Has the file changed? Compare file hashing.

Commit for marking

The current repository state is stored as a COMMIT. You can restore to any state with a Commit. Git tag is essentially just a tag (alias) given to the commit. Git Branch is the same.

To restore to a commit is to restore the repository state it represents, the entire contents of the file and the current commit to that state.

Synchronize with the remote server

Git calls itself A distributed version management system because if A, B, and C work together, each of them theoretically has A version of the server and can develop it independently to resolve conflicts.

How does Git work?

Git does not use a database to store its contents in a.git folder.

What’s in it?

. | -- -- the HEAD/point/branch, the tag (ref: refs/heads/devbranch) |-- index |-- objects | |-- 05 | | `-- 76fac355dd17e39fd2671b010e36299f713b4d | |-- 0c | | `-- 819c497e4eca8e08422e61adec781cc91d125d | |-- fe | | `-- 897108953cc224f417551031beacc396b11fb0 | |-- fe | | `-- 897108953 cc224f417551031beacc396b11fb0 | | - info | ` - refs | - heads / / heads of each branch | ` - master / / this branch of the latest commit id | '-- devBranch // checkout -b branch will generate branch' -- tags' -- v0.1Copy the code

I want you to reunite

Let me expand on this:

  • HEAD: refers to branch or tag, marking which branch or tag is currently on.
  • index: TODO
  • objects: Records the contents of files. The names of each folder are the first two bits of the SHA1 value of the object, and the names of files in the folder are the last 18 bits of the SHA1 value. Sha1 computes the hash value of the current content as the file name of object. The resulting hash value is a string of hexadecimal digits (length 40).
  • refs
    • heads: headsThe ones in are the branchesHEADWhich one to point tocommit id; What is the latest commit for each branchgit checkout branchI can switch to the right place
    • tagsIn the same way, this folder is filled with tags

To create a branch, simply create a new branch name file in the refs/heads folder and store the current COMMIT ID.

When creating a COMMIT, find the current branch or tag in the HEAD file and modify it.

A little hard to understand? We give an example of git, in a folder by default git init, add a file and to commit the information, the commit with id 017 aa3d7851e8bbff78a697566b5f827b183483c:

$ cat .git/HEAD
ref: refs/heads/master
$ cat .git/refs/heads/master
017aa3d7851e8bbff78a697566b5f827b183483c
Copy the code

As shown above, HEAD points to the master, and the master’s commit ID is the same ID that was just committed.

Now that the store reads are done, how is the commit organized?

Store the current repository state as a COMMIT. You can restore to any state with a Commit. Git tag is essentially just a tag (alias) given to the commit. Git Branch is the same.

To restore to a commit is to restore the repository state it represents, the entire contents of the file and the current commit to that state.

A folder can be nested in a repository state, but a folder needs to be nested in a hierarchy that also stores the contents of a file.

We can introduce the following concepts:

  • Git init = “./ “, “./ “, “./ “;

  • Blob: a file that can be contained in a Tree;

The relationship is shown as follows:

A tree can have a bloB or a tree, so we use a union; Parent and Next are used as linked lists and managed as folders.

struct tree_entry_list {
    struct tree_entry_list *next;
    union {
        struct tree *tree;
        struct blob *blob;
    } item;
    struct tree_entry_list *parent;
};

struct tree {
    struct tree_entry_list *entries;
};
Copy the code

A COMMIT, like a tree, is a hierarchical single linked list, but only

struct commit {
    struct commit *parents;
    struct tree *tree;

    char *commit_id[10];
    char *author;
    char *committer;
    char *changelog;
};
Copy the code

A picture is worth a thousand words. Here’s a picture:

Cloud wind game resource warehouse and upgrade release

Yunfeng has done a game resource warehouse management with reference to the principle of Git. Let me talk about the difference between it and Git. I think his article is quite convoluted, and it is difficult for people without background knowledge to understand.

background

An important feature of our engine is that it is developed on a PC and debugged on a mobile device. We need to synchronize resources to the device frequently

When the program runs in C/S structure, an empty mirror warehouse is first established on the mobile device to synchronize the RESOURCE warehouse on the PC. The running process looks like this:

First, when the client starts up, ask the server for a hash of the root index and set the root on the local image.

When a client requests a file path, it searches for the corresponding directory index file from the root level to level. If a hash object is available locally, use it directly. Otherwise, request the server until you finally get the target file. The API is designed to open a resource path that returns either the final file or a hash, indicating that the hash object is still missing. In this way, the object can be requested through the network module; Once you get the object, you don’t care what the object is, you simply write it to the mirror repository, and then repeat the previous process, requesting the incomplete path again, and finally opening the desired resource file.

The scene is: Client <- his game server, one-way synchronization;

The way he does it is, the client’s repository is a database of key-values, the key is the hash of the file, the value is the content of the file;

During the synchronization, the hash file will be downloaded to the database.

If the client uses the resource and finds that the file is missing, it hashes the file to the server to pull it down.

In other words, because there is no need to manage the local version and synchronize upstream, there is no need to record the full version status locally

Git Git

The scenarios are as follows: Client <-> gitHub, bidirectional synchronization;

Git requires a local organization commit, switching to a version that is locally available but not on the server (that is, offline), and synchronizing changes upstream.

Final advice

If reading this article makes you want to try it, please don’t write in C, please don’t write in C, please don’t write in C.

I have written several large projects from scratch, and every time I felt it was too uncomfortable to write projects with C. This time, when I wrote git Commit, I found that I had to read and write files and parse the contents, and I sighed in my heart:

It’s too hard, not to write this, but C is too hard to use.

I have to iterate through the files, get the hash of the tree based on the directory, update the tree, commit the tree, save the bloB sequence in the file, read it out, and then organize the linked list.