summaryrefslogtreecommitdiff
path: root/cgitcgi/ui-log.c
blob: a768d7b783fe97d991c91f2cce4351364907d275 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#else
#include <stdint.h>
#endif
#include <stddef.h>   /* offsetof(3) */
#include <dirent.h>
#include <sys/stat.h> /* chmod(2)    */
#include <sys/file.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>   /* strdup(3)   */
#include <libgen.h>   /* basename(3) */
#include <ctype.h>    /* tolower(3)  */
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <pwd.h>
#include <grp.h>
#include <stdarg.h>
#include <locale.h>
#include <unistd.h>

#include <git2.h>

#include <nls.h>

#include <defs.h>

#include <fatal.h>
#include <http.h>
#include <html.h>

#include <dlist.h>
#include <strbuf.h>
#include <repolist.h>
#include <wrapper.h>
#include <system.h>
#include <date.h>

#include <ctx.h>
#include <git-shared.h>
#include <ui-shared.h>


static void print_log_start( struct strbuf *sb )
{
  if( !sb ) return;
  strbuf_addstr( sb, "\n" );
  strbuf_addf( sb, "              <div class=\"repo-log-header\">\n" );
  strbuf_addf( sb, "                <div class=\"row\">\n" );
  strbuf_addf( sb, "                  <div class=\"col-date\"><div class=\"log-date\">Date</div></div>\n" );
  strbuf_addf( sb, "                  <div class=\"col-cmsg\"><div class=\"log-cmsg trunc\">Commit Message</div></div>\n" );
  strbuf_addf( sb, "                  <div class=\"col-rev\"><div class=\"log-rev trunc\">Rev</div></div>\n" );
  strbuf_addf( sb, "                  <div class=\"col-author\"><div class=\"log-author trunc\">Author</div></div>\n" );
  strbuf_addf( sb, "                </div>\n" );
  strbuf_addf( sb, "              </div>\n\n" );
  strbuf_addf( sb, "              <div class=\"log\">\n\n" );
}

static void print_log_stop( struct strbuf *sb )
{
  if( !sb ) return;
  strbuf_addf( sb, "              </div> <!-- End of Log -->\n\n" );
}

static void print_direction( struct strbuf *sb, const char *relative_path, int reminder, int limit )
{
  char *path = NULL;
  int   page_size = atoi( ctx.vars.page_size );
  int   prev, next;

  if( !sb ) return;

  if( relative_path && *relative_path )
  {
    path = (char *)xmalloc( strlen( relative_path ) + 2 );
    path[0] = '/';
    sprintf( (char *)&path[1], relative_path );
  }
  else
  {
    path = (char *)xmalloc( 1 );
    path[0] = '\0';
  }

  strbuf_addf( sb, "              <div class=\"log-direction\">\n" );
  strbuf_addf( sb, "                <div class=\"row\">\n" );
  strbuf_addf( sb, "                  <div class=\"left col-prev\">\n" );
  strbuf_addf( sb, "                    <div class=\"prev-log-direction\">\n" );

  if( ctx.query.ofs )
  {
    prev = ctx.query.ofs - page_size;
    if( prev < 0 ) prev = 0;

    if( ctx.env.query_string && *ctx.env.query_string )
      strbuf_addf( sb, "                      <a href=\"/%s%s/?ofs=%d&%s\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.name, path, prev, ctx.env.query_string );
    else
      strbuf_addf( sb, "                      <a href=\"/%s%s/?ofs=%d\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.name, path, prev );
  }

  strbuf_addf( sb, "                    </div>\n" );
  strbuf_addf( sb, "                  </div>\n" );
  strbuf_addf( sb, "                  <div class=\"right col-next\">\n" );
  strbuf_addf( sb, "                    <div class=\"next-log-direction\">\n" );

  if( reminder )
  {
    next = ctx.query.ofs + page_size;

    if( ctx.env.query_string && *ctx.env.query_string )
      strbuf_addf( sb, "                      <a href=\"/%s%s/?ofs=%d&%s\">Next &nbsp;&#x226b;</a>\n", ctx.repo.name, path, next, ctx.env.query_string );
    else
      strbuf_addf( sb, "                      <a href=\"/%s%s/?ofs=%d\">Next &nbsp;&#x226b;</a>\n", ctx.repo.name, path, next );
  }

  strbuf_addf( sb, "                    </div>\n" );
  strbuf_addf( sb, "                  </div>\n" );
  strbuf_addf( sb, "                </div>\n" );
  strbuf_addf( sb, "              </div>\n" );

  free( path );
}

static void print_commit_line( struct strbuf *sb, git_commit *commit, const char *relative_path, const char *revision )
{
  char *path = NULL;

  if( !sb || !commit ) return;

  if( relative_path && *relative_path )
  {
    path = (char *)xmalloc( strlen( relative_path ) + 2 );
    path[0] = '/';
    sprintf( (char *)&path[1], relative_path );
  }
  else
  {
    path = (char *)xmalloc( 1 );
    path[0] = '\0';
  }

  {
    char id[GIT_OID_HEXSZ+1] = { 0 };

    const git_oid       *oid     = git_commit_id( commit );
    const char          *message = git_commit_summary( commit );
    git_time_t           date    = git_commit_time( commit );
    int                  offset  = git_commit_time_offset( commit );
    const git_signature *author  = git_commit_author( commit );

    const char *query_string = ctx_remove_query_param( ctx.env.query_string, "rev" );

    query_string = ctx_remove_query_param( query_string, "op" );

    offset = (offset % 60) + ((offset / 60) * 100);

    if( git_oid_fmt( (char *)&id[0], oid ) < 0 || strcmp( (char *)&id[0], revision ) ) return;


    strbuf_addf( sb, "                <div class=\"row\">\n" );
    if( date != -1 )
    {
      strbuf_addf( sb, "                  <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"log-date trunc\">" );
      cgit_print_age( sb, (time_t)date, offset, 0 );
      strbuf_addf( sb, "</div></div>\n" );
    }
    else
    {
      strbuf_addf( sb, "                  <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"log-date trunc\">unknown</div></div>\n" );
    }
    strbuf_addf( sb, "                  <div class=\"col-cmsg\"><div onclick=\"trunc(this)\" class=\"log-cmsg trunc\">%s</div></div>\n", (char *)message );
    if( query_string && *query_string )
    {
      strbuf_addf( sb, "                  <div class=\"col-rev\"><div onclick=\"trunc(this)\" class=\"log-rev trunc\"><a title=\"Compare with Previous\" href=\"/%s%s/?op=diff&rev=%s&%s\"><span class=\"rev-short\">%0.8s</span></a></div></div>\n", ctx.repo.name, path, (char *)revision, query_string, (char *)revision );
    }
    else
    {
      strbuf_addf( sb, "                  <div class=\"col-rev\"><div onclick=\"trunc(this)\" class=\"log-rev trunc\"><a title=\"Compare with Previous\" href=\"/%s%s/?op=diff&rev=%s\"><span class=\"rev-short\">%0.8s</span></a></div></div>\n", ctx.repo.name, path, (char *)revision, (char *)revision );
    }
    strbuf_addf( sb, "                  <div class=\"col-author\"><div onclick=\"trunc(this)\" class=\"log-author trunc\">%s</div></div>\n", (char *)author->name );
    strbuf_addf( sb, "                </div>\n\n" );
  }

  free( path );
}


static void cgit_print_log( struct strbuf *sb, const char *relative_path, const char *revision )
{
  struct cgit_hex_commits *commits = NULL;
  size_t i, limit, reminder, page_size = (size_t)atoi( ctx.vars.page_size );

  if( !sb ) return;

  print_log_start( sb );

  cgit_fill_commits_list( &commits, ctx.query.ofs, relative_path, revision );

  limit = min( page_size, commits->len );

  for( i = 0; i < limit; ++i )
  {
    git_commit *commit = NULL;
    commit = lookup_commit_by_hex( commits->hex[i] );

    print_commit_line( sb, commit, relative_path, (const char *)commits->hex[i] );

    git_commit_free( commit );
  }

  reminder = commits->len - limit;

  cgit_hex_commits_free( commits );

  print_log_stop( sb );

  if( !(!ctx.query.ofs && !reminder) )
    print_direction( sb, relative_path, (int)reminder, (int)limit );
}


void cgit_print_log_page( void )
{
  FILE  *fp;
  struct strbuf buf = STRBUF_INIT;

  fp = xfopen( ctx.page.header, "r" );
  (void)strbuf_env_fread( &buf, fp );
  fclose( fp );

  strbuf_addf( &buf, "        <div class=\"content segment\">\n" );
  strbuf_addf( &buf, "          <div class=\"container\">\n" );
  strbuf_addf( &buf, "            <div class=\"cgit-main-content\">\n" );

  if( ctx.repo.name )
  {
    cgit_print_log( &buf, ctx.repo.relative_path, (!strcmp( ctx.query.rev, "0" )) ? (const char *)&ctx.repo.relative_info.revision[0] : ctx.query.rev );
  }
  else
  {
    strbuf_addf( &buf, "              <h1>Requested resource cannot be shown</h1>\n" );
    strbuf_addf( &buf, "              <p class='leading'>Repository '%s' not found.</p>\n", ctx.repo.name );
  }

  strbuf_addf( &buf, "            </div> <!-- End of cgit-main-content -->\n" );
  strbuf_addf( &buf, "          </div> <!-- End of container -->\n" );
  strbuf_addf( &buf, "        </div> <!-- End of content segment -->\n" );

  fp = xfopen( ctx.page.footer, "r" );
  (void)strbuf_env_fread( &buf, fp );
  fclose( fp );

  ctx.page.size = buf.len;
  cgit_print_http_headers();
  strbuf_write( &buf, STDOUT_FILENO );
  strbuf_release( &buf );
}