ã¯ããã¾ãã¦ããµã¼ããµã¤ãã¨ã³ã¸ãã¢ã® @DQNEO ã§ãã
ä»æ¥ã¯Gitã®ã¤ããããããç´¹ä»ãã¾ãã
Cè¨èªå¦ç¿ææã¨ãã¦ã®Git
Gitã¨åããã®ãã¼ãããä½ã£ã¦ä½ã®æå³ãããã®ãï¼ã¨æãã¾ãããã
ç§ããã®åçºæãããå§ããåæ©ã¯ãCè¨èªãæ¸ããããã«ãªããããã§ããã
å®éã«éä¸ã¾ã§ãã£ã¦ã¿ãã¨ããã
- Cè¨èªããã§ãããããããã«ãªã£ã
- Gitã®å é¨æ§é ã«è©³ãããªã£ã
ã¨ããã¡ãªãããå¾ããã¾ããã
Cè¨èªãåå¼·ããé¡æã¯ããããªã¹ã¨ãWebãµã¼ãã¨ãä»ã«ãããã§ãããã®ã§ãããGitãå®è£ ãã¦ã¿ãã®ã¯ããªãããããã§ããçç±ã¯ä¸è¨ã®éãã§ãã
- å é¨æ§é ãæå¤ã¨åç´
- (ãã¼ã«ã«ã§åããåã«ã¯)ãããã¯ã¼ã¯ã®ç¥èãä¸è¦
- æ®æ®µä½¿ã£ã¦ãããã¼ã«ã§å¤é¨ä»æ§ãããã£ã¦ããã®ã§ãããã¹ããã¨ãæ確
ä½è«ã§ãããæ¬å®¶Gitã®ã½ã¼ã¹ã³ã¼ããåèã«ãããã¨æã£ã¦èªãã§ããããLinus Tovalsãæ¸ããã³ã¼ããç®ã«ãã¦ã«ãã³ã·ã§ã³ããããã¾ããã
Gitã®ã³ã³ãã³ã管çã®ä»çµã¿
誤解ãæããã«è¨ãã°ãGitã¨ã¯ä¸ç¨®ã®ã³ã³ãã³ã管çã·ã¹ãã ã§ããããã®å®ä½ã¯KVSã§ãã
git checkoutãgit showã¯KVSãããã¼ã¿ãåãåºãã³ãã³ãã§ã
git addãgit commitã¯KVSã«ãã¼ã¿ãä¿åããã³ãã³ãã§ãã
KVSãç´æ¥æä½ããä½ã¬ã¤ã¤ãªã³ãã³ãã¨ãã¦ã
- ãã¼ã¿ãåå¾: git cat-file -p
- ãã¼ã¿ãä¿å: git hash-object -w
ãªã©ãããã¾ãã
ãã®ãã¡ä»çµã¿ãæãç°¡åãªã®ãgit cat-file -p
ã§ãã
ä»åã¯ãããå®è£
ãã¦ã¿ã¾ãããã
æºå
ãããããgitã¬ãã¸ããªãä½ã£ã¦ã³ã³ãã³ããæ ¼ç´ãã¾ãã
ä¾ã¨ãã¦ããã¹ããã¡ã¤ã«ã2ã¤ã³ããããã¦ã¿ã¾ãããã
- “hello world\n”ã¨æ¸ãããããã¹ããã¡ã¤ã«
- å¤ç®æ¼±ç³ãåã£ã¡ãããã®å ¨æ
# ã¬ãã¸ããªä½æ $ git init repo $ cd repo # hello worldãadd $ echo "hello world" > helloworld.txt $ git add helloworld.txt # ãåã£ã¡ããããé空æ庫ãããã¦ã³ãã¼ããã¦add $ wget http://www.aozora.gr.jp/cards/000148/files/752_ruby_2438.zip $ unzip 752_ruby_2438.zip $ rm 752_ruby_2438.zip $ git add bocchan.txt # ã³ããã $ git commit -m "initial commit" [master (root-commit) 2b8c1c1] initial commit 2 files changed, 539 insertions(+) create mode 100644 bocchan.txt create mode 100644 helloworld.txt
ããã¹ããã¡ã¤ã«ãï¼ã¤ã³ããããã¾ããã
æ ¼ç´ãããã³ã³ãã³ããgit cat-file -pã§åãåºã
æ ¼ç´ããã³ã³ãã³ããåãåºãã¦ã¿ã¾ãã
ã¾ãã³ããããªãã¸ã§ã¯ãã®ä¸èº«ãããã¹ã¦ããªã¼ãªãã¸ã§ã¯ãã®hashå¤ã調ã¹ã¾ãã
$ git cat-file -p HEAD tree a97f2489fa15982425e3df14dcf7af9947bec21e author DQNEO <[email protected]> 1441790109 +0900 committer DQNEO <[email protected]> 1441790109 +0900
次ã«ããªã¼ãªãã¸ã§ã¯ãã®ä¸èº«ãããã¹ã¦blobãªãã¸ã§ã¯ãã®hashå¤ã調ã¹ã¾ãã
$ git cat-file -p a97f2489fa15982425e3df14dcf7af9947bec21e 100644 blob a4940ec3db4cd24542203a9447c4259c96294c09 bocchan.txt 100644 blob 3b18e512dba79e4c8300dd08aeb37f8e728b8dad helloworld.txt
æå¾ã«blobãªãã¸ã§ã¯ãã®ä¸èº«ã表示ãã¾ãã
ãããã³ã³ãã³ãæ¬ä½ã§ãã
$ git cat-file -p 3b18e512dba79e4c8300dd08aeb37f8e728b8dad hello world $ git cat-file -p a4940ec3db4cd24542203a9447c4259c96294c09 | head | nkf -w åã£ã¡ãã å¤ç®æ¼±ç³ ------------------------------------------------------- ãããã¹ãä¸ã«ç¾ããè¨å·ã«ã¤ãã¦ã ããï¼ã«ã ï¼ä¾ï¼åãã¼ãã£ã¡ãã
KVSãããã¼ã¿ãåãåºããã¨ãã§ãã¾ããã
blob objectã®ãã¼ã¿æ§é
ãã¦ãgitã®ã³ã³ãã³ã㯔blob object”ã¨å¼ã°ããzlibå§ç¸®ããããã¡ã¤ã«ã«ãªã£ã¦ãã¾ãã
ãã¼ã¿æ§é ã®ä»æ§ã¯ãã®ããã«ãªã£ã¦ãã¾ãã
<blob><åè§ã¹ãã¼ã¹><ã³ã³ãã³ãã®ãµã¤ãº><ãã«æå><ã³ã³ãã³ãæ¬ä½>
ä¾ãã°”hello world\n” ã¨ããã³ã³ãã³ãããblob objectãä½ãã¨ããã¨ã
“hello world\n”ã®ã³ã³ãã³ãã®é·ãã¯12ãã¤ããªã®ã§
blob 12\0hello world\n
ã¨ãªãã¾ãã
ã³ã³ãã³ãã巨大ãªå ´åãåãã§ãå¤ç®æ¼±ç³ãåã¡ãããå ¨æãæ ¼ç´ããå ´åã¯ãããªãã¾ã
blob 209990\0åã£ã¡ãã\nå¤ç®æ¼±ç³\n....
git cat-file -p
ãèªåã§å®è£
ãã
ã§ã¯å®éã«å®è£ ãã¦ã¿ã¾ãããã
æ¬è³ªçã«ã¯zlib解åãã¦ä¸è¨ãã©ã¼ãããããã¼ã¹ããã ããªã®ã§ãã·ã³ãã«ã§ãã
git_cat_file-p.c
/** * git cat-file -p ç¸å½ã®ãã¨ãããã³ãã³ã * * original from http://oku.edu.mie-u.ac.jp/~okumura/compression/comptest.c * licensed under http://creativecommons.org/licenses/by/4.0/ */ #include <stdio.h> #include <stdlib.h> #include <zlib.h> #include <string.h> #define INBUFSIZ 1024 #define OUTBUFSIZ 1024 int _write_skipping_header(char *outbuf, size_t size, size_t n ,FILE *fout) { static int is_header = 1; int tmp_n = n; if (is_header) { while (*outbuf) {outbuf++; tmp_n--;} fwrite(outbuf, size, tmp_n, fout); is_header = 0; return n; } return fwrite(outbuf, size, n, fout); } void _decompress(FILE *fin, FILE *fout) { z_stream z; char inbuf[INBUFSIZ]; char outbuf[OUTBUFSIZ]; int count, status; z.zalloc = Z_NULL; z.zfree = Z_NULL; z.opaque = Z_NULL; z.next_in = Z_NULL; z.avail_in = 0; if (inflateInit(&z) != Z_OK) { fprintf(stderr, "inflateInit: %s\n", (z.msg) ? z.msg : "???"); exit(1); } z.next_out = (Bytef *)outbuf; z.avail_out = OUTBUFSIZ; status = Z_OK; while (status != Z_STREAM_END) { if (z.avail_in == 0) { z.next_in = (Bytef *)inbuf; z.avail_in = fread(inbuf, 1, INBUFSIZ, fin); } status = inflate(&z, Z_NO_FLUSH); if (status == Z_STREAM_END) break; if (status != Z_OK) { fprintf(stderr, "inflate: %s\n", (z.msg) ? z.msg : "???"); exit(1); } if (z.avail_out == 0) { if (_write_skipping_header(outbuf, 1, OUTBUFSIZ, fout) != OUTBUFSIZ) { fprintf(stderr, "Write error\n"); exit(1); } z.next_out = (Bytef *)outbuf; z.avail_out = OUTBUFSIZ; } } if ((count = OUTBUFSIZ - z.avail_out) != 0) { if (_write_skipping_header(outbuf, 1, count, fout) != count) { fprintf(stderr, "Write error\n"); exit(1); } } if (inflateEnd(&z) != Z_OK) { fprintf(stderr, "inflateEnd: %s\n", (z.msg) ? z.msg : "???"); exit(1); } } void usage() { fprintf(stderr, "Usage:\n"); fprintf(stderr, " git_cat_file-p blob_file\n"); } int main(int argc, char *argv[]) { FILE *fin; if (argc == 1) { usage(); exit(0); } if ((fin = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "Can't open %s\n", argv[1]); exit(1); } _decompress(fin, stdout); return 0; }
zlib解åããé¨åãã¡ãã£ã¨é£è§£ã§ãããããã¯èªåã§æ¸ããããã§ã¯ãªãä¸é大å¦ã®å¥¥æææãå
¬éããã¦ãããµã³ãã«ã³ã¼ã
ãã³ãããã¦ããã ãã§ãã(奥æå
çãããã¨ããããã¾ãï¼)
ã§ã¯ã³ã³ãã¤ã«ãã¦å®è¡ãã¦ã¿ã¾ãã
$ gcc -g -Wall -O0 -std=c99 -lz -o git_cat_file-p git_cat_file-p.c $ ./git_cat_file-p .git/objects/3b/18e512dba79e4c8300dd08aeb37f8e728b8dad hello world $ ./git_cat_file-p .git/objects/a4/940ec3db4cd24542203a9447c4259c96294c09 | head | nkf -w åã£ã¡ãã å¤ç®æ¼±ç³ ------------------------------------------------------- ãããã¹ãä¸ã«ç¾ããè¨å·ã«ã¤ãã¦ã ããï¼ã«ã ï¼ä¾ï¼åãã¼ãã£ã¡ãã
git cat-file -p
ã¨åããã®*1ãã§ãã¾ããï¼ï¼
ãããªæãã§Gitã®ãµãã³ãã³ããã¤ãããã¨ãã§ãã¾ãã*2
ããã§ãã£ãã®ã¯ã解åãã§ããããããéã«ãã¦ãå§ç¸®ããå®è£ ããã° git add (ã®ã³ã¢ã®é¨å)ãã¤ãããã¨ãã§ãã¾ãã
YAPC::Asiaã§ã®LTè³æ
åèè³æ
- Gitãé£æ³é åè¨æ¶è£ ç½®ã§ãããã¨ãä½ã¬ã¤ã¤ã¼ãªæä½ãéãã¦ä½æãããï¼
- å®è·µ Git – ä½ã¬ãã«ã«ç¥ã Git
- Git ãããã ã¢ããããç解ãã
- Gitã®å å´ – Gitãªãã¸ã§ã¯ã
- ãµãã³ãã³ãä¸è¦§
- gitcore-tutorial(7)
- gitã®ã½ã¼ã¹ã³ã¼ããèªã – ããè¯ãç°å¢ãæ±ãã¦
- ãã¡ã¤ã«ã·ã¹ãã ã¨ãã¦ã® Git – è¨èªã²ã¼ã
ã¾ã¨ã
Gitã®ãµãã³ãã³ããã²ã¨ã¤å®è£ ãããã¨ãã§ãã¾ããã
Gitãä½ã£ã¦ã¿ãã¨Cè¨èªã®åå¼·ã«ãªããGitã®å
é¨æ§é ã«ã詳ãããªãã¾ãã
èå³ãããã°ãã²ãã£ã¬ã³ã¸ãã¦ã¿ã¦ãã ããï¼
*1:å³å¯ã«è¨ãã¨ãgit cat-fille -pã¯ãã£ã¨å¤æ©è½ãªã®ã§å®å ¨ã«åãã§ã¯ãªãã§ãã
*2:ãªããä»åã¯å ¥éè¨äºãªã®ã§ pack filesã®èª¬æã¯å²æãã¾ãããå®éã®gitã¯ããã¡ãã£ã¨è¤éã§ããGit – パックファイル