You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@subversion.apache.org by ju...@apache.org on 2012/11/21 00:41:49 UTC
svn commit: r1411927 - in /subversion/trunk/subversion/libsvn_diff: diff.h
diff_file.c diff_memory.c util.c
Author: julianfoad
Date: Tue Nov 20 23:41:48 2012
New Revision: 1411927
URL: http://svn.apache.org/viewvc?rev=1411927&view=rev
Log:
Factor out some unidiff formatting functions from two places in libsvn_diff.
* subversion/libsvn_diff/diff.h,
subversion/libsvn_diff/util.c
(svn_diff__unified_no_eol_msg, svn_diff__unified_write_hunk_header,
svn_diff__unidiff_write_header): New functions, factored out from below.
* subversion/libsvn_diff/diff_file.c
(output_unified_line): Use svn_diff__unified_no_eol_msg().
(output_unified_flush_hunk): Use svn_diff__unified_write_hunk_header().
(svn_diff_file_output_unified3): Use svn_diff__unidiff_write_header().
* subversion/libsvn_diff/diff_memory.c
(output_unified_token_range): Use svn_diff__unified_no_eol_msg().
(output_unified_flush_hunk): Use svn_diff__unified_write_hunk_header().
(svn_diff_mem_string_output_unified2): Use svn_diff__unidiff_write_header().
Modified:
subversion/trunk/subversion/libsvn_diff/diff.h
subversion/trunk/subversion/libsvn_diff/diff_file.c
subversion/trunk/subversion/libsvn_diff/diff_memory.c
subversion/trunk/subversion/libsvn_diff/util.c
Modified: subversion/trunk/subversion/libsvn_diff/diff.h
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_diff/diff.h?rev=1411927&r1=1411926&r2=1411927&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_diff/diff.h (original)
+++ subversion/trunk/subversion/libsvn_diff/diff.h Tue Nov 20 23:41:48 2012
@@ -184,5 +184,48 @@ svn_diff__normalize_buffer(char **tgt,
const char *buf,
const svn_diff_file_options_t *opts);
+/* Set *OUT_STR to a newline followed by a "\ No newline at end of file" line.
+ *
+ * The text will be encoded into HEADER_ENCODING.
+ */
+svn_error_t *
+svn_diff__unified_append_no_newline_msg(svn_stringbuf_t *stringbuf,
+ const char *header_encoding,
+ apr_pool_t *scratch_pool);
+
+/* Write a unidiff hunk header to OUTPUT_STREAM.
+ *
+ * The header will use HUNK_DELIMITER (which should usually be "@@") before
+ * and after the line-number ranges which are formed from OLD_START,
+ * OLD_LENGTH, NEW_START and NEW_LENGTH. If HUNK_EXTRA_CONTEXT is not NULL,
+ * it will be written after the final delimiter, with an intervening space.
+ *
+ * The text will be encoded into HEADER_ENCODING.
+ */
+svn_error_t *
+svn_diff__unified_write_hunk_header(svn_stream_t *output_stream,
+ const char *header_encoding,
+ const char *hunk_delimiter,
+ apr_off_t old_start,
+ apr_off_t old_length,
+ apr_off_t new_start,
+ apr_off_t new_length,
+ const char *hunk_extra_context,
+ apr_pool_t *scratch_pool);
+
+/* Write a unidiff "---" and "+++" header to OUTPUT_STREAM.
+ *
+ * Write "---" followed by a space and OLD_HEADER and a newline,
+ * then "+++" followed by a space and NEW_HEADER and a newline.
+ *
+ * The text will be encoded into HEADER_ENCODING.
+ */
+svn_error_t *
+svn_diff__unidiff_write_header(svn_stream_t *output_stream,
+ const char *header_encoding,
+ const char *old_header,
+ const char *new_header,
+ apr_pool_t *scratch_pool);
+
#endif /* DIFF_H */
Modified: subversion/trunk/subversion/libsvn_diff/diff_file.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_diff/diff_file.c?rev=1411927&r1=1411926&r2=1411927&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_diff/diff_file.c (original)
+++ subversion/trunk/subversion/libsvn_diff/diff_file.c Tue Nov 20 23:41:48 2012
@@ -1491,14 +1491,8 @@ output_unified_line(svn_diff__file_outpu
if (bytes_processed && (type != svn_diff__file_output_unified_skip)
&& ! had_cr)
{
- const char *out_str;
- SVN_ERR(svn_utf_cstring_from_utf8_ex2(
- &out_str,
- /* The string below is intentionally not marked for translation,
- for wider interoperability with patch(1) programs. */
- APR_EOL_STR "\\ No newline at end of file" APR_EOL_STR,
- baton->header_encoding, baton->pool));
- svn_stringbuf_appendcstr(baton->hunk, out_str);
+ SVN_ERR(svn_diff__unified_append_no_newline_msg(
+ baton->hunk, baton->header_encoding, baton->pool));
}
baton->length[idx] = 0;
@@ -1538,41 +1532,13 @@ output_unified_flush_hunk(svn_diff__file
baton->hunk_start[i]++;
}
- /* Output the hunk header. If the hunk length is 1, the file is a one line
- file. In this case, surpress the number of lines in the hunk (it is
- 1 implicitly)
- */
- SVN_ERR(svn_stream_printf_from_utf8(baton->output_stream,
- baton->header_encoding,
- baton->pool,
- "@@ -%" APR_OFF_T_FMT,
- baton->hunk_start[0]));
- if (baton->hunk_length[0] != 1)
- {
- SVN_ERR(svn_stream_printf_from_utf8(baton->output_stream,
- baton->header_encoding,
- baton->pool, ",%" APR_OFF_T_FMT,
- baton->hunk_length[0]));
- }
-
- SVN_ERR(svn_stream_printf_from_utf8(baton->output_stream,
- baton->header_encoding,
- baton->pool, " +%" APR_OFF_T_FMT,
- baton->hunk_start[1]));
- if (baton->hunk_length[1] != 1)
- {
- SVN_ERR(svn_stream_printf_from_utf8(baton->output_stream,
- baton->header_encoding,
- baton->pool, ",%" APR_OFF_T_FMT,
- baton->hunk_length[1]));
- }
-
- SVN_ERR(svn_stream_printf_from_utf8(baton->output_stream,
- baton->header_encoding,
- baton->pool, " @@%s%s" APR_EOL_STR,
- baton->hunk_extra_context[0]
- ? " " : "",
- baton->hunk_extra_context));
+ /* Write the hunk header */
+ SVN_ERR(svn_diff__unified_write_hunk_header(
+ baton->output_stream, baton->header_encoding, "@@",
+ baton->hunk_start[0], baton->hunk_length[0],
+ baton->hunk_start[1], baton->hunk_length[1],
+ baton->hunk_extra_context,
+ baton->pool));
/* Output the hunk content */
hunk_len = baton->hunk->len;
@@ -1821,10 +1787,9 @@ svn_diff_file_output_unified3(svn_stream
(&modified_header, modified_path, pool));
}
- SVN_ERR(svn_stream_printf_from_utf8(output_stream, header_encoding, pool,
- "--- %s" APR_EOL_STR
- "+++ %s" APR_EOL_STR,
- original_header, modified_header));
+ SVN_ERR(svn_diff__unidiff_write_header(
+ output_stream, header_encoding,
+ original_header, modified_header, pool));
SVN_ERR(svn_diff_output(diff, &baton,
&svn_diff__file_output_unified_vtable));
Modified: subversion/trunk/subversion/libsvn_diff/diff_memory.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_diff/diff_memory.c?rev=1411927&r1=1411926&r2=1411927&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_diff/diff_memory.c (original)
+++ subversion/trunk/subversion/libsvn_diff/diff_memory.c Tue Nov 20 23:41:48 2012
@@ -417,14 +417,8 @@ output_unified_token_range(output_baton_
}
if (past_last == source->tokens->nelts && source->ends_without_eol)
{
- const char *out_str;
- SVN_ERR(svn_utf_cstring_from_utf8_ex2
- (&out_str,
- /* The string below is intentionally not marked for translation,
- for wider interoperability with patch(1) programs. */
- APR_EOL_STR "\\ No newline at end of file" APR_EOL_STR,
- btn->header_encoding, btn->pool));
- svn_stringbuf_appendcstr(btn->hunk, out_str);
+ SVN_ERR(svn_diff__unified_append_no_newline_msg(
+ btn->hunk, btn->header_encoding, btn->pool));
}
if (tokens == 0)
@@ -458,45 +452,19 @@ output_unified_flush_hunk(output_baton_t
if (hunk_delimiter == NULL)
hunk_delimiter = "@@";
- /* Write the hunk header */
+ /* Convert our 0-based line numbers into unidiff 1-based numbers */
if (baton->hunk_length[0] > 0)
- /* Convert our 0-based line numbers into unidiff 1-based numbers */
baton->hunk_start[0]++;
- SVN_ERR(svn_stream_printf_from_utf8(
- baton->output_stream, baton->header_encoding,
- baton->pool,
- /* Hunk length 1 is implied, don't show the
- length field if we have a hunk that long */
- (baton->hunk_length[0] == 1)
- ? ("%s -%" APR_OFF_T_FMT)
- : ("%s -%" APR_OFF_T_FMT ",%" APR_OFF_T_FMT),
- hunk_delimiter,
- baton->hunk_start[0], baton->hunk_length[0]));
-
if (baton->hunk_length[1] > 0)
- /* Convert our 0-based line numbers into unidiff 1-based numbers */
baton->hunk_start[1]++;
-
- /* Hunk length 1 is implied, don't show the
- length field if we have a hunk that long */
- if (baton->hunk_length[1] == 1)
- {
- SVN_ERR(svn_stream_printf_from_utf8(
- baton->output_stream, baton->header_encoding,
- baton->pool,
- " +%" APR_OFF_T_FMT " %s" APR_EOL_STR,
- baton->hunk_start[1], hunk_delimiter));
- }
- else
- {
- SVN_ERR(svn_stream_printf_from_utf8(
- baton->output_stream, baton->header_encoding,
- baton->pool,
- " +%" APR_OFF_T_FMT ",%" APR_OFF_T_FMT " %s" APR_EOL_STR,
- baton->hunk_start[1], baton->hunk_length[1],
- hunk_delimiter));
- }
+ /* Write the hunk header */
+ SVN_ERR(svn_diff__unified_write_hunk_header(
+ baton->output_stream, baton->header_encoding, hunk_delimiter,
+ baton->hunk_start[0], baton->hunk_length[0],
+ baton->hunk_start[1], baton->hunk_length[1],
+ NULL /* hunk_extra_context */,
+ baton->pool));
hunk_len = baton->hunk->len;
SVN_ERR(svn_stream_write(baton->output_stream,
@@ -601,12 +569,9 @@ svn_diff_mem_string_output_unified2(svn_
if (with_diff_header)
{
- SVN_ERR(svn_stream_printf_from_utf8(output_stream,
- header_encoding, pool,
- "--- %s" APR_EOL_STR
- "+++ %s" APR_EOL_STR,
- original_header,
- modified_header));
+ SVN_ERR(svn_diff__unidiff_write_header(
+ output_stream, header_encoding,
+ original_header, modified_header, pool));
}
SVN_ERR(svn_diff_output(diff, &baton,
Modified: subversion/trunk/subversion/libsvn_diff/util.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_diff/util.c?rev=1411927&r1=1411926&r2=1411927&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_diff/util.c (original)
+++ subversion/trunk/subversion/libsvn_diff/util.c Tue Nov 20 23:41:48 2012
@@ -29,6 +29,7 @@
#include "svn_diff.h"
#include "svn_types.h"
#include "svn_ctype.h"
+#include "svn_utf.h"
#include "svn_version.h"
#include "diff.h"
@@ -332,6 +333,84 @@ svn_diff__normalize_buffer(char **tgt,
#undef COPY_INCLUDED_SECTION
}
+svn_error_t *
+svn_diff__unified_append_no_newline_msg(svn_stringbuf_t *stringbuf,
+ const char *header_encoding,
+ apr_pool_t *scratch_pool)
+{
+ const char *out_str;
+
+ SVN_ERR(svn_utf_cstring_from_utf8_ex2(
+ &out_str,
+ /* The string below is intentionally not marked for translation,
+ for wider interoperability with patch(1) programs. */
+ APR_EOL_STR "\\ No newline at end of file" APR_EOL_STR,
+ header_encoding, scratch_pool));
+ svn_stringbuf_appendcstr(stringbuf, out_str);
+ return SVN_NO_ERROR;
+}
+
+svn_error_t *
+svn_diff__unified_write_hunk_header(svn_stream_t *output_stream,
+ const char *header_encoding,
+ const char *hunk_delimiter,
+ apr_off_t old_start,
+ apr_off_t old_length,
+ apr_off_t new_start,
+ apr_off_t new_length,
+ const char *hunk_extra_context,
+ apr_pool_t *scratch_pool)
+{
+ SVN_ERR(svn_stream_printf_from_utf8(output_stream, header_encoding,
+ scratch_pool,
+ "%s -%" APR_OFF_T_FMT,
+ hunk_delimiter, old_start));
+ /* If the hunk length is 1, suppress the number of lines in the hunk
+ * (it is 1 implicitly) */
+ if (old_length != 1)
+ {
+ SVN_ERR(svn_stream_printf_from_utf8(output_stream, header_encoding,
+ scratch_pool,
+ ",%" APR_OFF_T_FMT, old_length));
+ }
+
+ SVN_ERR(svn_stream_printf_from_utf8(output_stream, header_encoding,
+ scratch_pool,
+ " +%" APR_OFF_T_FMT, new_start));
+ if (new_length != 1)
+ {
+ SVN_ERR(svn_stream_printf_from_utf8(output_stream, header_encoding,
+ scratch_pool,
+ ",%" APR_OFF_T_FMT, new_length));
+ }
+
+ if (hunk_extra_context == NULL)
+ hunk_extra_context = "";
+ SVN_ERR(svn_stream_printf_from_utf8(output_stream, header_encoding,
+ scratch_pool,
+ " %s%s%s" APR_EOL_STR,
+ hunk_delimiter,
+ hunk_extra_context[0] ? " " : "",
+ hunk_extra_context));
+ return SVN_NO_ERROR;
+}
+
+svn_error_t *
+svn_diff__unidiff_write_header(svn_stream_t *output_stream,
+ const char *header_encoding,
+ const char *old_header,
+ const char *new_header,
+ apr_pool_t *scratch_pool)
+{
+ SVN_ERR(svn_stream_printf_from_utf8(output_stream, header_encoding,
+ scratch_pool,
+ "--- %s" APR_EOL_STR
+ "+++ %s" APR_EOL_STR,
+ old_header,
+ new_header));
+ return SVN_NO_ERROR;
+}
+
/* Return the library version number. */
const svn_version_t *