@matheusbsbs / matheus-bagliano / commits / d4289fff87
Fork 0

git-fetch--tool: start rewriting parts of git-fetch in C.

Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Jan 16, 2007 at 00:23 UTC d4289fff870a85b1b7e55ead2a5baf98847fc72a
5 files changed +227 -124
Makefile
+1
@@ -282,6 +282,7 @@ BUILTIN_OBJS = \
282 builtin-diff-index.o \
283 builtin-diff-stages.o \
284 builtin-diff-tree.o \
285 + builtin-fetch--tool.o \
286 builtin-fmt-merge-msg.o \
287 builtin-for-each-ref.o \
288 builtin-fsck.o \
builtin-fetch--tool.c new
+214
@@ -0,0 +1,214 @@
1 +#include "cache.h"
2 +#include "refs.h"
3 +#include "commit.h"
4 +
5 +static void show_new(char *type, unsigned char *sha1_new)
6 +{
7 + fprintf(stderr, " %s: %s\n", type,
8 + find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
9 +}
10 +
11 +static int update_ref(const char *action,
12 + const char *refname,
13 + unsigned char *sha1,
14 + unsigned char *oldval)
15 +{
16 + int len;
17 + char msg[1024];
18 + char *rla = getenv("GIT_REFLOG_ACTION");
19 + static struct ref_lock *lock;
20 +
21 + if (!rla)
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);
27 + if (!lock)
28 + return 1;
29 + if (write_ref_sha1(lock, sha1, msg) < 0)
30 + return 1;
31 + return 0;
32 +}
33 +
34 +static int update_local_ref(const char *name,
35 + const char *new_head,
36 + const char *note,
37 + int verbose, int force)
38 +{
39 + char type[20];
40 + unsigned char sha1_old[20], sha1_new[20];
41 + char oldh[41], newh[41];
42 + struct commit *current, *updated;
43 +
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);
48 +
49 + if (!*name) {
50 + /* Not storing */
51 + if (verbose) {
52 + fprintf(stderr, "* fetched %s\n", note);
53 + show_new(type, sha1_new);
54 + }
55 + return 0;
56 + }
57 +
58 + if (get_sha1(name, sha1_old)) {
59 + char *msg;
60 + just_store:
61 + /* new ref */
62 + if (!strncmp(name, "refs/tags/", 10))
63 + msg = "storing tag";
64 + else
65 + msg = "storing head";
66 + fprintf(stderr, "* %s: storing %s\n",
67 + name, note);
68 + show_new(type, sha1_new);
69 + return update_ref(msg, name, sha1_new, NULL);
70 + }
71 +
72 + if (!hashcmp(sha1_old, sha1_new)) {
73 + if (verbose) {
74 + fprintf(stderr, "* %s: same as %s\n", name, note);
75 + show_new(type, sha1_new);
76 + }
77 + return 0;
78 + }
79 +
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);
84 + }
85 +
86 + current = lookup_commit_reference(sha1_old);
87 + updated = lookup_commit_reference(sha1_new);
88 + if (!current || !updated)
89 + goto just_store;
90 +
91 + strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
92 + strcpy(newh, find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
93 +
94 + if (in_merge_bases(current, &updated, 1)) {
95 + fprintf(stderr, "* %s: fast forward to %s\n",
96 + name, note);
97 + fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
98 + return update_ref("fast forward", name, sha1_new, sha1_old);
99 + }
100 + if (!force) {
101 + fprintf(stderr,
102 + "* %s: not updating to non-fast forward %s\n",
103 + name, note);
104 + fprintf(stderr,
105 + " old...new: %s...%s\n", oldh, newh);
106 + return 1;
107 + }
108 + fprintf(stderr,
109 + "* %s: forcing update to non-fast forward %s\n",
110 + name, note);
111 + fprintf(stderr, " old...new: %s...%s\n", oldh, newh);
112 + return update_ref("forced-update", name, sha1_new, sha1_old);
113 +}
114 +
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)
120 +{
121 + struct commit *commit;
122 + int remote_len, i, note_len;
123 + unsigned char sha1[20];
124 + char note[1024];
125 + const char *what, *kind;
126 +
127 + if (get_sha1(head, sha1))
128 + return error("Not a valid object name: %s", head);
129 + commit = lookup_commit_reference(sha1);
130 + if (!commit)
131 + not_for_merge = 1;
132 +
133 + if (!strcmp(remote_name, "HEAD")) {
134 + kind = "";
135 + what = "";
136 + }
137 + else if (!strncmp(remote_name, "refs/heads/", 11)) {
138 + kind = "branch";
139 + what = remote_name + 11;
140 + }
141 + else if (!strncmp(remote_name, "refs/tags/", 10)) {
142 + kind = "tag";
143 + what = remote_name + 10;
144 + }
145 + else if (!strncmp(remote_name, "refs/remotes/", 13)) {
146 + kind = "remote branch";
147 + what = remote_name + 13;
148 + }
149 + else {
150 + kind = "";
151 + what = remote_name;
152 + }
153 +
154 + remote_len = strlen(remote);
155 + for (i = remote_len - 1; remote[i] == '/' && 0 <= i; i--)
156 + ;
157 + remote_len = i + 1;
158 + if (4 < i && !strncmp(".git", remote + i - 3, 4))
159 + remote_len = i - 3;
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" : "");
163 + if (*what) {
164 + if (*kind)
165 + note_len += sprintf(note + note_len, "%s ", kind);
166 + note_len += sprintf(note + note_len, "'%s' of ", what);
167 + }
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);
171 +}
172 +
173 +int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
174 +{
175 + int verbose = 0;
176 + int force = 0;
177 +
178 + while (1 < argc) {
179 + const char *arg = argv[1];
180 + if (!strcmp("-v", arg))
181 + verbose = 1;
182 + else if (!strcmp("-f", arg))
183 + force = 1;
184 + else
185 + break;
186 + argc--;
187 + argv++;
188 + }
189 +
190 + if (argc <= 1)
191 + return error("Missing subcommand");
192 +
193 + if (!strcmp("append-fetch-head", argv[1])) {
194 + int result;
195 + FILE *fp;
196 +
197 + if (argc != 8)
198 + return error("append-fetch-head takes 6 args");
199 + fp = fopen(git_path("FETCH_HEAD"), "a");
200 + result = append_fetch_head(fp, argv[2], argv[3],
201 + argv[4], argv[5],
202 + argv[6], !!argv[7][0],
203 + verbose, force);
204 + fclose(fp);
205 + return result;
206 + }
207 + if (!strcmp("update-local-ref", argv[1])) {
208 + if (argc != 5)
209 + return error("update-local-ref takes 3 args");
210 + return update_local_ref(argv[2], argv[3], argv[4],
211 + verbose, force);
212 + }
213 + return error("Unknown subcommand: %s", argv[1]);
214 +}
builtin.h
+1
@@ -31,6 +31,7 @@ extern int cmd_diff_index(int argc, const char **argv, const char *prefix);
31 extern int cmd_diff(int argc, const char **argv, const char *prefix);
32 extern int cmd_diff_stages(int argc, const char **argv, const char *prefix);
33 extern int cmd_diff_tree(int argc, const char **argv, const char *prefix);
34 +extern int cmd_fetch__tool(int argc, const char **argv, const char *prefix);
35 extern int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix);
36 extern int cmd_for_each_ref(int argc, const char **argv, const char *prefix);
37 extern int cmd_format_patch(int argc, const char **argv, const char *prefix);
git-fetch.sh
+10 -124
@@ -107,133 +107,19 @@ ls_remote_result=$(git ls-remote $exec "$remote") ||
107 die "Cannot get the repository state from $remote"
108
109 append_fetch_head () {
110 - head_="$1"
111 - remote_="$2"
112 - remote_name_="$3"
113 - remote_nick_="$4"
114 - local_name_="$5"
115 - case "$6" in
116 - t) not_for_merge_='not-for-merge' ;;
117 - '') not_for_merge_= ;;
118 - esac
119 -
120 - # remote-nick is the URL given on the command line (or a shorthand)
121 - # remote-name is the $GIT_DIR relative refs/ path we computed
122 - # for this refspec.
123 -
124 - # the $note_ variable will be fed to git-fmt-merge-msg for further
125 - # processing.
126 - case "$remote_name_" in
127 - HEAD)
128 - note_= ;;
129 - refs/heads/*)
130 - note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
131 - note_="branch '$note_' of " ;;
132 - refs/tags/*)
133 - note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
134 - note_="tag '$note_' of " ;;
135 - refs/remotes/*)
136 - note_="$(expr "$remote_name_" : 'refs/remotes/\(.*\)')"
137 - note_="remote branch '$note_' of " ;;
138 - *)
139 - note_="$remote_name of " ;;
140 - esac
141 - remote_1_=$(expr "z$remote_" : 'z\(.*\)\.git/*$') &&
142 - remote_="$remote_1_"
143 - note_="$note_$remote_"
144 -
145 - # 2.6.11-tree tag would not be happy to be fed to resolve.
146 - if git-cat-file commit "$head_" >/dev/null 2>&1
147 - then
148 - headc_=$(git-rev-parse --verify "$head_^0") || exit
149 - echo "$headc_ $not_for_merge_ $note_" >>"$GIT_DIR/FETCH_HEAD"
150 - else
151 - echo "$head_ not-for-merge $note_" >>"$GIT_DIR/FETCH_HEAD"
152 - fi
153 -
154 - update_local_ref "$local_name_" "$head_" "$note_"
110 + flags=
111 + test -n "$verbose" && flags="$flags -v"
112 + test -n "$force" && flags="$flags -f"
113 + GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
114 + git-fetch--tool append-fetch-head $flags "$@"
115 }
116
117 update_local_ref () {
158 - # If we are storing the head locally make sure that it is
159 - # a fast forward (aka "reverse push").
160 -
161 - label_=$(git-cat-file -t $2)
162 - newshort_=$(git-rev-parse --short $2)
163 - if test -z "$1" ; then
164 - [ "$verbose" ] && echo >&2 "* fetched $3"
165 - [ "$verbose" ] && echo >&2 " $label_: $newshort_"
166 - return 0
167 - fi
168 - oldshort_=$(git show-ref --hash --abbrev "$1" 2>/dev/null)
169 -
170 - case "$1" in
171 - refs/tags/*)
172 - # Tags need not be pointing at commits so there
173 - # is no way to guarantee "fast-forward" anyway.
174 - if test -n "$oldshort_"
175 - then
176 - if now_=$(git show-ref --hash "$1") && test "$now_" = "$2"
177 - then
178 - [ "$verbose" ] && echo >&2 "* $1: same as $3"
179 - [ "$verbose" ] && echo >&2 " $label_: $newshort_" ||:
180 - else
181 - echo >&2 "* $1: updating with $3"
182 - echo >&2 " $label_: $newshort_"
183 - git-update-ref -m "$GIT_REFLOG_ACTION: updating tag" "$1" "$2"
184 - fi
185 - else
186 - echo >&2 "* $1: storing $3"
187 - echo >&2 " $label_: $newshort_"
188 - git-update-ref -m "$GIT_REFLOG_ACTION: storing tag" "$1" "$2"
189 - fi
190 - ;;
191 -
192 - refs/heads/* | refs/remotes/*)
193 - # $1 is the ref being updated.
194 - # $2 is the new value for the ref.
195 - local=$(git-rev-parse --verify "$1^0" 2>/dev/null)
196 - if test "$local"
197 - then
198 - # Require fast-forward.
199 - mb=$(git-merge-base "$local" "$2") &&
200 - case "$2,$mb" in
201 - $local,*)
202 - if test -n "$verbose"
203 - then
204 - echo >&2 "* $1: same as $3"
205 - echo >&2 " $label_: $newshort_"
206 - fi
207 - ;;
208 - *,$local)
209 - echo >&2 "* $1: fast forward to $3"
210 - echo >&2 " old..new: $oldshort_..$newshort_"
211 - git-update-ref -m "$GIT_REFLOG_ACTION: fast-forward" "$1" "$2" "$local"
212 - ;;
213 - *)
214 - false
215 - ;;
216 - esac || {
217 - case ",$force,$single_force," in
218 - *,t,*)
219 - echo >&2 "* $1: forcing update to non-fast forward $3"
220 - echo >&2 " old...new: $oldshort_...$newshort_"
221 - git-update-ref -m "$GIT_REFLOG_ACTION: forced-update" "$1" "$2" "$local"
222 - ;;
223 - *)
224 - echo >&2 "* $1: not updating to non-fast forward $3"
225 - echo >&2 " old...new: $oldshort_...$newshort_"
226 - exit 1
227 - ;;
228 - esac
229 - }
230 - else
231 - echo >&2 "* $1: storing $3"
232 - echo >&2 " $label_: $newshort_"
233 - git-update-ref -m "$GIT_REFLOG_ACTION: storing head" "$1" "$2"
234 - fi
235 - ;;
236 - esac
118 + flags=
119 + test -n "$verbose" && flags="$flags -v"
120 + test -n "$force" && flags="$flags -f"
121 + GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
122 + git-fetch--tool update-local-ref $flags "$@"
123 }
124
125 # updating the current HEAD with git-fetch in a bare
git.c
+1
@@ -242,6 +242,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
242 { "diff-index", cmd_diff_index, RUN_SETUP },
243 { "diff-stages", cmd_diff_stages, RUN_SETUP },
244 { "diff-tree", cmd_diff_tree, RUN_SETUP },
245 + { "fetch--tool", cmd_fetch__tool, RUN_SETUP },
246 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
247 { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
248 { "format-patch", cmd_format_patch, RUN_SETUP },