Skip to content

Commit 3125fe5

Browse files
pcloudsgitster
authored andcommitted
move setup_alternate_shallow and write_shallow_commits to shallow.c
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2c2b664 commit 3125fe5

File tree

3 files changed

+58
-52
lines changed

3 files changed

+58
-52
lines changed

commit.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ extern struct commit_list *get_shallow_commits(struct object_array *heads,
198198
int depth, int shallow_flag, int not_shallow_flag);
199199
extern void check_shallow_file_for_update(void);
200200
extern void set_alternate_shallow_file(const char *path);
201+
extern int write_shallow_commits(struct strbuf *out, int use_pack_protocol);
202+
extern void setup_alternate_shallow(struct lock_file *shallow_lock,
203+
const char **alternate_shallow_file);
201204

202205
int is_descendant_of(struct commit *, struct commit_list *);
203206
int in_merge_bases(struct commit *, struct commit *);

fetch-pack.c

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -184,36 +184,6 @@ static void consume_shallow_list(struct fetch_pack_args *args, int fd)
184184
}
185185
}
186186

187-
struct write_shallow_data {
188-
struct strbuf *out;
189-
int use_pack_protocol;
190-
int count;
191-
};
192-
193-
static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
194-
{
195-
struct write_shallow_data *data = cb_data;
196-
const char *hex = sha1_to_hex(graft->sha1);
197-
data->count++;
198-
if (data->use_pack_protocol)
199-
packet_buf_write(data->out, "shallow %s", hex);
200-
else {
201-
strbuf_addstr(data->out, hex);
202-
strbuf_addch(data->out, '\n');
203-
}
204-
return 0;
205-
}
206-
207-
static int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
208-
{
209-
struct write_shallow_data data;
210-
data.out = out;
211-
data.use_pack_protocol = use_pack_protocol;
212-
data.count = 0;
213-
for_each_commit_graft(write_one_shallow, &data);
214-
return data.count;
215-
}
216-
217187
static enum ack_type get_ack(int fd, unsigned char *result_sha1)
218188
{
219189
int len;
@@ -795,27 +765,6 @@ static int cmp_ref_by_name(const void *a_, const void *b_)
795765
return strcmp(a->name, b->name);
796766
}
797767

798-
static void setup_alternate_shallow(void)
799-
{
800-
struct strbuf sb = STRBUF_INIT;
801-
int fd;
802-
803-
check_shallow_file_for_update();
804-
fd = hold_lock_file_for_update(&shallow_lock, git_path("shallow"),
805-
LOCK_DIE_ON_ERROR);
806-
if (write_shallow_commits(&sb, 0)) {
807-
if (write_in_full(fd, sb.buf, sb.len) != sb.len)
808-
die_errno("failed to write to %s", shallow_lock.filename);
809-
alternate_shallow_file = shallow_lock.filename;
810-
} else
811-
/*
812-
* is_repository_shallow() sees empty string as "no
813-
* shallow file".
814-
*/
815-
alternate_shallow_file = "";
816-
strbuf_release(&sb);
817-
}
818-
819768
static struct ref *do_fetch_pack(struct fetch_pack_args *args,
820769
int fd[2],
821770
const struct ref *orig_ref,
@@ -896,7 +845,7 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
896845
if (args->stateless_rpc)
897846
packet_flush(fd[1]);
898847
if (args->depth > 0)
899-
setup_alternate_shallow();
848+
setup_alternate_shallow(&shallow_lock, &alternate_shallow_file);
900849
if (get_pack(args, fd, pack_lockfile))
901850
die("git fetch-pack: fetch failed.");
902851

shallow.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "cache.h"
22
#include "commit.h"
33
#include "tag.h"
4+
#include "pkt-line.h"
45

56
static int is_shallow = -1;
67
static struct stat shallow_stat;
@@ -141,3 +142,56 @@ void check_shallow_file_for_update(void)
141142
)
142143
die("shallow file was changed during fetch");
143144
}
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+
data->count++;
157+
if (data->use_pack_protocol)
158+
packet_buf_write(data->out, "shallow %s", hex);
159+
else {
160+
strbuf_addstr(data->out, hex);
161+
strbuf_addch(data->out, '\n');
162+
}
163+
return 0;
164+
}
165+
166+
int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
167+
{
168+
struct write_shallow_data data;
169+
data.out = out;
170+
data.use_pack_protocol = use_pack_protocol;
171+
data.count = 0;
172+
for_each_commit_graft(write_one_shallow, &data);
173+
return data.count;
174+
}
175+
176+
void setup_alternate_shallow(struct lock_file *shallow_lock,
177+
const char **alternate_shallow_file)
178+
{
179+
struct strbuf sb = STRBUF_INIT;
180+
int fd;
181+
182+
check_shallow_file_for_update();
183+
fd = hold_lock_file_for_update(shallow_lock, git_path("shallow"),
184+
LOCK_DIE_ON_ERROR);
185+
if (write_shallow_commits(&sb, 0)) {
186+
if (write_in_full(fd, sb.buf, sb.len) != sb.len)
187+
die_errno("failed to write to %s",
188+
shallow_lock->filename);
189+
*alternate_shallow_file = shallow_lock->filename;
190+
} else
191+
/*
192+
* is_repository_shallow() sees empty string as "no
193+
* shallow file".
194+
*/
195+
*alternate_shallow_file = "";
196+
strbuf_release(&sb);
197+
}

0 commit comments

Comments
 (0)