Skip to content

Commit

Permalink
Fix parsing of git ls-tree for submodules
Browse files Browse the repository at this point in the history
Closes jonas#1282
  • Loading branch information
koutcher committed Mar 28, 2024
1 parent e314418 commit a37ce5c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
1 change: 1 addition & 0 deletions NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Bug fixes:
- Fix status view lockup.
- Fix untracked changes and chunk staging behaviour in plain stage view.
- Reset state variables when selecting a commit with no reference.
- Fix parsing of `git ls-tree` for submodules. (#1282)

tig-2.5.8
---------
Expand Down
11 changes: 5 additions & 6 deletions src/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,10 @@ draw_id(struct view *view, struct view_column *column, const char *id)
}

static bool
draw_filename(struct view *view, struct view_column *column, const char *filename, mode_t mode)
draw_filename(struct view *view, struct view_column *column, enum line_type type, const char *filename)
{
size_t width = filename ? utf8_width(filename) : 0;
bool trim = width >= column->width;
enum line_type type = S_ISDIR(mode) ? LINE_DIRECTORY : LINE_FILE;
int column_width = column->width ? column->width : width;

if (column->opt.file_name.display == FILENAME_NO)
Expand All @@ -290,9 +289,9 @@ draw_filename(struct view *view, struct view_column *column, const char *filenam
}

static bool
draw_file_size(struct view *view, struct view_column *column, unsigned long size, mode_t mode)
draw_file_size(struct view *view, struct view_column *column, enum line_type type, unsigned long size)
{
const char *str = S_ISDIR(mode) ? NULL : mkfilesize(size, column->opt.file_size.display);
const char *str = type == LINE_FILE ? mkfilesize(size, column->opt.file_size.display) : NULL;

if (!column->width || column->opt.file_size.display == FILE_SIZE_NO)
return false;
Expand Down Expand Up @@ -518,7 +517,7 @@ view_column_draw(struct view *view, struct line *line, unsigned int lineno)
continue;

case VIEW_COLUMN_FILE_SIZE:
if (draw_file_size(view, column, column_data.file_size ? *column_data.file_size : 0, mode))
if (draw_file_size(view, column, line->type, column_data.file_size ? *column_data.file_size : 0))
return true;
continue;

Expand All @@ -529,7 +528,7 @@ view_column_draw(struct view *view, struct line *line, unsigned int lineno)
continue;

case VIEW_COLUMN_FILE_NAME:
if (draw_filename(view, column, column_data.file_name, mode))
if (draw_filename(view, column, line->type, column_data.file_name))
return true;
continue;

Expand Down
51 changes: 35 additions & 16 deletions src/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,6 @@ push_tree_stack_entry(struct view *view, const char *name, struct position *posi
* 100644 blob 95925677ca47beb0b8cce7c0e0011bcc3f61470f 213045 tig.c
*/

#define SIZEOF_TREE_ATTR \
STRING_SIZE("100644 blob f931e1d229c3e185caad4449bf5b66ed72462657\t")

#define SIZEOF_TREE_MODE \
STRING_SIZE("100644 ")

#define TREE_ID_OFFSET \
STRING_SIZE("100644 blob ")

#define tree_path_is_parent(path) (!strcmp("..", (path)))

struct tree_entry {
Expand Down Expand Up @@ -222,21 +213,50 @@ tree_read(struct view *view, struct buffer *buf, bool force_stop)
struct tree_entry *data;
struct line *entry, *line;
enum line_type type;
char *pos;
char *mode;
char *id;
char *path;
size_t size;

if (state->read_date || !buf)
return tree_read_date(view, buf, state);

if (buf->size <= SIZEOF_TREE_ATTR)
return false;
if (view->lines == 0 &&
!tree_entry(view, LINE_HEADER, view->env->directory, NULL, NULL, 0))
return false;

size = parse_size(buf->data + SIZEOF_TREE_ATTR);
path = strchr(buf->data + SIZEOF_TREE_ATTR, '\t');
if (!path)
/* 100644 */
mode = buf->data;

/* blob */
pos = strchr(mode, ' ');
if (!pos || !*pos)
return false;
pos++;
if (!strncmp(pos, "blob", STRING_SIZE("blob")))
type = LINE_FILE;
else if (!strncmp(pos, "tree", STRING_SIZE("tree")))
type = LINE_DIRECTORY;
else
type = LINE_DEFAULT;

/* 95925677ca47beb0b8cce7c0e0011bcc3f61470f */
id = strchr(pos, ' ');
if (!id || !*id)
return false;
id++;

/* 213045 */
pos = strchr(id, ' ');
if (!pos || !*pos)
return false;
pos++;
size = parse_size(pos);

/* tig.c */
path = strchr(pos, '\t');
if (!path || !*path)
return false;
path++;

Expand All @@ -255,8 +275,7 @@ tree_read(struct view *view, struct buffer *buf, bool force_stop)
return false;
}

type = buf->data[SIZEOF_TREE_MODE] == 't' ? LINE_DIRECTORY : LINE_FILE;
entry = tree_entry(view, type, path, buf->data, buf->data + TREE_ID_OFFSET, size);
entry = tree_entry(view, type, path, mode, id, size);
if (!entry)
return false;
data = entry->data;
Expand Down

0 comments on commit a37ce5c

Please sign in to comment.