-
Notifications
You must be signed in to change notification settings - Fork 28
Fix interdiff output when using --color=always #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d51e74a
Finally create a working test-case for #119
twaugh 111cc97
Create our own ANSI codes for colours instead of passing through --color
twaugh 13548ef
Use variadic function and puts() line colour prefix/suffix
twaugh 5ec12f8
Incorporate Sultan Alsawaf's suggestions from PR review
twaugh 126b622
Remove some unnecessary braces
twaugh 05c4b2d
Remove another unnecessary extra code block
twaugh 151b978
More improvements from PR review
twaugh badc0f1
Avoid name collision with LINE_MAX
twaugh d30408f
Update NEWS
twaugh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ | |
| #endif /* HAVE_ERROR_H */ | ||
| #include <ctype.h> | ||
| #include <errno.h> | ||
| #include <stdarg.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
@@ -58,6 +59,27 @@ | |
| #define PATCH "patch" | ||
| #endif | ||
|
|
||
| #define COLOR_RESET "\033[0m" | ||
|
|
||
| /* Line type for coloring */ | ||
| enum line_type { | ||
| LINE_FILE = 0, | ||
twaugh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| LINE_HEADER, | ||
| LINE_HUNK, | ||
| LINE_ADDED, | ||
| LINE_REMOVED, | ||
twaugh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| /* ANSI color codes for diff output */ | ||
| static char*color_codes[] = { | ||
twaugh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
twaugh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| [LINE_FILE] = "\033[1m", /* Bold for filenames */ | ||
| [LINE_HEADER] = "\033[1m", /* Bold for headers */ | ||
| [LINE_HUNK] = "\033[36m", /* Cyan for hunk headers */ | ||
| [LINE_ADDED] = "\033[32m", /* Green for added lines */ | ||
| [LINE_REMOVED] = "\033[31m", /* Red for removed lines */ | ||
twaugh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| /* This can be invoked as interdiff, combinediff, or flipdiff. */ | ||
| static enum { | ||
| mode_inter, | ||
|
|
@@ -98,23 +120,57 @@ static int ignore_components = 0; | |
| static int ignore_components_specified = 0; | ||
| static int unzip = 0; | ||
| static int no_revert_omitted = 0; | ||
| static int use_colors = 0; | ||
| static int color_option_specified = 0; | ||
| static int debug = 0; | ||
|
|
||
| static struct patlist *pat_drop_context = NULL; | ||
|
|
||
| static struct file_list *files_done = NULL; | ||
|
|
||
twaugh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| static struct file_list *files_in_patch2 = NULL; | ||
| static struct file_list *files_in_patch1 = NULL; | ||
|
|
||
| /* checks whether file needs processing and sets context */ | ||
| /* | ||
| * Print colored output using a variadic format string. | ||
| */ | ||
| static void | ||
| print_color (FILE *output_file, enum line_type type, const char *format, ...) | ||
| { | ||
| const char *color_start = ""; | ||
twaugh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const char *color_end = ""; | ||
twaugh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| va_list args; | ||
|
|
||
| /* Only colorize if colors are enabled AND we're outputting to stdout */ | ||
| if (use_colors && output_file == stdout) { | ||
| color_start = color_codes[type]; | ||
| if (color_start[0] != '\0') | ||
| color_end = COLOR_RESET; | ||
twaugh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /* Print color start code */ | ||
| if (color_start[0] != '\0') | ||
twaugh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| fputs (color_start, output_file); | ||
|
|
||
| /* Print the formatted content */ | ||
| va_start (args, format); | ||
| vfprintf (output_file, format, args); | ||
| va_end (args); | ||
|
|
||
| /* Print color end code */ | ||
| if (color_end[0] != '\0') | ||
|
||
| fputs (color_end, output_file); | ||
twaugh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /* checks whether file needs processing and sets context */ | ||
| static int | ||
| check_filename (const char *fn) | ||
| { | ||
| if (patlist_match(pat_drop_context, fn)) { | ||
| if (patlist_match(pat_drop_context, fn)) | ||
| max_context = 0; | ||
| } else { | ||
| else | ||
| max_context = max_context_real; | ||
| } | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
|
|
@@ -181,9 +237,8 @@ determine_ignore_components (struct file_list *list1, struct file_list *list2) | |
| const char *stripped1 = stripped(l1->file, p); | ||
| for (struct file_list *l2 = list2; l2; l2 = l2->next) { | ||
| const char *stripped2 = stripped(l2->file, p); | ||
| if (!strcmp(stripped1, stripped2)) { | ||
| if (!strcmp(stripped1, stripped2)) | ||
| return p; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -1050,15 +1105,24 @@ trim_context (FILE *f /* positioned at start of @@ line */, | |
| printf ("Trim: %lu,%lu\n", strip_pre, strip_post); | ||
|
|
||
| fsetpos (f, &pos); | ||
| fprintf (out, "@@ -%lu", orig_offset); | ||
| if (new_orig_count != 1) | ||
| fprintf (out, ",%lu", new_orig_count); | ||
| fprintf (out, " +%lu", new_offset); | ||
| if (new_new_count != 1) | ||
| fprintf (out, ",%lu", new_new_count); | ||
| fprintf (out, " @@\n"); | ||
| { | ||
twaugh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (new_orig_count != 1 && new_new_count != 1) { | ||
| print_color (out, LINE_HUNK, "@@ -%lu,%lu +%lu,%lu @@\n", | ||
| orig_offset, new_orig_count, new_offset, new_new_count); | ||
| } else if (new_orig_count != 1) { | ||
| print_color (out, LINE_HUNK, "@@ -%lu,%lu +%lu @@\n", | ||
| orig_offset, new_orig_count, new_offset); | ||
| } else if (new_new_count != 1) { | ||
| print_color (out, LINE_HUNK, "@@ -%lu +%lu,%lu @@\n", | ||
| orig_offset, new_offset, new_new_count); | ||
| } else { | ||
| print_color (out, LINE_HUNK, "@@ -%lu +%lu @@\n", | ||
| orig_offset, new_offset); | ||
| } | ||
| } | ||
|
|
||
| while (total_count--) { | ||
| enum line_type type; | ||
| ssize_t got = getline (&line, &linelen, f); | ||
| assert (got > 0); | ||
|
|
||
|
|
@@ -1070,7 +1134,18 @@ trim_context (FILE *f /* positioned at start of @@ line */, | |
| if (total_count < strip_post) | ||
| continue; | ||
|
|
||
| fwrite (line, (size_t) got, 1, out); | ||
| switch (line[0]) { | ||
| case '+': | ||
| type = LINE_ADDED; | ||
| break; | ||
| case '-': | ||
| type = LINE_REMOVED; | ||
| break; | ||
| default: | ||
| fwrite (line, (size_t) got, 1, out); | ||
| continue; | ||
| } | ||
| print_color (out, type, "%s", line); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1264,13 +1339,13 @@ output_delta (FILE *p1, FILE *p2, FILE *out) | |
| d = *q; | ||
| *q = '\0'; | ||
| } | ||
| fprintf (out, DIFF " %s %s %s\n", options, oldname + 4, | ||
| newname + 4); | ||
| print_color (out, LINE_HEADER, DIFF " %s %s %s\n", options, oldname + 4, | ||
| newname + 4); | ||
| if (p) *p = c; | ||
| if (q) *q = d; | ||
| } | ||
| fprintf (out, "--- %s\n", oldname + 4); | ||
| fprintf (out, "+++ %s\n", newname + 4); | ||
| print_color (out, LINE_FILE, "--- %s\n", oldname + 4); | ||
| print_color (out, LINE_FILE, "+++ %s\n", newname + 4); | ||
| rewind (tmpdiff); | ||
| trim_context (tmpdiff, file.unline, out); | ||
| fclose (tmpdiff); | ||
|
|
@@ -2096,12 +2171,10 @@ interdiff (FILE *p1, FILE *p2, const char *patch1, const char *patch2) | |
|
|
||
| if (flipdiff_inplace) { | ||
| /* Use atomic in-place writing for safety */ | ||
| if (write_file_inplace(patch2, flip1) != 0) { | ||
| if (write_file_inplace(patch2, flip1) != 0) | ||
| error (EXIT_FAILURE, errno, "failed to write %s", patch2); | ||
| } | ||
| if (write_file_inplace(patch1, flip2) != 0) { | ||
| if (write_file_inplace(patch1, flip2) != 0) | ||
| error (EXIT_FAILURE, errno, "failed to write %s", patch1); | ||
| } | ||
| } else { | ||
| copy (flip1, stdout); | ||
| puts ("\n=== 8< === cut here === 8< ===\n"); | ||
|
|
@@ -2280,12 +2353,12 @@ main (int argc, char *argv[]) | |
| const char *color_mode = optarg ? optarg : "auto"; | ||
|
|
||
| /* Handle auto mode: check if stdout is a terminal */ | ||
| if (strcmp(color_mode, "auto") == 0) { | ||
| if (strcmp(color_mode, "auto") == 0) | ||
| color_mode = isatty(STDOUT_FILENO) ? "always" : "never"; | ||
| } | ||
|
|
||
| if (asprintf (diff_opts + num_diff_opts++, "--color=%s", color_mode) < 0) | ||
| error (EXIT_FAILURE, errno, "Memory allocation failed"); | ||
| /* Set our internal color flag instead of passing to diff */ | ||
| use_colors = (strcmp(color_mode, "always") == 0); | ||
| color_option_specified = 1; | ||
| break; | ||
| } | ||
| case 1000 + 'I': | ||
|
|
@@ -2317,18 +2390,9 @@ main (int argc, char *argv[]) | |
| error (EXIT_FAILURE, 0, | ||
| "-z and --in-place are mutually exclusive."); | ||
|
|
||
| /* Add default color=always if no color option was specified and we're in a terminal */ | ||
| int has_color_option = 0; | ||
| for (int i = 0; i < num_diff_opts; i++) { | ||
| if (strncmp(diff_opts[i], "--color", 7) == 0) { | ||
| has_color_option = 1; | ||
| break; | ||
| } | ||
| } | ||
| if (!has_color_option && isatty(STDOUT_FILENO)) { | ||
| if (asprintf (diff_opts + num_diff_opts++, "--color=always") < 0) | ||
| error (EXIT_FAILURE, errno, "Memory allocation failed"); | ||
| } | ||
| /* Set default color behavior if no color option was specified */ | ||
| if (!color_option_specified && isatty(STDOUT_FILENO)) | ||
| use_colors = 1; | ||
|
|
||
| if (optind + 2 != argc) | ||
| syntax (1); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.