]> git.proxmox.com Git - libgit2.git/commitdiff
revwalk: add convenience function to push/hide HEAD
authorCarlos Martín Nieto <carlos@cmartin.tk>
Mon, 27 Feb 2012 21:22:45 +0000 (22:22 +0100)
committerCarlos Martín Nieto <carlos@cmartin.tk>
Mon, 27 Feb 2012 21:26:37 +0000 (22:26 +0100)
It's not unusual to want the walker to act on HEAD, so add a
convencience function for the case that the user doesn't already have
a resolved HEAD reference.

include/git2/revwalk.h
src/revwalk.c
tests-clar/revwalk/basic.c

index 020c898ca627b598e14cda37ba990a0e6f0be229..e7ec2abf35d4951d2f49b1a9b97ad06b2fa97e5b 100644 (file)
@@ -116,6 +116,14 @@ GIT_EXTERN(int) git_revwalk_push(git_revwalk *walk, const git_oid *oid);
  */
 GIT_EXTERN(int) git_revwalk_push_glob(git_revwalk *walk, const char *glob);
 
+/**
+ * Push the repository's HEAD
+ *
+ * @param walk the walker being used for the traversal
+ * @return GIT_SUCCESS or an error code
+ */
+GIT_EXTERN(int) git_revwalk_push_head(git_revwalk *walk);
+
 /**
  * Mark a commit (and its ancestors) uninteresting for the output.
  *
@@ -147,6 +155,14 @@ GIT_EXTERN(int) git_revwalk_hide(git_revwalk *walk, const git_oid *oid);
  */
 GIT_EXTERN(int) git_revwalk_hide_glob(git_revwalk *walk, const char *glob);
 
+/**
+ * Hide the repository's HEAD
+ *
+ * @param walk the walker being used for the traversal
+ * @return GIT_SUCCESS or an error code
+ */
+GIT_EXTERN(int) git_revwalk_hide_head(git_revwalk *walk);
+
 /**
  * Get the next commit from the revision walk.
  *
index 8f818b81483aa48453ae2afeb8ec81917110c946..cd971b5d9c3bb7da284b9d8985ab4758d23a3924 100644 (file)
@@ -391,6 +391,39 @@ int git_revwalk_hide_glob(git_revwalk *walk, const char *glob)
        return push_glob(walk, glob, 1);
 }
 
+static int push_head(git_revwalk *walk, int hide)
+{
+       git_reference *ref, *resolved;
+       int error;
+
+       error = git_reference_lookup(&ref, walk->repo, "HEAD");
+       if (error < GIT_SUCCESS) {
+               return error;
+       }
+       error = git_reference_resolve(&resolved, ref);
+       if (error < GIT_SUCCESS) {
+               return error;
+       }
+       git_reference_free(ref);
+
+       error  = push_commit(walk, git_reference_oid(resolved), hide);
+
+       git_reference_free(resolved);
+       return error;
+}
+
+int git_revwalk_push_head(git_revwalk *walk)
+{
+       assert(walk);
+       return push_head(walk, 0);
+}
+
+int git_revwalk_hide_head(git_revwalk *walk)
+{
+       assert(walk);
+       return push_head(walk, 1);
+}
+
 static int revwalk_enqueue_timesort(git_revwalk *walk, commit_object *commit)
 {
        return git_pqueue_insert(&walk->iterator_time, commit);
index f013945ff90ab839f0950ad45fbbb16dd4a82b51..fff93ec9326c143d48ceefc84b830bd9a04b0b56 100644 (file)
@@ -132,3 +132,18 @@ void test_revwalk_basic__glob_heads(void)
        /* git log --branches --oneline | wc -l => 13 */
        cl_assert(i == 13);
 }
+
+void test_revwalk_basic__push_head(void)
+{
+       int i = 0;
+       git_oid oid;
+
+       cl_git_pass(git_revwalk_push_head(_walk));
+
+       while (git_revwalk_next(&oid, _walk) == GIT_SUCCESS) {
+               i++;
+       }
+
+       /* git log HEAD --oneline | wc -l => 7 */
+       cl_assert(i == 7);
+}