Skip to content

Commit b4196cf

Browse files
author
Junio C Hamano
committed
Merge branches 'master' and 'jc/combine' into next
* master: Add git-clean command diff_flush(): leakfix. parse_date(): fix parsing 03/10/2006 * jc/combine: combine-diff: refactor built-in xdiff interface.
3 parents 110cb41 + c3b831b + d9ea73e commit b4196cf

File tree

9 files changed

+243
-68
lines changed

9 files changed

+243
-68
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ git-checkout
1515
git-checkout-index
1616
git-cherry
1717
git-cherry-pick
18+
git-clean
1819
git-clone
1920
git-clone-pack
2021
git-commit

Documentation/git-clean.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
git-clean(1)
2+
============
3+
4+
NAME
5+
----
6+
git-clean - Remove untracked files from the working tree
7+
8+
SYNOPSIS
9+
--------
10+
[verse]
11+
'git-clean' [-d] [-n] [-q] [-x | -X]
12+
13+
DESCRIPTION
14+
-----------
15+
Removes files unknown to git. This allows to clean the working tree
16+
from files that are not under version control. If the '-x' option is
17+
specified, ignored files are also removed, allowing to remove all
18+
build products.
19+
20+
OPTIONS
21+
-------
22+
-d::
23+
Remove untracked directories in addition to untracked files.
24+
25+
-n::
26+
Don't actually remove anything, just show what would be done.
27+
28+
-q::
29+
Be quiet, only report errors, but not the files that are
30+
successfully removed.
31+
32+
-x::
33+
Don't use the ignore rules. This allows removing all untracked
34+
files, including build products. This can be used (possibly in
35+
conjunction with gitlink:git-reset[1]) to create a pristine
36+
working directory to test a clean build.
37+
38+
-X::
39+
Remove only files ignored by git. This may be useful to rebuild
40+
everything from scratch, but keep manually created files.
41+
42+
43+
Author
44+
------
45+
Written by Pavel Roskin <proski@gnu.org>
46+
47+
48+
GIT
49+
---
50+
Part of the gitlink:git[7] suite

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ SPARSE_FLAGS = -D__BIG_ENDIAN__ -D__powerpc__
114114

115115
SCRIPT_SH = \
116116
git-add.sh git-bisect.sh git-branch.sh git-checkout.sh \
117-
git-cherry.sh git-clone.sh git-commit.sh \
117+
git-cherry.sh git-clean.sh git-clone.sh git-commit.sh \
118118
git-count-objects.sh git-diff.sh git-fetch.sh \
119119
git-format-patch.sh git-log.sh git-ls-remote.sh \
120120
git-merge-one-file.sh git-parse-remote.sh \
@@ -208,7 +208,7 @@ LIB_OBJS = \
208208
quote.o read-cache.o refs.o run-command.o \
209209
server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
210210
tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
211-
fetch-clone.o revision.o pager.o tree-walk.o \
211+
fetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \
212212
$(DIFF_OBJS)
213213

214214
GITLIBS = $(LIB_FILE) $(XDIFF_LIB)

combine-diff.c

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "diff.h"
55
#include "diffcore.h"
66
#include "quote.h"
7-
#include "xdiff/xdiff.h"
7+
#include "xdiff-interface.h"
88

99
static int uninteresting(struct diff_filepair *p)
1010
{
@@ -195,8 +195,8 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)
195195
}
196196

197197
struct combine_diff_state {
198-
char *remainder;
199-
unsigned long remainder_size;
198+
struct xdiff_emit_state xm;
199+
200200
unsigned int lno, ob, on, nb, nn;
201201
unsigned long nmask;
202202
int num_parent;
@@ -205,10 +205,9 @@ struct combine_diff_state {
205205
struct sline *lost_bucket;
206206
};
207207

208-
static void consume_line(struct combine_diff_state *state,
209-
char *line,
210-
unsigned long len)
208+
static void consume_line(void *state_, char *line, unsigned long len)
211209
{
210+
struct combine_diff_state *state = state_;
212211
if (5 < len && !memcmp("@@ -", line, 4)) {
213212
if (parse_hunk_header(line, len,
214213
&state->ob, &state->on,
@@ -249,41 +248,6 @@ static void consume_line(struct combine_diff_state *state,
249248
}
250249
}
251250

252-
static int combine_diff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
253-
{
254-
struct combine_diff_state *priv = priv_;
255-
int i;
256-
for (i = 0; i < nbuf; i++) {
257-
if (mb[i].ptr[mb[i].size-1] != '\n') {
258-
/* Incomplete line */
259-
priv->remainder = realloc(priv->remainder,
260-
priv->remainder_size +
261-
mb[i].size);
262-
memcpy(priv->remainder + priv->remainder_size,
263-
mb[i].ptr, mb[i].size);
264-
priv->remainder_size += mb[i].size;
265-
continue;
266-
}
267-
268-
/* we have a complete line */
269-
if (!priv->remainder) {
270-
consume_line(priv, mb[i].ptr, mb[i].size);
271-
continue;
272-
}
273-
priv->remainder = realloc(priv->remainder,
274-
priv->remainder_size +
275-
mb[i].size);
276-
memcpy(priv->remainder + priv->remainder_size,
277-
mb[i].ptr, mb[i].size);
278-
consume_line(priv, priv->remainder,
279-
priv->remainder_size + mb[i].size);
280-
free(priv->remainder);
281-
priv->remainder = NULL;
282-
priv->remainder_size = 0;
283-
}
284-
return 0;
285-
}
286-
287251
static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
288252
struct sline *sline, int cnt, int n, int num_parent)
289253
{
@@ -304,19 +268,17 @@ static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
304268
xpp.flags = XDF_NEED_MINIMAL;
305269
xecfg.ctxlen = 0;
306270
xecfg.flags = 0;
307-
ecb.outf = combine_diff_outf;
271+
ecb.outf = xdiff_outf;
308272
ecb.priv = &state;
309273
memset(&state, 0, sizeof(state));
274+
state.xm.consume = consume_line;
310275
state.nmask = nmask;
311276
state.sline = sline;
312277
state.lno = 1;
313278
state.num_parent = num_parent;
314279
state.n = n;
315280

316281
xdl_diff(&parent_file, result_file, &xpp, &xecfg, &ecb);
317-
if (state.remainder && state.remainder_size)
318-
consume_line(&state, state.remainder, state.remainder_size);
319-
free(state.remainder);
320282
free(parent_file.ptr);
321283

322284
/* Assign line numbers for this parent.

date.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,10 @@ static int match_multi_number(unsigned long num, char c, const char *date, char
255255
break;
256256
}
257257
/* mm/dd/yy ? */
258-
if (is_date(num3, num2, num, tm))
258+
if (is_date(num3, num, num2, tm))
259259
break;
260260
/* dd/mm/yy ? */
261-
if (is_date(num3, num, num2, tm))
261+
if (is_date(num3, num2, num, tm))
262262
break;
263263
return 0;
264264
}

diff.c

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,28 +1282,34 @@ void diff_flush(struct diff_options *options)
12821282

12831283
for (i = 0; i < q->nr; i++) {
12841284
struct diff_filepair *p = q->queue[i];
1285-
if ((diff_output_format == DIFF_FORMAT_NO_OUTPUT) ||
1286-
(p->status == DIFF_STATUS_UNKNOWN))
1287-
continue;
1288-
if (p->status == 0)
1289-
die("internal error in diff-resolve-rename-copy");
1290-
switch (diff_output_format) {
1291-
case DIFF_FORMAT_PATCH:
1292-
diff_flush_patch(p, options);
1293-
break;
1294-
case DIFF_FORMAT_RAW:
1295-
case DIFF_FORMAT_NAME_STATUS:
1296-
diff_flush_raw(p, line_termination,
1297-
inter_name_termination,
1298-
options);
1285+
1286+
switch (p->status) {
1287+
case DIFF_STATUS_UNKNOWN:
12991288
break;
1300-
case DIFF_FORMAT_NAME:
1301-
diff_flush_name(p,
1302-
inter_name_termination,
1303-
line_termination);
1289+
case 0:
1290+
die("internal error in diff-resolve-rename-copy");
13041291
break;
1292+
default:
1293+
switch (diff_output_format) {
1294+
case DIFF_FORMAT_PATCH:
1295+
diff_flush_patch(p, options);
1296+
break;
1297+
case DIFF_FORMAT_RAW:
1298+
case DIFF_FORMAT_NAME_STATUS:
1299+
diff_flush_raw(p, line_termination,
1300+
inter_name_termination,
1301+
options);
1302+
break;
1303+
case DIFF_FORMAT_NAME:
1304+
diff_flush_name(p,
1305+
inter_name_termination,
1306+
line_termination);
1307+
break;
1308+
case DIFF_FORMAT_NO_OUTPUT:
1309+
break;
1310+
}
13051311
}
1306-
diff_free_filepair(q->queue[i]);
1312+
diff_free_filepair(p);
13071313
}
13081314
free(q->queue);
13091315
q->queue = NULL;

git-clean.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2005-2006 Pavel Roskin
4+
#
5+
6+
USAGE="[-d] [-n] [-q] [-x | -X]"
7+
LONG_USAGE='Clean untracked files from the working directory
8+
-d remove directories as well
9+
-n don'\''t remove anything, just show what would be done
10+
-q be quiet, only report errors
11+
-x remove ignored files as well
12+
-X remove only ignored files as well'
13+
SUBDIRECTORY_OK=Yes
14+
. git-sh-setup
15+
16+
ignored=
17+
ignoredonly=
18+
cleandir=
19+
quiet=
20+
rmf="rm -f"
21+
rmrf="rm -rf"
22+
rm_refuse="echo Not removing"
23+
echo1="echo"
24+
25+
while case "$#" in 0) break ;; esac
26+
do
27+
case "$1" in
28+
-d)
29+
cleandir=1
30+
;;
31+
-n)
32+
quiet=1
33+
rmf="echo Would remove"
34+
rmrf="echo Would remove"
35+
rm_refuse="echo Would not remove"
36+
echo1=":"
37+
;;
38+
-q)
39+
quiet=1
40+
;;
41+
-x)
42+
ignored=1
43+
;;
44+
-X)
45+
ignoredonly=1
46+
;;
47+
*)
48+
usage
49+
esac
50+
shift
51+
done
52+
53+
case "$ignored,$ignoredonly" in
54+
1,1) usage;;
55+
esac
56+
57+
if [ -z "$ignored" ]; then
58+
excl="--exclude-per-directory=.gitignore"
59+
if [ -f "$GIT_DIR/info/exclude" ]; then
60+
excl_info="--exclude-from=$GIT_DIR/info/exclude"
61+
fi
62+
if [ "$ignoredonly" ]; then
63+
excl="$excl --ignored"
64+
fi
65+
fi
66+
67+
git-ls-files --others --directory $excl ${excl_info:+"$excl_info"} |
68+
while read -r file; do
69+
if [ -d "$file" -a ! -L "$file" ]; then
70+
if [ -z "$cleandir" ]; then
71+
$rm_refuse "$file"
72+
continue
73+
fi
74+
$echo1 "Removing $file"
75+
$rmrf "$file"
76+
else
77+
$echo1 "Removing $file"
78+
$rmf "$file"
79+
fi
80+
done

xdiff-interface.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include "cache.h"
2+
#include "xdiff-interface.h"
3+
4+
static void consume_one(void *priv_, char *s, unsigned long size)
5+
{
6+
struct xdiff_emit_state *priv = priv_;
7+
char *ep;
8+
while (size) {
9+
unsigned long this_size;
10+
ep = memchr(s, '\n', size);
11+
this_size = (ep == NULL) ? size : (ep - s + 1);
12+
priv->consume(priv, s, this_size);
13+
size -= this_size;
14+
s += this_size;
15+
}
16+
}
17+
18+
int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
19+
{
20+
struct xdiff_emit_state *priv = priv_;
21+
int i;
22+
23+
for (i = 0; i < nbuf; i++) {
24+
if (mb[i].ptr[mb[i].size-1] != '\n') {
25+
/* Incomplete line */
26+
priv->remainder = realloc(priv->remainder,
27+
priv->remainder_size +
28+
mb[i].size);
29+
memcpy(priv->remainder + priv->remainder_size,
30+
mb[i].ptr, mb[i].size);
31+
priv->remainder_size += mb[i].size;
32+
continue;
33+
}
34+
35+
/* we have a complete line */
36+
if (!priv->remainder) {
37+
consume_one(priv, mb[i].ptr, mb[i].size);
38+
continue;
39+
}
40+
priv->remainder = realloc(priv->remainder,
41+
priv->remainder_size +
42+
mb[i].size);
43+
memcpy(priv->remainder + priv->remainder_size,
44+
mb[i].ptr, mb[i].size);
45+
consume_one(priv, priv->remainder,
46+
priv->remainder_size + mb[i].size);
47+
free(priv->remainder);
48+
priv->remainder = NULL;
49+
priv->remainder_size = 0;
50+
}
51+
if (priv->remainder) {
52+
consume_one(priv, priv->remainder, priv->remainder_size);
53+
free(priv->remainder);
54+
priv->remainder = NULL;
55+
priv->remainder_size = 0;
56+
}
57+
return 0;
58+
}

xdiff-interface.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef XDIFF_INTERFACE_H
2+
#define XDIFF_INTERFACE_H
3+
4+
#include "xdiff/xdiff.h"
5+
6+
struct xdiff_emit_state;
7+
8+
typedef void (*xdiff_emit_consume_fn)(void *, char *, unsigned long);
9+
10+
struct xdiff_emit_state {
11+
xdiff_emit_consume_fn consume;
12+
char *remainder;
13+
unsigned long remainder_size;
14+
};
15+
16+
int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf);
17+
18+
#endif

0 commit comments

Comments
 (0)