Code

Only draw dates with non-zero seconds
[tig.git] / tig.c
diff --git a/tig.c b/tig.c
index 602c8678614339d8ad6310f6ee158c4063ec25f6..c1dd51366a2694bffe61de408c9e3f2c0ce37e61 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -70,6 +70,7 @@ static void warn(const char *msg, ...);
 static void report(const char *msg, ...);
 static void set_nonblocking_input(bool loading);
 static size_t utf8_length(const char **string, size_t col, int *width, size_t max_width, int *trimmed, bool reserve);
+static inline unsigned char utf8_char_length(const char *string, const char *end);
 
 #define ABS(x)         ((x) >= 0  ? (x) : -(x))
 #define MIN(x, y)      ((x) < (y) ? (x) :  (y))
@@ -358,32 +359,6 @@ suffixcmp(const char *str, int slen, const char *suffix)
 }
 
 
-/*
- * What value of "tz" was in effect back then at "time" in the
- * local timezone?
- */
-static int local_tzoffset(time_t time)
-{
-       time_t t, t_local;
-       struct tm tm;
-       int offset, eastwest; 
-
-       t = time;
-       localtime_r(&t, &tm);
-       t_local = mktime(&tm);
-
-       if (t_local < t) {
-               eastwest = -1;
-               offset = t - t_local;
-       } else {
-               eastwest = 1;
-               offset = t_local - t;
-       }
-       offset /= 60; /* in minutes */
-       offset = (offset % 60) + ((offset / 60) * 100);
-       return offset * eastwest;
-}
-
 #define DATE_INFO \
        DATE_(NO), \
        DATE_(DEFAULT), \
@@ -402,8 +377,18 @@ static const struct enum_map date_map[] = {
 #undef DATE_
 };
 
+struct time {
+       time_t sec;
+       int tz;
+};
+
+static inline int timecmp(const struct time *t1, const struct time *t2)
+{
+       return t1->sec - t2->sec;
+}
+
 static const char *
-string_date(const time_t *time, enum date date)
+string_date(const struct time *time, enum date date)
 {
        static char buf[DATE_COLS + 1];
        static const struct enum_map reldate[] = {
@@ -418,7 +403,7 @@ string_date(const time_t *time, enum date date)
 
        if (date == DATE_RELATIVE) {
                struct timeval now;
-               time_t date = *time + local_tzoffset(*time);
+               time_t date = time->sec + time->tz;
                time_t seconds;
                int i;
 
@@ -438,7 +423,7 @@ string_date(const time_t *time, enum date date)
                }
        }
 
-       gmtime_r(time, &tm);
+       gmtime_r(&time->sec, &tm);
        return strftime(buf, sizeof(buf), DATE_FORMAT, &tm) ? buf : NULL;
 }
 
@@ -461,22 +446,36 @@ static const struct enum_map author_map[] = {
 #undef AUTHOR_
 };
 
-/* FIXME: Handle multi-byte and multi-column characters. */
 static const char *
-get_author_initials(const char *author, size_t max_columns)
+get_author_initials(const char *author)
 {
-       static char initials[AUTHOR_COLS];
-       size_t pos;
+       static char initials[AUTHOR_COLS * 6 + 1];
+       size_t pos = 0;
+       const char *end = strchr(author, '\0');
 
 #define is_initial_sep(c) (isspace(c) || ispunct(c) || (c) == '@' || (c) == '-')
 
        memset(initials, 0, sizeof(initials));
-       for (pos = 0; *author && pos < sizeof(initials) - 1; author++, pos++) {
+       while (author < end) {
+               unsigned char bytes;
+               size_t i;
+
                while (is_initial_sep(*author))
                        author++;
-               strncpy(&initials[pos], author, sizeof(initials) - 1 - pos);
-               while (*author && author[1] && !is_initial_sep(author[1]))
-                       author++;
+
+               bytes = utf8_char_length(author, end);
+               if (bytes < sizeof(initials) - 1 - pos) {
+                       while (bytes--) {
+                               initials[pos++] = *author++;
+                       }
+               }
+
+               for (i = pos; author < end && !is_initial_sep(*author); author++) {
+                       if (i < sizeof(initials) - 1)
+                               initials[i++] = *author;
+               }
+
+               initials[i++] = 0;
        }
 
        return initials;
@@ -2244,9 +2243,9 @@ draw_field(struct view *view, enum line_type type, const char *text, int len, bo
 }
 
 static bool
-draw_date(struct view *view, time_t *time)
+draw_date(struct view *view, struct time *time)
 {
-       const char *date = time ? mkdate(time) : "";
+       const char *date = time && time->sec ? mkdate(time) : "";
        int cols = opt_date == DATE_SHORT ? DATE_SHORT_COLS : DATE_COLS;
 
        return draw_field(view, LINE_DATE, date, cols, FALSE);
@@ -2259,7 +2258,7 @@ draw_author(struct view *view, const char *author)
        bool abbreviate = opt_author == AUTHOR_ABBREVIATED || !trim;
 
        if (abbreviate && author)
-               author = get_author_initials(author, opt_author_cols);
+               author = get_author_initials(author);
 
        return draw_field(view, LINE_AUTHOR, author, opt_author_cols, trim);
 }
@@ -3081,7 +3080,7 @@ prepare_update_file(struct view *view, const char *name)
 {
        if (view->pipe)
                end_update(view, TRUE);
-       return io_open(&view->io, "%s", name);
+       return io_open(&view->io, "%s/%s", opt_cdup[0] ? opt_cdup : ".", name);
 }
 
 static bool
@@ -3719,7 +3718,13 @@ get_author(const char *name)
 }
 
 static void
-parse_timezone(time_t *time, const char *zone)
+parse_timesec(struct time *time, const char *sec)
+{
+       time->sec = (time_t) atol(sec);
+}
+
+static void
+parse_timezone(struct time *time, const char *zone)
 {
        long tz;
 
@@ -3731,14 +3736,15 @@ parse_timezone(time_t *time, const char *zone)
        if (zone[0] == '-')
                tz = -tz;
 
-       *time -= tz;
+       time->tz = tz;
+       time->sec -= tz;
 }
 
 /* Parse author lines where the name may be empty:
  *     author  <email@address.tld> 1138474660 +0100
  */
 static void
-parse_author_line(char *ident, const char **author, time_t *time)
+parse_author_line(char *ident, const char **author, struct time *time)
 {
        char *nameend = strchr(ident, '<');
        char *emailend = strchr(ident, '>');
@@ -3760,7 +3766,7 @@ parse_author_line(char *ident, const char **author, time_t *time)
                char *secs = emailend + 2;
                char *zone = strchr(secs, ' ');
 
-               *time = (time_t) atol(secs);
+               parse_timesec(time, secs);
 
                if (zone && strlen(zone) == STRING_SIZE(" +0700"))
                        parse_timezone(time, zone + 1);
@@ -4079,7 +4085,7 @@ help_open_keymap(struct view *view, enum keymap keymap)
 
                if (add_title && help_open_keymap_title(view, keymap))
                        return;
-               add_title = false;
+               add_title = FALSE;
 
                if (group) {
                        add_line_text(view, group, LINE_HELP_GROUP);
@@ -4230,7 +4236,7 @@ push_tree_stack_entry(const char *name, unsigned long lineno)
 struct tree_entry {
        char id[SIZEOF_REV];
        mode_t mode;
-       time_t time;                    /* Date from the author ident. */
+       struct time time;               /* Date from the author ident. */
        const char *author;             /* Author of the commit. */
        char name[1];
 };
@@ -4269,7 +4275,7 @@ tree_compare(const void *l1, const void *l2)
 
        switch (get_sort_field(tree_sort_state)) {
        case ORDERBY_DATE:
-               return sort_order(tree_sort_state, entry1->time - entry2->time);
+               return sort_order(tree_sort_state, timecmp(&entry1->time, &entry2->time));
 
        case ORDERBY_AUTHOR:
                return sort_order(tree_sort_state, strcmp(entry1->author, entry2->author));
@@ -4306,7 +4312,7 @@ static bool
 tree_read_date(struct view *view, char *text, bool *read_date)
 {
        static const char *author_name;
-       static time_t author_time;
+       static struct time author_time;
 
        if (!text && *read_date) {
                *read_date = FALSE;
@@ -4453,7 +4459,7 @@ tree_draw(struct view *view, struct line *line, unsigned int lineno)
                if (opt_author && draw_author(view, entry->author))
                        return TRUE;
 
-               if (opt_date && draw_date(view, entry->author ? &entry->time : NULL))
+               if (opt_date && draw_date(view, &entry->time))
                        return TRUE;
        }
        if (draw_text(view, line->type, entry->name, TRUE))
@@ -4696,7 +4702,7 @@ struct blame_commit {
        char id[SIZEOF_REV];            /* SHA1 ID. */
        char title[128];                /* First line of the commit message. */
        const char *author;             /* Author of the commit. */
-       time_t time;                    /* Date from the author ident. */
+       struct time time;               /* Date from the author ident. */
        char filename[128];             /* Name of file. */
        bool has_previous;              /* Was a "previous" line detected. */
 };
@@ -4883,7 +4889,7 @@ blame_read(struct view *view, char *line)
                commit->author = get_author(line);
 
        } else if (match_blame_header("author-time ", &line)) {
-               commit->time = (time_t) atol(line);
+               parse_timesec(&commit->time, line);
 
        } else if (match_blame_header("author-tz ", &line)) {
                parse_timezone(&commit->time, line);
@@ -4906,7 +4912,7 @@ static bool
 blame_draw(struct view *view, struct line *line, unsigned int lineno)
 {
        struct blame *blame = line->data;
-       time_t *time = NULL;
+       struct time *time = NULL;
        const char *id = NULL, *author = NULL;
        char text[SIZEOF_STR];
 
@@ -5098,7 +5104,7 @@ static struct view_ops blame_ops = {
 
 struct branch {
        const char *author;             /* Author of the last commit. */
-       time_t time;                    /* Date of the last activity. */
+       struct time time;               /* Date of the last activity. */
        const struct ref *ref;          /* Name and commit ID information. */
 };
 
@@ -5117,7 +5123,7 @@ branch_compare(const void *l1, const void *l2)
 
        switch (get_sort_field(branch_sort_state)) {
        case ORDERBY_DATE:
-               return sort_order(branch_sort_state, branch1->time - branch2->time);
+               return sort_order(branch_sort_state, timecmp(&branch1->time, &branch2->time));
 
        case ORDERBY_AUTHOR:
                return sort_order(branch_sort_state, strcmp(branch1->author, branch2->author));
@@ -5134,7 +5140,7 @@ branch_draw(struct view *view, struct line *line, unsigned int lineno)
        struct branch *branch = line->data;
        enum line_type type = branch->ref->head ? LINE_MAIN_HEAD : LINE_DEFAULT;
 
-       if (opt_date && draw_date(view, branch->author ? &branch->time : NULL))
+       if (opt_date && draw_date(view, &branch->time))
                return TRUE;
 
        if (opt_author && draw_author(view, branch->author))
@@ -6307,7 +6313,7 @@ struct commit {
        char id[SIZEOF_REV];            /* SHA1 ID. */
        char title[128];                /* First line of the commit message. */
        const char *author;             /* Author of the commit. */
-       time_t time;                    /* Date from the author ident. */
+       struct time time;               /* Date from the author ident. */
        struct ref_list *refs;          /* Repository references. */
        chtype graph[SIZEOF_REVGRAPH];  /* Ancestry chain graphics. */
        size_t graph_size;              /* The width of the graph array. */
@@ -6795,6 +6801,14 @@ static const unsigned char utf8_bytes[256] = {
        3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3, 4,4,4,4,4,4,4,4, 5,5,5,5,6,6,1,1,
 };
 
+static inline unsigned char
+utf8_char_length(const char *string, const char *end)
+{
+       int c = *(unsigned char *) string;
+
+       return utf8_bytes[c];
+}
+
 /* Decode UTF-8 multi-byte representation into a Unicode character. */
 static inline unsigned long
 utf8_to_unicode(const char *string, size_t length)
@@ -6862,8 +6876,7 @@ utf8_length(const char **start, size_t skip, int *width, size_t max_width, int *
        *trimmed = 0;
 
        while (string < end) {
-               int c = *(unsigned char *) string;
-               unsigned char bytes = utf8_bytes[c];
+               unsigned char bytes = utf8_char_length(string, end);
                size_t ucwidth;
                unsigned long unicode;