5 static void show_new(char *type, unsigned char *sha1_new)
7 fprintf(stderr, " %s: %s\n", type,
8 find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
11 static int update_ref(const char *action,
14 unsigned char *oldval)
18 char *rla = getenv("GIT_REFLOG_ACTION");
19 static struct ref_lock *lock;
22 rla = "(reflog update)";
23 len = snprintf(msg, sizeof(msg), "%s: %s", rla, action);
24 if (sizeof(msg) <= len)
25 die("insanely long action");
26 lock = lock_any_ref_for_update(refname, oldval);
29 if (write_ref_sha1(lock, sha1, msg) < 0)
34 static int update_local_ref(const char *name,
37 int verbose, int force)
40 unsigned char sha1_old[20], sha1_new[20];
41 char oldh[41], newh[41];
42 struct commit *current, *updated;
44 if (get_sha1_hex(new_head, sha1_new))
45 die("malformed object name %s", new_head);
46 if (sha1_object_info(sha1_new, type, NULL))
47 die("object %s not found", new_head);
52 fprintf(stderr, "* fetched %s\n", note);
53 show_new(type, sha1_new);
58 if (get_sha1(name, sha1_old)) {
62 if (!strncmp(name, "refs/tags/", 10))
66 fprintf(stderr, "* %s: storing %s\n",
68 show_new(type, sha1_new);
69 return update_ref(msg, name, sha1_new, NULL);
72 if (!hashcmp(sha1_old, sha1_new)) {
74 fprintf(stderr, "* %s: same as %s\n", name, note);
75 show_new(type, sha1_new);
80 if (!strncmp(name, "refs/tags/", 10)) {
81 fprintf(stderr, "* %s: updating with %s\n", name, note);
82 show_new(type, sha1_new);
83 return update_ref("updating tag", name, sha1_new, NULL);
86 current = lookup_commit_reference(sha1_old);
87 updated = lookup_commit_reference(sha1_new);
88 if (!current || !updated)
91 strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
92 strcpy(newh, find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
94 if (in_merge_bases(current, &updated, 1)) {
95 fprintf(stderr, "* %s: fast forward to %s\n",
97 fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
98 return update_ref("fast forward", name, sha1_new, sha1_old);
102 "* %s: not updating to non-fast forward %s\n",
105 " old...new: %s...%s\n", oldh, newh);
109 "* %s: forcing update to non-fast forward %s\n",
111 fprintf(stderr, " old...new: %s...%s\n", oldh, newh);
112 return update_ref("forced-update", name, sha1_new, sha1_old);
115 static int append_fetch_head(FILE *fp,
116 const char *head, const char *remote,
117 const char *remote_name, const char *remote_nick,
118 const char *local_name, int not_for_merge,
119 int verbose, int force)
121 struct commit *commit;
122 int remote_len, i, note_len;
123 unsigned char sha1[20];
125 const char *what, *kind;
127 if (get_sha1(head, sha1))
128 return error("Not a valid object name: %s", head);
129 commit = lookup_commit_reference(sha1);
133 if (!strcmp(remote_name, "HEAD")) {
137 else if (!strncmp(remote_name, "refs/heads/", 11)) {
139 what = remote_name + 11;
141 else if (!strncmp(remote_name, "refs/tags/", 10)) {
143 what = remote_name + 10;
145 else if (!strncmp(remote_name, "refs/remotes/", 13)) {
146 kind = "remote branch";
147 what = remote_name + 13;
154 remote_len = strlen(remote);
155 for (i = remote_len - 1; remote[i] == '/' && 0 <= i; i--)
158 if (4 < i && !strncmp(".git", remote + i - 3, 4))
160 note_len = sprintf(note, "%s\t%s\t",
161 sha1_to_hex(commit ? commit->object.sha1 : sha1),
162 not_for_merge ? "not-for-merge" : "");
165 note_len += sprintf(note + note_len, "%s ", kind);
166 note_len += sprintf(note + note_len, "'%s' of ", what);
168 note_len += sprintf(note + note_len, "%.*s", remote_len, remote);
169 fprintf(fp, "%s\n", note);
170 return update_local_ref(local_name, head, note, verbose, force);
174 static void remove_keep(void)
180 static void remove_keep_on_signal(int signo)
183 signal(SIGINT, SIG_DFL);
187 static char *find_local_name(const char *remote_name, const char *refs,
188 int *force_p, int *not_for_merge_p)
190 const char *ref = refs;
191 int len = strlen(remote_name);
195 int single_force, not_for_merge;
201 next = strchr(ref, '\n');
203 single_force = not_for_merge = 0;
216 if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
217 const char *local_part = ref + len + 1;
222 retlen = strlen(local_part);
224 retlen = next - local_part;
225 ret = xmalloc(retlen + 1);
226 memcpy(ret, local_part, retlen);
228 *force_p = single_force;
229 *not_for_merge_p = not_for_merge;
237 static int fetch_native_store(FILE *fp,
239 const char *remote_nick,
241 int verbose, int force)
246 signal(SIGINT, remove_keep_on_signal);
249 while (fgets(buffer, sizeof(buffer), stdin)) {
253 int single_force, not_for_merge;
255 for (cp = buffer; *cp && !isspace(*cp); cp++)
260 if (len && cp[len-1] == '\n')
262 if (!strcmp(buffer, "failed"))
263 die("Fetch failure: %s", remote);
264 if (!strcmp(buffer, "pack"))
266 if (!strcmp(buffer, "keep")) {
267 char *od = get_object_directory();
268 int len = strlen(od) + strlen(cp) + 50;
270 sprintf(keep, "%s/pack/pack-%s.keep", od, cp);
274 local_name = find_local_name(cp, refs,
275 &single_force, ¬_for_merge);
278 err |= append_fetch_head(fp,
279 buffer, remote, cp, remote_nick,
280 local_name, not_for_merge,
281 verbose, force || single_force);
286 int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
292 const char *arg = argv[1];
293 if (!strcmp("-v", arg))
295 else if (!strcmp("-f", arg))
304 return error("Missing subcommand");
306 if (!strcmp("append-fetch-head", argv[1])) {
311 return error("append-fetch-head takes 6 args");
312 fp = fopen(git_path("FETCH_HEAD"), "a");
313 result = append_fetch_head(fp, argv[2], argv[3],
315 argv[6], !!argv[7][0],
320 if (!strcmp("update-local-ref", argv[1])) {
322 return error("update-local-ref takes 3 args");
323 return update_local_ref(argv[2], argv[3], argv[4],
326 if (!strcmp("native-store", argv[1])) {
331 return error("fetch-native-store takes 3 args");
332 fp = fopen(git_path("FETCH_HEAD"), "a");
333 result = fetch_native_store(fp, argv[2], argv[3], argv[4],
338 return error("Unknown subcommand: %s", argv[1]);