summaryrefslogtreecommitdiff
path: root/csvncgi/ui-log.c
blob: a97dd06af4c91fb6607830ecb972eb5b3088b47b (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#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 <libxml/parser.h>
#include <libxml/tree.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 <ui-shared.h>


static void xml_csvn_log( struct strbuf *sb, const struct strbuf *buf, const char *relative_path )
{
  xmlDocPtr doc = NULL;
  xmlNode *root = NULL;
  char    *path = NULL;

  int count = 0, printed = 0, reminder = 0, page_size = 200;

  if( !sb || !sb->len || !buf || !buf->buf ) 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';
  }

  page_size = atoi( ctx.vars.page_size );

  LIBXML_TEST_VERSION

  doc = xmlReadMemory( buf->buf, buf->len, "log.xml", NULL, 0 );
  if( !doc )
  {
    html_fatal( "cannot parse svn log.xml" );
    return;
  }

  root = xmlDocGetRootElement( doc );
  if( !root )
  {
    free( path );
    xmlFreeDoc( doc );
    xmlCleanupParser();
    return;
  }

  if( !strcmp( "log", (char *)root->name ) )
  {
    xmlNode *node = NULL;

    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" );

    /**************************
      Print directories first:
     */
    for( node = root->children; node; node = node->next )
    {
      if( node->type == XML_ELEMENT_NODE && !strcmp( "logentry", (char *)node->name ) )
      {
        xmlNode *param = NULL;
        xmlChar *date = NULL, *cmsg = NULL, *revision = NULL, *author = NULL;

        revision = xmlGetProp( node, (const unsigned char *)"revision" );

        for( param = node->children; param; param= param->next )
        {
          if( param->type == XML_ELEMENT_NODE && !strcmp( "date", (char *)param->name ) )
          {
            date = xmlNodeGetContent( param );
          }
          if( param->type == XML_ELEMENT_NODE && !strcmp( "msg", (char *)param->name ) )
          {
            cmsg = xmlNodeGetContent( param );
          }
          if( param->type == XML_ELEMENT_NODE && !strcmp( "author", (char *)param->name ) )
          {
            author = xmlNodeGetContent( param );
          }

        } /* End for entry parameters */

        if( date && cmsg && revision && author )
        {
          struct tm   tm;
          time_t      time = -1;
          const char *query_string = ctx_remove_query_param( ctx.env.query_string, "rev" );

          query_string = ctx_remove_query_param( query_string, "op" );

          time = parse_date( &tm, (const char *)date );

          ++count;
          if( count > ctx.query.ofs && printed < page_size )
          {

            strbuf_addf( sb, "                <div class=\"row\">\n" );
            if( time != -1 )
            {
              strbuf_addf( sb, "                  <div class=\"col-date\"><div onclick=\"trunc(this)\" class=\"log-date trunc\">" );
              csvn_print_age( sb, time, 0, 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 *)cmsg );
            if( query_string && *query_string )
            {
              if( ctx.repo.repo_root && *ctx.repo.repo_root )
              {
                strbuf_addf( sb, "                  <div class=\"col-rev\"><div onclick=\"trunc(this)\" class=\"log-rev trunc\"><a title=\"Compare with Previous\" href=\"/%s/%s%s/?op=diff&rev=%s&%s\">%s</a></div></div>\n", ctx.repo.repo_root, 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&%s\">%s</a></div></div>\n", ctx.repo.name, path, (char *)revision, query_string, (char *)revision );
              }
            }
            else
            {
              if( ctx.repo.repo_root && *ctx.repo.repo_root )
              {
                strbuf_addf( sb, "                  <div class=\"col-rev\"><div onclick=\"trunc(this)\" class=\"log-rev trunc\"><a title=\"Compare with Previous\" href=\"/%s/%s%s/?op=diff&rev=%s\">%s</a></div></div>\n", ctx.repo.repo_root, path, ctx.repo.name, path, (char *)revision, (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\">%s</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 );
            strbuf_addf( sb, "                </div>\n\n" );

            ++printed;
          }

          xmlFree( date );
          xmlFree( cmsg );
          xmlFree( revision );
          xmlFree( author );
        }
        else
        {
          if( date )     xmlFree( date );
          if( cmsg )     xmlFree( cmsg );
          if( revision ) xmlFree( revision );
          if( author )   xmlFree( author );
        }
      }
    }

    strbuf_addf( sb, "              </div> <!-- End of Log -->\n\n" );

    /********************************
      Print log direction:
     */
    reminder = count - ctx.query.ofs - printed;
    if( page_size < count )
    {
      int prev, next;

      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 )
        {
          if( ctx.repo.repo_root && *ctx.repo.repo_root )
          {
            strbuf_addf( sb, "                      <a href=\"/%s/%s%s/?ofs=%d&%s\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.repo_root, ctx.repo.name, path, prev, ctx.env.query_string );
          }
          else
          {
            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
        {
          if( ctx.repo.repo_root && *ctx.repo.repo_root )
          {
            strbuf_addf( sb, "                      <a href=\"/%s/%s%s/?ofs=%d\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.repo_root, ctx.repo.name, path, prev );
          }
          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 )
        {
          if( ctx.repo.repo_root && *ctx.repo.repo_root )
          {
            strbuf_addf( sb, "                      <a href=\"/%s/%s%s/?ofs=%d&%s\">Next &nbsp;&#x226b;</a>\n", ctx.repo.repo_root, ctx.repo.name, path, next, ctx.env.query_string );
          }
          else
          {
            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
        {
          if( ctx.repo.repo_root && *ctx.repo.repo_root )
          {
            strbuf_addf( sb, "                      <a href=\"/%s/%s%s/?ofs=%d\">Next &nbsp;&#x226b;</a>\n", ctx.repo.repo_root, ctx.repo.name, path, next );
          }
          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" );
    }
    /*
      End of printing log direction.
     ********************************/

  }

  free( path );
  xmlFreeDoc(doc);
  xmlCleanupParser();
}

static void csvn_print_log( struct strbuf *sb, const char *relative_path, int revision )
{
  const char *co_prefix = ctx.repo.checkout_ro_prefix;
  const char *name      = ctx.repo.name;
  const char *repo_root = ctx.repo.repo_root;
  char *path = NULL;

  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';
  }

  if( co_prefix )
  {
    char repo_path[PATH_MAX] = { 0 };
    char cmd[PATH_MAX];
    struct strbuf buf = STRBUF_INIT;
    pid_t p = (pid_t) -1;
    int   rc;

    if( repo_root && *repo_root )
    {
      strcat( (char *)&repo_path[0], repo_root );
      strcat( (char *)&repo_path[0], "/" );
    }
    strcat( (char *)&repo_path[0], name );

    if( revision )
      snprintf( (char *)&cmd[0], 1024,
                "svn log --revision %d:0 --xml %s/%s%s 2>/dev/null",
                revision, co_prefix, (char *)&repo_path[0], path );
    else
      snprintf( (char *)&cmd[0], 1024,
                "svn log --xml %s/%s%s 2>/dev/null",
                co_prefix, (char *)&repo_path[0], path );
    p = sys_exec_command( &buf, cmd );
    rc = sys_wait_command( p, NULL );
    if( rc != 0 )
    {
      strbuf_release( &buf );
      free( path );
      return;
    }

    xml_csvn_log( sb, (const struct strbuf *)&buf, relative_path );

    strbuf_release( &buf );
  }

  free( path );
  return;
}


void csvn_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=\"csvn-main-content\">\n" );

  if( ctx.repo.name )
  {
    csvn_print_log( &buf, ctx.repo.relative_path, 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 csvn-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;
  csvn_print_http_headers();
  strbuf_write( &buf, STDOUT_FILENO );
  strbuf_release( &buf );
}