File: cut-diff.c

package info (click to toggle)
cutter-testing-framework 1.1.7-1.2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 18,556 kB
  • sloc: ansic: 79,864; xml: 38,680; sh: 10,266; makefile: 2,324; ruby: 845; cpp: 697
file content (190 lines) | stat: -rw-r--r-- 5,873 bytes parent folder | download
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
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 *  Copyright (C) 2009-2011  Kouhei Sutou <kou@clear-code.com>
 *
 *  This library is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif /* HAVE_CONFIG_H */

#include <stdlib.h>

#include <glib/gi18n.h>
#include "cut-main.h"
#include "cut-colorize-differ.h"
#include "cut-unified-differ.h"
#include "cut-console.h"
#include "cut-console-diff-writer.h"

static gboolean use_color = FALSE;
static gboolean unified_diff = FALSE;
static gint context_lines = -1;
static gchar **labels = NULL;

static gboolean
print_version (const gchar *option_name, const gchar *value,
               gpointer data, GError **error)
{
    g_print("%s\n", VERSION);
    exit(EXIT_SUCCESS);
    return TRUE;
}

static gboolean
parse_color_arg (const gchar *option_name, const gchar *value,
                 gpointer data, GError **error)
{
    return cut_console_parse_color_arg(option_name, value,
                                       &use_color, error);
}

static const GOptionEntry option_entries[] =
{
    {"version", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, print_version,
     N_("Show version"), NULL},
    {"color", 'c', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
     parse_color_arg, N_("Output diff with colors"),
     "[yes|true|no|false|auto]"},
    {"unified", 'u', 0, G_OPTION_ARG_NONE, &unified_diff,
     N_("Use unified diff format"), NULL},
    {"context-lines", '\0', 0, G_OPTION_ARG_INT, &context_lines,
     N_("Use LINES as diff context lines"), "LINES"},
    {"label", 'L', 0, G_OPTION_ARG_STRING_ARRAY, &labels,
     N_("Use LABEL as label"), "LABEL"},
    {NULL}
};

static gboolean
pre_parse (GOptionContext *context, GOptionGroup *group, gpointer data,
           GError **error)
{
    use_color = cut_console_guess_color_usability();

    return TRUE;
}

static void
print_diff_header (CutDiffWriter *writer,
                   const gchar *from_label, const gchar *to_label)
{
    cut_diff_writer_mark_line(writer, "---", " ", from_label,
                              CUT_DIFF_WRITER_TAG_DELETED_MARK);
    cut_diff_writer_mark_line(writer, "+++", " ", to_label,
                              CUT_DIFF_WRITER_TAG_INSERTED_MARK);
}

static gboolean
print_diff (const gchar *from, const gchar *to)
{
    GError *error = NULL;
    gchar *from_contents, *to_contents;
    CutDiffer *differ;
    CutDiffWriter *writer;
    const gchar *from_label = from;
    const gchar *to_label = to;

    if (!g_file_get_contents(from, &from_contents, NULL, &error)) {
        g_print("%s\n", error->message);
        g_error_free(error);
        return FALSE;
    }

    if (!g_file_get_contents(to, &to_contents, NULL, &error)) {
        g_print("%s\n", error->message);
        g_error_free(error);
        g_free(from_contents);
        return FALSE;
    }

    if (labels[0]) {
        from_label = labels[0];
        if (labels[1]) {
            to_label = labels[1];
        }
    }

    writer = cut_console_diff_writer_new(use_color);
    if (unified_diff) {
        differ = cut_unified_differ_new(from_contents, to_contents);
        cut_unified_differ_set_from_label(differ, from_label);
        cut_unified_differ_set_to_label(differ, to_label);
    } else {
        if (use_color) {
            differ = cut_colorize_differ_new(from_contents, to_contents);
        } else {
            differ = cut_readable_differ_new(from_contents, to_contents);
        }
        print_diff_header(writer, from_label, to_label);
    }
    if (context_lines > 0)
        cut_differ_set_context_size(differ, context_lines);
    cut_differ_diff(differ, writer);
    g_object_unref(writer);
    g_object_unref(differ);

    return TRUE;
}

int
main (int argc, char *argv[])
{
    gboolean success = TRUE;
    GOptionContext *option_context;
    GOptionGroup *main_group;
    GError *error = NULL;

    cut_init_environment(&argc, &argv);

    option_context = g_option_context_new(N_("FILE_FROM FILE_TO"));
    g_option_context_set_help_enabled(option_context, TRUE);
    g_option_context_set_ignore_unknown_options(option_context, FALSE);

    main_group = g_option_group_new(NULL, NULL, NULL, option_context, NULL);
    g_option_group_add_entries(main_group, option_entries);
    g_option_group_set_parse_hooks(main_group, pre_parse, NULL);
    g_option_group_set_translation_domain(main_group, GETTEXT_PACKAGE);
    g_option_context_set_main_group(option_context, main_group);

    if (!g_option_context_parse(option_context, &argc, &argv, &error)) {
        g_print("%s\n", error->message);
        g_error_free(error);
        g_option_context_free(option_context);
        exit(EXIT_FAILURE);
    }

    if (argc != 3) {
        gint help_argc = 2;
        gchar *help_argv[2];
        gchar **help_argv_p;
        help_argv[0] = argv[0];
        help_argv[1] = "--help";
        help_argv_p = help_argv;
        g_option_context_parse(option_context, &help_argc, &help_argv_p, NULL);
        g_option_context_free(option_context);
        exit(EXIT_FAILURE);
    }

    success = print_diff(argv[1], argv[2]);

    cut_quit_environment();

    return success ? EXIT_SUCCESS : EXIT_FAILURE;
}

/*
vi:ts=4:nowrap:ai:expandtab:sw=4
*/