|
1 | 1 | #include "cache.h" |
2 | 2 | #include "commit.h" |
3 | 3 | #include "tag.h" |
| 4 | +#include "pkt-line.h" |
4 | 5 |
|
5 | 6 | static int is_shallow = -1; |
6 | 7 | static struct stat shallow_stat; |
@@ -141,3 +142,81 @@ void check_shallow_file_for_update(void) |
141 | 142 | ) |
142 | 143 | die("shallow file was changed during fetch"); |
143 | 144 | } |
| 145 | + |
| 146 | +struct write_shallow_data { |
| 147 | + struct strbuf *out; |
| 148 | + int use_pack_protocol; |
| 149 | + int count; |
| 150 | +}; |
| 151 | + |
| 152 | +static int write_one_shallow(const struct commit_graft *graft, void *cb_data) |
| 153 | +{ |
| 154 | + struct write_shallow_data *data = cb_data; |
| 155 | + const char *hex = sha1_to_hex(graft->sha1); |
| 156 | + if (graft->nr_parent != -1) |
| 157 | + return 0; |
| 158 | + data->count++; |
| 159 | + if (data->use_pack_protocol) |
| 160 | + packet_buf_write(data->out, "shallow %s", hex); |
| 161 | + else { |
| 162 | + strbuf_addstr(data->out, hex); |
| 163 | + strbuf_addch(data->out, '\n'); |
| 164 | + } |
| 165 | + return 0; |
| 166 | +} |
| 167 | + |
| 168 | +int write_shallow_commits(struct strbuf *out, int use_pack_protocol) |
| 169 | +{ |
| 170 | + struct write_shallow_data data; |
| 171 | + data.out = out; |
| 172 | + data.use_pack_protocol = use_pack_protocol; |
| 173 | + data.count = 0; |
| 174 | + for_each_commit_graft(write_one_shallow, &data); |
| 175 | + return data.count; |
| 176 | +} |
| 177 | + |
| 178 | +char *setup_temporary_shallow(void) |
| 179 | +{ |
| 180 | + struct strbuf sb = STRBUF_INIT; |
| 181 | + int fd; |
| 182 | + |
| 183 | + if (write_shallow_commits(&sb, 0)) { |
| 184 | + struct strbuf path = STRBUF_INIT; |
| 185 | + strbuf_addstr(&path, git_path("shallow_XXXXXX")); |
| 186 | + fd = xmkstemp(path.buf); |
| 187 | + if (write_in_full(fd, sb.buf, sb.len) != sb.len) |
| 188 | + die_errno("failed to write to %s", |
| 189 | + path.buf); |
| 190 | + close(fd); |
| 191 | + strbuf_release(&sb); |
| 192 | + return strbuf_detach(&path, NULL); |
| 193 | + } |
| 194 | + /* |
| 195 | + * is_repository_shallow() sees empty string as "no shallow |
| 196 | + * file". |
| 197 | + */ |
| 198 | + return xstrdup(""); |
| 199 | +} |
| 200 | + |
| 201 | +void setup_alternate_shallow(struct lock_file *shallow_lock, |
| 202 | + const char **alternate_shallow_file) |
| 203 | +{ |
| 204 | + struct strbuf sb = STRBUF_INIT; |
| 205 | + int fd; |
| 206 | + |
| 207 | + check_shallow_file_for_update(); |
| 208 | + fd = hold_lock_file_for_update(shallow_lock, git_path("shallow"), |
| 209 | + LOCK_DIE_ON_ERROR); |
| 210 | + if (write_shallow_commits(&sb, 0)) { |
| 211 | + if (write_in_full(fd, sb.buf, sb.len) != sb.len) |
| 212 | + die_errno("failed to write to %s", |
| 213 | + shallow_lock->filename); |
| 214 | + *alternate_shallow_file = shallow_lock->filename; |
| 215 | + } else |
| 216 | + /* |
| 217 | + * is_repository_shallow() sees empty string as "no |
| 218 | + * shallow file". |
| 219 | + */ |
| 220 | + *alternate_shallow_file = ""; |
| 221 | + strbuf_release(&sb); |
| 222 | +} |
0 commit comments