Skip to content

Commit 6ce15dd

Browse files
committed
Merge branch '0.4.x'
2 parents 754b4a5 + 968d881 commit 6ce15dd

File tree

3 files changed

+80
-31
lines changed

3 files changed

+80
-31
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: CI
22

33
on:
44
push:
5-
branches: [ master, main ]
5+
branches: [ master, main, 0.4.x ]
66
pull_request:
7-
branches: [ master, main ]
7+
branches: [ master, main, 0.4.x ]
88

99
jobs:
1010
test:

src/filterdiff.c

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ static int
359359
do_git_diff_no_hunks (FILE *f, char **header, unsigned int num_headers,
360360
int match, char **line, size_t *linelen,
361361
unsigned long *linenum, unsigned long start_linenum,
362-
char status, const char *bestname, const char *patchname,
362+
char *status, const char *bestname, const char *patchname,
363363
int *orig_file_exists, int *new_file_exists,
364364
enum git_diff_type git_type)
365365
{
@@ -386,6 +386,16 @@ do_git_diff_no_hunks (FILE *f, char **header, unsigned int num_headers,
386386
break;
387387
}
388388

389+
/* Update status based on file existence (do this early so returns below have correct status) */
390+
if (status != NULL && mode != mode_filter && show_status &&
391+
orig_file_exists != NULL && new_file_exists != NULL) {
392+
if (!*orig_file_exists)
393+
*status = '+';
394+
else if (!*new_file_exists)
395+
*status = '-';
396+
/* else: keep existing '!' value for modifications */
397+
}
398+
389399
/* If this diff matches the filter, display it */
390400
if (match) {
391401
if (mode == mode_filter) {
@@ -394,7 +404,7 @@ do_git_diff_no_hunks (FILE *f, char **header, unsigned int num_headers,
394404
fputs (header[i], stdout);
395405
} else if (mode == mode_list && !displayed_filename) {
396406
if (!show_status) {
397-
display_filename (start_linenum, status,
407+
display_filename (start_linenum, *status,
398408
bestname, patchname);
399409
}
400410
displayed_filename = 1;
@@ -455,7 +465,7 @@ static int
455465
do_unified (FILE *f, char **header, unsigned int num_headers,
456466
int match, char **line,
457467
size_t *linelen, unsigned long *linenum,
458-
unsigned long start_linenum, char status,
468+
unsigned long start_linenum, char *status,
459469
const char *bestname, const char *patchname,
460470
int *orig_file_exists, int *new_file_exists)
461471
{
@@ -671,7 +681,7 @@ do_unified (FILE *f, char **header, unsigned int num_headers,
671681
if (!displayed_filename) {
672682
displayed_filename = 1;
673683
display_filename (start_linenum,
674-
status, bestname,
684+
*status, bestname,
675685
patchname);
676686
}
677687

@@ -746,14 +756,24 @@ do_unified (FILE *f, char **header, unsigned int num_headers,
746756
*new_file_exists = 0;
747757
}
748758

759+
/* Update status based on final file existence after empty file processing */
760+
if (status != NULL && mode != mode_filter && show_status &&
761+
orig_file_exists != NULL && new_file_exists != NULL) {
762+
if (!*orig_file_exists)
763+
*status = '+';
764+
else if (!*new_file_exists)
765+
*status = '-';
766+
/* else: keep existing '!' value for modifications */
767+
}
768+
749769
return ret;
750770
}
751771

752772
static int
753773
do_context (FILE *f, char **header, unsigned int num_headers,
754774
int match, char **line,
755775
size_t *linelen, unsigned long *linenum,
756-
unsigned long start_linenum, char status,
776+
unsigned long start_linenum, char *status,
757777
const char *bestname, const char *patchname,
758778
int *orig_file_exists, int *new_file_exists)
759779
{
@@ -1020,7 +1040,7 @@ do_context (FILE *f, char **header, unsigned int num_headers,
10201040
if (!displayed_filename) {
10211041
displayed_filename = 1;
10221042
display_filename(start_linenum,
1023-
status,
1043+
*status,
10241044
bestname,
10251045
patchname);
10261046
}
@@ -1150,6 +1170,16 @@ do_context (FILE *f, char **header, unsigned int num_headers,
11501170
*new_file_exists = 0;
11511171
}
11521172

1173+
/* Update status based on final file existence after empty file processing */
1174+
if (status != NULL && mode != mode_filter && show_status &&
1175+
orig_file_exists != NULL && new_file_exists != NULL) {
1176+
if (!*orig_file_exists)
1177+
*status = '+';
1178+
else if (!*new_file_exists)
1179+
*status = '-';
1180+
/* else: keep existing '!' value for modifications */
1181+
}
1182+
11531183
return ret;
11541184
}
11551185

@@ -1180,7 +1210,7 @@ static int filterdiff (FILE *f, const char *patchname)
11801210
int (*do_diff) (FILE *, char **, unsigned int,
11811211
int, char **, size_t *,
11821212
unsigned long *, unsigned long,
1183-
char, const char *, const char *,
1213+
char *, const char *, const char *,
11841214
int *, int *);
11851215

11861216
orig_file_exists = 0; // shut gcc up
@@ -1375,19 +1405,13 @@ static int filterdiff (FILE *f, const char *patchname)
13751405
/* Process the git diff (it will handle filename display) */
13761406
result = do_git_diff_no_hunks (f, header, num_headers,
13771407
match, &line, &linelen, &linenum,
1378-
start_linenum, status, p, patchname,
1408+
start_linenum, &status, p, patchname,
13791409
&orig_file_exists, &new_file_exists,
13801410
git_type);
13811411

13821412
/* Print filename with status if in list mode and matches */
1383-
if (match && show_status && mode == mode_list) {
1384-
if (!orig_file_exists)
1385-
status = '+';
1386-
else if (!new_file_exists)
1387-
status = '-';
1388-
1413+
if (match && show_status && mode == mode_list)
13891414
display_filename (start_linenum, status, p, patchname);
1390-
}
13911415

13921416
/* Clean up */
13931417
free (git_old_name);
@@ -1476,19 +1500,13 @@ static int filterdiff (FILE *f, const char *patchname)
14761500
result = do_diff (f, header, num_headers,
14771501
match, &line,
14781502
&linelen, &linenum,
1479-
start_linenum, status, p, patchname,
1503+
start_linenum, &status, p, patchname,
14801504
&orig_file_exists, &new_file_exists);
14811505

14821506
// print if it matches.
1483-
if (match && show_status && mode == mode_list) {
1484-
if (!orig_file_exists)
1485-
status = '+';
1486-
else if (!new_file_exists)
1487-
status = '-';
1488-
1507+
if (match && show_status && mode == mode_list)
14891508
display_filename (start_linenum, status,
14901509
p, patchname);
1491-
}
14921510

14931511
switch (result) {
14941512
case EOF:
@@ -1587,8 +1605,8 @@ const char * syntax_str =
15871605
" prefix pathnames in old files with PREFIX\n"
15881606
" --addnewprefix=PREFIX\n"
15891607
" prefix pathnames in new files with PREFIX\n"
1590-
" -s, --status (lsdiff)\n"
1591-
" show file additions and removals (lsdiff)\n"
1608+
" -s, --status (lsdiff, grepdiff)\n"
1609+
" show file additions (+), removals (-), and modifications (!) (lsdiff, grepdiff)\n"
15921610
" -v, --verbose\n"
15931611
" verbose output -- use more than once for extra verbosity\n"
15941612
" -E, --extended-regexp (grepdiff)\n"

tests/grepdiff-status/run-test

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#!/bin/sh
22

33
# Test grepdiff -s/--status option
4-
# Shows file status: + (addition), - (removal), ! (modification)
4+
# According to the documentation, -s should show:
5+
# + for file additions
6+
# - for file removals
7+
# ! for file modifications
58

69
. ${top_srcdir-.}/tests/common.sh
710

@@ -19,20 +22,48 @@ cat << EOF > diff
1922
@@ -1 +1 @@
2023
-old
2124
+new
25+
--- another-modified
26+
+++ another-modified
27+
@@ -1,2 +1,2 @@
28+
context
29+
-old content
30+
+new content
2231
EOF
2332

33+
# Test additions and deletions (files with 'content' pattern)
2434
${GREPDIFF} -s 'content' diff 2>errors >output || exit 1
2535
[ -s errors ] && exit 1
2636

2737
cat << EOF | cmp - output || exit 1
28-
! newfile
29-
! oldfile
38+
+ newfile
39+
- oldfile
40+
! another-modified
3041
EOF
3142

32-
# Test with modification
43+
# Test modification only (file with 'new' pattern)
3344
${GREPDIFF} -s 'new' diff 2>errors >output2 || exit 1
3445
[ -s errors ] && exit 1
3546

3647
cat << EOF | cmp - output2 || exit 1
3748
! modified
49+
! another-modified
50+
EOF
51+
52+
# Test with --empty-files-as-absent
53+
# File with only additions should be treated as new file
54+
cat << EOF > diff3
55+
--- emptyfile
56+
+++ emptyfile
57+
@@ -0,0 +1 @@
58+
+content
59+
@@ -60 +60 @@
60+
-old
61+
+new
62+
EOF
63+
64+
${GREPDIFF} -s --empty-files-as-absent 'content' diff3 2>errors >output3 || exit 1
65+
[ -s errors ] && exit 1
66+
67+
cat << EOF | cmp - output3 || exit 1
68+
+ emptyfile
3869
EOF

0 commit comments

Comments
 (0)