git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
* Refresh index without discard_index + repo_read_index
@ 2019-03-09 19:59 Roman Perepelitsa
  2019-03-11 16:00 ` Roman Perepelitsa
  0 siblings, 1 reply; 2+ messages in thread
From: Roman Perepelitsa @ 2019-03-09 19:59 UTC (permalink / raw)
  To: git

I’m writing a program that reads stdin in a loop and prints the
equivalent of `git status` whenever it reads a character. It always
prints its results for the same repository, the same pathspec, etc.
The input characters have no effect on the output, only the sate of
the repository does.
I’m hoping that I can make it produce results faster than a bash
script that literally calls `git status` in a loop. I’m thinking that
maybe I can keep some caches around so that I don’t have to redo all
the work on every iteration that git status does.

What I cannot figure out is how to refresh the index so that it picks
up all the changes that might have happened to the repository and
workdir since the last iteration. Here’s what I have:

```c
#include <stdio.h>
#include <string.h>

#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h"
#include "wt-status.h"
#include "pathspec.h"
#include "repository.h"

int cmd_multi_status(int argc, const char** argv, const char* prefix) {
  struct pathspec pathspec;
  memset(&pathspec, 0, sizeof(pathspec));
  repo_read_index(the_repository);
  while (getchar() != EOF) {
    refresh_index(&the_index,
                  REFRESH_QUIET | REFRESH_UNMERGED | REFRESH_REALLY,
                  &pathspec, NULL, NULL);
    int uncommitted = has_uncommitted_changes(the_repository, 1);
    int unstaged = has_unstaged_changes(the_repository, 1);
    int untracked = has_untracked(the_repository);  // I added this to
wt-status.h
    printf("%d %d %d\n", uncommitted, unstaged, untracked);
  }
  return 0;
}
```

This produces correct results on the first iteration but then it
doesn’t pick up all changes. I’ve hacked some code in preload-index.c
and cache.c so that it picks up more changes but I still cannot detect
when an unstaged file becomes uncommitted or the other way around.

Any pointers would be greatly appreciated.

Roman.

P.S.

I have working code that uses libgit2. Updating the index with
`git_index_read(index, 0)` works as expected and is indeed faster than
discarding the index and starting from scratch. But it's still slower
than `git status` on large repositories, especially with many ignored
files.

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

* Re: Refresh index without discard_index + repo_read_index
  2019-03-09 19:59 Refresh index without discard_index + repo_read_index Roman Perepelitsa
@ 2019-03-11 16:00 ` Roman Perepelitsa
  0 siblings, 0 replies; 2+ messages in thread
From: Roman Perepelitsa @ 2019-03-11 16:00 UTC (permalink / raw)
  To: git

In the end I managed to patch diffing code in libgit2
(git_diff_tree_to_index and git_diff_index_to_workdir) to make it
about 4x faster. It's now faster than `git status`, so good enough for
me.

I'm still curious whether it's possible to refresh index in git (not
libgit2), so please chime if if you know.

Roman.

On Sat, Mar 9, 2019 at 8:59 PM Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
>
> I’m writing a program that reads stdin in a loop and prints the
> equivalent of `git status` whenever it reads a character. It always
> prints its results for the same repository, the same pathspec, etc.
> The input characters have no effect on the output, only the sate of
> the repository does.
> I’m hoping that I can make it produce results faster than a bash
> script that literally calls `git status` in a loop. I’m thinking that
> maybe I can keep some caches around so that I don’t have to redo all
> the work on every iteration that git status does.
>
> What I cannot figure out is how to refresh the index so that it picks
> up all the changes that might have happened to the repository and
> workdir since the last iteration. Here’s what I have:
>
> ```c
> #include <stdio.h>
> #include <string.h>
>
> #define USE_THE_INDEX_COMPATIBILITY_MACROS
> #include "cache.h"
> #include "wt-status.h"
> #include "pathspec.h"
> #include "repository.h"
>
> int cmd_multi_status(int argc, const char** argv, const char* prefix) {
>   struct pathspec pathspec;
>   memset(&pathspec, 0, sizeof(pathspec));
>   repo_read_index(the_repository);
>   while (getchar() != EOF) {
>     refresh_index(&the_index,
>                   REFRESH_QUIET | REFRESH_UNMERGED | REFRESH_REALLY,
>                   &pathspec, NULL, NULL);
>     int uncommitted = has_uncommitted_changes(the_repository, 1);
>     int unstaged = has_unstaged_changes(the_repository, 1);
>     int untracked = has_untracked(the_repository);  // I added this to
> wt-status.h
>     printf("%d %d %d\n", uncommitted, unstaged, untracked);
>   }
>   return 0;
> }
> ```
>
> This produces correct results on the first iteration but then it
> doesn’t pick up all changes. I’ve hacked some code in preload-index.c
> and cache.c so that it picks up more changes but I still cannot detect
> when an unstaged file becomes uncommitted or the other way around.
>
> Any pointers would be greatly appreciated.
>
> Roman.
>
> P.S.
>
> I have working code that uses libgit2. Updating the index with
> `git_index_read(index, 0)` works as expected and is indeed faster than
> discarding the index and starting from scratch. But it's still slower
> than `git status` on large repositories, especially with many ignored
> files.

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

end of thread, other threads:[~2019-03-11 16:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-09 19:59 Refresh index without discard_index + repo_read_index Roman Perepelitsa
2019-03-11 16:00 ` Roman Perepelitsa

git@vger.kernel.org list mirror (unofficial, one of many)

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 git git/ https://public-inbox.org/git \
		git@vger.kernel.org
	public-inbox-index git

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://ou63pmih66umazou.onion/inbox.comp.version-control.git
	nntp://czquwvybam4bgbro.onion/inbox.comp.version-control.git
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.version-control.git
	nntp://news.gmane.io/gmane.comp.version-control.git
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git