跳至主要内容

Git Abstraction

Git Abstraction

Git is widely used in current software development routine to do the version control of code. With the help of IDE like IntelliJ Idea, we can avoid manually inputing git command in command line, like git add, git commit, git push etc. But when some things goes wrong, it always need command line operations.

Today, we are not going to introduce git command, but to understand how git works and what abstraction it have. As soon as we make it clear, we can understand many restrictions of git usage and many command in depth. So, when we met a new problem, we can infer a new solution from what we known, rather than just relying on Google.

Abstraction

As Torvalds explains, Git is first designed as a file system, so it has different concepts as old VCS system.

In many ways you can just see git as a filesystem – it’s content-addressable, and it has a notion of versioning

Git is a content-addressable filesystem, which means that we can retrieve the file with the “content” of file (it is actually the content identifier, i.e. sha checksum). What this means is that we can insert any kind of content into a Git repository, for which Git will hand you back a unique key you can use later to retrieve that content. So, we can take Git as a simple key-value data store, what we are going to dive into is what is the key and value.

Working Tree & Stage Area & Object Database

The working directory and all files we are working on is called Working Tree (or Working Directory, Workspace) in Git. After editing files in our Working Tree, we need to add them to a mutable index (Stage Area) before commit those changes to persistent state (Object Database).

The Stage Area is a mutable index which records all tracked files in current branch and other related info like sha checksum, file name etc (we can see the content of .git/index, which is the index file, to verify).

The Object Database store four kinds of objects:

  • A blob object is the content of a file, in binary form, with a hash as its name;
  • A tree object is the equivalent of a directory of a normal file system, which has some entries and each stores the meta info of files like file name, ;
  • A commit object refer to tree objects and some meta info like author, linking the history of commit;
  • A tag object is the reference of other object with added attributes;

From above facts, we can understand that:

  • When we edit a file and commit it, Git store a new file rather than delta/changed part: because the edited file has different binary content, so a different blob;
  • In Git, rename a file will not lost this file’s history: renaming the file will not change the hash of a file, so not change blob’s name;

The following is an example shows the relationship between objects in Git:
Examples from git book
Notice that we actually has multiple versions of Working Tree, Stage Area, because in different commit, those things are different.

References

As we have discussed, the tag object is a references with extra info. Because tag often refers to a commit, which is much like branch reference, like master, test, dev. The difference is tag will not move, but branch ref will go forward as commits evolve.

The next reference is HEAD. HEAD is a special reference to commit, always points to current working branch, so, in strict sense, it is a reference to reference.

The third type of reference is the remote reference. They are actually branch reference but they are read-only, i.e. can’t be influenced by local action.

Ref

Written with StackEdit.

评论

此博客中的热门博文

Spring Boot: Customize Environment

Spring Boot: Customize Environment Environment variable is a very commonly used feature in daily programming: used in init script used in startup configuration used by logging etc In Spring Boot, all environment variables are a part of properties in Spring context and managed by Environment abstraction. Because Spring Boot can handle the parse of configuration files, when we want to implement a project which uses yml file as a separate config file, we choose the Spring Boot. The following is the problems we met when we implementing the parse of yml file and it is recorded for future reader. Bind to Class Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties. As the document says, there exists three ways to access properties in *.properties or *.yml : @Value : access single value Environment : can access multi

Elasticsearch: Join and SubQuery

Elasticsearch: Join and SubQuery Tony was bothered by the recent change of search engine requirement: they want the functionality of SQL-like join in Elasticsearch! “They are crazy! How can they think like that. Didn’t they understand that Elasticsearch is kind-of NoSQL 1 in which every index should be independent and self-contained? In this way, every index can work independently and scale as they like without considering other indexes, so the performance can boost. Following this design principle, Elasticsearch has little related supports.” Tony thought, after listening their requirements. Leader notice tony’s unwillingness and said, “Maybe it is hard to do, but the requirement is reasonable. We need to search person by his friends, didn’t we? What’s more, the harder to implement, the more you can learn from it, right?” Tony thought leader’s word does make sense so he set out to do the related implementations Application-Side Join “The first implementation

Implement isdigit

It is seems very easy to implement c library function isdigit , but for a library code, performance is very important. So we will try to implement it and make it faster. Function So, first we make it right. int isdigit ( char c) { return c >= '0' && c <= '9' ; } Improvements One – Macro When it comes to performance for c code, macro can always be tried. #define isdigit (c) c >= '0' && c <= '9' Two – Table Upper version use two comparison and one logical operation, but we can do better with more space: # define isdigit(c) table[c] This works and faster, but somewhat wasteful. We need only one bit to represent true or false, but we use a int. So what to do? There are many similar functions like isalpha(), isupper ... in c header file, so we can combine them into one int and get result by table[c]&SOME_BIT , which is what source do. Source code of ctype.h : # define _ISbit(bit) (1 << (