log-tree.c: cleanup a bit append_signoff()
This patch clean up append_signoff() by moving specific code that looks up for "^[-A-Za-z]+: [^@]+@" pattern into a function. It also stops the primary search when the cursor oversteps 'buf + at' limit. This patch changes slightly append_signoff() behaviour too. If we detect any Signed-off-by pattern during the primary search, we needn't to do a pattern research after. Signed-off-by: Franck Bui-Huu <vagabon.xyz@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
Franck Bui-Huu committed
Aug 29, 2006 at 13:37 UTC
fc1c75ec74c372670f01f22c68193156a445728a
1 file changed
+66
-50
log-tree.c
+66
-50
@@ -12,10 +12,58 @@ static void show_parents(struct commit *commit, int abbrev)
12
}
13
}
14
15
+/*
16
+ * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
17
+ * Signed-off-by: and Acked-by: lines.
18
+ */
19
+static int detect_any_signoff(char *letter, int size)
20
+{
21
+ char ch, *cp;
22
+ int seen_colon = 0;
23
+ int seen_at = 0;
24
+ int seen_name = 0;
25
+ int seen_head = 0;
26
+
27
+ cp = letter + size;
28
+ while (letter <= --cp && (ch = *cp) == '\n')
29
+ continue;
30
+
31
+ while (letter <= cp) {
32
+ ch = *cp--;
33
+ if (ch == '\n')
34
+ break;
35
+
36
+ if (!seen_at) {
37
+ if (ch == '@')
38
+ seen_at = 1;
39
+ continue;
40
+ }
41
+ if (!seen_colon) {
42
+ if (ch == '@')
43
+ return 0;
44
+ else if (ch == ':')
45
+ seen_colon = 1;
46
+ else
47
+ seen_name = 1;
48
+ continue;
49
+ }
50
+ if (('A' <= ch && ch <= 'Z') ||
51
+ ('a' <= ch && ch <= 'z') ||
52
+ ch == '-') {
53
+ seen_head = 1;
54
+ continue;
55
+ }
56
+ /* no empty last line doesn't match */
57
+ return 0;
58
+ }
59
+ return seen_head && seen_name;
60
+}
61
+
62
static int append_signoff(char *buf, int buf_sz, int at, const char *signoff)
63
{
17
- int signoff_len = strlen(signoff);
64
static const char signed_off_by[] = "Signed-off-by: ";
65
+ int signoff_len = strlen(signoff);
66
+ int has_signoff = 0;
67
char *cp = buf;
68
69
/* Do we have enough space to add it? */
@@ -23,58 +71,26 @@ static int append_signoff(char *buf, int buf_sz, int at, const char *signoff)
71
return at;
72
73
/* First see if we already have the sign-off by the signer */
26
- while (1) {
27
- cp = strstr(cp, signed_off_by);
28
- if (!cp)
29
- break;
74
+ while ((cp = strstr(cp, signed_off_by))) {
75
+
76
+ has_signoff = 1;
77
+
78
cp += strlen(signed_off_by);
31
- if ((cp + signoff_len < buf + at) &&
32
- !strncmp(cp, signoff, signoff_len) &&
33
- isspace(cp[signoff_len]))
34
- return at; /* we already have him */
79
+ if (cp + signoff_len >= buf + at)
80
+ break;
81
+ if (strncmp(cp, signoff, signoff_len))
82
+ continue;
83
+ if (!isspace(cp[signoff_len]))
84
+ continue;
85
+ /* we already have him */
86
+ return at;
87
}
88
37
- /* Does the last line already end with "^[-A-Za-z]+: [^@]+@"?
38
- * If not, add a blank line to separate the message from
39
- * the run of Signed-off-by: and Acked-by: lines.
40
- */
41
- {
42
- char ch;
43
- int seen_colon, seen_at, seen_name, seen_head, not_signoff;
44
- seen_colon = 0;
45
- seen_at = 0;
46
- seen_name = 0;
47
- seen_head = 0;
48
- not_signoff = 0;
49
- cp = buf + at;
50
- while (buf <= --cp && (ch = *cp) == '\n')
51
- ;
52
- while (!not_signoff && buf <= cp && (ch = *cp--) != '\n') {
53
- if (!seen_at) {
54
- if (ch == '@')
55
- seen_at = 1;
56
- continue;
57
- }
58
- if (!seen_colon) {
59
- if (ch == '@')
60
- not_signoff = 1;
61
- else if (ch == ':')
62
- seen_colon = 1;
63
- else
64
- seen_name = 1;
65
- continue;
66
- }
67
- if (('A' <= ch && ch <= 'Z') ||
68
- ('a' <= ch && ch <= 'z') ||
69
- ch == '-') {
70
- seen_head = 1;
71
- continue;
72
- }
73
- not_signoff = 1;
74
- }
75
- if (not_signoff || !seen_head || !seen_name)
76
- buf[at++] = '\n';
77
- }
89
+ if (!has_signoff)
90
+ has_signoff = detect_any_signoff(buf, at);
91
+
92
+ if (!has_signoff)
93
+ buf[at++] = '\n';
94
95
strcpy(buf + at, signed_off_by);
96
at += strlen(signed_off_by);