/**
 * repo.c
 *
 * Copyright (C) 2017 Nickolas Burr <nickolasburr@gmail.com>
 */
#include "repo.h"

/**
 * Get all stash entries for a repository.
 */
struct stash *get_stash (struct repo *r) {
	return r->stash;
}

/**
 * Set a copy of all stash entries for a repository.
 */
void set_stash (struct repo *r) {
	char *cmd;
	FILE *fp;

	// Allocate space for command string
	cmd = ALLOC(sizeof(char) * (strlen(r->path) + 100));

	sprintf(cmd, "/usr/bin/git -C %s stash list", r->path);
	printf("set_stashes -> cmd -> %s\n", cmd);

	fp = popen(cmd, "r");

	if (!fp) {
		printf("Could not open pipe!\n");

		exit(EXIT_FAILURE);
	}

	while (fgets(r->stash->entries, (sizeof(r->stash->entries) - 1), fp) != NULL) {
		printf("%s\n", r->stash->entries);
	}

	// Free space allocated for commnad string
	FREE(cmd);
	pclose(fp);
}


/**
 * Check if the worktree has changed since the last check.
 */
int has_worktree_changed (struct repo *r) {
	return 1;
}

/**
 * repo.c
 *
 * Copyright (C) 2017 Nickolas Burr <nickolasburr@gmail.com>
 */

#include "repo.h"

/**
 * Get all stashes for a repository.
 */
struct stash *get_stashes (struct repo *r) {
	return r->stashes;
}

/**
 * Set a copy of all stashes for a repository in memory.
 */
void set_stashes (struct repo *r) {
	// @todo: Build this function out.
	char *cmd;
	FILE *fp;

	printf("set_stashes -> r->path -> %s\n", r->path);

	sprintf(cmd, "/usr/bin/git -C %s -- stash list", r->path);

	fp = popen(cmd, "r");

	if (!fp) {
		printf("Could not open pipe!\n");

		exit(EXIT_FAILURE);
	}

	while (fgets(r->stashes->entries, (sizeof(r->stashes->entries) - 1), fp) != NULL) {
		printf("%s\n", r->stashes->entries);
	}

	pclose(fp);
}


/**
 * Check if the worktree has changed since our last check.
 */
int has_worktree_changed (struct repo *r) {
	return 1;
}

--- 
+++ 
@@ -3,27 +3,27 @@
  *
  * Copyright (C) 2017 Nickolas Burr <nickolasburr@gmail.com>
  */
-
 #include "repo.h"
 
 /**
- * Get all stashes for a repository.
+ * Get all stash entries for a repository.
  */
-struct stash *get_stashes (struct repo *r) {
-	return r->stashes;
+struct stash *get_stash (struct repo *r) {
+	return r->stash;
 }
 
 /**
- * Set a copy of all stashes for a repository in memory.
+ * Set a copy of all stash entries for a repository.
  */
-void set_stashes (struct repo *r) {
-	// @todo: Build this function out.
+void set_stash (struct repo *r) {
 	char *cmd;
 	FILE *fp;
 
-	printf("set_stashes -> r->path -> %s\n", r->path);
+	// Allocate space for command string
+	cmd = ALLOC(sizeof(char) * (strlen(r->path) + 100));
 
-	sprintf(cmd, "/usr/bin/git -C %s -- stash list", r->path);
+	sprintf(cmd, "/usr/bin/git -C %s stash list", r->path);
+	printf("set_stashes -> cmd -> %s\n", cmd);
 
 	fp = popen(cmd, "r");
 
@@ -33,16 +33,18 @@
 		exit(EXIT_FAILURE);
 	}
 
-	while (fgets(r->stashes->entries, (sizeof(r->stashes->entries) - 1), fp) != NULL) {
-		printf("%s\n", r->stashes->entries);
+	while (fgets(r->stash->entries, (sizeof(r->stash->entries) - 1), fp) != NULL) {
+		printf("%s\n", r->stash->entries);
 	}
 
+	// Free space allocated for commnad string
+	FREE(cmd);
 	pclose(fp);
 }
 
 
 /**
- * Check if the worktree has changed since our last check.
+ * Check if the worktree has changed since the last check.
  */
 int has_worktree_changed (struct repo *r) {
 	return 1;
Work on stash retrieval, memory allocation
