8 static FILE *info_ref_fp
;
10 static int add_info_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
12 struct object
*o
= parse_object(sha1
);
16 fprintf(info_ref_fp
, "%s %s\n", sha1_to_hex(sha1
), path
);
17 if (o
->type
== OBJ_TAG
) {
18 o
= deref_tag(o
, path
, 0);
20 fprintf(info_ref_fp
, "%s %s^{}\n",
21 sha1_to_hex(o
->sha1
), path
);
26 static int update_info_refs(int force
)
28 char *path0
= git_pathdup("info/refs");
29 int len
= strlen(path0
);
30 char *path1
= xmalloc(len
+ 2);
33 strcpy(path1
+ len
, "+");
35 safe_create_leading_directories(path0
);
36 info_ref_fp
= fopen(path1
, "w");
38 return error("unable to update %s", path1
);
39 for_each_ref(add_info_ref
, NULL
);
41 adjust_shared_perm(path1
);
49 static struct pack_info
{
55 unsigned char (*head
)[20];
58 static const char *objdir
;
61 static struct pack_info
*find_pack_by_name(const char *name
)
64 for (i
= 0; i
< num_pack
; i
++) {
65 struct packed_git
*p
= info
[i
]->p
;
66 /* skip "/pack/" after ".git/objects" */
67 if (!strcmp(p
->pack_name
+ objdirlen
+ 6, name
))
73 /* Returns non-zero when we detect that the info in the
74 * old file is useless.
76 static int parse_pack_def(const char *line
, int old_cnt
)
78 struct pack_info
*i
= find_pack_by_name(line
+ 2);
84 /* The file describes a pack that is no longer here */
89 /* Returns non-zero when we detect that the info in the
90 * old file is useless.
92 static int read_pack_info_file(const char *infofile
)
98 fp
= fopen(infofile
, "r");
100 return 1; /* nonexistent is not an error. */
102 while (fgets(line
, sizeof(line
), fp
)) {
103 int len
= strlen(line
);
104 if (len
&& line
[len
-1] == '\n')
111 case 'P': /* P name */
112 if (parse_pack_def(line
, old_cnt
++))
115 case 'D': /* we used to emit D but that was misguided. */
116 case 'T': /* we used to emit T but nobody uses it. */
119 error("unrecognized: %s", line
);
130 static int compare_info(const void *a_
, const void *b_
)
132 struct pack_info
*const *a
= a_
;
133 struct pack_info
*const *b
= b_
;
135 if (0 <= (*a
)->old_num
&& 0 <= (*b
)->old_num
)
136 /* Keep the order in the original */
137 return (*a
)->old_num
- (*b
)->old_num
;
138 else if (0 <= (*a
)->old_num
)
139 /* Only A existed in the original so B is obviously newer */
141 else if (0 <= (*b
)->old_num
)
142 /* The other way around. */
145 /* then it does not matter but at least keep the comparison stable */
146 if ((*a
)->p
== (*b
)->p
)
148 else if ((*a
)->p
< (*b
)->p
)
154 static void init_pack_info(const char *infofile
, int force
)
156 struct packed_git
*p
;
160 objdir
= get_object_directory();
161 objdirlen
= strlen(objdir
);
163 prepare_packed_git();
164 for (p
= packed_git
; p
; p
= p
->next
) {
165 /* we ignore things on alternate path since they are
166 * not available to the pullers in general.
173 info
= xcalloc(num_pack
, sizeof(struct pack_info
*));
174 for (i
= 0, p
= packed_git
; p
; p
= p
->next
) {
177 info
[i
] = xcalloc(1, sizeof(struct pack_info
));
179 info
[i
]->old_num
= -1;
183 if (infofile
&& !force
)
184 stale
= read_pack_info_file(infofile
);
188 for (i
= 0; i
< num_pack
; i
++) {
190 info
[i
]->old_num
= -1;
191 info
[i
]->nr_heads
= 0;
196 qsort(info
, num_pack
, sizeof(info
[0]), compare_info
);
197 for (i
= 0; i
< num_pack
; i
++)
198 info
[i
]->new_num
= i
;
201 static void write_pack_info_file(FILE *fp
)
204 for (i
= 0; i
< num_pack
; i
++)
205 fprintf(fp
, "P %s\n", info
[i
]->p
->pack_name
+ objdirlen
+ 6);
209 static int update_info_packs(int force
)
211 char infofile
[PATH_MAX
];
216 namelen
= sprintf(infofile
, "%s/info/packs", get_object_directory());
217 strcpy(name
, infofile
);
218 strcpy(name
+ namelen
, "+");
220 init_pack_info(infofile
, force
);
222 safe_create_leading_directories(name
);
223 fp
= fopen(name
, "w");
225 return error("cannot open %s", name
);
226 write_pack_info_file(fp
);
228 adjust_shared_perm(name
);
229 rename(name
, infofile
);
234 int update_server_info(int force
)
236 /* We would add more dumb-server support files later,
237 * including index of available pack files and their
238 * intended audiences.
242 errs
= errs
| update_info_refs(force
);
243 errs
= errs
| update_info_packs(force
);
245 /* remove leftover rev-cache file if there is any */
246 unlink_or_warn(git_path("info/rev-cache"));