예제 #1
0
        /// <summary>
        /// Fast forwarding means making the
        /// current branch reflect the commit that `giverHash` points at.  No
        /// new commit is created.
        /// </summary>
        public static void WriteFastForwardMerge(string receiverHash, string giverHash)
        {
            // Point head at `giverHash`.
            Refs.Write(Refs.ToLocalRef(Refs.HeadBranchName()), giverHash);

            // Make the index mirror the content of `giverHash`.
            Index.Write(Index.TocToIndex(Objects.CommitToc(giverHash)));

            // If the repo is bare, it has no working copy, so there is no
            // more work to do.  If the repo is not bare...
            if (!Config.Read().Bare)
            {
                // ...Get an object that maps from file paths in the
                // `receiverHash` commit to hashes of the files' content.  If
                // `recevierHash` is undefined, the repository has no commits,
                // yet, and the mapping object is empty.
                var receiverToc =
                    receiverHash == null
                        ? new Dictionary <string, string>()
                        : Objects.CommitToc(receiverHash);

                // ...and write the content of the files to the working copy.
                WorkingCopy.Write(Diff.TocDiff(receiverToc, Objects.CommitToc(giverHash)));
            }
        }
예제 #2
0
        public static string Hash(string refOrHash)
        {
            /*if (objects.exists(refOrHash)) {
             *  return refOrHash;
             * } else {
             *  var terminalRef = refs.terminalRef(refOrHash);
             *  if (terminalRef === "FETCH_HEAD") {
             *      return refs.fetchHeadBranchToMerge(refs.headBranchName());
             *  } else if (refs.exists(terminalRef)) {
             *      return files.read(files.gitletPath(terminalRef));
             *  }
             * }*/

            if (Objects.Exists(refOrHash))
            {
                return(refOrHash);
            }

            var terminalRef = Refs.TerminalRef(refOrHash);

            if (terminalRef == "FETCH_HEAD")
            {
                return(Refs.FetchHeadBranchToMerge(Refs.HeadBranchName()));
            }

            if (Refs.Exists(terminalRef))
            {
                return(Files.Read(Path.Combine(Files.GitletPath(), terminalRef)));
            }

            return(null);
        }
예제 #3
0
 public static void Write(string @ref, string content)
 {
     if (Refs.IsRef(@ref))
     {
         Files.Write(Path.Combine(Files.GitletPath(), @ref), content);
     }
 }
예제 #4
0
 public static void Rm(string @ref)
 {
     if (Refs.IsRef(@ref))
     {
         File.Delete(Path.Combine(Files.GitletPath(), @ref));
     }
 }
예제 #5
0
        private static string[] ToBeCommitted()
        {
            var headHash = Refs.Hash("HEAD");
            var headToc  = headHash == null ? new Dictionary <string, string>() : Objects.CommitToc(headHash);
            var ns       = Diff.NameStatus(Diff.TocDiff(headToc, Index.Toc()));

            return(ns.Select(item => item.Value.Value + " " + item.Key).ToArray());
        }
예제 #6
0
        public static string[] AddedOrModifiedFiles()
        {
            var headToc = Refs.Hash("HEAD") != null?Objects.CommitToc(Refs.Hash("HEAD")) : new Dictionary <string, string>();

            var wc = Diff.NameStatus(Diff.TocDiff(headToc, Index.WorkingCopyToc()));

            return(wc.Where(item => item.Value != FileStatus.DELETE).Select(item => item.Key).ToArray());
        }
예제 #7
0
        /// <summary>
        /// Gets a list of files
        /// changed in the working copy.  It gets a list of the files that
        /// are different in the head commit and the commit for the passed
        /// hash.  It returns a list of paths that appear in both lists.
        /// </summary>
        public static string[] ChangedFilesCommitWouldOverwrite(string hash)
        {
            var headHash = Refs.Hash("HEAD");

            return
                (Diff.NameStatus(Diff.GetDiff(headHash)).Keys
                 .Intersect(Diff.NameStatus(Diff.GetDiff(headHash, hash)).Keys)
                 .ToArray());
        }
예제 #8
0
        public static string Get()
        {
            var lines =
                new[] { "On branch " + Refs.HeadBranchName() }
            .Concat(Listing("Untracked files: ", Untracked()))
            .Concat(Listing("Unmerged paths: ", Index.ConflictedPaths()))
            .Concat(Listing("Changes to be committed: ", ToBeCommitted()))
            .Concat(Listing("Changes not staged for commit: ", NotStagedForCommit()));

            return(string.Join("\n", lines));
        }
예제 #9
0
        /// <summary>
        /// creates a message for the merge commit that
        /// will potentially be created when the `giverHash` commit is merged
        /// into the `receiverHash` commit.  It writes this message to
        /// `.gitlet/MERGE_MSG`.
        /// </summary>
        private static void WriteMergeMsg(string receiverHash, string giverHash, string @ref)
        {
            var msg = "Merge " + @ref + " into " + Refs.HeadBranchName();

            var mergeDiff = Merge.MergeDiff(receiverHash, giverHash);
            var conflicts = mergeDiff.Where(item => item.Value.Status == Diff.FileStatus.CONFLICT).ToList();

            if (conflicts.Any())
            {
                msg += "\nConflicts:\n" + string.Join("\n", conflicts);
            }

            Files.Write(Path.Combine(Files.GitletPath(), "MERGE_MSG"), msg);
        }
예제 #10
0
        public static string[] CommitParentHashes()
        {
            var headHash = Refs.Hash("HEAD");

            // If the repository is in the middle of a merge, return the
            // hashes of the two commits being merged.
            if (Merge.IsMergeInProgress())
            {
                return(new[] { headHash, Refs.Hash("MERGE_HEAD") });
            }

            // If this repository has no commits, return an empty array.
            if (headHash == null)
            {
                return(new string[0]);
            }

            // Otherwise, return the hash of the commit that `HEAD` is
            // currently pointing at.

            return(new[] { headHash });
        }
예제 #11
0
        public static void WriteNonFastForwardMerge(string receiverHash, string giverHash, string giverRef)
        {
            // Write `giverHash` to `.gitlet/MERGE_HEAD`.  This file acts as a
            // record of `giverHash` and as the signal that the repository is
            // in the merging state.
            Refs.Write("MERGE_HEAD", giverHash);

            // Write a standard merge commit message that will be used when
            // the merge commit is created.
            Merge.WriteMergeMsg(receiverHash, giverHash, giverRef);

            // Merge the `receiverHash` commit with the `giverHash` commit and
            // write the content to the index.
            Merge.WriteIndex(receiverHash, giverHash);

            // If the repo is bare, it has no working copy, so there is no
            // more work to do.  If the repo is not bare...
            if (!Config.Read().Bare)
            {
                // ...merge the `receiverHash` commit with the `giverHash`
                // commit and write the content to the working copy.
                WorkingCopy.Write(Merge.MergeDiff(receiverHash, giverHash));
            }
        }
예제 #12
0
 /// <summary>
 /// returns true if the repository is in the middle of a merge.
 /// </summary>
 /// <returns></returns>
 public static bool IsMergeInProgress()
 {
     return(Refs.Hash("MERGE_HEAD") != null);
 }
예제 #13
0
 private static bool Exists(string @ref)
 {
     return(Refs.IsRef(@ref) && File.Exists(Path.Combine(Files.GitletPath(), @ref)));
 }