Skip to content

Commit ecbbc0a

Browse files
pcloudsgitster
authored andcommitted
blame.c: remove implicit dependency on the_index
Side note, since we gain access to the right repository, we can stop rely on the_repository in this code as well. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1b5c6c1 commit ecbbc0a

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

blame.c

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ static struct blame_origin *get_origin(struct commit *commit, const char *path)
9090

9191

9292

93-
static void verify_working_tree_path(struct commit *work_tree, const char *path)
93+
static void verify_working_tree_path(struct repository *repo,
94+
struct commit *work_tree, const char *path)
9495
{
9596
struct commit_list *parents;
9697
int pos;
@@ -101,15 +102,15 @@ static void verify_working_tree_path(struct commit *work_tree, const char *path)
101102
unsigned mode;
102103

103104
if (!get_tree_entry(commit_oid, path, &blob_oid, &mode) &&
104-
oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB)
105+
oid_object_info(repo, &blob_oid, NULL) == OBJ_BLOB)
105106
return;
106107
}
107108

108-
pos = cache_name_pos(path, strlen(path));
109+
pos = index_name_pos(repo->index, path, strlen(path));
109110
if (pos >= 0)
110111
; /* path is in the index */
111-
else if (-1 - pos < active_nr &&
112-
!strcmp(active_cache[-1 - pos]->name, path))
112+
else if (-1 - pos < repo->index->cache_nr &&
113+
!strcmp(repo->index->cache[-1 - pos]->name, path))
113114
; /* path is in the index, unmerged */
114115
else
115116
die("no such path '%s' in HEAD", path);
@@ -165,7 +166,8 @@ static void set_commit_buffer_from_strbuf(struct commit *c, struct strbuf *sb)
165166
* Prepare a dummy commit that represents the work tree (or staged) item.
166167
* Note that annotating work tree item never works in the reverse.
167168
*/
168-
static struct commit *fake_working_tree_commit(struct diff_options *opt,
169+
static struct commit *fake_working_tree_commit(struct repository *repo,
170+
struct diff_options *opt,
169171
const char *path,
170172
const char *contents_from)
171173
{
@@ -181,7 +183,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
181183
unsigned mode;
182184
struct strbuf msg = STRBUF_INIT;
183185

184-
read_cache();
186+
read_index(repo->index);
185187
time(&now);
186188
commit = alloc_commit_node(the_repository);
187189
commit->object.parsed = 1;
@@ -193,7 +195,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
193195

194196
parent_tail = append_parent(parent_tail, &head_oid);
195197
append_merge_parents(parent_tail);
196-
verify_working_tree_path(commit, path);
198+
verify_working_tree_path(repo, commit, path);
197199

198200
origin = make_origin(commit, path);
199201

@@ -251,7 +253,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
251253
if (strbuf_read(&buf, 0, 0) < 0)
252254
die_errno("failed to read from stdin");
253255
}
254-
convert_to_git(&the_index, path, buf.buf, buf.len, &buf, 0);
256+
convert_to_git(repo->index, path, buf.buf, buf.len, &buf, 0);
255257
origin->file.ptr = buf.buf;
256258
origin->file.size = buf.len;
257259
pretend_object_file(buf.buf, buf.len, OBJ_BLOB, &origin->blob_oid);
@@ -262,27 +264,28 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
262264
* bits; we are not going to write this index out -- we just
263265
* want to run "diff-index --cached".
264266
*/
265-
discard_cache();
266-
read_cache();
267+
discard_index(repo->index);
268+
read_index(repo->index);
267269

268270
len = strlen(path);
269271
if (!mode) {
270-
int pos = cache_name_pos(path, len);
272+
int pos = index_name_pos(repo->index, path, len);
271273
if (0 <= pos)
272-
mode = active_cache[pos]->ce_mode;
274+
mode = repo->index->cache[pos]->ce_mode;
273275
else
274276
/* Let's not bother reading from HEAD tree */
275277
mode = S_IFREG | 0644;
276278
}
277-
ce = make_empty_cache_entry(&the_index, len);
279+
ce = make_empty_cache_entry(repo->index, len);
278280
oidcpy(&ce->oid, &origin->blob_oid);
279281
memcpy(ce->name, path, len);
280282
ce->ce_flags = create_ce_flags(0);
281283
ce->ce_namelen = len;
282284
ce->ce_mode = create_ce_mode(mode);
283-
add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
285+
add_index_entry(repo->index, ce,
286+
ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
284287

285-
cache_tree_invalidate_path(&the_index, path);
288+
cache_tree_invalidate_path(repo->index, path);
286289

287290
return commit;
288291
}
@@ -519,13 +522,14 @@ static void queue_blames(struct blame_scoreboard *sb, struct blame_origin *porig
519522
*
520523
* This also fills origin->mode for corresponding tree path.
521524
*/
522-
static int fill_blob_sha1_and_mode(struct blame_origin *origin)
525+
static int fill_blob_sha1_and_mode(struct repository *repo,
526+
struct blame_origin *origin)
523527
{
524528
if (!is_null_oid(&origin->blob_oid))
525529
return 0;
526530
if (get_tree_entry(&origin->commit->object.oid, origin->path, &origin->blob_oid, &origin->mode))
527531
goto error_out;
528-
if (oid_object_info(the_repository, &origin->blob_oid, NULL) != OBJ_BLOB)
532+
if (oid_object_info(repo, &origin->blob_oid, NULL) != OBJ_BLOB)
529533
goto error_out;
530534
return 0;
531535
error_out:
@@ -1767,7 +1771,9 @@ void init_scoreboard(struct blame_scoreboard *sb)
17671771
sb->copy_score = BLAME_DEFAULT_COPY_SCORE;
17681772
}
17691773

1770-
void setup_scoreboard(struct blame_scoreboard *sb, const char *path, struct blame_origin **orig)
1774+
void setup_scoreboard(struct blame_scoreboard *sb,
1775+
const char *path,
1776+
struct blame_origin **orig)
17711777
{
17721778
const char *final_commit_name = NULL;
17731779
struct blame_origin *o;
@@ -1779,6 +1785,9 @@ void setup_scoreboard(struct blame_scoreboard *sb, const char *path, struct blam
17791785
if (sb->reverse && sb->contents_from)
17801786
die(_("--contents and --reverse do not blend well."));
17811787

1788+
if (!sb->repo)
1789+
BUG("repo is NULL");
1790+
17821791
if (!sb->reverse) {
17831792
sb->final = find_single_final(sb->revs, &final_commit_name);
17841793
sb->commits.compare = compare_commits_by_commit_date;
@@ -1800,7 +1809,8 @@ void setup_scoreboard(struct blame_scoreboard *sb, const char *path, struct blam
18001809
* or "--contents".
18011810
*/
18021811
setup_work_tree();
1803-
sb->final = fake_working_tree_commit(&sb->revs->diffopt,
1812+
sb->final = fake_working_tree_commit(sb->repo,
1813+
&sb->revs->diffopt,
18041814
path, sb->contents_from);
18051815
add_pending_object(sb->revs, &(sb->final->object), ":");
18061816
}
@@ -1845,7 +1855,7 @@ void setup_scoreboard(struct blame_scoreboard *sb, const char *path, struct blam
18451855
}
18461856
else {
18471857
o = get_origin(sb->final, path);
1848-
if (fill_blob_sha1_and_mode(o))
1858+
if (fill_blob_sha1_and_mode(sb->repo, o))
18491859
die(_("no such path %s in %s"), path, final_commit_name);
18501860

18511861
if (sb->revs->diffopt.flags.allow_textconv &&

blame.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ struct blame_scoreboard {
102102
struct commit *final;
103103
/* Priority queue for commits with unassigned blame records */
104104
struct prio_queue commits;
105+
struct repository *repo;
105106
struct rev_info *revs;
106107
const char *path;
107108

builtin/blame.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
988988
sb.revs = &revs;
989989
sb.contents_from = contents_from;
990990
sb.reverse = reverse;
991+
sb.repo = the_repository;
991992
setup_scoreboard(&sb, path, &o);
992993
lno = sb.num_lines;
993994

0 commit comments

Comments
 (0)