| 94 | } |
| 95 | |
| 96 | func parseObj(obj string) (result GitObject, err error) { |
| 97 | |
| 98 | parts := strings.Split(obj, "\x00") |
| 99 | parts = strings.Fields(parts[0]) |
| 100 | resultType := parts[0] |
| 101 | resultSize := parts[1] |
| 102 | nullIndex := strings.Index(obj, "\x00") |
| 103 | |
| 104 | lines := strings.Split(obj[nullIndex+1:], "\n") |
| 105 | |
| 106 | switch resultType { |
| 107 | case "commit": |
| 108 | var commit = Commit{_type: resultType, size: resultSize} |
| 109 | for i, line := range lines { |
| 110 | // The next line is the commit message |
| 111 | if len(strings.Fields(line)) == 0 { |
| 112 | commit.Message = strings.Join(lines[i+1:], "\n") |
| 113 | break |
| 114 | } |
| 115 | parts := strings.Fields(line) |
| 116 | key := parts[0] |
| 117 | switch KeyType(key) { |
| 118 | case TreeKey: |
| 119 | commit.Tree = parts[1] |
| 120 | case ParentKey: |
| 121 | commit.Parents = append(commit.Parents, parts[1]) |
| 122 | case AuthorKey: |
| 123 | commit.Author = strings.Join(parts[1:], " ") |
| 124 | case CommitterKey: |
| 125 | commit.Committer = strings.Join(parts[1:], " ") |
| 126 | default: |
| 127 | err = fmt.Errorf("Encountered unknown field in commit: %s", key) |
| 128 | return |
| 129 | } |
| 130 | } |
| 131 | result = commit |
| 132 | case "tree": |
| 133 | var tree = Tree{_type: resultType, size: resultSize} |
| 134 | |
| 135 | scanner := bufio.NewScanner(bytes.NewBuffer([]byte(obj))) |
| 136 | scanner.Split(ScanNullLines) |
| 137 | |
| 138 | var tmp objectMeta |
| 139 | |
| 140 | var resultObjs []objectMeta |
| 141 | |
| 142 | for count := 0; ; count++ { |
| 143 | done := !scanner.Scan() |
| 144 | if done { |
| 145 | break |
| 146 | } |
| 147 | |
| 148 | txt := scanner.Text() |
| 149 | |
| 150 | if count == 0 { |
| 151 | // the first time through, scanner.Text() will be |
| 152 | // "tree <size>" |
| 153 | continue |