Commit 65b09b1d by Russell Belfer

Implement diff lists and formatters

This reworks the diff API to separate the steps of producing
a diff descriptions from formatting the diff.  This will allow
us to share diff output code with the various diff creation
scenarios and will allow us to implement rename detection as
an optional pass that can be run on a diff list.
parent cd33323b
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "git2/commit.h" #include "git2/commit.h"
#include "git2/tag.h" #include "git2/tag.h"
#include "git2/tree.h" #include "git2/tree.h"
#include "git2/diff.h"
#include "git2/index.h" #include "git2/index.h"
#include "git2/config.h" #include "git2/config.h"
......
...@@ -21,81 +21,126 @@ ...@@ -21,81 +21,126 @@
*/ */
GIT_BEGIN_DECL GIT_BEGIN_DECL
typedef struct {
int context_lines;
int interhunk_lines;
int ignore_whitespace;
int force_text;
git_strarray pathspec;
} git_diff_options;
typedef struct {
git_status_t status; /* value from tree.h */
unsigned int old_attr;
unsigned int new_attr;
git_oid old_oid;
git_oid new_oid;
git_blob *old_blob;
git_blob *new_blob;
const char *path;
const char *new_path; /* NULL unless status is RENAMED or COPIED */
int similarity; /* value from 0 to 100 */
int binary; /* diff as binary? */
} git_diff_delta;
typedef int (*git_diff_file_fn)( typedef int (*git_diff_file_fn)(
void *cb_data, void *cb_data,
const git_oid *old, git_diff_delta *delta,
const char *old_path, float progress);
int old_mode,
const git_oid *new, /* hashed object if from work tree */ typedef struct {
const char *new_path, int old_start;
int new_mode); int old_lines;
int new_start;
int new_lines;
} git_diff_range;
typedef int (*git_diff_hunk_fn)( typedef int (*git_diff_hunk_fn)(
void *cb_data, void *cb_data,
int old_start, git_diff_delta *delta,
int old_lines, git_diff_range *range,
int new_start, const char *header,
int new_lines); size_t header_len);
#define GIT_DIFF_LINE_CONTEXT 0 #define GIT_DIFF_LINE_CONTEXT ' '
#define GIT_DIFF_LINE_ADDITION 1 #define GIT_DIFF_LINE_ADDITION '+'
#define GIT_DIFF_LINE_DELETION 2 #define GIT_DIFF_LINE_DELETION '-'
#define GIT_DIFF_LINE_ADD_EOFNL '\n'
#define GIT_DIFF_LINE_DEL_EOFNL '\0'
typedef int (*git_diff_line_fn)( typedef int (*git_diff_line_fn)(
void *cb_data, void *cb_data,
int origin, /* GIT_DIFF_LINE value from above */ git_diff_delta *delta,
char line_origin, /* GIT_DIFF_LINE value from above */
const char *content, const char *content,
size_t content_len); size_t content_len);
typedef struct { typedef struct git_diff_list git_diff_list;
int context_lines;
int interhunk_lines;
int ignore_whitespace;
git_diff_file_fn file_cb;
git_diff_hunk_fn hunk_cb;
git_diff_line_fn line_cb;
void *cb_data;
} git_diff_opts;
GIT_EXTERN(int) git_diff_blobs( /*
git_repository *repo, * Generate diff lists
git_blob *old, */
git_blob *new,
git_diff_opts *options);
GIT_EXTERN(int) git_diff_trees( GIT_EXTERN(int) git_diff_tree_to_tree(
git_repository *repo, git_repository *repo,
const git_diff_options *opts,
git_tree *old, git_tree *old,
git_tree *new, git_tree *new,
git_diff_opts *options); git_diff_list **diff);
GIT_EXTERN(int) git_diff_index( GIT_EXTERN(int) git_diff_index_to_tree(
git_repository *repo, git_repository *repo,
const git_diff_options *opts,
git_tree *old, git_tree *old,
git_diff_opts *options); git_diff_list **diff);
/* pass NULL for the git_tree to diff workdir against index */ GIT_EXTERN(int) git_diff_workdir_to_tree(
GIT_EXTERN(int) git_diff_workdir(
git_repository *repo, git_repository *repo,
const git_diff_options *opts,
git_tree *old, git_tree *old,
git_diff_opts *options); git_diff_list **diff);
GIT_EXTERN(int) git_diff_workdir_file( GIT_EXTERN(int) git_diff_workdir_to_index(
git_repository *repo, git_repository *repo,
git_blob *old, const git_diff_options *opts,
const char *path, git_diff_list **diff);
git_diff_opts *options);
/* pass git_objects to diff against or NULL for index. GIT_EXTERN(void) git_diff_list_free(git_diff_list *diff);
* can handle: blob->blob, tree->index, tree->tree
* it will be an error if object types don't match /*
* Process diff lists
*/ */
/* pass git_object to diff WT against or NULL for index
* can handle: index->wt, tree->wt, blob->wt with path GIT_EXTERN(int) git_diff_foreach(
git_diff_list *diff,
void *cb_data,
git_diff_file_fn file_cb,
git_diff_hunk_fn hunk_cb,
git_diff_line_fn line_cb);
#ifndef _STDIO_H_
#include <stdio.h>
#endif
GIT_EXTERN(int) git_diff_print_compact(
FILE *fp, git_diff_list *diff);
GIT_EXTERN(int) git_diff_print_patch(
FILE *fp, git_diff_list *diff);
/*
* Misc
*/ */
GIT_EXTERN(int) git_diff_blobs(
git_repository *repo,
git_blob *old,
git_blob *new,
git_diff_options *options,
void *cb_data,
git_diff_hunk_fn hunk_cb,
git_diff_line_fn line_cb);
GIT_END_DECL GIT_END_DECL
/** @} */ /** @} */
......
...@@ -316,9 +316,14 @@ GIT_EXTERN(int) git_tree_walk(git_tree *tree, git_treewalk_cb callback, int mode ...@@ -316,9 +316,14 @@ GIT_EXTERN(int) git_tree_walk(git_tree *tree, git_treewalk_cb callback, int mode
/** @} */ /** @} */
typedef enum { typedef enum {
GIT_STATUS_UNMODIFIED = 0,
GIT_STATUS_ADDED = 1, GIT_STATUS_ADDED = 1,
GIT_STATUS_DELETED = 2, GIT_STATUS_DELETED = 2,
GIT_STATUS_MODIFIED = 3, GIT_STATUS_MODIFIED = 3,
GIT_STATUS_RENAMED = 4,
GIT_STATUS_COPIED = 5,
GIT_STATUS_IGNORED = 6,
GIT_STATUS_UNTRACKED = 7
} git_status_t; } git_status_t;
typedef struct { typedef struct {
......
/*
* Copyright (C) 2009-2012 the libgit2 contributors
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_diff_h__
#define INCLUDE_diff_h__
#include <stdio.h>
#include "vector.h"
#include "buffer.h"
struct git_diff_list {
git_repository *repo;
git_diff_options opts;
git_buf pfx;
git_vector files; /* vector of git_diff_file_delta */
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment