Commit 92a684f46395bfe5077629585a4d140f6853f06d

Stefan Sperling 2018-03-12T21:34:38

show progress during checkout

diff --git a/got/got.c b/got/got.c
index 8d6ac0c..f0cb1f8 100644
--- a/got/got.c
+++ b/got/got.c
@@ -135,6 +135,17 @@ usage_checkout(void)
 	exit(1);
 }
 
+static void
+checkout_progress(void *arg, const char *path)
+{
+	char *worktree_path = arg;
+
+	while (path[0] == '/')
+		path++;
+
+	printf("A  %s/%s\n", worktree_path, path);
+}
+
 const struct got_error *
 cmd_checkout(int argc, char *argv[])
 {
@@ -173,8 +184,6 @@ cmd_checkout(int argc, char *argv[])
 	} else
 		usage_checkout();
 
-	printf("%s %s %s %s\n", getprogname(), argv[0], repo_path, worktree_path);
-
 	error = got_repo_open(&repo, repo_path);
 	if (error != NULL)
 		goto done;
@@ -190,7 +199,8 @@ cmd_checkout(int argc, char *argv[])
 	if (error != NULL)
 		goto done;
 
-	error = got_worktree_checkout_files(worktree, head_ref, repo);
+	error = got_worktree_checkout_files(worktree, head_ref, repo,
+	    checkout_progress, worktree_path);
 	if (error != NULL)
 		goto done;
 
diff --git a/include/got_worktree.h b/include/got_worktree.h
index 3bfc191..9031acf 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -22,5 +22,7 @@ const struct got_error *got_worktree_open(struct got_worktree **, const char *);
 void got_worktree_close(struct got_worktree *);
 char *got_worktree_get_repo_path(struct got_worktree *);
 char  *got_worktree_get_head_ref_name(struct got_worktree *);
+typedef void (*got_worktree_checkout_cb)(void *, const char *);
 const struct got_error *got_worktree_checkout_files(struct got_worktree *,
-    struct got_reference *, struct got_repository *);
+    struct got_reference *, struct got_repository *,
+    got_worktree_checkout_cb progress_cb, void *progress_arg);
diff --git a/lib/worktree.c b/lib/worktree.c
index 1273d61..0df2fb5 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -463,12 +463,14 @@ done:
 
 static const struct got_error *
 tree_checkout(struct got_worktree *, struct got_fileindex *,
-    struct got_tree_object *, const char *, struct got_repository *);
+    struct got_tree_object *, const char *, struct got_repository *,
+    got_worktree_checkout_cb progress_cb, void *progress_arg);
 
 static const struct got_error *
 tree_checkout_entry(struct got_worktree *worktree,
     struct got_fileindex *fileindex, struct got_tree_entry *te,
-    const char *parent, struct got_repository *repo)
+    const char *parent, struct got_repository *repo,
+    got_worktree_checkout_cb progress_cb, void *progress_arg)
 {
 	const struct got_error *err = NULL;
 	struct got_object *obj = NULL;
@@ -493,6 +495,8 @@ tree_checkout_entry(struct got_worktree *worktree,
 	if (err)
 		goto done;
 
+	(*progress_cb)(progress_arg, path);
+
 	switch (got_object_get_type(obj)) {
 	case GOT_OBJ_TYPE_BLOB:
 		if (strlen(worktree->path_prefix) >= strlen(path))
@@ -511,7 +515,8 @@ tree_checkout_entry(struct got_worktree *worktree,
 			if (err)
 				break;
 		}
-		err = tree_checkout(worktree, fileindex, tree, path, repo);
+		err = tree_checkout(worktree, fileindex, tree, path, repo,
+		    progress_cb, progress_arg);
 		break;
 	default:
 		break;
@@ -531,7 +536,8 @@ done:
 static const struct got_error *
 tree_checkout(struct got_worktree *worktree,
     struct got_fileindex *fileindex, struct got_tree_object *tree,
-    const char *path, struct got_repository *repo)
+    const char *path, struct got_repository *repo,
+    got_worktree_checkout_cb progress_cb, void *progress_arg)
 {
 	const struct got_error *err = NULL;
 	struct got_tree_entry *te;
@@ -543,7 +549,8 @@ tree_checkout(struct got_worktree *worktree,
 		return NULL;
 
 	SIMPLEQ_FOREACH(te, &tree->entries, entry) {
-		err = tree_checkout_entry(worktree, fileindex, te, path, repo);
+		err = tree_checkout_entry(worktree, fileindex, te, path, repo,
+		    progress_cb, progress_arg);
 		if (err)
 			break;
 	}
@@ -553,7 +560,8 @@ tree_checkout(struct got_worktree *worktree,
 
 const struct got_error *
 got_worktree_checkout_files(struct got_worktree *worktree,
-    struct got_reference *head_ref, struct got_repository *repo)
+    struct got_reference *head_ref, struct got_repository *repo,
+    got_worktree_checkout_cb progress_cb, void *progress_arg)
 {
 	const struct got_error *err = NULL, *unlockerr;
 	struct got_object_id *commit_id = NULL;
@@ -616,7 +624,8 @@ got_worktree_checkout_files(struct got_worktree *worktree,
 	if (err)
 		goto done;
 
-	err = tree_checkout(worktree, fileindex, tree, "/", repo);
+	err = tree_checkout(worktree, fileindex, tree, "/", repo,
+	    progress_cb, progress_arg);
 	if (err)
 		goto done;