-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch.c
110 lines (86 loc) · 2.4 KB
/
fetch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "common.h"
#include <git2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void show_refs(git_headarray *refs)
{
int i;
git_remote_head *head;
if(refs->len == 0)
puts("Everything up-to-date");
for(i = 0; i < refs->len; ++i){
char oid[GIT_OID_HEXSZ + 1] = {0};
char *havewant;
head = refs->heads[i];
git_oid_fmt(oid, &head->oid);
printf("%s\t%s\n", oid, head->name);
}
}
static int rename_packfile(char *packname, git_indexer *idx)
{
char path[GIT_PATH_MAX], oid[GIT_OID_HEXSZ + 1], *slash;
int ret;
strcpy(path, packname);
slash = strrchr(path, '/');
if (!slash)
return GIT_EINVALIDARGS;
memset(oid, 0x0, sizeof(oid));
git_oid_fmt(oid, git_indexer_hash(idx));
ret = sprintf(slash + 1, "pack-%s.pack", oid);
if(ret < 0)
return GIT_ERROR;
printf("Renameing pack to %s\n", path);
return rename(packname, path);
}
int fetch(git_repository *repo, int argc, char **argv)
{
git_remote *remote = NULL;
git_config *cfg = NULL;
git_indexer *idx = NULL;
git_indexer_stats stats;
int error;
char *packname = NULL;
error = git_repository_config(&cfg, repo, NULL, NULL);
if (error < GIT_SUCCESS)
return error;
printf("Fetching %s\n", argv[1]);
error = git_remote_get(&remote, cfg, argv[1]);
if (error < GIT_SUCCESS)
return error;
error = git_remote_connect(remote, GIT_DIR_FETCH);
if (error < GIT_SUCCESS)
return error;
error = git_remote_negotiate(remote);
if (error < GIT_SUCCESS)
return error;
error = git_remote_download(&packname, remote);
if (error < GIT_SUCCESS)
return error;
/* No error and a NULL packname means no packfile was needed */
if (packname != NULL) {
printf("The packname is %s\n", packname);
error = git_indexer_new(&idx, packname);
if (error < GIT_SUCCESS)
return error;
/* This should be run in paralel, but it'd be too complicated right now */
error = git_indexer_run(idx, &stats);
if (error < GIT_SUCCESS)
return error;
printf("Received %d objects\n", stats.total);
error = git_indexer_write(idx);
if (error < GIT_SUCCESS)
return error;
error = rename_packfile(packname, idx);
if (error < GIT_SUCCESS)
return error;
}
error = git_remote_update_tips(remote);
if (error < GIT_SUCCESS)
return error;
free(packname);
git_indexer_free(idx);
git_remote_free(remote);
git_config_free(cfg);
return GIT_SUCCESS;
}