Skip to content

Commit dd4bca3

Browse files
author
Junio C Hamano
committed
Merge branch 'jc/blame' into next
* jc/blame: blame -S <ancestry-file> Match ofs/cnt types in diff interface.
2 parents 38b525e + 5040f17 commit dd4bca3

File tree

6 files changed

+135
-67
lines changed

6 files changed

+135
-67
lines changed

blame.c

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -703,11 +703,30 @@ static void* topo_getter(struct commit* c)
703703
return util->topo_data;
704704
}
705705

706+
static int read_ancestry(const char *graft_file,
707+
unsigned char **start_sha1)
708+
{
709+
FILE *fp = fopen(graft_file, "r");
710+
char buf[1024];
711+
if (!fp)
712+
return -1;
713+
while (fgets(buf, sizeof(buf), fp)) {
714+
/* The format is just "Commit Parent1 Parent2 ...\n" */
715+
int len = strlen(buf);
716+
struct commit_graft *graft = read_graft_line(buf, len);
717+
register_commit_graft(graft, 0);
718+
if (!*start_sha1)
719+
*start_sha1 = graft->sha1;
720+
}
721+
fclose(fp);
722+
return 0;
723+
}
724+
706725
int main(int argc, const char **argv)
707726
{
708727
int i;
709728
struct commit *initial = NULL;
710-
unsigned char sha1[20];
729+
unsigned char sha1[20], *sha1_p = NULL;
711730

712731
const char *filename = NULL, *commit = NULL;
713732
char filename_buf[256];
@@ -741,6 +760,14 @@ int main(int argc, const char **argv)
741760
!strcmp(argv[i], "--compability")) {
742761
compability = 1;
743762
continue;
763+
} else if(!strcmp(argv[i], "-S")) {
764+
if (i + 1 < argc &&
765+
!read_ancestry(argv[i + 1], &sha1_p)) {
766+
compability = 1;
767+
i++;
768+
continue;
769+
}
770+
usage(blame_usage);
744771
} else if(!strcmp(argv[i], "--")) {
745772
options = 0;
746773
continue;
@@ -762,7 +789,9 @@ int main(int argc, const char **argv)
762789

763790
if(!filename)
764791
usage(blame_usage);
765-
if(!commit)
792+
if (commit && sha1_p)
793+
usage(blame_usage);
794+
else if(!commit)
766795
commit = "HEAD";
767796

768797
if(prefix)
@@ -771,9 +800,12 @@ int main(int argc, const char **argv)
771800
strcpy(filename_buf, filename);
772801
filename = filename_buf;
773802

774-
if (get_sha1(commit, sha1))
775-
die("get_sha1 failed, commit '%s' not found", commit);
776-
start_commit = lookup_commit_reference(sha1);
803+
if (!sha1_p) {
804+
if (get_sha1(commit, sha1))
805+
die("get_sha1 failed, commit '%s' not found", commit);
806+
sha1_p = sha1;
807+
}
808+
start_commit = lookup_commit_reference(sha1_p);
777809
get_util(start_commit)->pathname = filename;
778810
if (fill_util_info(start_commit)) {
779811
printf("%s not found in %s\n", filename, commit);

combine-diff.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)
151151
struct combine_diff_state {
152152
struct xdiff_emit_state xm;
153153

154-
unsigned int lno, ob, on, nb, nn;
154+
unsigned int lno;
155+
int ob, on, nb, nn;
155156
unsigned long nmask;
156157
int num_parent;
157158
int n;

commit.c

Lines changed: 79 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,7 @@ static unsigned long parse_commit_date(const char *buf)
101101
return date;
102102
}
103103

104-
static struct commit_graft {
105-
unsigned char sha1[20];
106-
int nr_parent;
107-
unsigned char parent[0][20]; /* more */
108-
} **commit_graft;
104+
static struct commit_graft **commit_graft;
109105
static int commit_graft_alloc, commit_graft_nr;
110106

111107
static int commit_graft_pos(const unsigned char *sha1)
@@ -127,70 +123,98 @@ static int commit_graft_pos(const unsigned char *sha1)
127123
return -lo - 1;
128124
}
129125

130-
static void prepare_commit_graft(void)
126+
int register_commit_graft(struct commit_graft *graft, int ignore_dups)
127+
{
128+
int pos = commit_graft_pos(graft->sha1);
129+
130+
if (0 <= pos) {
131+
if (ignore_dups)
132+
free(graft);
133+
else {
134+
free(commit_graft[pos]);
135+
commit_graft[pos] = graft;
136+
}
137+
return 1;
138+
}
139+
pos = -pos - 1;
140+
if (commit_graft_alloc <= ++commit_graft_nr) {
141+
commit_graft_alloc = alloc_nr(commit_graft_alloc);
142+
commit_graft = xrealloc(commit_graft,
143+
sizeof(*commit_graft) *
144+
commit_graft_alloc);
145+
}
146+
if (pos < commit_graft_nr)
147+
memmove(commit_graft + pos + 1,
148+
commit_graft + pos,
149+
(commit_graft_nr - pos - 1) *
150+
sizeof(*commit_graft));
151+
commit_graft[pos] = graft;
152+
return 0;
153+
}
154+
155+
struct commit_graft *read_graft_line(char *buf, int len)
156+
{
157+
/* The format is just "Commit Parent1 Parent2 ...\n" */
158+
int i;
159+
struct commit_graft *graft = NULL;
160+
161+
if (buf[len-1] == '\n')
162+
buf[--len] = 0;
163+
if (buf[0] == '#')
164+
return 0;
165+
if ((len + 1) % 41) {
166+
bad_graft_data:
167+
error("bad graft data: %s", buf);
168+
free(graft);
169+
return NULL;
170+
}
171+
i = (len + 1) / 41 - 1;
172+
graft = xmalloc(sizeof(*graft) + 20 * i);
173+
graft->nr_parent = i;
174+
if (get_sha1_hex(buf, graft->sha1))
175+
goto bad_graft_data;
176+
for (i = 40; i < len; i += 41) {
177+
if (buf[i] != ' ')
178+
goto bad_graft_data;
179+
if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
180+
goto bad_graft_data;
181+
}
182+
return graft;
183+
}
184+
185+
int read_graft_file(const char *graft_file)
131186
{
132-
char *graft_file = get_graft_file();
133187
FILE *fp = fopen(graft_file, "r");
134188
char buf[1024];
135-
if (!fp) {
136-
commit_graft = (struct commit_graft **) "hack";
137-
return;
138-
}
189+
if (!fp)
190+
return -1;
139191
while (fgets(buf, sizeof(buf), fp)) {
140192
/* The format is just "Commit Parent1 Parent2 ...\n" */
141193
int len = strlen(buf);
142-
int i;
143-
struct commit_graft *graft = NULL;
144-
145-
if (buf[len-1] == '\n')
146-
buf[--len] = 0;
147-
if (buf[0] == '#')
148-
continue;
149-
if ((len + 1) % 41) {
150-
bad_graft_data:
151-
error("bad graft data: %s", buf);
152-
free(graft);
153-
continue;
154-
}
155-
i = (len + 1) / 41 - 1;
156-
graft = xmalloc(sizeof(*graft) + 20 * i);
157-
graft->nr_parent = i;
158-
if (get_sha1_hex(buf, graft->sha1))
159-
goto bad_graft_data;
160-
for (i = 40; i < len; i += 41) {
161-
if (buf[i] != ' ')
162-
goto bad_graft_data;
163-
if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
164-
goto bad_graft_data;
165-
}
166-
i = commit_graft_pos(graft->sha1);
167-
if (0 <= i) {
194+
struct commit_graft *graft = read_graft_line(buf, len);
195+
if (register_commit_graft(graft, 1))
168196
error("duplicate graft data: %s", buf);
169-
free(graft);
170-
continue;
171-
}
172-
i = -i - 1;
173-
if (commit_graft_alloc <= ++commit_graft_nr) {
174-
commit_graft_alloc = alloc_nr(commit_graft_alloc);
175-
commit_graft = xrealloc(commit_graft,
176-
sizeof(*commit_graft) *
177-
commit_graft_alloc);
178-
}
179-
if (i < commit_graft_nr)
180-
memmove(commit_graft + i + 1,
181-
commit_graft + i,
182-
(commit_graft_nr - i - 1) *
183-
sizeof(*commit_graft));
184-
commit_graft[i] = graft;
185197
}
186198
fclose(fp);
199+
return 0;
200+
}
201+
202+
static void prepare_commit_graft(void)
203+
{
204+
static int commit_graft_prepared;
205+
char *graft_file;
206+
207+
if (commit_graft_prepared)
208+
return;
209+
graft_file = get_graft_file();
210+
read_graft_file(graft_file);
211+
commit_graft_prepared = 1;
187212
}
188213

189214
static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
190215
{
191216
int pos;
192-
if (!commit_graft)
193-
prepare_commit_graft();
217+
prepare_commit_graft();
194218
pos = commit_graft_pos(sha1);
195219
if (pos < 0)
196220
return NULL;

commit.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,15 @@ void sort_in_topological_order(struct commit_list ** list, int lifo);
9090
void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
9191
topo_sort_set_fn_t setter,
9292
topo_sort_get_fn_t getter);
93+
94+
struct commit_graft {
95+
unsigned char sha1[20];
96+
int nr_parent;
97+
unsigned char parent[FLEX_ARRAY][20]; /* more */
98+
};
99+
100+
struct commit_graft *read_graft_line(char *buf, int len);
101+
int register_commit_graft(struct commit_graft *, int);
102+
int read_graft_file(const char *graft_file);
103+
93104
#endif /* COMMIT_H */

xdiff-interface.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#include "cache.h"
22
#include "xdiff-interface.h"
33

4-
static int parse_num(char **cp_p, unsigned int *num_p)
4+
static int parse_num(char **cp_p, int *num_p)
55
{
66
char *cp = *cp_p;
7-
unsigned int num = 0;
7+
int num = 0;
88
int read_some;
99

1010
while ('0' <= *cp && *cp <= '9')
@@ -17,8 +17,8 @@ static int parse_num(char **cp_p, unsigned int *num_p)
1717
}
1818

1919
int parse_hunk_header(char *line, int len,
20-
unsigned int *ob, unsigned int *on,
21-
unsigned int *nb, unsigned int *nn)
20+
int *ob, int *on,
21+
int *nb, int *nn)
2222
{
2323
char *cp;
2424
cp = line + 4;

xdiff-interface.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct xdiff_emit_state {
1515

1616
int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf);
1717
int parse_hunk_header(char *line, int len,
18-
unsigned int *ob, unsigned int *on,
19-
unsigned int *nb, unsigned int *nn);
18+
int *ob, int *on,
19+
int *nb, int *nn);
2020

2121
#endif

0 commit comments

Comments
 (0)