git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] git-add (-a|-u)  and -n support
@ 2007-10-14 10:26 Michael Witten
  2007-10-14 12:11 ` [PATCH] [BUG FIXED] " Michael Witten
  2007-10-15  4:20 ` [PATCH] " Jeff King
  0 siblings, 2 replies; 10+ messages in thread
From: Michael Witten @ 2007-10-14 10:26 UTC (permalink / raw)
  To: git

Hello,

The git-add command doesn't handle -n when using -u.

I fixed this and added -a for adding ALL files, not
just those below the current directory (just like
git-commit).

The patch is below, but you can also download it from
http://web.mit.edu/mfwitten/git/0001-git-add-now-understands-two- 
kinds-of-update.patch



 From acc846f5243d26a96aaf0bf1c4f04ecc021385a2 Mon Sep 17 00:00:00 2001
From: Michael Witten <mfwitten@mit.edu>
Date: Sun, 14 Oct 2007 06:13:20 -0400
Subject: [PATCH] git-add now understands two kinds of update:

  	-u: update as before
  	-a: update all as in a true 'git commit -a'

Also, -n works correctly now with the above options.

Signed-off-by: Michael Witten <mfwitten@mit.edu>
---
  builtin-add.c |   69 +++++++++++++++++++++++++++++++++++++ 
+-------------------
  1 files changed, 46 insertions(+), 23 deletions(-)

diff --git a/builtin-add.c b/builtin-add.c
index f9a6580..24887c7 100644
--- a/builtin-add.c
+++ b/builtin-add.c
@@ -13,10 +13,11 @@
  #include "commit.h"
  #include "revision.h"

+enum update_type {NONE, ALL, CURRENT_DIRECTORY};
+
  static const char builtin_add_usage[] =
  "git-add [-n] [-v] [-f] [--interactive | -i] [-u] [--refresh] [--]  
<filepattern>...";

-static int take_worktree_changes;
  static const char *excludes_file;

  static void prune_directory(struct dir_struct *dir, const char  
**pathspec, int prefix)
@@ -83,40 +84,57 @@ static void fill_directory(struct dir_struct  
*dir, const char **pathspec,
  static void update_callback(struct diff_queue_struct *q,
  			    struct diff_options *opt, void *cbdata)
  {
-	int i, verbose;
-
-	verbose = *((int *)cbdata);
+	int i;
+	
+	int* options   = (int*)cbdata;
+	int  verbose   = options[0];
+	int  show_only = options[1];
+	
  	for (i = 0; i < q->nr; i++) {
  		struct diff_filepair *p = q->queue[i];
  		const char *path = p->one->path;
-		switch (p->status) {
-		default:
-			die("unexpected diff status %c", p->status);
-		case DIFF_STATUS_UNMERGED:
-		case DIFF_STATUS_MODIFIED:
-		case DIFF_STATUS_TYPE_CHANGED:
-			add_file_to_cache(path, verbose);
-			break;
-		case DIFF_STATUS_DELETED:
-			remove_file_from_cache(path);
-			if (verbose)
-				printf("remove '%s'\n", path);
-			break;
+		
+		switch (p->status) {			
+			case DIFF_STATUS_UNMERGED:
+			case DIFF_STATUS_MODIFIED:
+			case DIFF_STATUS_TYPE_CHANGED:
+				if (show_only)
+					printf("add '%s'\n", path);
+				else
+					add_file_to_cache(path, verbose);
+				break;
+			
+			case DIFF_STATUS_DELETED:
+				if (show_only)
+					remove_file_from_cache(path);
+				if (verbose)
+					printf("remove '%s'\n", path);
+				break;
+			
+			default:
+				die("unexpected diff status %c", p->status);
  		}
  	}
  }

-static void update(int verbose, const char *prefix, const char **files)
+static void update(enum update_type type, int verbose, int show_only,
+                  const char *prefix, const char **files)
  {
  	struct rev_info rev;
+	int callback_options[] = {verbose, show_only};
+	
  	init_revisions(&rev, prefix);
  	setup_revisions(0, NULL, &rev, NULL);
-	rev.prune_data = get_pathspec(prefix, files);
+	
+	rev.prune_data = type == ALL ? NULL : get_pathspec(prefix, files);
+	
  	rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
  	rev.diffopt.format_callback = update_callback;
-	rev.diffopt.format_callback_data = &verbose;
+	rev.diffopt.format_callback_data = callback_options;
+	
  	if (read_cache() < 0)
  		die("index file corrupt");
+	
  	run_diff_files(&rev, 0);
  }

@@ -158,6 +176,7 @@ int cmd_add(int argc, const char **argv, const  
char *prefix)
  {
  	int i, newfd;
  	int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
+	enum update_type update_type = NONE;
  	const char **pathspec;
  	struct dir_struct dir;
  	int add_interactive = 0;
@@ -201,8 +220,12 @@ int cmd_add(int argc, const char **argv, const  
char *prefix)
  			verbose = 1;
  			continue;
  		}
+		if (!strcmp(arg, "-a")) {
+			update_type = ALL;
+			continue;
+		}
  		if (!strcmp(arg, "-u")) {
-			take_worktree_changes = 1;
+			update_type = CURRENT_DIRECTORY;
  			continue;
  		}
  		if (!strcmp(arg, "--refresh")) {
@@ -212,8 +235,8 @@ int cmd_add(int argc, const char **argv, const  
char *prefix)
  		usage(builtin_add_usage);
  	}

-	if (take_worktree_changes) {
-		update(verbose, prefix, argv + i);
+	if (update_type) {
+		update(update_type, verbose, show_only, prefix, argv + i);
  		goto finish;
  	}

-- 
1.5.3.4.206.g58ba4-dirty

^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2007-10-15 14:54 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-14 10:26 [PATCH] git-add (-a|-u) and -n support Michael Witten
2007-10-14 12:11 ` [PATCH] [BUG FIXED] " Michael Witten
2007-10-14 12:24   ` [PATCH] [BUG FIXED 2] " Michael Witten
2007-10-14 13:25     ` Matthieu Moy
2007-10-14 22:00       ` Michael Witten
2007-10-15 14:31         ` Karl Hasselström
2007-10-15 14:47           ` Michael Witten
2007-10-15 14:54             ` Johannes Schindelin
2007-10-15  4:20 ` [PATCH] " Jeff King
2007-10-15  9:09   ` Michael Witten

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).