Commit ea8178638c22887ec340b822b59a27e6cbbc888f

Ben Straub 2012-07-09T20:32:42

Tabify.

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
diff --git a/src/clone.c b/src/clone.c
index 5c83bde..d8d3503 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -28,128 +28,128 @@
 GIT_BEGIN_DECL
 
 struct HeadInfo {
-   git_repository *repo;
-   git_oid remote_head_oid;
-   git_buf branchname;
+	git_repository *repo;
+	git_oid remote_head_oid;
+	git_buf branchname;
 };
 
 static int create_tracking_branch(git_repository *repo, const git_oid *target, const char *name)
 {
-   git_object *head_obj = NULL;
-   git_oid branch_oid;
-   int retcode = GIT_ERROR;
-
-   /* Find the target commit */
-   if (git_object_lookup(&head_obj, repo, target, GIT_OBJ_ANY) < 0)
-      return GIT_ERROR;
-
-   /* Create the new branch */
-   if (!git_branch_create(&branch_oid, repo, name, head_obj, 0)) {
-      /* Set up tracking */
-      git_config *cfg;
-      if (!git_repository_config(&cfg, repo)) {
-         git_buf remote = GIT_BUF_INIT;
-         git_buf merge = GIT_BUF_INIT;
-         git_buf merge_target = GIT_BUF_INIT;
-         if (!git_buf_printf(&remote, "branch.%s.remote", name) &&
-             !git_buf_printf(&merge, "branch.%s.merge", name) &&
-             !git_buf_printf(&merge_target, "refs/heads/%s", name) &&
-             !git_config_set_string(cfg, git_buf_cstr(&remote), "origin") &&
-             !git_config_set_string(cfg, git_buf_cstr(&merge), git_buf_cstr(&merge_target))) {
-            retcode = 0;
-         }
-         git_buf_free(&remote);
-         git_buf_free(&merge);
-         git_buf_free(&merge_target);
-         git_config_free(cfg);
-      }
-   }
-
-   git_object_free(head_obj);
-   return retcode;
+	git_object *head_obj = NULL;
+	git_oid branch_oid;
+	int retcode = GIT_ERROR;
+
+	/* Find the target commit */
+	if (git_object_lookup(&head_obj, repo, target, GIT_OBJ_ANY) < 0)
+		return GIT_ERROR;
+
+	/* Create the new branch */
+	if (!git_branch_create(&branch_oid, repo, name, head_obj, 0)) {
+		/* Set up tracking */
+		git_config *cfg;
+		if (!git_repository_config(&cfg, repo)) {
+			git_buf remote = GIT_BUF_INIT;
+			git_buf merge = GIT_BUF_INIT;
+			git_buf merge_target = GIT_BUF_INIT;
+			if (!git_buf_printf(&remote, "branch.%s.remote", name) &&
+				 !git_buf_printf(&merge, "branch.%s.merge", name) &&
+				 !git_buf_printf(&merge_target, "refs/heads/%s", name) &&
+				 !git_config_set_string(cfg, git_buf_cstr(&remote), "origin") &&
+				 !git_config_set_string(cfg, git_buf_cstr(&merge), git_buf_cstr(&merge_target))) {
+				retcode = 0;
+			}
+			git_buf_free(&remote);
+			git_buf_free(&merge);
+			git_buf_free(&merge_target);
+			git_config_free(cfg);
+		}
+	}
+
+	git_object_free(head_obj);
+	return retcode;
 }
 
 static int reference_matches_remote_head(const char *head_name, void *payload)
 {
-   struct HeadInfo *head_info = (struct HeadInfo *)payload;
-   git_oid oid;
-
-   /* Stop looking if we've already found a match */
-   if (git_buf_len(&head_info->branchname) > 0) return 0;
-
-   if (!git_reference_name_to_oid(&oid, head_info->repo, head_name) &&
-       !git_oid_cmp(&head_info->remote_head_oid, &oid)) {
-      git_buf_puts(&head_info->branchname,
-                   head_name+strlen("refs/remotes/origin/"));
-   }
-   return 0;
+	struct HeadInfo *head_info = (struct HeadInfo *)payload;
+	git_oid oid;
+
+	/* Stop looking if we've already found a match */
+	if (git_buf_len(&head_info->branchname) > 0) return 0;
+
+	if (!git_reference_name_to_oid(&oid, head_info->repo, head_name) &&
+		 !git_oid_cmp(&head_info->remote_head_oid, &oid)) {
+		git_buf_puts(&head_info->branchname,
+						 head_name+strlen("refs/remotes/origin/"));
+	}
+	return 0;
 }
 
 static int update_head_to_new_branch(git_repository *repo, const git_oid *target, const char *name)
 {
-   int retcode = GIT_ERROR;
-
-   if (!create_tracking_branch(repo, target, name)) {
-      git_reference *head;
-      if (!git_reference_lookup(&head, repo, GIT_HEAD_FILE)) {
-         git_buf targetbuf = GIT_BUF_INIT;
-         if (!git_buf_printf(&targetbuf, "refs/heads/%s", name) &&
-             !git_reference_set_target(head, git_buf_cstr(&targetbuf))) {
-            /* Read the tree into the index */
-            git_commit *commit;
-            if (!git_commit_lookup(&commit, repo, target)) {
-               git_tree *tree;
-               if (!git_commit_tree(&tree, commit)) {
-                  git_index *index;
-                  if (!git_repository_index(&index, repo)) {
-                     if (!git_index_read_tree(index, tree)) {
-                        git_index_write(index);
-                        retcode = 0;
-                     }
-                     git_index_free(index);
-                  }
-                  git_tree_free(tree);
-               }
-               git_commit_free(commit);
-            }
-         }
-         git_buf_free(&targetbuf);
-         git_reference_free(head);
-      }
-   }
-
-   return retcode;
+	int retcode = GIT_ERROR;
+
+	if (!create_tracking_branch(repo, target, name)) {
+		git_reference *head;
+		if (!git_reference_lookup(&head, repo, GIT_HEAD_FILE)) {
+			git_buf targetbuf = GIT_BUF_INIT;
+			if (!git_buf_printf(&targetbuf, "refs/heads/%s", name) &&
+				 !git_reference_set_target(head, git_buf_cstr(&targetbuf))) {
+				/* Read the tree into the index */
+				git_commit *commit;
+				if (!git_commit_lookup(&commit, repo, target)) {
+					git_tree *tree;
+					if (!git_commit_tree(&tree, commit)) {
+						git_index *index;
+						if (!git_repository_index(&index, repo)) {
+							if (!git_index_read_tree(index, tree)) {
+								git_index_write(index);
+								retcode = 0;
+							}
+							git_index_free(index);
+						}
+						git_tree_free(tree);
+					}
+					git_commit_free(commit);
+				}
+			}
+			git_buf_free(&targetbuf);
+			git_reference_free(head);
+		}
+	}
+
+	return retcode;
 }
 
 static int update_head_to_remote(git_repository *repo, git_remote *remote)
 {
-   int retcode = GIT_ERROR;
-   git_remote_head *remote_head;
-   git_oid oid;
-   struct HeadInfo head_info;
-
-   /* Get the remote's HEAD. This is always the first ref in remote->refs. */
-   remote_head = remote->refs.contents[0];
-   git_oid_cpy(&head_info.remote_head_oid, &remote_head->oid);
-   git_buf_init(&head_info.branchname, 16);
-   head_info.repo = repo;
-
-   /* Check to see if "master" matches the remote head */
-   if (!git_reference_name_to_oid(&oid, repo, "refs/remotes/origin/master") &&
-       !git_oid_cmp(&remote_head->oid, &oid)) {
-      retcode = update_head_to_new_branch(repo, &oid, "master");
-   }
-   /* Not master. Check all the other refs. */
-   else if (!git_reference_foreach(repo, GIT_REF_LISTALL,
-                                     reference_matches_remote_head,
-                                     &head_info) &&
-            git_buf_len(&head_info.branchname) > 0) {
-      retcode = update_head_to_new_branch(repo, &head_info.remote_head_oid,
-                                          git_buf_cstr(&head_info.branchname));
-   }
-
-   git_buf_free(&head_info.branchname);
-   return retcode;
+	int retcode = GIT_ERROR;
+	git_remote_head *remote_head;
+	git_oid oid;
+	struct HeadInfo head_info;
+
+	/* Get the remote's HEAD. This is always the first ref in remote->refs. */
+	remote_head = remote->refs.contents[0];
+	git_oid_cpy(&head_info.remote_head_oid, &remote_head->oid);
+	git_buf_init(&head_info.branchname, 16);
+	head_info.repo = repo;
+
+	/* Check to see if "master" matches the remote head */
+	if (!git_reference_name_to_oid(&oid, repo, "refs/remotes/origin/master") &&
+		 !git_oid_cmp(&remote_head->oid, &oid)) {
+		retcode = update_head_to_new_branch(repo, &oid, "master");
+	}
+	/* Not master. Check all the other refs. */
+	else if (!git_reference_foreach(repo, GIT_REF_LISTALL,
+												 reference_matches_remote_head,
+												 &head_info) &&
+				git_buf_len(&head_info.branchname) > 0) {
+		retcode = update_head_to_new_branch(repo, &head_info.remote_head_oid,
+														git_buf_cstr(&head_info.branchname));
+	}
+
+	git_buf_free(&head_info.branchname);
+	return retcode;
 }
 
 /*
@@ -161,151 +161,151 @@ static int update_head_to_remote(git_repository *repo, git_remote *remote)
 
 
 static int setup_remotes_and_fetch(git_repository *repo,
-                                   const char *origin_url,
-                                   git_indexer_stats *stats)
+											  const char *origin_url,
+											  git_indexer_stats *stats)
 {
-   int retcode = GIT_ERROR;
-   git_remote *origin = NULL;
-   git_off_t bytes = 0;
-   git_indexer_stats dummy_stats;
-
-   if (!stats) stats = &dummy_stats;
-
-   /* Create the "origin" remote */
-   if (!git_remote_add(&origin, repo, "origin", origin_url)) {
-      /* Connect and download everything */
-      if (!git_remote_connect(origin, GIT_DIR_FETCH)) {
-         if (!git_remote_download(origin, &bytes, stats)) {
-            /* Create "origin/foo" branches for all remote branches */
-            if (!git_remote_update_tips(origin, NULL)) {
-               /* Point HEAD to the same ref as the remote's head */
-               if (!update_head_to_remote(repo, origin)) {
-                  retcode = 0;
-               }
-            }
-         }
-         git_remote_disconnect(origin);
-      }
-      git_remote_free(origin);
-   }
-
-   return retcode;
+	int retcode = GIT_ERROR;
+	git_remote *origin = NULL;
+	git_off_t bytes = 0;
+	git_indexer_stats dummy_stats;
+
+	if (!stats) stats = &dummy_stats;
+
+	/* Create the "origin" remote */
+	if (!git_remote_add(&origin, repo, "origin", origin_url)) {
+		/* Connect and download everything */
+		if (!git_remote_connect(origin, GIT_DIR_FETCH)) {
+			if (!git_remote_download(origin, &bytes, stats)) {
+				/* Create "origin/foo" branches for all remote branches */
+				if (!git_remote_update_tips(origin, NULL)) {
+					/* Point HEAD to the same ref as the remote's head */
+					if (!update_head_to_remote(repo, origin)) {
+						retcode = 0;
+					}
+				}
+			}
+			git_remote_disconnect(origin);
+		}
+		git_remote_free(origin);
+	}
+
+	return retcode;
 }
 
 
 static bool is_dot_or_dotdot(const char *name)
 {
-   return (name[0] == '.' &&
-           (name[1] == '\0' ||
-            (name[1] == '.' && name[2] == '\0')));
+	return (name[0] == '.' &&
+			  (name[1] == '\0' ||
+				(name[1] == '.' && name[2] == '\0')));
 }
 
 /* TODO: p_opendir, p_closedir */
 static bool path_is_okay(const char *path)
 {
 #ifdef GIT_WIN32
-   HANDLE hFind = INVALID_HANDLE_VALUE;
-   wchar_t *wbuf;
-   WIN32_FIND_DATAW ffd;
+	HANDLE hFind = INVALID_HANDLE_VALUE;
+	wchar_t *wbuf;
+	WIN32_FIND_DATAW ffd;
 #else
-   DIR *dir = NULL;
-   struct dirent *e;
+	DIR *dir = NULL;
+	struct dirent *e;
 #endif
 
-   bool retval = true;
+	bool retval = true;
 
-   /* The path must either not exist, or be an empty directory */
-   if (!git_path_exists(path)) return true;
+	/* The path must either not exist, or be an empty directory */
+	if (!git_path_exists(path)) return true;
 
-   if (!git_path_isdir(path)) {
-      giterr_set(GITERR_INVALID,
-                 "'%s' exists and is not an empty directory", path);
-      return false;
-   }
+	if (!git_path_isdir(path)) {
+		giterr_set(GITERR_INVALID,
+					  "'%s' exists and is not an empty directory", path);
+		return false;
+	}
 
 #ifdef GIT_WIN32
-   wbuf = gitwin_to_utf16(path);
-   gitwin_append_utf16(wbuf, "\\*", 2);
-   hFind = FindFirstFileW(wbuf, &ffd);
-   if (INVALID_HANDLE_VALUE != hFind) {
-      retval = false;
-      FindClose(hFind);
-   }
-   git__free(wbuf);
+	wbuf = gitwin_to_utf16(path);
+	gitwin_append_utf16(wbuf, "\\*", 2);
+	hFind = FindFirstFileW(wbuf, &ffd);
+	if (INVALID_HANDLE_VALUE != hFind) {
+		retval = false;
+		FindClose(hFind);
+	}
+	git__free(wbuf);
 #else
-   dir = opendir(path);
-   if (!dir) {
-      giterr_set(GITERR_OS, "Couldn't open '%s'", path);
-      return false;
-   }
-
-   while ((e = readdir(dir)) != NULL) {
-      if (!is_dot_or_dotdot(e->d_name)) {
-         giterr_set(GITERR_INVALID,
-                    "'%s' exists and is not an empty directory", path);
-         retval = false;
-         break;
-      }
-   }
-   closedir(dir);
+	dir = opendir(path);
+	if (!dir) {
+		giterr_set(GITERR_OS, "Couldn't open '%s'", path);
+		return false;
+	}
+
+	while ((e = readdir(dir)) != NULL) {
+		if (!is_dot_or_dotdot(e->d_name)) {
+			giterr_set(GITERR_INVALID,
+						  "'%s' exists and is not an empty directory", path);
+			retval = false;
+			break;
+		}
+	}
+	closedir(dir);
 #endif
 
-   return retval;
+	return retval;
 }
 
 
 static int clone_internal(git_repository **out,
-                          const char *origin_url,
-                          const char *path,
-                          git_indexer_stats *stats,
-                          int is_bare)
+								  const char *origin_url,
+								  const char *path,
+								  git_indexer_stats *stats,
+								  int is_bare)
 {
-   int retcode = GIT_ERROR;
-   git_repository *repo = NULL;
-
-   if (!path_is_okay(path)) {
-      return GIT_ERROR;
-   }
-
-   if (!(retcode = git_repository_init(&repo, path, is_bare))) {
-      if ((retcode = setup_remotes_and_fetch(repo, origin_url, stats)) < 0) {
-         /* Failed to fetch; clean up */
-         git_repository_free(repo);
-         git_futils_rmdir_r(path, GIT_DIRREMOVAL_FILES_AND_DIRS);
-      } else {
-         *out = repo;
-         retcode = 0;
-      }
-   }
-
-   return retcode;
+	int retcode = GIT_ERROR;
+	git_repository *repo = NULL;
+
+	if (!path_is_okay(path)) {
+		return GIT_ERROR;
+	}
+
+	if (!(retcode = git_repository_init(&repo, path, is_bare))) {
+		if ((retcode = setup_remotes_and_fetch(repo, origin_url, stats)) < 0) {
+			/* Failed to fetch; clean up */
+			git_repository_free(repo);
+			git_futils_rmdir_r(path, GIT_DIRREMOVAL_FILES_AND_DIRS);
+		} else {
+			*out = repo;
+			retcode = 0;
+		}
+	}
+
+	return retcode;
 }
 
 int git_clone_bare(git_repository **out,
-                   const char *origin_url,
-                   const char *dest_path,
-                   git_indexer_stats *stats)
+						 const char *origin_url,
+						 const char *dest_path,
+						 git_indexer_stats *stats)
 {
-   assert(out && origin_url && dest_path);
-   return clone_internal(out, origin_url, dest_path, stats, 1);
+	assert(out && origin_url && dest_path);
+	return clone_internal(out, origin_url, dest_path, stats, 1);
 }
 
 
 int git_clone(git_repository **out,
-              const char *origin_url,
-              const char *workdir_path,
-              git_indexer_stats *stats)
+				  const char *origin_url,
+				  const char *workdir_path,
+				  git_indexer_stats *stats)
 {
-   int retcode = GIT_ERROR;
+	int retcode = GIT_ERROR;
 
-   assert(out && origin_url && workdir_path);
+	assert(out && origin_url && workdir_path);
 
-   if (!(retcode = clone_internal(out, origin_url, workdir_path, stats, 0))) {
-      git_indexer_stats checkout_stats;
-      retcode = git_checkout_force(*out, &checkout_stats);
-   }
+	if (!(retcode = clone_internal(out, origin_url, workdir_path, stats, 0))) {
+		git_indexer_stats checkout_stats;
+		retcode = git_checkout_force(*out, &checkout_stats);
+	}
 
-   return retcode;
+	return retcode;
 }