Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

formats: Index read support #91

Merged
merged 13 commits into from
Oct 26, 2016
Merged

formats: Index read support #91

merged 13 commits into from
Oct 26, 2016

Conversation

mcuadros
Copy link
Contributor

@mcuadros mcuadros commented Oct 20, 2016

This is a basic implementation of the Index format

The index format contains the information about which objects are currently checked out in the worktree, having information about the working files. Changes in worktree are detected using this Index. The Index is also used during merge.

Three types of version are available 2, 3 and 4, this implementation only supports 2, and I am planning to add support for version 3 and 4, in this same PR

This format also have several extensions, this implementation only have the cache Tree extension.

So the pending stuff is:

  • Support version 3 and 4
  • Proper decoding of nested types like Flags and Mode
  • Implementation of Resolve undo extension
  • Implementation of Untracked cache extension
  • Support split index

I must say that this is one of the most complex format in git

UPDATE: I am not implementing Split Index and Untracked cache extensions because are not supported by libgit or jgit, also because are optional and arcane extension. The reader is ready for review

@mcuadros mcuadros changed the title formats: Index support (WIP) formats: Index read support (WIP) Oct 21, 2016
@mcuadros mcuadros changed the title formats: Index read support (WIP) formats: Index read support Oct 25, 2016

func validateHeader(r io.Reader) (version uint32, err error) {
var s = make([]byte, 4)
if _, err := r.Read(s); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be a ReadFull.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, fixing it

return nil, err
}

read := 62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a magic number.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, fixing it

e.Stage = Stage(e.Flags>>12) & 0x3

if e.Flags&EntryExtended != 0 {
read += 2
Copy link
Contributor

@alcortesm alcortesm Oct 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this and all other magic numbers by making readBinary return the number of bytes read.


if err := d.readEntryName(idx, e); err != nil {
return nil, err

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this empty line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, fixing it

name, err = d.doReadEntryName(e)
case 4:
name, err = d.doReadEntryNameV4()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Version is a public field, there should be a default in this switch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, fixing it

var buf [1]byte
value := make([]byte, 0, 16)
for {
if _, err := r.Read(buf[:]); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReadFull or check n!=0 then continue with a sleep or something.


func readUntil(r io.Reader, delim byte) ([]byte, error) {
var buf [1]byte
value := make([]byte, 0, 16)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you choose 16 as a good initial size?


e.Entries = i

trees, err := readUntil(d.r, 10)
Copy link
Contributor

@alcortesm alcortesm Oct 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NO WAY, use \n!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hahaha

return nil, err
}

return e, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you replace all this function with fmt.Fscanf(d.r, "%s\x00%d %s\n%s" &e.Path, &e.Entries, &e.Tree, &e.Hash) ? or some other one-liner?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic now is a bit more complex, so... I prefer not

}
}

// dheader[pos] = ofs & 127;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last 30 lines look familiar, didn't we have code this before?

This comment must be explained.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep don't worry I am making another PR creating a package for binary reads

func (d *TreeExtensionDecoder) readEntry() (*TreeEntry, error) {
e := &TreeEntry{}

path, err := readUntil(d.r, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason using an integer here sounds weird, I think it will be much better if you use \x00 here as the delimiter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

solved

@mcuadros mcuadros merged commit 91bc613 into src-d:master Oct 26, 2016
@mcuadros mcuadros deleted the format-index branch December 13, 2016 10:14
mcuadros added a commit that referenced this pull request Jan 31, 2017
* utils: fs generic TestSuite

* fs: fs.TempFile

* utils: fs small changes requested

* utils: fs, test fs.Create overwriting files

* formats: index, basic v2 reader

* formats: index, tree extension support

* formats: index, stage decoding

* formats: index, extended flags, v3 support

* formats: index, v4 support

* formats: index, Resolve undo support

* formats: index, fix error when decoding invalidated entries

* formats: index, fix style issues
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants