commit 80e3b5c5e0a1ed5d127c6b25a36a8dbb930bfa08
parent e0636a2651927691ff939d8cddfc79457cf36b12
Author: Mohamed Aslan <maslan@sce.carleton.ca>
Date: Thu, 2 Oct 2014 23:52:04 -0600
add support for recursively listing the content of directories via -R flag in cmd-ls
Diffstat:
M | baseline.1 | | | 6 | ++++-- |
M | cmd-ls.c | | | 55 | ++++++++++++++++++++++++++++++++++++++++++++++--------- |
2 files changed, 50 insertions(+), 11 deletions(-)
diff --git a/baseline.1 b/baseline.1
@@ -27,7 +27,7 @@
.Op Cm help
.Op Cm init
.Op Cm log
-.Op Cm ls
+.Op Cm ls Fl R
.Op Cm version
.Sh DESCRIPTION
The
@@ -86,8 +86,10 @@ otherwise, baseline will complain about missing commit message.
To list all commits:
.Dl $ baseline log
.Pp
-To list files or directories:
+To list files and directories:
.Dl $ baseline ls
+To recursively list files and directories:
+.Dl $ baseline ls -R
.Pp
To get the content of a certain file written to the stdout:
.Dl $ baseline cat </path/to/file>
diff --git a/cmd-ls.c b/cmd-ls.c
@@ -17,6 +17,7 @@
#include <stdio.h> /* printf(3) */
#include <stdlib.h> /* EXIT_SUCCESS */
#include <string.h> /* strdup(3) */
+#include <unistd.h> /* getopt(3) */
#include <sys/stat.h> /* S_ISDIR */
#include "cmd.h"
@@ -25,17 +26,60 @@
#include "objects.h"
+void
+ls(struct session *s, struct dir* d, const char *prefix, int is_recursive)
+{
+ char *nextprefix = NULL;
+ struct dirent *ent;
+ struct dir *child;
+
+ if (d == NULL)
+ return;
+ ent = d->children;
+ while(ent != NULL) {
+ if (S_ISDIR(ent->mode)) {
+ printf("%s%s/\n", prefix, ent->name);
+ if (is_recursive) {
+ asprintf(&nextprefix, "%s%s/", prefix, ent->name);
+ child = baseline_dir_new();
+ s->db_ops->select_dir(s->db_ctx, ent->id, child);
+ ls(s, child, nextprefix, is_recursive);
+ baseline_dir_free(child);
+ free(nextprefix);
+ }
+ }
+ else {
+ printf("%s%s\n", prefix, ent->name);
+ }
+ ent = ent->next;
+ }
+}
+
int
cmd_ls(int argc, char **argv)
{
+ char ch;
char *head;
+ int recursive = 0;
struct session s;
struct commit *comm;
struct dir *dir;
- struct dirent *ent;
baseline_session_begin(&s, 0);
+ /* parse command line options */
+ while ((ch = getopt(argc, argv, "R")) != -1) {
+ switch (ch) {
+ case 'R':
+ recursive = 1;
+ break;
+ default:
+ exit(EXIT_FAILURE);
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
s.db_ops->branch_get_head(s.db_ctx, s.branch, &head);
if (head == NULL)
goto ret;
@@ -47,14 +91,7 @@ cmd_ls(int argc, char **argv)
s.db_ops->select_dir(s.db_ctx, comm->dir, dir);
printf("commit: %s\n", comm->id);
- ent = dir->children;
- while (ent != NULL) {
- if (S_ISDIR(ent->mode))
- printf("%s/\n", ent->name);
- else
- printf("%s\n", ent->name);
- ent = ent->next;
- }
+ ls(&s, dir, "", recursive);
baseline_dir_free(dir);