| 4696 | } |
| 4697 | |
| 4698 | static struct diff_tempfile *prepare_temp_file(struct repository *r, |
| 4699 | struct diff_filespec *one) |
| 4700 | { |
| 4701 | struct diff_tempfile *temp = claim_diff_tempfile(); |
| 4702 | |
| 4703 | if (!DIFF_FILE_VALID(one)) { |
| 4704 | not_a_valid_file: |
| 4705 | /* A '-' entry produces this for file-2, and |
| 4706 | * a '+' entry produces this for file-1. |
| 4707 | */ |
| 4708 | temp->name = "/dev/null"; |
| 4709 | xsnprintf(temp->hex, sizeof(temp->hex), "."); |
| 4710 | xsnprintf(temp->mode, sizeof(temp->mode), "."); |
| 4711 | return temp; |
| 4712 | } |
| 4713 | |
| 4714 | if (!S_ISGITLINK(one->mode) && |
| 4715 | (!one->oid_valid || |
| 4716 | reuse_worktree_file(r->index, one->path, &one->oid, 1))) { |
| 4717 | struct stat st; |
| 4718 | if (lstat(one->path, &st) < 0) { |
| 4719 | if (errno == ENOENT) |
| 4720 | goto not_a_valid_file; |
| 4721 | die_errno("stat(%s)", one->path); |
| 4722 | } |
| 4723 | if (S_ISLNK(st.st_mode)) { |
| 4724 | struct strbuf sb = STRBUF_INIT; |
| 4725 | if (strbuf_readlink(&sb, one->path, st.st_size) < 0) |
| 4726 | die_errno("readlink(%s)", one->path); |
| 4727 | prep_temp_blob(r->index, one->path, temp, sb.buf, sb.len, |
| 4728 | (one->oid_valid ? |
| 4729 | &one->oid : null_oid(the_hash_algo)), |
| 4730 | (one->oid_valid ? |
| 4731 | one->mode : S_IFLNK)); |
| 4732 | strbuf_release(&sb); |
| 4733 | } |
| 4734 | else { |
| 4735 | /* we can borrow from the file in the work tree */ |
| 4736 | temp->name = one->path; |
| 4737 | if (!one->oid_valid) |
| 4738 | oid_to_hex_r(temp->hex, null_oid(the_hash_algo)); |
| 4739 | else |
| 4740 | oid_to_hex_r(temp->hex, &one->oid); |
| 4741 | /* Even though we may sometimes borrow the |
| 4742 | * contents from the work tree, we always want |
| 4743 | * one->mode. mode is trustworthy even when |
| 4744 | * !(one->oid_valid), as long as |
| 4745 | * DIFF_FILE_VALID(one). |
| 4746 | */ |
| 4747 | xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode); |
| 4748 | } |
| 4749 | return temp; |
| 4750 | } |
| 4751 | else { |
| 4752 | if (diff_populate_filespec(r, one, NULL)) |
| 4753 | die("cannot read data blob for %s", one->path); |
| 4754 | prep_temp_blob(r->index, one->path, temp, |
| 4755 | one->data, one->size, |
no test coverage detected