summaryrefslogtreecommitdiff
path: root/csvncgi/ui-diff.c
blob: e74de6c165aea9b113f46a384b4f14e974f16517 (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
#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 <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 csvn_print_diff( 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;

  if( !sb || !relative_path || !revision ) return;

  strbuf_addf( sb, "<pre><code class='language-Diff'>" );

  if( co_prefix )
  {
    char repo_path[PATH_MAX] = { 0 };
    char cmd[PATH_MAX];
    struct strbuf buf = STRBUF_INIT;
    char  *raw = NULL;
    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 );

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

    raw = strbuf_detach( &buf, NULL );
    strbuf_addstr_xml_quoted( &buf, (const char *)raw );
    free( raw );

    strbuf_addbuf( sb, (const struct strbuf *)&buf );
    strbuf_release( &buf );
  }

  strbuf_addstr( sb, "\n</code></pre>\n" );

  return;
}

void csvn_print_diff_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 )
  {
    if( ctx.query.rev )
    {
      csvn_print_diff( &buf, ctx.repo.relative_path, ctx.query.rev );
    }
    else
    {
      strbuf_addf( &buf, "              <h1>Requested diff cannot be shown</h1>\n" );
      strbuf_addf( &buf, "              <p class='leading'>Revision for diff operation is not defined.</p>\n" );
    }
  }
  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 );
}