Skip to content

Commit f505ff9

Browse files
ManzurManzur
Manzur
authored and
Manzur
committed
- Added special case to cat for reading trees: sha of the files are showed as text.
- Fixed bug in checkout-index: path didn't included REPOPATH. - Added interface between filesytem and checkrepo, checkoutindex, commit-tree, show-diff, read-tree, write-tree, diff-tree
1 parent 228807b commit f505ff9

19 files changed

+195
-61
lines changed

cat-file.b

+14-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Iobuf: import bufio;
1313

1414
include "utils.m";
1515
utils: Utils;
16+
bytepos, sha2string, SHALEN: import utils;
1617

1718
include "cat-file.m";
1819

@@ -36,14 +37,25 @@ init(repopath: string, typeonly: int, path: string, ch: chan of array of byte)
3637
catfile(path: string)
3738
{
3839
(filetype, filesize, buf) := utils->readsha1file(path);
39-
offset := 0;
4040

4141
msgchan <-= sys->aprint("filetype: %s\n", filetype);
4242
if(printtypeonly){
4343
msgchan <-= nil;
4444
return;
4545
}
46-
msgchan <-= buf[offset:];
46+
str := "";
47+
offset := 0;
48+
while(filetype == "tree" && (pos := bytepos(buf, offset, byte 0)) != -1){
49+
pos++;
50+
sha1 := buf[pos: pos + SHALEN];
51+
str += string buf[offset: pos - 1];
52+
str += " " + sha2string(sha1);
53+
offset = pos + SHALEN;
54+
}
55+
if(filetype == "tree")
56+
msgchan <-= sys->aprint("%s", str);
57+
else
58+
msgchan <-= buf[:];
4759
}
4860

4961
usage()

checkout-index.b

+7-4
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,18 @@ init(args: list of string)
3939
utils = load Utils Utils->PATH;
4040

4141
REPOPATH = hd args;
42-
args = tl args;
42+
4343
utils->init(REPOPATH);
4444
index = Index.new(REPOPATH);
4545
stderr = sys->fildes(2);
46-
if(!index.readindex(REPOPATH + INDEXPATH))
46+
if(!index.readindex(INDEXPATH))
4747
{
4848
sys->fprint(stderr, "index read error\n");
4949
exit;
5050
}
5151

5252
arg->init(args);
53-
while((c := arg->opt()) != 0)
54-
{
53+
while((c := arg->opt()) != 0){
5554
case c
5655
{
5756
'a' => all = 1;
@@ -118,7 +117,10 @@ checkoutentry(fd: ref Sys->FD, entry: ref Entry)
118117
checkoutall()
119118
{
120119
for(l := index.entries.all(); l != nil; l = tl l)
120+
{
121+
sys->print("checking out: %s\n", (hd l).name);
121122
checkoutfile((hd l).name);
123+
}
122124
}
123125

124126
dirname(path: string): string
@@ -135,6 +137,7 @@ makedirs(path: string)
135137
if(!exists(path))
136138
makedirs(dirname(path));
137139
sys->create(path, Sys->OREAD, Sys->DMDIR|8r755);
140+
138141
}
139142

140143
exists(path: string): int

checkout-index.m

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
Checkoutindex: module
33
{
4+
PATH: con "/dis/git/checkout-index.dis";
45
init: fn(args: list of string);
56
};
67

checkrepo.b

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ init(args: list of string)
5656

5757
check()
5858
{
59-
readrepo(OBJECTSTOREPATH);
59+
readrepo(REPOPATH + OBJECTSTOREPATH);
6060

6161
cnt := 0;
6262
for(i := 0; i < HASHSZ; i++){

checkrepo.m

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Checkrepo: module
22
{
3+
PATH: con "/dis/git/checkrepo.dis";
34
init: fn(args: list of string);
45
};
56

commit-tree.b

+47-20
Original file line numberDiff line numberDiff line change
@@ -58,33 +58,31 @@ init(args: list of string)
5858
REPOPATH = hd args;
5959
args = tl args;
6060

61-
arg->init(args);
6261
utils->init(REPOPATH);
6362
deflate->init();
6463

6564
parents: list of string = nil;
66-
sha := arg->arg();
6765

68-
while((c := arg->opt()) != 0)
69-
{
70-
case c
71-
{
66+
sha := hd args;
67+
arg->init(args);
68+
69+
while((c := arg->opt()) != 0){
70+
case c{
7271

7372
'p' => parents = arg->arg() :: parents;
7473
* => usage(); return;
7574
}
7675
}
7776

78-
commit(sha, parents);
77+
commit(sha, parents, arg->argv());
7978
}
8079

81-
commit(treesha: string, parents: list of string): string
80+
commit(treesha: string, parents: list of string, args: list of string): string
8281
{
8382
if(!utils->exists(treesha)){
8483
sys->fprint(stderr, "no such tree file\n");
8584
return "";
8685
}
87-
8886
for(l := parents; l != nil; l = tl l){
8987
if(!utils->exists(hd l)){
9088
sys->fprint(stderr, "no such sha file: %s\n", hd l);
@@ -100,15 +98,34 @@ commit(treesha: string, parents: list of string): string
10098
}
10199
config: ref Strhash[ref Config];
102100
config = utils->getuserinfo();
103-
authorname := env->getenv("AUTHOR_NAME");
104-
authoremail := env->getenv("AUTHOR_EMAIL");
105-
authordate := env->getenv("AUTHOR_DATE");
101+
102+
authorname := hd args;
103+
args = tl args;
104+
105+
authoremail := hd args;
106+
args = tl args;
106107

107-
if(authorname == "" || authoremail == ""){
108-
(authorname, authoremail) = getpersoninfo("author");
109-
}
108+
authordate := hd args;
109+
args = tl args;
110110

111-
(comname, comemail) := getpersoninfo("committer");
111+
comname := hd args;
112+
args = tl args;
113+
114+
comemail := hd args;
115+
args = tl args;
116+
117+
# authorname := env->getenv("AUTHOR_NAME");
118+
# authoremail := env->getenv("AUTHOR_EMAIL");
119+
# authordate := env->getenv("AUTHOR_DATE");
120+
#
121+
# if(authorname == "" || authoremail == ""){
122+
# (authorname, authoremail) = getpersoninfo("author");
123+
# }
124+
#
125+
# (comname, comemail) := (config.find("user"), config.find("email")););
126+
# if(comname == "" || comemail == ""){
127+
# (comname, comemail) = getpersoninfo("committer");
128+
# }
112129
date := daytime->time();
113130

114131
if(authordate == "")
@@ -117,7 +134,12 @@ commit(treesha: string, parents: list of string): string
117134
commitmsg += "author " + authorname + " <" + authoremail + "> " + authordate + "\n";
118135
commitmsg += "committer " + comname + " <" + comemail + "> " + date + "\n\n";
119136

120-
commitmsg += getcomment();
137+
while(args != nil){
138+
commitmsg += hd args;
139+
args = tl args;
140+
}
141+
# commitmsg += getcomment();
142+
121143

122144
commitlen := int2string(len commitmsg);
123145
#6 - "commit", 1 - " ", 1 - '\0'
@@ -137,9 +159,13 @@ commit(treesha: string, parents: list of string): string
137159
ch <-= (0, buf);
138160
(sz, sha) = <-ch;
139161

140-
fd := sys->open(REPOPATH + "head", Sys->OWRITE);
141-
sys->fprint(fd, "%s", treesha);
142-
return sha2string(sha);
162+
fd := sys->create(REPOPATH + "head", Sys->OWRITE, 8r644);
163+
164+
ret := sha2string(sha);
165+
166+
sys->fprint(fd, "%s", ret);
167+
168+
return ret;
143169
}
144170

145171

@@ -149,6 +175,7 @@ getpersoninfo(pos: string): (string, string)
149175
ibuf := bufio->fopen(sys->fildes(0), bufio->OREAD);
150176

151177
sys->print("Enter %s's name: ", pos);
178+
buf := array[128] of byte;
152179
name := readline(ibuf);
153180

154181
sys->print("Enter %s's email: ", pos);

commit-tree.m

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
Committree: module
33
{
4+
PATH: con "/dis/git/commit-tree.dis";
45
init: fn(args: list of string);
56
};

diff-tree.m

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
Difftree: module
33
{
4+
PATH: con "/dis/git/diff-tree.dis";
45
init: fn(args: list of string);
56
};

gitindex.b

+9-6
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ REPOPATH: string;
3434

3535
Index.new(repopath: string): ref Index
3636
{
37-
tables = load Tables Tables->PATH;
3837
sys = load Sys Sys->PATH;
38+
tables = load Tables Tables->PATH;
39+
if(tables == nil){
40+
sys->print("tables is nil: %r\n");
41+
42+
}
3943
utils = load Utils Utils->PATH;
4044
keyring = load Keyring Keyring->PATH;
4145
exclude = load Exclude Exclude->PATH;
@@ -155,7 +159,7 @@ Header.new(): ref Header
155159
#return: number of elements read from index file
156160
Index.readindex(index: self ref Index, path : string) : int
157161
{
158-
indexfd := sys->open(path, Sys->OREAD);
162+
indexfd := sys->open(REPOPATH + path, Sys->OREAD);
159163

160164
if(indexfd == nil){
161165
sys->fprint(stderr,"file access error: %r\n");
@@ -222,7 +226,7 @@ Index.readindex(index: self ref Index, path : string) : int
222226
#return: number of elements written to the index file
223227
Index.writeindex(index: self ref Index, path : string) : int
224228
{
225-
fd := sys->create(path, Sys->OWRITE, 8r644);
229+
fd := sys->create(REPOPATH + path, Sys->OWRITE, 8r644);
226230
if(fd == nil){
227231
sys->fprint(stderr, "write index error: %r\n");
228232
return 0;
@@ -254,8 +258,7 @@ Index.writeindex(index: self ref Index, path : string) : int
254258
entrybuf = (hd l).unpack();
255259

256260
cnt := sys->write(fd, entrybuf, len entrybuf);
257-
if(cnt != len entrybuf)
258-
{
261+
if(cnt != len entrybuf){
259262
sys->print("couldn't write entry to the file: %r\n");
260263
return cnt;
261264
}
@@ -407,7 +410,7 @@ initentry(filename: string): ref Entry
407410
{
408411
entry: Entry;
409412

410-
(retval, dirstat) := sys->stat(filename);
413+
(retval, dirstat) := sys->stat(REPOPATH + filename);
411414
if(retval != 0)
412415
{
413416
sys->fprint(stderr,"stat error: %r\n");

log.b

+3
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,15 @@ showlog(commits: list of string)
5454
if(filetype != "commit")
5555
return showlog(commits);
5656

57+
5758
msgchan <-= sys->aprint("Commit: %s\n", sha1);
59+
5860
ibuf := bufio->aopen(buf);
5961

6062
#reading tree and throwing it away(maybe it can be used in the future).
6163
ibuf.gets('\n');
6264

65+
6366
while((s := ibuf.gets('\n')) != nil){
6467
(t, parentsha1) := splitr(s, " ");
6568
if(t != "parent ")

0 commit comments

Comments
 (0)